vector<TString> list_files(const char *dirname, const char *exp=".*HiForestAOD.*\\.root") { vector<TString> names; TSystemDirectory dir(dirname, dirname); TList *files = dir.GetListOfFiles(); if (files) { TSystemFile *file; TString fname; TIter next(files); while ((file=(TSystemFile*)next())) { fname = file->GetName(); if (!file->IsDirectory() && fname.Contains(TRegexp(exp))) { names.push_back(TString(dirname)+"/"+fname); } } } if (names.size()==0) return {dirname}; return names; }
void SavePerfInfo(const Char_t *filename) { // Save PROOF timing information from TPerfStats to file 'filename' if (!gProof) { cout << "PROOF must be run to save output performance information" << endl; return; } TFile f(filename, "UPDATE"); if (f.IsZombie()) { cout << "Could not open file " << filename << " for writing" << endl; } else { f.cd(); TIter NextObject(gProof->GetOutputList()); TObject* obj = 0; while (obj = NextObject()) { TString objname = obj->GetName(); if (objname.Contains(TRegexp("^PROOF_"))) { // must list the objects since other PROOF_ objects exist // besides timing objects if (objname == "PROOF_PerfStats" || objname == "PROOF_PacketsHist" || objname == "PROOF_EventsHist" || objname == "PROOF_NodeHist" || objname == "PROOF_LatencyHist" || objname == "PROOF_ProcTimeHist" || objname == "PROOF_CpuTimeHist") obj->Write(); } } f.Close(); } }
void plottingmacro_IVF() { double fa = 0.46502; double fb = 0.53498; bool debug_ = true; // std::string path("Nov10thFall11Plots/"); // std::string path("Nov10Fall1160MTopSlimPlots/"); std::string path("Nov10Fall1160MTopIVFPlots_b/"); if(debug_) std::cout << "Init the style form setTDRStyle" << std::endl; setTDRStyle(); gStyle->SetErrorX(0.5); gROOT->ForceStyle(); initOptions(); if(debug_) std::cout << "Init the sample" << std::endl; // std::vector<Sample> s = Nov10thDiJetPtUpdatedSlimHistos(); //std::vector<Sample> s = Nov10Fall1160MTopSlimHistos(); std::vector<Sample> s = Nov10Fall1160MTopIVFHistos(); Sample data(1,"fake data","S1.root",0,true,1000); if(debug_) std::cout << "Init the data sample" << std::endl; for(size_t i=0;i< s.size();i++) if(s[i].data) {data=s[i];break;} if(debug_) std::cout << "Ls data sample" << std::endl; data.file()->ls(); if(debug_) std::cout << "Init the mc sample" << std::endl; for(size_t i=0;i< s.size();i++) s[i].dump(1,fa,fb); std::vector<std::string> names; if(debug_) std::cout << "Get List of Keys" << std::endl; TList * subs = data.file()->GetListOfKeys(); for(size_t i=0;i< subs->GetSize();i++) { TString nn = subs->At(i)->GetName(); if( nn.Contains(TRegexp("Count*")) ) continue; if(debug_) std::cout << "Get List of Keys in subdirs" << std::endl; TList * objs = ((TDirectoryFile *)data.file()->Get(subs->At(i)->GetName()))->GetListOfKeys(); for(size_t j=0;j< objs->GetSize();j++) { if(debug_) std::cout << "Name = " << subs->At(i)->GetName()+std::string("/") + objs->At(j)->GetName() << std::endl; names.push_back(subs->At(i)->GetName()+std::string("/") + objs->At(j)->GetName()); // std::cout << subs->At(i)->GetName() << "/" << objs->At(j)->GetName() << std::endl; //TODO: select plots via regexp } } if(debug_) std::cout << "Starting plotting" << std::endl; std::string process; for(size_t i = 0 ; i < names.size() ; i++) { std::map<std::string,TH1F *> grouped; TString n=names[i]; // if(!n.Contains(TRegexp("VlightRegionHZee/HiggsPtVlightRegionHZee"))) continue; // if(!n.Contains(TRegexp("VlightRegionHZee/ZPtVlightRegionHZee"))) continue; // if(!n.Contains(TRegexp("VlightRegionHZee"))) continue; // if(!n.Contains(TRegexp("ZSVRegionZmmSV"))) continue; // if(!n.Contains(TRegexp("ZSVRegionZeeSV"))) continue; // if(!n.Contains(TRegexp("ZSVRegionZcombSV"))) continue; // if(!n.Contains(TRegexp("ZSVPureRegionZcombSV"))) continue; // if(!n.Contains(TRegexp("ZSVTTbarPureRegionZcombSV"))) continue; if(!n.Contains(TRegexp("TTbarRegionZeeSVJets"))) continue; if(n.Contains(TRegexp("RegionHZcomb"))) process = "Z(l^{+}l^{-})H(b#bar{b})"; if(n.Contains(TRegexp("RegionHZmm"))) process = "Z(#mu^{+}#mu^{-})H(b#bar{b})"; if(n.Contains(TRegexp("RegionHZee"))) process = "Z(e^{+}e^{-})H(b#bar{b})"; if(debug_) std::cout << "Creating the Canvas" << std::endl; TCanvas *c = new TCanvas(); c->SetLogy(false); c->SetTitle(names[i].c_str()); if(debug_) std::cout << "Creating histograms" << std::endl; TH1F *hd = ((TH1F*)data.file()->Get(names[i].c_str())); hd->Sumw2(); Options o=options[names[i]]; // hd->Rebin(o.rebin); hd->SetMarkerStyle(20); hd->GetXaxis()->SetLabelOffset(99); hd->SetYTitle(o.yaxis.c_str()); double nbin = hd->GetNbinsX(); double min_bin = hd->GetXaxis()->GetXmin(); double max_bin = hd->GetXaxis()->GetXmax(); TH1F *hmc = new TH1F("hmc","hmc", nbin, min_bin, max_bin); hmc->SetFillColor(kWhite); hmc->Sumw2(); // hmc->Rebin(o.rebin); if(debug_) std::cout << "Creating the THStack and Legend" << std::endl; THStack * sta = new THStack("sta",hd->GetTitle()); TLegend * l = new TLegend(o.legendx1,o.legendy1,o.legendx2,o.legendy2); //0.7,0.1,0.9,0.6); l->SetFillColor(kWhite); l->SetBorderSize(0); l->SetTextFont(62); l->SetTextSize(0.03); if(debug_) std::cout << "Adding data to the legend" << std::endl; l->AddEntry(hd, "Data","P"); if(debug_) std::cout << "Adding MC to the THStack" << std::endl; //with the proper trigger eff // double SF[] = {1.01,1.03,1.00}; // double SF[] = {1.03,1.054,1.032}; double SF[] = {1.0,1.0,1.0}; if(debug_){ for(int i = 0; i< 3; ++i) std::cout << "SF [" << i << "] = " << SF[i] << std::endl; } double mcIntegral=0; for(size_t j=0;j< s.size() ;j++) { if(!s[j].data) { if(debug_) std::cout << "Creating TH1F from file " << s[j].name << std::endl; TH1F * h = ((TH1F*)s[j].file()->Get(names[i].c_str())); h->Sumw2(); if(debug_){ std::cout << "TH1F created from file " << s[j].name << std::endl; std::cout << "Scaling : " << s[j].scale(data.lumi(),fa,fb) << std::endl; std::cout << "Scaling with SF : " << s[j].scale(data.lumi(),fa,fb,SF) << std::endl; std::cout << "Histo integral before scaling = " << h->Integral() << std::endl; } h->Scale(s[j].scale(data.lumi(),fa,fb,SF)); if(debug_){ std::cout << "Histo integral after scaling = " << h->Integral() << std::endl; std::cout << "Managing style... " << std::endl; } h->SetLineWidth(1.); h->SetFillColor(s[j].color); h->SetLineColor(s[j].color); // h->Rebin(options[names[i]].rebin); if(debug_) std::cout << "Cloning and update legend " << std::endl; if(grouped.find(s[j].name) == grouped.end()){ l->AddEntry(h,s[j].name.c_str(),"F"); } std::cout << "Sample : " << s[j].name << " - Integral for plot " << names[i] << " = " << h->Integral(-10000,10000) << std::endl; mcIntegral += h->Integral(); sta->Add(h); hmc->Add(h); //TO FIX grouped map // sovrascrive histo con lo stesso nome tipo VV o ST etc... grouped[s[j].name]=(TH1F *)h->Clone(("_"+names[i]).c_str()); } } if(debug_){ std::cout << "Data total = " << hd->Integral() << std::endl; std::cout << "MC = " << mcIntegral << std::endl; std::cout << "Data/MC = " << hd->Integral()/mcIntegral << std::endl; } TPad * TopPad = new TPad("TopPad","Top Pad",0.,0.3,1.,1. ) ; TPad * BtmPad = new TPad("BtmPad","Bottom Pad",0.,0.,1.,0.313 ) ; TopPad->SetBottomMargin(0.02); BtmPad->SetTopMargin(0.0); BtmPad->SetFillStyle(4000); TopPad->SetFillStyle(4000); BtmPad->SetFillColor(0); BtmPad->SetBottomMargin(0.35); TopPad->Draw() ; BtmPad->Draw() ; std::cout << "hd maximum = " << hd->GetMaximum() << " sta maximum = " << sta->GetMaximum() << std::endl; double maxY; if(hd->GetMaximum() > sta->GetMaximum()) maxY = (hd->GetMaximum())*1.5; else maxY = (sta->GetMaximum())*1.5; TopPad->cd(); hd->Draw("E1X0"); sta->Draw("sameHIST"); hmc->Draw("sameE2"); hmc->SetFillColor(2); hmc->SetMarkerSize(0); hmc->SetFillStyle(3013); hd->Draw("E1X0same"); l->Draw("same"); std::cout << "Set Maximum to = " << maxY << std::endl; hd->GetYaxis()->SetRangeUser(0.,maxY); hd->GetXaxis()->SetRangeUser(options[names[i]].min,options[names[i]].max); BtmPad->cd(); std::cout << "Division" << std::endl; TH1D * divisionErrorBand = (TH1D*)(hmc)->Clone("divisionErrorBand"); divisionErrorBand->Sumw2(); divisionErrorBand->Divide(hmc); divisionErrorBand->Draw("E2"); divisionErrorBand->SetMaximum(2.49); divisionErrorBand->SetMinimum(0); divisionErrorBand->SetMarkerStyle(20); divisionErrorBand->SetMarkerSize(0.55); divisionErrorBand->GetXaxis()->SetTitleOffset(1.12); divisionErrorBand->GetXaxis()->SetLabelSize(0.12); divisionErrorBand->GetXaxis()->SetTitleSize(0.5); divisionErrorBand->GetYaxis()->SetTitle("Data/MC"); divisionErrorBand->GetYaxis()->SetLabelSize(0.12); divisionErrorBand->GetYaxis()->SetTitleSize(0.12); divisionErrorBand->GetYaxis()->SetTitleOffset(0.40); divisionErrorBand->GetYaxis()->SetNdivisions(505); //divisionErrorBand->UseCurrentStyle(); divisionErrorBand->SetFillColor(2); divisionErrorBand->SetFillStyle(3001); divisionErrorBand->SetMarkerSize(0.); TH1D * division = (TH1D*)(hd)->Clone("division"); division->Sumw2(); division->Divide(hmc); // division->SetMaximum(2.5); // division->SetMinimum(0); // division->SetMarkerStyle(20); // division->SetMarkerSize(0.55); // division->GetXaxis()->SetLabelSize(0.12); // division->GetXaxis()->SetTitleSize(0.14); // division->GetYaxis()->SetLabelSize(0.10); // division->GetYaxis()->SetTitleSize(0.10); // division->GetYaxis()->SetTitle("Data/MC"); Double_t min = division->GetXaxis()->GetXmin(); Double_t max = division->GetXaxis()->GetXmax(); division->Draw("E1X0same"); TLine *line = new TLine(min, 1.0, max, 1.0); line->SetLineColor(kRed); line->Draw("same"); TLegend * leg3 =new TLegend(0.50,0.86,0.69,0.96); leg3->AddEntry(divisionErrorBand,"MC uncert. (stat.)","f"); leg3->SetFillColor(0); leg3->SetLineColor(0); leg3->SetShadowColor(0); leg3->SetTextFont(62); leg3->SetTextSize(0.06); leg3->Draw(); TPaveText *pave = new TPaveText(0.15,0.85,0.32,0.96,"brNDC"); pave->SetTextAlign(12); pave->SetLineColor(0); pave->SetFillColor(0); pave->SetShadowColor(0); //TText *text = pave->AddText(Form("#chi_{#nu}^{2} = %.3f, K_{s} = %.3f",histDt->Chi2Test(histCopyMC5,"UWCHI2/NDF"),histDt->KolmogorovTest(histCopyMC5))); // stat + sys TText *text = pave->AddText(Form("#chi_{#nu}^{2} = %.3f, K_{s} = %.3f",hd->Chi2Test(hmc,"UWCHI2/NDF"),hd->KolmogorovTest(hmc))); // stat only text->SetTextFont(62); text->SetTextSize(0.08); pave->Draw(); TopPad->cd(); TLatex latex; latex.SetNDC(); latex.SetTextAlign(12); latex.SetTextSize(0.052); latex.DrawLatex(0.17,0.89,"CMS Preliminary"); latex.SetTextSize(0.04); latex.DrawLatex(0.17,0.84,"#sqrt{s} = 7 TeV, L = 4.7 fb^{-1}"); // latex.DrawLatex(0.17,0.79,"Z(e^{+}e^{-})H(b#bar{b})"); latex.DrawLatex(0.17,0.79,process.c_str()); c->Update(); std::string cName= hd->GetName(); cName += "_bare.pdf"; cName = path+cName; c->Print(cName.c_str(),"pdf"); // std::cout << names[i] << " d: " << hd->Integral() << " "; // THStack * sta2 = new THStack("sta2",hd->GetTitle()); // float tot=0; // float toterr2=0; // if(debug_) // std::cout << "Putting the iterator in the for loop" << std::endl; // for(std::map<std::string,TH1F *>::reverse_iterator it=grouped.rbegin(); it!=grouped.rend();++it) // { // if(debug_) // std::cout << "Using the iterator" << std::endl; // std::cout << (*it).first << " " << (*it).second->Integral() << " | " << std::endl ; // if((*it).second->GetEntries() > 0) { // float er=1.*sqrt((*it).second->GetEntries())/(*it).second->GetEntries()*(*it).second->Integral(); // toterr2+=er*er; // } // tot+=(*it).second->Integral(); // sta2->Add(it->second); // } // std::cout << " Tot: " << tot << "+-" << sqrt(toterr2) << " SF: " << hd->Integral()/tot << std::endl; // TCanvas *c2 = new TCanvas(); // c2->SetTitle(names[i].c_str()); // std::cout << "hd maximum = " << hd->GetMaximum() << " sta2 maximum = " << sta2->GetMaximum() << std::endl; // if(hd->GetMaximum() > sta2->GetMaximum()) maxY = hd->GetBinContent(hd->GetMaximumBin()) * 1.5; // else maxY = ( sta2->GetMaximum())*1.5; // // hd->Draw("E1"); // sta2->Draw("PADSHIST"); // // hd->Draw("E1same"); // // l->Draw("same"); // std::cout << "Set Maximum to = " << maxY << std::endl; // hd->GetYaxis()->SetRangeUser(0.,maxY); // hd->GetXaxis()->SetRangeUser(options[names[i]].min,options[names[i]].max); // c2->Update(); // std::string c2Name = hd->GetName(); // c2Name = path+c2Name; // c2Name += "_norm.pdf"; // c2->Print(c2Name.c_str(),"pdf"); } }
void GetOptimization() { struct path thisPath; std::vector<path> paths; std::vector<TString> pathNames; struct filter thisFilter; std::vector<filter> filters; std::vector<std::pair<std::vector<TString>,Double_t> > xSections; std::vector<TString> filenamesBkg; std::vector<TString> filenamesSig; /* Parameters */ Int_t nCuts = 120; Bool_t doBandE = true; // if true, do seperate for cuts in barrel and endcap Double_t luminosity = 2.0E33; // in cm^-2 s^-1 // Cross-sections in mb filenamesBkg.push_back("../test/QCD-HLTVars-1.root"); // filenamesBkg.push_back("sameXSection"); xSections.push_back(make_pair(filenamesBkg, 2.16E-2)); filenamesBkg.clear(); // filenamesBkg.push_back("newXSection.root"); // ... // xSections.push_back(make_pair(filenamesBkg, xSection)); // filenamesBkg.clear(); filenamesSig.push_back("../test/ZEE-HLTVars.root"); /* ********** */ // Filters thisFilter.name = "l1Match"; thisFilter.pathNum = 0; thisFilter.direction = 0; thisFilter.hltBarrelCut = 0.; thisFilter.hltEndcapCut = 0.; thisFilter.maxCut = 0.; filters.push_back(thisFilter); thisFilter.name = "Et"; thisFilter.pathNum = 0; thisFilter.direction = 1; thisFilter.hltBarrelCut = 15.; thisFilter.hltEndcapCut = 15.; thisFilter.maxCut = 60.; filters.push_back(thisFilter); thisFilter.name = "IHcal"; thisFilter.pathNum = 0; thisFilter.direction = -1; thisFilter.hltBarrelCut = 3.; thisFilter.hltEndcapCut = 3.; thisFilter.maxCut = 6.; filters.push_back(thisFilter); thisFilter.name = "pixMatch"; thisFilter.pathNum = 0; thisFilter.direction = 1; thisFilter.hltBarrelCut = 0; thisFilter.hltEndcapCut = 0; thisFilter.maxCut = 0; filters.push_back(thisFilter); thisFilter.name = "Eoverp"; thisFilter.pathNum = 1; thisFilter.direction = -1; thisFilter.hltBarrelCut = 1.5; thisFilter.hltEndcapCut = 2.45; thisFilter.maxCut = 5.; filters.push_back(thisFilter); thisFilter.name = "Itrack"; thisFilter.pathNum = 1; thisFilter.direction = -1; thisFilter.hltBarrelCut = 0.06; thisFilter.hltEndcapCut = 0.06; thisFilter.maxCut = 0.24; filters.push_back(thisFilter); pathNames.push_back("SingleElecsPT."); pathNames.push_back("SingleElecs."); thisPath.names = pathNames; thisPath.nCandsCut = 1; thisPath.filters = filters; paths.push_back(thisPath); pathNames.clear(); pathNames.push_back("RelaxedSingleElecsPT."); pathNames.push_back("RelaxedSingleElecs."); thisPath.names = pathNames; thisPath.nCandsCut = 1; thisPath.filters = filters; paths.push_back(thisPath); pathNames.clear(); filters.clear(); thisFilter.name = "l1Match"; thisFilter.pathNum = 0; thisFilter.direction = 0; thisFilter.hltBarrelCut = 0.; thisFilter.hltEndcapCut = 0.; thisFilter.maxCut = 0.; filters.push_back(thisFilter); thisFilter.name = "Et"; thisFilter.pathNum = 0; thisFilter.direction = 1; thisFilter.hltBarrelCut = 10.; thisFilter.hltEndcapCut = 10.; thisFilter.maxCut = 40.; filters.push_back(thisFilter); thisFilter.name = "IHcal"; thisFilter.pathNum = 0; thisFilter.direction = -1; thisFilter.hltBarrelCut = 9.; thisFilter.hltEndcapCut = 9.; thisFilter.maxCut = 18.; filters.push_back(thisFilter); thisFilter.name = "pixMatch"; thisFilter.pathNum = 0; thisFilter.direction = 1; thisFilter.hltBarrelCut = 0; thisFilter.hltEndcapCut = 0; thisFilter.maxCut = 0; filters.push_back(thisFilter); thisFilter.name = "Eoverp"; thisFilter.pathNum = 1; thisFilter.direction = -1; thisFilter.hltBarrelCut = 15000; thisFilter.hltEndcapCut = 24500; thisFilter.maxCut = 5.; filters.push_back(thisFilter); thisFilter.name = "Itrack"; thisFilter.pathNum = 1; thisFilter.direction = -1; thisFilter.hltBarrelCut = 0.4; thisFilter.hltEndcapCut = 0.4; thisFilter.maxCut = 0.12; filters.push_back(thisFilter); pathNames.push_back("DoubleElecsPT."); pathNames.push_back("DoubleElecs."); thisPath.names = pathNames; thisPath.nCandsCut = 2; thisPath.filters = filters; paths.push_back(thisPath); pathNames.clear(); pathNames.push_back("RelaxedDoubleElecsPT."); pathNames.push_back("RelaxedDoubleElecs."); thisPath.names = pathNames; thisPath.nCandsCut = 2; thisPath.filters = filters; paths.push_back(thisPath); pathNames.clear(); filters.clear(); thisFilter.name = "l1Match"; thisFilter.pathNum = 0; thisFilter.direction = 0; thisFilter.hltBarrelCut = 0.; thisFilter.hltEndcapCut = 0.; thisFilter.maxCut = 0.; filters.push_back(thisFilter); thisFilter.name = "Et"; thisFilter.pathNum = 0; thisFilter.direction = 1; thisFilter.hltBarrelCut = 30.; thisFilter.hltEndcapCut = 30.; thisFilter.maxCut = 60.; filters.push_back(thisFilter); thisFilter.name = "IEcal"; thisFilter.pathNum = 0; thisFilter.direction = -1; thisFilter.hltBarrelCut = 1.5; thisFilter.hltEndcapCut = 1.5; thisFilter.maxCut = 6.; filters.push_back(thisFilter); thisFilter.name = "IHcal"; thisFilter.pathNum = 0; thisFilter.direction = -1; thisFilter.hltBarrelCut = 4.; thisFilter.hltEndcapCut = 6.; thisFilter.maxCut = 12.; filters.push_back(thisFilter); thisFilter.name = "Itrack"; thisFilter.pathNum = 0; thisFilter.direction = -1; thisFilter.hltBarrelCut = 1; thisFilter.hltEndcapCut = 1; thisFilter.maxCut = 5; filters.push_back(thisFilter); pathNames.push_back("SinglePhots."); thisPath.names = pathNames; thisPath.nCandsCut = 1; thisPath.filters = filters; paths.push_back(thisPath); pathNames.clear(); pathNames.push_back("RelaxedSinglePhots."); thisPath.names = pathNames; thisPath.nCandsCut = 1; thisPath.filters = filters; paths.push_back(thisPath); pathNames.clear(); filters.clear(); thisFilter.name = "l1Match"; thisFilter.pathNum = 0; thisFilter.direction = 0; thisFilter.hltBarrelCut = 0.; thisFilter.hltEndcapCut = 0.; thisFilter.maxCut = 0.; filters.push_back(thisFilter); thisFilter.name = "Et"; thisFilter.pathNum = 0; thisFilter.direction = 1; thisFilter.hltBarrelCut = 20.; thisFilter.hltEndcapCut = 20.; thisFilter.maxCut = 40.; filters.push_back(thisFilter); thisFilter.name = "IEcal"; thisFilter.pathNum = 0; thisFilter.direction = -1; thisFilter.hltBarrelCut = 2.5; thisFilter.hltEndcapCut = 2.5; thisFilter.maxCut = 5.; filters.push_back(thisFilter); thisFilter.name = "IHcal"; thisFilter.pathNum = 0; thisFilter.direction = -1; thisFilter.hltBarrelCut = 6.; thisFilter.hltEndcapCut = 8.; thisFilter.maxCut = 24.; filters.push_back(thisFilter); thisFilter.name = "Itrack"; thisFilter.pathNum = 0; thisFilter.direction = -1; thisFilter.hltBarrelCut = 3; thisFilter.hltEndcapCut = 3; thisFilter.maxCut = 6; filters.push_back(thisFilter); pathNames.push_back("DoublePhots."); thisPath.names = pathNames; thisPath.nCandsCut = 2; thisPath.filters = filters; paths.push_back(thisPath); pathNames.clear(); pathNames.push_back("RelaxedDoublePhots."); thisPath.names = pathNames; thisPath.nCandsCut = 2; thisPath.filters = filters; paths.push_back(thisPath); pathNames.clear(); filters.clear(); /* *********** */ Int_t cutNum = 0, pathNum = 0, filterNum = 0, oldFilterNum = 0, fileNum = 0, xSecNum = 0; Double_t cut = 0.; Long64_t passSig = 0, totalSig = 0; Long64_t passBkg = 0, totalBkg = 0; Double_t effSig = 0., errSig = 0.; Double_t effBkg = 0., errBkg = 0., rateTotBkg = 0., errTotBkg = 0.; Double_t conversion = 1.0E-27; std::vector<std::pair<Double_t,Double_t> > sigPass; std::vector<std::pair<Double_t,Double_t> > bkgPass; TString cutText; TString cutTextEcap; TString baseCutText; TString baseCutTextEcap; TString cutBasePT1; TString cutBasePT2; TString cutBase1; TString cutBase2; // cutBasePT1 = "ElecHLTCutVarsPreTracks_hltCutVars_"; // cutBasePT2 = "ElecsPT_EGAMMAHLT.obj."; // cutBase1 = ""; // cutBase2 = "Elecs_EGAMMAHLT.obj."; std::vector<std::vector<TGraphErrors> > EffVBkg; std::vector<std::vector<TGraphErrors> > EffVBkgEcap; std::vector<TGraphErrors> pathEffVBkgs; TGraphErrors filterEffVBkgs(nCuts); for (pathNum = 0; pathNum < paths.size(); pathNum++) { for (filterNum = 0; filterNum < (paths[pathNum].filters).size(); filterNum++) { // filterEffVBkgs = new TGraphErrors(nCuts); pathEffVBkgs.push_back(filterEffVBkgs); } EffVBkg.push_back(pathEffVBkgs); if (doBandE) { EffVBkgEcap.push_back(pathEffVBkgs); } } std::vector<std::pair<TChain*,Double_t> > bkgEvents; for (xSecNum = 0; xSecNum < xSections.size(); xSecNum++) { TChain *bkgProc = new TChain("Events"); for (fileNum = 0; fileNum < (xSections[xSecNum].first).size(); fileNum++) { bkgProc->Add((xSections[xSecNum].first)[fileNum]); } bkgEvents.push_back(make_pair(bkgProc,xSections[xSecNum].second)); } TChain *sigEvents = new TChain("Events"); for (fileNum = 0; fileNum < filenamesSig.size(); fileNum++) { sigEvents->Add(filenamesSig[fileNum]); } Double_t testX, testY, testdX, testdY; Double_t thisBCut = 0., thisECut = 0.; TString pathName, filterName; for (cutNum = 0; cutNum < nCuts; cutNum++) { cout<<"Cut "<<cutNum<<endl; for (pathNum = 0; pathNum < paths.size(); pathNum++) { for (filterNum = 0; filterNum < (paths[pathNum].filters).size(); filterNum++) { if ((paths[pathNum].filters)[filterNum].maxCut != 0.) { cutText = "(Sum$("; for (oldFilterNum = 0; oldFilterNum < filterNum; oldFilterNum++) { pathName = (paths[pathNum].names)[(paths[pathNum].filters)[filterNum].pathNum]; filterName = (paths[pathNum].filters)[oldFilterNum].name; thisBCut = (paths[pathNum].filters)[oldFilterNum].hltBarrelCut; thisECut = (paths[pathNum].filters)[oldFilterNum].hltEndcapCut; if (thisBCut == thisECut) { cutText += pathName; cutText += filterName; switch ((paths[pathNum].filters)[oldFilterNum].direction) { case -1: cutText += " < "; cutText += (paths[pathNum].filters)[oldFilterNum].hltBarrelCut; break; case 0: break; case 1: cutText += " > "; cutText += (paths[pathNum].filters)[oldFilterNum].hltBarrelCut; break; default: cout<<"Invalid value of direction in "<<pathName<<filterName<<endl; break; } } else { cutText += "(("; cutText += pathName; cutText += filterName; switch ((paths[pathNum].filters)[oldFilterNum].direction) { case -1: cutText += " < "; cutText += thisBCut; break; case 0: break; case 1: cutText += " > "; cutText += thisBCut; break; default: cout<<"Invalid value of direction in "<<pathName<<filterName<<endl; break; } cutText += " && abs("; cutText += pathName; cutText += "eta) < 1.5) || ("; cutText += pathName; cutText += filterName; switch ((paths[pathNum].filters)[oldFilterNum].direction) { case -1: cutText += " < "; cutText += thisECut; break; case 0: break; case 1: cutText += " > "; cutText += thisECut; break; default: cout<<"Invalid value of direction in "<<pathName<<filterName<<endl; break; } cutText += " && abs("; cutText += pathName; cutText += "eta) > 1.5 && abs("; cutText += pathName; cutText += "eta) < 2.5))"; } if (oldFilterNum != filterNum - 1) cutText += " && "; } baseCutText = cutText; pathName = paths[pathNum].names[(paths[pathNum].filters)[filterNum].pathNum]; filterName = (paths[pathNum].filters)[filterNum].name; cutText += " && "; cutText += pathName; cutText += filterName; cut = (Double_t)cutNum / (Double_t)nCuts * (paths[pathNum].filters)[oldFilterNum].maxCut; switch ((paths[pathNum].filters)[filterNum].direction) { case -1: cutText += " < "; cutText += cut; break; case 0: break; case 1: cutText += " > "; cutText += cut; break; default: cout<<"Invalid value of direction in "<<pathName<<filterName<<endl; break; } if (doBandE) { cutTextEcap = cutText; cutText += " && abs("; cutText += pathName; cutText += "eta) < 1.5"; cutTextEcap += " && abs("; cutTextEcap += pathName; cutTextEcap += "eta) > 1.5 && abs("; cutTextEcap += pathName; cutTextEcap += ") < 2.5"; baseCutText += " && abs("; baseCutText += pathName; baseCutTextEcap = baseCutText; baseCutText += "eta) < 1.5"; baseCutTextEcap += "eta) > 1.5 && abs("; baseCutTextEcap += pathName; baseCutTextEcap += "eta) < 2.5"; } cutText += ") >= "; cutText += paths[pathNum].nCandsCut; cutText += ")"; cutTextEcap += ") >= "; cutTextEcap += paths[pathNum].nCandsCut; cutTextEcap += ")"; baseCutText += ") >= "; baseCutText += paths[pathNum].nCandsCut; baseCutText += ")"; baseCutTextEcap += ") >= "; baseCutTextEcap += paths[pathNum].nCandsCut; baseCutTextEcap += ")"; cout<<cutText<<endl; cout<<cutTextEcap<<endl; // cout<<cutText<<endl; // cout<<baseCutText<<endl; passSig = sigEvents->Draw("",cutText); totalSig = sigEvents->Draw("",baseCutText); if (totalSig != 0) { effSig = (Double_t)passSig / (Double_t)totalSig; errSig = sqrt(effSig * (1. - effSig) / (Double_t)totalSig); } else { effSig = 0.; errSig = 0.; } rateTotBkg = 0.; errTotBkg = 0.; for (xSecNum = 0; xSecNum < bkgEvents.size(); xSecNum++) { passBkg = bkgEvents[xSecNum].first->Draw("",cutText); totalBkg = bkgEvents[xSecNum].first->Draw("",""); if (totalBkg != 0) { effBkg = (Double_t)passBkg / (Double_t)totalBkg; errBkg = sqrt(effBkg * (1. - effBkg) / (Double_t)totalBkg); } else { effBkg = 0.; errBkg = 0.; } rateTotBkg += effBkg * bkgEvents[xSecNum].second * luminosity * conversion; errTotBkg = sqrt(errTotBkg * errTotBkg + errBkg * errBkg * bkgEvents[xSecNum].second * luminosity * conversion * bkgEvents[xSecNum].second * luminosity * conversion); } if (cutNum == 6) { cout<<cutText<<endl; cout<<rateTotBkg<<" +- "<<errTotBkg<<", "<<effSig<<" +- "<<errSig<<endl;; } EffVBkg[pathNum][filterNum].SetPoint(cutNum, rateTotBkg, effSig); EffVBkg[pathNum][filterNum].SetPointError(cutNum, errTotBkg, errSig); if (cutNum == 6) { EffVBkg[pathNum][filterNum].GetPoint(cutNum, testX, testY); cout<<testX<<", "<<testY<<endl; } if (doBandE) { passSig = sigEvents->Draw("",cutTextEcap); totalSig = sigEvents->Draw("",baseCutText); if (totalSig != 0) { effSig = (Double_t)passSig / (Double_t)totalSig; errSig = sqrt(effSig * (1. - effSig) / (Double_t)totalSig); } else { effSig = 0.; errSig = 0.; } rateTotBkg = 0.; errTotBkg = 0.; for (xSecNum = 0; xSecNum < bkgEvents.size(); xSecNum++) { passBkg = bkgEvents[xSecNum].first->Draw("",cutText); totalBkg = bkgEvents[xSecNum].first->Draw("",""); if (totalBkg != 0) { effBkg = (Double_t)passBkg / (Double_t)totalBkg; errBkg = sqrt(effBkg * (1. - effBkg) / (Double_t)totalBkg); } else { effBkg = 0.; errBkg = 0.; } rateTotBkg += effBkg * bkgEvents[xSecNum].second * luminosity * conversion; errTotBkg = sqrt(errTotBkg * errTotBkg + errBkg * errBkg * bkgEvents[xSecNum].second * luminosity * conversion * bkgEvents[xSecNum].second * luminosity * conversion); } if (cutNum == 6) { cout<<cutText<<endl; cout<<rateTotBkg<<" +- "<<errTotBkg<<", "<<effSig<<" +- "<<errSig<<endl;; } EffVBkgEcap[pathNum][filterNum].SetPoint(cutNum, rateTotBkg, effSig); EffVBkgEcap[pathNum][filterNum].SetPointError(cutNum, errTotBkg, errSig); if (cutNum == 6) { EffVBkg[pathNum][filterNum].GetPoint(cutNum, testX, testY); cout<<testX<<", "<<testY<<endl; } } } } } } TCanvas *myCanvas; TString tempPathName, canvasTitle, graphTitle, outFilename; Int_t n; Int_t nGraphs, curGraph; for (pathNum = 0; pathNum < paths.size(); pathNum++) { canvasTitle = "Efficiency vs. Background for "; tempPathName = paths[pathNum].names[paths[pathNum].filters[(paths[pathNum].filters).size()-1].pathNum]; tempPathName.Resize(tempPathName.Index(".", 1, 0, TString::kExact)); outFilename = "./images/"; outFilename += tempPathName; outFilename += "EffVBkg.gif"; n = 0; while (tempPathName.Contains(TRegexp("[a-z][A-Z]")) && n < 10) { tempPathName.Insert(tempPathName.Index(TRegexp("[a-z][A-Z]"))+1, " "); n++; } canvasTitle += tempPathName; nGraphs = 0; for (filterNum = 0; filterNum < (paths[pathNum].filters).size(); filterNum++) { if ((paths[pathNum].filters)[filterNum].maxCut != 0) nGraphs++; } myCanvas = new TCanvas("myCanvas", canvasTitle, 0, 0, 1000, 500*(nGraphs / 2 + 1)); myCanvas->Divide(2,nGraphs / 2 + 1); curGraph = 0; for (filterNum = 0; filterNum < (paths[pathNum].filters).size(); filterNum++) { if ((paths[pathNum].filters)[filterNum].maxCut != 0.) { myCanvas->cd(curGraph+1); curGraph++; graphTitle = "Efficiency vs. Background for "; graphTitle += (paths[pathNum].filters)[filterNum].name; graphTitle += " Filter;Background Rate (Hz);Signal Eff."; EffVBkg[pathNum][filterNum].SetTitle(graphTitle); EffVBkg[pathNum][filterNum].Draw("AP"); } } myCanvas->Print(outFilename); if (doBandE) { canvasTitle = "Efficiency vs. Background for "; tempPathName = paths[pathNum].names[paths[pathNum].filters[(paths[pathNum].filters).size()-1].pathNum]; tempPathName.Resize(tempPathName.Index(".", 1, 0, TString::kExact)); tempPathName += "Endcap"; outFilename = "./images/"; outFilename += tempPathName; outFilename += "EffVBkg.gif"; n = 0; while (tempPathName.Contains(TRegexp("[a-z][A-Z]")) && n < 10) { tempPathName.Insert(tempPathName.Index(TRegexp("[a-z][A-Z]"))+1, " "); n++; } canvasTitle += tempPathName; nGraphs = 0; for (filterNum = 0; filterNum < (paths[pathNum].filters).size(); filterNum++) { if ((paths[pathNum].filters)[filterNum].maxCut != 0) nGraphs++; } myCanvas = new TCanvas("myCanvas", canvasTitle, 0, 0, 1000, 500*(nGraphs / 2 + 1)); myCanvas->Divide(2,nGraphs / 2 + 1); curGraph = 0; for (filterNum = 0; filterNum < (paths[pathNum].filters).size(); filterNum++) { if ((paths[pathNum].filters)[filterNum].maxCut != 0.) { myCanvas->cd(curGraph+1); curGraph++; graphTitle = "Efficiency vs. Background for "; graphTitle += (paths[pathNum].filters)[filterNum].name; graphTitle += " Filter in Endcap;Background Rate (Hz);Signal Eff."; EffVBkgEcap[pathNum][filterNum].SetTitle(graphTitle); EffVBkgEcap[pathNum][filterNum].Draw("AP"); } } myCanvas->Print(outFilename); } } TH1F *timingSig = new TH1F("timingSig", "Timing of Single Electron Filters in Signal Events", 6, 0, 6); timingSig->SetCanExtend(TH1::kAllAxes); timingSig->SetStats(0); TTreeFormula *l1MatchTiming = new TTreeFormula("Timing","HLTTiming_hltCutVars_IsoTiming_EGAMMAHLT.obj.l1Match",sigEvents); TTreeFormula *EtTiming = new TTreeFormula("Timing","HLTTiming_hltCutVars_IsoTiming_EGAMMAHLT.obj.Et",sigEvents); TTreeFormula *IHcalTiming = new TTreeFormula("Timing","HLTTiming_hltCutVars_IsoTiming_EGAMMAHLT.obj.ElecIHcal",sigEvents); TTreeFormula *pixMatchTiming = new TTreeFormula("Timing","HLTTiming_hltCutVars_IsoTiming_EGAMMAHLT.obj.pixMatch",sigEvents); TTreeFormula *EoverpTiming = new TTreeFormula("Timing","HLTTiming_hltCutVars_IsoTiming_EGAMMAHLT.obj.Eoverp",sigEvents); TTreeFormula *ItrackTiming = new TTreeFormula("Timing","HLTTiming_hltCutVars_IsoTiming_EGAMMAHLT.obj.ElecItrack",sigEvents); Long64_t event = 0; Double_t avgL1Match = 0.; Double_t avgEt = 0.; Double_t avgIHcal = 0.; Double_t avgPixMatch = 0.; Double_t avgEoverp = 0.; Double_t avgItrack = 0.; for (event = 0; event < sigEvents->GetEntries(); event++) { sigEvents->LoadTree(event); avgL1Match = (event*avgL1Match + l1MatchTiming->EvalInstance(0))/ ((Double_t) (event+1)); avgEt = (event*avgEt + EtTiming->EvalInstance(0))/ ((Double_t) (event+1)); avgIHcal = (event*avgIHcal + IHcalTiming->EvalInstance(0))/ ((Double_t) (event+1)); avgPixMatch = (event*avgPixMatch + pixMatchTiming->EvalInstance(0))/ ((Double_t) (event+1)); avgEoverp = (event*avgEoverp + EoverpTiming->EvalInstance(0))/ ((Double_t) (event+1)); avgItrack = (event*avgItrack + ItrackTiming->EvalInstance(0))/ ((Double_t) (event+1)); } timingSig->Fill("L1 Match", avgL1Match); timingSig->Fill("Et", avgEt); timingSig->Fill("IHcal", avgIHcal); timingSig->Fill("Pix Match", avgPixMatch); timingSig->Fill("E/p", avgEoverp); timingSig->Fill("Itrack", avgItrack); timingSig->LabelsDeflate("X"); timingSig->LabelsOption("v"); TH1F *timingBkg = new TH1F("timingBkg", "Timing of Single Electron Filters in Background Events", 6, 0, 6); timingBkg->SetCanExtend(TH1::kAllAxes); timingBkg->SetStats(0); avgL1Match = 0.; avgEt = 0.; avgIHcal = 0.; avgPixMatch = 0.; avgEoverp = 0.; avgItrack = 0.; for (xSecNum = 0; xSecNum < bkgEvents.size(); xSecNum++) { delete l1MatchTiming; l1MatchTiming = new TTreeFormula("Timing","HLTTiming_hltCutVars_IsoTiming_EGAMMAHLT.obj.l1Match",bkgEvents[xSecNum].first); delete EtTiming; EtTiming = new TTreeFormula("Timing","HLTTiming_hltCutVars_IsoTiming_EGAMMAHLT.obj.Et",bkgEvents[xSecNum].first); delete IHcalTiming; IHcalTiming = new TTreeFormula("Timing","HLTTiming_hltCutVars_IsoTiming_EGAMMAHLT.obj.ElecIHcal",bkgEvents[xSecNum].first); delete pixMatchTiming; pixMatchTiming = new TTreeFormula("Timing","HLTTiming_hltCutVars_IsoTiming_EGAMMAHLT.obj.pixMatch",bkgEvents[xSecNum].first); delete EoverpTiming; EoverpTiming = new TTreeFormula("Timing","HLTTiming_hltCutVars_IsoTiming_EGAMMAHLT.obj.Eoverp",bkgEvents[xSecNum].first); delete ItrackTiming; ItrackTiming = new TTreeFormula("Timing","HLTTiming_hltCutVars_IsoTiming_EGAMMAHLT.obj.ElecItrack",bkgEvents[xSecNum].first); event = 0; for (event = 0; event < bkgEvents[xSecNum].first->GetEntries(); event++) { bkgEvents[xSecNum].first->LoadTree(event); avgL1Match = (event*avgL1Match + l1MatchTiming->EvalInstance(0))/ ((Double_t) (event+1)); avgEt = (event*avgEt + EtTiming->EvalInstance(0))/ ((Double_t) (event+1)); avgIHcal = (event*avgIHcal + IHcalTiming->EvalInstance(0))/ ((Double_t) (event+1)); avgPixMatch = (event*avgPixMatch + pixMatchTiming->EvalInstance(0))/ ((Double_t) (event+1)); avgEoverp = (event*avgEoverp + EoverpTiming->EvalInstance(0))/ ((Double_t) (event+1)); avgItrack = (event*avgItrack + ItrackTiming->EvalInstance(0))/ ((Double_t) (event+1)); } } timingBkg->Fill("L1 Match", avgL1Match); timingBkg->Fill("Et", avgEt); timingBkg->Fill("IHcal", avgIHcal); timingBkg->Fill("Pix Match", avgPixMatch); timingBkg->Fill("E/p", avgEoverp); timingBkg->Fill("Itrack", avgItrack); timingBkg->LabelsDeflate("X"); timingBkg->LabelsOption("v"); myCanvas = new TCanvas("myCanvas", "Timing vs. Filter for Isolated Electron Filters", 1000, 500); myCanvas->Divide(2,1); myCanvas->cd(1); timingSig->Draw(); myCanvas->cd(2); timingBkg->Draw(); myCanvas->Print("images/TimingIso.gif"); delete myCanvas; delete timingSig; delete timingBkg; timingSig = new TH1F("timingSig", "Timing of Single Photon Filters in Signal Events", 6, 0, 6); timingSig->SetCanExtend(TH1::kAllAxes); timingSig->SetStats(0); delete l1MatchTiming; l1MatchTiming = new TTreeFormula("Timing","HLTTiming_hltCutVars_IsoTiming_EGAMMAHLT.obj.l1Match",sigEvents); delete EtTiming; EtTiming = new TTreeFormula("Timing","HLTTiming_hltCutVars_IsoTiming_EGAMMAHLT.obj.Et",sigEvents); TTreeFormula *IEcalTiming = new TTreeFormula("Timing","HLTTiming_hltCutVars_IsoTiming_EGAMMAHLT.obj.IEcal",sigEvents); TTreeFormula *PhotIHcalTiming = new TTreeFormula("Timing","HLTTiming_hltCutVars_IsoTiming_EGAMMAHLT.obj.PhotIHcal",sigEvents); TTreeFormula *PhotItrackTiming = new TTreeFormula("Timing","HLTTiming_hltCutVars_IsoTiming_EGAMMAHLT.obj.PhotItrack",sigEvents); event = 0; avgL1Match = 0.; avgEt = 0.; Double_t avgIEcal = 0.; Double_t avgPhotIHcal = 0.; Double_t avgPhotItrack = 0.; for (event = 0; event < sigEvents->GetEntries(); event++) { sigEvents->LoadTree(event); avgL1Match = (event*avgL1Match + l1MatchTiming->EvalInstance(0))/ ((Double_t) (event+1)); avgEt = (event*avgEt + EtTiming->EvalInstance(0))/ ((Double_t) (event+1)); avgIEcal = (event*avgIEcal + IEcalTiming->EvalInstance(0))/ ((Double_t) (event+1)); avgPhotIHcal = (event*avgPhotIHcal + PhotIHcalTiming->EvalInstance(0))/ ((Double_t) (event+1)); avgPhotItrack = (event*avgPhotItrack + PhotItrackTiming->EvalInstance(0))/ ((Double_t) (event+1)); } timingSig->Fill("L1 Match", avgL1Match); timingSig->Fill("Et", avgEt); timingSig->Fill("IEcal", avgIEcal); timingSig->Fill("IHcal", avgPhotIHcal); timingSig->Fill("Itrack", avgPhotItrack); timingSig->LabelsDeflate("X"); timingSig->LabelsOption("v"); timingBkg = new TH1F("timingBkg", "Timing of Single Photon Filters in Background Events", 6, 0, 6); timingBkg->SetCanExtend(TH1::kAllAxes); timingBkg->SetStats(0); avgL1Match = 0.; avgEt = 0.; avgIEcal = 0.; avgPhotIHcal = 0.; avgPhotItrack = 0.; for (xSecNum = 0; xSecNum < bkgEvents.size(); xSecNum++) { delete l1MatchTiming; l1MatchTiming = new TTreeFormula("Timing","HLTTiming_hltCutVars_IsoTiming_EGAMMAHLT.obj.l1Match",bkgEvents[xSecNum].first); delete EtTiming; EtTiming = new TTreeFormula("Timing","HLTTiming_hltCutVars_IsoTiming_EGAMMAHLT.obj.Et",bkgEvents[xSecNum].first); delete IEcalTiming; IEcalTiming = new TTreeFormula("Timing","HLTTiming_hltCutVars_IsoTiming_EGAMMAHLT.obj.IEcal",bkgEvents[xSecNum].first); delete PhotIHcalTiming; PhotIHcalTiming = new TTreeFormula("Timing","HLTTiming_hltCutVars_IsoTiming_EGAMMAHLT.obj.PhotIHcal",bkgEvents[xSecNum].first); delete PhotItrackTiming; PhotItrackTiming = new TTreeFormula("Timing","HLTTiming_hltCutVars_IsoTiming_EGAMMAHLT.obj.PhotItrack",bkgEvents[xSecNum].first); event = 0; for (event = 0; event < bkgEvents[xSecNum].first->GetEntries(); event++) { bkgEvents[xSecNum].first->LoadTree(event); avgL1Match = (event*avgL1Match + l1MatchTiming->EvalInstance(0))/ ((Double_t) (event+1)); avgEt = (event*avgEt + EtTiming->EvalInstance(0))/ ((Double_t) (event+1)); avgIEcal = (event*avgIEcal + IEcalTiming->EvalInstance(0))/ ((Double_t) (event+1)); avgPhotIHcal = (event*avgPhotIHcal + PhotIHcalTiming->EvalInstance(0))/ ((Double_t) (event+1)); avgPhotItrack = (event*avgPhotItrack + PhotItrackTiming->EvalInstance(0))/ ((Double_t) (event+1)); } } timingBkg->Fill("L1 Match", avgL1Match); timingBkg->Fill("Et", avgEt); timingBkg->Fill("IEcal", avgIEcal); timingBkg->Fill("IHcal", avgPhotIHcal); timingBkg->Fill("Itrack", avgPhotItrack); timingBkg->LabelsDeflate("X"); timingBkg->LabelsOption("v"); myCanvas = new TCanvas("myCanvas", "Timing vs. Filter for Isolated Photon Filters", 1000, 500); myCanvas->Divide(2,1); myCanvas->cd(1); timingSig->Draw(); myCanvas->cd(2); timingBkg->Draw(); myCanvas->Print("images/TimingIsoPhot.gif"); }
void plottingmacro() { setTDRStyle(); gROOT->ForceStyle(); initOptions(); std::vector<Sample> s = samples(); Sample data(1,"fake data","S1.root",0,true,1000); for(size_t i=0;i< s.size();i++) if(s[i].data) {data=s[i];break;} data.file()->ls(); for(size_t i=0;i< s.size();i++) s[i].dump(data.lumi()); std::vector<std::string> names; TList * subs = data.file()->GetListOfKeys(); for(size_t i=0;i< subs->GetSize();i++) { TList * objs = ((TDirectoryFile *) data.file()->Get(subs->At(i)->GetName()))->GetListOfKeys(); for(size_t j=0;j< objs->GetSize();j++) { names.push_back(subs->At(i)->GetName()+std::string("/") + objs->At(j)->GetName()); // std::cout << subs->At(i)->GetName() << "/" << objs->At(j)->GetName() << std::endl; //TODO: select plots via regexp } } for(size_t i = 0 ; i < names.size() ; i++) { std::map<std::string,TH1F *> grouped; TString n=names[i]; if(!n.Contains(TRegexp("V.*RegionH.*mu.*HiggsMass"))) continue; TCanvas *c = new TCanvas(); c->SetLogy(true); c->SetTitle(names[i].c_str()); TH1F *hd = ((TH1F*)data.file()->Get(names[i].c_str())); Options o=options[names[i]]; hd->Rebin(o.rebin); hd->SetMarkerStyle(21); hd->Draw("E1"); hd->SetYTitle(o.yaxis.c_str()); THStack * sta = new THStack("sta",hd->GetTitle()); TLegend * l = new TLegend(o.legendx1,o.legendy1,o.legendx2,o.legendy2); //0.7,0.1,0.9,0.6); l->AddEntry(hd, "Data","LP"); for(size_t j=0;j< s.size() ;j++) { if(!s[j].data) { TH1F * h = ((TH1F*)s[j].file()->Get(names[i].c_str())); h->Scale(s[j].scale(data.lumi())); h->SetLineColor(s[j].color); h->SetFillColor(s[j].color); h->Rebin(options[names[i]].rebin); if(grouped.find(s[j].name)==grouped.end()) { grouped[s[j].name]=(TH1F *)h->Clone(("_"+names[i]).c_str()); l->AddEntry(h,s[j].name.c_str(),"F"); } else { grouped[s[j].name]->Add(h); } sta->Add(h); // h->Draw("same"); } } sta->Draw("same"); hd->Draw("E1same"); hd->GetYaxis()->SetRangeUser(options[names[i]].min,options[names[i]].max); l->Draw(); std::cout << names[i] << " d: " << hd->Integral() << " "; THStack * sta2 = new THStack("sta2",hd->GetTitle()); float tot=0; float toterr2=0; for(std::map<std::string,TH1F *>::iterator it = grouped.begin(); it != grouped.end();it++) { std::cout << it->first << " " << it->second->Integral() << " | " ; if(it->second->GetEntries() > 0) { float er=1.*sqrt(it->second->GetEntries())/it->second->GetEntries()*it->second->Integral(); toterr2+=er*er; } tot+=it->second->Integral(); sta2->Add(it->second); } std::cout << " Tot: " << tot << "+-" << sqrt(toterr2) << " SF: " << hd->Integral()/tot << std::endl; c = new TCanvas(); sta2->Draw(); hd->Draw("E1,same"); sta2->GetYaxis()->SetRangeUser(options[names[i]].min,options[names[i]].max); hd->GetYaxis()->SetRangeUser(options[names[i]].min,options[names[i]].max); l->Draw(); } }
// Main function to create the pdf's int main(int argc, char**argv){ TString version = "80X"; // Define binning for pdfs (details and more options in binningConfigurations.h) binClass bins; if(version.Contains("v2")) bins = getV2Binning(); if(version.Contains("80X")) bins = get76XBinning(); else return 1; // For different jet types (if _antib is added bTag is applied) for(TString jetType : {"AK4chs","AK4chs_antib"}){ //,"AK4","AK4_antib"}){ std::cout << "Building pdf's for " << jetType << "..." << std::endl; treeLooper t("QCD_AllPtBins", jetType); // Init tree (third argument is the directory path, if other than default in treeLooper.h) bins.setReference("pt", &t.pt); // Give the binning class a pointer to the variables used to bin in bins.setReference("eta", &t.eta); bins.setReference("rho", &t.rho); // Creation of the pdfs std::map<TString, TH1D*> pdfs; for(TString binName : bins.getAllBinNames()){ for(TString type : {"quark","gluon"}){ TString histName = "_" + type + "_" + binName; pdfs["axis2" + histName] = new TH1D("axis2" + histName, "axis2" + histName, 100, 0, 8); // Has been 200 bins before, but seemed to have a bit too much fluctuations still pdfs["mult" + histName] = new TH1D("mult" + histName, "mult" + histName, 140, 2.5, 142.5); pdfs["ptD" + histName] = new TH1D("ptD" + histName, "ptD" + histName, 100, 0, 1); // Also 200 before } } // Fill pdfs TString binName; while(t.next()){ if(!bins.getBinName(binName)) continue; // Find bin and return false if outside ranges if(t.jetIdLevel < 3) continue; // Select tight jets if(!t.matchedJet) continue; // Only matched jets if(t.nGenJetsInCone != 1 || t.nJetsForGenParticle != 1 || t.nGenJetsForGenParticle != 1) continue; // Use only jets matched to exactly one gen jet and gen particle, and no other jet candidates if((fabs(t.partonId) > 3 && t.partonId != 21)) continue; // Keep only udsg if(t.bTag) continue; // Anti-b tagging (always false if jetType does not contain "antib") if(!t.balanced) continue; // Take only two leading jets with pt3 < 0.15*(pt1+pt2) (surpresses small radiated jets with pt <<< pthat) if(t.mult < 3) continue; // Avoid jets with less than 3 particles (otherwise axis2=0) TString type = (t.partonId == 21? "gluon" : "quark"); // Define q/g TString histName = "_" + type + "_" + binName; pdfs["axis2" + histName]->Fill(t.axis2, t.weight); // "axis2" already contains the log pdfs["mult" + histName]->Fill(t.mult, t.weight); pdfs["ptD" + histName]->Fill(t.ptD, t.weight); } // Try to add statistics from neighbours (first make copy, so you don't get an iterative effect) std::map<TString, TH1D*> pdfsCopy; for(auto& pdf : pdfs) pdfsCopy[pdf.first] = (TH1D*) pdf.second->Clone(pdf.first + "clone"); for(TString binName : bins.getAllBinNames()){ for(TString var : {"axis2","mult","ptD"}){ for(TString neighbour : bins.getNeighbourBins(binName, var)){ // If neighbours are defined: add their statistics for(TString type : {"quark","gluon"}){ pdfs[var + "_" + type + "_" + binName]->Add(pdfsCopy[var + "_" + type + "_" + neighbour]); } } } } for(auto& copy : pdfsCopy) delete copy.second; // Store the mean and RMS of the original histogram (because they could be changed by rebinning operations) std::map<TString, float> mean; std::map<TString, float> rms; for(auto& pdf : pdfs){ if(pdf.second->GetEntries() == 0){ std::cout << "Error: no entries in " << pdf.first << std::endl; exit(1);} // Force to exit when no entries in pdfs: the binning configuration should be altered to avoid this mean[pdf.first] = pdf.second->GetMean(); rms[pdf.first] = pdf.second->GetRMS(); } // Check "smoothness" of the pdf: if fluctuations seem really big, we do a rebinning std::map<TString, int> rebinFactor; for(auto& pdf : pdfs){ bool isBelow = (mean[pdf.first] < mean[switchQG(pdf.first)]); // Define region between low(meanQ, meanG) - RMS <--> high(meanQ, meanG) + RMS TString low = isBelow? pdf.first : switchQG(pdf.first); // Most events will be within those borders, so we should not allow empty bins here TString high = isBelow? switchQG(pdf.first) : pdf.first; // (an empty bin for 1 of the three variables already results in L = 0 or 1) int leftBin = pdf.second->FindBin(mean[low] - rms[low]) - 1; int rightBin = pdf.second->FindBin(mean[high] + rms[high]) + 1; int leftBin2 = 0; // Define region of the peak: most extreme bins which exceed 80% of the maximum int rightBin2 = 0; // We will check for bins within this region which go below 70% for(int bin = 1; bin < pdf.second->GetNbinsX(); ++bin){ // In such cases the fluctuations are really high and a larger bin width is preferred if(pdf.second->GetBinContent(bin) > pdf.second->GetMaximum()*0.8){ // (Maybe the thresholds could still be optimized a bit more though) if(!leftBin2) leftBin2 = bin; else rightBin2 = bin; } } int leftBin3 = 0; // Define region of the peak: most extreme bins which exceed 90% of the maximum int rightBin3 = 0; // We will check for bins within this region which go below 80% for(int bin = 1; bin < pdf.second->GetNbinsX(); ++bin){ // In such cases the fluctuations are really high and a larger bin width is preferred if(pdf.second->GetBinContent(bin) > pdf.second->GetMaximum()*0.9){ // (Maybe the thresholds could still be optimized a bit more though) if(!leftBin3) leftBin3 = bin; else rightBin3 = bin; } } int emptyBins = 0; int maxEmptyBins = 0; for(int bin = 1; bin < pdf.second->GetNbinsX(); ++bin){ if( bin >= leftBin && bin <= rightBin && pdf.second->GetBinContent(bin) <= 0) ++emptyBins; else if(bin >= leftBin2 && bin <= rightBin2 && pdf.second->GetBinContent(bin) <= pdf.second->GetMaximum()*0.7) ++emptyBins; else if(bin >= leftBin3 && bin <= rightBin3 && pdf.second->GetBinContent(bin) <= pdf.second->GetMaximum()*0.8) ++emptyBins; else { if(emptyBins > maxEmptyBins) maxEmptyBins = emptyBins; emptyBins = 0; } } rebinFactor[pdf.first] = std::max(maxEmptyBins, emptyBins); } for(auto& pdf : pdfs) rebinFactor[pdf.first] = std::max(rebinFactor[pdf.first], rebinFactor[switchQG(pdf.first)]); // Use same rebin factor in quark and gluon pdf (otherwise bias if second derivate of pdf is non-zero) for(auto& pdf : pdfs){ if(rebinFactor[pdf.first] > 19) std::cout << "This pdf has a lot of emtpy bins and fluctuations:" << std::endl; if(rebinFactor[pdf.first] > 9) rebin(pdf.second, 20); else if(rebinFactor[pdf.first] > 4) rebin(pdf.second, 10); else if(rebinFactor[pdf.first] > 3) rebin(pdf.second, 5); else if(rebinFactor[pdf.first] > 1) rebin(pdf.second, 4); else if(rebinFactor[pdf.first] > 0) rebin(pdf.second, 2); } // Normalization of the pdf's for(auto& pdf : pdfs) pdf.second->Scale(1./pdf.second->Integral(0, pdf.second->GetNbinsX() + 1)); // Scale to integral=1 (also include underflow/overflow) // Try to average out leftover fluctuations and empty bins for(auto& pdf : pdfs){ if(pdf.first.Contains("gluon")) continue; TH1* tempQ = (TH1*) pdf.second->Clone(); TH1* tempG = (TH1*) pdfs[switchQG(pdf.first)]->Clone(); for(int i = 1; i < pdf.second->GetNbinsX() + 1; ++i){ // Do not consider underflow/overflow float contentQ = tempQ->GetBinContent(i); float contentG = tempG->GetBinContent(i); float width = tempQ->GetBinWidth(i); if((1.5*contentQ < tempQ->GetBinContent(i-1) && 1.5*contentQ < tempQ->GetBinContent(i+1)) || // Try to average out some extreme fluctuations (i.e. only allow a difference of max 50% between two neighbouring bins) (1.5*contentG < tempG->GetBinContent(i-1) && 1.5*contentG < tempG->GetBinContent(i+1)) || (contentQ < 1.5*tempQ->GetBinContent(i-1) && contentQ < 1.5*tempQ->GetBinContent(i+1)) || (contentG < 1.5*tempG->GetBinContent(i-1) && contentG < 1.5*tempG->GetBinContent(i+1))){ if(i-1 > 0 && i+1 < pdf.second->GetNbinsX() + 1){ contentQ += tempQ->GetBinContent(i-1) + tempQ->GetBinContent(i+1); contentG += tempG->GetBinContent(i-1) + tempG->GetBinContent(i+1); width += tempQ->GetBinWidth(i-1) + tempQ->GetBinWidth(i+1); } } int j = 1; while(contentQ <= 0 || contentG <= 0){ // Average empty bins if(tempQ->Integral(0, i-j) <= 0) break; // but not when surpassing the extreme edges of the pdf (see next part) if(tempG->Integral(0, i-j) <= 0) break; if(tempQ->Integral(i+j, tempQ->GetNbinsX()+1) <= 0) break; if(tempG->Integral(i+j, tempG->GetNbinsX()+1) <= 0) break; if(i-j == 0) break; if(i+j == pdf.second->GetNbinsX()) break; contentQ += tempQ->GetBinContent(i-j) + tempQ->GetBinContent(i+j); contentG += tempG->GetBinContent(i-j) + tempG->GetBinContent(i+j); width += tempQ->GetBinWidth(i-j) + tempQ->GetBinWidth(i+j); ++j; } pdf.second->SetBinContent(i, contentQ/width); pdfs[switchQG(pdf.first)]->SetBinContent(i, contentG/width); } delete tempQ; delete tempG; } // Now there are still empty bins left on the edges of the pdf, for which we assign extreme values to avoid a 0 // (relative though, so it does not dominate the pdf's when we want to inspect them in the ROOT file; 0.000001/0.000999 has the same effect as 0.001/0.999) for(auto& pdf : pdfs){ if(pdf.first.Contains("gluon")) continue; for(int i = 0; i <= pdf.second->GetNbinsX() + 1; ++i){ if(pdf.second->GetBinContent(i) <= 0 || pdfs[switchQG(pdf.first)]->GetBinContent(i) <= 0){ bool isBelow = (mean[pdf.first] < mean[switchQG(pdf.first)]); if(isBelow == pdf.second->GetBinCenter(i) < mean[pdf.first]){ pdf.second->SetBinContent(i, 0.000999); pdfs[switchQG(pdf.first)]->SetBinContent(i, 0.000001); } else { pdf.second->SetBinContent(i, 0.000001); pdfs[switchQG(pdf.first)]->SetBinContent(i, 0.000999); } } } } // Apply the likelihood weight for(auto& pdf : pdfs){ TString thisBin = pdf.first(pdf.first.Index(TRegexp("eta")), pdf.first.Length()); TString thisVar = pdf.first(0, pdf.first.Index(TRegexp("_"))); for(int i = 0; i < pdf.second->GetNbinsX() + 1; ++i){ pdf.second->SetBinContent(i, std::pow(pdf.second->GetBinContent(i), bins.getWeight(thisBin, (thisVar == "mult"? 0 : (thisVar == "ptD" ? 1 : 2 ))))); } } // Make file and write binnings TFile *pdfFile = new TFile("pdfQG_"+jetType + "_13TeV_" + version + ".root","RECREATE"); pdfFile->cd(); bins.writeBinsToFile(); // Write to file for(TString var : {"axis2","ptD","mult"}) pdfFile->mkdir(var); for(auto& pdf : pdfs){ for(TString var: {"axis2","ptD","mult"}) if(pdf.first.Contains(var)) pdfFile->cd(var); pdf.second->SetTitle(pdf.first); pdf.second->Write(); TString thisBin = pdf.first(pdf.first.Index(TRegexp("eta")), pdf.first.Length()); for(auto i : bins.getLinkedBins(thisBin)){ // Store copies for merged bins TString copyBin = pdf.first; copyBin.ReplaceAll(thisBin, i); pdf.second->Write(copyBin); } } for(auto& pdf : pdfs) delete pdf.second; for(auto& file : {pdfFile}){ file->Close(); delete file;} } return 0; }