void toyMCele_cat1(std::string inputFile, std::string outputFile){ // read the ntuples (in pcncu) TreeReader data(inputFile.data()); // Declare the histogram TFile* f = new TFile(inputFile.data()); TH1D* h_totalEvents = (TH1D*)f->Get("h_totalEv"); TH1D* h_PRmassNoSIG = new TH1D("h_PRmassNoSIG", "PRmassNoSIG", 40, 40, 240); TH1D* h_PRmassAll = new TH1D("h_PRmassAll", "PRmassAll", 40, 40, 240); h_PRmassNoSIG->Sumw2(); h_PRmassAll ->Sumw2(); h_PRmassNoSIG->GetXaxis()->SetTitle("corrPRmass"); h_PRmassAll ->GetXaxis()->SetTitle("corrPRmass"); // begin of event loop Long64_t nentries = data.GetEntriesFast(); for(Long64_t ev = 0; ev < nentries; ev++){ if( ev % 1000000 == 0 ) fprintf(stderr, "Processing event %lli of %lli\n", ev + 1, nentries); data.GetEntry(ev); Float_t eventWeight = data.GetFloat("ev_weight"); TClonesArray* eleP4 = (TClonesArray*) data.GetPtrTObject("eleP4"); Int_t FATnJet = data.GetInt("FATnJet"); Int_t* FATnSubSDJet = data.GetPtrInt("FATnSubSDJet"); Float_t* corrPRmass = data.GetPtrFloat("FATjetPRmassL2L3Corr"); TClonesArray* FATjetP4 = (TClonesArray*) data.GetPtrTObject("FATjetP4"); vector<bool>& FATjetPassIDLoose = *((vector<bool>*) data.GetPtr("FATjetPassIDLoose")); vector<float>* FATsubjetSDPx = data.GetPtrVectorFloat("FATsubjetSDPx", FATnJet); vector<float>* FATsubjetSDPy = data.GetPtrVectorFloat("FATsubjetSDPy", FATnJet); vector<float>* FATsubjetSDPz = data.GetPtrVectorFloat("FATsubjetSDPz", FATnJet); vector<float>* FATsubjetSDE = data.GetPtrVectorFloat("FATsubjetSDE", FATnJet); vector<float>* FATsubjetSDCSV = data.GetPtrVectorFloat("FATsubjetSDCSV", FATnJet); // select good leptons vector<Int_t> goodLepID; TLorentzVector* thisLep = NULL; TLorentzVector* thatLep = NULL; if( !isPassZee(data,goodLepID) ) continue; thisLep = (TLorentzVector*)eleP4->At(goodLepID[0]); thatLep = (TLorentzVector*)eleP4->At(goodLepID[1]); // select good FATjet Int_t goodFATJetID = -1; TLorentzVector* thisJet = NULL; for(Int_t ij = 0; ij < FATnJet; ij++){ thisJet = (TLorentzVector*)FATjetP4->At(ij); if( thisJet->Pt() < 200 ) continue; if( fabs(thisJet->Eta()) > 2.4 ) continue; if( !FATjetPassIDLoose[ij] ) continue; if( thisJet->DeltaR(*thisLep) < 0.8 || thisJet->DeltaR(*thatLep) < 0.8 ) continue; Int_t nsubBjet = 0; for(Int_t is = 0; is < FATnSubSDJet[ij]; is++){ if( FATsubjetSDCSV[ij][is] > 0.605 ) nsubBjet++; } Double_t subjetDeltaR = -1; if( nsubBjet == 2 ){ TLorentzVector l4_subjet0(0,0,0,0); TLorentzVector l4_subjet1(0,0,0,0); l4_subjet0.SetPxPyPzE(FATsubjetSDPx[ij][0], FATsubjetSDPy[ij][0], FATsubjetSDPz[ij][0], FATsubjetSDE [ij][0]); l4_subjet1.SetPxPyPzE(FATsubjetSDPx[ij][1], FATsubjetSDPy[ij][1], FATsubjetSDPz[ij][1], FATsubjetSDE [ij][1]); subjetDeltaR = l4_subjet0.DeltaR(l4_subjet1); } // deltaR depends loose cut if( subjetDeltaR > 0.3 ) continue; if( nsubBjet < 1 ) continue; goodFATJetID = ij; break; } if( goodFATJetID < 0 ) continue; Float_t mllbb = (*thisLep+*thatLep+*thisJet).M(); if( mllbb < 750 ) continue; h_PRmassAll->Fill(corrPRmass[goodFATJetID],eventWeight); if( corrPRmass[goodFATJetID] > 30 && !(corrPRmass[goodFATJetID] > 65 && corrPRmass[goodFATJetID] < 135) && corrPRmass[goodFATJetID] < 300 ) h_PRmassNoSIG->Fill(corrPRmass[goodFATJetID],eventWeight); } // end of event loop fprintf(stderr, "Processed all events\n"); TFile* outFile = new TFile(Form("%s_pseudoTest.root",outputFile.c_str()), "recreate"); h_PRmassNoSIG->Write("corrPRmass"); h_PRmassAll ->Write("corrPRmassAll"); h_totalEvents->Write("totalEvents"); outFile->Write(); delete f; delete outFile; }
void toyMCnew_ele(std::string inputFile, std::string outputFile){ // read the ntuples (in pcncu) TreeReader data(inputFile.data()); TFile* f = new TFile(inputFile.data()); TH1D* h_totalEvents = (TH1D*)f->Get("h_totalEv"); // Create a tree to store variables TFile* outFile = new TFile(Form("%s_toyMCnew.root",outputFile.c_str()), "recreate"); TTree* tree = new TTree("tree", "TreeForRooFit"); Int_t cat = 0; Float_t mllbb, prmass, evweight; tree->Branch("cat", &cat, "cat/I"); tree->Branch("mllbb", &mllbb, "mllbb/F"); tree->Branch("prmass", &prmass, "prmass/F"); tree->Branch("evweight", &evweight, "evweight/F"); // Calculate the scale correspond to inputFile Double_t totalEvents = h_totalEvents->Integral(); Double_t crossSection = CrossSection(inputFile.data()); Double_t scale = 3000./(totalEvents/crossSection); // dataLumi = 3000/pb // Mark minor backgounds Int_t minor = 1; if( (inputFile.find("WW") != std::string::npos) || (inputFile.find("WZ") != std::string::npos) || (inputFile.find("ZZ") != std::string::npos) || (inputFile.find("TT") != std::string::npos) ) minor = -1; // begin of event loop Long64_t nentries = data.GetEntriesFast(); for(Long64_t ev = 0; ev < nentries; ev++){ if( ev % 1000000 == 0 ) fprintf(stderr, "Processing event %lli of %lli\n", ev + 1, nentries); data.GetEntry(ev); Bool_t isData = data.GetBool("isData"); Float_t eventWeight = data.GetFloat("ev_weight"); TClonesArray* eleP4 = (TClonesArray*) data.GetPtrTObject("eleP4"); Int_t FATnJet = data.GetInt("FATnJet"); Int_t* FATnSubSDJet = data.GetPtrInt("FATnSubSDJet"); Float_t* corrPRmass = data.GetPtrFloat("FATjetPRmassL2L3Corr"); TClonesArray* FATjetP4 = (TClonesArray*) data.GetPtrTObject("FATjetP4"); vector<bool>& FATjetPassIDLoose = *((vector<bool>*) data.GetPtr("FATjetPassIDLoose")); vector<float>* FATsubjetSDPx = data.GetPtrVectorFloat("FATsubjetSDPx", FATnJet); vector<float>* FATsubjetSDPy = data.GetPtrVectorFloat("FATsubjetSDPy", FATnJet); vector<float>* FATsubjetSDPz = data.GetPtrVectorFloat("FATsubjetSDPz", FATnJet); vector<float>* FATsubjetSDE = data.GetPtrVectorFloat("FATsubjetSDE", FATnJet); vector<float>* FATsubjetSDCSV = data.GetPtrVectorFloat("FATsubjetSDCSV", FATnJet); // select good leptons vector<Int_t> goodLepID; TLorentzVector* thisLep = NULL; TLorentzVector* thatLep = NULL; if( !isPassZee(data,goodLepID) ) continue; thisLep = (TLorentzVector*)eleP4->At(goodLepID[0]); thatLep = (TLorentzVector*)eleP4->At(goodLepID[1]); // select good FATjet Int_t goodFATJetID = -1; TLorentzVector* thisJet = NULL; for(Int_t ij = 0; ij < FATnJet; ij++){ thisJet = (TLorentzVector*)FATjetP4->At(ij); if( thisJet->Pt() < 200 ) continue; if( fabs(thisJet->Eta()) > 2.4 ) continue; if( !FATjetPassIDLoose[ij] ) continue; if( thisJet->DeltaR(*thisLep) < 0.8 || thisJet->DeltaR(*thatLep) < 0.8 ) continue; Int_t nsubBjet = 0; for(Int_t is = 0; is < FATnSubSDJet[ij]; is++){ if( FATsubjetSDCSV[ij][is] > 0.605 ) nsubBjet++; } Double_t subjetDeltaR = -1; if( nsubBjet == 2 ){ TLorentzVector l4_subjet0(0,0,0,0); TLorentzVector l4_subjet1(0,0,0,0); l4_subjet0.SetPxPyPzE(FATsubjetSDPx[ij][0], FATsubjetSDPy[ij][0], FATsubjetSDPz[ij][0], FATsubjetSDE [ij][0]); l4_subjet1.SetPxPyPzE(FATsubjetSDPx[ij][1], FATsubjetSDPy[ij][1], FATsubjetSDPz[ij][1], FATsubjetSDE [ij][1]); subjetDeltaR = l4_subjet0.DeltaR(l4_subjet1); } // deltaR depends loose cut if ( subjetDeltaR < 0.3 && nsubBjet > 0 ) cat = 1; else if( subjetDeltaR > 0.3 && nsubBjet > 1 ) cat = 2; else continue; goodFATJetID = ij; break; } // end of FatnJet loop if( goodFATJetID < 0 ) continue; mllbb = (*thisLep+*thatLep+*thisJet).M(); if( mllbb < 750 ) continue; prmass = corrPRmass[goodFATJetID]; if( isData ) evweight = 1; else evweight = eventWeight * scale * minor; tree->Fill(); } // end of event loop fprintf(stderr, "Processed all events\n"); tree->Write(); outFile->Write(); delete f; delete outFile; }
void mZHLimit(string inputFile, string outputFile, string channel, int cat, int mzh=0){ // setup calibration and reader BTagCalibration calib("csvv1", "/afs/cern.ch/work/h/htong/ZpZHllbb_13TeV/CSVV1.csv"); BTagCalibrationReader reader_l(BTagEntry::OP_LOOSE, "central"); BTagCalibrationReader reader_c(BTagEntry::OP_LOOSE, "central"); BTagCalibrationReader reader_b(BTagEntry::OP_LOOSE, "central"); reader_l.load(calib, BTagEntry::FLAV_UDSG, "comb"); reader_c.load(calib, BTagEntry::FLAV_C, "mujets"); reader_b.load(calib, BTagEntry::FLAV_B, "mujets"); // to read b-tag effinciency TFile* f_l = TFile::Open(Form("/afs/cern.ch/work/h/htong/ZpZHllbb_13TeV/bTagEffroot/%s_udsgflavor_zjetsBtagEff.root", channel.data())); TFile* f_c = TFile::Open(Form("/afs/cern.ch/work/h/htong/ZpZHllbb_13TeV/bTagEffroot/%s_cflavor_zjetsBtagEff.root", channel.data())); TFile* f_b = mzh==0 ? TFile::Open(Form("/afs/cern.ch/work/h/htong/ZpZHllbb_13TeV/bTagEffroot/%s_bflavor_zjetsBtagEff.root", channel.data())) : TFile::Open(Form("/afs/cern.ch/work/h/htong/ZpZHllbb_13TeV/bTagEffroot/%s_bflavor_signalBtagEff.root", channel.data())); TH1F* h_l = (TH1F*)(f_l->Get(Form("%s_udsgflavor", channel.data()))); TH1F* h_c = (TH1F*)(f_c->Get(Form("%s_cflavor", channel.data()))); TH1F* h_b = mzh==0 ? (TH1F*)(f_b->Get(Form("%s_bflavor", channel.data()))) : (TH1F*)(f_b->Get(Form("%s_bflavor_m%i", channel.data(), mzh))); // to read lepton scale factor / trigger TFile* f_ele = TFile::Open("/afs/cern.ch/work/h/htong/ZpZHllbb_13TeV/leptonSFroot/CutBasedID_LooseWP_fromTemplates_withSyst_Final.txt_SF2D.root"); TFile* f_muScal = TFile::Open("/afs/cern.ch/work/h/htong/ZpZHllbb_13TeV/leptonSFroot/MuonHighPt_Z_RunCD_Reco74X_Dec17.root"); TFile* f_muTrig = TFile::Open("/afs/cern.ch/work/h/htong/ZpZHllbb_13TeV/leptonSFroot/SingleMuonTrigger_Z_RunCD_Reco74X_Dec1.root"); TH2F* h2_ele = (TH2F*)(f_ele->Get("EGamma_SF2D")); TH2F* h2_muPt20 = (TH2F*)(f_muScal->Get("HighPtID_PtEtaBins_Pt20/abseta_pTtuneP_ratio")); TH2F* h2_muPt53 = (TH2F*)(f_muScal->Get("HighPtID_PtEtaBins_Pt53/abseta_pTtuneP_ratio")); TH2F* h2_muRunD = (TH2F*)(f_muTrig->Get("runD_Mu45_eta2p1_PtEtaBins/abseta_pt_ratio")); // read the ntuples (in pcncu) TreeReader data(inputFile.data()); TFile f(inputFile.data()); // Declare the histogram TH1D* h_totalEv = (TH1D*)f.Get("h_totalEv"); TH1D* h_mZH = new TH1D("h_mZH", "", 71, 750, 4300); h_mZH->Sumw2(); // begin of event loop fprintf(stdout, "Total events %lli\n", data.GetEntriesFast()); for( Long64_t ev = data.GetEntriesFast()-1; ev >= 0; --ev ){ if( (unsigned)ev % 100000 == 0 ) fprintf(stdout, "Still left events %lli\n", ev); data.GetEntry(ev); Bool_t isData = data.GetBool("isData"); Float_t eventWeight = data.GetFloat("ev_weight"); TClonesArray* muP4 = (TClonesArray*) data.GetPtrTObject("muP4"); TClonesArray* eleP4 = (TClonesArray*) data.GetPtrTObject("eleP4"); TClonesArray* FATjetP4 = (TClonesArray*) data.GetPtrTObject("FATjetP4"); vector<bool>& isHighPtMuon = *((vector<bool>*) data.GetPtr("isHighPtMuon")); // select good reco level events // select good leptons vector<int> goodLepID; if( channel == "ele" && !isPassZee(data,goodLepID) ) continue; if( channel == "mu" && !isPassZmumu(data,goodLepID) ) continue; TLorentzVector* thisLep = (channel=="ele") ? (TLorentzVector*)eleP4->At(goodLepID[0]) : (TLorentzVector*)muP4->At(goodLepID[0]); TLorentzVector* thatLep = (channel=="ele") ? (TLorentzVector*)eleP4->At(goodLepID[1]) : (TLorentzVector*)muP4->At(goodLepID[1]); // calculate lepton weight float thisLepWeight = 1; float thatLepWeight = 1; if( mzh != 0){ if( channel == "ele" ){ thisLepWeight = leptonWeight(h2_ele, thisLep, false); thatLepWeight = leptonWeight(h2_ele, thatLep, false); } else if( channel == "mu" ){ if( isHighPtMuon[goodLepID[0]] ) thisLepWeight = (thisLep->Pt() < 53) ? leptonWeight(h2_muPt20, thisLep) : leptonWeight(h2_muPt53, thisLep); else thisLepWeight = 1; if( isHighPtMuon[goodLepID[1]] ) thatLepWeight = (thatLep->Pt() < 53) ? leptonWeight(h2_muPt20, thatLep) : leptonWeight(h2_muPt53, thatLep); else thatLepWeight = 1; } } // only signal samples // calculate trigger weight for muon float muTrigWeight = 1; if( mzh != 0 ){ if( channel=="mu" ){ if( thisLep->Pt() > thatLep->Pt() ) muTrigWeight = (fabs(thisLep->Eta()) < 2.1) ? leptonWeight(h2_muRunD, thisLep) : 1; else muTrigWeight = (fabs(thatLep->Eta()) < 2.1) ? leptonWeight(h2_muRunD, thatLep) : 1; } else muTrigWeight = 1; } // only signal samples // select good FATjet int goodFATJetID = -1; if( !isPassJet(data, &goodFATJetID, thisLep, thatLep) ) continue; TLorentzVector* thisJet = (TLorentzVector*)FATjetP4->At(goodFATJetID); float mllbb; if( !noiseCleaning(thisLep, thatLep, thisJet, &mllbb) ) continue; // b-tag cut int nsubBjet = 0; float btagWeight = bTagWeight(data, goodFATJetID, &nsubBjet, h_l, h_c, h_b, reader_l, reader_c, reader_b); if( cat == 1 && nsubBjet != 1 ) continue; if( cat == 2 && nsubBjet != 2 ) continue; float finalWeight = isData ? 1 : eventWeight * btagWeight * thisLepWeight * thatLepWeight * muTrigWeight; h_mZH->Fill(mllbb, finalWeight); } // end of event loop fprintf(stderr, "Processed all events\n"); TFile* outFile = new TFile(Form("%s_mZHLimit.root", outputFile.data()), "recreate"); h_mZH ->Write("mZH"); h_totalEv->Write("totalEvents"); outFile->Write(); delete outFile; }
void bTagUncTree(string inputFile, string outputFile, string region, string channel){ // setup calibration and reader BTagCalibration calib("csvv1", "/afs/cern.ch/work/h/htong/ZpZHllbb_13TeV/CSVV1.csv"); BTagCalibrationReader reader_l(BTagEntry::OP_LOOSE, region.data()); BTagCalibrationReader reader_c(BTagEntry::OP_LOOSE, region.data()); BTagCalibrationReader reader_b(BTagEntry::OP_LOOSE, region.data()); reader_l.load(calib, BTagEntry::FLAV_UDSG, "comb"); reader_c.load(calib, BTagEntry::FLAV_C, "mujets"); reader_b.load(calib, BTagEntry::FLAV_B, "mujets"); // to read b-tag effinciency TFile* f_l = TFile::Open(Form("/afs/cern.ch/work/h/htong/ZpZHllbb_13TeV/bTagEffroot/%s_udsgflavor_zjetsBtagEff.root", channel.data())); TFile* f_c = TFile::Open(Form("/afs/cern.ch/work/h/htong/ZpZHllbb_13TeV/bTagEffroot/%s_cflavor_zjetsBtagEff.root", channel.data())); TFile* f_b = TFile::Open(Form("/afs/cern.ch/work/h/htong/ZpZHllbb_13TeV/bTagEffroot/%s_bflavor_zjetsBtagEff.root", channel.data())); TH1F* h_l = (TH1F*)(f_l->Get(Form("%s_udsgflavor", channel.data()))); TH1F* h_c = (TH1F*)(f_c->Get(Form("%s_cflavor", channel.data()))); TH1F* h_b = (TH1F*)(f_b->Get(Form("%s_bflavor", channel.data()))); // read the ntuples (in pcncu) TreeReader data(inputFile.data()); TFile f(inputFile.data()); // Create a tree to store variables TFile* outFile = new TFile(Form("%s_%s_%sMiniTree.root", outputFile.data(), region.data(), channel.data()), "recreate"); TTree* tree = new TTree("tree", "TreeForRooFit"); Int_t cat; Float_t mllbb, prmass, evweight; tree->Branch("cat", &cat, "cat/I"); tree->Branch("mllbb", &mllbb, "mllbb/F"); tree->Branch("prmass", &prmass, "prmass/F"); tree->Branch("evweight", &evweight, "evweight/F"); // Calculate the scale correspond to inputFile float scale = 2512.*readHist::crossSection(outputFile.data())/((TH1F*)f.Get("h_totalEv"))->Integral(); // begin of event loop fprintf(stdout, "Total events %lli\n", data.GetEntriesFast()); for( Long64_t ev = data.GetEntriesFast()-1; ev >= 0; --ev ){ if( (unsigned)ev % 100000 == 0 ) fprintf(stdout, "Still left events %lli\n", ev); data.GetEntry(ev); Float_t eventWeight = data.GetFloat("ev_weight"); Float_t* FATjetPRmassCorr = data.GetPtrFloat("FATjetPRmassL2L3Corr"); TClonesArray* muP4 = (TClonesArray*) data.GetPtrTObject("muP4"); TClonesArray* eleP4 = (TClonesArray*) data.GetPtrTObject("eleP4"); TClonesArray* FATjetP4 = (TClonesArray*) data.GetPtrTObject("FATjetP4"); // select good reco level events // select good leptons vector<int> goodLepID; if( channel == "ele" && !isPassZee(data,goodLepID) ) continue; if( channel == "mu" && !isPassZmumu(data,goodLepID) ) continue; TLorentzVector* thisLep = (channel=="ele") ? (TLorentzVector*)eleP4->At(goodLepID[0]) : (TLorentzVector*)muP4->At(goodLepID[0]); TLorentzVector* thatLep = (channel=="ele") ? (TLorentzVector*)eleP4->At(goodLepID[1]) : (TLorentzVector*)muP4->At(goodLepID[1]); // select good FATjet int goodFATJetID = -1; if( !isPassJet(data, &goodFATJetID, thisLep, thatLep, false) ) continue; TLorentzVector* thisJet = (TLorentzVector*)FATjetP4->At(goodFATJetID); if( !noiseCleaning(thisLep, thatLep, thisJet, &mllbb) ) continue; // b-tag cut int nsubBjet = 0; float btagWeight = bTagWeight(data, goodFATJetID, &nsubBjet, h_l, h_c, h_b, reader_l, reader_c, reader_b, region.data()); if ( nsubBjet == 1 ) cat = 1; else if( nsubBjet == 2 ) cat = 2; else continue; prmass = FATjetPRmassCorr[goodFATJetID]; evweight = eventWeight * scale * btagWeight; tree->Fill(); } // end of event loop fprintf(stdout, "Processed all events\n"); tree->Write(); outFile->Write(); delete outFile; }