//____________________________________________________________________ void fitCircle(Int_t n=10000) { //generates n points around a circle and fit them TCanvas *c1 = new TCanvas("c1","c1",600,600); c1->SetGrid(); gr = new TGraph(n); if (n> 999) gr->SetMarkerStyle(1); else gr->SetMarkerStyle(3); TRandom3 r; Double_t x,y; for (Int_t i=0;i<n;i++) { r.Circle(x,y,r.Gaus(4,0.3)); gr->SetPoint(i,x,y); } c1->DrawFrame(-5,-5,5,5); gr->Draw("p"); auto chi2Function = [&](const Double_t *par) { //minimisation function computing the sum of squares of residuals // looping at the graph points Int_t np = gr->GetN(); Double_t f = 0; Double_t *x = gr->GetX(); Double_t *y = gr->GetY(); for (Int_t i=0;i<np;i++) { Double_t u = x[i] - par[0]; Double_t v = y[i] - par[1]; Double_t dr = par[2] - std::sqrt(u*u+v*v); f += dr*dr; } return f; }; // wrap chi2 funciton in a function object for the fit // 3 is the number of fit parameters (size of array par) ROOT::Math::Functor fcn(chi2Function,3); ROOT::Fit::Fitter fitter; double pStart[3] = {0,0,1}; fitter.SetFCN(fcn, pStart); fitter.Config().ParSettings(0).SetName("x0"); fitter.Config().ParSettings(1).SetName("y0"); fitter.Config().ParSettings(2).SetName("R"); // do the fit bool ok = fitter.FitFCN(); if (!ok) { Error("line3Dfit","Line3D Fit failed"); } const ROOT::Fit::FitResult & result = fitter.Result(); result.Print(std::cout); //Draw the circle on top of the points TArc *arc = new TArc(result.Parameter(0),result.Parameter(1),result.Parameter(2)); arc->SetLineColor(kRed); arc->SetLineWidth(4); arc->Draw(); }
void massfitvn_Jpsi() { double fit_range_low = 2.6; double fit_range_high = 3.5; double JPsi_mass = 3.097; int npt = 7; TFile* file1 = TFile::Open("HM185_JpsivnHist_etagap1p5_v30_eff_extdeta.root"); TFile ofile("v2vspt_fromfit_jpsi_HM185_250_deta1p5_doubleCB_v30_eff_exp_extdeta.root","RECREATE"); //v12 double alpha_fit[14] = {4.30986,3.50841,3.03436,2.73741,2.37934,2.10685,2.03615}; double n_fit[14] = {1.88853,1.9839,2.03198,2.07295,2.11001,2.15234,2.10154}; TF1* fmasssig[9]; TF1* fmassbkg[9]; TF1* fmasstotal[9]; TF1* fvn[9]; double pt[13]; double KET_ncq[13]; double v2[13]; double v2e[13]; double v2_bkg[13]; double v2_ncq[13]; double v2e_ncq[13]; double ptbin[14] = {0.2, 1.8, 3.0, 4.5, 6.0, 8.0, 10, 20}; double a[13]; double b[13]; double sigfrac[13]; TCanvas* c[10]; for(int i=0;i<npt;i++) { c[i] = new TCanvas(Form("c_%d",i),Form("c_%d",i),800,400); c[i]->Divide(2,1); } for(int i=0;i<npt;i++) { c[i]->cd(1)->SetTopMargin(0.06); c[i]->cd(1)->SetLeftMargin(0.18); c[i]->cd(1)->SetRightMargin(0.043); c[i]->cd(1)->SetBottomMargin(0.145); c[i]->cd(2)->SetTopMargin(0.06); c[i]->cd(2)->SetLeftMargin(0.18); c[i]->cd(2)->SetRightMargin(0.043); c[i]->cd(2)->SetBottomMargin(0.145); } TCanvas* c2 = new TCanvas("c2","c2",100,100); TLatex* tex = new TLatex; tex->SetNDC(); tex->SetTextFont(42); tex->SetTextSize(0.045); tex->SetLineWidth(2); TLatex* texCMS = new TLatex; texCMS->SetNDC(); texCMS->SetTextFont(42); texCMS->SetTextSize(0.05); texCMS->SetTextAlign(12); TH1D* hist = new TH1D("hist","",10,2.6,3.5); hist->SetLineWidth(0); //hist->GetYaxis()->SetRangeUser(0,0.3); hist->GetXaxis()->SetTitle("#it{m}_{#mu#mu} (GeV)"); hist->GetYaxis()->SetTitle("v_{2}^{S+B}"); hist->GetXaxis()->CenterTitle(); hist->GetYaxis()->CenterTitle(); hist->GetXaxis()->SetTitleOffset(1.3); hist->GetYaxis()->SetTitleOffset(2); hist->GetXaxis()->SetLabelOffset(0.007); hist->GetYaxis()->SetLabelOffset(0.007); hist->GetXaxis()->SetTitleSize(0.045); hist->GetYaxis()->SetTitleSize(0.045); hist->GetXaxis()->SetTitleFont(42); hist->GetYaxis()->SetTitleFont(42); hist->GetXaxis()->SetLabelFont(42); hist->GetYaxis()->SetLabelFont(42); hist->GetXaxis()->SetLabelSize(0.04); hist->GetYaxis()->SetLabelSize(0.04); hist->SetMinimum(0.01); hist->SetMaximum(0.33); c2->cd(); hist->Draw(); for(int i=0;i<npt;i++) { TH1D* h_data = (TH1D*)file1->Get(Form("massjpsi_pt%d",i)); h_data->SetMinimum(0); h_data->SetMarkerSize(0.8); h_data->SetMarkerStyle(20); h_data->SetLineWidth(1); h_data->SetOption("e"); h_data->Rebin(2); h_data->GetXaxis()->SetRangeUser(2.6,3.5); h_data->GetXaxis()->SetTitle("#it{m}_{#mu#mu} (GeV)"); h_data->GetYaxis()->SetTitle("Entries / 10 MeV"); h_data->GetXaxis()->CenterTitle(); h_data->GetYaxis()->CenterTitle(); h_data->GetXaxis()->SetTitleOffset(1.3); h_data->GetYaxis()->SetTitleOffset(2); h_data->GetXaxis()->SetLabelOffset(0.007); h_data->GetYaxis()->SetLabelOffset(0.007); h_data->GetXaxis()->SetTitleSize(0.045); h_data->GetYaxis()->SetTitleSize(0.045); h_data->GetXaxis()->SetTitleFont(42); h_data->GetYaxis()->SetTitleFont(42); h_data->GetXaxis()->SetLabelFont(42); h_data->GetYaxis()->SetLabelFont(42); h_data->GetXaxis()->SetLabelSize(0.04); h_data->GetYaxis()->SetLabelSize(0.04); h_data->GetXaxis()->SetNoExponent(true); ((TGaxis*)h_data->GetXaxis())->SetMaxDigits(7); h_data->SetMaximum(h_data->GetMaximum()*1.5); TH1D* h_pt = (TH1D*)file1->Get(Form("Ptjpsi_eff_pt%d",i)); TH1D* h_KET = (TH1D*)file1->Get(Form("KETjpsi_eff_pt%d",i)); pt[i] = h_pt->GetMean(); KET_ncq[i] = h_KET->GetMean()/2.0; c[i]->cd(1); /*p definitions [0] CB1 yield; [1] Common mean of CB and Gaus; [2] CB1 sigma; [3] CB n; [4] CB alpha; [5] CB2 yield; [6] CB2 sigma; [7-10] poly 3; [11] v2 signal; [12-13] v2 bkg; */ TF1* f = new TF1(Form("f_%d",i), crystalball_function_total, fit_range_low, fit_range_high, 11); f->SetLineColor(2); f->SetLineWidth(1); f->SetParNames("CB1_Yield","common_mean","CB1_sigma","CB_N","CB_Alpha","CB2_Yield","CB2_Sigma","Pol0","Pol1","Pol2","Pol3"); //first fit data mass signal + bkg f->SetParameter(0,10000.); f->SetParameter(1,JPsi_mass); f->SetParameter(2,0.03); f->SetParameter(3,1.0); f->SetParameter(4,1.0); f->SetParameter(5,10000); f->SetParameter(6,0.03); f->SetParLimits(2,0.01,0.1); f->SetParLimits(6,0.01,0.1); //fix alpha & n from MC f->FixParameter(4,alpha_fit[i]); f->FixParameter(3,n_fit[i]); f->FixParameter(1,JPsi_mass); //for first few attempt fix mean of gaussian to get reasonable estimation of other pars; later open it up h_data->Fit(Form("f_%d",i),"q","",fit_range_low,fit_range_high); h_data->Fit(Form("f_%d",i),"q","",fit_range_low,fit_range_high); f->ReleaseParameter(1); //now let gaussian mean float h_data->Fit(Form("f_%d",i),"L q","",fit_range_low,fit_range_high); h_data->Fit(Form("f_%d",i),"L q","",fit_range_low,fit_range_high); h_data->Fit(Form("f_%d",i),"L m","",fit_range_low,fit_range_high); //draw D0 signal separately TF1* f1 = new TF1(Form("f_sig_%d",i), crystalball_function_signal, fit_range_low, fit_range_high, 7); f1->SetLineColor(kOrange-3); f1->SetLineWidth(1); f1->SetLineStyle(2); f1->SetFillColorAlpha(kOrange-3,0.3); f1->SetFillStyle(1001); f1->FixParameter(0,f->GetParameter(0)); f1->FixParameter(1,f->GetParameter(1)); f1->FixParameter(2,f->GetParameter(2)); f1->FixParameter(3,f->GetParameter(3)); f1->FixParameter(4,f->GetParameter(4)); f1->FixParameter(5,f->GetParameter(5)); f1->FixParameter(6,f->GetParameter(6)); fmasssig[i] = (TF1*)f1->Clone(); fmasssig[i]->SetName(Form("masssigfcn_pt%d",i)); fmasssig[i]->Write(); f1->Draw("LSAME"); //draw poly bkg separately TF1* f3 = new TF1(Form("f_bkg_%d",i),"[7] + [8]*x + [9]*x*x + [10]*x*x*x", fit_range_low, fit_range_high); f3->SetLineColor(4); f3->SetLineWidth(1); f3->SetLineStyle(2); f3->FixParameter(7,f->GetParameter(7)); f3->FixParameter(8,f->GetParameter(8)); f3->FixParameter(9,f->GetParameter(9)); f3->FixParameter(10,f->GetParameter(10)); fmassbkg[i] = (TF1*)f3->Clone(); fmassbkg[i]->SetName(Form("massbkgfcn_pt%d",i)); fmassbkg[i]->Write(); f3->Draw("LSAME"); tex->DrawLatex(0.22,0.86,"185 #leq N_{trk}^{offline} < 250"); tex->DrawLatex(0.22,0.80,Form("%.1f < p_{T} < %.1f GeV",ptbin[i],ptbin[i+1])); tex->DrawLatex(0.22,0.74,"-2.86 < y_{cm} < -1.86 or 0.94 < y_{cm} < 1.94"); texCMS->DrawLatex(.18,.97,"#font[61]{CMS} #it{Preliminary}"); //texCMS->DrawLatex(.18,.97,"#font[61]{CMS}"); texCMS->DrawLatex(0.73,0.97, "#scale[0.8]{pPb 8.16 TeV}"); TLegend* leg = new TLegend(0.21,0.4,0.5,0.65,NULL,"brNDC"); leg->SetBorderSize(0); leg->SetTextSize(0.045); leg->SetTextFont(42); leg->SetFillStyle(0); leg->AddEntry(h_data,"data","p"); leg->AddEntry(f,"Fit","L"); leg->AddEntry(f1,"J/#psi Signal","f"); leg->AddEntry(f3,"Combinatorial","l"); leg->Draw("SAME"); sigfrac[i] = f1->Integral(2.94,3.24)/f->Integral(2.94,3.24); //c->Print(Form("plots/massfit_pt%d.pdf",i)); //fit vn //[9] is vn_sig //[10-11] is vn bkg, const + linear vn(pT) TGraphErrors* vn_data = (TGraphErrors*)file1->Get(Form("v2_mass_pt%d",i)); c[i]->cd(2); hist->Draw(); TF1* fmass_combinemassvnfit = new TF1(Form("fmass_combinemassvnfit_%d",i),crystalball_function_total, fit_range_low, fit_range_high, 11); TF1* fvn_combinemassvnfit = new TF1(Form("fvn_combinemassvnfit_%d",i), crystalball_function_v2, fit_range_low, fit_range_high, 15); fmass_combinemassvnfit->SetLineColor(2); fmass_combinemassvnfit->SetLineWidth(1); fvn_combinemassvnfit->SetLineColor(2); fvn_combinemassvnfit->SetLineWidth(1); ROOT::Math::WrappedMultiTF1 wfmass_combinemassvnfit(*fmass_combinemassvnfit,1); ROOT::Math::WrappedMultiTF1 wfvn_combinemassvnfit(*fvn_combinemassvnfit,1); ROOT::Fit::DataOptions opt; ROOT::Fit::DataRange range_massfit; range_massfit.SetRange(fit_range_low,fit_range_high); ROOT::Fit::BinData datamass(opt,range_massfit); ROOT::Fit::FillData(datamass, h_data); ROOT::Fit::DataRange range_vnfit; range_vnfit.SetRange(fit_range_low,fit_range_high); ROOT::Fit::BinData datavn(opt,range_vnfit); ROOT::Fit::FillData(datavn, vn_data); ROOT::Fit::Chi2Function chi2_B(datamass, wfmass_combinemassvnfit); ROOT::Fit::Chi2Function chi2_SB(datavn, wfvn_combinemassvnfit); GlobalChi2_poly3bkg_floatwidth globalChi2(chi2_B, chi2_SB); ROOT::Fit::Fitter fitter; const int Npar = 15; double par0[Npar]; for( int ipar = 0; ipar < f->GetNpar(); ipar++ ) par0[ipar] = f->GetParameter(ipar); par0[11] = 0.01; par0[12] = 0.10; par0[13] = 0.05; par0[14] = 0.01; fitter.Config().SetParamsSettings(Npar,par0); // fix parameter fitter.Config().ParSettings(0).Fix(); fitter.Config().ParSettings(1).Fix(); fitter.Config().ParSettings(2).Fix(); fitter.Config().ParSettings(3).Fix(); fitter.Config().ParSettings(4).Fix(); fitter.Config().ParSettings(5).Fix(); fitter.Config().ParSettings(6).Fix(); fitter.Config().ParSettings(7).Fix(); fitter.Config().ParSettings(8).Fix(); fitter.Config().ParSettings(9).Fix(); fitter.Config().ParSettings(10).Fix(); fitter.Config().MinimizerOptions().SetPrintLevel(0); fitter.Config().SetMinimizer("Minuit2","Migrad"); fitter.FitFCN(Npar,globalChi2,0,datamass.Size()+datavn.Size(),true); ROOT::Fit::FitResult result = fitter.Result(); result.Print(std::cout); fmass_combinemassvnfit->SetFitResult( result, iparmassfit_poly3bkg_floatwidth); fmass_combinemassvnfit->SetRange(range_massfit().first, range_massfit().second); fmass_combinemassvnfit->SetLineColor(kRed); h_data->GetListOfFunctions()->Add(fmass_combinemassvnfit); //c->cd(); //h_data->Draw(); fvn_combinemassvnfit->SetFitResult( result, iparvnfit_poly3bkg_floatwidth); fvn_combinemassvnfit->SetRange(range_vnfit().first, range_vnfit().second); fvn_combinemassvnfit->SetLineColor(2); //fvn_combinemassvnfit->SetLineStyle(2); vn_data->GetListOfFunctions()->Add(fvn_combinemassvnfit); vn_data->SetTitle(""); vn_data->SetMarkerSize(0.8); vn_data->SetLineWidth(1); //c1->cd(); vn_data->Draw("PESAME"); fvn[i] = (TF1*)fvn_combinemassvnfit->Clone(); fvn[i]->SetName(Form("vnfit_pt%d",i)); fvn[i]->Write(); fmasstotal[i] = (TF1*)fmass_combinemassvnfit->Clone(); fmasstotal[i]->SetName(Form("masstotalfcn_pt%d",i)); fmasstotal[i]->Write(); tex->DrawLatex(0.22,0.86,"185 #leq N_{trk}^{offline} < 250"); tex->DrawLatex(0.22,0.80,Form("%.1f < p_{T} < %.1f GeV",ptbin[i],ptbin[i+1])); //tex->DrawLatex(0.22,0.74,"1.4 < |y_{cm}+0.46| < 2.4"); tex->DrawLatex(0.22,0.74,"-2.86 < y_{cm} < -1.86 or 0.94 < y_{cm} < 1.94"); //tex->DrawLatex(0.22,0.68,"|#Delta#eta| > 2"); //texCMS->DrawLatex(.18,.97,"#font[61]{CMS}"); texCMS->DrawLatex(.18,.97,"#font[61]{CMS} #it{Preliminary}"); texCMS->DrawLatex(0.73,0.97, "#scale[0.8]{pPb 8.16 TeV}"); v2[i] = fvn_combinemassvnfit->GetParameter(11); v2e[i] = fvn_combinemassvnfit->GetParError(11); v2_bkg[i] = fvn_combinemassvnfit->GetParameter(12) + fvn_combinemassvnfit->GetParameter(13) * JPsi_mass; v2_ncq[i] = v2[i]/2.0; v2e_ncq[i] = v2e[i]/2.0; a[i] = fvn_combinemassvnfit->GetParameter(12); b[i] = fvn_combinemassvnfit->GetParameter(13); TF1* fvnbkg = new TF1(Form("fvnbkg_%d",1),"( [0] + [1] * x)", fit_range_low, fit_range_high); fvnbkg->FixParameter(0,fvn_combinemassvnfit->GetParameter(12)); fvnbkg->FixParameter(1,fvn_combinemassvnfit->GetParameter(13)); fvnbkg->SetName(Form("fvnbkg_fcn_pt%d",i)); fvnbkg->Write(); fvnbkg->SetLineStyle(7); //fvnbkg->Draw("LSAME"); TF1* fvnsig = new TF1(Form("fvnsig_%d",i),function_v2_sig,fit_range_low,fit_range_high,12); for(int k=0;k<12;k++) { fvnsig->FixParameter(k,fvn_combinemassvnfit->GetParameter(k)); } fvnsig->SetLineColor(kOrange-3); fvnsig->SetLineWidth(1); fvnsig->SetLineStyle(2); fvnsig->SetFillColorAlpha(kOrange-3,0.3); fvnsig->SetFillStyle(1001); //fvnsig->Draw("LSAME"); TLegend* leg1 = new TLegend(0.72,0.525,0.91,0.65,NULL,"brNDC"); leg1->SetBorderSize(0); leg1->SetTextSize(0.045); leg1->SetTextFont(42); leg1->SetFillStyle(0); leg1->AddEntry(h_data,"data","p"); leg1->AddEntry(fvn_combinemassvnfit,"Fit","l"); //leg1->AddEntry(fvnsig,"#alpha(#it{m}_{#mu#mu})v_{2}^{S}","f"); leg1->Draw("SAME"); double xmass[200]; double pullmass[200]; float Chi2=0; int ndf = (fit_range_high - fit_range_low)/0.01 - 8; for(int k=0;k<h_data->GetNbinsX();k++) { xmass[k] = h_data->GetBinCenter(k); pullmass[k] = (h_data->GetBinContent(k) - fmass_combinemassvnfit->Eval(xmass[k]))/h_data->GetBinError(k); if(fabs(pullmass[k])<5) { //cout<<pullmass[k]<<endl; Chi2 += pullmass[k]*pullmass[k]; } } c[i]->cd(1); tex->DrawLatex(0.22,0.67,Form("#chi^{2}/ndf = %.0f/%d",Chi2,ndf)); double xv2[200]; double pullv2[200]; double v2y[200]; float Chi2v2=0; int ndfv2 = 8 - 4; //Nbin - Npar for(int k=0;k<vn_data->GetN()-1;k++) { vn_data->GetPoint(k,xv2[k],v2y[k]); //xv2[k] = vn_dara->GetBinCenter(k); pullv2[k] = (v2y[k] - fvn_combinemassvnfit->Eval(xv2[k]))/vn_data->GetErrorY(k); cout<<k<<": "<<pullv2[k]<<endl; if(fabs(pullv2[k])<1000) { //cout<<pullmass[k]<<endl; Chi2v2 += pullv2[k]*pullv2[k]; } cout<<"fcn: "<<fvn_combinemassvnfit->Eval(xv2[k])<<endl; cout<<"data: "<<v2y[k]<<endl; } c[i]->cd(2); tex->DrawLatex(0.22,0.67,Form("#chi^{2}/ndf = %.1f/%d",Chi2v2,ndfv2)); } for(int i=0;i<npt;i++) { c[i]->Print(Form("plots/v30/eff/exp/JPsi_mass_vnfit_combine_pt%d.pdf",i)); c[i]->Print(Form("plots/v30/eff/exp/JPsi_mass_vnfit_combine_pt%d.gif",i)); } TGraphErrors* v2plot = new TGraphErrors(npt,pt,v2,0,v2e); TGraphErrors* v2ncqplot = new TGraphErrors(npt,KET_ncq,v2_ncq,0,v2e_ncq); TGraphErrors* v2bkgplot = new TGraphErrors(npt,pt,v2_bkg,0,0); v2plot->SetName("v2vspt"); v2ncqplot->SetName("v2vsKET_ncq"); v2bkgplot->SetName("v2bkgvspt"); v2plot->Write(); v2ncqplot->Write(); v2bkgplot->Write(); }
void combinedFit() { TH1D * hB = new TH1D("hB","histo B",100,0,100); TH1D * hSB = new TH1D("hSB","histo S+B",100, 0,100); TF1 * fB = new TF1("fB","expo",0,100); fB->SetParameters(1,-0.05); hB->FillRandom("fB"); TF1 * fS = new TF1("fS","gaus",0,100); fS->SetParameters(1,30,5); hSB->FillRandom("fB",2000); hSB->FillRandom("fS",1000); // perform now global fit TF1 * fSB = new TF1("fSB","expo + gaus(2)",0,100); ROOT::Math::WrappedMultiTF1 wfB(*fB,1); ROOT::Math::WrappedMultiTF1 wfSB(*fSB,1); ROOT::Fit::DataOptions opt; ROOT::Fit::DataRange rangeB; // set the data range rangeB.SetRange(10,90); ROOT::Fit::BinData dataB(opt,rangeB); ROOT::Fit::FillData(dataB, hB); ROOT::Fit::DataRange rangeSB; rangeSB.SetRange(10,50); ROOT::Fit::BinData dataSB(opt,rangeSB); ROOT::Fit::FillData(dataSB, hSB); ROOT::Fit::Chi2Function chi2_B(dataB, wfB); ROOT::Fit::Chi2Function chi2_SB(dataSB, wfSB); GlobalChi2 globalChi2(chi2_B, chi2_SB); ROOT::Fit::Fitter fitter; const int Npar = 6; double par0[Npar] = { 5,5,-0.1,100, 30,10}; // create before the parameter settings in order to fix or set range on them fitter.Config().SetParamsSettings(6,par0); // fix 5-th parameter fitter.Config().ParSettings(4).Fix(); // set limits on the third and 4-th parameter fitter.Config().ParSettings(2).SetLimits(-10,-1.E-4); fitter.Config().ParSettings(3).SetLimits(0,10000); fitter.Config().ParSettings(3).SetStepSize(5); fitter.Config().MinimizerOptions().SetPrintLevel(0); fitter.Config().SetMinimizer("Minuit2","Migrad"); // fit FCN function directly // (specify optionally data size and flag to indicate that is a chi2 fit) fitter.FitFCN(6,globalChi2,0,dataB.Size()+dataSB.Size(),true); ROOT::Fit::FitResult result = fitter.Result(); result.Print(std::cout); TCanvas * c1 = new TCanvas("Simfit","Simultaneous fit of two histograms", 10,10,700,700); c1->Divide(1,2); c1->cd(1); gStyle->SetOptFit(1111); fB->SetFitResult( result, iparB); fB->SetRange(rangeB().first, rangeB().second); fB->SetLineColor(kBlue); hB->GetListOfFunctions()->Add(fB); hB->Draw(); c1->cd(2); fSB->SetFitResult( result, iparSB); fSB->SetRange(rangeSB().first, rangeSB().second); fSB->SetLineColor(kRed); hSB->GetListOfFunctions()->Add(fSB); hSB->Draw(); }
Int_t line3Dfit() { gStyle->SetOptStat(0); gStyle->SetOptFit(); //double e = 0.1; Int_t nd = 10000; // double xmin = 0; double ymin = 0; // double xmax = 10; double ymax = 10; TGraph2D * gr = new TGraph2D(); // Fill the 2D graph double p0[4] = {10,20,1,2}; // generate graph with the 3d points for (Int_t N=0; N<nd; N++) { double x,y,z = 0; // Generate a random number double t = gRandom->Uniform(0,10); line(t,p0,x,y,z); double err = 1; // do a gaussian smearing around the points in all coordinates x += gRandom->Gaus(0,err); y += gRandom->Gaus(0,err); z += gRandom->Gaus(0,err); gr->SetPoint(N,x,y,z); //dt->SetPointError(N,0,0,err); } // fit the graph now ROOT::Fit::Fitter fitter; // make the functor objet SumDistance2 sdist(gr); #ifdef __CINT__ ROOT::Math::Functor fcn(&sdist,4,"SumDistance2"); #else ROOT::Math::Functor fcn(sdist,4); #endif // set the function and the initial parameter values double pStart[4] = {1,1,1,1}; fitter.SetFCN(fcn,pStart); // set step sizes different than default ones (0.3 times parameter values) for (int i = 0; i < 4; ++i) fitter.Config().ParSettings(i).SetStepSize(0.01); bool ok = fitter.FitFCN(); if (!ok) { Error("line3Dfit","Line3D Fit failed"); return 1; } const ROOT::Fit::FitResult & result = fitter.Result(); std::cout << "Total final distance square " << result.MinFcnValue() << std::endl; result.Print(std::cout); gr->Draw("p0"); // get fit parameters const double * parFit = result.GetParams(); // draw the fitted line int n = 1000; double t0 = 0; double dt = 10; TPolyLine3D *l = new TPolyLine3D(n); for (int i = 0; i <n;++i) { double t = t0+ dt*i/n; double x,y,z; line(t,parFit,x,y,z); l->SetPoint(i,x,y,z); } l->SetLineColor(kRed); l->Draw("same"); // draw original line TPolyLine3D *l0 = new TPolyLine3D(n); for (int i = 0; i <n;++i) { double t = t0+ dt*i/n; double x,y,z; line(t,p0,x,y,z); l0->SetPoint(i,x,y,z); } l0->SetLineColor(kBlue); l0->Draw("same"); return 0; }
void view_SMEvents_3D_from_Hits() { /*** Displays an 3D occupancy plot for each SM Event. (stop mode event) Can choose which SM event to start at. (find "CHOOSE THIS" in this script) Input file must be a Hits file (_interpreted_Hits.root file). ***/ gROOT->Reset(); // Setting up file, treereader, histogram TFile *f = new TFile("/home/pixel/pybar/tags/2.0.2_new/pyBAR-master/pybar/module_202_new/101_module_202_new_stop_mode_ext_trigger_scan_interpreted_Hits.root"); if (!f) { // if we cannot open the file, print an error message and return immediately cout << "Error: cannot open the root file!\n"; //return; } TTreeReader *reader = new TTreeReader("Table", f); TTreeReaderValue<UInt_t> h5_file_num(*reader, "h5_file_num"); TTreeReaderValue<Long64_t> event_number(*reader, "event_number"); TTreeReaderValue<UChar_t> tot(*reader, "tot"); TTreeReaderValue<UChar_t> relative_BCID(*reader, "relative_BCID"); TTreeReaderValue<Long64_t> SM_event_num(*reader, "SM_event_num"); TTreeReaderValue<Double_t> x(*reader, "x"); TTreeReaderValue<Double_t> y(*reader, "y"); TTreeReaderValue<Double_t> z(*reader, "z"); // Initialize the canvas and graph TCanvas *c1 = new TCanvas("c1","3D Occupancy for Specified SM Event", 1000, 10, 900, 550); c1->SetRightMargin(0.25); TGraph2D *graph = new TGraph2D(); // Variables used to loop the main loop bool endOfReader = false; // if reached end of the reader bool quit = false; // if pressed q int smEventNum = 1; // the current SM-event CHOOSE THIS to start at desired SM event number // Main Loop (loops for every smEventNum) while (!endOfReader && !quit) { // Variables used in this main loop int startEntryNum = 0; int endEntryNum = 0; string histTitle = "3D Occupancy for SM Event "; string inString = ""; bool fitFailed = false; // true if the 3D fit failed bool lastEvent = false; // Declaring some important output values for the current graph and/or line fit int numEntries = 0; double sumSquares = 0; // Get startEntryNum and endEntryNum startEntryNum = getEntryNumWithSMEventNum(reader, smEventNum); endEntryNum = getEntryNumWithSMEventNum(reader, smEventNum + 1); if (startEntryNum == -2) { // can't find the smEventNum cout << "Error: There should not be any SM event numbers that are missing." << "\n"; } else if (startEntryNum == -3) { endOfReader = true; break; } else if (endEntryNum == -3) { // assuming no SM event nums are skipped endEntryNum = reader->GetEntries(false); lastEvent = true; } // Fill TGraph with points and set title and axes graph = new TGraph2D(); // create a new TGraph to refresh reader->SetEntry(startEntryNum); for (int i = 0; i < endEntryNum - startEntryNum; i++) { graph->SetPoint(i, (*x - 0.001), (*y + 0.001), (*z - 0.001)); endOfReader = !(reader->Next()); } histTitle.append(to_string(smEventNum)); graph->SetTitle(histTitle.c_str()); graph->GetXaxis()->SetTitle("x (mm)"); graph->GetYaxis()->SetTitle("y (mm)"); graph->GetZaxis()->SetTitle("z (mm)"); graph->GetXaxis()->SetLimits(0, 20); // ROOT is buggy, x and y use setlimits() graph->GetYaxis()->SetLimits(-16.8, 0); // but z uses setrangeuser() graph->GetZaxis()->SetRangeUser(0, 40.96); c1->SetTitle(histTitle.c_str()); // 3D Fit, display results, draw graph and line fit, only accept "good" events, get input if (!endOfReader || lastEvent) { // Display some results numEntries = graph->GetN(); cout << "Current SM Event Number: " << smEventNum << "\n"; cout << "Number of entries: " << numEntries << "\n"; // Starting the fit. First, get decent starting parameters for the fit - do two 2D fits (one for x vs z, one for y vs z) TGraph *graphZX = new TGraph(); TGraph *graphZY = new TGraph(); reader->SetEntry(startEntryNum); for (int i = 0; i < endEntryNum - startEntryNum; i++) { graphZX->SetPoint(i, (*z - 0.001), (*x + 0.001)); graphZY->SetPoint(i, (*z - 0.001), (*y + 0.001)); reader->Next(); } TFitResultPtr fitZX = graphZX->Fit("pol1", "WQS"); // w for ignore error of each pt, q for quiet (suppress results output), s for return a tfitresultptr TFitResultPtr fitZY = graphZY->Fit("pol1", "WQS"); Double_t param0 = fitZX->GetParams()[0]; Double_t param1 = fitZX->GetParams()[1]; Double_t param2 = fitZY->GetParams()[0]; Double_t param3 = fitZY->GetParams()[1]; // // Draw the lines for the two 2D fits // int n = 2; // TPolyLine3D *lineZX = new TPolyLine3D(n); // TPolyLine3D *lineZY = new TPolyLine3D(n); // lineZX->SetPoint(0, param0, 0, 0); // lineZX->SetPoint(1, param0 + param1 * 40.96, 0, 40.96); // lineZX->SetLineColor(kBlue); // lineZX->Draw("same"); // lineZY->SetPoint(0, 0, param2, 0); // lineZY->SetPoint(1, 0, param2 + param3 * 40.96, 40.96); // lineZY->SetLineColor(kGreen); // lineZY->Draw("same"); // 3D FITTING CODE (based on line3Dfit.C), draw graph and line fit ROOT::Fit::Fitter fitter; SumDistance2 sdist(graph); #ifdef __CINT__ ROOT::Math::Functor fcn(&sdist,4,"SumDistance2"); #else ROOT::Math::Functor fcn(sdist,4); #endif // set the function and the initial parameter values double pStart[4] = {param0,param1,param2,param3}; fitter.SetFCN(fcn,pStart); // set step sizes different than default ones (0.3 times parameter values) for (int i = 0; i < 4; ++i) fitter.Config().ParSettings(i).SetStepSize(0.01); bool ok = fitter.FitFCN(); if (!ok) { Error("line3Dfit","Line3D Fit failed"); fitFailed = true; } else { const ROOT::Fit::FitResult & result = fitter.Result(); const double * fitParams = result.GetParams(); sumSquares = result.MinFcnValue(); std::cout << "Sum of distance squares: " << sumSquares << std::endl; std::cout << "Sum of distance squares divided by numEntries: " << sumSquares/numEntries << std::endl; std::cout << "Theta : " << TMath::ATan(sqrt(pow(fitParams[1], 2) + pow(fitParams[3], 2))) << std::endl; // result.Print(std::cout); // (un)suppress results output // Draw the graph graph->SetMarkerStyle(8); graph->SetMarkerSize(0.5); graph->Draw("pcol"); // Draw the fitted line int n = 1000; double t0 = 0; // t is the z coordinate double dt = 40.96; TPolyLine3D *l = new TPolyLine3D(n); for (int i = 0; i <n;++i) { double t = t0+ dt*i/n; double x,y,z; line(t,fitParams,x,y,z); l->SetPoint(i,x,y,z); } l->SetLineColor(kRed); l->Draw("same"); // Access fit params and minfcnvalue // cout << "FIT1: " << fitParams[1] << "\n"; // cout << "FIT2: " << result.MinFcnValue() << "\n"; } // Criteria to be a good event (if not good entry, then don't show) bool isGoodEvent = false; // the following block of code finds the mean X, Y ans Z values double meanX = 0; double meanY = 0; double meanZ = 0; reader->SetEntry(startEntryNum); for (int i = 0; i < endEntryNum - startEntryNum; i++) { meanX += graph->GetX()[i]; meanY += graph->GetY()[i]; meanZ += graph->GetZ()[i]; reader->Next(); } meanX /= endEntryNum - startEntryNum; meanY /= endEntryNum - startEntryNum; meanZ /= endEntryNum - startEntryNum; // the following code block calculates the fraction of the hits in the smEvent that are inside a sphere, centered at the mean XYZ, of radius 'radius' (larger fraction means the track is less like a long streak and more like a dense blob) double radius = 1; // length in mm double fractionInsideSphere = 0; reader->SetEntry(startEntryNum); for (int i = 0; i < endEntryNum - startEntryNum; i++) { double distanceFromMeanXYZ = sqrt(pow(graph->GetX()[i] - meanX, 2) + pow(graph->GetY()[i] - meanY, 2) + pow(graph->GetZ()[i] - meanZ, 2)); if (distanceFromMeanXYZ <= 2) { fractionInsideSphere += 1; } reader->Next(); } fractionInsideSphere /= endEntryNum - startEntryNum; cout << "fraction inside sphere: " << fractionInsideSphere << "\n"; // if (numEntries >= 50 // && sumSquares/numEntries < 2.0 // && fractionInsideSphere < 0.8) { // isGoodEvent = true; // } isGoodEvent = true; if (isGoodEvent) { // won't show drawings or ask for input unless its a good event c1->Update(); // show all the drawings // handle input bool inStringValid = false; do { cout << "<Enter>: next event; 'b': previous SM event; [number]: specific SM event number; 'q': quit.\n"; getline(cin, inString); // Handles behavior according to input if (inString.empty()) { // <Enter> // leave things be inStringValid = true; } else if (inString.compare("b") == 0) { smEventNum -= 2; // because it gets incremented once at the end of this do while loop inStringValid = true; } else if (inString.compare("q") == 0 || inString.compare(".q") == 0) { quit = true; inStringValid = true; } else if (canConvertStringToPosInt(inString)) { smEventNum = convertStringToPosInt(inString) - 1; // -1 because it gets incremented once at the end of this do while loop inStringValid = true; } // else, leave inStringValid as false, so that it asks for input again } while (!inStringValid); } else { cout << "\n"; } } smEventNum++; } cout << "Exiting program.\n"; }
TF1* fit_histo_poly3bkg_floatwidth_poly2bkg_combinemassvnfit( bool isPbPb, int centlow, int centhigh, TH1D * histo, TH1D * h_mc_matched_signal, TH1D * h_mc_matched_kpiswapped, int ipt, TString cfgname, bool get_sig_bkg_ratio = false, TH1D * Ratio_signal_foreground = NULL, TH1D * h_vnvsmass = NULL, TH1D * h_vnvspt = NULL, TString vnorder = "v2", TString EPorSP = "SP", TH1D * h_vnvspt_bkg = NULL) { Double_t setparam0=100.; Double_t setparam1=1.8648; Double_t setparam2=0.03; Double_t setparam3=0.005; Double_t setparam4=0.1; Double_t setparam7=0.1; Double_t fixparam1=1.8648; double fit_range_low = generalfitrange_masslow; double fit_range_high = generalfitrange_masshigh; double histomassbinsize = histo->GetBinWidth(10); float ptmin = ptbins[ipt]; float ptmax = ptbins[ipt+1]; //remove the fit function from v2 fit when perform v3 fit if( histo->GetListOfFunctions()->FindObject(Form("fmass_combinemassvnfit_%s_%d",cfgname.Data(),ipt)) ) histo->GetListOfFunctions()->Remove( histo->GetListOfFunctions()->FindObject(Form("fmass_combinemassvnfit_%s_%d",cfgname.Data(),ipt)) ); TH1F* histo_copy_nofitfun = ( TH1F * ) histo->Clone("histo_copy_nofitfun"); TH1F* histo_massfit = ( TH1F * ) histo->Clone("histo_massfit"); TCanvas* cfg= new TCanvas(Form("cfg_poly3bkg_floatwidth_poly2bkg_combinemassvnfit_%s_%d",cfgname.Data(),ipt),Form("cfg_poly3bkg_floatwidth_poly2bkg_combinemassvnfit_%s_%d",cfgname.Data(),ipt),600,600); gPad->SetRightMargin(0.043); gPad->SetLeftMargin(0.18); gPad->SetTopMargin(0.1); gPad->SetBottomMargin(0.145); TF1* f = new TF1(Form("f_%s_%d",cfgname.Data(),ipt),"[0]*([5]*([4]*TMath::Gaus(x,[1],[2]*(1.0 +[6]))/(sqrt(2*3.14159)*[2]*(1.0 +[6]))+(1-[4])*TMath::Gaus(x,[1],[3]*(1.0 +[6]))/(sqrt(2*3.14159)*[3]*(1.0 +[6])))+(1-[5])*TMath::Gaus(x,[8],[7]*(1.0 +[6]))/(sqrt(2*3.14159)*[7]*(1.0 +[6]))) + [9] + [10]*x + [11]*x*x + [12]*x*x*x", fit_range_low, fit_range_high); f->SetParLimits(10,-1000,1000); f->SetParLimits(3,0.001,0.05); f->SetParLimits(2,0.01,0.1); f->SetParLimits(7,0.02,0.2); f->SetParLimits(5,0,1); f->SetParLimits(4,0,1); f->SetParameter(0,setparam0); f->SetParameter(1,setparam1); f->SetParameter(2,setparam2); f->SetParameter(3,setparam3); f->SetParameter(4,setparam4); f->FixParameter(7,setparam7); f->FixParameter(8,setparam1); f->FixParameter(5,1); f->FixParameter(1,fixparam1); f->FixParameter(9,0); f->FixParameter(10,0); f->FixParameter(11,0); f->FixParameter(12,0); f->FixParameter(6,0); h_mc_matched_signal->Fit(Form("f_%s_%d",cfgname.Data(),ipt),"q","",fit_range_low,fit_range_high); h_mc_matched_signal->Fit(Form("f_%s_%d",cfgname.Data(),ipt),"q","",fit_range_low,fit_range_high); f->ReleaseParameter(1); h_mc_matched_signal->Fit(Form("f_%s_%d",cfgname.Data(),ipt),"L q","",fit_range_low,fit_range_high); h_mc_matched_signal->Fit(Form("f_%s_%d",cfgname.Data(),ipt),"L q","",fit_range_low,fit_range_high); h_mc_matched_signal->Fit(Form("f_%s_%d",cfgname.Data(),ipt),"L m","",fit_range_low,fit_range_high); f->FixParameter(1,f->GetParameter(1)); f->FixParameter(2,f->GetParameter(2)); f->FixParameter(3,f->GetParameter(3)); f->FixParameter(4,f->GetParameter(4)); f->FixParameter(5,0); f->ReleaseParameter(7); f->ReleaseParameter(8); f->SetParameter(7,setparam7); f->SetParameter(8,setparam1);//mean for swapped candidates //if want to fix parameter 8 to parameter 1 //f->FixParameter(8,f->GetParameter(1)); h_mc_matched_kpiswapped->Fit(Form("f_%s_%d",cfgname.Data(),ipt),"L q","",fit_range_low,fit_range_high); h_mc_matched_kpiswapped->Fit(Form("f_%s_%d",cfgname.Data(),ipt),"L q","",fit_range_low,fit_range_high); h_mc_matched_kpiswapped->Fit(Form("f_%s_%d",cfgname.Data(),ipt),"L q","",fit_range_low,fit_range_high); h_mc_matched_kpiswapped->Fit(Form("f_%s_%d",cfgname.Data(),ipt),"L m","",fit_range_low,fit_range_high); f->FixParameter(5,h_mc_matched_signal->Integral(0,1000)/(h_mc_matched_kpiswapped->Integral(0,1000)+h_mc_matched_signal->Integral(0,1000))); f->FixParameter(7,f->GetParameter(7)); f->FixParameter(8,f->GetParameter(8)); f->ReleaseParameter(9); f->ReleaseParameter(10); f->ReleaseParameter(11); f->ReleaseParameter(12); f->SetLineColor(kRed); histo_massfit->Fit(Form("f_%s_%d",cfgname.Data(),ipt),"q","",fit_range_low,fit_range_high); histo_massfit->Fit(Form("f_%s_%d",cfgname.Data(),ipt),"q","",fit_range_low,fit_range_high); f->ReleaseParameter(1); ////Release Parameter 6 to float signal width f->ReleaseParameter(6); f->SetParameter(6,0); f->SetParLimits(6,-1.0,1.0); histo_massfit->Fit(Form("f_%s_%d",cfgname.Data(),ipt),"L q","",fit_range_low,fit_range_high); histo_massfit->Fit(Form("f_%s_%d",cfgname.Data(),ipt),"L q","",fit_range_low,fit_range_high); histo_massfit->Fit(Form("f_%s_%d",cfgname.Data(),ipt),"L q","",fit_range_low,fit_range_high); histo_massfit->Fit(Form("f_%s_%d",cfgname.Data(),ipt),"L m","",fit_range_low,fit_range_high); //begin combine fit TF1* fmass_combinemassvnfit = new TF1(Form("fmass_combinemassvnfit_%s_%d",cfgname.Data(),ipt),"[0]*([5]*([4]*TMath::Gaus(x,[1],[2]*(1.0 +[6]))/(sqrt(2*3.14159)*[2]*(1.0 +[6]))+(1-[4])*TMath::Gaus(x,[1],[3]*(1.0 +[6]))/(sqrt(2*3.14159)*[3]*(1.0 +[6])))+(1-[5])*TMath::Gaus(x,[8],[7]*(1.0 +[6]))/(sqrt(2*3.14159)*[7]*(1.0 +[6]))) + [9] + [10]*x + [11]*x*x + [12]*x*x*x", fit_range_low, fit_range_high); TF1* fvn_combinemassvnfit = new TF1(Form("fvn_combinemassvnfit_%s_%d",cfgname.Data(),ipt), "( ( [0]*([5]*([4]*TMath::Gaus(x,[1],[2]*(1.0 +[6]))/(sqrt(2*3.14159)*[2]*(1.0 +[6]))+(1-[4])*TMath::Gaus(x,[1],[3]*(1.0 +[6]))/(sqrt(2*3.14159)*[3]*(1.0 +[6])))+(1-[5])*TMath::Gaus(x,[8],[7]*(1.0 +[6]))/(sqrt(2*3.14159)*[7]*(1.0 +[6]))) ) / ( [0]*([5]*([4]*TMath::Gaus(x,[1],[2]*(1.0 +[6]))/(sqrt(2*3.14159)*[2]*(1.0 +[6]))+(1-[4])*TMath::Gaus(x,[1],[3]*(1.0 +[6]))/(sqrt(2*3.14159)*[3]*(1.0 +[6])))+(1-[5])*TMath::Gaus(x,[8],[7]*(1.0 +[6]))/(sqrt(2*3.14159)*[7]*(1.0 +[6]))) + [9] + [10]*x + [11]*x*x + [12]*x*x*x ) ) * [13] + ( 1.0 - ( ( [0]*([5]*([4]*TMath::Gaus(x,[1],[2]*(1.0 +[6]))/(sqrt(2*3.14159)*[2]*(1.0 +[6]))+(1-[4])*TMath::Gaus(x,[1],[3]*(1.0 +[6]))/(sqrt(2*3.14159)*[3]*(1.0 +[6])))+(1-[5])*TMath::Gaus(x,[8],[7]*(1.0 +[6]))/(sqrt(2*3.14159)*[7]*(1.0 +[6]))) ) / ( [0]*([5]*([4]*TMath::Gaus(x,[1],[2]*(1.0 +[6]))/(sqrt(2*3.14159)*[2]*(1.0 +[6]))+(1-[4])*TMath::Gaus(x,[1],[3]*(1.0 +[6]))/(sqrt(2*3.14159)*[3]*(1.0 +[6])))+(1-[5])*TMath::Gaus(x,[8],[7]*(1.0 +[6]))/(sqrt(2*3.14159)*[7]*(1.0 +[6]))) + [9] + [10]*x + [11]*x*x + [12]*x*x*x ) ) ) * ( [14] + [15] * x + [16] * x * x)", fit_range_low, fit_range_high); ROOT::Math::WrappedMultiTF1 wfmass_combinemassvnfit(*fmass_combinemassvnfit,1); ROOT::Math::WrappedMultiTF1 wfvn_combinemassvnfit(*fvn_combinemassvnfit,1); ROOT::Fit::DataOptions opt; ROOT::Fit::DataRange range_massfit; // set the data range range_massfit.SetRange(fit_range_low,fit_range_high); ROOT::Fit::BinData datamass(opt,range_massfit); ROOT::Fit::FillData(datamass, histo); ROOT::Fit::DataRange range_vnfit; range_vnfit.SetRange(fit_range_low,fit_range_high); ROOT::Fit::BinData datavn(opt,range_vnfit); ROOT::Fit::FillData(datavn, h_vnvsmass); ROOT::Fit::Chi2Function chi2_B(datamass, wfmass_combinemassvnfit); ROOT::Fit::Chi2Function chi2_SB(datavn, wfvn_combinemassvnfit); GlobalChi2_poly3bkg_floatwidth_poly2bkg globalChi2(chi2_B, chi2_SB); ROOT::Fit::Fitter fitter; const int Npar = 17; double par0[Npar]; for( int ipar = 0; ipar < f->GetNpar(); ipar++ ) par0[ipar] = f->GetParameter(ipar); par0[13] = 0.01; par0[14] = 0.10; par0[15] = 0.05; par0[16] = 0.05; // create before the parameter settings in order to fix or set range on them fitter.Config().SetParamsSettings(Npar,par0); // fix parameter fitter.Config().ParSettings(2).Fix(); fitter.Config().ParSettings(3).Fix(); fitter.Config().ParSettings(4).Fix(); fitter.Config().ParSettings(5).Fix(); fitter.Config().ParSettings(7).Fix(); fitter.Config().ParSettings(8).Fix(); // set limits on the third and 4-th parameter fitter.Config().ParSettings(1).SetLimits(1.7, 2.0); //fitter.Config().ParSettings(12).SetStepSize(0.005); //fitter.Config().UseWeightCorrection(); fitter.Config().MinimizerOptions().SetPrintLevel(0); fitter.Config().SetMinimizer("Minuit2","Migrad"); // fit FCN function directly // (specify optionally data size and flag to indicate that is a chi2 fit) //fitter.FitFCN(Npar,globalChi2,0,datamass.Size()+datavn.Size(),false); fitter.FitFCN(Npar,globalChi2,0,datamass.Size()+datavn.Size(),true); ROOT::Fit::FitResult result = fitter.Result(); result.Print(std::cout); fmass_combinemassvnfit->SetFitResult( result, iparmassfit_poly3bkg_floatwidth_poly2bkg); fmass_combinemassvnfit->SetRange(range_massfit().first, range_massfit().second); fmass_combinemassvnfit->SetLineColor(kRed); histo->GetListOfFunctions()->Add(fmass_combinemassvnfit); fvn_combinemassvnfit->SetFitResult( result, iparvnfit_poly3bkg_floatwidth_poly2bkg); fvn_combinemassvnfit->SetRange(range_vnfit().first, range_vnfit().second); fvn_combinemassvnfit->SetLineColor(4.0); fvn_combinemassvnfit->SetLineStyle(2); h_vnvsmass->GetListOfFunctions()->Add(fvn_combinemassvnfit); h_vnvspt->SetBinContent( ipt+1, fvn_combinemassvnfit->GetParameter(13)); h_vnvspt->SetBinError( ipt+1, fvn_combinemassvnfit->GetParError(13)); //double x[2] = {1.73, 1.864}; //double error[2]; //does not work //result.GetConfidenceIntervals(2, 1, 1, x, error, 0.683, false); //h_vnvspt_bkg->SetBinContent( ipt+1, fvn_combinemassvnfit->GetParameter(13) + fvn_combinemassvnfit->GetParameter(14) * 1.864); //h_vnvspt_bkg->SetBinError( ipt+1, error[0]); h_vnvspt_bkg->SetBinContent( ipt+1, fvn_combinemassvnfit->GetParameter(14) + fvn_combinemassvnfit->GetParameter(15) * 1.864 + fvn_combinemassvnfit->GetParameter(16) * 1.864 * 1.864); h_vnvspt_bkg->SetBinError( ipt+1, 0. ); TCanvas* cfg_massfit_combinemassvn = new TCanvas(Form("cfg_poly3bkg_floatwidth_poly2bkg_combinemassvnfit_massfit_combinemassvn_%s_%d_%s_%s",cfgname.Data(),ipt,vnorder.Data(),EPorSP.Data()),Form("cfg_poly3bkg_floatwidth_poly2bkg_combinemassvnfit_massfit_combinemassvn_%s_%d_%s_%s",cfgname.Data(),ipt,vnorder.Data(),EPorSP.Data()),600,600); gPad->SetRightMargin(0.043); gPad->SetLeftMargin(0.18); // gPad->SetTopMargin(0.1); gPad->SetBottomMargin(0.145); histo->SetXTitle("m_{#piK} (GeV/c^{2})"); histo->SetYTitle("Entries / (5 MeV/c^{2})"); histo->GetXaxis()->CenterTitle(); histo->GetYaxis()->CenterTitle(); //histo->SetAxisRange(0,histo->GetMaximum()*1.4*1.2,"Y"); histo->GetXaxis()->SetRangeUser(fit_range_low+0.0001,fit_range_high-0.0001); histo->GetXaxis()->SetTitleOffset(1.3); histo->GetYaxis()->SetTitleOffset(1.8); histo->GetXaxis()->SetLabelOffset(0.007); histo->GetYaxis()->SetLabelOffset(0.007); histo->GetXaxis()->SetTitleSize(0.045); histo->GetYaxis()->SetTitleSize(0.045); histo->GetXaxis()->SetTitleFont(42); histo->GetYaxis()->SetTitleFont(42); histo->GetXaxis()->SetLabelFont(42); histo->GetYaxis()->SetLabelFont(42); histo->GetXaxis()->SetLabelSize(0.04); histo->GetYaxis()->SetLabelSize(0.04); histo->SetMarkerSize(0.8); histo->SetMarkerStyle(20); histo->SetStats(0); histo->Draw("e"); TF1* background = new TF1(Form("background_%s_%d",cfgname.Data(),ipt),"[0]+[1]*x+[2]*x*x+[3]*x*x*x"); background->SetParameter(0,fmass_combinemassvnfit->GetParameter(9)); background->SetParameter(1,fmass_combinemassvnfit->GetParameter(10)); background->SetParameter(2,fmass_combinemassvnfit->GetParameter(11)); background->SetParameter(3,fmass_combinemassvnfit->GetParameter(12)); background->SetLineColor(4); background->SetRange(fit_range_low,fit_range_high); background->SetLineStyle(2); TF1* mass = new TF1(Form("fmass_%s_%d",cfgname.Data(),ipt),"[0]*([5]*([4]*Gaus(x,[1],[2]*(1.0 +[6]))/(sqrt(2*3.14159)*[2]*(1.0 +[6]))+(1-[4])*Gaus(x,[1],[3]*(1.0 +[6]))/(sqrt(2*3.14159)*[3]*(1.0 +[6]))))"); mass->SetParameters(f->GetParameter(0),f->GetParameter(1),f->GetParameter(2),f->GetParameter(3),f->GetParameter(4),f->GetParameter(5),f->GetParameter(6)); mass->SetParError(0,f->GetParError(0)); mass->SetParError(1,f->GetParError(1)); mass->SetParError(2,f->GetParError(2)); mass->SetParError(3,f->GetParError(3)); mass->SetParError(4,f->GetParError(4)); mass->SetParError(5,f->GetParError(5)); mass->SetParError(6,f->GetParError(6)); mass->SetFillColor(kOrange-3); mass->SetFillStyle(3002); mass->SetLineColor(kOrange-3); mass->SetLineWidth(3); mass->SetLineStyle(2); TF1* massSwap = new TF1(Form("fmassSwap_%s_%d",cfgname.Data(),ipt),"[0]*(1-[2])*Gaus(x,[1],[3]*(1.0 +[4]))/(sqrt(2*3.14159)*[3]*(1.0 +[4]))"); massSwap->SetParameters(fmass_combinemassvnfit->GetParameter(0),fmass_combinemassvnfit->GetParameter(8),fmass_combinemassvnfit->GetParameter(5),fmass_combinemassvnfit->GetParameter(7),fmass_combinemassvnfit->GetParameter(6)); massSwap->SetParError(0,fmass_combinemassvnfit->GetParError(0)); massSwap->SetParError(1,fmass_combinemassvnfit->GetParError(8)); massSwap->SetParError(2,fmass_combinemassvnfit->GetParError(5)); massSwap->SetParError(3,fmass_combinemassvnfit->GetParError(7)); massSwap->SetParError(4,fmass_combinemassvnfit->GetParError(6)); massSwap->SetFillColor(kGreen+4); massSwap->SetFillStyle(3005); massSwap->SetLineColor(kGreen+4); massSwap->SetLineWidth(3); massSwap->SetLineStyle(1); background->Draw("same"); mass->SetRange(fit_range_low,fit_range_high); mass->Draw("same"); massSwap->SetRange(fit_range_low,fit_range_high); massSwap->Draw("same"); Double_t yield = mass->Integral(fit_range_low,fit_range_high)/histomassbinsize; Double_t yieldErr = mass->Integral(fit_range_low,fit_range_high)/histomassbinsize*mass->GetParError(0)/mass->GetParameter(0); TLegend* leg = new TLegend(0.65,0.58,0.82,0.88,NULL,"brNDC"); leg->SetBorderSize(0); leg->SetTextSize(0.04); leg->SetTextFont(42); leg->SetFillStyle(0); leg->AddEntry(histo,"Data","pl"); leg->AddEntry(fmass_combinemassvnfit,"Fit","l"); leg->AddEntry(mass,"D^{0}+#bar{D^{#lower[0.2]{0}}} Signal","f"); leg->AddEntry(massSwap,"K-#pi swapped","f"); leg->AddEntry(background,"Combinatorial","l"); leg->Draw("same"); TLatex Tl; Tl.SetNDC(); Tl.SetTextAlign(12); Tl.SetTextSize(0.05); Tl.SetTextFont(42); Tl.DrawLatex(0.18,0.965, "#font[61]{CMS}"); if( isPbPb ) Tl.DrawLatex(0.61,0.965, "#scale[0.8]{PbPb #sqrt{s_{NN}} = 5.02 TeV}"); else Tl.DrawLatex(0.65,0.965, "#scale[0.8]{pp #sqrt{s_{NN}} = 5.02 TeV}"); TLatex* tex; if( isPbPb ) { tex = new TLatex(0.22,0.83,"|y| < 1.0"); tex->SetNDC(); tex->SetTextFont(42); tex->SetTextSize(0.04); tex->SetLineWidth(2); tex->Draw(); tex = new TLatex(0.22,0.78,Form("Cent. %d-%d%%", centlow, centhigh)); tex->SetNDC(); tex->SetTextFont(42); tex->SetTextSize(0.04); tex->SetLineWidth(2); tex->Draw(); tex = new TLatex(0.22,0.73,Form("%.1f < p_{T} < %.1f GeV/c",ptmin,ptmax)); tex->SetNDC(); tex->SetTextFont(42); tex->SetTextSize(0.04); tex->SetLineWidth(2); tex->Draw(); tex = new TLatex(0.22,0.68,Form("N_{sig}: %d #pm %d",int(yield),int(yieldErr))); tex->SetNDC(); tex->SetTextFont(42); tex->SetTextSize(0.04); tex->SetLineWidth(2); tex->Draw(); } else { tex = new TLatex(0.22,0.83,"|y| < 1.0"); tex->SetNDC(); tex->SetTextFont(42); tex->SetTextSize(0.04); tex->SetLineWidth(2); tex->Draw(); tex = new TLatex(0.22,0.78,Form("%.1f < p_{T} < %.1f GeV/c",ptmin,ptmax)); tex->SetNDC(); tex->SetTextFont(42); tex->SetTextSize(0.04); tex->SetLineWidth(2); tex->Draw(); tex = new TLatex(0.22,0.73,Form("N_{sig}: %d #pm %d",int(yield),int(yieldErr))); tex->SetNDC(); tex->SetTextFont(42); tex->SetTextSize(0.04); tex->SetLineWidth(2); tex->Draw(); } histo_copy_nofitfun->Draw("esame"); if( get_sig_bkg_ratio ) { for(int ibin = 0; ibin < histo->GetNbinsX(); ibin++) { double foreground = 0.; double signal = 0.; double ratio = 0.; double ratioError = 999.; double massbinleftedge = massmin + histomassbinsize * ibin; double massbinrightedge = massmin + histomassbinsize * (ibin+1); if( massbinleftedge > (fit_range_low - 0.0002) && massbinrightedge < (fit_range_high + 0.0002) ) { foreground = f->Integral(massbinleftedge, massbinrightedge)/histomassbinsize; //foregroundErr = f->IntegralError(massbinleftedge, massbinrightedge)/histomassbinsize; //foreground = histo->Integral(ibin+1, ibin+1); signal = mass->Integral(massbinleftedge, massbinrightedge)/histomassbinsize + massSwap->Integral(massbinleftedge, massbinrightedge)/histomassbinsize; //signal = mass->Integral(massbinleftedge, massbinrightedge)/histomassbinsize; //signal = foreground - background->Integral(massbinleftedge, massbinrightedge)/histomassbinsize; //signalErr = signal * yieldErr/yield; if( foreground > 0 ) { ratio = signal/foreground; ratioError = TMath::Sqrt( foreground * ratio * (1.0 - ratio) ) / foreground; } else { ratio = 0.5; ratioError = 0.5; } } else { ratio = 0.0; ratioError = 1.0; } Ratio_signal_foreground->SetBinContent(ibin+1, ratio); Ratio_signal_foreground->SetBinError(ibin+1, ratioError); } TF1* Func_Ratio_signal_foreground = new TF1(Form("Func_Ratio_signal_foreground_%s_%d",cfgname.Data(),ipt),"([0]*([5]*([4]*TMath::Gaus(x,[1],[2]*(1.0 +[6]))/(sqrt(2*3.14159)*[2]*(1.0 +[6]))+(1-[4])*TMath::Gaus(x,[1],[3]*(1.0 +[6]))/(sqrt(2*3.14159)*[3]*(1.0 +[6])))+(1-[5])*TMath::Gaus(x,[8],[7]*(1.0 +[6]))/(sqrt(2*3.14159)*[7]*(1.0 +[6]))))/([0]*([5]*([4]*TMath::Gaus(x,[1],[2]*(1.0 +[6]))/(sqrt(2*3.14159)*[2]*(1.0 +[6]))+(1-[4])*TMath::Gaus(x,[1],[3]*(1.0 +[6]))/(sqrt(2*3.14159)*[3]*(1.0 +[6])))+(1-[5])*TMath::Gaus(x,[8],[7]*(1.0 +[6]))/(sqrt(2*3.14159)*[7]*(1.0 +[6]))) + [9] + [10]*x + [11]*x*x + [12]*x*x*x)", generalfitrange_masslow, generalfitrange_masshigh); for( int ipar = 0; ipar < 13; ipar++ ) { Func_Ratio_signal_foreground->SetParameter( ipar, f->GetParameter(ipar)); Func_Ratio_signal_foreground->SetParError(ipar, f->GetParError(ipar)); } Func_Ratio_signal_foreground->SetLineColor(2.0); Ratio_signal_foreground->GetListOfFunctions()->Add(Func_Ratio_signal_foreground); } if(isPbPb) { cfg_massfit_combinemassvn->SaveAs(Form("Plots_vn/combinemassvnfit/DMass_combinemassvnfit_isPbPb%d_%s_cent%dto%d_%d_%s_%s_poly3bkg_floatwidth_poly2bkg_combinemassvnfit.pdf", isPbPb, cfgname.Data(), centlow, centhigh, ipt, vnorder.Data(),EPorSP.Data())); cfg_massfit_combinemassvn->SaveAs(Form("Plots_vn/combinemassvnfit/DMass_combinemassvnfit_isPbPb%d_%s_cent%dto%d_%d_%s_%s_poly3bkg_floatwidth_poly2bkg_combinemassvnfit.png", isPbPb, cfgname.Data(), centlow, centhigh, ipt, vnorder.Data(),EPorSP.Data())); } TCanvas* cfg_vnfit_combinemassvn = new TCanvas(Form("cfg_poly3bkg_floatwidth_poly2bkg_combinemassvnfit_vnfit_combinemassvn_%s_%d_%s_%s",cfgname.Data(),ipt,vnorder.Data(),EPorSP.Data()),Form("cfg_poly3bkg_floatwidth_poly2bkg_combinemassvnfit_vnfit_combinemassvn_%s_%d_%s_%s",cfgname.Data(),ipt,vnorder.Data(),EPorSP.Data()),600,600); h_vnvsmass->GetYaxis()->SetRangeUser(-0.2, 0.6); if( vnorder == "v2") h_vnvsmass->GetYaxis()->SetTitle("v_{2}"); if( vnorder == "v3") h_vnvsmass->GetYaxis()->SetTitle("v_{3}"); h_vnvsmass->GetXaxis()->SetTitle("m_{#piK} (GeV/c^{2})"); h_vnvsmass->GetXaxis()->SetTitleSize(0.05); h_vnvsmass->GetYaxis()->SetTitleSize(0.05); h_vnvsmass->SetMarkerColor(4.0); h_vnvsmass->SetLineColor(4.0); h_vnvsmass->SetMarkerStyle(21); h_vnvsmass->SetMarkerSize(1.3); h_vnvsmass->Draw(); TLatex Tl2; Tl2.SetNDC(); Tl2.SetTextAlign(12); Tl2.SetTextSize(0.05); Tl2.SetTextFont(42); Tl2.DrawLatex(0.125,0.965, "#font[61]{CMS}"); Tl2.DrawLatex(0.57,0.965, "#scale[0.8]{PbPb #sqrt{s_{NN}} = 5.02 TeV}"); tex = new TLatex(0.18,0.83,"|y| < 1.0"); tex->SetNDC(); tex->SetTextFont(42); tex->SetTextSize(0.04); tex->SetLineWidth(2); tex->Draw(); tex = new TLatex(0.18,0.78,Form("Cent. %d-%d%%", centlow, centhigh)); tex->SetNDC(); tex->SetTextFont(42); tex->SetTextSize(0.04); tex->SetLineWidth(2); tex->Draw(); tex = new TLatex(0.18,0.73,Form("%.1f < p_{T} < %.1f GeV/c",ptmin,ptmax)); tex->SetNDC(); tex->SetTextFont(42); tex->SetTextSize(0.04); tex->SetLineWidth(2); tex->Draw(); if( vnorder == "v2" ) tex = new TLatex(0.55,0.83,Form("v_{2}^{sig} = %.3f #pm %.3f",fvn_combinemassvnfit->GetParameter(13), fvn_combinemassvnfit->GetParError(13))); else if( vnorder == "v3" ) tex = new TLatex(0.55,0.83,Form("v_{3}^{sig} = %.3f #pm %.3f",fvn_combinemassvnfit->GetParameter(13), fvn_combinemassvnfit->GetParError(13))); tex->SetNDC(); tex->SetTextFont(42); tex->SetTextSize(0.04); tex->SetLineWidth(2); tex->Draw(); if(isPbPb) { cfg_vnfit_combinemassvn->SaveAs(Form("Plots_vn/combinemassvnfit/cfg_vnfit_combinemassvn_%s_cent%dto%d_%d_%s_%s_poly3bkg_floatwidth_poly2bkg_combinemassvnfit.pdf",cfgname.Data(),centlow,centhigh,ipt,vnorder.Data(),EPorSP.Data())); cfg_vnfit_combinemassvn->SaveAs(Form("Plots_vn/combinemassvnfit/cfg_vnfit_combinemassvn_%s_cent%dto%d_%d_%s_%s_poly3bkg_floatwidth_poly2bkg_combinemassvnfit.png",cfgname.Data(),centlow,centhigh,ipt,vnorder.Data(),EPorSP.Data())); } return mass; }
Int_t line3dfit_copy() { gStyle->SetOptStat(0); gStyle->SetOptFit(); //double e = 0.1; // Int_t nd = 5; // double xmin = 0; double ymin = 0; // double xmax = 10; double ymax = 10; TGraph2D * gr = new TGraph2D(); // Fill the 2D graph // double p0[4] = {10,20,1,2}; // generate graph with the 3d points // for (Int_t N=0; N<nd; N++) { // double x,y,z = 0; // // Generate a random number // double t = gRandom->Uniform(0,10); // line(t,p0,x,y,z); // double err = 1; // // do a gaussian smearing around the points in all coordinates // x += gRandom->Gaus(0,err); // y += gRandom->Gaus(0,err); // z += gRandom->Gaus(0,err); // gr->SetPoint(N,x,y,z); // //dt->SetPointError(N,0,0,err); // } // gr->SetPoint(0, 3, 3, 5); // gr->SetPoint(1, 3, 3, 3); // gr->SetPoint(2, 4, 3.5, 5); // gr->SetPoint(3, 6.2, 7.3, 5.8); // gr->SetPoint(4, 9, 8, 7); // gr->SetPoint(5, 5, 5, 5); gr->SetPoint(0, 19, -4.25, 1.92); gr->SetPoint(1, 19, -4.30, 1.92); gr->SetPoint(2, 19, -4.35, 1.92); gr->SetPoint(3, 19, -4.40, 1.92); gr->SetPoint(4, 19, -4.45, 1.92); gr->SetPoint(5, 19, -4.50, 1.92); gr->SetPoint(6, 19, -4.55, 1.92); gr->SetPoint(7, 19, -4.60, 1.92); gr->SetPoint(8, 18.75, -4.30, 1.92); gr->SetPoint(9, 18.75, -4.35, 1.92); gr->SetPoint(10, 18.75, -4.40, 1.92); gr->SetPoint(11, 18.75, -4.45, 1.92); gr->SetPoint(12, 18.75, -4.50, 1.92); gr->SetPoint(13, 18.75, -4.55, 1.92); gr->SetPoint(14, 19, -4.20, 2.08); gr->SetPoint(15, 19, -4.65, 2.08); gr->SetPoint(16, 19, -4.70, 2.08); gr->SetPoint(17, 18.75, -4.25, 2.08); gr->SetPoint(18, 18.75, -4.60, 2.08); // gr->SetPoint(19, 19.001, -4.151, 2.241); // gr->SetPoint(20, 18.751, -4.201, 2.241); gr->SetPoint(19, 19., -4.15, 2.24); gr->SetPoint(20, 18.5723, -4.20, 2.24); // gr->SetMarkerStyle(8); // gr->Draw("p"); // gr->Draw("p0"); // TFitResultPtr fit = gr->Fit("pol1", "WS"); // // fit->Print("V"); // Double_t p0 = fit->Value(0); // Double_t p1 = fit->Value(1); // // draw the line // TPolyLine3D *l = new TPolyLine3D(2); // double dz = 8; // l->SetPoint(0,0,0,p0); // l->SetPoint(1,dz,0,dz * p1); // l->SetLineColor(kRed); // l->Draw("same"); // fit the graph now, and make the functor objet ROOT::Fit::Fitter fitter; SumDistance2 sdist(gr); #ifdef __CINT__ ROOT::Math::Functor fcn(&sdist,4,"SumDistance2"); #else ROOT::Math::Functor fcn(sdist,4); #endif // set the function and the initial parameter values double pStart[4] = {1,1,1,1}; fitter.SetFCN(fcn,pStart); // set step sizes different than default ones (0.3 times parameter values) for (int i = 0; i < 4; ++i) fitter.Config().ParSettings(i).SetStepSize(0.01); bool ok = fitter.FitFCN(); if (!ok) { Error("line3Dfit","Line3D Fit failed"); return 1; } const ROOT::Fit::FitResult & result = fitter.Result(); std::cout << "Total final distance square " << result.MinFcnValue() << std::endl; // result.Print(std::cout); // @@@ suppress output // get fit parameters const double * parFit = result.GetParams(); // draw the fitted line int n = 1000; double t0 = 0; double dt = 10; TPolyLine3D *l = new TPolyLine3D(n); for (int i = 0; i <n;++i) { double t = t0+ dt*i/n; double x,y,z; line(t,parFit,x,y,z); l->SetPoint(i,x,y,z); } l->SetLineColor(kRed); l->Draw("same"); // Access to fit params and minfcnvalue cout << parFit[1] << "\n"; cout << result.MinFcnValue() << "\n"; // // draw original line // TPolyLine3D *l0 = new TPolyLine3D(n); // for (int i = 0; i <n;++i) { // double t = t0+ dt*i/n; // double x,y,z; // line(t,p0,x,y,z); // l0->SetPoint(i,x,y,z); // } // l0->SetLineColor(kBlue); // l0->Draw("same"); return 0; }