int main(int argc, char** argv){ if(argc!=2){ std::cerr << "### Usage like ... \n ./gaus2d 10000(imax)" << std::endl ; return EXIT_FAILURE ; } else{/* DO NOT ANYTHING */} TApplication app( "app", &argc, argv ); double x ; double y ; int imax = atoi(argv[1]) ; TString title("2D Gaussian") ; TString titleimax(argv[1]) ; title += titleimax ; TCanvas *c1 = new TCanvas("c1", "c1", 600, 600) ; gStyle->SetOptStat("enRM") ; c1->SetTicks(1,1) ; c1->SetGrid(1,1) ; c1->SetRightMargin(0.15) ; TH2D *hist = new TH2D("hist",title,100, -5., 5., 100, -5., 5.) ; TF2 *gausfunc = new TF2("gausfunc","[0]*TMath::Exp( - TMath::Sqrt( TMath::Power((x-[1])/[2],2)/2. + TMath::Power((y-[3])/[4],2)/2. ))", -5., 5., -5., 5.) ; gausfunc->SetParameters(1., 0., 0.5, 0., 0.5) ; gausfunc->SetNpx(300) ; gausfunc->SetNpy(300) ; gRandom->SetSeed(unsigned(time(NULL))) ; for(int i =0 ; i< imax ; i++){ gausfunc->GetRandom2(x,y) ; hist->Fill(x,y) ; } hist->Draw("colz") ; gPad->Update(); TPaveStats *st = (TPaveStats*)hist->FindObject("stats") ; st->SetX1NDC(0.60) ; st->SetX2NDC(0.85) ; st->SetY1NDC(0.70) ; st->SetY2NDC(0.90) ; c1->SaveAs("gaus2dc1.eps") ; app.Run() ; delete c1 ; delete hist ; delete gausfunc ; return EXIT_SUCCESS ; }
int TwoHistoFit2D(bool global = true) { // create two histograms int nbx1 = 50; int nby1 = 50; int nbx2 = 50; int nby2 = 50; double xlow1 = 0.; double ylow1 = 0.; double xup1 = 10.; double yup1 = 10.; double xlow2 = 5.; double ylow2 = 5.; double xup2 = 20.; double yup2 = 20.; TH2D * h1 = new TH2D("h1","core",nbx1,xlow1,xup1,nby1,ylow1,yup1); TH2D * h2 = new TH2D("h2","tails",nbx2,xlow2,xup2,nby2,ylow2,yup2); double iniParams[10] = { 100, 6., 2., 7., 3, 100, 12., 3., 11., 2. }; // create fit function TF2 * func = new TF2("func",my2Dfunc,xlow2,xup2,ylow2,yup2, 10); func->SetParameters(iniParams); // fill Histos int n1 = 50000; int n2 = 50000; // h1->FillRandom("func", n1); //h2->FillRandom("func",n2); FillHisto(h1,n1,iniParams); FillHisto(h2,n2,iniParams); // scale histograms to same heights (for fitting) double dx1 = (xup1-xlow1)/double(nbx1); double dy1 = (yup1-ylow1)/double(nby1); double dx2 = (xup2-xlow2)/double(nbx2); double dy2 = (yup2-ylow2)/double(nby2); // h1->Sumw2(); // h1->Scale( 1.0 / ( n1 * dx1 * dy1 ) ); // scale histo 2 to scale of 1 h2->Sumw2(); h2->Scale( ( double(n1) * dx1 * dy1 ) / ( double(n2) * dx2 * dy2 ) ); if (global) { // fill data structure for fit (coordinates + values + errors) std::cout << "Do global fit" << std::endl; // fit now all the function together // fill data structure for fit (coordinates + values + errors) TAxis *xaxis1 = h1->GetXaxis(); TAxis *yaxis1 = h1->GetYaxis(); TAxis *xaxis2 = h2->GetXaxis(); TAxis *yaxis2 = h2->GetYaxis(); int nbinX1 = h1->GetNbinsX(); int nbinY1 = h1->GetNbinsY(); int nbinX2 = h2->GetNbinsX(); int nbinY2 = h2->GetNbinsY(); /// reset data structure coords = std::vector<std::pair<double,double> >(); values = std::vector<double>(); errors = std::vector<double>(); for (int ix = 1; ix <= nbinX1; ++ix) { for (int iy = 1; iy <= nbinY1; ++iy) { if ( h1->GetBinContent(ix,iy) > 0 ) { coords.push_back( std::make_pair(xaxis1->GetBinCenter(ix), yaxis1->GetBinCenter(iy) ) ); values.push_back( h1->GetBinContent(ix,iy) ); errors.push_back( h1->GetBinError(ix,iy) ); } } } for (int ix = 1; ix <= nbinX2; ++ix) { for (int iy = 1; iy <= nbinY2; ++iy) { if ( h2->GetBinContent(ix,iy) > 0 ) { coords.push_back( std::make_pair(xaxis2->GetBinCenter(ix), yaxis2->GetBinCenter(iy) ) ); values.push_back( h2->GetBinContent(ix,iy) ); errors.push_back( h2->GetBinError(ix,iy) ); } } } TVirtualFitter::SetDefaultFitter("Minuit"); TVirtualFitter * minuit = TVirtualFitter::Fitter(0,10); for (int i = 0; i < 10; ++i) { minuit->SetParameter(i, func->GetParName(i), func->GetParameter(i), 0.01, 0,0); } minuit->SetFCN(myFcn); double arglist[100]; arglist[0] = 0; // set print level minuit->ExecuteCommand("SET PRINT",arglist,2); // minimize arglist[0] = 5000; // number of function calls arglist[1] = 0.01; // tolerance minuit->ExecuteCommand("MIGRAD",arglist,2); //get result double minParams[10]; double parErrors[10]; for (int i = 0; i < 10; ++i) { minParams[i] = minuit->GetParameter(i); parErrors[i] = minuit->GetParError(i); } double chi2, edm, errdef; int nvpar, nparx; minuit->GetStats(chi2,edm,errdef,nvpar,nparx); func->SetParameters(minParams); func->SetParErrors(parErrors); func->SetChisquare(chi2); int ndf = coords.size()-nvpar; func->SetNDF(ndf); std::cout << "Chi2 Fit = " << chi2 << " ndf = " << ndf << " " << func->GetNDF() << std::endl; // add to list of functions h1->GetListOfFunctions()->Add(func); h2->GetListOfFunctions()->Add(func); } else { // fit independently h1->Fit(func); h2->Fit(func); } // Create a new canvas. TCanvas * c1 = new TCanvas("c1","Two HIstogram Fit example",100,10,900,800); c1->Divide(2,2); gStyle->SetOptFit(); gStyle->SetStatY(0.6); c1->cd(1); h1->Draw(); func->SetRange(xlow1,ylow1,xup1,yup1); func->DrawCopy("cont1 same"); c1->cd(2); h1->Draw("lego"); func->DrawCopy("surf1 same"); c1->cd(3); func->SetRange(xlow2,ylow2,xup2,yup2); h2->Draw(); func->DrawCopy("cont1 same"); c1->cd(4); h2->Draw("lego"); gPad->SetLogz(); func->Draw("surf1 same"); return 0; }
int main(int argc, char **argv) { // This allows you to view ROOT-based graphics in your C++ program // If you don't want view graphics (eg just read/process/write data files), // this can be ignored TApplication theApp("App", &argc, argv); TCanvas* canvas = new TCanvas(); // *************************************** // not important for this exmple // Set a bunch of parameters to make the plot look nice canvas->SetFillColor(0); canvas->UseCurrentStyle(); canvas->SetBorderMode(0); // still leaves red frame bottom and right canvas->SetFrameBorderMode(0); // need this to turn off red hist frame! gROOT->SetStyle("Plain"); canvas->UseCurrentStyle(); gROOT->ForceStyle(); canvas->Divide(2, 1); gStyle->SetOptStat(0); gStyle->SetTitleBorderSize(0); gStyle->SetTitleSize(0.04); gStyle->SetTitleFont(42, "hxy"); // for histogram and axis titles gStyle->SetLabelFont(42, "xyz"); // for axis labels (values) gROOT->ForceStyle(); // *************************************** TFile *f=new TFile("fitInputs.root"); TH2F *hist1 = (TH2F*)f->Get("hdata"); TH2F *hback = (TH2F*)f->Get("hbkg"); double xmin = 0.0; double xmax = 6.0; double ymin = 0.0; double ymax=6.0; // Initialize minuit, set initial values etc. of parameters. const int npar = 6; // the number of parameters TMinuit minuit(npar); minuit.SetFCN(fcn); // the fcn to be minized // (eg calculates chi^2 or NLL, not the // parameterization of the data! TF2* myfunc = new TF2("myfunc", gausPdf, xmin, xmax, ymin, ymax, npar); // use this to fit the data and get results double par[npar]; // the start values double stepSize[npar]; // step sizes double minVal[npar]; // minimum bound on parameter double maxVal[npar]; // maximum bound on parameter TString parName[npar]; par[0] = hback->GetMaximum(); // guesses for starting the fit par[1] = hist1->GetMaximum(); par[2] = 3.555; //use mean and rms values from histogram par[3] = 1.17; //as initial guesses for parameters par[4] = 1.879; par[5] = 1.229; stepSize[0] = TMath::Abs(par[0]*0.1); stepSize[1] = TMath::Abs(par[1]*0.1); // step size MUST be positive! stepSize[2] = TMath::Abs(par[2]*0.1); stepSize[3] = TMath::Abs(par[3]*0.1); stepSize[4] = TMath::Abs(par[4]*0.1); stepSize[5] = TMath::Abs(par[5]*0.1); minVal[0] = 0; // if min and max values = 0, parameter is unbounded. maxVal[0] = 0; minVal[1] = 0; maxVal[1] = 0; minVal[2] = 0; maxVal[2] = 6; minVal[3] = 0; maxVal[3] = 6; minVal[4] = 0; maxVal[4] = 6; minVal[5] = 0; maxVal[5] = 6; parName[0] = "Bkg"; parName[1] = "A"; parName[2] = "x0"; parName[3] = "sigma_x"; parName[4] = "y0"; parName[5] = "sigma_y"; for (int i=0; i<npar; i++){ minuit.DefineParameter(i, parName[i].Data(), par[i], stepSize[i], minVal[i], maxVal[i]); } // here we define the pointers to pass information to Minuit's fcn // not pretty, but works well hdata=hist1; hback->Scale(1.0/3600.0); //scale over total number of entries first hb=hback; fparam=myfunc; // Do the minimization! minuit.Migrad(); // Minuit's best minimization algorithm double outpar[npar], err[npar]; for (int i=0; i<npar; i++){ minuit.GetParameter(i,outpar[i],err[i]); } TH2F *residual = new TH2F("residual", "Residuals Plot: data-fit", 50, 0, 6, 50, 0, 6); myfunc->SetParameters(outpar); for (int i=1; i<=hist1->GetNbinsX(); i++){ for(int j=1; j<=hist1->GetNbinsY(); j++){ double x = hist1->GetBinCenter(i); double y = hist1->GetBinCenter(j); double f= myfunc->Eval(x, y); double r = hist1->GetBinContent(i, j) -f; residual->Fill(x, y, r); } } canvas->cd(1); residual->GetXaxis()->SetTitle("x"); residual->GetYaxis()->SetTitle("y"); residual->GetXaxis()->CenterTitle(); residual->GetYaxis()->CenterTitle(); residual->GetXaxis()->SetTitleOffset(1.25); residual->GetYaxis()->SetTitleOffset(1.25); residual->Draw("colz"); canvas->cd(2); hist1->Add(hback, -outpar[0]); hist1->SetTitle("Data Signal with Fitted Background Subtracted"); hist1->GetXaxis()->SetTitle("x"); hist1->GetYaxis()->SetTitle("y"); hist1->GetZaxis()->SetTitle("signal value"); hist1->GetXaxis()->CenterTitle(); hist1->GetYaxis()->CenterTitle(); hist1->GetXaxis()->SetTitleOffset(1.25); hist1->GetYaxis()->SetTitleOffset(1.25); hist1->Draw("lego"); cout << "To exit, quit ROOT from the File menu of the plot (or use control-C)" << endl; theApp.Run(true); canvas->Close(); return 0; }
void PlotPhaseVelocityFunctions( const TString &opt="") { #ifdef __CINT__ gSystem->Load("libplasma.so"); #endif PlasmaGlob::Initialize(); // Palettes! gROOT->Macro("PlasmaPalettes.C"); gStyle->SetPadTopMargin(0.06); gStyle->SetPadGridY(0); gStyle->SetPadGridX(0); gStyle->SetFrameLineWidth(2); gStyle->SetLabelSize(0.04, "xyz"); gStyle->SetTitleOffset(1.2,"y"); gStyle->SetTitleOffset(1.2,"z"); gStyle->SetNdivisions(505,"xyz"); PUnits::UnitsTable::Get(); // // SPS parameters from Pukhov, Kumar et al. PRL107,145003(2011) // Double_t n0 = 7.76e14 / PUnits::cm3; // Double_t nb = 1.5e12 / PUnits::cm3; // Double_t lambda = PFunc::PlasmaWavelength(n0); // Double_t kp = PFunc::PlasmaWavenumber(n0); // Double_t skindepth = PFunc::PlasmaSkindepth(n0); // Double_t r0 = 0.19 * PUnits::mm; // Double_t z = 2.5 * PUnits::m; // Double_t zg = -28.6 * PUnits::mm; // Double_t E = 450 * PUnits::GeV; // Double_t gamma = E / PConst::ProtonMassE ; // SPS parameters from Schroeder et al. PRL107,145002(2011) // Double_t n0 = 1.0; // Double_t nb = 0.002; // Double_t lambda = TMath::TwoPi(); // Double_t kp = 1.0; // Double_t r0 = 1.0; // Double_t z = 13105; // Double_t E = 450 * PUnits::GeV; // Double_t gamma = E / PConst::ProtonMassE ; // PITZ parameters Double_t n0 = 1.0e15 / PUnits::cm3; Double_t nb = 1.05e13 / PUnits::cm3; Double_t lambda = PFunc::PlasmaWavelength(n0); Double_t kp = PFunc::PlasmaWavenumber(n0); Double_t skindepth = PFunc::PlasmaSkindepth(n0); Double_t r0 = 34.259 * PUnits::um; Double_t z = 150. * PUnits::mm; Double_t zg = -5. * PUnits::mm; Double_t E = 25 * PUnits::MeV; Double_t gamma = (E / PConst::ElectronMassE) + 1.0; Double_t vb = TMath::Sqrt(1. - (1./(gamma*gamma))); cout << " n0 = " << n0 * PUnits::cm3 << " e/cc" << endl; cout << " Wavelength = " << PUnits::BestUnit(lambda, "Length") << endl; cout << " Skindepth = " << PUnits::BestUnit(skindepth, "Length") << endl; cout << " Wavenumber = " << kp * PUnits::mm << " mm^-1" << endl; // Normalized variables if(!opt.Contains("units")){ r0 *= kp; nb /= n0; n0 = 1.0; z *= kp; zg *= kp; } Double_t N = PFunc::Nefoldings(z,zg,r0,gamma,nb,n0); Double_t r1 = (TMath::Power(3.,1./4.)/TMath::Power(8.*TMath::Pi()*N,1./2.)) * TMath::Exp(N) ; Double_t Gamma = PFunc::PhaseGamma(z,zg,r0,gamma,nb,n0); Double_t vph = PFunc::PhaseVelocity(z,zg,r0,gamma,nb,n0); Double_t vph2 = PFunc::PhaseVelocity2(z,zg,gamma,nb,n0); cout << " gamma beam = " << gamma << endl; cout << " zeta = " << z << endl; cout << " zeta comov. = " << zg << endl; cout << " r0 = " << r0 << endl; cout << " n_b0/n_0 = " << nb/n0 << endl; cout << " nu constant = " << PFunc::Nu(r0,n0) << endl; cout << " Number e-foldings = " << N << endl; cout << " r1 = " << r1 << endl; cout << " Gamma wake = " << Gamma << endl; cout << " Wake Phase velocity = " << vph << endl; cout << " Wake Phase velocity2 = " << vph2 << endl; cout << " Beam phase velocity = " << vb << endl; const Int_t NPAR = 5; Double_t par[NPAR] = {zg,r0,gamma,nb,n0}; TF1 *fPhaseVsZ2 = new TF1("fPhaseVsZ2",PhaseVelocityVsZ2,0,z,NPAR); fPhaseVsZ2->SetParameters(par); TF1 *fPhaseVsZ = new TF1("fPhaseVsZ",PhaseVelocityVsZ,0,z,NPAR); fPhaseVsZ->SetParameters(par); Double_t par2[NPAR] = {z,r0,gamma,nb,n0}; TF1 *fPhaseVsZg2 = new TF1("fPhaseVsZg2",PhaseVelocityVsZg2,zg,0.,NPAR); fPhaseVsZg2->SetParameters(par2); TF1 *fPhaseVsZg = new TF1("fPhaseVsZg",PhaseVelocityVsZg,zg,0.,NPAR); fPhaseVsZg->SetParameters(par2); const Int_t NPAR2D = 4; Double_t par3[NPAR2D] = {r0,gamma,nb,n0}; TF2 *fPhaseVsZVsZg2 = new TF2("fPhaseVsZVsZg2",PhaseVelocityVsZVsZg2,0.,z,zg,0.,NPAR2D); fPhaseVsZVsZg2->SetParameters(par3); TF2 *fPhaseVsZVsZg = new TF2("fPhaseVsZVsZg",PhaseVelocityVsZVsZg,0.,z,zg,0.,NPAR2D); fPhaseVsZVsZg->SetParameters(par3); char ctext[64]; TPaveText *textZetag = new TPaveText(0.13,0.85,0.38,0.92,"NDC"); PlasmaGlob::SetPaveTextStyle(textZetag,12); textZetag->SetTextColor(kGray+3); if(opt.Contains("units")) sprintf(ctext,"#zeta_{0} = %6.2f mm", zg / PUnits::mm); else sprintf(ctext,"#zeta_{0} = %6.2f c/#omega_{p}", zg); textZetag->AddText(ctext); TPaveText *textZeta = new TPaveText(0.13,0.85,0.38,0.92,"NDC"); PlasmaGlob::SetPaveTextStyle(textZeta,12); textZeta->SetTextColor(kGray+3); if(opt.Contains("units")) sprintf(ctext,"z_{0} = %6.2f mm", z / PUnits::mm); else sprintf(ctext,"z_{0} = %6.2f c/#omega_{p}", z); textZeta->AddText(ctext); // Graph for de-phasing: const Int_t NP = 100; TGraph *gPhase = new TGraph(NP); TGraph *gPhase2 = new TGraph(NP); Float_t phase = zg; Float_t phase2 = zg; Float_t Dz = z / NP; for(Int_t i=0;i<NP;i++) { Float_t zp = (i+1)*Dz; Float_t v = PFunc::PhaseVelocity(zp,phase,r0,gamma,nb,n0); phase += (v - vb) * Dz; // cout << " z = " << zp << " phase = " << phase << endl; if(opt.Contains("units")) gPhase->SetPoint(i,zp,(phase-zg)*kp); else gPhase->SetPoint(i,zp,(phase-zg)); v = PFunc::PhaseVelocity2(zp,phase2,gamma,nb,n0); phase2 += (v - 1) * Dz; // cout << " z = " << zp << " phase = " << phase << endl; if(opt.Contains("units")) gPhase2->SetPoint(i,zp,(phase2-zg)*kp); else gPhase2->SetPoint(i,zp,(phase2-zg)); } TCanvas *C = new TCanvas("C","Wake phase velocity",1000,750); C->Divide(2,2); TLegend *Leg = new TLegend(0.6,0.20,0.85,0.35); PlasmaGlob::SetPaveStyle(Leg); Leg->SetTextAlign(22); Leg->SetTextColor(kGray+3); Leg->SetLineColor(1); Leg->SetBorderSize(1); Leg->SetFillColor(0); Leg->SetFillStyle(1001); //Leg-> SetNColumns(2); Leg->AddEntry(fPhaseVsZ,"PRL 107,145002","L"); Leg->AddEntry(fPhaseVsZ2,"PRL 107,145003","L"); Leg->SetTextColor(kGray+3); C->cd(1); fPhaseVsZ->GetYaxis()->SetTitle("(v_{p} - c)/c"); if(opt.Contains("units")) fPhaseVsZ->GetXaxis()->SetTitle("z [m]"); else fPhaseVsZ->GetXaxis()->SetTitle("z [c/#omega_{p}]"); fPhaseVsZ->GetYaxis()->SetRangeUser(-3e-4,0.); fPhaseVsZ->GetXaxis()->CenterTitle(); fPhaseVsZ->GetYaxis()->CenterTitle(); fPhaseVsZ->SetLineWidth(2); fPhaseVsZ->Draw("C"); fPhaseVsZ2->SetLineWidth(2); fPhaseVsZ2->SetLineStyle(2); fPhaseVsZ2->Draw("C same"); textZetag->Draw(); Leg->Draw(); C->cd(2); fPhaseVsZg->GetYaxis()->SetTitle("(v_{p} - c)/c"); if(opt.Contains("units")) fPhaseVsZg->GetXaxis()->SetTitle("#zeta [m]"); else fPhaseVsZg->GetXaxis()->SetTitle("#zeta [c/#omega_{p}]"); fPhaseVsZg->GetYaxis()->SetRangeUser(-3e-4,0.); fPhaseVsZg->GetYaxis()->SetNdivisions(505); fPhaseVsZg->GetXaxis()->CenterTitle(); fPhaseVsZg->SetLineWidth(2); fPhaseVsZg->GetYaxis()->CenterTitle(); fPhaseVsZg->Draw("C"); fPhaseVsZg2->SetLineWidth(2); fPhaseVsZg2->SetLineStyle(2); fPhaseVsZg2->Draw("C same"); textZeta->Draw(); C->cd(3); gPhase->GetYaxis()->SetTitle("#Delta#zeta [c/#omega_{p}]"); if(opt.Contains("units")) { gPhase->GetXaxis()->SetTitle("z [m]"); gPhase->GetXaxis()->SetNdivisions(510); } else gPhase->GetXaxis()->SetTitle("z [c/#omega_{p}]"); // gPhase->GetXaxis()->SetNdivisions(510); gPhase->GetXaxis()->CenterTitle(); gPhase->GetYaxis()->CenterTitle(); gPhase->GetXaxis()->SetRangeUser(0.,z); gPhase->GetYaxis()->SetNdivisions(505); gPhase->SetLineWidth(2); gPhase->Draw("AC"); gPhase2->SetLineStyle(2); gPhase2->SetLineWidth(2); gPhase2->Draw("C"); textZetag->Draw(); C->cd(4); gPad->SetLeftMargin(0.18); if(opt.Contains("units")) { fPhaseVsZVsZg->GetYaxis()->SetTitle("#zeta [m]"); } else fPhaseVsZVsZg->GetYaxis()->SetTitle("#zeta [c/#omega_{p}]"); if(opt.Contains("units")) { fPhaseVsZVsZg->GetXaxis()->SetTitle("z [m]"); } else fPhaseVsZVsZg->GetXaxis()->SetTitle("z [c/#omega_{p}]"); fPhaseVsZVsZg->GetZaxis()->SetTitle("(v_{p} - c)/c"); fPhaseVsZVsZg->GetYaxis()->SetNdivisions(505); // fPhaseVsZVsZg->GetXaxis()->SetNdivisions(510); fPhaseVsZVsZg->GetXaxis()->CenterTitle(); fPhaseVsZVsZg->GetXaxis()->SetTitleOffset(1.6); fPhaseVsZVsZg->GetYaxis()->CenterTitle(); fPhaseVsZVsZg->GetYaxis()->SetTitleOffset(2.0); fPhaseVsZVsZg->GetZaxis()->CenterTitle(); fPhaseVsZVsZg->GetZaxis()->SetTitleOffset(1.4); fPhaseVsZVsZg->Draw("surf2"); C->cd(); // Print to a file PlasmaGlob::imgconv(C,"./WakePhaseVelocity",opt); // --------------------------------------------------------- }