ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitHzz4l/NonMCBackground/src/MuonFakeRateMod.cc
Revision: 1.3
Committed: Thu Jul 5 18:21:22 2012 UTC (12 years, 10 months ago) by anlevin
Content type: text/plain
Branch: MAIN
Changes since 1.2: +3 -6 lines
Log Message:
reverted to a version that compiles

File Contents

# User Rev Content
1 anlevin 1.1 #include "MuonFakeRateMod.h"
2    
3    
4     //needed because of externs
5     vector<bool> PFnoPUflag;
6     Float_t computePFMuonIso(const mithep::Muon *mu,
7     const mithep::Vertex * vtx,
8     const mithep::Array<mithep::PFCandidate> * pfCandidates,
9     const Double_t dRMax);
10    
11     unsigned makePFnoPUArray(const mithep::Array<mithep::PFCandidate> * fPFCandidates,
12     vector<bool> &pfNoPileUpFlag,
13     const mithep::Array<mithep::Vertex> * vtxArr );
14    
15     Bool_t passMuonDenominatorCuts(const mithep::Muon *mu, const mithep::Vertex * vtx, Int_t DenominatorType);
16    
17     string IntToString(int i) {
18     char temp[100];
19     sprintf(temp, "%d", i);
20     string str = temp;
21     return str;
22     }
23    
24     class TTree;
25     class TFile;
26     class TString;
27    
28     const Float_t g_muon_mass = 105.658369e-3;
29    
30    
31     void mithep::MuonFakeRateMod::SlaveBegin()
32     {
33     denominatorType.push_back(1);
34     denominatorType.push_back(2);
35    
36     nProbes = 0;
37    
38     ReqBranch("PFMet", fPFMet);
39    
40     ReqBranch(mithep::Names::gkMvfConversionBrn, fConversions);
41    
42     fMuonName = mithep::Names::gkMuonBrn;
43     ReqBranch(fMuonName, fMuons);
44    
45     fElectronName = mithep::Names::gkElectronBrn;
46     ReqBranch(fElectronName, fElectrons);
47    
48     fPFCandidateName = mithep::Names::gkPFCandidatesBrn;
49     ReqBranch(fPFCandidateName, fPFCandidates);
50    
51     fPrimVtxName = mithep::Names::gkPVBrn;
52     ReqBranch(fPrimVtxName, fPrimVerts);
53    
54     fPUEnergyDensityName = mithep::Names::gkPileupEnergyDensityBrn;
55     ReqBranch(fPUEnergyDensityName, fPileupEnergyDensity);
56    
57     fTrigMaskName = mithep::Names::gkHltBitBrn;
58     ReqBranch(fTrigMaskName, fTrigMask);
59    
60     fPileupName = mithep::Names::gkPileupInfoBrn;
61     ReqBranch(fPileupName, fPileup);
62    
63     fOutputFile = new TFile(fOutputName, "RECREATE");
64     fOutputTrees = vector<TTree*>(denominatorType.size(),0);
65     for (UInt_t denominatorTypeIndex = 0; denominatorTypeIndex < denominatorType.size(); ++denominatorTypeIndex) {
66     fOutputTrees[denominatorTypeIndex] = new TTree(TString("denominator_v"+IntToString(denominatorType[denominatorTypeIndex])),TString("denominator_v"+IntToString(denominatorType[denominatorTypeIndex])));
67     }
68    
69     fake_muon_pt = vector<Float_t>(denominatorType.size(),0);
70     fake_muon_eta = vector<Float_t>(denominatorType.size(),0);
71     fake_muon_phi = vector<Float_t>(denominatorType.size(),0);
72     fake_muon_pass = vector<UInt_t>(denominatorType.size(),0);
73    
74    
75     for (UInt_t denominatorTypeIndex = 0; denominatorTypeIndex < denominatorType.size(); ++denominatorTypeIndex) {
76     fOutputTrees[denominatorTypeIndex]->Branch("nvertices",&nvertices,"nvertices/i");
77     fOutputTrees[denominatorTypeIndex]->Branch("evt_num",&evt_num,"evt_num/i");
78     fOutputTrees[denominatorTypeIndex]->Branch("lumi_sec",&lumi_sec,"lumi_sec/i");
79     fOutputTrees[denominatorTypeIndex]->Branch("run_num",&run_num,"run_num/i");
80    
81     fOutputTrees[denominatorTypeIndex]->Branch(TString("fake_muon_pt"),&fake_muon_pt[denominatorTypeIndex],TString("fake_muon_pt/F"));
82     fOutputTrees[denominatorTypeIndex]->Branch(TString("fake_muon_eta"),&fake_muon_eta[denominatorTypeIndex],TString("fake_muon_eta/F"));
83     fOutputTrees[denominatorTypeIndex]->Branch(TString("fake_muon_phi"),&fake_muon_phi[denominatorTypeIndex],TString("fake_muon_phi/F"));
84     fOutputTrees[denominatorTypeIndex]->Branch(TString("fake_muon_pass"),&fake_muon_pass[denominatorTypeIndex],TString("fake_muon_pass/i"));
85     }
86    
87     rlrm.AddJSONFile("/afs/cern.ch/cms/CAF/CMSCOMM/COMM_DQM/certification/Collisions12/8TeV/Prompt/Cert_190456-194479_8TeV_PromptReco_Collisions12_JSON.txt");
88     }
89    
90     void mithep::MuonFakeRateMod::SlaveTerminate()
91     {
92     fOutputFile->cd();
93     for (UInt_t denominatorTypeIndex = 0; denominatorTypeIndex < denominatorType.size(); ++denominatorTypeIndex) {
94     fOutputTrees[denominatorTypeIndex]->Print();
95    
96     }
97     fOutputFile->Write();
98     fOutputFile->Close();
99    
100     }
101    
102    
103     void mithep::MuonFakeRateMod::Process()
104     {
105     vector<UInt_t> nLeptons(denominatorType.size(),0);
106     vector<UInt_t> nMuons(denominatorType.size(),0);
107    
108    
109     gDebugMask = mithep::Debug::kAnalysis; // debug message category
110     gDebugLevel = 1; // higher level allows more messages to print
111    
112     LoadBranch(fMuonName);
113     LoadBranch(fElectronName);
114     LoadBranch(fPFCandidateName);
115     LoadBranch(fPrimVtxName);
116     LoadBranch(fPUEnergyDensityName);
117     LoadBranch(fTrigMaskName);
118     LoadBranch("PFMet");
119    
120     LoadBranch(mithep::Names::gkMvfConversionBrn);
121     if(store_npu)
122     LoadBranch(fPileupName);
123    
124     mithep::RunLumiRangeMap::RunLumiPairType rl(GetEventHeader()->RunNum(), GetEventHeader()->LumiSec());
125     if(useJSON && !rlrm.HasRunLumi(rl)) return;
126    
127     fillTriggerBits( GetHLTTable(), fTrigMask, fTriggerBits );
128    
129     makePFnoPUArray(fPFCandidates, PFnoPUflag, fPrimVerts );
130    
131     if(fPFMet->At(0)->Et() >25) return;
132    
133     vector<const Muon*> goodMuons;
134     vector<const Electron*> goodElectrons;
135    
136     for(Int_t i=0; i<fMuons->GetEntries(); i++) {
137     const Muon *mu = fMuons->At(i);
138     for ( UInt_t denominatorTypeIndex = 0 ; denominatorTypeIndex < denominatorType.size() ; ++denominatorTypeIndex ) {
139     if (passMuonDenominatorCuts(mu, fPrimVerts->At(0), denominatorType[denominatorTypeIndex]))
140     nMuons[denominatorTypeIndex]++;
141     }
142 anlevin 1.3
143 anlevin 1.1 if ( //maybe you should do a loop to make sure the muon passes all of the denominators? right now this is ok because 2 is tighter...
144     passMuonDenominatorCuts(mu, fPrimVerts->At(0), denominatorType[1])
145     &&
146 anlevin 1.3 muon2012CutBasedIDTight(mu,fPrimVerts->At(0),fPFCandidates,fPileupEnergyDensity)
147 anlevin 1.1 )
148     goodMuons.push_back(mu);
149    
150     }
151     for(Int_t i=0; i<fElectrons->GetEntries(); i++) {
152     const Electron *ele = fElectrons->At(i);
153 anlevin 1.3 if(electron2012CutBasedIDMedium(ele, fPrimVerts->At(0), fPFCandidates, fConversions, fPileupEnergyDensity)) goodElectrons.push_back(ele);
154 anlevin 1.1
155     }
156    
157     Int_t NZCandidates = 0;
158     const mithep::Muon* ZMuon1 = 0;
159     const mithep::Muon* ZMuon2 = 0;
160     const mithep::Electron *ZEle1 = 0;
161     const mithep::Electron *ZEle2 = 0;
162     Double_t ZPt = 0;
163     Double_t ZMass = 0;
164     for(UInt_t i=0; i<goodMuons.size(); i++) {
165     FourVectorM mu1;
166     mu1.SetCoordinates(goodMuons[i]->Pt(), goodMuons[i]->Eta(), goodMuons[i]->Phi(), 105.658369e-3 );
167     for(UInt_t j=i+1; j<goodMuons.size(); j++) {
168     FourVectorM mu2;
169     mu2.SetCoordinates(goodMuons[j]->Pt(), goodMuons[j]->Eta(), goodMuons[j]->Phi(), 105.658369e-3 );
170     FourVectorM dilepton = mu1+mu2;
171     if (dilepton.M() > massLo && dilepton.M() < massHi) {
172     ZMuon1 = goodMuons[i];
173     ZMuon2 = goodMuons[j];
174     ZPt = dilepton.Pt();
175     ZMass = dilepton.M();
176     NZCandidates++;
177     }
178     }
179     }
180    
181     for(UInt_t i=0; i<goodElectrons.size(); i++) {
182     FourVectorM ele1;
183     ele1.SetCoordinates(goodElectrons[i]->Pt(), goodElectrons[i]->Eta(), goodElectrons[i]->Phi(), 0.51099892e-3 );
184     for(UInt_t j=i+1; j<goodElectrons.size(); j++) {
185     FourVectorM ele2;
186     ele2.SetCoordinates(goodElectrons[j]->Pt(), goodElectrons[j]->Eta(), goodElectrons[j]->Phi(), 0.51099892e-3 );
187     FourVectorM dilepton = ele1+ele2;
188     if (dilepton.M() > massLo && dilepton.M() < massHi) {
189     ZEle1 = goodElectrons[i];
190     ZEle2 = goodElectrons[j];
191     ZPt = dilepton.Pt();
192     ZMass = dilepton.M();
193     NZCandidates++;
194     }
195     }
196     }
197    
198     if (NZCandidates != 1) return;
199     Int_t ZDecayType = 0;
200     if (ZMuon1 && ZMuon2) ZDecayType = 13;
201     else if (ZEle1 && ZEle2) ZDecayType = 11;
202     else cout << "Error. Z Leptons not properly found.\n";
203    
204     if(ZDecayType == 13){
205    
206     std::bitset<1024> zmuon1_matchbits = mithep::MuonFakeRateMod::GetHLTMatchBits(ZMuon1->Pt(), ZMuon1->Eta(), ZMuon1->Phi());
207     std::bitset<1024> zmuon2_matchbits = mithep::MuonFakeRateMod::GetHLTMatchBits(ZMuon2->Pt(), ZMuon2->Eta(), ZMuon2->Phi());
208    
209     Bool_t pass_dimuon_trigger1 = (ZDecayType == 13)
210     && (fTriggerBits.test(kHLT_Mu17_Mu8))
211     &&
212     (
213     ((zmuon1_matchbits.test(kHLT_Mu17_Mu8_Mu1Obj))
214     && (zmuon2_matchbits.test(kHLT_Mu17_Mu8_Mu2Obj)))
215     ||
216     ((zmuon1_matchbits.test(kHLT_Mu17_Mu8_Mu2Obj))
217     && (zmuon2_matchbits.test(kHLT_Mu17_Mu8_Mu1Obj)))
218     );
219    
220     Bool_t pass_dimuon_trigger2 = (ZDecayType == 13)
221     && (fTriggerBits.test(kHLT_Mu17_TkMu8))
222     &&
223     (
224     ((zmuon1_matchbits.test(kHLT_Mu17_TkMu8_Mu1Obj))
225     && (zmuon2_matchbits.test(kHLT_Mu17_TkMu8_Mu2Obj)))
226     ||
227     ((zmuon1_matchbits.test(kHLT_Mu17_TkMu8_Mu2Obj))
228     && (zmuon2_matchbits.test(kHLT_Mu17_TkMu8_Mu1Obj)))
229     );
230    
231    
232     if(!pass_dimuon_trigger1 && !pass_dimuon_trigger2) return;
233     if(!(die_or_dimu == e_dimuon_data)) return;
234     }
235     else if (ZDecayType == 11){
236    
237     std::bitset<1024> zele1_matchbits = mithep::MuonFakeRateMod::GetHLTMatchBits(ZEle1->Pt(), ZEle1->Eta(), ZEle1->Phi());
238     std::bitset<1024> zele2_matchbits = mithep::MuonFakeRateMod::GetHLTMatchBits(ZEle2->Pt(), ZEle2->Eta(), ZEle2->Phi());
239    
240     Bool_t pass_dielectron_trigger =
241     (fTriggerBits.test(kHLT_Ele17_CaloIdT_TrkIdVL_CaloIsoVL_TrkIsoVL_Ele8_CaloIdT_TrkIdVL_CaloIsoVL_TrkIsoVL))
242     &&
243     (
244     ((zele1_matchbits.test(kHLT_Ele17_CaloIdT_TrkIdVL_CaloIsoVL_TrkIsoVL_Ele8_CaloIdT_TrkIdVL_CaloIsoVL_TrkIsoVL_Ele1Obj))
245     && (zele2_matchbits.test(kHLT_Ele17_CaloIdT_TrkIdVL_CaloIsoVL_TrkIsoVL_Ele8_CaloIdT_TrkIdVL_CaloIsoVL_TrkIsoVL_Ele2Obj)))
246     ||
247     ((zele1_matchbits.test(kHLT_Ele17_CaloIdT_TrkIdVL_CaloIsoVL_TrkIsoVL_Ele8_CaloIdT_TrkIdVL_CaloIsoVL_TrkIsoVL_Ele2Obj))
248     && (zele2_matchbits.test(kHLT_Ele17_CaloIdT_TrkIdVL_CaloIsoVL_TrkIsoVL_Ele8_CaloIdT_TrkIdVL_CaloIsoVL_TrkIsoVL_Ele1Obj)))
249     );
250    
251     if(!pass_dielectron_trigger) return;
252     if(!(die_or_dimu == e_dielectron_data)) return;
253    
254     }
255     else assert(0);
256    
257     nvertices = fPrimVerts->GetEntries();
258     run_num = GetEventHeader()->RunNum();
259     lumi_sec = GetEventHeader()->LumiSec();
260     evt_num = GetEventHeader()->EvtNum();
261    
262    
263     for(Int_t i=0; i<fMuons->GetEntries(); i++) {
264     const Muon *mu = fMuons->At(i);
265    
266     if (ZDecayType == 13 && (mu == ZMuon1 || mu == ZMuon2)) continue;
267     if (ZEle1 && mithep::MathUtils::DeltaR(mu->Eta(), mu->Phi(), ZEle1->Eta(), ZEle1->Phi()) < 0.3) continue;
268     if (ZEle2 && mithep::MathUtils::DeltaR(mu->Eta(), mu->Phi(), ZEle2->Eta(), ZEle2->Phi()) < 0.3) continue;
269     if (ZMuon1 && mithep::MathUtils::DeltaR(mu->Eta(), mu->Phi(), ZMuon1->Eta(), ZMuon1->Phi()) < 0.3) continue;
270     if (ZMuon2 && mithep::MathUtils::DeltaR(mu->Eta(), mu->Phi(), ZMuon2->Eta(), ZMuon2->Phi()) < 0.3) continue;
271    
272     for ( UInt_t denominatorTypeIndex = 0 ; denominatorTypeIndex < denominatorType.size() ; ++denominatorTypeIndex ) {
273     if ((ZDecayType == 11 && nMuons[denominatorTypeIndex] > 1)
274     ||
275     (ZDecayType == 13 && nMuons[denominatorTypeIndex] > 3)
276     ) continue;
277    
278     if (passMuonDenominatorCuts(mu, fPrimVerts->At(0), denominatorType[denominatorTypeIndex])) {
279    
280     fake_muon_pt[denominatorTypeIndex] = mu->Pt();
281     fake_muon_eta[denominatorTypeIndex] = mu->Eta();
282     fake_muon_phi[denominatorTypeIndex] = mu->Phi();
283    
284     Bool_t passNumerator = kTRUE;
285     vector<const mithep::PFCandidate*> photonsToVeto;
286    
287     ControlFlags ctrl;
288     mithep::MuonTools::EMuonEffectiveAreaTarget eraMu = mithep::MuonTools::kMuEAData2012;
289     if(!muonReferencePreSelection(ctrl, mu, fPrimVerts->At(0), fPFCandidates ).passPre()) passNumerator = kFALSE;
290     if(!muonIDPFSelection(ctrl, mu, fPrimVerts->At(0), fPFCandidates ).looseID()) passNumerator = kFALSE;
291     if(fabs(mu->Ip3dPVSignificance()) > 4) passNumerator = kFALSE;
292     if(!muonReferenceIsoSelection(ctrl,mu,fPrimVerts->At(0),fPFCandidates,fPileupEnergyDensity,eraMu,photonsToVeto).passLooseIso()) passNumerator = kFALSE;
293    
294     if (passNumerator)
295     fake_muon_pass[denominatorTypeIndex] = passNumerator ? 1 : 0;
296    
297     fOutputTrees[denominatorTypeIndex]->Fill();
298    
299     }//if pass denominator cut
300     } //loop over denominator types
301     } //loop over muons
302     }
303    
304     unsigned makePFnoPUArray(const mithep::Array<mithep::PFCandidate> * fPFCandidates,
305     vector<bool> &pfNoPileUpflag,
306     const mithep::Array<mithep::Vertex> * vtxArr )
307     {
308     for(UInt_t i = 0; i < fPFCandidates->GetEntries(); i++) {
309     const mithep::PFCandidate *pf = fPFCandidates->At(i);
310     assert(pf);
311    
312     if(pf->PFType() == mithep::PFCandidate::eHadron) {
313     if(pf->HasTrackerTrk() &&
314     vtxArr->At(0)->HasTrack(pf->TrackerTrk()) &&
315     vtxArr->At(0)->TrackWeight(pf->TrackerTrk()) > 0) {
316    
317     pfNoPileUpflag.push_back(1);
318    
319     } else {
320    
321     Bool_t vertexFound = kFALSE;
322     const mithep::Vertex *closestVtx = 0;
323     Double_t dzmin = 10000;
324    
325     // loop over vertices
326     for(UInt_t j = 0; j < vtxArr->GetEntries(); j++) {
327     const mithep::Vertex *vtx = vtxArr->At(j);
328     assert(vtx);
329    
330     if(pf->HasTrackerTrk() &&
331     vtx->HasTrack(pf->TrackerTrk()) &&
332     vtx->TrackWeight(pf->TrackerTrk()) > 0) {
333     vertexFound = kTRUE;
334     closestVtx = vtx;
335     break;
336     }
337     Double_t dz = fabs(pf->SourceVertex().Z() - vtx->Z());
338     if(dz < dzmin) {
339     closestVtx = vtx;
340     dzmin = dz;
341     }
342     }
343    
344     // if(fCheckClosestZVertex) {
345     if(1) {
346     // Fallback: if track is not associated with any vertex,
347     // associate it with the vertex closest in z
348     if(vertexFound || closestVtx != vtxArr->At(0)) {
349     // pfPileUp->Add(pf);
350     pfNoPileUpflag.push_back(0);
351     } else {
352     pfNoPileUpflag.push_back(1);
353     }
354     } else {
355     if(vertexFound && closestVtx != vtxArr->At(0)) {
356     // pfPileUp->Add(pf);
357     pfNoPileUpflag.push_back(0);
358     } else {
359     // PFCandidate * pfNoPileUp->AddNew(); // Ridiculous but that's how it is
360     pfNoPileUpflag.push_back(1);
361     }
362     }
363     } //hadron & trk stuff
364     } else { // hadron
365     // PFCandidate * ptr = pfNoPileUp->AddNew();
366     pfNoPileUpflag.push_back(1);
367     }
368     } // Loop over PF candidates
369    
370     return pfNoPileUpflag.size();
371     }
372    
373     std::bitset<1024> mithep::MuonFakeRateMod::GetHLTMatchBits(const Double_t pt, const Double_t eta, const Double_t phi)
374     {
375     std::bitset<1024> object_bitset;
376     const Double_t hltMatchR = 0.2;
377    
378     if(HasHLTInfo()) {
379     const TriggerTable *hltTable = GetHLTTable();
380     assert(hltTable);
381     for(UInt_t itrig=0; itrig<triggerNames.size(); itrig++) {
382     const TriggerName *trigname = hltTable->Get(triggerNames[itrig].c_str());
383     if(!trigname) continue;
384    
385     const TList *list = GetHLTObjectsTable()->GetList(triggerNames[itrig].c_str());
386     if(!list) continue;
387     TIter iter(list->MakeIterator());
388     const TriggerObject *to = dynamic_cast<const TriggerObject*>(iter.Next());
389    
390     while(to) {
391     if(to->IsHLT()) {
392    
393     //assert(triggerObjNames1[itrig].length()>0);
394    
395     if(triggerObjNames1[itrig].length()>0 && (triggerObjNames1[itrig] == string(to->ModuleName()))) {
396     if(MathUtils::DeltaR(phi,eta,to->Phi(),to->Eta()) < hltMatchR){
397     object_bitset.set(triggerObjIds1[itrig]);
398     }
399     }
400    
401     if(triggerObjNames2[itrig].length()>0 && (triggerObjNames2[itrig] == string(to->ModuleName()))) {
402     if(MathUtils::DeltaR(phi,eta,to->Phi(),to->Eta()) < hltMatchR){
403     object_bitset.set(triggerObjIds2[itrig]);
404     }
405     }
406    
407     }
408     to = dynamic_cast<const TriggerObject*>(iter.Next());
409     }
410     }
411     }
412    
413     return object_bitset;
414     }
415    
416     Bool_t passMuonDenominatorCuts(const mithep::Muon *mu, const mithep::Vertex * vtx, Int_t DenominatorType) {
417    
418     Bool_t pass = kTRUE;
419    
420     if (DenominatorType == 1) {
421    
422     if (fabs(mu->Eta()) > 2.4) pass = kFALSE;
423    
424     if (mu->Pt() < 5) pass = kFALSE;
425    
426     if(!mu->IsTrackerMuon() && !mu->IsGlobalMuon()) pass = kFALSE;
427    
428     if(fabs(mu->Ip3dPVSignificance()) > 100) pass = kFALSE;
429    
430     if(!(mu->HasTrackerTrk() && fabs(mu->TrackerTrk()->D0Corrected(*vtx)) < 0.5 && fabs(mu->TrackerTrk()->DzCorrected(*vtx)) < 1.0)) pass = kFALSE;
431    
432     }
433     else if (DenominatorType == 2) {
434    
435     if (fabs(mu->Eta()) > 2.4) pass = kFALSE;
436    
437     if (mu->Pt() < 5) pass = kFALSE;
438    
439     if(!mu->IsTrackerMuon() && !mu->IsGlobalMuon())
440     pass = kFALSE;
441    
442     if(fabs(mu->Ip3dPVSignificance()) > 4) pass = kFALSE;
443    
444     if(!(mu->HasTrackerTrk() && fabs(mu->TrackerTrk()->D0Corrected(*vtx)) < 0.5 && fabs(mu->TrackerTrk()->DzCorrected(*vtx)) < 1.0)) pass = kFALSE;
445    
446     }
447     else
448     assert(0);
449    
450     return pass;
451     }
452    
453