////////////////////////////////////////////////////////////////////// // This template analysis code has been built with fragments from the // classes automatically obtained by the TTree MakeClass() method. // // The template shows the structre of a potential analysis code // where more TTree friends can be added with more physics objects. // // The analysis part is based on the RDataFrame analysis example by Stefan Wunch: // https://github.com/cms-opendata-analyses/HiggsTauTauNanoAODOutreachAnalysis // // Done with ROOT version 5.32/00 // from TTree Events/Events // found on file: myoutput.root // // // Compile me with: // g++ -std=c++11 -g -O3 -Wall -Wextra -o EventLoopAnalysis EventLoopAnalysisTemplate.cxx $(root-config --cflags --libs) ///////////////////////////////////////////////////////////////////// //Include ROOT classes #include #include #include #include #include #include #include #include "TLatex.h" #include "TStopwatch.h" //Include C++ classes #include #include #include #include #include using namespace std; /* * Base path to local filesystem or to EOS containing the datasets */ //const std::string samplesBasePath = "root://eospublic.cern.ch//eos/opendata/cms/upload/od-workshop/ws2021/"; const std::string samplesBasePath = ""; //book example histograms for specific variables //copy them in the constructor if you add more const int nhists = 9; //Histograms for signal region TH1F* dataRunB_npv = new TH1F("dataRunB_npv","Number of primary vertices",25,5,30); TH1F* dataRunC_npv = new TH1F("dataRunC_npv","Number of primary vertices",25,5,30); TH1F* ZLL_npv = new TH1F("ZLL_npv","Number of primary vertices",25,5,30); TH1F* TT_npv = new TH1F("TT_npv","Number of primary vertices",25,5,30); TH1F* W3J_npv = new TH1F("W3J_npv","Number of primary vertices",25,5,30); TH1F* W2J_npv = new TH1F("W2J_npv","Number of primary vertices",25,5,30); TH1F* W1J_npv = new TH1F("W1J_npv","Number of primary vertices",25,5,30); TH1F* qqH_npv = new TH1F("qqH_npv","Number of primary vertices",25,5,30); TH1F* ggH_npv = new TH1F("ggH_npv","Number of primary vertices",25,5,30); //Requiered trigger string triggerRequest = "HLT_IsoMu17_eta2p1_LooseIsoPFTau20"; // Fixed size dimensions of array or collections stored in the TTree if any. class EventLoopAnalysisTemplate { public : TTree *fChain; //!pointer to the analyzed TTree or TChain TTree *tevents; TTree *tvertex; TTree *ttrigger; TTree *tmuons; TTree *ttaus; TTree *tmets; Int_t fCurrent; //!current Tree number in a TChain TString labeltag; TString filename; Float_t theweight; //array to keep histograms to be written and easily loop over them TH1F *hists[nhists]; // Declaration of example leaf types Int_t run; UInt_t luminosityBlock; ULong64_t event; Int_t PV_npvs; std::map *triggermap; vector *muon_pt; vector *muon_eta; vector *muon_phi; vector *muon_ch; vector *muon_tightid; vector *muon_pfreliso03all; vector *muon_mass; vector *tau_pt; vector *tau_eta; vector *tau_phi; vector *tau_ch; vector *tau_iddecaymode; vector *tau_idisotight; vector *tau_idantieletight; vector *tau_idantimutight; vector *tau_reliso_all; vector *tau_mass; Float_t met_pt; Float_t met_phi; // List of example branches TBranch *b_run; //! TBranch *b_luminosityBlock; //! TBranch *b_event; //! TBranch *b_PV_npvs; //! TBranch *b_triggermap; //! TBranch *b_muon_pt; //! TBranch *b_muon_eta; //! TBranch *b_muon_phi; //! TBranch *b_muon_ch; //! TBranch *b_muon_tightid; //! TBranch *b_muon_pfreliso03all; //! TBranch *b_muon_mass; //! TBranch *b_tau_pt; //! TBranch *b_tau_eta; //! TBranch *b_tau_phi; //! TBranch *b_tau_ch; //! TBranch *b_tau_iddecaymode; //! TBranch *b_tau_idisotight; //! TBranch *b_tau_idantieletight; //! TBranch *b_tau_idantimutight; //! TBranch *b_tau_reliso_all; //! TBranch *b_tau_mass; //! TBranch *b_met_pt; //! TBranch *b_met_phi; //! EventLoopAnalysisTemplate(TString filename, TString labeltag, Float_t theweight); virtual ~EventLoopAnalysisTemplate(); virtual Int_t GetEntry(Long64_t entry); virtual Long64_t LoadTree(Long64_t entry); virtual void Init(TTree *tree); virtual void Loop(); virtual Bool_t Notify(); virtual void Show(Long64_t entry = -1); void analysis(); bool MinimalSelection(); }; EventLoopAnalysisTemplate::EventLoopAnalysisTemplate(TString thefile, TString thelabel, Float_t sampleweight) : fChain(0) { //Prepare some info for the object: filename = thefile; labeltag = thelabel; theweight = sampleweight; //Load histograms for signal region hists[0] = dataRunB_npv; hists[1] = dataRunC_npv; hists[2] = ZLL_npv; hists[3] = TT_npv; hists[4] = W3J_npv; hists[5] = W2J_npv; hists[6] = W1J_npv; hists[7] = qqH_npv; hists[8] = ggH_npv; // if parameter tree is not specified (or zero), connect the file // used to generate this class and read the Tree. TTree* tree = 0; TFile *f = TFile::Open(filename); //trigger should go first as it is the more complicated one tree = (TTree*)f->Get("mytriggers/Events"); //Get trees for friendship tevents = (TTree*)f->Get("myevents/Events"); tvertex = (TTree*)f->Get("mypvertex/Events"); tmuons = (TTree*)f->Get("mymuons/Events"); ttaus = (TTree*)f->Get("mytaus/Events"); tmets = (TTree*)f->Get("mymets/Events"); //Make friends so we can have access to friends variables //we may not use all of the available information //it is just an example tree->AddFriend(tevents); tree->AddFriend(tvertex); tree->AddFriend(tmuons); tree->AddFriend(ttaus); tree->AddFriend(tmets); Init(tree); } EventLoopAnalysisTemplate::~EventLoopAnalysisTemplate() { if (!fChain) return; delete fChain->GetCurrentFile(); } Int_t EventLoopAnalysisTemplate::GetEntry(Long64_t entry) { // Read contents of entry. if (!fChain) return 0; return fChain->GetEntry(entry); } Long64_t EventLoopAnalysisTemplate::LoadTree(Long64_t entry) { //cout<<" Set the environment to read one entry"<LoadTree(entry); if (centry < 0) return centry; if (fChain->GetTreeNumber() != fCurrent) { fCurrent = fChain->GetTreeNumber(); Notify(); } return centry; } void EventLoopAnalysisTemplate::Init(TTree *tree) { // The Init() function is called when the selector needs to initialize // a new tree or chain. Typically here the branch addresses and branch // pointers of the tree will be set. // It is normally not necessary to make changes to the generated // code, but the routine can be extended by the user if needed. // Init() will be called many times when running on PROOF // (once per file to be processed). // Set object pointer triggermap =0; muon_pt = 0; muon_eta = 0; muon_phi = 0; muon_ch = 0; muon_tightid = 0; muon_pfreliso03all = 0; muon_mass = 0; tau_pt = 0; tau_eta = 0; tau_phi = 0; tau_ch = 0; tau_iddecaymode = 0; tau_idisotight = 0; tau_idantieletight = 0; tau_idantimutight = 0; tau_reliso_all = 0; tau_mass = 0; // Set branch addresses and branch pointers if (!tree) return; fChain = tree; fCurrent = -1; //Comment out to be able to read map //https://root-forum.cern.ch/t/std-map-in-ttree-with-makeclass/14171 //fChain->SetMakeClass(1); fChain->SetBranchAddress("run", &run, &b_run); fChain->SetBranchAddress("luminosityBlock", &luminosityBlock, &b_luminosityBlock); fChain->SetBranchAddress("event", &event, &b_event); fChain->SetBranchAddress("PV_npvs", &PV_npvs, &b_PV_npvs); fChain->SetBranchAddress("triggermap",&triggermap,&b_triggermap); fChain->SetBranchAddress("muon_pt", &muon_pt, &b_muon_pt); fChain->SetBranchAddress("muon_eta", &muon_eta, &b_muon_eta); fChain->SetBranchAddress("muon_phi", &muon_phi, &b_muon_phi); fChain->SetBranchAddress("muon_ch", &muon_ch, &b_muon_ch); fChain->SetBranchAddress("muon_tightid", &muon_tightid, &b_muon_tightid); fChain->SetBranchAddress("muon_pfreliso03all", &muon_pfreliso03all, &b_muon_pfreliso03all); fChain->SetBranchAddress("muon_mass", &muon_mass, &b_muon_mass); fChain->SetBranchAddress("tau_pt", &tau_pt, &b_tau_pt); fChain->SetBranchAddress("tau_eta", &tau_eta, &b_tau_eta); fChain->SetBranchAddress("tau_phi", &tau_phi, &b_tau_phi); fChain->SetBranchAddress("tau_ch", &tau_ch, &b_tau_ch); fChain->SetBranchAddress("tau_iddecaymode", &tau_iddecaymode, &b_tau_iddecaymode); fChain->SetBranchAddress("tau_idisotight", &tau_idisotight, &b_tau_idisotight); fChain->SetBranchAddress("tau_idantieletight", &tau_idantieletight, &b_tau_idantieletight); fChain->SetBranchAddress("tau_idantimutight", &tau_idantimutight, &b_tau_idantimutight); fChain->SetBranchAddress("tau_reliso_all", &tau_reliso_all, &b_tau_reliso_all); fChain->SetBranchAddress("tau_mass", &tau_mass, &b_tau_mass); fChain->SetBranchAddress("met_pt", &met_pt, &b_met_pt); fChain->SetBranchAddress("met_phi", &met_phi, &b_met_phi); Notify(); } Bool_t EventLoopAnalysisTemplate::Notify() { // The Notify() function is called when a new file is opened. This // can be either for a new TTree in a TChain or when when a new TTree // is started when using PROOF. It is normally not necessary to make changes // to the generated code, but the routine can be extended by the // user if needed. The return value is currently not used. return kTRUE; } void EventLoopAnalysisTemplate::Show(Long64_t entry) { // Print contents of entry. // If entry is not specified, print current entry if (!fChain) return; fChain->Show(entry); } void EventLoopAnalysisTemplate::Loop() { if (fChain == 0) return; Long64_t nentries = fChain->GetEntriesFast(); Long64_t nbytes = 0, nb = 0; for (Long64_t jentry=0; jentryGetEntry(jentry); nbytes += nb; analysis(); } } //----------------------------------------------------------------- void EventLoopAnalysisTemplate::analysis() { //----------------------------------------------------------------- //cout<<"analysis() execution"<GetName()); TString thelabel = histname(0,histname.First("_")); TString thevar = histname(histname.First("_")+1,histname.Sizeof()); if (thelabel == labeltag){ //primary vertices if(thevar == "npv"){ hists[j]->Fill(PV_npvs,theweight); } } } }//------analysis() /* * Perform a selection on the minimal requirements of an event */ //----------------------------------------------------------------- bool EventLoopAnalysisTemplate::MinimalSelection() { //----------------------------------------------------------------- //cout<<"Applying minimal selection"<::iterator it=triggermap->begin();it!=triggermap->end();it++){ if(it->first.find(triggerRequest)!=string::npos && it->second!=0){ //cout<first<<" "<second<ProcessLine("#include"); /* * Compute event weights to be used for the respective datasets * * The event weight reweights the full dataset so that the sum of the weights * is equal to the expected number of events in data. The expectation is given by * multiplying the integrated luminosity of the data with the cross-section of * the process in the datasets divided by the number of simulated events. */ //const float integratedLuminosity = 4.412 * 1000.0; // Run2012B only //const float integratedLuminosity = 7.055 * 1000.0; // Run2012C only const float integratedLuminosity = 11.467 * 1000.0; // Run2012B+C const float ggH_w = 19.6 / 476963.0 * integratedLuminosity; const float qqH_w = 1.55 / 491653.0 * integratedLuminosity; const float W1J_w = 6381.2 / 29784800.0 * integratedLuminosity; const float W2J_w = 2039.8 / 30693853.0 * integratedLuminosity; const float W3J_w = 612.5 / 15241144.0 * integratedLuminosity; const float TT_w = 225.2 / 6423106.0 * integratedLuminosity; const float ZLL_w = 3503.7 / 30458871.0 * integratedLuminosity; const float dataRunB_w = 1.0; const float dataRunC_w = 1.0; map > sampleNames; sampleNames.insert(make_pair("GluGluToHToTauTau",make_pair("ggH",ggH_w))); sampleNames.insert(make_pair("VBF_HToTauTau",make_pair("qqH",qqH_w))); sampleNames.insert(make_pair("W1JetsToLNu",make_pair("W1J",W1J_w))); sampleNames.insert(make_pair("W2JetsToLNu",make_pair("W2J",W2J_w))); sampleNames.insert(make_pair("W3JetsToLNu",make_pair("W3J",W3J_w))); sampleNames.insert(make_pair("TTbar",make_pair("TT",TT_w))); sampleNames.insert(make_pair("DYJetsToLL",make_pair("ZLL",ZLL_w))); sampleNames.insert(make_pair("Run2012B_TauPlusX",make_pair("dataRunB",dataRunB_w))); sampleNames.insert(make_pair("Run2012C_TauPlusX",make_pair("dataRunC",dataRunC_w))); //loop over sample files with names defined above for(map< string,pair >::iterator it=sampleNames.begin(); it!=sampleNames.end();it++){ TString samplename = it->first; TString thelabel = it->second.first; Float_t sampleweight = it->second.second; TStopwatch time; time.Start(); cout << ">>> Processing sample " << samplename <<" with label "<Write(); dataRunC_npv->Write(); ZLL_npv->Write(); TT_npv->Write(); W3J_npv->Write(); W2J_npv->Write(); W1J_npv->Write(); qqH_npv->Write(); ggH_npv->Write(); hfile->Close(); return 0; }