ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitHzz4l/Selection/src/applySelection.cc
Revision: 1.35
Committed: Tue Jun 5 19:35:48 2012 UTC (12 years, 11 months ago) by khahn
Content type: text/plain
Branch: MAIN
CVS Tags: synced_FSR_2, synced_FSR
Changes since 1.34: +5 -8 lines
Log Message:
final sync for FSR

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.35 if(ctrl.fillGen ) 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.35 //if( info->EvtNum() != 217998 ) continue;
332     // if( info->EvtNum() != 238893 ) continue;
333     // if( info->EvtNum() != 289066 ) continue;
334    
335 khahn 1.28 string fname = string(chain->GetFile()->GetEndpointUrl()->GetFile());
336     setEra( fname, ctrl );
337    
338 khahn 1.19
339 khahn 1.22 //
340     // pfNoPU
341     //
342     //mithep::Array<PFCandidate> pfNoPileUp;
343     PFnoPUflag.clear();
344     int pfnopu_size = makePFnoPUArray( pfArr, PFnoPUflag, vtxArr );
345     assert(pfnopu_size == pfArr->GetEntries());
346 khahn 1.13
347 khahn 1.16 //
348     // data/MC
349     //
350 khahn 1.13 if(ctrl.mc) {
351 khahn 1.16 //
352 khahn 1.28 // gen info
353     //
354 khahn 1.31 if( ctrl.fillGen )
355 khahn 1.28 fillGenInfo( mcArr, mcEvtInfo, geninfo, ESampleType::kHZZ, ctrl);
356    
357     //
358 khahn 1.16 // xsec & PU weights
359     //
360 khahn 1.31 weights.w = getWeight(xstab,entrymap,chain)/ctrl.totalMC;
361 khahn 1.23 cout << "xsec weight: " << weights.w << endl;
362 khahn 1.16 int npu = -1;
363     for(int i=0;i<puArr->GetEntries();i++) {
364     if(puArr->At(i)->GetBunchCrossing() == 0)
365     npu = puArr->At(i)->GetPU_NumInteractions();
366     }
367     assert(npu>=0);
368     evtinfo.npu;
369     weights.npuw = getPUWeight(evtinfo.npu);
370 khahn 1.28
371     //
372     // pt reweighting for Higgs < 140, F11
373     //
374     if( ctrl.fillGen ) {
375     if( (fname.find("f11-h115zz4l") != string::npos) ||
376     (fname.find("f11-h120zz4l") != string::npos) ||
377     (fname.find("f11-h130zz4l") != string::npos) ) {
378     weights.w *= h_wf11_hpt->GetBinContent(h_wf11_hpt->FindBin(geninfo.pt_zz));
379     }
380     }
381    
382 khahn 1.20 //
383     // trigger
384     //
385     if( string(chain->GetFile()->GetEndpointUrl()->GetFile()) != currentFile ) {
386     currentFile = string(chain->GetFile()->GetEndpointUrl()->GetFile());
387     hltchain->SetBranchAddress(fTriggerTableName, &hltTableStrings);
388     hltchain->GetEntry(0);
389     hltTable->Clear();
390 anlevin 1.32 fillTriggerNames(hltTable, hltTableStrings );
391 khahn 1.20 }
392     if( ctrl.debug ) cout << "file is : " << currentFile << endl;
393 khahn 1.34 fillTriggerBits( hltTable, trigMask, triggerBits );
394 khahn 1.21 passes_HLT_MC = true; // passed to apply as extern global, just for sync
395 anlevin 1.27 if( !passHLTMC(triggerBits, info->RunNum(), info->EvtNum(), k2012_MC ) ) {
396 khahn 1.21 passes_HLT_MC = false;
397 khahn 1.20 }
398 khahn 1.13 } else {
399 khahn 1.16 //
400     // JSON
401     //
402 khahn 1.18 /*
403 khahn 1.16 RunLumiRangeMap::RunLumiPairType rl(info->RunNum(), info->LumiSec());
404     if( !(rlrm.HasRunLumi(rl)) ) {
405     if( ctrl.debug ) cout << "\tfails JSON" << endl;
406     continue;
407     }
408 khahn 1.18 */
409 khahn 1.16 //
410     // trigger
411     //
412 khahn 1.13 if( string(chain->GetFile()->GetEndpointUrl()->GetFile()) != currentFile ) {
413     currentFile = string(chain->GetFile()->GetEndpointUrl()->GetFile());
414     hltchain->SetBranchAddress(fTriggerTableName, &hltTableStrings);
415     hltchain->GetEntry(0);
416     hltTable->Clear();
417 anlevin 1.32 fillTriggerNames(hltTable, hltTableStrings );
418 khahn 1.13 }
419     if( ctrl.debug ) cout << "file is : " << currentFile << endl;
420 khahn 1.21 /*
421 khahn 1.34 fillTriggerBits( hltTable, trigMask, triggerBits );
422 khahn 1.13 if( !passHLT(triggerBits, info->RunNum(), info->EvtNum() ) ) {
423     if( ctrl.debug ) cout << "\tfails trigger ... " << endl;
424     continue;
425     }
426 khahn 1.21 */
427 khahn 1.13 }
428 khahn 1.16
429 khahn 1.13 //
430     // lepton/kinematic selection ...
431     //
432 khahn 1.16 failingLeptons.clear();
433     passingLeptons.clear();
434 khahn 1.13 EventData ret4l =
435 khahn 1.21
436 khahn 1.23
437     // reference
438 khahn 1.29 apply_HZZ4L_reference_selection(ctrl, info,
439 khahn 1.33 vtxArr,
440     pfArr,
441     puDArr,
442     electronArr,
443     &electronReferencePreSelection,
444     &electronReferenceIDMVASelectionV1,
445     &electronReferenceIsoSelection,
446     muonArr,
447     &muonReferencePreSelection,
448     &muonIDPFSelection,
449     &muonReferenceIsoSelection);
450    
451 khahn 1.13 if( ctrl.debug ) cout << endl;
452 khahn 1.14
453 khahn 1.19
454     int Z1type=0, Z2type=0;
455     if( ret4l.status.selectionBits.to_ulong() >= PASS_ZCANDIDATE ) {
456     if( abs(ret4l.Z1leptons[0].type) == 11 ) Z1type = 11;
457     if( abs(ret4l.Z1leptons[0].type) == 13 ) Z1type = 13;
458     }
459     if( ret4l.status.selectionBits.to_ulong() >= PASS_ZZCANDIDATE ) {
460     if( abs(ret4l.Z2leptons[0].type) == 11 ) Z2type = 11;
461     if( abs(ret4l.Z2leptons[0].type) == 13 ) Z2type = 13;
462     }
463    
464 khahn 1.22 #ifdef SYNC
465 khahn 1.19 cout << "bits: " << ret4l.status.selectionBits << "\t"
466     << "Z1: " << Z1type << "\t"
467     << "Z2: " << Z2type << endl;
468     cout << endl;
469 khahn 1.22 #endif
470 khahn 1.13
471     if( ret4l.status.pass() ) {
472     fillAngles(ret4l,angles);
473     fillKinematics(ret4l,kinematics);
474 khahn 1.31 fillMassErrors(ret4l,muonArr,electronArr,kinematics);
475 khahn 1.13 fillEventInfo(info,evtinfo);
476 khahn 1.16 if( ctrl.mc) {
477     setEffiencyWeights(ret4l, weights);
478     if(ctrl.debug)
479     cout << "w: " << weights.w << "\t"
480     << "won: " << weights.won << "\t"
481     << "woff: " << weights.woff << endl;
482     }
483 khahn 1.13
484 khahn 1.28
485 khahn 1.13 nt.Fill();
486    
487     cerr << "PASS:: "
488     << "\trun: " << evtinfo.run
489     << "\tevt: " << evtinfo.evt
490     << "\tlumi: " << evtinfo.lumi
491     << "\tcostheta1: " << angles.costheta1
492     << "\tcostheta2: " << angles.costheta2
493     << "\tcostheta*: " << angles.costhetastar
494     << "\tbdt: " << angles.bdt
495     << endl;
496     pass++;
497     // if( pass > 3 ) break;
498 khahn 1.8
499 khahn 1.33 } else if( ret4l.status.selectionBits.test(PASS_GOODZ1) &&
500     ret4l.status.selectionBits.to_ulong() < PASS_4L) { // save for fakes ...
501 khahn 1.21 // if(ctrl.debug) {
502 khahn 1.18 cout << "failingLeptons : " << failingLeptons.size() << endl;
503     for( int f=0; f<failingLeptons.size(); f++ ) {
504     SimpleLepton l = failingLeptons[f];
505     cout << "f: " << f << "\t"
506     << "type: " << l.type << "\t"
507     << "pT: " << l.vec.Pt() << "\t"
508     << "eta: " << l.vec.Eta()
509     << endl;
510 khahn 1.21 // }
511 khahn 1.16 }
512     foTree.Fill();
513 khahn 1.15 }
514 khahn 1.13 }
515 khahn 1.1
516 khahn 1.16
517     foTree.getFile()->cd();
518     foTree.getTree()->Write();
519 khahn 1.13 nt.WriteClose();
520 khahn 1.19
521     cout << endl;
522     cout << endl;
523     for( int i=0; i<cutvec.size(); i++ ) {
524     cout << "cut: " << i << "\t"
525     << "pass: " << cutvec[i];
526    
527 khahn 1.21 if( i>PASS_TRIGGER&&i<=PASS_GOODZ1 )
528 khahn 1.19 cout << "\t2e: " << zcutvec[0][i]
529     << "\t2m: " << zcutvec[1][i];
530    
531 khahn 1.21 if( i>PASS_ZCANDIDATE )
532 khahn 1.19 cout << "\t4e: " << zzcutvec[0][i]
533     << "\t4m: " << zzcutvec[1][i]
534     << "\t2e2m: " << zzcutvec[2][i];
535    
536     cout << "\t" << cutstrs[i] << endl;
537    
538     cout << endl;
539     }
540    
541 khahn 1.13 }
542 dkralph 1.10
543 khahn 1.16
544    
545     //----------------------------------------------------------------------------
546     void setEffiencyWeights(EventData & evtdat, WeightStruct & weights )
547     //----------------------------------------------------------------------------
548     {
549     vector<SimpleLepton> lepvec = evtdat.Z1leptons;
550     lepvec.insert(lepvec.end(), evtdat.Z2leptons.begin(), evtdat.Z2leptons.end());
551     double w_offline=-1, werr_offline=0;
552     double w_online=-1, werr_online=0;
553    
554     vector< pair <double,double> > wlegs; // pair here is eff & err
555     vector< pair <float,float> > mvec; // pair here is eta & pt
556    
557     // vector< pair <float,float> > evec; // pair here is eta & pt
558     // now deal with medium vs loose
559     vector< pair< bool, pair <float,float> > > evec; // pair here is eta & pt
560    
561     for( int k=0; k<lepvec.size(); k++ ) {
562     if( abs(lepvec[k].type) == 13 ) {
563 khahn 1.17 mvec.push_back( std::pair<float,float> (fabs(lepvec[k].vec.Eta()), lepvec[k].vec.Pt()) );
564     wlegs.push_back( muonPerLegOfflineEfficiencyWeight( fabs(lepvec[k].vec.Eta()),
565     lepvec[k].vec.Pt() ) );
566 khahn 1.16 } else {
567    
568     // now deal with medium vs loose
569     // evec.push_back( std::pair<float,float> (fabs(lepvec[k].vec.Eta()), lepvec[k].vec.Pt()) );
570    
571 khahn 1.17 std::pair<float,float> tmppair(fabs(lepvec[k].vec.Eta()), lepvec[k].vec.Pt());
572 khahn 1.16 evec.push_back( std::pair<bool, std::pair<float,float> > (lepvec[k].isTight, tmppair) );
573    
574 khahn 1.17 // wlegs.push_back( elePerLegOfflineEfficiencyWeight( fabs(lepvec[k].vec.Eta()),
575     // lepvec[k].vec.Pt() ) );
576 khahn 1.16
577 khahn 1.17 wlegs.push_back( elePerLegOfflineEfficiencyWeight( fabs(lepvec[k].vec.Eta()),
578     lepvec[k].vec.Pt(),
579 khahn 1.16 lepvec[k].isTight ) );
580    
581     }
582     }
583    
584     pair<double,double> offpair = getOfflineEfficiencyWeight( wlegs );
585     weights.woff = offpair.first;
586     weights.werroff = offpair.second;
587    
588     pair<double,double> onpair = getOnlineEfficiencyWeight( mvec, evec );
589     weights.won = onpair.first;
590     weights.werron = onpair.second;
591     }
592    
593    
594     //----------------------------------------------------------------------------
595     void initRunLumiRangeMap()
596     //----------------------------------------------------------------------------
597     {
598     /*
599     rlrm.AddJSONFile(std::string("./data/Cert_136033-149442_7TeV_Apr21ReReco_Collisions10_JSON.txt"));
600     // rlrm.AddJSONFile(std::string("./data/Cert_160404-173244_7TeV_PromptReco_Collisions11_JSON_v2.txt"));
601     rlrm.AddJSONFile(std::string("./data/Cert_160404-178078_7TeV_PromptReco_Collisions11_JSON.txt"));
602     rlrm.AddJSONFile(std::string("./data/Cert_160404-163869_7TeV_May10ReReco_Collisions11_JSON_v3.txt"));
603     rlrm.AddJSONFile(std::string("./data/Cert_170249-172619_7TeV_ReReco5Aug_Collisions11_JSON.txt"));
604     // r11b
605     rlrm.AddJSONFile(std::string("./data/Cert_160404-180252_7TeV_PromptReco_Collisions11_JSON.txt"));
606     */
607     };
608    
609    
610     //----------------------------------------------------------------------------
611     void initPUWeights()
612     //----------------------------------------------------------------------------
613     {
614     TFile * puf = new TFile("data/PileupReweighting.Summer11DYmm_To_Full2011.root");
615     hpu = (TH1D*)(puf->Get("puWeights"));
616     }
617    
618     //----------------------------------------------------------------------------
619     double getPUWeight(unsigned npu)
620     //----------------------------------------------------------------------------
621     {
622     return hpu->GetBinContent(hpu->FindBin(npu));
623     }
624 khahn 1.19
625     //----------------------------------------------------------------------------
626     void initEvtRhoMap( map<unsigned, float> & evtrhoMap )
627     //----------------------------------------------------------------------------
628     {
629     unsigned evt;
630     float rho;
631    
632 khahn 1.22 cout << "initialzing EvtRhoMap ";
633     //FILE * f = fopen("evtrho.dat", "r");
634     // FILE * f = fopen("allrho.cali.uniq.dat", "r");
635 anlevin 1.25 FILE * f = fopen("/data/blue/khahn/CMSSW_4_4_2/src/MitHzz4l/allrhoAA.cali.uniq.dat", "r");
636 khahn 1.22 int lcount=0;
637 khahn 1.19 while( fscanf( f, "%u:%f", &evt, &rho ) ) {
638 khahn 1.22 if( feof(f) ) break;
639 khahn 1.19 evtrhoMap[evt] = rho;
640 khahn 1.22 lcount++;
641     if( !(lcount%1000) ) cout << "."; flush(cout);
642 khahn 1.19 }
643 khahn 1.22 cout << " done" << endl;
644     }
645    
646     //----------------------------------------------------------------------------
647     unsigned makePFnoPUArray(mithep::Array<PFCandidate> * fPFCandidates,
648 khahn 1.28 vector<bool> &pfNoPileUpflag,
649     const mithep::Array<mithep::Vertex> * vtxArr )
650 khahn 1.22 //----------------------------------------------------------------------------
651     {
652     for(UInt_t i = 0; i < fPFCandidates->GetEntries(); i++) {
653     const PFCandidate *pf = fPFCandidates->At(i);
654     assert(pf);
655    
656     if(pf->PFType() == PFCandidate::eHadron) {
657     if(pf->HasTrackerTrk() &&
658     vtxArr->At(0)->HasTrack(pf->TrackerTrk()) &&
659     vtxArr->At(0)->TrackWeight(pf->TrackerTrk()) > 0) {
660    
661     pfNoPileUpflag.push_back(1);
662    
663     } else {
664    
665     Bool_t vertexFound = kFALSE;
666     const Vertex *closestVtx = 0;
667     Double_t dzmin = 10000;
668    
669     // loop over vertices
670     for(UInt_t j = 0; j < vtxArr->GetEntries(); j++) {
671     const Vertex *vtx = vtxArr->At(j);
672     assert(vtx);
673    
674     if(pf->HasTrackerTrk() &&
675     vtx->HasTrack(pf->TrackerTrk()) &&
676     vtx->TrackWeight(pf->TrackerTrk()) > 0) {
677     vertexFound = kTRUE;
678     closestVtx = vtx;
679     break;
680     }
681     Double_t dz = fabs(pf->SourceVertex().Z() - vtx->Z());
682     if(dz < dzmin) {
683     closestVtx = vtx;
684     dzmin = dz;
685     }
686     }
687    
688     // if(fCheckClosestZVertex) {
689     if(1) {
690     // Fallback: if track is not associated with any vertex,
691     // associate it with the vertex closest in z
692     if(vertexFound || closestVtx != vtxArr->At(0)) {
693     // pfPileUp->Add(pf);
694     pfNoPileUpflag.push_back(0);
695     } else {
696     pfNoPileUpflag.push_back(1);
697     }
698     } else {
699     if(vertexFound && closestVtx != vtxArr->At(0)) {
700     // pfPileUp->Add(pf);
701     pfNoPileUpflag.push_back(0);
702     } else {
703     // PFCandidate * pfNoPileUp->AddNew(); // Ridiculous but that's how it is
704     pfNoPileUpflag.push_back(1);
705     }
706     }
707     } //hadron & trk stuff
708     } else { // hadron
709     // PFCandidate * ptr = pfNoPileUp->AddNew();
710     pfNoPileUpflag.push_back(1);
711     }
712     } // Loop over PF candidates
713    
714     return pfNoPileUpflag.size();
715 khahn 1.19 }
716 khahn 1.22
717 khahn 1.28 void setEra(string fname, ControlFlags &ctrl)
718     {
719     if( ctrl.mc && (fname.find("f11-") != string::npos) ) ctrl.era=2011 ;
720     if( ctrl.mc && (fname.find("s12-") != string::npos) ) ctrl.era=2012 ;
721     if( !ctrl.mc && (fname.find("r11-") != string::npos) ) ctrl.era=2011 ;
722     if( !ctrl.mc && (fname.find("r12-") != string::npos) ) ctrl.era=2012 ;
723     }