Esempio n. 1
0
double gauss4(double A1,double S1, /* S1-S4 are sigma^2 */
	     double A2,double S2,
	     double A3,double S3,
	     double A4,double S4,
	     double r)
{
  return gaus(A1,S1,r)+gaus(A2,S2,r)+gaus(A3,S3,r)+gaus(A4,S4,r);
}
Esempio n. 2
0
void MC_StepOld(
    VecCl &ph,
    double Phi0,
    int N,
    double MainHarm,
    int FurieDiscr,
    double FullTime,
    double StrongShort) {
    double gZero = 0, gSigma = 1;
    RndGaus gaus(gZero, gSigma);
    gaus.Init(-4 * gSigma, 4 * gSigma, 100, 1e-5);
    int NumJ = FurieDiscr;
    VecCl Phi_cos(NumJ), Phi_sin(NumJ);
    for(int k = 1; k <= NumJ; k++) {
        Phi_cos[k] = gaus.Rnd();
        Phi_sin[k] = gaus.Rnd();
    }
    double StrongCoef = StrongShort * sqr(2 * M_PI / FullTime);
    MainHarm *= 2 / FullTime;
    Phi_cos = Phi_cos * MainHarm;
    Phi_sin = Phi_sin * MainHarm;
    for(int k = 1; k <= N; k++) {
        ph[k] = Phi0;
        for(int j = 1; j <= NumJ; j++)
            ph[k] += (Phi_cos[j] * cos(M_PI * k * (j - 1) / N) +
                      Phi_sin[j] * sin(M_PI * k * (j - 1) / N)) /
                     (sqrt(max<double>(j - 1, 0.25)) * (1 + sqr(j) * StrongCoef));
    }
};
Esempio n. 3
0
int main(){
    /* We can use any function that implements the IntegrableFunction 
       interface, here just use a gaussian
     */
    Gaussian gaus(std::vector<double>(2, 0), std::vector<double>(2, 1));
    
    AnalyticPdf gausPdf(&gaus); // takes ownership of a copy of gaussian

    // The dimensionality of the pdf now matches the underlying function
    std::cout << "Pdf is " << gausPdf.GetNDims() << std::endl;

    // now we can call it directly
    std::cout << " gausPdf(0, 0) = "
              << gausPdf(std::vector<double>(2, 0)) << std::endl;

    // or use a data representation to query an event
    // this pdf picks out the 0th and 2nd observables in an event
    std::vector<size_t> relevantIndices;
    relevantIndices.push_back(0);
    relevantIndices.push_back(2);
    
    gausPdf.SetDataRep(DataRepresentation(relevantIndices));

    // fake event with ten observables in it, our pdf knows where to look
    EventData event(std::vector<double>(10, 0));
    std::cout << "Probability = "
              << gausPdf.Probability(event) << std::endl;

    return 0;
}
Esempio n. 4
0
/** Restore a circular beam*/
void ModelRenderer::Restore(double* imageData, size_t imageWidth, size_t imageHeight, const Model& model, long double beamSize, long double startFrequency, long double endFrequency, PolarizationEnum polarization)
{
	// Using the FWHM formula for a Gaussian:
	long double sigma = beamSize / (2.0L * sqrtl(2.0L * logl(2.0L)));
	
	int boundingBoxSize = ceil(sigma * 20.0 / std::min(_pixelScaleL, _pixelScaleM));
	for(Model::const_iterator src=model.begin(); src!=model.end(); ++src)
	{
		for(ModelSource::const_iterator comp=src->begin(); comp!=src->end(); ++comp)
		{
			long double
				posRA = comp->PosRA(),
				posDec = comp->PosDec(),
				sourceL, sourceM;
			ImageCoordinates::RaDecToLM(posRA, posDec, _phaseCentreRA, _phaseCentreDec, sourceL, sourceM);
			const SpectralEnergyDistribution &sed = comp->SED();
			const long double intFlux = sed.IntegratedFlux(startFrequency, endFrequency, polarization);
			
			//std::cout << "Source: " << comp->PosRA() << "," << comp->PosDec() << " Phase centre: " << _phaseCentreRA << "," << _phaseCentreDec << " beamsize: " << beamSize << "\n";
				
			int sourceX, sourceY;
			ImageCoordinates::LMToXY<long double>(sourceL-_phaseCentreDL, sourceM-_phaseCentreDM, _pixelScaleL, _pixelScaleM, imageWidth, imageHeight, sourceX, sourceY);
			//std::cout << "Adding source " << comp->Name() << " at " << sourceX << "," << sourceY << " of "
			//	<< intFlux << " Jy ("
			//	<< startFrequency/1000000.0 << "-" << endFrequency/1000000.0 << " MHz).\n";
			int
				xLeft = sourceX - boundingBoxSize,
				xRight = sourceX + boundingBoxSize,
				yTop = sourceY - boundingBoxSize,
				yBottom = sourceY + boundingBoxSize;
			if(xLeft < 0) xLeft = 0;
			if(xLeft > (int) imageWidth) xLeft = (int) imageWidth;
			if(xRight < xLeft) xRight = xLeft;
			if(xRight > (int) imageWidth) xRight = (int) imageWidth;
			if(yTop < 0) yTop = 0;
			if(yTop > (int) imageHeight) yTop = (int) imageHeight;
			if(yBottom < yTop) yBottom = yTop;
			if(yBottom > (int) imageHeight) yBottom = (int) imageHeight;
			
			for(int y=yTop; y!=yBottom; ++y)
			{
				double *imageDataPtr = imageData + y*imageWidth+xLeft;
				for(int x=xLeft; x!=xRight; ++x)
				{
					long double l, m;
					ImageCoordinates::XYToLM<long double>(x, y, _pixelScaleL, _pixelScaleM, imageWidth, imageHeight, l, m);
					l += _phaseCentreDL; m += _phaseCentreDM;
					long double dist = sqrt((l-sourceL)*(l-sourceL) + (m-sourceM)*(m-sourceM));
					long double g = gaus(dist, sigma);
					(*imageDataPtr) += double(g * intFlux);
					++imageDataPtr;
				}
			}
		}
	}
}
Esempio n. 5
0
/**
 * Restore a diffuse image (e.g. produced with multi-scale clean)
 */
void ModelRenderer::Restore(double* imageData, const double* modelData, size_t imageWidth, size_t imageHeight, long double beamMaj, long double beamMin, long double beamPA, long double pixelScaleL, long double pixelScaleM)
{
	if(beamMaj == 0.0 && beamMin == 0.0)
	{
		for(size_t j=0; j!=imageWidth*imageHeight; ++j)
			imageData[j] += modelData[j];
	}
	else {
		// Using the FWHM formula for a Gaussian:
		long double sigmaMaj = beamMaj / (2.0L * sqrtl(2.0L * logl(2.0L)));
		long double sigmaMin = beamMin / (2.0L * sqrtl(2.0L * logl(2.0L)));
		
		// Make rotation matrix
		long double transf[4];
		// Position angle is angle from North: 
		long double angle = beamPA+0.5*M_PI;
		transf[2] = sin(angle);
		transf[0] = cos(angle);
		transf[1] = -transf[2];
		transf[3] = transf[0];
		double sigmaMax = std::max(std::fabs(sigmaMaj * transf[0]), std::fabs(sigmaMaj * transf[1]));
		// Multiple with scaling matrix to make variance 1.
		transf[0] = transf[0] / sigmaMaj;
		transf[1] = transf[1] / sigmaMaj;
		transf[2] = transf[2] / sigmaMin;
		transf[3] = transf[3] / sigmaMin;
		
		size_t minDimension = std::min(imageWidth, imageHeight);
		size_t boundingBoxSize = std::min<size_t>(ceil(sigmaMax * 40.0 / std::min(pixelScaleL, pixelScaleM)), minDimension);
		if(boundingBoxSize%2!=0) ++boundingBoxSize;
		ao::uvector<double> kernel(boundingBoxSize*boundingBoxSize);
		typename ao::uvector<double>::iterator i=kernel.begin();
		for(size_t y=0; y!=boundingBoxSize; ++y)
		{
			for(size_t x=0; x!=boundingBoxSize; ++x)
			{
				long double l, m;
				ImageCoordinates::XYToLM<long double>(x, y, pixelScaleL, pixelScaleM, boundingBoxSize, boundingBoxSize, l, m);
				long double
					lTransf = l*transf[0] + m*transf[1],
					mTransf = l*transf[2] + m*transf[3];
				long double dist = sqrt(lTransf*lTransf + mTransf*mTransf);
				*i = gaus(dist, (long double) 1.0);
				++i;
			}
		}
		
		ao::uvector<double> convolvedModel(imageWidth*imageHeight);
		memcpy(convolvedModel.data(), modelData, sizeof(double)*imageWidth*imageHeight);
		
		FFTWManager fftw;
		FFTConvolver::Convolve(fftw, convolvedModel.data(), imageWidth, imageHeight, kernel.data(), boundingBoxSize);
		for(size_t j=0; j!=imageWidth*imageHeight; ++j)
			imageData[j] += convolvedModel[j];
	}
}
Esempio n. 6
0
void fix0Bins(TH1F* h){

  //determine if this is a low stats shape
  float binUnc=0.;
  int nNon0bins=0;
  for(Int_t i=1;i<=h->GetNbinsX();i++){
    if(h->GetBinContent(i)>0.){
      nNon0bins++;
      binUnc+=h->GetBinError(i)/h->GetBinContent(i);
    }
  }
  binUnc=binUnc/nNon0bins;

  //if the average bin uncertainty is greater than 30% then smooth
  if(binUnc>0.5){

    //TH1F* hs=new TH1F(TString(h->GetName())+"smeared",h->GetTitle(),h->GetXaxis()->GetNbins(),h->GetXaxis()->GetXmin(),h->GetXaxis()->GetXmax());
    TH1F* hs=h->Clone("hs");
    TF1 gaus("gauss","[0]*exp(-0.5*(x-[1])**2/[2]**2)",h->GetXaxis()->GetXmin(),h->GetXaxis()->GetXmax());
    gaus.SetParameter(2,10);

    for(Int_t b=1;b<=h->GetXaxis()->GetNbins();b++){
      gaus.SetParameter(0,h->GetBinContent(b));
      gaus.SetParameter(1,h->GetBinCenter(b));
      for(Int_t bs=1;bs<=h->GetXaxis()->GetNbins();bs++){
        hs->AddBinContent(bs,hs->GetBinWidth(bs)*gaus.Eval(hs->GetBinCenter(bs))) ;
      }
    }
    for(Int_t bs=1;bs<=h->GetXaxis()->GetNbins();bs++){
      hs->SetBinError(bs,0.);//not sure this is necessary
    }
    
    if(hs->Integral()>0.)
      hs->Scale(h->Integral()/hs->Integral());//make sure the output histo has the same integral
    else hs->Scale(0.);

    for(Int_t b=1;b<=h->GetXaxis()->GetNbins();b++){
      h->SetBinContent(b,hs->GetBinContent(b));
      h->SetBinError(b,hs->GetBinContent(b)*0.4);
    }
    delete hs;
  }


  //fix empty distributions for limit to run
  if(h->Integral()<=0.){
    for(Int_t i=1;i<=h->GetNbinsX();i++){
      h->SetBinContent(i,0.001);
      h->SetBinError(i,0.001);
    }
  }

}
Esempio n. 7
0
int proverca(double **a ,int n,int y)
{   int i,j,z;
    double min;
    do {
        t=2;//указатель ищет отрицательные правые части
        for( i=0; i<n ; i++ )
        {
            if (a[i][y]<0)
            {
                t=t-1;
                if (t==1) {
                    min=a[i][y];
                }
                k=i;
            }
            if (a[i][y]<min)
            {
                min=a[i][y];
                k=i;
            }
        }
        if (t==2) return 0;
        z=2;
        for (j=0 ; j<y ; j++)
        {
            if (a[k][j]<0)
            {   z=z-1;
                if (t!=2)
                {
                    printf("ggg11111 \n");
                    l=j;
                    gaus(a,y,n);
                    j=y;
                }
            }
        }
        if (z==2) {
            printf("нет решения");
            return 0;
        }
    } while(t!=2);
    return 0;
}
Esempio n. 8
0
void MC_Step(
    VecCl &ph,
    double Phi0,
    int N,
    double MainHarm,
    int FurieDiscr,
    double FullTime,
    double StrongShort) {
    double gZero = 0, gSigma = 1;
    RndGaus gaus(gZero, gSigma);
    gaus.Init(-4 * gSigma, 4 * gSigma, 100, 1e-5);

    int NumJ = FurieDiscr, j, k;
    VecCl Phi_cos(NumJ), Phi_sin(NumJ);
    for(k = 1; k <= NumJ; k++) {
        Phi_cos[k] = gaus.Rnd();
        Phi_sin[k] = gaus.Rnd();
    }
    double StrongCoef = StrongShort * sqr(2 * M_PI / FullTime);
    MainHarm *= 2 / FullTime;
    Phi_cos = Phi_cos * MainHarm;
    Phi_sin = Phi_sin * MainHarm;

    VecCl CosIJ(N), SinIJ(N);
    double coef = M_PI / N;
    for(k = 1; k <= N; k++) {
        CosIJ[k] = cos(coef * (k - 1));
        SinIJ[k] = sin(coef * (k - 1));
    }
    for(k = 1; k <= N; k++) {
        ph[k] = Phi0;
        for(j = 1; j <= NumJ; j++) {
            int r = k * (j - 1) / N;
            double SCCoef = 1;
            if((r / 2) * 2 != r)
                SCCoef = -1;
            r = k * (j - 1) - r * N + 1;   //if (r==0) r=N;
            ph[k] += (SCCoef * Phi_cos[j] * CosIJ[r] + SCCoef * Phi_sin[j] * SinIJ[r]) /
                     sqrt(max<double>(j - 1, 0.25) * (1 + sqr(j) * StrongCoef));
        }
    }
};
Esempio n. 9
0
void makePlots2() {
	gROOT->ProcessLine(".x lhcbStyle.C");
	gStyle->SetOptStat(0);

	Double_t ignore, eff, massfit1, massfit2, bias1, bias2;

	Double_t value[6][19], stat[6][19], syst[6][19];
	Int_t ng(4);

	TH1D pull("pull","",15,-3.0,3.0);

	std::ifstream fin("errs.dat");
	fin.ignore(256,'\n');
	for(Int_t q=0; q<19; ++q) {
		for(Int_t g=1; g<ng+1; ++g) {	
			fin >> ignore >> ignore >> value[g+1][q] >> stat[g+1][q] >> eff >> massfit1 >> massfit2 >> bias1 >> bias2;
			syst[g+1][q] = TMath::Sqrt(stat[g+1][q]*stat[g+1][q] + eff*eff + massfit1*massfit1 + massfit2*massfit2 + bias1*bias1 + bias2*bias2);
			//plot syst errors on top of stats

			if(q<17) pull.Fill(value[g+1][q]/syst[g+1][q]);
		}
		value[0][q] = value[2][q];
		stat[0][q]  = stat[2][q];
		syst[0][q]  = syst[2][q];

		value[1][q] = 2*value[3][q]+1;
		stat[1][q]  = 2*stat[3][q];
		syst[1][q]  = 2*syst[3][q];
	}

	fin.close();

	Double_t  xs[19] = { 0.54, 1.55, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 11.375, 12.125, 15.5, 16.5, 17.5, 18.5, 19.5, 20.5, 21.5, 3.55, 18.5};
	Double_t exs[19] = { 0.44, 0.45, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,  0.375,  0.375,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5, 2.45,  3.5};

	TCanvas c;

	TF1 gaus("gaus","gaus(0)",-3.,3.);
	gaus.SetParameter(0,68);
	gaus.SetParameter(1,pull.GetBinCenter(pull.GetMaximumBin()));
	gaus.SetParameter(2,pull.GetStdDev());
	gaus.SetParLimits(1,-2.0,2.0);
	gaus.SetParLimits(2,0.3,3.0);
	pull.Fit(&gaus,"S");
	pull.Draw();
	c.SaveAs("gPulls.pdf");

	for(Int_t i=0; i<ng+2; ++i) {
            TString varName("");
            TString varName2("");

	    switch(i) {
		    case 0:
			    varName="A_{FB}";
			    varName2="AFB";
			    break;
		    case 1:
			    varName="F_{H}";
			    varName2="FH";
			    break;
		    default:
			    varName="G^{("; varName+=(i-1); varName+=")}";
			    varName2="G"; varName2+=(i-1);
	    }
	    
	    Double_t yMin = getMin(19,value[i])-getMax(19,syst[i]);
	    Double_t yMax = getMax(19,value[i])+getMax(19,syst[i]);

	    if(yMax-yMin == 0) continue;

	    TH2D h("h","",1,0.0,22.,1,yMin,yMax);

	    TLine l(0.0,0.0,22.0,0.0);
	    l.SetLineStyle(kDashed);
	    l.SetLineColor(kRed);
	    
	    h.GetYaxis()->SetTitle(varName);
	    h.GetXaxis()->SetTitle("q^{2} [GeV^{2}/c^{4}]");

	    TGraphErrors g1(17,xs,   value[i],   exs,   stat[i]   );
	    TGraphErrors g2( 2,xs+17,value[i]+17,exs+17,stat[i]+17);
	    TGraphErrors g3(17,xs,   value[i],   exs,   syst[i]   );
	    TGraphErrors g4( 2,xs+17,value[i]+17,exs+17,syst[i]+17);

	    g2.SetLineColor(kBlue);
	    g2.SetMarkerColor(kBlue);
	    g3.SetLineColor(kRed);
	    g4.SetLineColor(kRed);
	    g2.SetFillColor(kBlue);
	    g4.SetFillColor(kRed);
	    g2.SetFillStyle(3001);
	    g4.SetFillStyle(3002);

	    h.Draw();
	    l.Draw();
	    g4.Draw("E2same");
	    g2.Draw("E2same");
	    g3.Draw("PEsame");
	    g1.Draw("PEsame");
	    c.SaveAs("plots/fromPatrickNew/"+varName2+".pdf");
	    c.SaveAs("plots/fromPatrickNew/"+varName2+".png");
	}

}
Esempio n. 10
0
int main()
{
    double  **a,P1[5][7]= {{2.0,1,1,0,0,64,0},{1.0,3,0,1,0,72,0},{0,1,0,0,1,20,0},{-4,-6,0.0,0,0,0,0},{0,0,0,0,0,0,0,0}}, bb[]= {64.0,72.0,20.0}  ;
    int i,j,n=3,y=5 ,v[4]= {0,0,0,0},m; // n-количество строк, y-количество столбцов
    double min;
    a=(double**)malloc(5*sizeof(double*));
    for(i=0; i<5; i++)
    {   a[i]=(double*)malloc(7*sizeof(double));
        for(j=0; j<7; j++) a[i][j]=P1[i][j];
    }
    for (i=0; i<=n ; i++)
    {
        for (j=0 ; j< y+1 ; j++)
        {
            printf("%.0f   ",a[i][j]);
        }
        printf("\n");
    }
//proverca(a,n,y);
    t=2;
    do
    {   ocenki(a,bb,n,y);
        t=2;
        for (j=0; j<y ; j++)
        {
            if (a[n][j]<0)
            {
                t=t-1;
                if (t==1)
                {
                    l=j;
                    min=a[n+1][j];
                }
                if (a[n][j]<min)
                {
                    min=a[n][j];
                    l=j;
                }
            }
        }
        printf("AAAAAA%dAAAAAA\n",l);
        j=0;
        if(t<2)
        {
            for ( i=0 ; i<n ; i++)
            {
                if (a[i][l]>0)
                {
                    if (j==0)
                    {
                        min=a[i][y]/a[i][l];
                        j=2;
                        k=i;
                    }
                    if ((a[i][y]/a[i][l])<min)
                    {
                        min=a[i][y]/a[i][l];
                        k=i;
                    }
                }
            }
            gaus(a,y,n);
        }
        for (i=0; i<=n ; i++)
        {
            for (j=0 ; j<=y ; j++)
            {
                printf("%.1f     ",a[i][j]);
            }
            printf("\n");
        }
    } while (t!=2);

    return 0;
}
Esempio n. 11
0
void ModelRenderer::renderGaussianComponent(double* imageData, size_t imageWidth, size_t imageHeight, long double posRA, long double posDec, long double gausMaj, long double gausMin, long double gausPA, long double flux, bool normalizeIntegratedFlux)
{
	// Using the FWHM formula for a Gaussian:
	long double sigmaMaj = gausMaj / (2.0L * sqrtl(2.0L * logl(2.0L)));
	long double sigmaMin = gausMin / (2.0L * sqrtl(2.0L * logl(2.0L)));
	// TODO this won't work for non-equally spaced dimensions
	long double minPixelScale = std::min(_pixelScaleL, _pixelScaleM);
	// ...And this is not accurate, as the correlation between beam and source PA has
	// to be taken into account
	if(normalizeIntegratedFlux)
	{
		double factor = 2.0L * M_PI * sigmaMaj * sigmaMin / (minPixelScale * minPixelScale);
		if(factor > 1.0)
			flux /= factor;
	}
	
	// Make rotation matrix
	long double transf[4];
	// Position angle is angle from North: 
	long double angle = gausPA+0.5*M_PI;
	transf[2] = sin(angle);
	transf[0] = cos(angle);
	transf[1] = -transf[2];
	transf[3] = transf[0];
	double sigmaMax = std::max(std::fabs(sigmaMaj * transf[0]), std::fabs(sigmaMaj * transf[1]));
	// Multiply with scaling matrix to make variance 1.
	transf[0] = transf[0] / sigmaMaj;
	transf[1] = transf[1] / sigmaMaj;
	transf[2] = transf[2] / sigmaMin;
	transf[3] = transf[3] / sigmaMin;
	int boundingBoxSize = ceil(sigmaMax * 20.0 / minPixelScale);
	long double sourceL, sourceM;
	ImageCoordinates::RaDecToLM(posRA, posDec, _phaseCentreRA, _phaseCentreDec, sourceL, sourceM);

	// Calculate the bounding box
	int sourceX, sourceY;
	ImageCoordinates::LMToXY<long double>(sourceL-_phaseCentreDL, sourceM-_phaseCentreDM, _pixelScaleL, _pixelScaleM, imageWidth, imageHeight, sourceX, sourceY);
	int
		xLeft = sourceX - boundingBoxSize,
		xRight = sourceX + boundingBoxSize,
		yTop = sourceY - boundingBoxSize,
		yBottom = sourceY + boundingBoxSize;
	if(xLeft < 0) xLeft = 0;
	if(xLeft > (int) imageWidth) xLeft = (int) imageWidth;
	if(xRight < xLeft) xRight = xLeft;
	if(xRight > (int) imageWidth) xRight = (int) imageWidth;
	if(yTop < 0) yTop = 0;
	if(yTop > (int) imageHeight) yTop = (int) imageHeight;
	if(yBottom < yTop) yBottom = yTop;
	if(yBottom > (int) imageHeight) yBottom = (int) imageHeight;
	
	for(int y=yTop; y!=yBottom; ++y)
	{
		double *imageDataPtr = imageData + y*imageWidth+xLeft;
		for(int x=xLeft; x!=xRight; ++x)
		{
			long double l, m;
			ImageCoordinates::XYToLM<long double>(x, y, _pixelScaleL, _pixelScaleM, imageWidth, imageHeight, l, m);
			l += _phaseCentreDL; m += _phaseCentreDM;
			long double
				lTransf = (l-sourceL)*transf[0] + (m-sourceM)*transf[1],
				mTransf = (l-sourceL)*transf[2] + (m-sourceM)*transf[3];
			long double dist = sqrt(lTransf*lTransf + mTransf*mTransf);
			long double g = gaus(dist, 1.0L);
			(*imageDataPtr) += double(g * flux);
			++imageDataPtr;
		}
	}
}
Esempio n. 12
0
void BfmWrapper::SpectralRange(bfm_qdp<Float>  &bfm)
{
  int Ls = bfm.Ls;
  multi1d<T4> gaus(Ls);
  for(int s=0;s<Ls;s++)
    gaussian(gaus[s]);


  Fermion_t tmp1,tmp2;
  Fermion_t b;
  Fermion_t Ab;
  double mu;
  tmp1   = bfm.allocFermion();
  tmp2   = bfm.allocFermion();
  b      = bfm.allocFermion();
  Ab     = bfm.allocFermion();

  bfm.importFermion(gaus,b,0);
  
  int dagyes= 1;
  int dagno = 0;
  int donrm = 1;
  double abnorm;
  double absq;
  double n;
  ///////////////////////////////////////
  // Get the highest evalue of MdagM
  ///////////////////////////////////////
#pragma omp parallel for  
for(int i=0;i<bfm.nthread;i++) {
  int me = bfm.thread_barrier();

  for(int k=0;k<100;k++){

    n = bfm.norm(b);

    absq = bfm.Mprec(b,tmp2,tmp1,dagno,donrm);
           bfm.Mprec(tmp2,Ab,tmp1,dagyes); //MdagM
    
    mu = absq/n;
    if(bfm.isBoss() && (!me) ) { 
      printf("PowerMethod %d mu=%le absq=%le n=%le \n",k,mu,absq,n);
    }

    abnorm = sqrt(absq);

    // b = Ab / |Ab|
    bfm.axpby(b,Ab,Ab,0.0,1.0/abnorm);
  }
 }

 /////////////////////////////////////////
 // Get the lowest eigenvalue of mdagm
 // Apply power method to [lambda_max - MdagM]
 /////////////////////////////////////////
 double lambda_max = mu;

 bfm.importFermion(gaus,b,0);

#pragma omp parallel for  
for(int i=0;i<bfm.nthread;i++) {
  int me = bfm.thread_barrier();

  for(int k=0;k<100;k++){

    n = bfm.norm(b);

    absq = bfm.Mprec(b,tmp2,tmp1,dagno,donrm);
    bfm.Mprec(tmp2,Ab,tmp1,dagyes); 
    bfm.axpby(Ab,Ab,b,-1,lambda_max);

    absq = lambda_max * n - absq;

    mu = absq/n;

    if(bfm.isBoss() && (!me) ) { 
      printf("PowerMethod for low EV %d mu=%le absq=%le n=%le \n",k,mu,absq,n);
      printf("PowerMethod  %d lambda_min  =%le\n",k,lambda_max - mu);
    }

    abnorm = sqrt(absq);

    // b = Ab / |Ab|
    bfm.axpby(b,Ab,Ab,0.0,1.0/abnorm);

  }
}

  bfm.freeFermion(tmp1);
  bfm.freeFermion(tmp2);
  bfm.freeFermion(b);
  bfm.freeFermion(Ab);


}