ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitHzz4l/NonMCBackground/src/select_fakes.cc
Revision: 1.10
Committed: Thu Jul 12 15:10:53 2012 UTC (12 years, 10 months ago) by dkralph
Content type: text/plain
Branch: MAIN
Changes since 1.9: +11 -7 lines
Log Message:
Added a PlotHeader that my plot exe's use heavily, and cleaned up the plotters.

Cleaned up my fake rate code so it almost all runs off of ntuples made by applyZPlusX in one of either ZPlusF or ZPlusFF mode, rather than different selectors.

Added a plotter for their fakes, NonMCBackground/src/plotTheirFakes.cc

Made some adjustments to fake selector to be in sync tih ZPlusX and vice versa.

Removed all the json_spirit and RunLumiRangeMap stuff from Util -- it was only there because we were compiling without MitCommon, now that we pick up MitCommon, it just causes problems to have this in two different places.

File Contents

# User Rev Content
1 dkralph 1.2 //
2     // System headers
3     //
4     #include <vector> // STL vector class
5     #include <iostream> // standard I/O
6     #include <iomanip> // functions to format standard I/O
7     #include <bitset>
8     #include <map>
9     using namespace std;
10    
11     //
12     // root headers
13     //
14     #include <TFile.h>
15     #include <TTree.h>
16     #include <TChain.h>
17     #include <TBranch.h>
18     #include <TClonesArray.h>
19    
20     //
21     // TMVA
22     //
23     #include "TMVA/Reader.h"
24     #include "TMVA/Tools.h"
25     #include "TMVA/Config.h"
26     #include "TMVA/MethodBDT.h"
27    
28     //
29     // ntuple format headers
30     //
31     #include "EventHeader.h"
32     #include "PFMet.h"
33     #include "Electron.h"
34     #include "Muon.h"
35     #include "Vertex.h"
36     #include "PFCandidate.h"
37     #include "PFCandidateCol.h"
38     #include "PileupInfoCol.h"
39     #include "PileupEnergyDensity.h"
40     #include "MCParticle.h"
41     #include "MCEventInfo.h"
42     #include "TriggerMask.h"
43     #include "TriggerTable.h"
44     #include "Names.h"
45     #include "BaseMod.h"
46    
47     //
48     // our headers
49     //
50     #include "ParseArgs.h"
51     #include "MuonSelection.h"
52     #include "ElectronSelection.h"
53     #include "IsolationSelection.h"
54 dkralph 1.3 #include "SelectionFuncs.h"
55 dkralph 1.9 #include "CommonDefs.h"
56     #include "Angles.h"
57     #include "AngleTuple.h"
58     #include "FOTuple.h"
59 dkralph 1.2 #include "TriggerUtils.h"
60     #include "PassHLT.h"
61     #include "RunLumiRangeMap.h"
62 dkralph 1.3 #include "fake_defs.h"
63 dkralph 1.5 #include "Various.h"
64 dkralph 1.2 #include "CommonDefs.h"
65    
66     #ifndef CMSSW_BASE
67     #define CMSSW_BASE "../"
68     #endif
69    
70     using namespace mithep;
71     //
72     // globals
73     //----------------------------------------------------------------------------
74 khahn 1.6 TH1D *hpu_2011, *hpu_2012;
75 dkralph 1.2 mithep::RunLumiRangeMap rlrm;
76     vector<SimpleLepton> failingLeptons ; // for fake estimation
77     vector<SimpleLepton> passingLeptons; // for fake estimation
78     vector<unsigned> cutvec;
79     vector<vector<unsigned> > zcutvec;
80     vector<vector<unsigned> > zzcutvec;
81     map<unsigned,float> evtrhoMap;
82     vector<string> cutstrs;
83     bool passes_HLT_MC;
84     vector<bool> PFnoPUflag;;
85    
86     //
87     // prototypes
88     //----------------------------------------------------------------------------
89 dkralph 1.9 SelectionStatus analysisSelection(ControlFlags &ctrl, const mithep::Muon *mu, const mithep::Vertex *vtx, mithep::Array<mithep::PFCandidate> *pfArr,
90     mithep::Array<mithep::PileupEnergyDensity> *puDArr);
91     SelectionStatus analysisSelection(ControlFlags &ctrl, const mithep::Electron *ele, const mithep::Vertex *vtx, mithep::Array<mithep::PFCandidate> *pfArr,
92     mithep::Array<mithep::PileupEnergyDensity> *puDArr);
93 dkralph 1.7 void initRunLumiRangeMap(ControlFlags &ctrl);
94 dkralph 1.9 float getMass(const mithep::Muon *zmu1, const mithep::Muon *zmu2, const mithep::Electron *zele1, const mithep::Electron *zele2);
95 dkralph 1.2 mithep::MuonTools::EMuonEffectiveAreaTarget eraMu;
96     mithep::ElectronTools::EElectronEffectiveAreaTarget eraEle;
97     vector<const mithep::PFCandidate*> photonsToVeto;
98 dkralph 1.9 vector<SimpleLepton> findFakes(ControlFlags &ctrl,
99     mithep::Array<mithep::Muon> *muonArr,
100     mithep::Array<mithep::Electron> *electronArr,
101     mithep::Array<mithep::PFJet> *jetArr,
102     mithep::Array<mithep::PFCandidate> *pfArr,
103     mithep::Array<mithep::PileupInfo> *puArr,
104     mithep::Array<mithep::PileupEnergyDensity> *puDArr,
105     mithep::Array<mithep::Vertex> *vtxArr,
106     const mithep::Vertex *vtx,
107     const mithep::Muon *zmu1,
108     const mithep::Muon *zmu2,
109     const mithep::Electron *zele1,
110     const mithep::Electron *zele2);
111     SimpleLepton fillSimpleLepton(const Muon *mu, const Electron *ele, SelectionStatus *ctrl);
112 dkralph 1.1 //=== MAIN =================================================================================================
113     int main(int argc, char** argv)
114     {
115 dkralph 1.2
116     string cmsswpath(CMSSW_BASE);
117     cmsswpath.append("/src");
118     std::bitset<1024> triggerBits;
119 dkralph 1.1 vector<vector<string> > inputFiles;
120     inputFiles.push_back(vector<string>());
121 dkralph 1.2 map<string,unsigned> entrymap; // number of unskimmed entries in each file
122 dkralph 1.1
123     //
124     // args
125     //--------------------------------------------------------------------------------------------------------------
126     ControlFlags ctrl;
127     parse_args( argc, argv, ctrl );
128     ctrl.dump();
129    
130 dkralph 1.9 // // hack: store low and high mass values in mcfmfname
131     // float massLo,massHi;
132     // TString massWindow(ctrl.mcfmfname);
133     // massWindow.ReplaceAll("..."," ");
134     // string line(massWindow);
135     // stringstream ss(line);
136     // ss >> massLo >> massHi;
137     // assert(massLo>=0 && massLo <= 8000);
138     // assert(massHi>=0 && massHi <= 8000);
139    
140 dkralph 1.1 //
141     // File I/O
142     //--------------------------------------------------------------------------------------------------------------
143     TChain * chain = new TChain("Events");
144 dkralph 1.2 TChain * hltchain = new TChain("HLT");
145    
146 dkralph 1.1 string fname;
147 dkralph 1.2 unsigned total_unskimmed=0;
148     if( !ctrl.inputfiles.empty() ) {
149     ifstream f(ctrl.inputfiles.c_str());
150     while (f >> fname) {
151     if( !(strncmp( fname.c_str(), "#", 1 ) ) ) continue; // skip commented lines
152     cout << "adding inputfile : " << fname.c_str() << endl;
153     entrymap[string(fname.c_str())] = unskimmedEntries(fname.c_str());
154     total_unskimmed += entrymap[string(fname.c_str())];
155     chain->AddFile(fname.c_str());
156     hltchain->AddFile(fname.c_str());
157     }
158     } else {
159     cout << "adding inputfile : " << ctrl.inputfile.c_str() << endl;
160     unsigned unsk_ents = unskimmedEntries(ctrl.inputfile.c_str());
161     entrymap[string(ctrl.inputfile.c_str())] = unsk_ents;
162     total_unskimmed += unsk_ents;
163     chain->AddFile(ctrl.inputfile.c_str());
164     hltchain->AddFile(ctrl.inputfile.c_str());
165 dkralph 1.1 }
166 dkralph 1.2 // write the total number of unskimmed events that went into making this output file to a text file
167     writeEntries(ctrl,total_unskimmed);
168 dkralph 1.1
169 dkralph 1.9 UInt_t nEvents=0,nMuDenom=0,nMuNumer=0,nEleDenom=0,nEleNumer=0;
170 dkralph 1.1
171 dkralph 1.9 // move this to compute_fakes
172     // double metMax = 25;
173 dkralph 1.1
174     //
175     // Setup
176     //--------------------------------------------------------------------------------------------------------------
177     const char * ofname;
178     if( strcmp( ctrl.outputfile.c_str(), "") ) {
179     ofname = ctrl.outputfile.c_str();
180     } else {
181     ofname = "fo.root";
182     }
183    
184 dkralph 1.9 // TFile *outFile = new TFile(ofname, "RECREATE");
185    
186     Angles angles;
187     KinematicsStruct kinematics;
188     InfoStruct evtinfo;
189     // GenInfoStruct geninfo;
190    
191     AngleTuple nt( (const char*)ofname, (const char*)"zznt");
192     nt.makeAngleBranch(angles);
193     nt.makeKinematicsBranch(kinematics);
194     nt.makeInfoBranch(evtinfo);
195    
196     vector<SimpleLepton> failingLeptons,passingLeptons;
197     FOTuple foTree( nt.getFile(), (const char*)"FOtree");
198     foTree.makeFailedLeptonBranch(failingLeptons);
199     foTree.makePassedLeptonBranch(passingLeptons);
200    
201     NtuplerBase foFlatTree( nt.getFile(), (const char*)"FO");
202     const char * brname = "FO";
203     const char * varlist = VAR_LIST_FOINFO;
204     foinfo foi;
205     foFlatTree.makeBranch( brname, (void*) &foi, varlist);
206 dkralph 1.2
207     // initMuonIDMVA();
208 dkralph 1.9 // getEATargets(ctrl,eraMu,eraEle);
209 dkralph 1.2 initElectronIDMVAV1();
210     initMuonIsoMVA();
211     initElectronIDMVA();
212     initElectronIsoMVA();
213     initTrigger();
214     if(!ctrl.mc)
215 dkralph 1.7 initRunLumiRangeMap(ctrl);
216 dkralph 1.1
217     //
218     // Access samples and fill histograms
219     TFile *inputFile=0;
220     TTree *eventTree=0;
221 dkralph 1.2 string currentFile("");
222 dkralph 1.1
223 dkralph 1.2 mithep::EventHeader *info = new mithep::EventHeader();
224     mithep::Array<mithep::PFMet> *metArr = new mithep::Array<mithep::PFMet>();
225     mithep::Array<mithep::Electron> *electronArr = new mithep::Array<mithep::Electron>();
226     mithep::Array<mithep::Muon> *muonArr = new mithep::Array<mithep::Muon>();
227     mithep::Array<mithep::Vertex> *vtxArr = new mithep::Array<mithep::Vertex>();
228     mithep::Array<mithep::PFCandidate> *pfArr = new mithep::Array<mithep::PFCandidate>();
229     mithep::Array<mithep::PFJet> *jetArr = new mithep::Array<mithep::PFJet>();
230     mithep::Array<mithep::PileupInfo> *puArr = new mithep::Array<mithep::PileupInfo>();
231     mithep::Array<mithep::PileupEnergyDensity> *puDArr = new mithep::Array<mithep::PileupEnergyDensity>();
232     mithep::Array<mithep::Track> *trkArr = new mithep::Array<mithep::Track>();
233     mithep::Array<mithep::MCParticle> *mcArr = new mithep::Array<mithep::MCParticle>();
234     mithep::MCEventInfo *mcEvtInfo = new mithep::MCEventInfo();
235     mithep::TriggerMask *trigMask = new mithep::TriggerMask();
236     mithep::TriggerTable *hltTable = new mithep::TriggerTable();
237     vector<string> *hltTableStrings = new vector<string>();
238    
239     TString fElectronName(Names::gkElectronBrn);
240     TString fMuonName(Names::gkMuonBrn);
241     TString fInfoName(Names::gkEvtHeaderBrn);
242     TString fPFMetName("PFMet");
243     TString fPrimVtxName(Names::gkPVBrn);
244     TString fPFCandidateName(Names::gkPFCandidatesBrn);
245     TString fPFJetName(Names::gkPFJetBrn);
246     TString fPileupInfoName(Names::gkPileupInfoBrn);
247     TString fPileupEnergyDensityName(Names::gkPileupEnergyDensityBrn);
248     TString fTrackName(Names::gkTrackBrn);
249     TString fMCParticleName(Names::gkMCPartBrn);
250     TString fMCEvtInfoName(Names::gkMCEvtInfoBrn);
251     TString fTriggerMaskName(Names::gkHltBitBrn);
252     TString fTriggerTableName(Names::gkHltTableBrn);
253    
254     chain->SetBranchAddress(fInfoName, &info);
255     chain->SetBranchAddress(fPFMetName, &metArr);
256     chain->SetBranchAddress(fElectronName, &electronArr);
257     chain->SetBranchAddress(fMuonName, &muonArr);
258     chain->SetBranchAddress(fPrimVtxName, &vtxArr);
259     chain->SetBranchAddress(fPFCandidateName, &pfArr);
260     chain->SetBranchAddress(fPFJetName, &jetArr);
261     if( ctrl.mc ) {
262     chain->SetBranchAddress(fPileupInfoName, &puArr);
263     chain->SetBranchAddress(fMCParticleName, &mcArr);
264     chain->SetBranchAddress(fMCEvtInfoName, &mcEvtInfo);
265     }
266     chain->SetBranchAddress(fPileupEnergyDensityName, &puDArr);
267     chain->SetBranchAddress(fTrackName, &trkArr);
268     chain->SetBranchAddress(fTriggerMaskName, &trigMask);
269     cout << "hlttable: " << fTriggerTableName << endl;
270     hltchain->SetBranchAddress(fTriggerTableName, &hltTableStrings);
271    
272     const mithep::Vertex *vtx; // best primary vertex in the event
273 dkralph 1.1
274 dkralph 1.2 // ginfo = NULL;
275     // if( ctrl.mc ) { chain->SetBranchAddress("Gen", &ginfo);}
276    
277     // only 1 HLT table / file ???
278     hltchain->GetEntry(0);
279    
280     int imax = chain->GetEntries();
281     cout << "nEntries: " << imax << endl;
282 dkralph 1.1 for(UInt_t ientry=0; ientry<imax; ientry++) {
283     chain->GetEntry(ientry);
284 dkralph 1.2 if(!(ientry%10000)) cout << "entry: " << ientry << "\tfrac: " << setw(15) << float(ientry)/chain->GetEntries() << endl;
285    
286 dkralph 1.9 string fname = string(chain->GetFile()->GetEndpointUrl()->GetFile());
287     if( ctrl.era == 0 )
288     setEra( fname, ctrl );
289     getEATargets(ctrl,eraMu,eraEle);
290    
291 dkralph 1.2 // pfNoPU
292     PFnoPUflag.clear();
293     int pfnopu_size = makePFnoPUArray( pfArr, PFnoPUflag, vtxArr );
294     assert(pfnopu_size == pfArr->GetEntries());
295    
296 dkralph 1.7 if(!(ctrl.noJSON) ) {
297     RunLumiRangeMap::RunLumiPairType rl(info->RunNum(), info->LumiSec());
298     if( !(rlrm.HasRunLumi(rl)) ) continue;
299     }
300 dkralph 1.2
301 dkralph 1.9 nEvents++;
302 dkralph 1.1
303 dkralph 1.10 bool goodVertex = setPV( ctrl, vtxArr, vtx );
304     if(!goodVertex) continue;
305 dkralph 1.1
306 dkralph 1.9 // move this to compute_fakes
307     // TLorentzVector pfmet; pfmet.SetPxPyPzE(metArr->At(0)->Mex(),metArr->At(0)->Mey(),0,0);
308     // if(pfmet.Pt() > metMax) continue;
309 dkralph 1.1
310 dkralph 1.2 // good lepton selection
311 dkralph 1.9 failingLeptons.clear();
312     passingLeptons.clear();
313 dkralph 1.2 vector<const mithep::Muon*> goodMuons;
314     vector<const mithep::Electron*> goodElectrons;
315 dkralph 1.9 vector<SelectionStatus> muselv,eleselv;
316 dkralph 1.2 for(int imu=0; imu<muonArr->GetEntries(); imu++) {
317     const mithep::Muon *mu = (*muonArr)[imu];
318 dkralph 1.9 SelectionStatus status = analysisSelection(ctrl,mu,vtx,pfArr,puDArr);
319     if(status.loose()) {
320     goodMuons.push_back(mu);
321     muselv.push_back(status);
322     passingLeptons.push_back(fillSimpleLepton(mu,&status));
323     } else {
324     failingLeptons.push_back(fillSimpleLepton(mu,&status));
325     }
326 dkralph 1.2 }
327 dkralph 1.1 for(Int_t i=0; i<electronArr->GetEntries(); i++) {
328 dkralph 1.2 const mithep::Electron *ele = (*electronArr)[i];
329 dkralph 1.4 bool muonMatch=false;
330     for(unsigned imu=0; imu<muonArr->GetEntries(); imu++) {
331     const mithep::Muon *mu = (*muonArr)[imu];
332     if(dr(mu,ele) < 0.05) muonMatch = true;
333     }
334 dkralph 1.9 if(muonMatch) continue;
335     SelectionStatus status = analysisSelection(ctrl,ele,vtx,pfArr,puDArr);
336     if(status.loose()) {
337     goodElectrons.push_back(ele);
338     eleselv.push_back(status);
339     passingLeptons.push_back(fillSimpleLepton(ele,&status));
340     } else {
341     failingLeptons.push_back(fillSimpleLepton(ele,&status));
342     }
343 dkralph 1.1 }
344    
345 dkralph 1.2 // look for z pairs
346     int NZCandidates=0;
347 dkralph 1.9 float bestMass=-999;
348 dkralph 1.2 const mithep::Muon *zmu1=0,*zmu2=0;
349     const mithep::Electron *zele1=0,*zele2=0;
350 dkralph 1.9 SelectionStatus *status1,*status2;
351 dkralph 1.2 for(unsigned imu=0; imu<goodMuons.size(); imu++) {
352     for(unsigned jmu=imu+1; jmu<goodMuons.size(); jmu++) {
353     if(goodMuons[imu]->Charge() == goodMuons[jmu]->Charge()) continue;
354 dkralph 1.9 float mass = getMass(goodMuons[imu],goodMuons[jmu],0,0);
355     if( fabs(mass - Z_MASS) < fabs(bestMass - Z_MASS)) {
356     bestMass = mass;
357     zmu1 = goodMuons[imu]; status1 = &muselv[imu];
358     zmu2 = goodMuons[jmu]; status2 = &muselv[jmu];
359     }
360 dkralph 1.2 }
361     }
362     for(unsigned iele=0; iele<goodElectrons.size(); iele++) {
363     for(unsigned jele=iele+1; jele<goodElectrons.size(); jele++) {
364     if(goodElectrons[iele]->Charge() == goodElectrons[jele]->Charge()) continue;
365 dkralph 1.9 float mass = getMass(0,0,goodElectrons[iele],goodElectrons[jele]);
366     if( fabs(mass - Z_MASS) < fabs(bestMass - Z_MASS)) {
367     bestMass = mass;
368     zele1 = goodElectrons[iele]; status1 = &eleselv[iele];
369     zele2 = goodElectrons[jele]; status2 = &eleselv[jele];
370     zmu1 = zmu2 = 0;
371     }
372 dkralph 1.1 }
373 dkralph 1.2 }
374 dkralph 1.9 if(bestMass < 0) continue;
375     assert( (zmu1 && zmu2) || (zele1 && zele2));
376     assert( !(zmu1!=0 && zele1!=0) );
377     assert( !(zmu2!=0 && zele2!=0) );
378    
379     EventData data;
380     data.Z1leptons.push_back(fillSimpleLepton(zmu1,zele1,status1));
381     data.Z1leptons.push_back(fillSimpleLepton(zmu2,zele2,status2));
382     data.Z2leptons.push_back(fillSimpleLepton(zmu1,zele1,status1));
383     data.Z2leptons.push_back(fillSimpleLepton(zmu2,zele2,status2));
384    
385     // loop over fakes
386     vector<SimpleLepton> fakes = findFakes(ctrl,muonArr,electronArr,jetArr,pfArr,puArr,puDArr,vtxArr,vtx,zmu1,zmu2,zele1,zele2);
387 dkralph 1.10 //????????????????????????????????????????????????????????????????????????????????????????
388     // if(fakes.size() > 1) continue;
389     //????????????????????????????????????????????????????????????????????????????????????????
390 dkralph 1.9 for(unsigned ifake=0; ifake<fakes.size(); ifake++) {
391     SimpleLepton fake = fakes[ifake];
392     if(zmu1) {
393     if(dr(fake,zmu1) < 0.3) continue;
394     if(dr(fake,zmu2) < 0.3) continue;
395     }
396     if(zele1) {
397     if(dr(fake,zele1) < 0.3) continue;
398     if(dr(fake,zele2) < 0.3) continue;
399     }
400 dkralph 1.2
401 dkralph 1.9 if(abs(fake.type) == 13) {
402     nMuDenom++;
403     if(fake.isLoose) nMuNumer++;
404     } else {
405     nEleDenom++;
406     if(fake.isLoose) nEleNumer++;
407     }
408 dkralph 1.1
409 dkralph 1.9 // fill output TTree
410 dkralph 1.10 fillKinematics(data,kinematics);
411     TLorentzVector pfmet; pfmet.SetPxPyPzE(metArr->At(0)->Mex(),metArr->At(0)->Mey(),0,0);
412     fillEventInfo( info, pfmet, evtinfo, ctrl.mc ? getNPU(puArr) : 0, vtxArr->GetEntries());
413     foTree.Fill();
414     nt.Fill();
415    
416 dkralph 1.9 foi.run = info->RunNum();
417     foi.evt = info->EvtNum();
418     foi.lumi = info->LumiSec();
419     foi.type = fake.type;
420     foi.npu = ctrl.mc ? getNPU(puArr) : 0;
421     foi.npv = vtxArr->GetEntries(); // note: should maybe apply some selection here
422     foi.fopt = fake.vec.Pt();
423     foi.foeta = fake.vec.Eta();
424     foi.fophi = fake.vec.Phi();
425     foi.fopass = fake.isLoose ? 1 : 0;
426     foi.mass = getMass(zmu1,zmu2,zele1,zele2);
427     foi.l1pt = zmu1 ? zmu1->Pt() : zele1->Pt();
428     foi.l2pt = zmu2 ? zmu2->Pt() : zele2->Pt();
429     foi.met = pfmet.Pt();
430     foFlatTree.Fill();
431     }
432 dkralph 1.2 }
433    
434 dkralph 1.9 // outFile->Write();
435     // outFile->Close();
436     foTree.getFile()->cd();
437     foTree.getTree()->Write();
438     foFlatTree.getFile()->cd();
439     foFlatTree.getTree()->Write();
440     nt.WriteClose();
441 dkralph 1.2
442 dkralph 1.1 //--------------------------------------------------------------------------------------------------------------
443     // Summary print out
444     //==============================================================================================================
445     cout << endl;
446     cout << "*" << endl;
447     cout << "* SUMMARY" << endl;
448     cout << "*--------------------------------------------------" << endl;
449     cout << endl;
450    
451 dkralph 1.9 cout << " >>> No. of events processed: " << nEvents << endl;
452     cout << " >>> No. of denominator muons: " << nMuDenom << endl;
453     cout << " >>> No. of numerator muons: " << nMuNumer << endl;
454     cout << " >>> ratio: " << float(nMuNumer)/nMuDenom << endl;
455     cout << " >>> No. of denominator eles: " << nEleDenom << endl;
456     cout << " >>> No. of numerator eles: " << nEleNumer << endl;
457     cout << " >>> ratio: " << float(nEleNumer)/nEleDenom << endl;
458 dkralph 1.1 cout << endl;
459    
460 dkralph 1.2 TString outputDir(ofname);
461     outputDir = outputDir(0,outputDir.Last('/'));
462 dkralph 1.1 ofstream txtfile;
463     char txtfname[100];
464 dkralph 1.2 TString txtname(ctrl.outputfile);
465     txtname.ReplaceAll(".root",".txt");
466     cout << "writing text to: " << txtname << endl;
467     txtfile.open(txtname);
468 dkralph 1.1 txtfile << "*" << endl;
469     txtfile << "* SUMMARY" << endl;
470     txtfile << "*--------------------------------------------------" << endl;
471     txtfile << endl;
472    
473 dkralph 1.9 txtfile << " >>> No. of events processed: " << nEvents << endl;
474     txtfile << " >>> No. of denominator muons: " << nMuDenom << endl;
475     txtfile << " >>> No. of numerator muons: " << nMuNumer << endl;
476     txtfile << " >>> ratio: " << float(nMuNumer)/nMuDenom << endl;
477     txtfile << " >>> No. of denominator eles: " << nEleDenom << endl;
478     txtfile << " >>> No. of numerator eles: " << nEleNumer << endl;
479     txtfile << " >>> ratio: " << float(nEleNumer)/nEleDenom << endl;
480 dkralph 1.1 txtfile << endl;
481     txtfile.close();
482    
483     cout << " <> Output saved in " << outputDir << "/" << endl;
484     cout << endl;
485    
486     cout << "done!" << endl;
487 dkralph 1.2 }
488 dkralph 1.9 //----------------------------------------------------------------------------
489     void initRunLumiRangeMap(ControlFlags &ctrl)
490     //----------------------------------------------------------------------------
491     {
492     /*
493     rlrm.AddJSONFile(std::string("./data/Cert_136033-149442_7TeV_Apr21ReReco_Collisions10_JSON.txt"));
494     // rlrm.AddJSONFile(std::string("./data/Cert_160404-173244_7TeV_PromptReco_Collisions11_JSON_v2.txt"));
495     rlrm.AddJSONFile(std::string("./data/Cert_160404-178078_7TeV_PromptReco_Collisions11_JSON.txt"));
496     rlrm.AddJSONFile(std::string("./data/Cert_160404-163869_7TeV_May10ReReco_Collisions11_JSON_v3.txt"));
497     rlrm.AddJSONFile(std::string("./data/Cert_170249-172619_7TeV_ReReco5Aug_Collisions11_JSON.txt"));
498     // r11b
499     rlrm.AddJSONFile(std::string("./data/Cert_160404-180252_7TeV_PromptReco_Collisions11_JSON.txt"));
500     */
501    
502     // 2012 only for now ...
503     rlrm.AddJSONFile(string(ctrl.jsonFile));
504    
505     };
506     //----------------------------------------------------------------------------------------
507     float getMass(const mithep::Muon *zmu1, const mithep::Muon *zmu2, const mithep::Electron *zele1, const mithep::Electron *zele2)
508     {
509     TLorentzVector lep1,lep2;
510     if(zmu1 && zmu2) {
511     lep1.SetPtEtaPhiM(zmu1->Pt(),zmu1->Eta(),zmu1->Phi(),MUON_MASS);
512     lep2.SetPtEtaPhiM(zmu2->Pt(),zmu2->Eta(),zmu2->Phi(),MUON_MASS);
513     } else if(zele1 && zele2) {
514     lep1.SetPtEtaPhiM(zele1->Pt(),zele1->Eta(),zele1->Phi(),ELECTRON_MASS);
515     lep2.SetPtEtaPhiM(zele2->Pt(),zele2->Eta(),zele2->Phi(),ELECTRON_MASS);
516     } else assert(0);
517    
518     return (lep1 + lep2).M();
519     }
520     //----------------------------------------------------------------------------------------
521     SelectionStatus analysisSelection(ControlFlags &ctrl, const mithep::Muon *mu, const mithep::Vertex *vtx, mithep::Array<mithep::PFCandidate> *pfArr,
522     mithep::Array<mithep::PileupEnergyDensity> *puDArr)
523     {
524     SelectionStatus status;
525     status |= muonReferencePreSelection(ctrl,mu,vtx,pfArr);
526     status |= muonIDPFSelection(ctrl,mu,vtx,pfArr);
527     status |= muonReferenceIsoSelection(ctrl,mu,vtx,pfArr,puDArr,eraMu,photonsToVeto);
528     return status;
529     }
530 dkralph 1.2 //----------------------------------------------------------------------------------------
531 dkralph 1.9 SelectionStatus analysisSelection(ControlFlags &ctrl, const mithep::Electron *ele, const mithep::Vertex *vtx, mithep::Array<mithep::PFCandidate> *pfArr,
532     mithep::Array<mithep::PileupEnergyDensity> *puDArr)
533     {
534     SelectionStatus status;
535     status |= electronReferencePreSelection(ctrl,ele,vtx);
536     status |= electronReferenceIDMVASelectionV1(ctrl,ele,vtx);
537     status |= electronReferenceIsoSelection(ctrl,ele,vtx,pfArr,puDArr,eraEle,photonsToVeto);
538     return status;
539     }
540     //----------------------------------------------------------------------------------------
541     vector<SimpleLepton> findFakes(ControlFlags &ctrl,
542     mithep::Array<mithep::Muon> *muonArr,
543     mithep::Array<mithep::Electron> *electronArr,
544     mithep::Array<mithep::PFJet> *jetArr,
545     mithep::Array<mithep::PFCandidate> *pfArr,
546     mithep::Array<mithep::PileupInfo> *puArr,
547     mithep::Array<mithep::PileupEnergyDensity> *puDArr,
548     mithep::Array<mithep::Vertex> *vtxArr,
549     const mithep::Vertex *vtx,
550     const mithep::Muon *zmu1,
551     const mithep::Muon *zmu2,
552     const mithep::Electron *zele1,
553     const mithep::Electron *zele2)
554     {
555     vector<SimpleLepton> fakes;
556    
557 dkralph 1.2 for(Int_t imu=0; imu<muonArr->GetEntries(); imu++) {
558     const mithep::Muon* mu = (*muonArr)[imu];
559    
560 dkralph 1.9 if(mu==zmu1 || mu==zmu2) continue;
561 dkralph 1.2
562 dkralph 1.7 SelectionStatus denomsel;
563 dkralph 1.8 denomsel |= muonPreSelectionNoD0DzIP(ctrl,mu,vtx,pfArr);;
564     if( !denomsel.passPre() ) continue;
565 dkralph 1.2
566 dkralph 1.9 SelectionStatus status = analysisSelection(ctrl,mu,vtx,pfArr,puDArr);
567 dkralph 1.2
568 dkralph 1.9 SimpleLepton fake;
569     fake.vec.SetPtEtaPhiM(mu->Pt(),mu->Eta(),mu->Phi(),MUON_MASS);
570     fake.type = 13;
571     fake.isLoose = status.loose();
572    
573     fakes.push_back(fake);
574 dkralph 1.2 }
575     for(Int_t iele=0; iele<electronArr->GetEntries(); iele++) {
576     const mithep::Electron* ele = (*electronArr)[iele];
577    
578 dkralph 1.4 bool muonMatch=false;
579     for(unsigned imu=0; imu<muonArr->GetEntries(); imu++) {
580     const mithep::Muon *mu = (*muonArr)[imu];
581     if(dr(mu,ele) < 0.05) muonMatch = true;
582     }
583 dkralph 1.9 if(muonMatch) continue;
584 dkralph 1.4
585 dkralph 1.9 if(ele==zele1 || ele==zele2) continue;
586 dkralph 1.2
587 dkralph 1.7 SelectionStatus denomsel;
588 dkralph 1.8 denomsel |= electronPreSelectionNoD0DzIP(ctrl,ele,vtx);
589     if( !denomsel.passPre() ) continue;
590 dkralph 1.2
591 dkralph 1.9 SelectionStatus status = analysisSelection(ctrl,ele,vtx,pfArr,puDArr);
592    
593     SimpleLepton fake;
594     fake.vec.SetPtEtaPhiM(ele->Pt(),ele->Eta(),ele->Phi(),ELECTRON_MASS);
595     fake.type = 11;
596     fake.isLoose = status.loose();
597 dkralph 1.8
598 dkralph 1.9 fakes.push_back(fake);
599     }
600 dkralph 1.2
601 dkralph 1.9 return fakes;
602 dkralph 1.1 }
603 dkralph 1.9 //----------------------------------------------------------------------------------------
604     SimpleLepton fillSimpleLepton(const Muon *mu, const Electron *ele, SelectionStatus *ctrl)
605 dkralph 1.2 {
606 dkralph 1.9 if(mu) return fillSimpleLepton(mu, ctrl);
607     else if(ele) return fillSimpleLepton(ele,ctrl);
608     else assert(0);
609     return SimpleLepton();
610     }