ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitHzz4l/Selection/src/applySelection.cc
Revision: 1.22
Committed: Mon May 14 17:27:34 2012 UTC (13 years ago) by khahn
Content type: text/plain
Branch: MAIN
Changes since 1.21: +129 -20 lines
Log Message:
*** empty log message ***

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