ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitHzz4l/Selection/src/applySelection.cc
Revision: 1.34
Committed: Sun Jun 3 16:30:41 2012 UTC (12 years, 11 months ago) by khahn
Content type: text/plain
Branch: MAIN
Changes since 1.33: +2 -2 lines
Log Message:
fix commit error for trigger

File Contents

# User Rev Content
1 khahn 1.1 //
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 khahn 1.13 #include <bitset>
8 khahn 1.19 #include <map>
9 khahn 1.1 using namespace std;
10    
11     //
12 khahn 1.13 // root headers
13 khahn 1.1 //
14 khahn 1.13 #include <TFile.h>
15     #include <TTree.h>
16     #include <TChain.h>
17     #include <TBranch.h>
18     #include <TClonesArray.h>
19 khahn 1.1
20 khahn 1.11
21 khahn 1.1 //
22 khahn 1.13 // TMVA
23     //
24     #include "TMVA/Reader.h"
25     #include "TMVA/Tools.h"
26     #include "TMVA/Config.h"
27     #include "TMVA/MethodBDT.h"
28    
29     //
30 khahn 1.1 // ntuple format headers
31     //
32 khahn 1.13 #include "EventHeader.h"
33     #include "Electron.h"
34     #include "Muon.h"
35     #include "Vertex.h"
36     #include "PFCandidate.h"
37     #include "PFCandidateCol.h"
38 khahn 1.16 #include "PileupInfoCol.h"
39     #include "PileupEnergyDensity.h"
40 khahn 1.23 #include "MCParticle.h"
41 khahn 1.13 #include "TriggerMask.h"
42     #include "TriggerTable.h"
43     #include "Names.h"
44     #include "BaseMod.h"
45 khahn 1.1
46     //
47 khahn 1.13 // our headers
48 khahn 1.1 //
49     #include "ParseArgs.h"
50 khahn 1.13 #include "MuonSelection.h"
51     #include "ElectronSelection.h"
52     #include "IsolationSelection.h"
53 khahn 1.29 #include "ReferenceSelection.h"
54 khahn 1.14
55 khahn 1.13 #include "TriggerUtils.h"
56     #include "PassHLT.h"
57     #include "Angles.h"
58     #include "KinematicsStruct.h"
59     #include "InfoStruct.h"
60 khahn 1.28 #include "GenInfoStruct.h"
61 khahn 1.13 #include "WeightStruct.h"
62     #include "AngleTuple.h"
63 khahn 1.15 #include "FOTuple.h"
64 khahn 1.13
65 khahn 1.33 #include "RunLumiRangeMap.h"
66 dkralph 1.2 #include "SampleWeight.h"
67 khahn 1.13 #include "EfficiencyWeightsInterface.h"
68 khahn 1.1
69 khahn 1.16 #include "SimpleLepton.h"
70    
71 khahn 1.13 #ifndef CMSSW_BASE
72     #define CMSSW_BASE "../"
73     #endif
74 khahn 1.1
75 khahn 1.13 using namespace mithep;
76 khahn 1.11
77 khahn 1.16 //
78     // globals
79     //----------------------------------------------------------------------------
80     TH1D * hpu;
81     RunLumiRangeMap rlrm;
82     vector<SimpleLepton> failingLeptons ; // for fake estimation
83     vector<SimpleLepton> passingLeptons; // for fake estimation
84 khahn 1.19 vector<unsigned> cutvec;
85     vector<vector<unsigned> > zcutvec;
86     vector<vector<unsigned> > zzcutvec;
87     map<unsigned,float> evtrhoMap;
88     vector<string> cutstrs;
89 khahn 1.21 bool passes_HLT_MC;
90 khahn 1.22 vector<bool> PFnoPUflag;;
91 khahn 1.11
92 khahn 1.16 //
93     // prototypes
94 khahn 1.13 //----------------------------------------------------------------------------
95 khahn 1.16 void initPUWeights();
96     double getPUWeight(unsigned npu);
97 khahn 1.28 void setEra(string, ControlFlags&);
98 khahn 1.16 void setEffiencyWeights(EventData&, WeightStruct& );
99     void initRunLumiRangeMap();
100 khahn 1.19 void initEvtRhoMap(map<unsigned,float> &);
101 khahn 1.22 unsigned makePFnoPUArray(mithep::Array<PFCandidate> * fPFCandidates,
102     vector<bool> &pfNoPileUpFlag,
103 khahn 1.28 const mithep::Array<mithep::Vertex> * vtxArr );
104 khahn 1.13 //----------------------------------------------------------------------------
105 khahn 1.11
106 khahn 1.16
107     //
108     // MAIN
109     //----------------------------------------------------------------------------
110 khahn 1.1 int main(int argc, char** argv)
111 khahn 1.16 //----------------------------------------------------------------------------
112 khahn 1.1 {
113 khahn 1.19 cutstrs.push_back(string("skim0")); //0
114     cutstrs.push_back(string("skim1")); //1
115 anlevin 1.25 cutstrs.push_back(string("skim2")); //1
116 khahn 1.21 cutstrs.push_back(string("trigger")); //2
117 khahn 1.19 // -------------------------------------------------
118 khahn 1.21 cutstrs.push_back(string("Z candidate")); //3
119     cutstrs.push_back(string("good Z1")); //4
120 khahn 1.19 // -------------------------------------------------
121 khahn 1.21 cutstrs.push_back(string("4l")); //5
122     cutstrs.push_back(string("ZZ candidate")); //6
123     cutstrs.push_back(string("good Z2")); //7
124     cutstrs.push_back(string("ZZ 20/10")); //8
125     cutstrs.push_back(string("resonance")); //9
126     cutstrs.push_back(string("m4l>70")); //10
127 anlevin 1.25 cutstrs.push_back(string("m4l>100, mZ2 > 12")); //11
128 khahn 1.19
129    
130    
131    
132 khahn 1.13 string cmsswpath(CMSSW_BASE);
133     cmsswpath.append("/src");
134     std::bitset<1024> triggerBits;
135 khahn 1.1 vector<vector<string> > inputFiles;
136     inputFiles.push_back(vector<string>());
137 khahn 1.16 map<string,unsigned> entrymap; // number of unskimmed entries in each file
138 khahn 1.21 for( int i=0; i<cutstrs.size(); i++ ) cutvec.push_back(0);
139 khahn 1.19 for( int i=0; i<2; i++ ) {
140     zcutvec.push_back(vector<unsigned>());
141 khahn 1.21 for( int j=0; j<cutstrs.size(); j++ ) zcutvec[i].push_back(0);
142 khahn 1.19 }
143     for( int i=0; i<3; i++ ) {
144     zzcutvec.push_back(vector<unsigned>());
145 khahn 1.21 for( int j=0; j<cutstrs.size(); j++ ) zzcutvec[i].push_back(0);
146 khahn 1.19 }
147    
148 khahn 1.1
149     //
150     // args
151     //--------------------------------------------------------------------------------------------------------------
152     ControlFlags ctrl;
153     parse_args( argc, argv, ctrl );
154 khahn 1.13 if( ctrl.inputfiles.empty() &&ctrl.inputfile.empty() )
155     {
156     cerr << "usage: applySelection.exe <flags> " << endl;
157     cerr << "\tmandoatory flags : " << endl;
158     cerr << "\t--inputfiles | file containing a list of ntuples to run over" << endl;
159     cerr << "\t--inputfile | a file to run over" << endl;
160     cerr << "\t--outputfile | file to store selected evet" << endl;
161     return 1;
162     }
163 khahn 1.1 ctrl.dump();
164 khahn 1.13
165 khahn 1.1
166 dkralph 1.4
167 khahn 1.1 //
168     // File I/O
169     //--------------------------------------------------------------------------------------------------------------
170     TChain * chain = new TChain("Events");
171 khahn 1.13 TChain * hltchain = new TChain("HLT");
172    
173 khahn 1.1 string fname;
174 khahn 1.13 if( !ctrl.inputfiles.empty() ) {
175     ifstream f(ctrl.inputfiles.c_str());
176     while (f >> fname) {
177     if( !(strncmp( fname.c_str(), "#", 1 ) ) ) continue; // skip commented lines
178     cout << "adding inputfile : " << fname.c_str() << endl;
179     entrymap[string(fname.c_str())] = unskimmedEntries(fname.c_str());
180 khahn 1.16 cout << "unskimmed entries: " << entrymap[string(fname.c_str())] << endl;
181 khahn 1.13 chain->AddFile(fname.c_str());
182     hltchain->AddFile(fname.c_str());
183     }
184     } else {
185     cout << "adding inputfile : " << ctrl.inputfile.c_str() << endl;
186 khahn 1.16 unsigned tmpent = unskimmedEntries(ctrl.inputfile.c_str());
187     cout << "tmpent: " << tmpent << endl;
188 khahn 1.13 entrymap[string(ctrl.inputfile.c_str())] = unskimmedEntries(ctrl.inputfile.c_str());
189 khahn 1.16 cout << "unskimmed entries: " << entrymap[string(ctrl.inputfile.c_str())] << endl;
190 khahn 1.13 chain->AddFile(ctrl.inputfile.c_str());
191     hltchain->AddFile(ctrl.inputfile.c_str());
192 khahn 1.1 }
193    
194 dkralph 1.10 const char * ofname;
195     if( strcmp( ctrl.outputfile.c_str(), "") ) {
196     ofname = ctrl.outputfile.c_str();
197     } else {
198     ofname = "tmp.root";
199     }
200 khahn 1.13 // table of cross section values
201     string xspath = (cmsswpath+string("/MitPhysics/data/xs.dat"));
202     cout << "xspath: " << xspath.c_str() << endl;
203     SimpleTable xstab(xspath.c_str());
204 khahn 1.11
205 khahn 1.1
206     //
207 khahn 1.13 // Setup
208 khahn 1.1 //--------------------------------------------------------------------------------------------------------------
209 khahn 1.13 Angles angles;
210     KinematicsStruct kinematics;
211     InfoStruct evtinfo;
212     WeightStruct weights;
213 khahn 1.28 GenInfoStruct geninfo;
214 khahn 1.13
215     AngleTuple nt( (const char*)ofname, (const char*)"zznt");
216     nt.makeAngleBranch(angles);
217     nt.makeKinematicsBranch(kinematics);
218     nt.makeInfoBranch(evtinfo);
219 khahn 1.17
220 khahn 1.33
221 khahn 1.17 FOTuple foTree( nt.getFile(), (const char*)"FOtree");
222 khahn 1.16 foTree.makeFailedLeptonBranch(failingLeptons);
223 khahn 1.33 foTree.makePassedLeptonBranch(passingLeptons);
224 khahn 1.13
225 khahn 1.28 TH1F * h_wf11_hpt;
226 khahn 1.13 if(ctrl.mc) {
227     nt.makeWeightBranch(weights);
228 khahn 1.33 // nt.makeGenInfoBranch(geninfo);
229 khahn 1.13 initEfficiencyWeights();
230 khahn 1.16 initPUWeights();
231 khahn 1.28
232 khahn 1.13 // Higgs only, pt reweighting
233     if( ctrl.mH <= 140 ) {
234     char wptname[256];
235     sprintf( wptname, "/data/blue/pharris/Flat/ntupler/root/weight_ptH_%i.root", ctrl.mH );
236     TFile * f_hpt = new TFile(wptname);
237     f_hpt->Print();
238     sprintf(wptname, "weight_hqt_fehipro_fit_%i", ctrl.mH);
239 khahn 1.28 h_wf11_hpt = (TH1F*)(f_hpt->FindObjectAny(wptname));
240 khahn 1.13 }
241 khahn 1.28
242 khahn 1.13 } else {
243     initRunLumiRangeMap();
244     }
245    
246 khahn 1.14 // initMuonIDMVA();
247 khahn 1.33 initElectronIDMVAV1();
248 khahn 1.13 initTrigger();
249 khahn 1.19
250     // hack
251     initEvtRhoMap(evtrhoMap);
252 khahn 1.15
253 khahn 1.13
254 khahn 1.16 //
255 khahn 1.13 // Setup tree I/O
256 khahn 1.16 //--------------------------------------------------------------------------------------------------------------
257 khahn 1.1 TFile *inputFile=0;
258     TTree *eventTree=0;
259 khahn 1.13 string currentFile("");
260    
261     mithep::EventHeader *info = new mithep::EventHeader();
262     mithep::Array<mithep::Electron> *electronArr = new mithep::Array<mithep::Electron>();
263     mithep::Array<mithep::Muon> *muonArr = new mithep::Array<mithep::Muon>();
264 khahn 1.28 mithep::Array<mithep::Vertex> *vtxArr = new mithep::Array<mithep::Vertex>();
265 khahn 1.13 mithep::Array<mithep::PFCandidate> *pfArr = new mithep::Array<mithep::PFCandidate>();
266 khahn 1.16 mithep::Array<mithep::PileupInfo> *puArr = new mithep::Array<mithep::PileupInfo>();
267     mithep::Array<mithep::PileupEnergyDensity> *puDArr = new mithep::Array<mithep::PileupEnergyDensity>();
268 khahn 1.13 mithep::Array<mithep::Track> *trkArr = new mithep::Array<mithep::Track>();
269 khahn 1.23 mithep::Array<mithep::MCParticle> *mcArr = new mithep::Array<mithep::MCParticle>();
270 khahn 1.28 mithep::MCEventInfo *mcEvtInfo = new mithep::MCEventInfo();
271 khahn 1.13 mithep::TriggerMask *trigMask = new mithep::TriggerMask();
272     mithep::TriggerTable *hltTable = new mithep::TriggerTable();
273     vector<string> *hltTableStrings = new vector<string>();
274    
275     TString fElectronName(Names::gkElectronBrn);
276     TString fMuonName(Names::gkMuonBrn);
277     TString fInfoName(Names::gkEvtHeaderBrn);
278     TString fPrimVtxName(Names::gkPVBrn);
279     TString fPFCandidateName(Names::gkPFCandidatesBrn);
280 khahn 1.16 TString fPileupInfoName(Names::gkPileupInfoBrn);
281 khahn 1.13 TString fPileupEnergyDensityName(Names::gkPileupEnergyDensityBrn);
282     TString fTrackName(Names::gkTrackBrn);
283 khahn 1.23 TString fMCParticleName(Names::gkMCPartBrn);
284 khahn 1.28 TString fMCEvtInfoName(Names::gkMCEvtInfoBrn);
285 khahn 1.13 TString fTriggerMaskName(Names::gkHltBitBrn);
286     TString fTriggerTableName(Names::gkHltTableBrn);
287 khahn 1.1
288 khahn 1.13 chain->SetBranchAddress(fInfoName, &info);
289     chain->SetBranchAddress(fElectronName, &electronArr);
290     chain->SetBranchAddress(fMuonName, &muonArr);
291     chain->SetBranchAddress(fPrimVtxName, &vtxArr);
292     chain->SetBranchAddress(fPFCandidateName, &pfArr);
293 khahn 1.23 if( ctrl.mc ) {
294     chain->SetBranchAddress(fPileupInfoName, &puArr);
295     chain->SetBranchAddress(fMCParticleName, &mcArr);
296 khahn 1.28 chain->SetBranchAddress(fMCEvtInfoName, &mcEvtInfo);
297 khahn 1.23 }
298 khahn 1.16 chain->SetBranchAddress(fPileupEnergyDensityName, &puDArr);
299 khahn 1.13 chain->SetBranchAddress(fTrackName, &trkArr);
300     chain->SetBranchAddress(fTriggerMaskName, &trigMask);
301     cout << "hlttable: " << fTriggerTableName << endl;
302     hltchain->SetBranchAddress(fTriggerTableName, &hltTableStrings);
303 khahn 1.1
304 khahn 1.13 mithep::Vertex vtx; // best primary vertex in the event
305 khahn 1.1
306 khahn 1.13 // ginfo = NULL;
307     // if( ctrl.mc ) { chain->SetBranchAddress("Gen", &ginfo);}
308 khahn 1.1
309 khahn 1.9 int count=0, pass=0;
310 khahn 1.13 float passcorr=0., passcorr_errup=0., passcorr_errdown=0.;
311 khahn 1.11 float denom[3]={0.,0.,0.};
312     float numer[3]={0.,0.,0.};
313     float numercorr[3]={0.,0.,0.};
314    
315 khahn 1.13 // only 1 HLT table / file ???
316     hltchain->GetEntry(0);
317     cerr << "here... " << endl;
318    
319     int imax = chain->GetEntries();
320 khahn 1.11 cout << "nEntries: " << imax << endl;
321    
322    
323 khahn 1.16 //
324     // Loop !!!!!!!!!
325     //--------------------------------------------------------------------------------------------------------------
326 khahn 1.13 for(UInt_t ientry=0; ientry<imax; ientry++)
327     {
328     chain->GetEntry(ientry);
329     if(!(ientry%1000)) cerr << "entry: " << ientry << endl;
330    
331 khahn 1.28 #ifdef HACKED_RHOS
332 khahn 1.22 float rho = evtrhoMap[info->EvtNum()];
333 khahn 1.28 #endif
334     string fname = string(chain->GetFile()->GetEndpointUrl()->GetFile());
335     setEra( fname, ctrl );
336    
337 khahn 1.19
338 khahn 1.22 //
339     // pfNoPU
340     //
341     //mithep::Array<PFCandidate> pfNoPileUp;
342     PFnoPUflag.clear();
343     int pfnopu_size = makePFnoPUArray( pfArr, PFnoPUflag, vtxArr );
344     assert(pfnopu_size == pfArr->GetEntries());
345 khahn 1.13
346 khahn 1.16 //
347     // data/MC
348     //
349 khahn 1.13 if(ctrl.mc) {
350 khahn 1.16 //
351 khahn 1.28 // gen info
352     //
353 khahn 1.31 if( ctrl.fillGen )
354 khahn 1.28 fillGenInfo( mcArr, mcEvtInfo, geninfo, ESampleType::kHZZ, ctrl);
355    
356     //
357 khahn 1.16 // xsec & PU weights
358     //
359 khahn 1.31 weights.w = getWeight(xstab,entrymap,chain)/ctrl.totalMC;
360 khahn 1.23 cout << "xsec weight: " << weights.w << endl;
361 khahn 1.16 int npu = -1;
362     for(int i=0;i<puArr->GetEntries();i++) {
363     if(puArr->At(i)->GetBunchCrossing() == 0)
364     npu = puArr->At(i)->GetPU_NumInteractions();
365     }
366     assert(npu>=0);
367     evtinfo.npu;
368     weights.npuw = getPUWeight(evtinfo.npu);
369 khahn 1.28
370     //
371     // pt reweighting for Higgs < 140, F11
372     //
373     if( ctrl.fillGen ) {
374     if( (fname.find("f11-h115zz4l") != string::npos) ||
375     (fname.find("f11-h120zz4l") != string::npos) ||
376     (fname.find("f11-h130zz4l") != string::npos) ) {
377     weights.w *= h_wf11_hpt->GetBinContent(h_wf11_hpt->FindBin(geninfo.pt_zz));
378     }
379     }
380    
381 khahn 1.20 //
382     // trigger
383     //
384     if( string(chain->GetFile()->GetEndpointUrl()->GetFile()) != currentFile ) {
385     currentFile = string(chain->GetFile()->GetEndpointUrl()->GetFile());
386     hltchain->SetBranchAddress(fTriggerTableName, &hltTableStrings);
387     hltchain->GetEntry(0);
388     hltTable->Clear();
389 anlevin 1.32 fillTriggerNames(hltTable, hltTableStrings );
390 khahn 1.20 }
391     if( ctrl.debug ) cout << "file is : " << currentFile << endl;
392 khahn 1.34 fillTriggerBits( hltTable, trigMask, triggerBits );
393 khahn 1.21 passes_HLT_MC = true; // passed to apply as extern global, just for sync
394 anlevin 1.27 if( !passHLTMC(triggerBits, info->RunNum(), info->EvtNum(), k2012_MC ) ) {
395 khahn 1.21 passes_HLT_MC = false;
396 khahn 1.20 }
397 khahn 1.13 } else {
398 khahn 1.16 //
399     // JSON
400     //
401 khahn 1.18 /*
402 khahn 1.16 RunLumiRangeMap::RunLumiPairType rl(info->RunNum(), info->LumiSec());
403     if( !(rlrm.HasRunLumi(rl)) ) {
404     if( ctrl.debug ) cout << "\tfails JSON" << endl;
405     continue;
406     }
407 khahn 1.18 */
408 khahn 1.16 //
409     // trigger
410     //
411 khahn 1.13 if( string(chain->GetFile()->GetEndpointUrl()->GetFile()) != currentFile ) {
412     currentFile = string(chain->GetFile()->GetEndpointUrl()->GetFile());
413     hltchain->SetBranchAddress(fTriggerTableName, &hltTableStrings);
414     hltchain->GetEntry(0);
415     hltTable->Clear();
416 anlevin 1.32 fillTriggerNames(hltTable, hltTableStrings );
417 khahn 1.13 }
418     if( ctrl.debug ) cout << "file is : " << currentFile << endl;
419 khahn 1.21 /*
420 khahn 1.34 fillTriggerBits( hltTable, trigMask, triggerBits );
421 khahn 1.13 if( !passHLT(triggerBits, info->RunNum(), info->EvtNum() ) ) {
422     if( ctrl.debug ) cout << "\tfails trigger ... " << endl;
423     continue;
424     }
425 khahn 1.21 */
426 khahn 1.13 }
427 khahn 1.16
428 khahn 1.13 //
429     // lepton/kinematic selection ...
430     //
431 khahn 1.16 failingLeptons.clear();
432     passingLeptons.clear();
433 khahn 1.13 EventData ret4l =
434 khahn 1.21
435 khahn 1.23
436     // reference
437 khahn 1.29 apply_HZZ4L_reference_selection(ctrl, info,
438 khahn 1.33 vtxArr,
439     pfArr,
440 khahn 1.24 #ifdef HACKED_RHOS
441 khahn 1.33 rho,
442 khahn 1.24 #else
443 khahn 1.33 puDArr,
444 khahn 1.24 #endif
445 khahn 1.33 electronArr,
446     &electronReferencePreSelection,
447     &electronReferenceIDMVASelectionV1,
448     &electronReferenceIsoSelection,
449     muonArr,
450     &muonReferencePreSelection,
451     &muonIDPFSelection,
452     &muonReferenceIsoSelection);
453    
454 khahn 1.13 if( ctrl.debug ) cout << endl;
455 khahn 1.14
456 khahn 1.19
457     int Z1type=0, Z2type=0;
458     if( ret4l.status.selectionBits.to_ulong() >= PASS_ZCANDIDATE ) {
459     if( abs(ret4l.Z1leptons[0].type) == 11 ) Z1type = 11;
460     if( abs(ret4l.Z1leptons[0].type) == 13 ) Z1type = 13;
461     }
462     if( ret4l.status.selectionBits.to_ulong() >= PASS_ZZCANDIDATE ) {
463     if( abs(ret4l.Z2leptons[0].type) == 11 ) Z2type = 11;
464     if( abs(ret4l.Z2leptons[0].type) == 13 ) Z2type = 13;
465     }
466    
467 khahn 1.22 #ifdef SYNC
468 khahn 1.19 cout << "bits: " << ret4l.status.selectionBits << "\t"
469     << "Z1: " << Z1type << "\t"
470     << "Z2: " << Z2type << endl;
471     cout << endl;
472 khahn 1.22 #endif
473 khahn 1.13
474     if( ret4l.status.pass() ) {
475     fillAngles(ret4l,angles);
476     fillKinematics(ret4l,kinematics);
477 khahn 1.31 fillMassErrors(ret4l,muonArr,electronArr,kinematics);
478 khahn 1.13 fillEventInfo(info,evtinfo);
479 khahn 1.16 if( ctrl.mc) {
480     setEffiencyWeights(ret4l, weights);
481     if(ctrl.debug)
482     cout << "w: " << weights.w << "\t"
483     << "won: " << weights.won << "\t"
484     << "woff: " << weights.woff << endl;
485     }
486 khahn 1.13
487 khahn 1.28
488 khahn 1.13 nt.Fill();
489    
490     cerr << "PASS:: "
491     << "\trun: " << evtinfo.run
492     << "\tevt: " << evtinfo.evt
493     << "\tlumi: " << evtinfo.lumi
494     << "\tcostheta1: " << angles.costheta1
495     << "\tcostheta2: " << angles.costheta2
496     << "\tcostheta*: " << angles.costhetastar
497     << "\tbdt: " << angles.bdt
498     << endl;
499     pass++;
500     // if( pass > 3 ) break;
501 khahn 1.8
502 khahn 1.33 } else if( ret4l.status.selectionBits.test(PASS_GOODZ1) &&
503     ret4l.status.selectionBits.to_ulong() < PASS_4L) { // save for fakes ...
504 khahn 1.21 // if(ctrl.debug) {
505 khahn 1.18 cout << "failingLeptons : " << failingLeptons.size() << endl;
506     for( int f=0; f<failingLeptons.size(); f++ ) {
507     SimpleLepton l = failingLeptons[f];
508     cout << "f: " << f << "\t"
509     << "type: " << l.type << "\t"
510     << "pT: " << l.vec.Pt() << "\t"
511     << "eta: " << l.vec.Eta()
512     << endl;
513 khahn 1.21 // }
514 khahn 1.16 }
515     foTree.Fill();
516 khahn 1.15 }
517 khahn 1.13 }
518 khahn 1.1
519 khahn 1.16
520     foTree.getFile()->cd();
521     foTree.getTree()->Write();
522 khahn 1.13 nt.WriteClose();
523 khahn 1.19
524     cout << endl;
525     cout << endl;
526     for( int i=0; i<cutvec.size(); i++ ) {
527     cout << "cut: " << i << "\t"
528     << "pass: " << cutvec[i];
529    
530 khahn 1.21 if( i>PASS_TRIGGER&&i<=PASS_GOODZ1 )
531 khahn 1.19 cout << "\t2e: " << zcutvec[0][i]
532     << "\t2m: " << zcutvec[1][i];
533    
534 khahn 1.21 if( i>PASS_ZCANDIDATE )
535 khahn 1.19 cout << "\t4e: " << zzcutvec[0][i]
536     << "\t4m: " << zzcutvec[1][i]
537     << "\t2e2m: " << zzcutvec[2][i];
538    
539     cout << "\t" << cutstrs[i] << endl;
540    
541     cout << endl;
542     }
543    
544 khahn 1.13 }
545 dkralph 1.10
546 khahn 1.16
547    
548     //----------------------------------------------------------------------------
549     void setEffiencyWeights(EventData & evtdat, WeightStruct & weights )
550     //----------------------------------------------------------------------------
551     {
552     vector<SimpleLepton> lepvec = evtdat.Z1leptons;
553     lepvec.insert(lepvec.end(), evtdat.Z2leptons.begin(), evtdat.Z2leptons.end());
554     double w_offline=-1, werr_offline=0;
555     double w_online=-1, werr_online=0;
556    
557     vector< pair <double,double> > wlegs; // pair here is eff & err
558     vector< pair <float,float> > mvec; // pair here is eta & pt
559    
560     // vector< pair <float,float> > evec; // pair here is eta & pt
561     // now deal with medium vs loose
562     vector< pair< bool, pair <float,float> > > evec; // pair here is eta & pt
563    
564     for( int k=0; k<lepvec.size(); k++ ) {
565     if( abs(lepvec[k].type) == 13 ) {
566 khahn 1.17 mvec.push_back( std::pair<float,float> (fabs(lepvec[k].vec.Eta()), lepvec[k].vec.Pt()) );
567     wlegs.push_back( muonPerLegOfflineEfficiencyWeight( fabs(lepvec[k].vec.Eta()),
568     lepvec[k].vec.Pt() ) );
569 khahn 1.16 } else {
570    
571     // now deal with medium vs loose
572     // evec.push_back( std::pair<float,float> (fabs(lepvec[k].vec.Eta()), lepvec[k].vec.Pt()) );
573    
574 khahn 1.17 std::pair<float,float> tmppair(fabs(lepvec[k].vec.Eta()), lepvec[k].vec.Pt());
575 khahn 1.16 evec.push_back( std::pair<bool, std::pair<float,float> > (lepvec[k].isTight, tmppair) );
576    
577 khahn 1.17 // wlegs.push_back( elePerLegOfflineEfficiencyWeight( fabs(lepvec[k].vec.Eta()),
578     // lepvec[k].vec.Pt() ) );
579 khahn 1.16
580 khahn 1.17 wlegs.push_back( elePerLegOfflineEfficiencyWeight( fabs(lepvec[k].vec.Eta()),
581     lepvec[k].vec.Pt(),
582 khahn 1.16 lepvec[k].isTight ) );
583    
584     }
585     }
586    
587     pair<double,double> offpair = getOfflineEfficiencyWeight( wlegs );
588     weights.woff = offpair.first;
589     weights.werroff = offpair.second;
590    
591     pair<double,double> onpair = getOnlineEfficiencyWeight( mvec, evec );
592     weights.won = onpair.first;
593     weights.werron = onpair.second;
594     }
595    
596    
597     //----------------------------------------------------------------------------
598     void initRunLumiRangeMap()
599     //----------------------------------------------------------------------------
600     {
601     /*
602     rlrm.AddJSONFile(std::string("./data/Cert_136033-149442_7TeV_Apr21ReReco_Collisions10_JSON.txt"));
603     // rlrm.AddJSONFile(std::string("./data/Cert_160404-173244_7TeV_PromptReco_Collisions11_JSON_v2.txt"));
604     rlrm.AddJSONFile(std::string("./data/Cert_160404-178078_7TeV_PromptReco_Collisions11_JSON.txt"));
605     rlrm.AddJSONFile(std::string("./data/Cert_160404-163869_7TeV_May10ReReco_Collisions11_JSON_v3.txt"));
606     rlrm.AddJSONFile(std::string("./data/Cert_170249-172619_7TeV_ReReco5Aug_Collisions11_JSON.txt"));
607     // r11b
608     rlrm.AddJSONFile(std::string("./data/Cert_160404-180252_7TeV_PromptReco_Collisions11_JSON.txt"));
609     */
610     };
611    
612    
613     //----------------------------------------------------------------------------
614     void initPUWeights()
615     //----------------------------------------------------------------------------
616     {
617     TFile * puf = new TFile("data/PileupReweighting.Summer11DYmm_To_Full2011.root");
618     hpu = (TH1D*)(puf->Get("puWeights"));
619     }
620    
621     //----------------------------------------------------------------------------
622     double getPUWeight(unsigned npu)
623     //----------------------------------------------------------------------------
624     {
625     return hpu->GetBinContent(hpu->FindBin(npu));
626     }
627 khahn 1.19
628     //----------------------------------------------------------------------------
629     void initEvtRhoMap( map<unsigned, float> & evtrhoMap )
630     //----------------------------------------------------------------------------
631     {
632     unsigned evt;
633     float rho;
634    
635 khahn 1.22 cout << "initialzing EvtRhoMap ";
636     //FILE * f = fopen("evtrho.dat", "r");
637     // FILE * f = fopen("allrho.cali.uniq.dat", "r");
638 anlevin 1.25 FILE * f = fopen("/data/blue/khahn/CMSSW_4_4_2/src/MitHzz4l/allrhoAA.cali.uniq.dat", "r");
639 khahn 1.22 int lcount=0;
640 khahn 1.19 while( fscanf( f, "%u:%f", &evt, &rho ) ) {
641 khahn 1.22 if( feof(f) ) break;
642 khahn 1.19 evtrhoMap[evt] = rho;
643 khahn 1.22 lcount++;
644     if( !(lcount%1000) ) cout << "."; flush(cout);
645 khahn 1.19 }
646 khahn 1.22 cout << " done" << endl;
647     }
648    
649     //----------------------------------------------------------------------------
650     unsigned makePFnoPUArray(mithep::Array<PFCandidate> * fPFCandidates,
651 khahn 1.28 vector<bool> &pfNoPileUpflag,
652     const mithep::Array<mithep::Vertex> * vtxArr )
653 khahn 1.22 //----------------------------------------------------------------------------
654     {
655     for(UInt_t i = 0; i < fPFCandidates->GetEntries(); i++) {
656     const PFCandidate *pf = fPFCandidates->At(i);
657     assert(pf);
658    
659     if(pf->PFType() == PFCandidate::eHadron) {
660     if(pf->HasTrackerTrk() &&
661     vtxArr->At(0)->HasTrack(pf->TrackerTrk()) &&
662     vtxArr->At(0)->TrackWeight(pf->TrackerTrk()) > 0) {
663    
664     pfNoPileUpflag.push_back(1);
665    
666     } else {
667    
668     Bool_t vertexFound = kFALSE;
669     const Vertex *closestVtx = 0;
670     Double_t dzmin = 10000;
671    
672     // loop over vertices
673     for(UInt_t j = 0; j < vtxArr->GetEntries(); j++) {
674     const Vertex *vtx = vtxArr->At(j);
675     assert(vtx);
676    
677     if(pf->HasTrackerTrk() &&
678     vtx->HasTrack(pf->TrackerTrk()) &&
679     vtx->TrackWeight(pf->TrackerTrk()) > 0) {
680     vertexFound = kTRUE;
681     closestVtx = vtx;
682     break;
683     }
684     Double_t dz = fabs(pf->SourceVertex().Z() - vtx->Z());
685     if(dz < dzmin) {
686     closestVtx = vtx;
687     dzmin = dz;
688     }
689     }
690    
691     // if(fCheckClosestZVertex) {
692     if(1) {
693     // Fallback: if track is not associated with any vertex,
694     // associate it with the vertex closest in z
695     if(vertexFound || closestVtx != vtxArr->At(0)) {
696     // pfPileUp->Add(pf);
697     pfNoPileUpflag.push_back(0);
698     } else {
699     pfNoPileUpflag.push_back(1);
700     }
701     } else {
702     if(vertexFound && closestVtx != vtxArr->At(0)) {
703     // pfPileUp->Add(pf);
704     pfNoPileUpflag.push_back(0);
705     } else {
706     // PFCandidate * pfNoPileUp->AddNew(); // Ridiculous but that's how it is
707     pfNoPileUpflag.push_back(1);
708     }
709     }
710     } //hadron & trk stuff
711     } else { // hadron
712     // PFCandidate * ptr = pfNoPileUp->AddNew();
713     pfNoPileUpflag.push_back(1);
714     }
715     } // Loop over PF candidates
716    
717     return pfNoPileUpflag.size();
718 khahn 1.19 }
719 khahn 1.22
720 khahn 1.28 void setEra(string fname, ControlFlags &ctrl)
721     {
722     if( ctrl.mc && (fname.find("f11-") != string::npos) ) ctrl.era=2011 ;
723     if( ctrl.mc && (fname.find("s12-") != string::npos) ) ctrl.era=2012 ;
724     if( !ctrl.mc && (fname.find("r11-") != string::npos) ) ctrl.era=2011 ;
725     if( !ctrl.mc && (fname.find("r12-") != string::npos) ) ctrl.era=2012 ;
726     }