ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitHzz4l/Selection/src/ReferenceSelection.cc
Revision: 1.21
Committed: Tue Oct 23 11:39:21 2012 UTC (12 years, 6 months ago) by dkralph
Content type: text/plain
Branch: MAIN
Changes since 1.20: +242 -507 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 khahn 1.4 // system headers
2     #include <map>
3     #include <utility>
4    
5     // mit headers
6     #include "Vertex.h"
7    
8     // 4L stuff
9 khahn 1.1 #include "SelectionStatus.h"
10     #include "EventData.h"
11     #include "SimpleLepton.h"
12     #include "EfficiencyWeightsInterface.h"
13     #include "ElectronSelection.h"
14     #include "MuonSelection.h"
15     #include "IsolationSelection.h"
16     #include "ReferenceSelection.h"
17     #include "Selection.h"
18 khahn 1.4 #include "CommonDefs.h"
19 khahn 1.1 #include "SelectionDefs.h"
20 khahn 1.4 #include "FSR.h"
21 dkralph 1.9 #include "SelectionFuncs.h"
22 anlevin 1.17 #include "ElectronMomentumCorrection.h"
23 anlevin 1.16 #include "MuonMomentumCorrection.h"
24 khahn 1.1
25    
26     extern vector<SimpleLepton> failingLeptons;
27     extern vector<SimpleLepton> passingLeptons;
28    
29 khahn 1.3 extern map<unsigned,float> evtrhoMap;
30 anlevin 1.19 extern bool passes_HLT;
31 anlevin 1.18 extern map<TString, map<TString,int>* > counts;
32 dkralph 1.21 extern ElectronMomentumCorrection electron_momentum_correction;
33     extern MuCorr *muCorr;
34 khahn 1.1
35 dkralph 1.21 //----------------------------------------------------------------------------------------
36 khahn 1.7 void updateSimpleLepton(SimpleLepton &tmplep);
37 dkralph 1.21 //----------------------------------------------------------------------------------------
38 anlevin 1.18 void increment(TString cutName, int z1type=0, int z2type=0)
39 dkralph 1.21 //----------------------------------------------------------------------------------------
40     // increment counters that keep track up event counts at each cut
41 anlevin 1.18 {
42     assert(counts.find(cutName) != counts.end());
43     (*(counts[cutName]))["all"] ++;
44     if(z1type!=0 && z2type!=0) {
45     assert(counts.find(cutName) != counts.end());
46     (*(counts[cutName]))[getChannel(z1type,z2type)] ++;
47     }
48     }
49 khahn 1.1 //--------------------------------------------------------------------------------------------------
50     EventData apply_HZZ4L_reference_selection(ControlFlags &ctrl, // input control
51 dkralph 1.21 const EventHeader *info, // input event info
52     const Array<Vertex> * vtxArr ,
53     const Array<PFCandidate> *pfCandidates,
54     const Array<PileupEnergyDensity> *puEnergyDensity,
55     const Array<Electron> *electronArr, // input electrons
56 khahn 1.1 SelectionStatus (*ElectronPreSelector)( ControlFlags &,
57 dkralph 1.21 const Electron*,
58     const Vertex *),
59 khahn 1.1 SelectionStatus (*ElectronIDSelector)( ControlFlags &,
60 dkralph 1.21 const Electron*,
61     const Vertex *),
62 khahn 1.1 SelectionStatus (*ElectronIsoSelector)( ControlFlags &,
63 dkralph 1.21 const Electron*,
64     const Vertex *,
65     const Array<PFCandidate> *,
66     const Array<PileupEnergyDensity> *,
67     ElectronTools::EElectronEffectiveAreaTarget,
68     vector<const PFCandidate*>),
69     const Array<Muon> *muonArr, // input muons
70 khahn 1.1 SelectionStatus (*MuonPreSelector)( ControlFlags &,
71 dkralph 1.21 const Muon*,
72     const Vertex *,
73     const Array<PFCandidate> *),
74 khahn 1.1 SelectionStatus (*MuonIDSelector)( ControlFlags &,
75 dkralph 1.21 const Muon*,
76     // const Vertex &),
77     const Vertex *,
78     const Array<PFCandidate> *),
79 khahn 1.1 SelectionStatus (*MuonIsoSelector)( ControlFlags &,
80 dkralph 1.21 const Muon*,
81     const Vertex *,
82     const Array<PFCandidate> *,
83     const Array<PileupEnergyDensity> *,
84     MuonTools::EMuonEffectiveAreaTarget,
85     vector<const PFCandidate*>)
86 dkralph 1.9 )
87 khahn 1.1 //--------------------------------------------------------------------------------------------------
88 dkralph 1.21 {
89     increment("start");
90 khahn 1.1 EventData ret;
91     failingLeptons.clear();
92     passingLeptons.clear();
93    
94 dkralph 1.21 MuonTools::EMuonEffectiveAreaTarget eraMu;
95     ElectronTools::EElectronEffectiveAreaTarget eraEle;
96 dkralph 1.9 getEATargets(ctrl,eraMu,eraEle);
97 khahn 1.1
98     if( ctrl.debug ) {
99     cout << "presel nlep: " << muonArr->GetEntries() + electronArr->GetEntries()
100     << "\tnmuon: " << muonArr->GetEntries()
101     << "\tnelectron: " << electronArr->GetEntries()
102     << endl;
103     }
104    
105 dkralph 1.21 // correct muon momentum
106     if(ctrl.correct_muon_momentum) {
107     assert(muCorr->corr2011 && muCorr->corr2012);
108 anlevin 1.16 for(int i=0; i<muonArr->GetEntries(); i++) {
109 dkralph 1.21 const Muon *const_mu = (Muon*)((*muonArr)[i]);
110     Muon *mu = const_cast<Muon*>(const_mu);
111     correct_muon_momentum(ctrl,muCorr,mu,info->RunNum());
112 anlevin 1.16 }
113     }
114 khahn 1.1
115 dkralph 1.21 const Vertex * vtx;
116 khahn 1.3 bool goodVertex = setPV( ctrl, vtxArr, vtx );
117 dkralph 1.21 if(!goodVertex) {
118 dkralph 1.9 if(ctrl.debug) cout << "found bad vertex" << endl;
119 khahn 1.3 ret.status.setStatus(SelectionStatus::FAIL);
120     return ret;
121     }
122 dkralph 1.21 if(ctrl.debug)
123     cout << "vtx :: ntrks: " << vtx->NTracksFit() << endl;
124 khahn 1.3
125     //***********************************************************
126 khahn 1.11 // Trigger Selection
127 khahn 1.3 //***********************************************************
128 anlevin 1.19 if( passes_HLT ) {
129     increment("trigger");
130     } else {
131 dkralph 1.21 if(ctrl.debug) cout << "fails trigger" << endl;
132 anlevin 1.19 ret.status.setStatus(SelectionStatus::FAIL);
133     return ret;
134 khahn 1.3 }
135 khahn 1.11
136 khahn 1.1 //***********************************************************
137     // Lepton Selection
138     //***********************************************************
139     vector<SimpleLepton> lepvec;
140 dkralph 1.21 vector<const PFCandidate*> photonsToVeto;
141 khahn 1.1
142     if( ctrl.debug ) cout << "\tnMuons: " << muonArr->GetEntries() << endl;
143     for(int i=0; i<muonArr->GetEntries(); i++)
144     {
145 dkralph 1.21 const Muon *mu = (Muon*)((*muonArr)[i]);
146    
147 khahn 1.1 SelectionStatus musel;
148     if(ctrl.debug) cout << "musel.status before anything: " << musel.getStatus() << endl;
149    
150     musel |= (*MuonPreSelector)(ctrl,mu,vtx,pfCandidates);
151     if(ctrl.debug) cout << "musel.status after presel: " << musel.getStatus() << endl;
152     if( !(musel.getStatus() & SelectionStatus::PRESELECTION) ) continue;
153     if( ctrl.debug ) cout << endl;
154    
155     musel |= (*MuonIDSelector)(ctrl,mu,vtx,pfCandidates );
156     if(ctrl.debug) cout << "musel.status after ID: " << musel.getStatus() << endl;
157     if( ctrl.debug ) cout << endl;
158    
159 dkralph 1.21 // if we *do* FSR, isolation is calculated only after FSR recovery
160 khahn 1.7 if( !(ctrl.doFSR) ) {
161 khahn 1.8 musel |= (*MuonIsoSelector)(ctrl,mu,vtx,pfCandidates,puEnergyDensity,eraMu,photonsToVeto);
162 khahn 1.7 if(ctrl.debug) cout << "musel.status after iso: " << musel.getStatus() << endl;
163     }
164 khahn 1.1
165 dkralph 1.21 if( ctrl.debug )
166     cout << "muon:: pt: " << mu->Pt() << "\teta: " << mu->Eta() << "\tstatus: " << hex << musel.getStatus() << dec << endl;
167 khahn 1.1
168     SimpleLepton tmplep;
169 dkralph 1.21 tmplep.vec.SetPtEtaPhiM(mu->Pt(), mu->Eta(), mu->Phi(), MUON_MASS);
170 khahn 1.1 tmplep.type = 13;
171     tmplep.index = i;
172     tmplep.charge = mu->Charge();
173     tmplep.ip3dSig = mu->Ip3dPVSignificance();
174     tmplep.is4l = false;
175     tmplep.isEB = (fabs(mu->Eta()) < 1.479 ? 1 : 0 );
176     tmplep.isLoose = musel.loose();
177 dkralph 1.21 tmplep.isTight = mu->IsPFMuon() || mu->IsGlobalMuon();
178     tmplep.status = musel;
179     tmplep.fsrRecoveryAttempted = false; // NOTE: this is *used* inside FSR.cc
180 khahn 1.1 lepvec.push_back(tmplep);
181     if( ctrl.debug ) cout << endl;
182     }
183    
184     if( ctrl.debug ) { cout << "\tnElectron: " << electronArr->GetEntries() << endl; }
185     for(int i=0; i<electronArr->GetEntries(); i++)
186     {
187 dkralph 1.21 const Electron *const_ele = (Electron*)((*electronArr)[i]);
188     Electron *ele = const_cast<Electron*>(const_ele);
189 anlevin 1.19
190 dkralph 1.21 // evaluate MVA *before* regression correction (but cut on mvaVal *after*)
191     double mvaVal = getElectronIDMVAval(ctrl, ele, vtx);
192    
193     electron_momentum_correction.correct_electron_momentum(ctrl, ele,puEnergyDensity->At(0)->RhoKt6PFJets(),vtxArr->GetEntries());
194 khahn 1.1
195     SelectionStatus elesel;
196     if( ctrl.debug ) cout << "--> status before anything: " << hex << elesel.getStatus() << dec << endl;
197    
198 dkralph 1.21 elesel |= (*ElectronPreSelector)(ctrl,ele,vtx);
199     if( ctrl.debug ) cout << "--> status after presel: " << hex << elesel.getStatus() << dec << endl;
200     if( !(elesel.getStatus() & SelectionStatus::PRESELECTION) ) continue;
201    
202     // elesel |= (*ElectronIDSelector)(ctrl,ele,vtx);
203     elesel |= passElectronIDMVA(ctrl, mvaVal, ele);
204 anlevin 1.17 if( ctrl.debug ) cout << "--> status after ID: " << hex << elesel.getStatus() << dec << endl;
205    
206 dkralph 1.21 if( !(ctrl.doFSR) ) {
207     elesel |= (*ElectronIsoSelector)(ctrl,ele,vtx,pfCandidates,puEnergyDensity,eraEle,photonsToVeto);
208     if( ctrl.debug ) cout << "--> status after iso: " << hex << elesel.getStatus() << dec << endl;
209     }
210 anlevin 1.19
211 dkralph 1.21 if( ctrl.debug )
212     cout << "\tscEt: " << ele->SCluster()->Et()
213     << "\tscEta: " << ele->SCluster()->Eta()
214     << "\tstatus: " << hex << elesel.getStatus() << dec
215     << endl;
216 anlevin 1.17
217 dkralph 1.21 SimpleLepton tmplep;
218     tmplep.vec.SetPtEtaPhiM( ele->Pt(), ele->Eta(), ele->Phi(), ELECTRON_MASS );
219     tmplep.type = 11;
220     tmplep.index = i;
221     tmplep.charge = ele->Charge();
222     tmplep.ip3dSig = ele->Ip3dPVSignificance();
223     tmplep.is4l = false;
224     tmplep.isEB = ele->IsEB();
225     tmplep.scID = ele->SCluster()->GetUniqueID();
226     tmplep.isTight = elesel.tight();
227     tmplep.isLoose = elesel.loose();
228     tmplep.status = elesel;
229     tmplep.fsrRecoveryAttempted = false; // NOTE: this is *used* inside FSR.cc
230     lepvec.push_back(tmplep);
231 khahn 1.1 if( ctrl.debug ) cout << endl;
232 khahn 1.7 }
233 khahn 1.1
234     //********************************************************
235     // Dump Stuff
236     //********************************************************
237     sort( lepvec.begin(), lepvec.end(), SimpleLepton::lep_pt_sort );
238     int nmu=0, nele=0;
239     for( int i=0; i<lepvec.size(); i++ ) {
240 khahn 1.3 if(ctrl.debug) {
241 dkralph 1.21 bitset<16> tmpbits(lepvec[i].status.getStatus());
242     cout << "lepvec :: evt: " << setw(8) << info->EvtNum() << " index: " << setw(3) << i << " type: " << setw(4) << lepvec[i].type
243     << " pt: " << setw(11) << lepvec[i].vec.Pt() << " eta: " << setw(11) << lepvec[i].vec.Eta() << " status: " << tmpbits;
244     if(!ctrl.doFSR)
245     cout << "\tpf: " << lepvec[i].status.isoPF04 << "\tch: " << lepvec[i].status.chisoPF04 << "\tga: " << lepvec[i].status.gaisoPF04
246     << "\tne: " << lepvec[i].status.neisoPF04 << endl;
247 khahn 1.8 if( abs(lepvec[i].type) == 11 ) {
248 dkralph 1.21 const Electron *tmpele = (Electron*)((*electronArr)[lepvec[i].index]);
249 khahn 1.8 cout << "\tSCeta: " << tmpele->SCluster()->Eta()
250 dkralph 1.21 << "\tidMVA: " << lepvec[i].status.idMVA;
251 khahn 1.8 }
252 khahn 1.3 cout << endl;
253     }
254 khahn 1.1 if( abs(lepvec[i].type) == 11 ) nele++;
255     else nmu++;
256     }
257     if( ctrl.debug ) {
258     cout << "postsel nlep: " << lepvec.size()
259     << "\tnmuon: " << nmu
260     << "\tnelectron: " << nele
261     << endl;
262     }
263    
264     //********************************************************
265 dkralph 1.21 // Step 2: Lepton Cross-Cleaning: remove electrons that are next to 'good'ish muons
266 khahn 1.1 //********************************************************
267     vector<vector<SimpleLepton>::iterator> electrons_to_erase;
268     for (vector<SimpleLepton>::iterator it1=lepvec.begin();
269     it1 != lepvec.end(); it1++ ) {
270     if ( abs(it1->type) != 11 ) continue;
271     TVector3 evec = it1->vec.Vect();
272    
273 khahn 1.3 bool erase_this_electron=false;
274 khahn 1.1 for (vector<SimpleLepton>::iterator it2=lepvec.begin();
275     it2 != lepvec.end(); it2++ ) {
276     if ( it2 == it1 ) continue;
277     if ( abs(it2->type) != 13 ) continue;
278 dkralph 1.21 // from ANL:
279     // ??- if( !it2->status.passPre() || (!it2->isPFMuon && !it2->isGlobalMuon) ) continue;
280     if( !it2->status.passPre() ) continue;
281     if( !it2->isTight ) continue; // NOTE: isTight is set to it2->isPFMuon || it2->isGlobalMuon
282 anlevin 1.19
283 khahn 1.1 TVector3 mvec = it2->vec.Vect();
284     if ( evec.DrEtaPhi(mvec) < 0.05 ) {
285 khahn 1.3 erase_this_electron=true;
286     break;
287 khahn 1.1 }
288     }
289 khahn 1.3 if( erase_this_electron ) {
290     if( ctrl.debug ) cout << "erasing electron with pt " << it1->vec.Pt() << endl;
291     electrons_to_erase.push_back(it1);
292     }
293 khahn 1.1 }
294     for( int i=0; i<electrons_to_erase.size(); i++ ) {
295     lepvec.erase(electrons_to_erase[i]);
296     }
297    
298     //********************************************************
299 dkralph 1.21 // Step 3: Good Leptons (remove non-id-and-pre'd leptons)
300 khahn 1.1 //********************************************************
301 khahn 1.3 vector<double> pt_of_leptons_to_erase;
302     for (int i=0; i<lepvec.size(); i++ ) {
303     bool already_pushed=false;
304 khahn 1.7 if( !(lepvec[i].status.looseIDAndPre()) ) {
305 khahn 1.3 pt_of_leptons_to_erase.push_back(lepvec[i].vec.Pt());
306     already_pushed = true;
307 khahn 1.1 if(ctrl.debug)
308 khahn 1.3 cout << "pushing failed lepton type: " << lepvec[i].type
309     << "\tpt: " << lepvec[i].vec.Pt()
310     << "\teta: " << lepvec[i].vec.Eta()
311 khahn 1.1 << endl;
312 khahn 1.3 failingLeptons.push_back(lepvec[i]); // these should pass preselection
313 khahn 1.1 } else {
314 khahn 1.3 passingLeptons.push_back(lepvec[i]);
315     }
316     if( !already_pushed && fabs(lepvec[i].ip3dSig)>4 )
317 dkralph 1.13 pt_of_leptons_to_erase.push_back(lepvec[i].vec.Pt());
318 khahn 1.3 }
319 dkralph 1.13 for( int i=0; i<pt_of_leptons_to_erase.size(); i++ ) {
320 khahn 1.3 for( vector<SimpleLepton>::iterator it=lepvec.begin();
321     it != lepvec.end(); it++ ) {
322     SimpleLepton flep = *it;
323     if( flep.vec.Pt() != pt_of_leptons_to_erase[i] ) continue;
324     if(ctrl.debug) cout << "erasing lepton : "
325     << flep.vec.Pt() << "\t"
326     << flep.type << "\t"
327     << endl;
328     lepvec.erase(it);
329     break;
330 khahn 1.1 }
331     }
332 khahn 1.3 if( ctrl.debug ) cout << "good leptons : " << lepvec.size() << endl;
333    
334 khahn 1.1 //********************************************************
335 dkralph 1.21 // Ghost Removal
336     //********************************************************
337     vector<vector<SimpleLepton>::iterator> leptons_to_erase;
338     for (vector<SimpleLepton>::iterator it1=lepvec.begin(); it1 != lepvec.end(); it1++ ) {
339     TVector3 lep1 = it1->vec.Vect();
340    
341     bool erase_this_lepton=false;
342     for (vector<SimpleLepton>::iterator it2=it1+1; it2 != lepvec.end(); it2++ ) {
343     TVector3 lep2 = it2->vec.Vect();
344    
345     if ( lep1.DrEtaPhi(lep2) <= 0.02 ) {
346     erase_this_lepton=true;
347     break;
348     }
349     }
350     if( erase_this_lepton ) {
351     if( ctrl.debug ) cout << "erasing ghost with pt " << it1->vec.Pt() << endl;
352     leptons_to_erase.push_back(it1);
353     }
354     }
355     for( int i=0; i<leptons_to_erase.size(); i++ ) {
356     lepvec.erase(leptons_to_erase[i]);
357     }
358    
359     //********************************************************
360 khahn 1.1 // Step 4: Z candidate preselection
361     //********************************************************
362 dkralph 1.21 vector<pair<int,int> > ZCandidates;
363     vector<pair<SimpleLepton,SimpleLepton> > ZCandidatesLeptons;
364     vector<pair<SelectionStatus,SelectionStatus> > ZCandidatesSelectionStatus;
365 khahn 1.1 for(int i = 0; i < lepvec.size(); ++i) {
366     for(int j = i+1; j < lepvec.size(); ++j) {
367     if( abs(lepvec[i].type) != abs(lepvec[j].type) ) continue;
368     if( lepvec[i].charge == lepvec[j].charge ) continue;
369 khahn 1.7
370     TLorentzVector Zvec = (lepvec[i].vec+lepvec[j].vec);
371    
372 khahn 1.8 vector<SimpleLepton> lepvec_i = lepvec;
373     vector<SimpleLepton> lepvec_j = lepvec;
374    
375 dkralph 1.13 if( ctrl.doFSR ) {
376 dkralph 1.21 if(ctrl.debug) cout << endl << "----------------> FSR ("<<i<<","<<j<<") <----------------------" << endl;
377 khahn 1.8 photonsToVeto.clear();
378     float old_pt_i = lepvec[i].vec.Pt();
379     float old_pt_j = lepvec[j].vec.Pt();
380     float old_M = Zvec.M();
381    
382 dkralph 1.21 if(ctrl.debug) cout << "i: " << i << endl;
383 khahn 1.7 if( abs(lepvec[i].type) == 13 ) {
384 dkralph 1.21 const Muon *mu = (Muon*)((*muonArr)[lepvec[i].index]);
385     Muon * newmu = const_cast<Muon *>(mu);
386     if( recover_typeI_Photon( ctrl, ret, newmu, i, lepvec_i, pfCandidates, electronArr, &Zvec, photonsToVeto ) )
387     if(ctrl.debug) cout << " FSR TYPEI :: oldpt: " << old_pt_i << "\tnewpt: " << lepvec_i[i].vec.Pt() << "\tindex: " << i << endl;
388     if( recover_typeII_Photon( ctrl, ret, newmu, i, lepvec_i, pfCandidates ) )
389     if(ctrl.debug) cout << " FSR TYPEII :: oldpt: " << old_pt_i << "\tnewpt: " << lepvec_i[i].vec.Pt() << "\tindex: " << i << endl;
390 khahn 1.7 } else {
391 dkralph 1.21 const Electron *el = (Electron*)((*electronArr)[lepvec[i].index]);
392     Electron* newel = const_cast<Electron*>(el);
393     if( recover_typeI_Photon( ctrl, ret, newel, i, lepvec_i, pfCandidates, electronArr, &Zvec, photonsToVeto ) )
394     if(ctrl.debug) cout << " FSR TYPEI :: oldpt: " << old_pt_i << "\tnewpt: " << lepvec_i[i].vec.Pt() << "\tindex: " << i << endl;
395 khahn 1.7 }
396    
397 dkralph 1.21 if(ctrl.debug) cout << "j: " << j << endl;
398     ChargedParticle *cp;
399 khahn 1.7 if( abs(lepvec[j].type) == 13 ) {
400 dkralph 1.21 const Muon *mu = (Muon*)((*muonArr)[lepvec[j].index]);
401     Muon * newmu = const_cast<Muon *>(mu);
402     if( recover_typeI_Photon( ctrl, ret, newmu, j, lepvec_j, pfCandidates, electronArr, &Zvec, photonsToVeto ) )
403     if(ctrl.debug) cout << " FSR TYPEI :: oldpt: " << old_pt_j << "\tnewpt: " << lepvec_j[j].vec.Pt() << "\tindex: " << j << endl;
404     if( recover_typeII_Photon( ctrl, ret, newmu, j, lepvec_j, pfCandidates ) )
405     if(ctrl.debug) cout << " FSR TYPEII :: oldpt: " << old_pt_j << "\tnewpt: " << lepvec_j[j].vec.Pt() << "\tindex: " << j << endl;
406 khahn 1.7 } else {
407 dkralph 1.21 const Electron *el = (Electron*)((*electronArr)[lepvec[j].index]);
408     Electron* newel = const_cast<Electron*>(el);
409     if( recover_typeI_Photon( ctrl, ret, newel, j, lepvec_j, pfCandidates, electronArr, &Zvec, photonsToVeto ) )
410     if(ctrl.debug) cout << " FSR TYPEI :: oldpt: " << old_pt_j << "\tnewpt: " << lepvec_j[j].vec.Pt() << "\tindex: " << j << endl;
411 khahn 1.7 }
412 khahn 1.8
413     //
414     // now fix isolation
415     //
416     if( abs(lepvec[i].type) == 11 ) {
417 dkralph 1.21 const Electron *el = (Electron*)((*electronArr)[lepvec_i[i].index]);
418 khahn 1.8 lepvec_i[i].status |=
419     (*ElectronIsoSelector)(ctrl,el,vtx,pfCandidates,puEnergyDensity,eraEle,photonsToVeto);
420    
421     } else {
422 dkralph 1.21 const Muon *mu = (Muon*)((*muonArr)[lepvec_i[i].index]);
423 khahn 1.8 lepvec_i[i].status |=
424     (*MuonIsoSelector)(ctrl,mu,vtx,pfCandidates,puEnergyDensity,eraMu,photonsToVeto);
425     }
426     updateSimpleLepton(lepvec_i[i]);
427    
428     if( abs(lepvec[j].type) == 11 ) {
429 dkralph 1.21 const Electron *el = (Electron*)((*electronArr)[lepvec_j[j].index]);
430 khahn 1.8 lepvec_j[j].status |=
431     (*ElectronIsoSelector)(ctrl,el,vtx,pfCandidates,puEnergyDensity,eraEle,photonsToVeto);
432     } else {
433 dkralph 1.21 const Muon *mu = (Muon*)((*muonArr)[lepvec_j[j].index]);
434 khahn 1.8 lepvec_j[j].status |=
435     (*MuonIsoSelector)(ctrl,mu,vtx,pfCandidates,puEnergyDensity,eraMu,photonsToVeto);
436     }
437     updateSimpleLepton(lepvec_j[j]);
438    
439     float new_M = (lepvec_i[i].vec+lepvec_j[j].vec).M();
440     float new_pt_i = lepvec_i[i].vec.Pt();
441     float new_pt_j = lepvec_j[j].vec.Pt();
442 dkralph 1.21 if( ctrl.debug ) {
443     cout << " ----> post FSR Z:";
444     cout << " oldM: " << old_M << "\tnewM:" << new_M << endl;
445     cout << " old_pt_i: " << old_pt_i << "\tnew_pt_i:" << new_pt_i << endl;
446     cout << " old_pt_j: " << old_pt_j << "\tnew_pt_j:" << new_pt_j << endl;
447 khahn 1.8 }
448    
449 khahn 1.7 } // doFSR
450 dkralph 1.21
451     if( !(lepvec_i[i].status.loose()) || !(lepvec_j[j].status.loose()) ) {
452     if(ctrl.debug) {
453     bitset<16> tmpbits_i(lepvec_i[i].status.getStatus()),tmpbits_j(lepvec_j[j].status.getStatus());
454     cout << " leptons fail selection (i: " << tmpbits_i << ", j: " << tmpbits_j << ")" << endl;
455     }
456     continue;
457     }
458     ZCandidates.push_back(pair<int,int> (i,j) );
459     ZCandidatesLeptons.push_back(pair<SimpleLepton,SimpleLepton> (lepvec_i[i],lepvec_j[j]) );
460 khahn 1.3 if( ctrl.debug ) cout << "Z candidate ("<< i << "," << j << ")"
461 khahn 1.8 << "\tmass: " << (lepvec_i[i].vec+lepvec_j[j].vec).M() << endl;
462 khahn 1.1 }
463     }
464     if( ZCandidates.size() > 0 ) {
465     ret.status.selectionBits.flip(PASS_ZCANDIDATE);
466 dkralph 1.21 if( ctrl.debug ) cout << "event has >0 Z candidates (" << ZCandidates.size() << " of em)" << endl;
467 khahn 1.1 } else {
468     ret.status.setStatus(SelectionStatus::FAIL);
469     return ret;
470     }
471    
472 khahn 1.3 //
473     // !!!!!!!!!!!!!! Z1 SELECTED HERE
474     //
475     int best_Z1_index;
476     float best_Z1_mass = 9999.;
477     TLorentzVector Z1vec;
478     for( int i=0; i<ZCandidates.size(); i++ ) {
479 khahn 1.8 // TLorentzVector tmpZ1vec = (lepvec[ZCandidates[i].first].vec) +
480     // (lepvec[ZCandidates[i].second].vec);
481     TLorentzVector tmpZ1vec = (ZCandidatesLeptons[i].first.vec) +
482     (ZCandidatesLeptons[i].second.vec);
483 khahn 1.3 if( fabs(tmpZ1vec.M()-Z_MASS) < fabs(best_Z1_mass-Z_MASS) ) {
484     best_Z1_index=i;
485     best_Z1_mass=tmpZ1vec.M();
486     Z1vec = tmpZ1vec;
487     }
488     }
489 khahn 1.8 // update the leptons, damn FSR ...
490     // ret.Z1leptons.push_back(lepvec[ZCandidates[best_Z1_index].first]);
491     // ret.Z1leptons.push_back(lepvec[ZCandidates[best_Z1_index].second]);
492     ret.Z1leptons.push_back(ZCandidatesLeptons[best_Z1_index].first);
493     ret.Z1leptons.push_back(ZCandidatesLeptons[best_Z1_index].second);
494    
495    
496 khahn 1.7 cout << "best mZ1: " << best_Z1_mass << " from (" <<
497     ZCandidates[best_Z1_index].first << "," << ZCandidates[best_Z1_index].second << ")" << endl;
498 khahn 1.8
499 khahn 1.3 //******************************************************************************
500     // Step 6.3 : require Z1 with 40<m<120
501     //******************************************************************************
502     if( Z1vec.M() > 40. && Z1vec.M() < 120. ) {
503     ret.status.selectionBits.flip(PASS_GOODZ1);
504     } else {
505     ret.status.setStatus(SelectionStatus::FAIL);
506     return ret;
507     }
508    
509     //******************************************************************************
510     // Step 6.3 : 4 good leptons
511     //******************************************************************************
512     if( lepvec.size() >= 4 ) {
513     if( ctrl.debug) cout << "pass4L: " << info->EvtNum() << endl;
514     ret.status.selectionBits.flip(PASS_4L);
515     } else {
516     ret.status.setStatus(SelectionStatus::FAIL);
517     return ret;
518     }
519     int nEl=0, nMu=0;
520     for( int i=0; i<4; i++ ) {
521     if(abs(lepvec[i].type) == 11 ) nEl++;
522     if(abs(lepvec[i].type) == 13 ) nMu++;
523     }
524    
525 khahn 1.1 //********************************************************
526     // Step 5: ZZ candidates
527     //********************************************************
528     int nZZCandidates=0;
529 dkralph 1.21 vector<pair<int,int> > ZZCandidates;
530 khahn 1.3 int Z2type;
531 dkralph 1.21 if(ctrl.debug) cout << "looping over " << ZCandidates.size() << " Z candidates: " << endl;
532 khahn 1.3 for(int z2index=0; z2index<ZCandidates.size(); ++z2index) {
533     int z1index = best_Z1_index;
534 dkralph 1.21 if ( z2index == z1index ) { if(ctrl.debug) cout << " Z2 candidate failing z2i = z1i" << endl; continue; }
535     if( ZCandidates[z1index].first == ZCandidates[z2index].first ) { if(ctrl.debug) cout << " Z2 candidate failing icheck1" << endl; continue; }
536     if( ZCandidates[z1index].first == ZCandidates[z2index].second ) { if(ctrl.debug) cout << " Z2 candidate failing icheck2" << endl; continue; }
537     if( ZCandidates[z1index].second == ZCandidates[z2index].first ) { if(ctrl.debug) cout << " Z2 candidate failing icheck3" << endl; continue; }
538     if( ZCandidates[z1index].second == ZCandidates[z2index].second ) { if(ctrl.debug) cout << " Z2 candidate failing icheck4" << endl; continue; }
539 anlevin 1.14
540 dkralph 1.21 ZZCandidates.push_back(pair<int,int> (z1index,z2index));
541 khahn 1.8 Z2type = abs(ZCandidatesLeptons[z2index].first.type);
542 khahn 1.1 }
543     if( ZZCandidates.size() > 0 ) {
544 khahn 1.3 if( ctrl.debug) cout << "passZZ: " << info->EvtNum() << endl;
545 khahn 1.1 ret.status.selectionBits.flip(PASS_ZZCANDIDATE);
546 khahn 1.3 if( ZZCandidates.size() > 1 ) Z2type=999;
547 khahn 1.1 if( ctrl.debug ) cout << "nZZcandidates: " << ZZCandidates.size() << endl;
548 dkralph 1.21 if( ctrl.debug ) {
549     cout << "evt: " << info->EvtNum() << "\tnZZcandidates: " << ZZCandidates.size()
550     << "\tZ1f: " << abs(ret.Z1leptons[0].type) << "\tZ2f: " << Z2type << endl;
551     cout << "------------------------------------------------------- ( un-fsrd )" << endl;
552 khahn 1.3 for( int l=0; l<lepvec.size(); l++ ) lepvec[l].print();
553     cout << "-------------------------------------------------------" << endl;
554 dkralph 1.21 }
555 khahn 1.1 } else {
556     ret.status.setStatus(SelectionStatus::FAIL);
557     return ret;
558     }
559    
560 khahn 1.3 //
561     // !!!!!!!!!!!!!! Z2 SELECTED HERE
562     //
563     int best_Z2_index;
564     float best_Z2_pt = -1.;
565 khahn 1.1 for( int i=0; i<ZZCandidates.size(); i++ ) {
566 khahn 1.3 int z2index = ZZCandidates[i].second;
567 khahn 1.8 TLorentzVector Z2 = (ZCandidatesLeptons[z2index].first.vec) +
568     (ZCandidatesLeptons[z2index].second.vec);
569 khahn 1.3 if( Z2.Pt() > best_Z2_pt ) {
570     best_Z2_index=z2index;
571     best_Z2_pt=Z2.Pt();
572 khahn 1.1 }
573     }
574 khahn 1.8 // update the leptons, damn FSR ...
575     // ret.Z2leptons.push_back(lepvec[ZCandidates[best_Z2_index].first]);
576     // ret.Z2leptons.push_back(lepvec[ZCandidates[best_Z2_index].second]);
577     ret.Z2leptons.push_back(ZCandidatesLeptons[best_Z2_index].first);
578     ret.Z2leptons.push_back(ZCandidatesLeptons[best_Z2_index].second);
579    
580 anlevin 1.18 int theZ1type = abs(ZCandidatesLeptons[best_Z1_index].first.type);
581     int theZ2type = abs(ZCandidatesLeptons[best_Z2_index].first.type);
582 khahn 1.8
583 dkralph 1.21 if(ctrl.debug) cout << "best mZ2: " << (ret.Z2leptons[0].vec+ret.Z2leptons[1].vec).M() << " from ("
584     << ZCandidates[best_Z2_index].first << "," << ZCandidates[best_Z2_index].second << ")" << endl;
585    
586 anlevin 1.18 increment("sfOsHiPt",theZ1type,theZ2type);
587 khahn 1.1
588 dkralph 1.21 if(ctrl.debug) cout << "ZZ :: evt: " << info->EvtNum()
589     << "\tmZ1: " << (ret.Z1leptons[0].vec+ret.Z1leptons[1].vec).M()
590     << "\tmZ2: " << (ret.Z2leptons[0].vec+ret.Z2leptons[1].vec).M()
591     << endl;
592    
593 khahn 1.7
594 khahn 1.1 //******************************************************************************
595 khahn 1.3 // Step 6.4 : require Z2 with 4<m<120
596 khahn 1.1 //******************************************************************************
597 khahn 1.8 TLorentzVector Z2vec = (ZCandidatesLeptons[best_Z2_index].first.vec) +
598     (ZCandidatesLeptons[best_Z2_index].second.vec);
599 khahn 1.3 if( Z2vec.M() > 4 && Z2vec.M() < 120 ) {
600     ret.status.selectionBits.flip(PASS_GOODZ2);
601 anlevin 1.18 increment("4<mZ2<120",theZ1type,theZ2type);
602 khahn 1.1 } else {
603     ret.status.setStatus(SelectionStatus::FAIL);
604     return ret;
605     }
606    
607 dkralph 1.21
608 khahn 1.1 //******************************************************************************
609 khahn 1.3 // Step 6.1 : any two leptons 20/10
610 khahn 1.1 //******************************************************************************
611 khahn 1.8 vector<SimpleLepton> zzleptons;
612     zzleptons.push_back( ZCandidatesLeptons[best_Z1_index].first );
613     zzleptons.push_back( ZCandidatesLeptons[best_Z1_index].second );
614     zzleptons.push_back( ZCandidatesLeptons[best_Z2_index].first );
615     zzleptons.push_back( ZCandidatesLeptons[best_Z2_index].second );
616 anlevin 1.19 int nlep_above_10=0,nlep_above_20=0;
617 khahn 1.3 for( int i=0; i<zzleptons.size(); i++ ) {
618 khahn 1.8 if( zzleptons[i].vec.Pt() > 10 ) nlep_above_10++;
619     if( zzleptons[i].vec.Pt() > 20 ) nlep_above_20++;
620 khahn 1.1 }
621 khahn 1.3 if( nlep_above_10 > 1 && nlep_above_20 > 0 ) {
622     ret.status.selectionBits.flip(PASS_ZZ_20_10);
623 anlevin 1.18 increment("pt>20,10",theZ1type,theZ2type);
624 khahn 1.3 if( ctrl.debug ) cout << "passess 20/10 ..." << endl;
625 khahn 1.1 } else {
626     ret.status.setStatus(SelectionStatus::FAIL);
627     return ret;
628     }
629 khahn 1.3
630 dkralph 1.21
631    
632    
633 khahn 1.1 //******************************************************************************
634 khahn 1.7 // Step 6.5 : resonance killing (4/4)
635 khahn 1.1 //******************************************************************************
636 khahn 1.3 bool resonance = false;
637     for( int i=0; i<zzleptons.size(); i++ ) {
638     for( int j=i+1; j<zzleptons.size(); j++ ) {
639 khahn 1.8 if( zzleptons[i].charge == zzleptons[j].charge ) continue; // 4/4
640 dkralph 1.21 if( (zzleptons[i].vec+zzleptons[j].vec).M() <= 4. ) {
641 khahn 1.3 resonance = true;
642     break;
643 khahn 1.1 }
644     }
645     }
646 khahn 1.3 if( !resonance ) {
647 khahn 1.1 ret.status.selectionBits.flip(PASS_RESONANCE);
648 anlevin 1.18 increment("mll>4",theZ1type,theZ2type);
649 khahn 1.1 if( ctrl.debug ) cout << "\tpasses resonance killing ... " << endl;
650     } else {
651     ret.status.setStatus(SelectionStatus::FAIL);
652     return ret;
653 dkralph 1.21 }
654    
655    
656 khahn 1.3
657 khahn 1.1 //******************************************************************************
658 khahn 1.3 // Step 6.6 : m(4l) > 70 , m(4l) > 100
659 khahn 1.1 //******************************************************************************
660 khahn 1.8 TLorentzVector zzvec = (ZCandidatesLeptons[best_Z1_index].first.vec) +
661     (ZCandidatesLeptons[best_Z1_index].second.vec) +
662     (ZCandidatesLeptons[best_Z2_index].first.vec) +
663     (ZCandidatesLeptons[best_Z2_index].second.vec);
664 khahn 1.3
665 dkralph 1.21 if(ctrl.debug)
666     cout << "forming zz from: "
667     << setw(9) << (ZCandidatesLeptons[best_Z1_index].first.vec).Pt()
668     << setw(9) << (ZCandidatesLeptons[best_Z1_index].second.vec).Pt()
669     << setw(9) << (ZCandidatesLeptons[best_Z2_index].first.vec).Pt()
670     << setw(9) << (ZCandidatesLeptons[best_Z2_index].second.vec).Pt() << endl;
671    
672 dkralph 1.13 if( zzvec.M() > 70. ) {
673     if(ctrl.debug) cout << "passes mzz > 70, mzz: " << zzvec.M() << endl;
674 khahn 1.3 ret.status.selectionBits.flip(PASS_m4l_GT_70);
675 anlevin 1.18 increment("m4l>70",theZ1type,theZ2type);
676 khahn 1.3 } else {
677     ret.status.setStatus(SelectionStatus::FAIL);
678     return ret;
679 khahn 1.1 }
680 dkralph 1.21
681     if( (ret.Z2leptons[0].vec+ret.Z2leptons[1].vec).M() > 12 ){
682     if(ctrl.debug) cout << "passes mZ2 > 12" << endl;
683     increment("mZ2>12",theZ1type,theZ2type);
684 anlevin 1.20 } else {
685     ret.status.setStatus(SelectionStatus::FAIL);
686     return ret;
687     }
688    
689 dkralph 1.21 if( zzvec.M() > 100 ) {
690     if(ctrl.debug) cout << "passes mzz > 100, mzz: " << zzvec.M() << endl;
691     increment("m4l>100",theZ1type,theZ2type);
692     } else {
693     cout << "NOTE: failed m4l>100 (" << zzvec.M() << "), but saving to ntuple anyway" << endl;
694     }
695 khahn 1.4
696     //***************************************************************
697     // finish
698     //***************************************************************
699 khahn 1.1
700 khahn 1.8 TLorentzVector theZ1 = (ZCandidatesLeptons[best_Z1_index].first.vec) +
701     (ZCandidatesLeptons[best_Z1_index].second.vec);
702     TLorentzVector theZ2 = (ZCandidatesLeptons[best_Z2_index].first.vec) +
703     (ZCandidatesLeptons[best_Z2_index].second.vec);
704 khahn 1.1 TLorentzVector theZZ = theZ1 + theZ2;
705 khahn 1.3
706 dkralph 1.21 if(ret.fsrPhotons.size() > 0) {
707     if(ctrl.debug)
708     cout << " fsr photon! pt: "
709     << setw(12) << ret.fsrPhotons[0].vec.Pt()
710     << " eta: " << setw(12) << ret.fsrPhotons[0].vec.Eta()
711     << " phi: " << setw(12) << ret.fsrPhotons[0].vec.Phi() << endl;
712     }
713     if(ret.fsrPhotons.size() > 1) {
714     if(ctrl.debug)
715     cout << " fsr photon! pt: "
716     << setw(12) << ret.fsrPhotons[1].vec.Pt()
717     << " eta: " << setw(12) << ret.fsrPhotons[1].vec.Eta()
718     << " phi: " << setw(12) << ret.fsrPhotons[1].vec.Phi() << endl;
719     }
720 khahn 1.1 if( ctrl.debug ) cout << "run: " << info->RunNum()
721     << "\tevt: " << info->EvtNum()
722     << "\tZ1channel: " << theZ1type
723     << "\tZ2channel: " << theZ2type
724     << "\tmZ1: " << theZ1.M()
725     << "\tmZ2: " << theZ2.M()
726     << "\tm4l: " << theZZ.M()
727     << endl;
728 khahn 1.3
729 dkralph 1.21 ret.status.setStatus(SelectionStatus::EVTPASS);
730 khahn 1.3
731 khahn 1.1 return ret;
732     }
733 khahn 1.7 //----------------------------------------------------------------------------
734     void updateSimpleLepton(SimpleLepton &tmplep)
735     //----------------------------------------------------------------------------
736     {
737     tmplep.isTight = tmplep.status.tight();
738     tmplep.isLoose = tmplep.status.loose();
739     }