// ------------------------------------------------------------------------------------- TF1* readTF1(const TString &fileName, const TString &fName, const TString &newFName) { TFile file(fileName,"READ"); TF1 *f = 0; file.GetObject(fName,f); if( f ) { //f->SetDirectory(0); if( newFName.Length() ) f->SetName(newFName); } else { std::cerr << "ERROR in FileOps::readTF1: TF1 with name '" << fName << "' does not exist in file '" << fileName << "'\n."; } file.Close(); return f; }
TF1* GetFitFunc_ZYAM_pp(TH1D* h) { TH1D* hcorrphi = (TH1D*)h->Clone(h->GetName()); // double histminY = hcorrphi->GetBinContent(hcorrphi->GetMinimumBin()); // double histminX = hcorrphi->GetBinCenter(hcorrphi->GetMinimumBin()); double histminY = hcorrphi->GetBinContent(hcorrphi->FindBin(1.2)); double histminX = hcorrphi->GetBinCenter(hcorrphi->FindBin(1.2)); //hcorrphi->SetAxisRange(-0.01,1.2,"X"); TF1* fitfunc = new TF1(Form("fitfunc_%s",h->GetName()),"[0]+[1]*(x-[2])*(x-[2])+[3]*abs((x-[2])*(x-[2])*(x-[2]))",0.5,2.5); //std 0.6 1.55 vs pT ; 0.6 1.8 vs eta fitfunc->SetParameters(histminY,0.002,histminX,0.0); fitfunc->SetParLimits(1,0,1000); fitfunc->SetParLimits(2,0.2,2.0); // fitfunc->SetParLimits(3,0,100); fitfunc->FixParameter(3,0); for(int ifit=0;ifit<3;ifit++) hcorrphi->Fit(Form("fitfunc_%s",h->GetName()),"NO","",histminX-0.6,histminX+0.6); fitfunc->SetName(Form("fitfunc_%s_%.3f",h->GetName(),fitfunc->GetParameter(0))); return fitfunc; }
void FitWithConstant(TH1D* h1, TFile *out) { Double_t fitRangeLow = 0.; Double_t fitRangeHigh = .4; TF1 *f = new TF1("f","[0]", fitRangeLow, fitRangeHigh); Int_t binLow = h1->FindBin(fitRangeLow); Int_t binHigh = h1->FindBin(fitRangeHigh); cout<<"\t\t********************Fitting with a constant value********************"<<endl<<endl;; h1->Fit(f, "R0"); TString fitName = h1->GetName(); fitName += "Fit"; f->SetName(fitName); f->SetTitle(fitName); // TFile out("Compare.root","update"); TDirectory *dir = out->GetDirectory("Fit"); if(!dir) dir = out->mkdir("Fit"); dir->cd(); // f->SetDirectory(0); f->Write(f->GetName(), TObject::kOverwrite); // out->Close(); }
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(); }
int main(int argc, char **argv) { // one or two input files assert(argc == 2 || argc == 3); // if not one or two input files -> abort std::auto_ptr<TFile> inFile(new TFile(argv[1])); assert(inFile.get()); TH2D *th2 = dynamic_cast<TH2D*>(inFile->Get("jets")); assert(th2); th2->Sumw2(); if (argc == 3) { std::auto_ptr<TFile> inFile2(new TFile(argv[2])); assert(inFile2.get()); TH2D *th2b = dynamic_cast<TH2D*>(inFile2->Get("jets")); assert(th2b); th2b->Sumw2(); th2->SetName("jets_ratio"); th2->Divide(th2b); // if there is a second input file, divide the histogram of the b jet pt/eta through the histogram of the non-b jet pt/eta } TFile g("out.root", "RECREATE"); //rootfile with control plots //create histogram that will contain the eta values of the jets for a certain ptbin (the real value of eta is transformed between -1 and 1 because the Chebychev polynominial is only defined on that range) TH1D *th1[th2->GetNbinsY()]; for(int i = 0; i < th2->GetNbinsY(); i++) { th1[i] = new TH1D(Form("ptslice%d", i), "slice",th2->GetNbinsX(), -1.0, +1.0); //number of bins related to number of etabins in histoJetEtaPt.C th1[i]->SetDirectory(0); } ///////////////// define the fitfunction for the eta distribution in a certain pt slice TF1 *cheb = new TF1("ChebS8", "[0] +" "[1] * (2 * (x^2) - 1) + " "[2] * (8 * (x^4) - 8 * (x^2) + 1) + " "[3] * (32 * (x^6) - 48 * (x^4) + 18 * (x^2) - 1) + " "[4] * (128 * (x^8) - 256 * (x^6) + 160 * (x^4) - 32 * (x^2) + 1) +" "[5] * (512 * (x^10) - 1280 * (x^8) + 1120 * (x^6) - 400 * (x^4) + 50 * (x^2) - 1) +" "[6] * (2048 * (x^12) - 6144 * (x^10) + 6912 * (x^8) - 3584 * (x^6) + 840 * (x^4) - 72 * (x^2) + 1)", -1.0, +1.0); cheb->SetParLimits(0, 0.0, 100000.0); ///////////////// fit the etadistributions for each ptslice and fill the distributions of the fitcoefficients for the different pt slices TH1D *coeffs[7]; for(int i = 0; i < 7; i++) { coeffs[i] = new TH1D(Form("coeff%d", i), "coeffs", th2->GetNbinsY()-2, 0.5, th2->GetNbinsY()-2 + 0.5); //for each coefficient create a histogram with the number of ptbins coeffs[i]->SetDirectory(0); } std::cout << "number of ptbins: " << th2->GetNbinsY() << std::endl; std::cout << "number of etabins: " << th2->GetNbinsX() << std::endl; for(int y = 1; y <= th2->GetNbinsY()-2; y++) { //loop over ptbins for(int x = 1; x <= th2->GetNbinsX(); x++) //loop over etabins { th1[y]->SetBinContent(x, th2->GetBinContent(x, y)); // weight! } th1[y]->SetDirectory(&g); th1[y]->Fit(cheb, "QRNB"); // fit the new histogram with the crazy fitfunction! th1[y]->Write(); cheb->SetName(Form("cheb%d", y)); cheb->Write(); for(int i = 0; i < 7; i++) { coeffs[i]->SetBinContent(y, cheb->GetParameter(i)); coeffs[i]->SetBinError(y, cheb->GetParError(i)); } } ///////////////// fit the coefficients as function of pt double params[7][6]; TF1 *pol[7]; for(int i = 0; i < 7; i++) { //the following piece of code is not relevant: expo, arg and f1 not used afterwards and coeffs[i] is refitted with pol5 /*coeffs[i]->Fit("expo", "Q0"); TF1 *f1 = coeffs[i]->GetFunction("expo"); double expo, arg; if (f1->GetParameter(0) >= -100000.0) { expo = std::exp(f1->GetParameter(0)); arg = f1->GetParameter(1); } else { coeffs[i]->Scale(-1.0); coeffs[i]->Fit("expo", "Q0"); f1 = coeffs[i]->GetFunction("expo"); expo = -std::exp(f1->GetParameter(0)); arg = f1->GetParameter(1); coeffs[i]->Scale(-1.0); }*/ coeffs[i]->Fit("pol5", "0B"); pol[i] = coeffs[i]->GetFunction("pol5"); pol[i]->SetName(Form("pol%d", i)); pol[i]->Write(); for(int j = 0; j < 6; j++) params[i][j] = pol[i]->GetParameter(j); } ///////////////// store the fit"function" in the histogram and calculate the chi2 double chi2 = 0.0; int ndf = 0; TH2D *th2c = new TH2D(*th2); th2c->SetName("fit"); for(int y = 1; y <= th2->GetNbinsY(); y++) { // ptbins int ry = y > th2->GetNbinsY()-4 ? th2->GetNbinsY()-4 : y; for(int x = 1; x <= th2->GetNbinsX(); x++) { // etabins //std::cout << "original histo, now in ptbin y: " << y << " (ry: " << ry <<") and etabin x: " << x << std::endl; //using the coefficients of the fitted functions, the value is calculated (this value will be close to the original value in case 1 rootfile is given and will the close to the original value of the divided histograms if 2 rootfiles are given) double val = compute(params, (x - 0.5) /(0.5*(float) th2->GetNbinsX()) - 1.0, ry); //std::cout << "val " << val << " for " << (x - 0.5) / 25.0 - 1.0 << " and for ry: " << ry << std::endl; if (val < 0) val = 0; double error = th2->GetBinError(x, y); if (error > 0 && val > 0) { double chi = th2->GetBinContent(x, y) - val; chi2 += chi * chi / val; ndf++; } th2c->SetBinContent(x, y, val); } } std::cout << "chi2/ndf(" << ndf << "): " << (chi2 / ndf) << std::endl; ///////////////// writing histos for(int i = 0; i < 7; i++) { coeffs[i]->SetDirectory(&g); coeffs[i]->Write(); } th2->SetDirectory(&g); th2->Write(); //write the original histogram (or the result of the two histograms divided by eachother) th2c->SetDirectory(&g); th2c->Write(); //write the histogram with the new weights g.Close(); ///////////////// writing relevant parameters for reweighting std::ofstream of("out.txt"); of << std::setprecision(16); for(int i = 0; i < 7; i++) for(int j = 0; j < 6; j++) of << params[i][j] << (j < 5 ? "\t" : "\n"); of.close(); return 0; }
void CsIProj() { TFile *file = new TFile("../root/NZ_55_New.root"); TFile *gates = new TFile("../gates/zlines.root"); TFile *gates2 = new TFile("../gates/zlines_new.root"); ofstream ofile("CsI_55A_New.dat"); TCanvas *mycan = (TCanvas*)gROOT->FindObjectAny("mycan"); if(!mycan) { mycan = new TCanvas("mycan","mycan"); mycan->Divide(1,2); } ostringstream outstring; string name; int p1= 30, p2=50; //+- fit limits up to 2 peaks. May be different. int const num_par = 5; //number of peaks times 2(pol1)+3(gaus). for(int ic =0;ic<56;ic++) { outstring.str(""); outstring << "dEE/dEE_" << ic; name = outstring.str(); mycan->cd(1); TH2I *hist = (TH2I*)file->Get(name.c_str()); hist->Draw("col"); hist->GetXaxis()->SetRangeUser(200.,1800.); hist->GetYaxis()->SetRangeUser(5.,50.); if(ic <16 || ic > 31) TCutG *mycut = (TCutG*)gates->Get(Form("Zline_%i_2_4",ic)); else TCutG *mycut = (TCutG*)gates2->Get(Form("Zline_%i_2_4",ic)); mycut->Draw(); file->cd(); outstring.str(""); outstring << "CsI/CsIGate/ECsI_" << ic << "_Gate"; name = outstring.str(); gPad->SetLogz(); mycan->cd(2); TH1I * proj = (TH1I*)file->Get(name.c_str()); proj->Draw(); proj->Rebin(4); proj->GetXaxis()->SetRangeUser(700.,1800.); mycan->Modified(); mycan->Update(); TMarker * mark; mark=(TMarker*)mycan->WaitPrimitive("TMarker"); //Get the Background limits int bkg_lo = mark->GetX(); delete mark; mark=(TMarker*)mycan->WaitPrimitive("TMarker"); int bkg_hi = mark->GetX(); delete mark; mark=(TMarker*)mycan->WaitPrimitive("TMarker"); // Get the 1st peak initial guess int peak1 = mark->GetX(); delete mark; double par[num_par] = {0.}; double out[num_par] = {0.}; int peak1_lo = peak1 - p1, peak1_hi = peak1 + p1; // Peak center and limits TF1 *l1 = new TF1("l1", "pol1", bkg_lo, bkg_hi); TF1 *g1 = new TF1("g1", "gaus", peak1_lo,peak1_hi); TF1 *total = new TF1("total", "pol1(0)+gaus(2)", bkg_lo,bkg_hi); proj->Fit(l1,"R"); proj->Fit(g1,"R+"); l1->GetParameters(&par[0]); g1->GetParameters(&par[2]); total->SetParameters(par); proj->Fit(total,"R"); total->GetParameters(out); ofile << ic << " " << out[3] << endl; outstring.str(""); outstring << "55A_" << ic; name = outstring.str(); total->SetName(name.c_str()); total->Draw("same"); mycan->Modified(); mycan->Update(); bool IsGood = 0; cout << "Good fit?" << endl; cin >> IsGood; if(IsGood) { ofile << ic << " " << out[3] << endl; } else ofile << ic << " " << -1 << endl; } return; }