ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitHzz4l/Selection/src/Selection.cc
Revision: 1.17
Committed: Mon Nov 7 06:53:01 2011 UTC (13 years, 6 months ago) by khahn
Content type: text/plain
Branch: MAIN
Changes since 1.16: +20 -6 lines
Log Message:
ifdef for Z2FO

File Contents

# User Rev Content
1 khahn 1.1 #include "Selection.h"
2     #include "PassHLT.h"
3 khahn 1.7
4     #include "SiMVAElectronSelection.h"
5    
6 khahn 1.1 #include "HZZCiCElectronSelection.h"
7 dkralph 1.3 #include "HZZLikelihoodElectronSelection.h"
8 dkralph 1.4 #include "HZZBDTElectronSelection.h"
9 khahn 1.1 #include "RunLumiRangeMap.h"
10 khahn 1.13 #include "EfficiencyWeightsInterface.h"
11 khahn 1.1
12 dkralph 1.2 RunLumiRangeMap rlrm;
13 khahn 1.15 TH1D * hpu;
14    
15 khahn 1.1
16 khahn 1.13 // #include "TH2D.h"
17     // extern TH2D * h_mu_eff_idiso_2011a;
18     // extern TH2D * h_mu_eff_idiso_s11;
19     // extern TH2D * h_mu_effdata_trigger_doublemu7_2011a;
20     // extern TH2D * h_mu_effdata_trigger_doublemu13_8_leading_2011a;
21     // extern TH2D * h_mu_effdata_trigger_doublemu13_8_trailing_2011a;
22    
23     // unsigned getGenChannel(mithep::TGenInfo * ginfo) {
24     // int gchannel=-1;
25     // if( abs(ginfo->id_1_a) == EGenType::kElectron && abs(ginfo->id_1_b) == EGenType::kElectron ) gchannel=0;
26     // else if( abs(ginfo->id_1_a) == EGenType::kMuon && abs(ginfo->id_1_b) == EGenType::kMuon ) gchannel=1;
27     // else if( (abs(ginfo->id_1_a) == EGenType::kElectron && abs(ginfo->id_1_b) == EGenType::kMuon) ||
28     // (abs(ginfo->id_1_a) == EGenType::kMuon && abs(ginfo->id_1_b) == EGenType::kElectron) ) gchannel=2;
29 khahn 1.12
30 khahn 1.13 // return gchannel;
31     // };
32    
33     void initRunLumiRangeMap() {
34     rlrm.AddJSONFile(std::string("./data/Cert_136033-149442_7TeV_Apr21ReReco_Collisions10_JSON.txt"));
35     // rlrm.AddJSONFile(std::string("./data/Cert_160404-173244_7TeV_PromptReco_Collisions11_JSON_v2.txt"));
36     rlrm.AddJSONFile(std::string("./data/Cert_160404-178078_7TeV_PromptReco_Collisions11_JSON.txt"));
37     rlrm.AddJSONFile(std::string("./data/Cert_160404-163869_7TeV_May10ReReco_Collisions11_JSON_v3.txt"));
38     rlrm.AddJSONFile(std::string("./data/Cert_170249-172619_7TeV_ReReco5Aug_Collisions11_JSON.txt"));
39 khahn 1.15 TFile * puf = new TFile("data/PileupReweighting.Summer11DYmm_To_Run2011A.root");
40     hpu = (TH1D*)(puf->Get("puWeights"));
41 khahn 1.12 };
42    
43 khahn 1.13 void initRunLumiRangeMap(RunLumiRangeMap &rlrm) {
44     cout << "adding JSONS ... " << endl;
45 khahn 1.1 rlrm.AddJSONFile(std::string("./data/Cert_136033-149442_7TeV_Apr21ReReco_Collisions10_JSON.txt"));
46 khahn 1.11 // rlrm.AddJSONFile(std::string("./data/Cert_160404-173244_7TeV_PromptReco_Collisions11_JSON_v2.txt"));
47     rlrm.AddJSONFile(std::string("./data/Cert_160404-178078_7TeV_PromptReco_Collisions11_JSON.txt"));
48 khahn 1.1 rlrm.AddJSONFile(std::string("./data/Cert_160404-163869_7TeV_May10ReReco_Collisions11_JSON_v3.txt"));
49     rlrm.AddJSONFile(std::string("./data/Cert_170249-172619_7TeV_ReReco5Aug_Collisions11_JSON.txt"));
50     };
51    
52 khahn 1.13 // unsigned fails_HZZ4L_selection(ControlFlags &ctrl, // input control
53     // mithep::TEventInfo *info, // input event inof
54     // TClonesArray *electronArr, // input electrons
55     // TClonesArray *muonArr, // input muons
56     // double eventweight, // weight
57     // TTree * passtuple ) {
58 khahn 1.1
59 khahn 1.13 // fails_HZZ4L_selection( ctrl, info, electronArr, muonArr, eventweight, passtuple, NULL );
60 khahn 1.1
61 khahn 1.13 // };
62 khahn 1.1
63 khahn 1.12
64    
65 khahn 1.13 // unsigned fails_HZZ4L_selection(ControlFlags &ctrl, // input control
66     // mithep::TEventInfo *info, // input event inof
67     // TClonesArray *electronArr, // input electrons
68     // TClonesArray *muonArr, // input muons
69     // double eventweight, // weight
70     // LabVectors *l ) {
71 khahn 1.1
72 khahn 1.13 // fails_HZZ4L_selection( ctrl, info, electronArr, muonArr, eventweight, NULL, l );
73 khahn 1.1
74 khahn 1.13 // };
75 khahn 1.1
76    
77     unsigned fails_HZZ4L_selection(ControlFlags &ctrl, // input control
78 khahn 1.12 mithep::TGenInfo *ginfo , // input gen info
79     mithep::TEventInfo *info, // input event info
80     TClonesArray *electronArr, // input electrons
81     TClonesArray *muonArr, // input muons
82     double eventweight, // weight
83     TTree * passtuple ) {
84    
85 khahn 1.13 fails_HZZ4L_selection( ctrl, info, electronArr, muonArr, eventweight, passtuple, NULL, NULL );
86 khahn 1.12
87     };
88    
89 khahn 1.13 unsigned fails_HZZ4L_selection(ControlFlags &ctrl, // input control
90     mithep::TEventInfo *info, // input event info
91     TClonesArray *electronArr, // input electrons
92     TClonesArray *muonArr, // input muons
93     double eventweight, // weight
94     TTree * passtuple,
95     LabVectors * l ) {
96     fails_HZZ4L_selection( ctrl, info, electronArr, muonArr, eventweight, passtuple, l, NULL );
97     }
98    
99 khahn 1.12
100     unsigned fails_HZZ4L_selection(ControlFlags &ctrl, // input control
101     mithep::TEventInfo *info, // input event info
102 khahn 1.1 TClonesArray *electronArr, // input electrons
103     TClonesArray *muonArr, // input muons
104 dkralph 1.2 double eventweight, // weight
105 khahn 1.5 TTree * passtuple,
106 khahn 1.13 LabVectors * l=NULL,
107     TClonesArray *jetArr=NULL ) { // output ntuple
108 khahn 1.1
109     unsigned evtfail = 0x0;
110 khahn 1.12 unsigned gchannel=0xdeaddead;
111    
112 khahn 1.13 // if( ctrl.mc && ginfo != NULL ) {
113     // gchannel = getGenChannel(ginfo);
114     // }
115 khahn 1.1
116 khahn 1.5
117     if( ctrl.debug ) {
118     cout << "Run: " << info->runNum
119     << "\tEvt: " << info->evtNum
120     << "\tLumi: " << info->lumiSec
121     << endl;
122     }
123    
124 khahn 1.15 unsigned npu; double npuw;
125 khahn 1.1 if( !ctrl.mc ) {
126     // not accounting for overlap atm
127 dkralph 1.2 RunLumiRangeMap::RunLumiPairType rl(info->runNum, info->lumiSec);
128 khahn 1.1 if( !(rlrm.HasRunLumi(rl)) ) {
129 khahn 1.5 if( ctrl.debug ) cout << "\tfails JSON" << endl;
130 khahn 1.1 evtfail |= (1<<EVTFAIL_JSON);
131     return evtfail;
132     }
133 khahn 1.15 } else {
134     npu = info->nPU;
135     npuw = hpu->GetBinContent(hpu->FindBin(npu));
136 khahn 1.1 }
137    
138    
139 khahn 1.5
140 khahn 1.1
141    
142     //********************************************************
143     // Trigger
144     //********************************************************
145     if( !ctrl.mc ) {
146     // if( !(passHLT(info->triggerBits, info->runNum, channel) ) ) {
147 khahn 1.13 // if( !(passHLT(info->triggerBits, info->runNum, 999) ) ) {
148     // evtfail |= (1<<EVTFAIL_TRIGGER);
149     // return evtfail;
150     // }
151 khahn 1.1 } else {
152     if( !(passHLTMC(info->triggerBits)) ) {
153     evtfail |= (1<<EVTFAIL_TRIGGER);
154     return evtfail;
155     }
156     // cout << "MC trigger bits: " << hex << info->triggerBits << dec << endl;
157     }
158    
159     if( ctrl.debug ) {
160     cout << "presel nlep: " << muonArr->GetEntries() + electronArr->GetEntries()
161     << "\tnmuon: " << muonArr->GetEntries()
162     << "\tnelectron: " << electronArr->GetEntries()
163     << endl;
164     }
165    
166     //********************************************************
167     // Lepton Selection
168     //********************************************************
169     vector<SimpleLepton> lepvec;
170    
171     //
172 dkralph 1.3 if( ctrl.debug ) cout << "\tnMuons: " << muonArr->GetEntries() << endl;
173 khahn 1.1 //----------------------------------------------------
174     for(Int_t i=0; i<muonArr->GetEntries(); i++) {
175     const mithep::TMuon *mu = (mithep::TMuon*)((*muonArr)[i]);
176 khahn 1.11 unsigned muonfail;
177     if( ctrl.muSele == "ksWW" )
178 khahn 1.16 muonfail = passKSMuonSelection(mu);
179 khahn 1.11 else
180     muonfail = passMuonSelectionZZ(mu);
181 khahn 1.1 if( ctrl.debug ) {
182     cout << "muon:: pt: " << mu->pt
183     << "\teta: " << mu->eta
184     << "\tmask: 0x" << hex << muonfail << dec
185     << endl;
186     }
187 khahn 1.17
188     #ifdef Z2FO
189     if ( isMuFO(mu) ) {
190     #else
191 khahn 1.1 if ( !muonfail ) {
192 khahn 1.17 #endif
193 khahn 1.1 SimpleLepton tmplep;
194 khahn 1.14
195     float pt = mu->pt;
196     if( ctrl.do_escale_up ) pt*=(1.01);
197     if( ctrl.do_escale_down ) pt*=(0.99);
198    
199     tmplep.vec.SetPtEtaPhiM(pt,
200 khahn 1.1 mu->eta,
201     mu->phi,
202     105.658369e-3);
203 khahn 1.14
204 khahn 1.1 tmplep.type = 13;
205     tmplep.index = i;
206     tmplep.charge = mu->q;
207     tmplep.isoTrk = mu->trkIso03;
208     tmplep.isoEcal = mu->emIso03;
209     tmplep.isoHcal = mu->hadIso03;
210 khahn 1.11 tmplep.isoPF03 = mu->pfIso03;
211 khahn 1.8 tmplep.isoPF04 = mu->pfIso04;
212 khahn 1.1 tmplep.ip3dSig = mu->ip3dSig;
213     tmplep.is4l = false;
214     tmplep.isEB = (fabs(mu->eta) < 1.479 ? 1 : 0 );
215 khahn 1.17 tmplep.isTight = (muonfail > 0 ? 0 : 1 );
216     tmplep.isLoose = (muonfail > 0 ? 0 : 1 );
217 khahn 1.1 lepvec.push_back(tmplep);
218     if( ctrl.debug ) { cout << "muon passes ... " << endl;}
219     }
220     }
221    
222     if( ctrl.debug ) { cout << "\tnElectron: " << electronArr->GetEntries() << endl; }
223 dkralph 1.3
224 khahn 1.1 //----------------------------------------------------
225     for(Int_t i=0; i<electronArr->GetEntries(); i++) {
226     const mithep::TElectron *ele = (mithep::TElectron*)((*electronArr)[i]);
227    
228 khahn 1.13 if( !(isEleFO(ctrl,ele) ) ) continue;
229     if( ele->pt < 7 ) continue; //move this to ID
230    
231 khahn 1.1 Bool_t isMuonOverlap = kFALSE;
232     for (int k=0; k<lepvec.size(); ++k) {
233     TVector3 tmplep;
234     tmplep.SetPtEtaPhi(ele->pt, ele->eta, ele->phi);
235 khahn 1.17 if ( lepvec[k].isLoose && lepvec[k].type == 13 && lepvec[k].vec.Vect().DrEtaPhi(tmplep) < 0.1 ) {
236 dkralph 1.3 if( ctrl.debug ) cout << "-----> isMuonOverlap! " << endl;
237 khahn 1.1 isMuonOverlap = kTRUE;
238     break;
239     }
240     }
241    
242 khahn 1.17 unsigned FAIL=0, isEleTight=0, isEleLoose=0;
243 khahn 1.11
244 dkralph 1.4 unsigned failsCIC=0;
245 khahn 1.11 CICStruct ciccuts_tight, ciccuts_medium, ciccuts_loose;
246 dkralph 1.4 if(ctrl.eleSele=="cic") {
247 khahn 1.11 if( ctrl.eleSeleScheme == "mediumloose" ) {
248     ciccuts_medium = getCiCCuts("medium");
249     unsigned failsCICMedium = failsCicSelection(ctrl, ele, ciccuts_medium, ctrl.kinematics);
250     ciccuts_loose = getCiCCuts("loose");
251     unsigned failsCICLoose = failsCicSelection(ctrl, ele, ciccuts_loose, ctrl.kinematics);
252 khahn 1.12 failsCIC = ( failsCICLoose > 0 && failsCICMedium > 0 );
253 khahn 1.11 if( !failsCICMedium ) isEleTight=1;
254     }
255     else {
256     ciccuts_tight = getCiCCuts(ctrl.eleSeleScheme);
257     failsCIC = failsCicSelection(ctrl, ele, ciccuts_tight, ctrl.kinematics);
258     if( !failsCIC ) isEleTight=1;
259     }
260 dkralph 1.4 FAIL = failsCIC;
261     }
262 khahn 1.11
263 dkralph 1.4 LikStruct likcuts;
264     unsigned failsLike=0;
265     if(ctrl.eleSele=="lik") {
266     likcuts = getLikCuts(ctrl.eleSeleScheme);
267     failsLike = failsLikelihoodSelection(ele, likcuts, ctrl.kinematics);
268     FAIL = failsLike;
269     }
270 khahn 1.11 unsigned failsBDT=0;
271 dkralph 1.4 if(ctrl.eleSele=="bdt") {
272 khahn 1.11 if( ctrl.eleSeleScheme == "mediumloose" ) {
273     unsigned failsBDTMedium = failsBDTSelection(ctrl,"medium",ele);
274     unsigned failsBDTLoose = failsBDTSelection(ctrl,"loose",ele);
275 khahn 1.12 failsBDT = ( failsBDTLoose > 0 && failsBDTMedium > 0 );
276 khahn 1.11 if( !failsBDTMedium ) isEleTight=1;
277 khahn 1.17 if( !failsBDTLoose ) isEleLoose=1;
278 khahn 1.11 } else {
279     failsBDT = failsBDTSelection(ctrl,"tight",ele);
280     if( !failsBDT ) isEleTight=1;
281     }
282 dkralph 1.4 FAIL = failsBDT;
283     }
284 khahn 1.7
285 dkralph 1.3
286 khahn 1.1 if( ctrl.debug ){
287     cout << "CIC category: " << cicCategory(ele)
288 dkralph 1.3 << "\tlikelihood: " << ele->likelihood
289     << "\tFAIL: 0x" << hex << FAIL << dec
290     << "\tfailsCIC: 0x" << hex << failsCIC << dec
291     << "\tfailsLike: 0x" << hex << failsLike << dec
292 dkralph 1.4 << "\tfailsBDT: 0x" << hex << failsBDT << dec
293 khahn 1.1 << "\tscEt: " << ele->scEt
294 dkralph 1.3 << "\tscEta: " << ele->scEta
295     << "\tncluster: " << ele->ncluster
296 khahn 1.1 << endl;
297     }
298 dkralph 1.3 if ( !FAIL && !isMuonOverlap ) {
299 khahn 1.1 SimpleLepton tmplep;
300 khahn 1.14
301     float pt = ele->pt;
302     if( ctrl.do_escale_up ) pt*=(1.02);
303     if( ctrl.do_escale_down ) pt*=(0.98);
304    
305     tmplep.vec.SetPtEtaPhiM( pt,
306 khahn 1.1 ele->eta,
307     ele->phi,
308     0.51099892e-3 );
309     tmplep.type = 11;
310     tmplep.index = i;
311     tmplep.charge = ele->q;
312     tmplep.isoTrk = ele->trkIso03;
313     tmplep.isoEcal = ele->emIso03;
314     tmplep.isoHcal = ele->hadIso03;
315 khahn 1.11 tmplep.isoPF03 = ele->pfIso03;
316 khahn 1.8 tmplep.isoPF04 = ele->pfIso04;
317 khahn 1.1 tmplep.ip3dSig = ele->ip3dSig;
318     tmplep.is4l = false;
319 khahn 1.11 tmplep.isTight = isEleTight;
320 khahn 1.1 tmplep.isEB = ele->isEB;
321     lepvec.push_back(tmplep);
322     if( ctrl.debug ) { cout << "\telectron passes ... " << endl; }
323     }
324     }
325    
326     sort( lepvec.begin(), lepvec.end(), SimpleLepton::lep_pt_sort );
327    
328 khahn 1.13 for( int i=0; i<lepvec.size(); i++ ) {
329     //
330     // check for a matched btagged jet
331     //
332     lepvec[i].tche = -1;
333     lepvec[i].tchp = -1;
334     lepvec[i].csv = -1;
335     lepvec[i].csvMva = -1;
336    
337     if( jetArr != NULL ) {
338     for(int k=0; k<jetArr->GetEntries(); k++) {
339     const mithep::TJet *jet = (mithep::TJet*)((*jetArr)[k]);
340     TVector3 jvec;
341     jvec.SetPtEtaPhi(jet->pt, jet->eta, jet->phi);
342     if( jvec.DrEtaPhi( lepvec[i].vec.Vect()) < 0.2 ) {
343     lepvec[i].tche = jet->tche;
344     lepvec[i].tchp = jet->tchp;
345     lepvec[i].csv = jet->csv;
346     lepvec[i].csvMva = jet->csvMva;
347     }
348     }
349     }
350     }
351    
352 khahn 1.1 int nmu=0, nele=0;
353     for( int i=0; i<lepvec.size(); i++ ) {
354     if( abs(lepvec[i].type) == 11 ) nele++;
355     else nmu++;
356     }
357     if( ctrl.debug ) {
358     cout << "postsel nlep: " << lepvec.size()
359     << "\tnmuon: " << nmu
360     << "\tnelectron: " << nele
361     << endl;
362     }
363    
364     //******************************************************************************
365     //Z1 Selection
366     //******************************************************************************
367     int Z1LeptonPlusIndex = -1;
368     int Z1LeptonMinusIndex = -1;
369     double BestZ1Mass = -999;
370     if( ctrl.debug ) { cout << "looking for a Z1 ..." << endl; }
371     for(int i = 0; i < lepvec.size(); ++i) {
372 khahn 1.17 if( !(lepvec[i].isLoose ) ) continue;
373 khahn 1.1 for(int j = i+1; j < lepvec.size(); ++j) {
374 khahn 1.17 if( !(lepvec[j].isLoose ) ) continue;
375 khahn 1.1 if( ctrl.debug ) { cout << "\tconsidering leptons " << i << " & " << j << endl; }
376     if (!(lepvec[i].vec.Pt() > 20.0 || lepvec[j].vec.Pt() > 20.0)) continue;
377     if( ctrl.debug ) { cout << "\tat least one is > 20 GeV" << endl; }
378     if (!(lepvec[i].vec.Pt() > 10.0 && lepvec[j].vec.Pt() > 10.0)) continue;
379     if( ctrl.debug ) { cout << "\tthe other is > 10 GeV" << endl; }
380     if (lepvec[i].charge == lepvec[j].charge) continue;
381     if( ctrl.debug ) { cout << "\tthey're opposite charge" << endl; }
382     if (fabs(lepvec[i].type) != fabs(lepvec[j].type)) continue;
383     if( ctrl.debug ) { cout << "\tthey're same flavor" << endl; }
384    
385     //Make Z1 hypothesis
386     TLorentzVector leptonPlus, leptonMinus;
387     if ( lepvec[i].charge > 0 ) {
388     leptonPlus = lepvec[i].vec;
389     leptonMinus = lepvec[j].vec;
390     } else {
391     leptonPlus = lepvec[j].vec;
392     leptonMinus = lepvec[i].vec;
393     }
394    
395     float tmpZ1Mass = (leptonPlus+leptonMinus).M();
396 dkralph 1.3 if( ctrl.debug ) cout << "Z1 selection, tmpZ1Mass: " << tmpZ1Mass << endl;
397 khahn 1.1 if( tmpZ1Mass > 60 ) {
398     if (fabs(tmpZ1Mass - 91.1876) < fabs(BestZ1Mass - 91.1876)) {
399     BestZ1Mass = tmpZ1Mass;
400 dkralph 1.3 if( ctrl.debug ) cout << "Z1 selection, new BestZ1Mass: " << BestZ1Mass
401 khahn 1.1 << "\tdM: " << fabs(BestZ1Mass - 91.1876)
402     << endl;
403     if (lepvec[i].charge > 0) {
404     Z1LeptonPlusIndex = i;
405     Z1LeptonMinusIndex = j;
406     } else {
407     Z1LeptonPlusIndex = j;
408     Z1LeptonMinusIndex = i;
409     }
410     }
411     }
412     }
413     }
414     // stop if no Z1 candidate is found
415     if( BestZ1Mass < 0 ) {
416     evtfail |= (1<<EVTFAIL_Z1);
417     return evtfail;
418     }
419 dkralph 1.3 if( ctrl.debug ) cout << "\tgot a Z1 ... run: " << info->runNum << "\tevt: " << info->evtNum << endl;
420     if( ctrl.debug ) cout << "\tZ1 plusindex: " << Z1LeptonPlusIndex << "\tminusindex: " << Z1LeptonMinusIndex << endl;
421 khahn 1.1 TLorentzVector Z1LeptonPlus = lepvec[Z1LeptonPlusIndex].vec;
422     TLorentzVector Z1LeptonMinus = lepvec[Z1LeptonMinusIndex].vec;
423     TLorentzVector Z1Candidate = Z1LeptonPlus + Z1LeptonMinus;
424     if( l != NULL ) {
425     l->vecz1 = Z1Candidate;
426     l->vecl1p = Z1LeptonPlus;
427     l->vecl1m = Z1LeptonMinus;
428     }
429    
430     //******************************************************************************
431     // Z1 + l
432     //******************************************************************************
433     if( lepvec.size() < 3 ) {
434     evtfail |= (1<<EVTFAIL_Z1_PLUSL);
435     return evtfail;
436     }
437    
438     //******************************************************************************
439     // 4l/Z2 Selection
440     //******************************************************************************
441     Int_t Z2LeptonPlusIndex = -1;
442     Int_t Z2LeptonMinusIndex = -1;
443     Double_t BestZ2Mass = -1;
444 dkralph 1.3 if( ctrl.debug ) cout << "looking for a Z2 ... out of " << lepvec.size() << " leptons" <<endl;
445 khahn 1.1 for(int i = 0; i < lepvec.size(); ++i) {
446 khahn 1.17
447     #ifndef Z2FO
448     if( ctrl.eleSeleScheme == "mediumloose" &&
449 khahn 1.11 !(lepvec[i].isTight) ) continue;
450 khahn 1.17 #endif
451 khahn 1.11
452 khahn 1.1 for(int j = i+1; j < lepvec.size(); ++j) {
453 khahn 1.17
454     #ifndef Z2FO
455     if( ctrl.eleSeleScheme == "mediumloose" &&
456 khahn 1.11 !(lepvec[j].isTight) ) continue;
457 khahn 1.17 #endif
458 khahn 1.11
459 khahn 1.1 // cout << "i: " << i << "\tj: " << j << endl;
460     if (i == Z1LeptonPlusIndex || i == Z1LeptonMinusIndex) {
461     // cout << "\ti matches a Z1 index, skipping ..." << endl;
462     continue; //skip Z1 leptons
463     }
464     if (j == Z1LeptonPlusIndex || j == Z1LeptonMinusIndex) {
465     // cout << "\tj matches a Z1 index, skipping ..." << endl;
466     continue; //skip Z1 leptons
467     }
468     if (lepvec[i].charge == lepvec[j].charge) {
469     // cout << "\ti and j are same sign, skipping ..." << endl;
470     continue; //require opp sign
471     }
472     if (fabs(lepvec[i].type) != fabs(lepvec[j].type)) {
473     // cout << "\ti and j are not same flavor, skipping ..." << endl;
474     continue; //require same flavor
475     }
476    
477    
478     //Make Z2 hypothesis
479     TLorentzVector leptonPlus, leptonMinus;
480    
481     if (lepvec[i].charge > 0 ) {
482     leptonPlus = lepvec[i].vec;
483     leptonMinus = lepvec[j].vec;
484     } else {
485     leptonPlus = lepvec[j].vec;
486     leptonMinus = lepvec[i].vec;
487     }
488    
489     TLorentzVector dilepton = leptonPlus+leptonMinus;
490     TLorentzVector fourLepton = Z1Candidate + dilepton;
491    
492 dkralph 1.3 if( ctrl.debug ) cout << "dilepton.M() : " << dilepton.M() << endl;
493     if( ctrl.debug ) cout << "fourLepton.M() : " << fourLepton.M() << endl;
494 khahn 1.1
495     if (!(dilepton.M() > 12.0)) continue;
496     if (!(fourLepton.M() > 100.0)) continue;
497    
498 khahn 1.11 /*
499 khahn 1.1 //for 4e and 4mu, require at least 1 of the other opp sign lepton pairs have mass > 12
500     if (fabs(lepvec[i].type) == fabs(lepvec[Z1LeptonPlusIndex].type)) {
501     TLorentzVector pair1 = Z1LeptonPlus+leptonMinus;
502     TLorentzVector pair2 = Z1LeptonMinus+leptonPlus;
503 dkralph 1.3 if( ctrl.debug ) cout << "pair1: " << pair1.M() << "\tpair2: "<< pair2.M() << endl;
504 khahn 1.1 if (!(pair1.M() > 12 || pair2.M() > 12)) continue;
505     }
506 khahn 1.11 */
507    
508 khahn 1.1 //Disambiguiation is done by choosing the pair with the largest ptMax and largest ptMin
509     if (Z2LeptonPlusIndex < 0) {
510     if (lepvec[i].charge > 0) {
511     Z2LeptonPlusIndex = i;
512     Z2LeptonMinusIndex = j;
513     } else {
514     Z2LeptonPlusIndex = j;
515     Z2LeptonMinusIndex = i;
516     }
517     } else {
518     Double_t BestPairPtMax = lepvec[Z2LeptonPlusIndex].vec.Pt();
519     Double_t BestPairPtMin = lepvec[Z2LeptonMinusIndex].vec.Pt();
520     if (lepvec[Z2LeptonMinusIndex].vec.Pt() > BestPairPtMax) {
521     BestPairPtMax = lepvec[Z2LeptonMinusIndex].vec.Pt();
522     BestPairPtMin = lepvec[Z2LeptonPlusIndex].vec.Pt();
523     }
524    
525     Double_t CurrentPairPtMax = lepvec[i].vec.Pt();
526     Double_t CurrentPairPtMin = lepvec[j].vec.Pt();
527     if (lepvec[j].vec.Pt() > CurrentPairPtMax) {
528     CurrentPairPtMax = lepvec[j].vec.Pt();
529     CurrentPairPtMin = lepvec[i].vec.Pt();
530     }
531    
532     if (CurrentPairPtMax > BestPairPtMax) {
533     if (lepvec[i].charge > 0) {
534     Z2LeptonPlusIndex = i;
535     Z2LeptonMinusIndex = j;
536     } else {
537     Z2LeptonPlusIndex = j;
538     Z2LeptonMinusIndex = i;
539     }
540     } else if (CurrentPairPtMax == BestPairPtMax) {
541     if (CurrentPairPtMin > BestPairPtMin) {
542     if (lepvec[i].charge > 0) {
543     Z2LeptonPlusIndex = i;
544     Z2LeptonMinusIndex = j;
545     } else {
546     Z2LeptonPlusIndex = j;
547     Z2LeptonMinusIndex = i;
548     }
549     }
550     }
551     }
552     }
553     }
554    
555     // stop if no Z2 candidate is found
556     if (Z2LeptonPlusIndex == -1) {
557     evtfail |= ( 1<<EVTFAIL_4L );
558     return evtfail;
559     // h_evtfail->Fill( evtfail );
560     // cout << "evtfail: " << hex << evtfail << dec << endl;
561     // continue;
562     }
563 dkralph 1.3 if( ctrl.debug ) cout << "\tgot a Z2 ..." << endl;
564     if( ctrl.debug ) cout << "\tZ2 plusindex: " << Z2LeptonPlusIndex
565     << "\tminusindex: " << Z2LeptonMinusIndex << endl;
566 khahn 1.1 TLorentzVector Z2LeptonPlus = lepvec[Z2LeptonPlusIndex].vec;
567     TLorentzVector Z2LeptonMinus = lepvec[Z2LeptonMinusIndex].vec;
568     TLorentzVector Z2Candidate = Z2LeptonPlus+Z2LeptonMinus;
569     TLorentzVector ZZSystem = Z1Candidate + Z2Candidate;
570     if( l != NULL ) {
571     l->vecz2 = Z2Candidate;
572     l->vecl2p = Z2LeptonPlus;
573     l->vecl2m = Z2LeptonMinus;
574     l->vec4l = ZZSystem;
575     }
576     lepvec[Z1LeptonPlusIndex].is4l = true;
577     lepvec[Z1LeptonMinusIndex].is4l = true;
578     lepvec[Z2LeptonPlusIndex].is4l = true;
579     lepvec[Z2LeptonMinusIndex].is4l = true;
580    
581     //***************************************************************
582     // Isolation
583     //***************************************************************
584     bool failiso=false;
585    
586 khahn 1.10 if( ctrl.isoScheme == "pf" ) {
587 khahn 1.1
588 khahn 1.8 for( int i=0; i<lepvec.size(); i++ ) {
589 khahn 1.11
590 khahn 1.8 if( !(lepvec[i].is4l) ) continue;
591 khahn 1.11
592 khahn 1.8 if( abs(lepvec[i].type) == 11 ) {
593 khahn 1.11 float reliso = lepvec[i].isoPF04/lepvec[i].vec.Pt();
594     if( lepvec[i].isEB && lepvec[i].vec.Pt() > 20 && reliso > PFISO_ELE_LOOSE_EB_HIGHPT ) {
595     failiso = true;
596     break;
597     }
598     if( lepvec[i].isEB && lepvec[i].vec.Pt() < 20 && reliso > PFISO_ELE_LOOSE_EB_LOWPT ) {
599     failiso = true;
600     break;
601     }
602     if( !(lepvec[i].isEB) && lepvec[i].vec.Pt() > 20 && reliso > PFISO_ELE_LOOSE_EE_HIGHPT ) {
603     failiso = true;
604     break;
605     }
606     if( !(lepvec[i].isEB) && lepvec[i].vec.Pt() < 20 && reliso > PFISO_ELE_LOOSE_EE_LOWPT ) {
607 khahn 1.8 failiso = true;
608     break;
609     }
610     }
611 khahn 1.11
612 khahn 1.8 if( abs(lepvec[i].type) == 13 ) {
613 khahn 1.11
614 khahn 1.14 /*
615 khahn 1.11 float reliso = lepvec[i].isoPF03/lepvec[i].vec.Pt();
616 khahn 1.12 if( lepvec[i].isEB && lepvec[i].vec.Pt() > 20 && reliso > PFISO_MU_LOOSE_EB_HIGHPT ) { //0.13
617 khahn 1.8 failiso = true;
618     break;
619     }
620 khahn 1.12 if( lepvec[i].isEB && lepvec[i].vec.Pt() < 20 && reliso > PFISO_MU_LOOSE_EB_LOWPT ) { //0.06
621 khahn 1.8 failiso = true;
622     break;
623     }
624 khahn 1.12 if( !(lepvec[i].isEB) && lepvec[i].vec.Pt() > 20 && reliso > PFISO_MU_LOOSE_EE_HIGHPT ) { //0.09
625 khahn 1.8 failiso = true;
626     break;
627     }
628 khahn 1.12 if( !(lepvec[i].isEB) && lepvec[i].vec.Pt() < 20 && reliso > PFISO_MU_LOOSE_EE_LOWPT ) { //0.05
629 khahn 1.8 failiso = true;
630     break;
631     }
632 khahn 1.14 */
633 khahn 1.8 }
634 khahn 1.9 }
635 khahn 1.10 } else if ( ctrl.isoScheme == "pairwise" ) {
636 khahn 1.8 float rho = info->rho;
637     for( int i=0; i<lepvec.size(); i++ ) {
638     if( !(lepvec[i].is4l) ) continue;
639 khahn 1.1 float effArea_ecal_i, effArea_hcal_i;
640     if( lepvec[i].isEB ) {
641     if( lepvec[i].type == 11 ) {
642     effArea_ecal_i = 0.101;
643     effArea_hcal_i = 0.021;
644     } else {
645     effArea_ecal_i = 0.074;
646     effArea_hcal_i = 0.022;
647     }
648     } else {
649     if( lepvec[i].type == 11 ) {
650     effArea_ecal_i = 0.046;
651     effArea_hcal_i = 0.040;
652     } else {
653     effArea_ecal_i = 0.045;
654     effArea_hcal_i = 0.030;
655     }
656     }
657     float isoEcal_corr_i = lepvec[i].isoEcal - (effArea_ecal_i*rho);
658     float isoHcal_corr_i = lepvec[i].isoHcal - (effArea_hcal_i*rho);
659     for( int j=i+1; j<lepvec.size(); j++ ) {
660     if( !(lepvec[j].is4l) ) continue;
661     float effArea_ecal_j, effArea_hcal_j;
662     if( lepvec[j].isEB ) {
663     if( lepvec[j].type == 11 ) {
664     effArea_ecal_j = 0.101;
665     effArea_hcal_j = 0.021;
666     } else {
667     effArea_ecal_j = 0.074;
668     effArea_hcal_j = 0.022;
669     }
670     } else {
671     if( lepvec[j].type == 11 ) {
672     effArea_ecal_j = 0.046;
673     effArea_hcal_j = 0.040;
674     } else {
675     effArea_ecal_j = 0.045;
676     effArea_hcal_j = 0.030;
677     }
678     }
679     float isoEcal_corr_j = lepvec[j].isoEcal - (effArea_ecal_j*rho);
680     float isoHcal_corr_j = lepvec[j].isoHcal - (effArea_hcal_j*rho);
681     float RIso_i = (lepvec[i].isoTrk+isoEcal_corr_i+isoHcal_corr_i)/lepvec[i].vec.Pt();
682     float RIso_j = (lepvec[j].isoTrk+isoEcal_corr_j+isoHcal_corr_j)/lepvec[j].vec.Pt();
683     float comboIso = RIso_i + RIso_j;
684     if( info->evtNum == 1038911933 ) {
685     float tmpdR = lepvec[i].vec.DrEtaPhi(lepvec[j].vec);
686     cout << "i: " << i
687     << "\tdR: " << tmpdR
688     << "\trho: " << rho
689     << "\tRIso_i: " << RIso_i
690     << "\ttkrel: " << lepvec[i].isoTrk/lepvec[i].vec.Pt()
691     << "\tecalrel: " << lepvec[i].isoEcal/lepvec[i].vec.Pt()
692     << "\tecalrelcor: " << isoEcal_corr_i/lepvec[i].vec.Pt()
693     << "\thcalrel: " << lepvec[i].isoHcal/lepvec[i].vec.Pt()
694     << "\thcalrelcor: " << isoHcal_corr_i/lepvec[i].vec.Pt()
695     << "\tpt_i: " << lepvec[i].vec.Pt()
696     << "\tj: " << j
697     << "\tRIso_j: " << RIso_j
698     << "\ttkrel: " << lepvec[j].isoTrk/lepvec[j].vec.Pt()
699     << "\tecalrel: " << lepvec[j].isoEcal/lepvec[j].vec.Pt()
700     << "\tecalrelcor: " << isoEcal_corr_j/lepvec[j].vec.Pt()
701     << "\thcalrel: " << lepvec[j].isoHcal/lepvec[j].vec.Pt()
702     << "\thcalrelcor: " << isoHcal_corr_j/lepvec[j].vec.Pt()
703     << "\tpt_j: " << lepvec[j].vec.Pt()
704     << "\tcombo: " << comboIso
705     << endl;
706     cout.flush();
707     }
708     if( comboIso > 0.35 ) {
709 dkralph 1.3 if( ctrl.debug ) cout << "combo failing for indices: " << i << "," << j << endl;
710 khahn 1.1 failiso = true;
711     // break;
712     }
713     }
714 khahn 1.9 }
715 khahn 1.1 }
716 khahn 1.8
717    
718 khahn 1.1 if( failiso ) {
719     evtfail |= ( 1<<EVTFAIL_ISOLATION );
720     return evtfail;
721     //h_evtfail->Fill( evtfail, eventweight );
722     // h_evtfail->Fill( evtfail );
723     // cout << "evtfail: " << hex << evtfail << dec << endl;
724     // continue;
725     }
726    
727     //***************************************************************
728     // IP significance
729     //***************************************************************
730     bool failip = false;
731     for( int i=0; i<lepvec.size(); i++ ) {
732     if( !(lepvec[i].is4l) ) continue;
733     if( lepvec[i].ip3dSig > 4 ) {
734     failip=true;
735     break;
736     }
737     }
738     if( failip ) {
739     evtfail |= (1<<EVTFAIL_IP );
740     return evtfail;
741     //h_evtfail->Fill( evtfail, eventweight );
742     // h_evtfail->Fill( evtfail );
743     // cout << "evtfail: " << hex << evtfail << dec << endl;
744     // continue;
745     }
746    
747     //***************************************************************
748     // remaining kinematic cuts
749     //***************************************************************
750 dkralph 1.3 double Z2massCut=0;
751     if ( ctrl.kinematics == "loose" ) Z2massCut = 12;
752     else if ( ctrl.kinematics == "tight" ) Z2massCut = 20;
753     else { cout << "error! kinematic tightness not defined!" << endl; assert(0); }
754    
755     if ( Z1Candidate.M() > 120 ||
756     Z2Candidate.M() < Z2massCut ||
757     Z2Candidate.M() > 120 ||
758 khahn 1.1 !(lepvec[Z1LeptonPlusIndex].vec.Pt() > 20.0 || lepvec[Z1LeptonMinusIndex].vec.Pt() > 20.0) ||
759     !(lepvec[Z1LeptonPlusIndex].vec.Pt() > 10.0 && lepvec[Z1LeptonMinusIndex].vec.Pt() > 10.0)
760     ) {
761     evtfail |= (1<<EVTFAIL_KINEMATICS );
762     return evtfail;
763     //h_evtfail->Fill( evtfail, eventweight );
764     // h_evtfail->Fill( evtfail );
765     // cout << "evtfail: " << hex << evtfail << dec << endl;
766     // continue;
767     }
768    
769 khahn 1.12 unsigned channel;
770 khahn 1.1 if( lepvec[Z1LeptonMinusIndex].type == 11 && lepvec[Z2LeptonMinusIndex].type == 11 ) channel=0;
771     if( lepvec[Z1LeptonMinusIndex].type == 13 && lepvec[Z2LeptonMinusIndex].type == 13 ) channel=1;
772     if( (lepvec[Z1LeptonMinusIndex].type == 11 && lepvec[Z2LeptonMinusIndex].type == 13) ||
773     (lepvec[Z1LeptonMinusIndex].type == 13 && lepvec[Z2LeptonMinusIndex].type == 11)) channel=2;
774    
775    
776 khahn 1.13 double w_offline=-1, werr_offline=0;
777     double w_online=-1, werr_online=0;
778 khahn 1.5
779 khahn 1.13 if( ctrl.mc ) {
780    
781     vector< pair <double,double> > wlegs; // pair here is eff & err
782     vector< pair <float,float> > mvec; // pair here is eta & pt
783 khahn 1.14 // now deal with medium vs loose
784     // vector< pair <float,float> > evec; // pair here is eta & pt
785     vector< pair< bool, pair <float,float> > > evec; // pair here is eta & pt
786 khahn 1.13
787     for( int k=0; k<lepvec.size(); k++ ) {
788     if( !(lepvec[k].is4l) ) continue;
789     if( abs(lepvec[k].type) == 13 ) {
790     mvec.push_back( std::pair<float,float> (fabs(lepvec[k].vec.Eta()), lepvec[k].vec.Pt()) );
791     wlegs.push_back( muonPerLegOfflineEfficiencyWeight( fabs(lepvec[k].vec.Eta()),
792     lepvec[k].vec.Pt() ) );
793     } else {
794 khahn 1.14
795     // now deal with medium vs loose
796     // evec.push_back( std::pair<float,float> (fabs(lepvec[k].vec.Eta()), lepvec[k].vec.Pt()) );
797    
798     std::pair<float,float> tmppair(fabs(lepvec[k].vec.Eta()), lepvec[k].vec.Pt());
799     evec.push_back( std::pair<bool, std::pair<float,float> > (lepvec[k].isTight, tmppair) );
800    
801 khahn 1.13 wlegs.push_back( elePerLegOfflineEfficiencyWeight( fabs(lepvec[k].vec.Eta()),
802     lepvec[k].vec.Pt() ) );
803     }
804     }
805    
806     pair<double,double> offpair = getOfflineEfficiencyWeight( wlegs );
807     w_offline = offpair.first;
808     werr_offline = offpair.second;
809    
810     pair<double,double> onpair = getOnlineEfficiencyWeight( mvec, evec );
811     w_online = onpair.first;
812     werr_online = onpair.second;
813 khahn 1.14
814 khahn 1.13 } // if mc
815    
816    
817     if( l != NULL ) {
818     l->vec4l = ZZSystem;
819     l->vecz1 = Z1Candidate;
820     l->vecz2 = Z2Candidate;
821     l->vecl1p = lepvec[Z1LeptonPlusIndex].vec;
822     l->vecl1m = lepvec[Z1LeptonMinusIndex].vec;
823     l->vecl2p = lepvec[Z2LeptonPlusIndex].vec;
824     l->vecl2m = lepvec[Z2LeptonMinusIndex].vec;
825     }
826 khahn 1.1 if( passtuple != NULL ) {
827 khahn 1.5 unsigned run = info->runNum;
828     unsigned evt = info->evtNum;
829     unsigned lumi = info->lumiSec;
830     unsigned chan = channel;
831 khahn 1.7 double w = eventweight;
832 khahn 1.5 float mZ1 = Z1Candidate.M() ;
833     float mZ2 = Z2Candidate.M() ;
834     float m4l = ZZSystem.M() ;
835     float pt4l = ZZSystem.Pt() ;
836 khahn 1.11 unsigned tZ1 = abs(lepvec[Z1LeptonMinusIndex].type);
837     unsigned tZ2 = abs(lepvec[Z2LeptonMinusIndex].type);
838 khahn 1.13 float l3tche = lepvec[Z2LeptonMinusIndex].tche;
839     float l4tche = lepvec[Z2LeptonPlusIndex].tche;
840     float l3tchp = lepvec[Z2LeptonMinusIndex].tchp;
841     float l4tchp = lepvec[Z2LeptonPlusIndex].tchp;
842     float l3csv = lepvec[Z2LeptonMinusIndex].csv;
843     float l4csv = lepvec[Z2LeptonPlusIndex].csv;
844     float l3csvMva = lepvec[Z2LeptonMinusIndex].csvMva;
845     float l4csvMva = lepvec[Z2LeptonPlusIndex].csvMva;
846    
847    
848 khahn 1.12 passtuple->SetBranchAddress("channel", &channel);
849 khahn 1.5 passtuple->SetBranchAddress("run", &run);
850     passtuple->SetBranchAddress("evt", &evt);
851     passtuple->SetBranchAddress("lumi", &lumi);
852     passtuple->SetBranchAddress("mZ1", &mZ1);
853     passtuple->SetBranchAddress("mZ2", &mZ2);
854 khahn 1.11 passtuple->SetBranchAddress("tZ1", &tZ1);
855     passtuple->SetBranchAddress("tZ2", &tZ2);
856 khahn 1.5 passtuple->SetBranchAddress("m4l", &m4l);
857     passtuple->SetBranchAddress("pt4l", &pt4l);
858 khahn 1.7 passtuple->SetBranchAddress("w", &w);
859 khahn 1.13 if( ctrl.mc ) {
860     passtuple->SetBranchAddress("woff", &w_offline );
861     passtuple->SetBranchAddress("werroff", &werr_offline );
862     passtuple->SetBranchAddress("won", &w_online );
863     passtuple->SetBranchAddress("werron", &werr_online );
864 khahn 1.15 passtuple->SetBranchAddress("npu", &npu );
865     passtuple->SetBranchAddress("npuw", &npuw );
866     passtuple->SetBranchAddress("gchannel", &gchannel);
867 khahn 1.13 }
868     if( ctrl.btag ) {
869     passtuple->SetBranchAddress("l3tche", &l3tche);
870     passtuple->SetBranchAddress("l4tche", &l4tche);
871     passtuple->SetBranchAddress("l3tchp", &l3tchp);
872     passtuple->SetBranchAddress("l4tchp", &l4tchp);
873     passtuple->SetBranchAddress("l3csv", &l3csv);
874     passtuple->SetBranchAddress("l4csv", &l4csv);
875     passtuple->SetBranchAddress("l3csvMva", &l3csvMva);
876     passtuple->SetBranchAddress("l4csvMva", &l4csvMva);
877     }
878    
879 khahn 1.5 passtuple->Fill( );
880 khahn 1.1 }
881    
882 dkralph 1.3 if( ctrl.debug ) cout << "run: " << info->runNum
883 khahn 1.1 << "\tevt: " << info->evtNum
884     << "\tZ1channel: " << lepvec[Z1LeptonMinusIndex].type
885     << "\tZ2channel: " << lepvec[Z2LeptonMinusIndex].type
886     << "\tmZ1: " << Z1Candidate.M()
887     << "\tmZ2: " << Z2Candidate.M()
888     << "\tm4l: " << ZZSystem.M()
889     << "\tevtfail: " << hex << evtfail << dec
890     << "\ttrigbits: " << hex << info->triggerBits << dec
891     // << "\ttree: " << inputFiles[q][f]
892     << endl;
893    
894     return evtfail;
895    
896     }
897    
898    
899    
900    
901