ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitHzz4l/NonMCBackground/src/ElectronFakeRateMod.cc
Revision: 1.5
Committed: Mon Jul 9 08:53:53 2012 UTC (12 years, 10 months ago) by dkralph
Content type: text/plain
Branch: MAIN
Changes since 1.4: +1 -1 lines
Log Message:
Change names on lepton tag ID functions; change rootcint flags

File Contents

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