ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitHzz4l/NonMCBackground/src/ElectronFakeRateMod.cc
Revision: 1.6
Committed: Wed Jul 18 09:05:38 2012 UTC (12 years, 10 months ago) by anlevin
Content type: text/plain
Branch: MAIN
Changes since 1.5: +1 -0 lines
Log Message:
corrected pfnopileupbug with fake rates

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 PFnoPUflag.clear();
138 makePFnoPUArray(fPFCandidates, PFnoPUflag, fPrimVerts );
139
140 vector<const Muon*> goodMuons;
141 vector<const Electron*> goodElectrons;
142
143 for(Int_t i=0; i<fMuons->GetEntries(); i++) {
144 const Muon *mu = fMuons->At(i);
145 if( muonPOG2012CutBasedIDTight(mu,fPrimVerts->At(0),fPFCandidates,fPileupEnergyDensity) )
146 goodMuons.push_back(mu);
147 }
148 for(Int_t i=0; i<fElectrons->GetEntries(); i++) {
149 const Electron *ele = fElectrons->At(i);
150 Bool_t fails_some_denominator = kFALSE;
151 for ( UInt_t denominatorTypeIndex = 0 ; denominatorTypeIndex < denominatorType.size() ; ++denominatorTypeIndex ) {
152 if (passElectronDenominatorCuts(ele, fPrimVerts->At(0), denominatorType[denominatorTypeIndex]))
153 nElectrons[denominatorTypeIndex]++;
154 else
155 fails_some_denominator = kTRUE;
156 }
157
158 if(!fails_some_denominator && electronPOG2012CutBasedIDMedium(ele, fPrimVerts->At(0), fPFCandidates, fConversions, fPileupEnergyDensity))
159 goodElectrons.push_back(ele);
160 }
161
162 Int_t NZCandidates = 0;
163 const mithep::Muon* ZMuon1 = 0;
164 const mithep::Muon* ZMuon2 = 0;
165 const mithep::Electron *ZEle1 = 0;
166 const mithep::Electron *ZEle2 = 0;
167 Double_t ZPt = 0;
168 Double_t ZMass = 0;
169 for(UInt_t i=0; i<goodMuons.size(); i++) {
170 FourVectorM mu1;
171 mu1.SetCoordinates(goodMuons[i]->Pt(), goodMuons[i]->Eta(), goodMuons[i]->Phi(), g_muon_mass );
172 for(UInt_t j=i+1; j<goodMuons.size(); j++) {
173 FourVectorM mu2;
174 mu2.SetCoordinates(goodMuons[j]->Pt(), goodMuons[j]->Eta(), goodMuons[j]->Phi(), g_muon_mass );
175 FourVectorM dilepton = mu1+mu2;
176 if (dilepton.M() > massLo && dilepton.M() < massHi) {
177 ZMuon1 = goodMuons[i];
178 ZMuon2 = goodMuons[j];
179 ZPt = dilepton.Pt();
180 ZMass = dilepton.M();
181 NZCandidates++;
182 }
183 }
184 }
185
186 for(UInt_t i=0; i<goodElectrons.size(); i++) {
187 FourVectorM ele1;
188 ele1.SetCoordinates(goodElectrons[i]->Pt(), goodElectrons[i]->Eta(), goodElectrons[i]->Phi(),g_electron_mass );
189 for(UInt_t j=i+1; j<goodElectrons.size(); j++) {
190 FourVectorM ele2;
191 ele2.SetCoordinates(goodElectrons[j]->Pt(), goodElectrons[j]->Eta(), goodElectrons[j]->Phi(), g_electron_mass );
192 FourVectorM dilepton = ele1+ele2;
193 if (dilepton.M() > massLo && dilepton.M() < massHi) {
194 ZEle1 = goodElectrons[i];
195 ZEle2 = goodElectrons[j];
196 ZPt = dilepton.Pt();
197 ZMass = dilepton.M();
198 NZCandidates++;
199 }
200 }
201 }
202
203 if (NZCandidates != 1) return;
204 Int_t ZDecayType = 0;
205 if (ZMuon1 && ZMuon2) ZDecayType = 13;
206 else if (ZEle1 && ZEle2) ZDecayType = 11;
207 else cout << "Error. Z Leptons not properly found.\n";
208
209 if(ZDecayType == 13){
210
211 if(!(ZMuon1->Pt() > 20 && ZMuon2->Pt() > 10) && !(ZMuon2->Pt() > 20 && ZMuon1->Pt() > 10)) return;
212
213 std::bitset<1024> zmuon1_matchbits = mithep::ElectronFakeRateMod::GetHLTMatchBits(ZMuon1->Pt(), ZMuon1->Eta(), ZMuon1->Phi());
214 std::bitset<1024> zmuon2_matchbits = mithep::ElectronFakeRateMod::GetHLTMatchBits(ZMuon2->Pt(), ZMuon2->Eta(), ZMuon2->Phi());
215
216 Bool_t pass_dimuon_trigger1 = (ZDecayType == 13)
217 && (fTriggerBits.test(kHLT_Mu17_Mu8))
218 &&
219 (
220 ((zmuon1_matchbits.test(kHLT_Mu17_Mu8_Mu1Obj))
221 && (zmuon2_matchbits.test(kHLT_Mu17_Mu8_Mu2Obj)))
222 ||
223 ((zmuon1_matchbits.test(kHLT_Mu17_Mu8_Mu2Obj))
224 && (zmuon2_matchbits.test(kHLT_Mu17_Mu8_Mu1Obj)))
225 );
226
227 Bool_t pass_dimuon_trigger2 = (ZDecayType == 13)
228 && (fTriggerBits.test(kHLT_Mu17_TkMu8))
229 &&
230 (
231 ((zmuon1_matchbits.test(kHLT_Mu17_TkMu8_Mu1Obj))
232 && (zmuon2_matchbits.test(kHLT_Mu17_TkMu8_Mu2Obj)))
233 ||
234 ((zmuon1_matchbits.test(kHLT_Mu17_TkMu8_Mu2Obj))
235 && (zmuon2_matchbits.test(kHLT_Mu17_TkMu8_Mu1Obj)))
236 );
237
238
239 if(!pass_dimuon_trigger1 && !pass_dimuon_trigger2) return;
240 if(!(die_or_dimu == e_dimuon_data)) return;
241 }
242 else if (ZDecayType == 11){
243
244 if(!(ZEle1->Pt() > 20 && ZEle2->Pt() > 10) && !(ZEle2->Pt() > 20 && ZEle1->Pt() > 10)) return;
245
246 std::bitset<1024> zele1_matchbits = mithep::ElectronFakeRateMod::GetHLTMatchBits(ZEle1->Pt(), ZEle1->Eta(), ZEle1->Phi());
247 std::bitset<1024> zele2_matchbits = mithep::ElectronFakeRateMod::GetHLTMatchBits(ZEle2->Pt(), ZEle2->Eta(), ZEle2->Phi());
248
249 Bool_t pass_dielectron_trigger =
250 (fTriggerBits.test(kHLT_Ele17_CaloIdT_TrkIdVL_CaloIsoVL_TrkIsoVL_Ele8_CaloIdT_TrkIdVL_CaloIsoVL_TrkIsoVL))
251 &&
252 (
253 ((zele1_matchbits.test(kHLT_Ele17_CaloIdT_TrkIdVL_CaloIsoVL_TrkIsoVL_Ele8_CaloIdT_TrkIdVL_CaloIsoVL_TrkIsoVL_Ele1Obj))
254 && (zele2_matchbits.test(kHLT_Ele17_CaloIdT_TrkIdVL_CaloIsoVL_TrkIsoVL_Ele8_CaloIdT_TrkIdVL_CaloIsoVL_TrkIsoVL_Ele2Obj)))
255 ||
256 ((zele1_matchbits.test(kHLT_Ele17_CaloIdT_TrkIdVL_CaloIsoVL_TrkIsoVL_Ele8_CaloIdT_TrkIdVL_CaloIsoVL_TrkIsoVL_Ele2Obj))
257 && (zele2_matchbits.test(kHLT_Ele17_CaloIdT_TrkIdVL_CaloIsoVL_TrkIsoVL_Ele8_CaloIdT_TrkIdVL_CaloIsoVL_TrkIsoVL_Ele1Obj)))
258 );
259
260
261 if(!pass_dielectron_trigger) return;
262 if(!(die_or_dimu == e_dielectron_data)) return;
263
264 }
265 else assert(0);
266
267 nvertices = fPrimVerts->GetEntries();
268 run_num = GetEventHeader()->RunNum();
269 lumi_sec = GetEventHeader()->LumiSec();
270 evt_num = GetEventHeader()->EvtNum();
271
272
273 for(Int_t i=0; i<fElectrons->GetEntries(); i++) {
274 const Electron *ele = fElectrons->At(i);
275
276 if (ZDecayType == 11 && (ele == ZEle1 || ele == ZEle2)) continue;
277 if (ZEle1 && mithep::MathUtils::DeltaR(ele->Eta(), ele->Phi(), ZEle1->Eta(), ZEle1->Phi()) < 0.3) continue;
278 if (ZEle2 && mithep::MathUtils::DeltaR(ele->Eta(), ele->Phi(), ZEle2->Eta(), ZEle2->Phi()) < 0.3) continue;
279 if (ZMuon1 && mithep::MathUtils::DeltaR(ele->Eta(), ele->Phi(), ZMuon1->Eta(), ZMuon1->Phi()) < 0.3) continue;
280 if (ZMuon2 && mithep::MathUtils::DeltaR(ele->Eta(), ele->Phi(), ZMuon2->Eta(), ZMuon2->Phi()) < 0.3) continue;
281
282 for ( UInt_t denominatorTypeIndex = 0 ; denominatorTypeIndex < denominatorType.size() ; ++denominatorTypeIndex ) {
283
284 if ((ZDecayType == 11 && nElectrons[denominatorTypeIndex] > 3)
285 ||
286 (ZDecayType == 13 && nElectrons[denominatorTypeIndex] > 1)
287 )
288 continue;
289
290 if (passElectronDenominatorCuts(ele, fPrimVerts->At(0), denominatorType[denominatorTypeIndex])) {
291
292 fake_electron_pt[denominatorTypeIndex] = ele->Pt();
293 fake_electron_eta[denominatorTypeIndex] = ele->Eta();
294 fake_electron_phi[denominatorTypeIndex] = ele->Phi();
295
296 Bool_t passNumerator = kTRUE;
297
298 ControlFlags ctrl;
299 mithep::ElectronTools::EElectronEffectiveAreaTarget eraEle = mithep::ElectronTools::kEleEAData2012;
300 vector<const mithep::PFCandidate*> photonsToVeto;
301
302 if(!electronReferencePreSelection(ctrl, ele, fPrimVerts->At(0)).passPre()) passNumerator = kFALSE;
303 if(fabs(ele->Ip3dPVSignificance()) > 4) passNumerator = kFALSE;
304 if(!electronReferenceIDMVASelectionV1(ctrl, ele, fPrimVerts->At(0)).looseID()) passNumerator = kFALSE;
305 if(!electronReferenceIsoSelection(ctrl,ele,fPrimVerts->At(0),fPFCandidates,fPileupEnergyDensity,eraEle,photonsToVeto).passLooseIso()) passNumerator = kFALSE;
306
307 fake_electron_pass[denominatorTypeIndex] = passNumerator ? 1 : 0;
308
309 fOutputTrees[denominatorTypeIndex]->Fill();
310 }//if pass denominator cut
311 } //loop over denominator types
312 } //loop over muons
313 }
314
315
316
317 unsigned makePFnoPUArray(const mithep::Array<mithep::PFCandidate> * fPFCandidates,
318 vector<bool> &pfNoPileUpflag,
319 const mithep::Array<mithep::Vertex> * vtxArr )
320 {
321 for(UInt_t i = 0; i < fPFCandidates->GetEntries(); i++) {
322 const mithep::PFCandidate *pf = fPFCandidates->At(i);
323 assert(pf);
324
325 if(pf->PFType() == mithep::PFCandidate::eHadron) {
326 if(pf->HasTrackerTrk() &&
327 vtxArr->At(0)->HasTrack(pf->TrackerTrk()) &&
328 vtxArr->At(0)->TrackWeight(pf->TrackerTrk()) > 0) {
329
330 pfNoPileUpflag.push_back(1);
331
332 } else {
333
334 Bool_t vertexFound = kFALSE;
335 const mithep::Vertex *closestVtx = 0;
336 Double_t dzmin = 10000;
337
338 // loop over vertices
339 for(UInt_t j = 0; j < vtxArr->GetEntries(); j++) {
340 const mithep::Vertex *vtx = vtxArr->At(j);
341 assert(vtx);
342
343 if(pf->HasTrackerTrk() &&
344 vtx->HasTrack(pf->TrackerTrk()) &&
345 vtx->TrackWeight(pf->TrackerTrk()) > 0) {
346 vertexFound = kTRUE;
347 closestVtx = vtx;
348 break;
349 }
350 Double_t dz = fabs(pf->SourceVertex().Z() - vtx->Z());
351 if(dz < dzmin) {
352 closestVtx = vtx;
353 dzmin = dz;
354 }
355 }
356
357 // if(fCheckClosestZVertex) {
358 if(1) {
359 // Fallback: if track is not associated with any vertex,
360 // associate it with the vertex closest in z
361 if(vertexFound || closestVtx != vtxArr->At(0)) {
362 // pfPileUp->Add(pf);
363 pfNoPileUpflag.push_back(0);
364 } else {
365 pfNoPileUpflag.push_back(1);
366 }
367 } else {
368 if(vertexFound && closestVtx != vtxArr->At(0)) {
369 // pfPileUp->Add(pf);
370 pfNoPileUpflag.push_back(0);
371 } else {
372 // PFCandidate * pfNoPileUp->AddNew(); // Ridiculous but that's how it is
373 pfNoPileUpflag.push_back(1);
374 }
375 }
376 } //hadron & trk stuff
377 } else { // hadron
378 // PFCandidate * ptr = pfNoPileUp->AddNew();
379 pfNoPileUpflag.push_back(1);
380 }
381 } // Loop over PF candidates
382
383 return pfNoPileUpflag.size();
384 }
385
386 std::bitset<1024> mithep::ElectronFakeRateMod::GetHLTMatchBits(const Double_t pt, const Double_t eta, const Double_t phi)
387 {
388 std::bitset<1024> object_bitset;
389 const Double_t hltMatchR = 0.2;
390
391 if(HasHLTInfo()) {
392 const TriggerTable *hltTable = GetHLTTable();
393 assert(hltTable);
394 for(UInt_t itrig=0; itrig<triggerNames.size(); itrig++) {
395 const TriggerName *trigname = hltTable->Get(triggerNames[itrig].c_str());
396 if(!trigname) continue;
397
398 const TList *list = GetHLTObjectsTable()->GetList(triggerNames[itrig].c_str());
399 if(!list) continue;
400 TIter iter(list->MakeIterator());
401 const TriggerObject *to = dynamic_cast<const TriggerObject*>(iter.Next());
402
403 while(to) {
404 if(to->IsHLT()) {
405
406 //assert(triggerObjNames1[itrig].length()>0);
407
408 if(triggerObjNames1[itrig].length()>0 && (triggerObjNames1[itrig] == string(to->ModuleName()))) {
409 if(MathUtils::DeltaR(phi,eta,to->Phi(),to->Eta()) < hltMatchR){
410 object_bitset.set(triggerObjIds1[itrig]);
411 }
412 }
413
414 if(triggerObjNames2[itrig].length()>0 && (triggerObjNames2[itrig] == string(to->ModuleName()))) {
415 if(MathUtils::DeltaR(phi,eta,to->Phi(),to->Eta()) < hltMatchR){
416 object_bitset.set(triggerObjIds2[itrig]);
417 }
418 }
419
420 }
421 to = dynamic_cast<const TriggerObject*>(iter.Next());
422 }
423 }
424 }
425
426 return object_bitset;
427 }
428
429 Bool_t passElectronDenominatorCuts(const mithep::Electron *ele, const mithep::Vertex * vtx, Int_t DenominatorType) {
430
431 Bool_t pass = kTRUE;
432
433 if (DenominatorType == 1) {
434 if (fabs(ele->Ip3dPVSignificance()) > 100) pass = kFALSE;
435 if (fabs(ele->Eta()) > 2.5) pass = kFALSE;
436 if (ele->Pt() < 7) pass = kFALSE;
437 if (ele->CorrectedNExpectedHitsInner() > 1) pass = kFALSE;
438 if (fabs(ele->BestTrk()->D0Corrected(*vtx)) > 0.5 ||
439 fabs(ele->BestTrk()->DzCorrected(*vtx)) > 1.0 ) pass = kFALSE;
440 }
441 else if (DenominatorType == 2) {
442 if (fabs(ele->Ip3dPVSignificance()) > 4) pass = kFALSE;
443 if (fabs(ele->Eta()) > 2.5) pass = kFALSE;
444 if (ele->Pt() < 7) pass = kFALSE;
445 if (ele->CorrectedNExpectedHitsInner() > 1) pass = kFALSE;
446 if (fabs(ele->BestTrk()->D0Corrected(*vtx)) > 0.5 ||
447 fabs(ele->BestTrk()->DzCorrected(*vtx)) > 1.0 ) pass = kFALSE;
448 }
449 else if (DenominatorType == 3) {
450 if (fabs(ele->Ip3dPVSignificance()) > 100) pass = kFALSE;
451 if (fabs(ele->Eta()) > 2.5) pass = kFALSE;
452 if (ele->Pt() < 7) pass = kFALSE;
453 if (ele->CorrectedNExpectedHitsInner() > 1) pass = kFALSE;
454 }
455 else if (DenominatorType == 4) {
456 if (fabs(ele->Ip3dPVSignificance()) > 4) pass = kFALSE;
457 if (fabs(ele->Eta()) > 2.5) pass = kFALSE;
458 if (ele->Pt() < 7) pass = kFALSE;
459 if (ele->CorrectedNExpectedHitsInner() > 1) pass = kFALSE;
460 }
461 else
462 assert(0);
463
464 return pass;
465 }
466
467
468