void fitDebug(string cut, string filename, string hlt, bool binned, string outdir, int isoWeight, int isFPR){ // string outdir = ""; TChain data("myTrees_withWeight"); /* if(hlt=="hltcut30") data.Add("/afs/cern.ch/work/g/gdimperi/GammaJet/giulia_repo/CMSSW_5_3_14/src/GammaJets/src/studioPesi/histo_v6/genIso4/isoWeight/tightPresel2/weights_rebin/data2012ABCD_withWeights_hlt30.root"); if(hlt=="hltcut50") data.Add("/afs/cern.ch/work/g/gdimperi/GammaJet/giulia_repo/CMSSW_5_3_14/src/GammaJets/src/studioPesi/histo_v6/genIso4/isoWeight/tightPresel2/weights_rebin/data2012ABCD_withWeights_hlt50.root"); if(hlt=="hltcut75") data.Add("/afs/cern.ch/work/g/gdimperi/GammaJet/giulia_repo/CMSSW_5_3_14/src/GammaJets/src/studioPesi/histo_v6/genIso4/isoWeight/tightPresel2/weights_rebin/data2012ABCD_withWeights_hlt75.root"); if(hlt=="hltcut90") data.Add("/afs/cern.ch/work/g/gdimperi/GammaJet/giulia_repo/CMSSW_5_3_14/src/GammaJets/src/studioPesi/histo_v6/genIso4/isoWeight/tightPresel2/weights_rebin/data2012ABCD_withWeights_hlt90.root"); if(hlt=="hltcut135") data.Add("/afs/cern.ch/work/g/gdimperi/GammaJet/giulia_repo/CMSSW_5_3_14/src/GammaJets/src/studioPesi/histo_v6/genIso4/isoWeight/tightPresel2/weights_rebin/data2012ABCD_withWeights_hlt135.root"); if(hlt=="hltcut150") data.Add("/afs/cern.ch/work/g/gdimperi/GammaJet/giulia_repo/CMSSW_5_3_14/src/GammaJets/src/studioPesi/histo_v6/genIso4/isoWeight/tightPresel2/weights_rebin/data2012ABCD_withWeights_hlt150.root"); */ if(hlt=="hltcut30") data.Add("/cmshome/gdimperi/GammaJet/CMSSW_6_0_1/src/GammaJets/src/studioPesi/histo_v6/genIso4/isoWeight/tightPresel2/weights_rebin/data2012ABCD_withWeights_hlt30.root"); if(hlt=="hltcut50") data.Add("/cmshome/gdimperi/GammaJet/CMSSW_6_0_1/src/GammaJets/src/studioPesi/histo_v6/genIso4/isoWeight/tightPresel2/weights_rebin/data2012ABCD_withWeights_hlt50.root"); if(hlt=="hltcut75") data.Add("/cmshome/gdimperi/GammaJet/CMSSW_6_0_1/src/GammaJets/src/studioPesi/histo_v6/genIso4/isoWeight/tightPresel2/weights_rebin/data2012ABCD_withWeights_hlt75.root"); if(hlt=="hltcut90") data.Add("/cmshome/gdimperi/GammaJet/CMSSW_6_0_1/src/GammaJets/src/studioPesi/histo_v6/genIso4/isoWeight/tightPresel2/weights_rebin/data2012ABCD_withWeights_hlt90.root"); if(hlt=="hltcut135") data.Add("/cmshome/gdimperi/GammaJet/CMSSW_6_0_1/src/GammaJets/src/studioPesi/histo_v6/genIso4/isoWeight/tightPresel2/weights_rebin/data2012ABCD_withWeights_hlt135.root"); if(hlt=="hltcut150") data.Add("/cmshome/gdimperi/GammaJet/CMSSW_6_0_1/src/GammaJets/src/studioPesi/histo_v6/genIso4/isoWeight/tightPresel2/weights_rebin/data2012ABCD_withWeights_hlt150.root"); RooRealVar combinedPfIso03Phot("combinedPfIso03Phot", "combinedPfIso03Phot", -7., 15.); RooRealVar combinedPfIsoFPR03Phot("combinedPfIsoFPR03Phot", "combinedPfIsoFPR03Phot", -7., 15.); RooRealVar etaPhot("etaPhot", "etaPhot", -2.5, 2.5); RooRealVar mvaIdPhot("mvaIdPhot", "mvaIdPhot", -1.,1.); RooRealVar isMatchedPhot("isMatchedPhot","isMatchedPhot", -1., 2.); RooRealVar isIsolatedGenPhot("isIsolatedGenPhot","isIsolatedGenPhot", -1., 2.); RooRealVar ptPhot("ptPhot", "ptPhot", 0., 1000.); RooRealVar weight("weight","weight", 0., 100.); RooRealVar isoW_EB("isoW_EB","isoW_EB", 0., 100.); RooRealVar isoW1_EB("isoW1_EB","isoW1_EB", 0., 100.); RooRealVar isoW2_EB("isoW2_EB","isoW2_EB", 0., 100.); RooRealVar isoFPRW_EB("isoFPRW_EB","isoW_EB", 0., 100.); RooRealVar isoFPRW1_EB("isoFPRW1_EB","isoFPRW1_EB", 0., 100.); RooRealVar isoFPRW2_EB("isoFPRW2_EB","isoFPRW2_EB", 0., 100.); RooProduct weight_times_isoWeight("weight_times_isoWeight", "weight_times_isoWeight", RooArgSet(weight,isoW_EB)); RooArgSet argSet("argSet"); //creating set of variables for the datasets std::cout<<"Creating RooArgSet with variables for fit"<<std::endl; argSet.add(combinedPfIso03Phot); argSet.add(combinedPfIsoFPR03Phot); argSet.add(etaPhot); argSet.add(mvaIdPhot); argSet.add(isMatchedPhot); argSet.add(isIsolatedGenPhot); argSet.add(ptPhot); argSet.add(weight); argSet.add(isoW_EB); argSet.add(isoW1_EB); argSet.add(isoW2_EB); argSet.add(isoFPRW_EB); argSet.add(isoFPRW1_EB); argSet.add(isoFPRW2_EB); //binning variables //std::cout<<"Binning variables for eventual binned fit"<<std::endl; //combinedPfIsoFPR03Phot.setBins(121); //etaPhot.setBins(120); //mvaIdPhot.setBins(180); //isIsolatedGenPhot.setBins(3); //ptPhot.setBins(1200); //weight_times_isoWeight.setBins(10000000); //creating complete dataset std::cout<<"Reading trees of MC into a complete general dataset"<<std::endl; RooDataSet* allSet; if(isFPR){ if(isoWeight==0) allSet = new RooDataSet("allSet", "allSet", argSet, WeightVar("isoFPRW_EB"), RooFit::Import(data)); if(isoWeight==1) allSet = new RooDataSet("allSet", "allSet", argSet, WeightVar("isoFPRW1_EB"), RooFit::Import(data)); if(isoWeight==2) allSet = new RooDataSet("allSet", "allSet", argSet, WeightVar("isoFPRW2_EB"), RooFit::Import(data)); } else{ if(isoWeight==0) allSet = new RooDataSet("allSet", "allSet", argSet, WeightVar("isoW_EB"), RooFit::Import(data)); if(isoWeight==1) allSet = new RooDataSet("allSet", "allSet", argSet, WeightVar("isoW1_EB"), RooFit::Import(data)); if(isoWeight==2) allSet = new RooDataSet("allSet", "allSet", argSet, WeightVar("isoW2_EB"), RooFit::Import(data)); } //allSet.setWeightVar(weight_times_isoWeight); std::cout<<"Complete dataset "<<allSet->GetName()<<" created"<<std::endl<<std::endl; std::cout<<"******************** "<<std::endl; std::cout<<" isWeighted = " << allSet->isWeighted() <<std::endl; std::cout<<"******************** "<<std::endl<<std::endl; //reducing complete dataset to interesting ones RooDataSet* d_r = (RooDataSet*)allSet->reduce((cut+" && mvaIdPhot<0.6 && mvaIdPhot>-0.6").c_str()); d_r->SetName("d_r"); std::cout<<"Reduced dataset "<<d_r->GetName()<<" created with cut "<<cut<<" && -0.6 < mvaIdPhot < 0.6"<<std::endl; std::cout<<"d_r entries: "<<d_r->sumEntries()<<std::endl; //models for fit //gaussian RooRealVar gaussmean("gaussmean","gaussmean", -1., -10., 20.); RooRealVar gausssigma("gausssigma", "gausssigma", 1., 0., 20.); RooGaussian* my_gauss; if(isFPR) my_gauss = new RooGaussian("my_gauss", "my_gauss", combinedPfIsoFPR03Phot, gaussmean, gausssigma); else my_gauss = new RooGaussian("my_gauss", "my_gauss", combinedPfIso03Phot, gaussmean, gausssigma); //crystalBall RooRealVar cbmean("cbmean", "cbmean", -1., -5., 15.); RooRealVar cbsigma("cbsigma", "cbsigma", 1., 0., 20.); RooRealVar cbalpha("cbalpha", "cbaplha", -1., -1000., 0.); RooRealVar cbn("cbn","cbn",10., 0., 1000.); RooCBShape* my_cb; if(isFPR) my_cb = new RooCBShape("my_cb", "my_cb", combinedPfIsoFPR03Phot, cbmean, cbsigma, cbalpha, cbn); else my_cb = new RooCBShape("my_cb", "my_cb", combinedPfIso03Phot, cbmean, cbsigma, cbalpha, cbn); RooRealVar frac("frac", "frac", 0.5, 0., 1.); //adding gauss to cb for both fit regions RooAddPdf my_add("my_add", "my_add", *my_cb, *my_gauss, frac); //creating simultaneous fit model //RooSimultaneous simPdf("simPdf", "simultaneous pdfs, scut and rcut", sample); //simPdf.addPdf(my_add_s, "scut"); //simPdf.addPdf(my_add_r, "rcut"); //fit RooFitResult* result; //unbinned if(!binned) result = my_add.fitTo(*d_r, Save(), Range(-5.,15.), SumW2Error(kFALSE)); //binned //if(binned) result = simPdf.fitTo(srcut_h, Save(), Range(-5.,15.), SumW2Error(kFALSE)); //drawing results RooPlot* frame_r; if(isFPR) frame_r = combinedPfIsoFPR03Phot.frame(RooFit::Title("Fit to combinedPfIsoFPR03Phot, rcut region")); else frame_r = combinedPfIso03Phot.frame(RooFit::Title("Fit to combinedPfIso03Phot, rcut region")); if(!binned) d_r->plotOn(frame_r, Name("data_rcut")); my_add.plotOn(frame_r,Name("pdf_r"),LineColor(kCyan)) ; my_add.plotOn(frame_r, RooFit::LineColor(kMagenta), Components("my_cb")); my_add.plotOn(frame_r, RooFit::LineColor(kMagenta), Components("my_gauss")); frame_r->SetMinimum(0.00001); TCanvas* c = new TCanvas(); c->SetTitle(frame_r->GetTitle()); frame_r->Draw(""); c->SaveAs((outdir+filename+"_r_dataReweight.png").c_str()); c->SaveAs((outdir+filename+"_r_dataReweight.root").c_str()); //Double_t chi2 = frame_r->chiSquare("pdf_r", "data_rcut", 6); Double_t chi2 = frame_r->chiSquare("pdf_r", "data_rcut", 7); Double_t prob = TMath::Prob(chi2,7); TPaveLabel *t1_r = new TPaveLabel(0.6,0.4,0.83,0.5, Form("#chi^{2}/dof = %.3f", chi2),"brNDC"); t1_r->SetFillColor(0); t1_r->SetLineWidth(0); t1_r->SetLineColor(0); t1_r->SetShadowColor(0); TPaveLabel *t2_r = new TPaveLabel(0.6,0.5,0.83,0.60, Form("Prob(#chi^{2},dof) = %.3f", prob),"brNDC"); t2_r->SetFillColor(0); t2_r->SetLineWidth(0); t2_r->SetLineColor(0); t2_r->SetShadowColor(0); std::cout<<"ChiSquared value, rcut: "<<chi2<<std::endl; c->SetLogy(); frame_r->Draw(""); t1_r->Draw(); t2_r->Draw(); c->SetTitle(frame_r->GetTitle()); c->SaveAs((outdir+filename+"_r_dataRewight_log.png").c_str()); c->SaveAs((outdir+filename+"_r_dataReweight_log.root").c_str()); TFile* f_fitRes = new TFile((outdir+"fitResult_"+filename+"_dataReweight.root").c_str(), "RECREATE"); result->Write(); // f_fitRes->Write(); f_fitRes->Close(); RooWorkspace* w_bg = new RooWorkspace("w_bg", "workspace"); w_bg->import(*d_r); w_bg->import(my_add); w_bg->import(combinedPfIsoFPR03Phot); w_bg->import(combinedPfIso03Phot); w_bg->Print(); w_bg->writeToFile((outdir+"workspace_"+filename+"_dataReweight.root").c_str()); }
void makefit(string inputFilename, string outFilename, Int_t ptBin, Int_t etaBin, Int_t R9Bin, double minMass, double maxMass, double mean_bw, double gamma_bw, double cutoff_cb, double power_cb, const char* plotOpt, const int nbins, Int_t isMC) { // gROOT->ProcessLine(".L tdrstyle.C"); // setTDRStyle(); // gStyle->SetPadRightMargin(0.05); //Create Data Set RooRealVar mass("zmass","m(e^{+}e^{-})",minMass,maxMass,"GeV/c^{2}"); RooRealVar puw("puW","pileup weight",0.,2.); // mass.setRange(80,100); // Reading everything from root tree instead TFile *tfile = TFile::Open(inputFilename.c_str()); TTree *ttree = (TTree*)tfile->Get("zeetree/probe_tree"); hzztree *zeeTree = new hzztree(ttree); TString nF(inputFilename.c_str()); nF.ReplaceAll(".root","_friend.root"); ttree->AddFriend("puweights=zeetree/probe_tree",nF.Data()); float puW; ttree->SetBranchAddress("puW",&puW); RooArgSet zMassArgSet(mass,puw); RooDataSet* data = new RooDataSet("data", "ntuple parameters", zMassArgSet, RooFit::WeightVar("puW")); for (int i = 0; i < zeeTree->fChain->GetEntries(); i++) { if(i%100000==0) cout << "Processing Event " << i << endl; zeeTree->fChain->GetEntry(i); //************************************************************************* //Electron Selection //************************************************************************* // already passed for this tree //************************************************************************* //Compute electron four vector; //************************************************************************* double ele1pt = zeeTree->l1pt; double ele2pt = zeeTree->l2pt; double ELECTRONMASS = 0.51e-3; TLorentzVector ele1FourVector; ele1FourVector.SetPtEtaPhiM(zeeTree->l1pt, zeeTree->l1eta, zeeTree->l1phi, ELECTRONMASS); TLorentzVector ele2FourVector; ele2FourVector.SetPtEtaPhiM(zeeTree->l2pt, zeeTree->l2eta, zeeTree->l2phi, ELECTRONMASS); //************************************************************************* //pt and eta cuts on electron //************************************************************************* if (! (ele1pt > 7 && ele2pt > 7 && fabs( zeeTree->l1eta) < 2.5 && fabs( zeeTree->l2eta) < 2.5 )) continue; //************************************************************************* //pt bins and eta bins //************************************************************************* Int_t Ele1PtBin = -1; Int_t Ele1EtaBin = -1; Int_t Ele2PtBin = -1; Int_t Ele2EtaBin = -1; Int_t Ele1R9Bin = -1; Int_t Ele2R9Bin = -1; if (ele1pt > 20 && ele1pt < 30) Ele1PtBin = 0; else if (ele1pt < 40) Ele1PtBin = 1; else if (ele1pt < 50) Ele1PtBin = 2; else Ele1PtBin = 3; if (ele2pt > 20 && ele2pt < 30) Ele2PtBin = 0; else if (ele2pt < 40) Ele2PtBin = 1; else if (ele2pt < 50) Ele2PtBin = 2; else Ele2PtBin = 3; if (fabs(zeeTree->l1sceta) < 1.479) Ele1EtaBin = 0; else Ele1EtaBin = 1; if (fabs(zeeTree->l2sceta) < 1.479) Ele2EtaBin = 0; else Ele2EtaBin = 1; if (zeeTree->l1r9 > 0.94) Ele1R9Bin = 0; else Ele1R9Bin = 1; if (zeeTree->l2r9 > 0.94) Ele2R9Bin = 0; else Ele2R9Bin = 1; if (!(Ele1PtBin == ptBin || Ele2PtBin == ptBin)) continue; if (!(Ele1EtaBin == etaBin && Ele2EtaBin == etaBin)) continue; if (!(Ele1R9Bin == R9Bin && Ele2R9Bin == R9Bin)) continue; //************************************************************************* // restrict range of mass //************************************************************************* double zMass = (ele1FourVector+ele2FourVector).M(); if (zMass < minMass || zMass > maxMass) continue; //************************************************************************* //set mass variable //************************************************************************* zMassArgSet.setRealValue("zmass", zMass); data->add(zMassArgSet,puW); } cout << "data->isWeighted() = " << data->isWeighted() << endl; // do binned fit to gain time... mass.setBins(nbins); RooDataHist *bdata = new RooDataHist("data_binned","data_binned", zMassArgSet, *data); cout << "bdata->isWeighted() = " << bdata->isWeighted() << endl; cout << "dataset size: " << data->numEntries() << endl; // // Closing file // treeFile->Close(); //====================== Parameters=========================== //Crystal Ball parameters // RooRealVar cbBias ("#Deltam_{CB}", "CB Bias", -.01, -10, 10, "GeV/c^{2}"); // RooRealVar cbSigma("sigma_{CB}", "CB Width", 1.7, 0.8, 5.0, "GeV/c^{2}"); // RooRealVar cbCut ("a_{CB}","CB Cut", 1.05, 1.0, 3.0); // RooRealVar cbPower("n_{CB}","CB Order", 2.45, 0.1, 20.0); RooRealVar cbBias ("#Deltam_{CB}", "CB Bias", -.01, -10, 10, "GeV/c^{2}"); RooRealVar cbSigma("#sigma_{CB}", "CB Width", 1.5, 0.8, 5.0, "GeV/c^{2}"); RooRealVar cbCut ("a_{CB}","CB Cut", 1.0, 1.0, 3.0); RooRealVar cbPower("n_{CB}","CB Order", 2.5, 0.1, 20.0); cbCut.setVal(cutoff_cb); cbPower.setVal(power_cb); // if(!isMC && !(ptBin==0 && etaBin==1 && R9Bin==0)) { // cbCut.setConstant(kTRUE); // cbPower.setConstant(kTRUE); // } // Just checking //cbCut.Print(); //cbPower.Print(); //Breit_Wigner parameters RooRealVar bwMean("m_{Z}","BW Mean", 91.1876, "GeV/c^{2}"); bwMean.setVal(mean_bw); RooRealVar bwWidth("#Gamma_{Z}", "BW Width", 2.4952, "GeV/c^{2}"); bwWidth.setVal(gamma_bw); // Fix the Breit-Wigner parameters to PDG values bwMean.setConstant(kTRUE); bwWidth.setConstant(kTRUE); // Exponential Background parameters RooRealVar expRate("#lambda_{exp}", "Exponential Rate", -0.064, -1, 1); RooRealVar c0("c_{0}", "c0", 1., 0., 50.); //Number of Signal and Background events RooRealVar nsig("N_{S}", "# signal events", 524, 0.1, 10000000000.); RooRealVar nbkg("N_{B}", "# background events", 43, 1., 10000000.); //============================ P.D.F.s============================= // Mass signal for two decay electrons p.d.f. RooBreitWigner bw("bw", "bw", mass, bwMean, bwWidth); RooCBShape cball("cball", "Crystal Ball", mass, cbBias, cbSigma, cbCut, cbPower); RooFFTConvPdf BWxCB("BWxCB", "bw X crystal ball", mass, bw, cball); // Mass background p.d.f. RooExponential bg("bg", "exp. background", mass, expRate); // Mass model for signal electrons p.d.f. RooAddPdf model("model", "signal", RooArgList(BWxCB), RooArgList(nsig)); TStopwatch t ; t.Start() ; RooFitResult *fitres = model.fitTo(*bdata,Hesse(1),Minos(1),Timer(1),Save(1)); fitres->SetName("fitres"); t.Print() ; TCanvas* c = new TCanvas("c","Unbinned Invariant Mass Fit", 0,0,800,600); //========================== Plotting ============================ //Create a frame RooPlot* plot = mass.frame(Range(minMass,maxMass),Bins(nbins)); // Add data and model to canvas int col = (isMC ? kAzure+4 : kGreen+1); data->plotOn(plot); model.plotOn(plot,LineColor(col)); data->plotOn(plot); model.paramOn(plot, Format(plotOpt, AutoPrecision(1)), Parameters(RooArgSet(cbBias, cbSigma, cbCut, cbPower, bwMean, bwWidth, expRate, nsig, nbkg)), Layout(0.15,0.45,0.80)); plot->getAttText()->SetTextSize(.03); plot->SetTitle(""); plot->Draw(); // Print Fit Values TLatex *tex = new TLatex(); tex->SetNDC(); tex->SetTextSize(.1); tex->SetTextFont(132); // tex->Draw(); tex->SetTextSize(0.057); if(isMC) tex->DrawLatex(0.65, 0.75, "Z #rightarrow e^{+}e^{-} MC"); else tex->DrawLatex(0.65, 0.75, "Z #rightarrow e^{+}e^{-} data"); tex->SetTextSize(0.030); tex->DrawLatex(0.645, 0.65, Form("BW Mean = %.2f GeV/c^{2}", bwMean.getVal())); tex->DrawLatex(0.645, 0.60, Form("BW #sigma = %.2f GeV/c^{2}", bwWidth.getVal())); c->Update(); c->SaveAs((outFilename + ".pdf").c_str()); c->SaveAs((outFilename + ".png").c_str()); // tablefile << Form(Outfile + "& $ %f $ & $ %f $ & $ %f $\\ \hline",cbBias.getVal(), cbSigma.getVal(), cbCut.getVal()); // Output workspace with model and data RooWorkspace *w = new RooWorkspace("ZeeMassScaleAndResolutionFit"); w->import(model); w->import(*bdata); w->writeToFile((outFilename + ".root").c_str()); TFile *tfileo = TFile::Open((outFilename + ".root").c_str(),"update"); fitres->Write(); tfileo->Close(); }