ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitHzz4l/Selection/src/Selection.cc
(Generate patch)

Comparing UserCode/MitHzz4l/Selection/src/Selection.cc (file contents):
Revision 1.9 by khahn, Fri Oct 14 11:38:10 2011 UTC vs.
Revision 1.23 by khahn, Tue May 1 16:39:10 2012 UTC

# Line 1 | Line 1
1 + #include "SelectionStatus.h"
2 + #include "EventData.h"
3 + #include "SimpleLepton.h"
4 + #include "EfficiencyWeightsInterface.h"
5 +
6 + #include "ElectronSelection.h"
7 + #include "MuonSelection.h"
8 + #include "IsolationSelection.h"
9 + //#include "PassHLT.h"
10   #include "Selection.h"
2 #include "PassHLT.h"
11  
12 < #include "SiMVAElectronSelection.h"
12 > #include "ExternData.h"
13 > #include "SelectionDefs.h"
14  
15 < #include "HZZCiCElectronSelection.h"
16 < #include "HZZLikelihoodElectronSelection.h"
17 < #include "HZZBDTElectronSelection.h"
18 < #include "RunLumiRangeMap.h"
19 <
20 < RunLumiRangeMap rlrm;
21 <
22 < void initRunLumiRangeMap() {
23 <  rlrm.AddJSONFile(std::string("./data/Cert_136033-149442_7TeV_Apr21ReReco_Collisions10_JSON.txt"));
24 <  rlrm.AddJSONFile(std::string("./data/Cert_160404-173244_7TeV_PromptReco_Collisions11_JSON_v2.txt"));
25 <  rlrm.AddJSONFile(std::string("./data/Cert_160404-163869_7TeV_May10ReReco_Collisions11_JSON_v3.txt"));  
26 <  rlrm.AddJSONFile(std::string("./data/Cert_170249-172619_7TeV_ReReco5Aug_Collisions11_JSON.txt"));  
27 < };
28 <
29 < unsigned fails_HZZ4L_selection(ControlFlags &ctrl,           // input control
30 <                               mithep::TEventInfo *info,     // input event inof
31 <                               TClonesArray *electronArr,    // input electrons
32 <                               TClonesArray *muonArr,        // input muons
33 <                               double eventweight,           // weight
34 <                               TTree * passtuple ) {
35 <
36 <  fails_HZZ4L_selection( ctrl, info, electronArr, muonArr, eventweight, passtuple, NULL );
37 <
38 < };
39 <
40 < unsigned fails_HZZ4L_selection(ControlFlags &ctrl,           // input control
41 <                               mithep::TEventInfo *info,     // input event inof
42 <                               TClonesArray *electronArr,    // input electrons
43 <                               TClonesArray *muonArr,        // input muons
44 <                               double eventweight,           // weight
45 <                               LabVectors  *l ) {
46 <
38 <  fails_HZZ4L_selection( ctrl, info, electronArr, muonArr, eventweight, NULL, l );
39 <
40 < };
41 <
42 <
43 < unsigned fails_HZZ4L_selection(ControlFlags &ctrl,           // input control
44 <                               mithep::TEventInfo *info,     // input event inof
45 <                               TClonesArray *electronArr,    // input electrons
46 <                               TClonesArray *muonArr,        // input muons
47 <                               double eventweight,           // weight
48 <                               TTree * passtuple,
49 <                               LabVectors * l) {       // output ntuple
15 > //--------------------------------------------------------------------------------------------------
16 > void fillVetoArrays( ControlFlags & ctrl,
17 >                     const mithep::Array<mithep::Muon> *muonArr,    
18 >                     vector< const mithep::Muon*>     & muonsToVeto,
19 >                     const mithep::Array<mithep::Electron> *electronArr,    
20 >                     vector< const mithep::Electron*> & electronsToVeto,
21 >                     const mithep::Vertex & vtx )
22 > //--------------------------------------------------------------------------------------------------
23 > {
24 >  if( ctrl.debug ) cout << "looping for isolation ..." << endl;
25 >  for(int i=0; i<muonArr->GetEntries(); i++)
26 >    {
27 >      const mithep::Muon *mu = (const mithep::Muon*)((*muonArr)[i]);      
28 >      SelectionStatus musel;
29 >      //      musel |= muonCutBasedVeto(ctrl,mu,vtx);
30 >      musel |= muonDummyVeto(ctrl,mu,vtx);
31 >      if( !(musel.getStatus() & SelectionStatus::PRESELECTION) ) continue;
32 >      if(ctrl.debug) cout << "pushing mu for isol veto ... " << endl;
33 >      muonsToVeto.push_back( mu );
34 >    }
35 >  for(int i=0; i<electronArr->GetEntries(); i++)
36 >    {
37 >      const mithep::Electron *ele = (const mithep::Electron*)((*electronArr)[i]);      
38 >      SelectionStatus esel;
39 >      //      esel |= electronCutBasedVeto(ctrl,ele,vtx);
40 >      esel |= electronDummyVeto(ctrl,ele,vtx);
41 >      if( !(esel.getStatus() & SelectionStatus::PRESELECTION) ) continue;
42 >      if(ctrl.debug) cout << "pushing ele for isol veto ... " << endl;
43 >      electronsToVeto.push_back( ele );
44 >    }
45 >  if( ctrl.debug ) cout << "done selecting for isolation veto ..." << endl << endl;;
46 > }
47  
51  unsigned evtfail = 0x0;
48  
49 + //--------------------------------------------------------------------------------------------------
50 + EventData apply_HZZ4L_selection(ControlFlags &ctrl,           // input control
51 +                                const mithep::EventHeader *info,     // input event info
52 +                                const mithep::Vertex & vtx,
53 +                                const mithep::Array<mithep::PFCandidate>  *pfCandidates,
54 +                                const mithep::Array<mithep::PileupEnergyDensity>  *puEnergyDensity,
55 +                                const mithep::Array<mithep::Electron> *electronArr,    // input electrons
56 +                                SelectionStatus (*ElectronPreSelector)( ControlFlags &,
57 +                                                                        const mithep::Electron*,
58 +                                                                        const mithep::Vertex &),
59 +                                SelectionStatus (*ElectronIDSelector)( ControlFlags &,
60 +                                                                       const mithep::Electron*,
61 +                                                                       const mithep::Vertex &),
62 +                                SelectionStatus (*ElectronIsoSelector)( ControlFlags &,
63 +                                                                        const mithep::Electron*,
64 +                                                                        const mithep::Vertex &,
65 +                                                                        const mithep::Array<mithep::PFCandidate> *,
66 +                                                                        const mithep::Array<mithep::PileupEnergyDensity> *,
67 +                                                                        mithep::ElectronTools::EElectronEffectiveAreaTarget,
68 +                                                                        vector<const mithep::Muon*>,
69 +                                                                        vector<const mithep::Electron*> ),
70 +                                const mithep::Array<mithep::Muon> *muonArr,    // input muons
71 +                                SelectionStatus (*MuonPreSelector)( ControlFlags &,
72 +                                                                    const mithep::Muon*,
73 +                                                                    const mithep::Vertex &,
74 +                                                                    const mithep::Array<mithep::PFCandidate> *),
75 +                                SelectionStatus (*MuonIDSelector)( ControlFlags &,
76 +                                                                   const mithep::Muon*,
77 +                                                                   // const mithep::Vertex &),
78 +                                                                   const mithep::Vertex &,
79 +                                                                   const mithep::Array<mithep::PFCandidate> *),
80 +                                SelectionStatus (*MuonIsoSelector)( ControlFlags &,
81 +                                                                    const mithep::Muon*,
82 +                                                                    const mithep::Vertex &,
83 +                                                                    const mithep::Array<mithep::PFCandidate> *,
84 +                                                                    const mithep::Array<mithep::PileupEnergyDensity> *,
85 +                                                                    mithep::MuonTools::EMuonEffectiveAreaTarget,
86 +                                                                    vector<const mithep::Muon*>,
87 +                                                                    vector<const mithep::Electron*> )
88 +                                )
89 + //--------------------------------------------------------------------------------------------------
90 + {      
91  
92 +  EventData ret;
93 +  unsigned evtfail = 0x0;
94 +  TRandom3 r;
95 +  
96    if( ctrl.debug ) {
97 <    cout << "Run: " << info->runNum
98 <         << "\tEvt: " << info->evtNum
99 <         << "\tLumi: " << info->lumiSec
97 >    cout << "-----------------------------------------------------------------" << endl;
98 >    cout << "-----------------------------------------------------------------" << endl;
99 >    cout << "Run: " << info->RunNum()
100 >         << "\tEvt: " << info->EvtNum()
101 >         << "\tLumi: " << info->LumiSec()
102           << endl;
103 +    cout << "-----------------------------------------------------------------" << endl;
104    }
105  
106    if( !ctrl.mc ) {
107      // not accounting for overlap atm
108 <    RunLumiRangeMap::RunLumiPairType rl(info->runNum, info->lumiSec);      
108 >    RunLumiRangeMap::RunLumiPairType rl(info->RunNum(), info->LumiSec());      
109      if( !(rlrm.HasRunLumi(rl)) )  {
110        if( ctrl.debug ) cout << "\tfails JSON" << endl;
111 <      evtfail |= (1<<EVTFAIL_JSON);
112 <      return evtfail;
111 >      ret.status.setStatus(0);
112 >      return ret;
113      }
114    }
70  
71  
115  
116 +  mithep::MuonTools::EMuonEffectiveAreaTarget         eraMu  = mithep::MuonTools::kMuEAFall11MC;
117 +  mithep::ElectronTools::EElectronEffectiveAreaTarget eraEle = mithep::ElectronTools::kEleEAFall11MC;
118 +  if( !ctrl.mc ) {
119 +    eraMu  = mithep::MuonTools::kMuEAData2011;
120 +    eraEle = mithep::ElectronTools::kEleEAData2011;
121 +  }
122    
123    
124    //********************************************************
125    // Trigger
126    //********************************************************
127 +  //
128 +  // still have to port this part to bambu
129 +  //
130 +  /*
131    if( !ctrl.mc ) {
132 <    //  if( !(passHLT(info->triggerBits, info->runNum, channel) )  ) {
132 >    //if( !(passHLT(info->triggerBits, info->runNum, channel) )  ) {
133      if( !(passHLT(info->triggerBits, info->runNum, 999) )  ) {
134 +      if( ctrl.debug ) cout << "\tfails trigger" << endl;
135        evtfail |= (1<<EVTFAIL_TRIGGER);
136 <      return evtfail;
136 >      ret.status.setStatus(0);
137 >      return ret;
138      }  
139 <  } else {
140 <    if( !(passHLTMC(info->triggerBits)) ) {
86 <      evtfail |= (1<<EVTFAIL_TRIGGER);
87 <      return evtfail;
88 <    }      
89 <    //    cout << "MC trigger bits: " << hex << info->triggerBits << dec << endl;
90 <  }
139 >  }
140 >  */
141  
142    if( ctrl.debug ) {
143      cout << "presel nlep: " << muonArr->GetEntries() + electronArr->GetEntries()
# Line 96 | Line 146 | unsigned fails_HZZ4L_selection(ControlFl
146           << endl;
147    }
148  
149 +
150    //********************************************************
151    // Lepton Selection
152    //********************************************************
153    vector<SimpleLepton> lepvec;
154 <  
154 >
155 >  vector<const mithep::Muon*> muonsToVeto;
156 >  vector<const mithep::Electron*> electronsToVeto;
157 >  fillVetoArrays( ctrl, muonArr, muonsToVeto, electronArr, electronsToVeto, vtx );
158 >
159    //    
160    if( ctrl.debug ) cout << "\tnMuons: " << muonArr->GetEntries() << endl;
161    //----------------------------------------------------
162 <  for(Int_t i=0; i<muonArr->GetEntries(); i++) {
163 <    const mithep::TMuon *mu = (mithep::TMuon*)((*muonArr)[i]);      
164 <    unsigned muonfail = passMuonSelectionZZ(mu);
165 <    if( ctrl.debug ) {
166 <      cout << "muon:: pt: " << mu->pt
167 <           << "\teta: " << mu->eta
168 <           << "\tmask: 0x" << hex << muonfail << dec
169 <           << endl;
170 <    }
171 <    if ( !muonfail ) {
172 <      SimpleLepton tmplep;
173 <      tmplep.vec.SetPtEtaPhiM(mu->pt,
174 <                              mu->eta,
175 <                              mu->phi,
176 <                              105.658369e-3);
177 <      tmplep.type    = 13;
178 <      tmplep.index   = i;
179 <      tmplep.charge  = mu->q;
180 <      tmplep.isoTrk  = mu->trkIso03;
181 <      tmplep.isoEcal = mu->emIso03;
182 <      tmplep.isoHcal = mu->hadIso03;
183 <      tmplep.isoPF04 = mu->pfIso04;
184 <      tmplep.ip3dSig = mu->ip3dSig;
185 <      tmplep.is4l    = false;
186 <      tmplep.isEB    = (fabs(mu->eta) < 1.479 ? 1 : 0 );
187 <      lepvec.push_back(tmplep);
188 <      if( ctrl.debug ) { cout << "muon passes ... " << endl;}
189 <    }
190 <  }
162 >  for(Int_t i=0; i<muonArr->GetEntries(); i++)
163 >    {
164 >      const mithep::Muon *mu = (mithep::Muon*)((*muonArr)[i]);      
165 >      
166 >      SelectionStatus musel;
167 >      if(ctrl.debug) cout << "musel.status  before anything: " << musel.getStatus() << endl;
168 >      musel |= (*MuonPreSelector)(ctrl,mu,vtx,pfCandidates);
169 >      if(ctrl.debug) cout << "musel.status  after presel: " << musel.getStatus() << endl;
170 >      if( !(musel.getStatus() & SelectionStatus::PRESELECTION) ) continue;
171 >      //      musel |= (*MuonIDSelector)(ctrl,mu,vtx );
172 >      musel |= (*MuonIDSelector)(ctrl,mu,vtx,pfCandidates );
173 >      if(ctrl.debug) cout << "musel.status  after ID: " << musel.getStatus() << endl;
174 >      musel |= (*MuonIsoSelector)(ctrl,mu,vtx,pfCandidates,puEnergyDensity,eraMu,muonsToVeto,electronsToVeto);
175 >      if(ctrl.debug) cout << "musel.status  after iso: " << musel.getStatus() << endl;
176 >
177 >      if( ctrl.debug ) {
178 >        cout << "muon:: pt: " << mu->Pt()
179 >             << "\teta: " << mu->Eta()
180 >             << "\tstatus: " << hex << musel.getStatus() << dec
181 >             << endl;
182 >      }
183 >      
184 >      if ( musel.pass() ) {
185 >        
186 >        SimpleLepton tmplep;
187 >        float pt = mu->Pt();
188 >        tmplep.vecorig->SetPtEtaPhiM(pt,
189 >                                     mu->Eta(),
190 >                                     mu->Phi(),
191 >                                     MUON_MASS);
192 >        
193 >        if( ctrl.do_escale_up ) {
194 >          pt=scale_smear_muon_Up(pt, 1,  r);
195 >        }
196 >        if( ctrl.do_escale_down ) {
197 >          pt=scale_smear_muon_Down(pt, 1,  r);
198 >        }
199 >        
200 >        tmplep.vec->SetPtEtaPhiM(pt,
201 >                                 mu->Eta(),
202 >                                 mu->Phi(),
203 >                                 MUON_MASS);
204 >        
205 >        tmplep.type    = 13;
206 >        tmplep.index   = i;
207 >        tmplep.charge  = mu->Charge();
208 >        tmplep.isoTrk  = mu->IsoR03SumPt();
209 >        tmplep.isoEcal = mu->IsoR03EmEt();
210 >        tmplep.isoHcal = mu->IsoR03HadEt();
211 >        tmplep.isoPF03 = computePFMuonIso(mu,vtx,pfCandidates,0.3);
212 >        tmplep.isoPF04 = computePFMuonIso(mu,vtx,pfCandidates,0.4);
213 >        tmplep.ip3dSig = mu->Ip3dPVSignificance();
214 >        tmplep.is4l    = false;
215 >        tmplep.isEB    = (fabs(mu->Eta()) < 1.479 ? 1 : 0 );
216 >        tmplep.isTight = musel.tight();
217 >        tmplep.isLoose = musel.loose();
218 >        lepvec.push_back(tmplep);
219 >        if( ctrl.debug ) { cout << "muon passes ... " << endl;}
220 >      }
221 >      //  }
222 >    }    
223 >  
224    
225 <  if( ctrl.debug ) { cout << "\tnElectron: " << electronArr->GetEntries() << endl; }
225 >    
226 >    //
227 >    if( ctrl.debug ) { cout << "\tnElectron: " << electronArr->GetEntries() << endl; }
228 >    // --------------------------------------------------------------------------------
229 >    for(Int_t i=0; i<electronArr->GetEntries(); i++)
230 >      {
231 >        const mithep::Electron *ele = (mithep::Electron*)((*electronArr)[i]);
232 >        
233 >        Bool_t isMuonOverlap = kFALSE;
234 >        for (int k=0; k<lepvec.size(); ++k) {
235 >          TVector3 tmplep;
236 >          tmplep.SetPtEtaPhi(ele->Pt(), ele->Eta(), ele->Phi());
237 >          if ( lepvec[k].isLoose && lepvec[k].type == 13 && lepvec[k].vec->Vect().DrEtaPhi(tmplep) < 0.1 ) {
238 >            if( ctrl.debug ) cout << "-----> isMuonOverlap! " << endl;
239 >            isMuonOverlap = kTRUE;
240 >            break;
241 >          }
242 >        }
243 >        
244 >        SelectionStatus elesel;
245 >        if( ctrl.debug ) cout << "--> status before anything: " << hex << elesel.getStatus() << dec << endl;
246 >        elesel |= (*ElectronPreSelector)(ctrl,ele,vtx);
247 >        if( ctrl.debug ) cout << "--> status after presel: " << hex << elesel.getStatus() << dec << endl;
248 >        elesel |= (*ElectronIDSelector)(ctrl,ele,vtx);
249 >        if( ctrl.debug ) cout << "--> status after ID: " << hex << elesel.getStatus() << dec << endl;
250 >        elesel |= (*ElectronIsoSelector)(ctrl,ele,vtx,pfCandidates,puEnergyDensity,eraEle,muonsToVeto,electronsToVeto);
251 >        if( ctrl.debug ) cout << "--> status after iso: " << hex << elesel.getStatus() << dec << endl;
252 >        
253 >        if( ctrl.debug ){
254 >          cout << "\tscEt: " << ele->SCluster()->Et()
255 >               << "\tscEta: " << ele->SCluster()->Eta()
256 >               << "\tstatus: " << hex << elesel.getStatus() << dec
257 >               << endl;
258 >        }
259  
260 <  //----------------------------------------------------
261 <  for(Int_t i=0; i<electronArr->GetEntries(); i++) {
262 <    const mithep::TElectron *ele = (mithep::TElectron*)((*electronArr)[i]);
260 >        if ( elesel.pass() && !isMuonOverlap )
261 >          {
262 >            SimpleLepton tmplep;
263 >            
264 >            float pt = ele->Pt();
265 >            tmplep.vecorig->SetPtEtaPhiM( pt,
266 >                                          ele->Eta(),
267 >                                          ele->Phi(),
268 >                                          ELECTRON_MASS );
269 >            
270 >            if( ctrl.do_escale ) {
271 >              pt=scale_smear_electron(pt, ele->IsEB(), r);
272 >            }
273 >            if( ctrl.do_escale_up ) {
274 >              pt=scale_smear_electron_Up(pt, ele->IsEB(),  r);
275 >            }
276 >            if( ctrl.do_escale_down ) {
277 >              pt=scale_smear_electron_Down(pt, ele->IsEB(),  r);
278 >            }
279  
280 <    Bool_t isMuonOverlap = kFALSE;
281 <    for (int k=0; k<lepvec.size(); ++k) {
282 <      TVector3 tmplep;
283 <      tmplep.SetPtEtaPhi(ele->pt, ele->eta, ele->phi);
284 <      if ( lepvec[k].type == 13 && lepvec[k].vec.Vect().DrEtaPhi(tmplep) < 0.1 ) {
285 <        if( ctrl.debug ) cout << "-----> isMuonOverlap! " << endl;
286 <        isMuonOverlap = kTRUE;
287 <        break;
280 >            
281 >            tmplep.vec->SetPtEtaPhiM( pt,
282 >                                      ele->Eta(),
283 >                                      ele->Phi(),
284 >                                      ELECTRON_MASS );
285 >            
286 >            tmplep.type    = 11;
287 >            tmplep.index   = i;
288 >            tmplep.charge  = ele->Charge();
289 >            tmplep.isoTrk  = ele->TrackIsolationDr03();
290 >            tmplep.isoEcal = ele->EcalRecHitIsoDr03();
291 >            tmplep.isoHcal = ele->HcalTowerSumEtDr03();
292 >            tmplep.isoPF03 = computePFEleIso(ele,vtx,pfCandidates,0.3);
293 >            tmplep.isoPF04 = computePFEleIso(ele,vtx,pfCandidates,0.4);
294 >            tmplep.ip3dSig = ele->Ip3dPVSignificance();
295 >            tmplep.is4l    = false;
296 >            tmplep.isEB    = ele->IsEB();
297 >            tmplep.scID    = ele->SCluster()->GetUniqueID();
298 >            tmplep.isTight = elesel.tight();
299 >            tmplep.isLoose = elesel.loose();
300 >            lepvec.push_back(tmplep);
301 >            if( ctrl.debug ) { cout << "\telectron passes ... " << endl; }
302 >          }
303        }
304 +    
305 +    
306 +    //********************************************************
307 +    // Dump Stuff
308 +    //********************************************************
309 +    sort( lepvec.begin(), lepvec.end(), SimpleLepton::lep_pt_sort );
310 +    int nmu=0, nele=0;
311 +    for( int i=0; i<lepvec.size(); i++ ) {
312 +      if(ctrl.debug) cout << "lepvec :: index: " << i
313 +                          << "\tpt: " << lepvec[i].vec->Pt()
314 +                          << "\ttype: " << lepvec[i].type
315 +                          << endl;
316 +      if( abs(lepvec[i].type) == 11 ) nele++;
317 +      else nmu++;
318      }
319 <
320 <    unsigned FAIL=0;
321 <    CICStruct ciccuts = getCiCCuts(ctrl.eleSeleScheme);
322 <    unsigned  failsCIC=0;
157 <    if(ctrl.eleSele=="cic") {
158 <      failsCIC = failsCicSelection(ctrl, ele, ciccuts, ctrl.kinematics);
159 <      FAIL = failsCIC;
160 <    }
161 <    LikStruct likcuts;
162 <    unsigned failsLike=0;
163 <    if(ctrl.eleSele=="lik") {
164 <      likcuts = getLikCuts(ctrl.eleSeleScheme);
165 <      failsLike = failsLikelihoodSelection(ele, likcuts, ctrl.kinematics);
166 <      FAIL = failsLike;
167 <    }
168 <    unsigned  failsBDT=0;
169 <    if(ctrl.eleSele=="bdt") {
170 <      failsBDT = failsBDTSelection(ctrl,ele);
171 <      FAIL = failsBDT;
172 <    }
173 <    unsigned  failsSi=0;
174 <    if(ctrl.eleSele=="si") {
175 <      failsSi = failsSiMVAElectronSelection(ctrl, ele, 0.95, ctrl.kinematics);
176 <      FAIL = failsSi;
177 <    }
178 <
179 <
180 <
181 <
182 <    if( ctrl.debug ){
183 <      cout << "CIC category: " << cicCategory(ele)
184 <           << "\tlikelihood: " << ele->likelihood
185 <           << "\tFAIL:  0x"     << hex << FAIL      << dec
186 <           << "\tfailsCIC:  0x" << hex << failsCIC  << dec
187 <           << "\tfailsLike: 0x" << hex << failsLike << dec
188 <           << "\tfailsBDT:  0x" << hex << failsBDT << dec
189 <           << "\tscEt: " << ele->scEt
190 <           << "\tscEta: " << ele->scEta
191 <           << "\tncluster: " << ele->ncluster
319 >    if( ctrl.debug ) {
320 >      cout << "postsel nlep: " << lepvec.size()
321 >           << "\tnmuon: " << nmu
322 >           << "\tnelectron: " << nele
323             << endl;
324      }
325 <    if ( !FAIL && !isMuonOverlap ) {
326 <      SimpleLepton tmplep;
327 <      tmplep.vec.SetPtEtaPhiM( ele->pt,
328 <                               ele->eta,
329 <                               ele->phi,
330 <                               0.51099892e-3 );
331 <      tmplep.type    = 11;
332 <      tmplep.index   = i;
333 <      tmplep.charge  = ele->q;
334 <      tmplep.isoTrk  = ele->trkIso03;
335 <      tmplep.isoEcal = ele->emIso03;
336 <      tmplep.isoHcal = ele->hadIso03;
337 <      tmplep.isoPF04 = ele->pfIso04;
338 <      tmplep.ip3dSig = ele->ip3dSig;
339 <      tmplep.is4l    = false;
340 <      tmplep.isEB    = ele->isEB;
341 <      lepvec.push_back(tmplep);
342 <      if( ctrl.debug ) { cout << "\telectron passes ... " << endl; }
325 >    
326 >    
327 >    //******************************************************************************
328 >    // Z1 Selection
329 >    //******************************************************************************
330 >    int Z1LeptonPlusIndex = -1;
331 >    int Z1LeptonMinusIndex = -1;
332 >    double BestZ1Mass = -999;
333 >    if( ctrl.debug ) { cout << "looking for a Z1 ..." << endl; }
334 >    for(int i = 0; i < lepvec.size(); ++i) {
335 >      if( !(lepvec[i].isLoose) ) continue;
336 >      for(int j = i+1; j < lepvec.size(); ++j) {
337 >        if( !(lepvec[j].isLoose) ) continue;
338 >        if( ctrl.debug ) { cout << "\tconsidering leptons " << i << " & " << j << endl; }
339 >        if (!(lepvec[i].vec->Pt() > 20.0 || lepvec[j].vec->Pt() > 20.0)) continue;
340 >        if( ctrl.debug ) { cout << "\tat least one is > 20 GeV" << endl; }
341 >        if (!(lepvec[i].vec->Pt() > 10.0 && lepvec[j].vec->Pt() > 10.0)) continue;
342 >        if( ctrl.debug ) { cout << "\tthe other  is > 10 GeV" << endl; }
343 >        if (lepvec[i].charge == lepvec[j].charge) continue;          
344 >        if( ctrl.debug ) { cout << "\tthey're opposite charge" << endl; }
345 >        if (fabs(lepvec[i].type) != fabs(lepvec[j].type)) continue;  
346 >        if( ctrl.debug ) { cout << "\tthey're same flavor" << endl; }
347 >        
348 >        //Make Z1 hypothesis
349 >        TLorentzVector *leptonPlus, *leptonMinus;
350 >        if ( lepvec[i].charge > 0 ) {
351 >          leptonPlus  = lepvec[i].vec;
352 >          leptonMinus = lepvec[j].vec;
353 >        } else {
354 >          leptonPlus  = lepvec[j].vec;
355 >          leptonMinus = lepvec[i].vec;
356 >        }
357 >        
358 >        float tmpZ1Mass = (*leptonPlus + *leptonMinus).M();
359 >        if( ctrl.debug ) cout << "Z1 selection, tmpZ1Mass: " << tmpZ1Mass << endl;
360 >        if( tmpZ1Mass > 50 ) {
361 >          if (fabs(tmpZ1Mass - Z_MASS) < fabs(BestZ1Mass - Z_MASS)) {
362 >            BestZ1Mass = tmpZ1Mass;
363 >            if( ctrl.debug ) cout << "Z1 selection, new BestZ1Mass: " << BestZ1Mass
364 >                                  << "\tdM: " << fabs(BestZ1Mass - Z_MASS)
365 >                                  << endl;
366 >            if (lepvec[i].charge > 0) {
367 >              Z1LeptonPlusIndex = i;
368 >              Z1LeptonMinusIndex = j;
369 >            } else {
370 >              Z1LeptonPlusIndex = j;
371 >              Z1LeptonMinusIndex = i;
372 >            }
373 >          }
374 >        }
375 >      }
376      }
377 <  }
378 <  
379 <  sort( lepvec.begin(), lepvec.end(), SimpleLepton::lep_pt_sort );
380 <
381 <  int nmu=0, nele=0;
382 <  for( int i=0; i<lepvec.size(); i++ ) {
383 <    if( abs(lepvec[i].type) == 11 ) nele++;
384 <    else nmu++;
385 <  }
386 <  if( ctrl.debug ) {
387 <    cout << "postsel nlep: " << lepvec.size()
388 <         << "\tnmuon: " << nmu
389 <         << "\tnelectron: " << nele
390 <         << endl;
391 <  }
392 <
393 <  //******************************************************************************
394 <  //Z1 Selection
395 <  //******************************************************************************
396 <  int Z1LeptonPlusIndex = -1;
397 <  int Z1LeptonMinusIndex = -1;
398 <  double BestZ1Mass = -999;
399 <  if( ctrl.debug ) { cout << "looking for a Z1 ..." << endl; }
400 <  for(int i = 0; i < lepvec.size(); ++i) {
401 <    for(int j = i+1; j < lepvec.size(); ++j) {
402 <      if( ctrl.debug ) { cout << "\tconsidering leptons " << i << " & " << j << endl; }
403 <      if (!(lepvec[i].vec.Pt() > 20.0 || lepvec[j].vec.Pt() > 20.0)) continue;
404 <      if( ctrl.debug ) { cout << "\tat least one is > 20 GeV" << endl; }
405 <      if (!(lepvec[i].vec.Pt() > 10.0 && lepvec[j].vec.Pt() > 10.0)) continue;
406 <      if( ctrl.debug ) { cout << "\tthe other  is > 10 GeV" << endl; }
407 <      if (lepvec[i].charge == lepvec[j].charge) continue;          
408 <      if( ctrl.debug ) { cout << "\tthey're opposite charge" << endl; }
245 <      if (fabs(lepvec[i].type) != fabs(lepvec[j].type)) continue;  
246 <      if( ctrl.debug ) { cout << "\tthey're same flavor" << endl; }
377 >    // stop if no Z1 candidate is found
378 >    if( BestZ1Mass < 0 ) {
379 >      evtfail |= (1<<EVTFAIL_Z1);
380 >      //ret.status = evtfail;
381 >      ret.status.setStatus(0);
382 >      return ret;
383 >    }
384 >    if( ctrl.debug ) cout << "\tgot a Z1 ... run: " << info->RunNum() << "\tevt: " << info->EvtNum()  << endl;
385 >    if( ctrl.debug ) cout << "\tZ1 plusindex: " << Z1LeptonPlusIndex << "\tminusindex: " << Z1LeptonMinusIndex << endl;
386 >    TLorentzVector Z1LeptonPlus  = *(lepvec[Z1LeptonPlusIndex].vec);
387 >    TLorentzVector Z1LeptonMinus = *(lepvec[Z1LeptonMinusIndex].vec);
388 >    TLorentzVector Z1Candidate   =  Z1LeptonPlus + Z1LeptonMinus;
389 >    
390 >    
391 >    //******************************************************************************
392 >    // Z1 + l
393 >    //******************************************************************************
394 >    if( lepvec.size() < 3 ) {
395 >      evtfail |= (1<<EVTFAIL_Z1_PLUSL);
396 >      //ret.status = evtfail;
397 >      ret.status.setStatus(0);
398 >      return ret;
399 >    }
400 >    
401 >    //******************************************************************************
402 >    // 4l/Z2 Selection
403 >    //******************************************************************************
404 >    Int_t Z2LeptonPlusIndex = -1;
405 >    Int_t Z2LeptonMinusIndex = -1;
406 >    Double_t BestZ2Mass = -1;
407 >    if( ctrl.debug ) cout << "looking for a Z2 ... out of " << lepvec.size() << " leptons" <<endl;
408 >    for(int i = 0; i < lepvec.size(); ++i) {
409        
410 <      //Make Z1 hypothesis
411 <      TLorentzVector leptonPlus, leptonMinus;
412 <      if ( lepvec[i].charge > 0 ) {
413 <        leptonPlus  = lepvec[i].vec;
414 <        leptonMinus = lepvec[j].vec;
415 <      } else {
416 <        leptonPlus  = lepvec[j].vec;
417 <        leptonMinus = lepvec[i].vec;
410 >      if( ctrl.debug)  cout << "i: " << i
411 >                            << "\tpt: " << lepvec[i].vec->Pt()
412 >                            << "\ttype: " << lepvec[i].type
413 >                            << endl;
414 >      
415 >      if( ctrl.eleSeleScheme == "mediumloose" &&
416 >          !(lepvec[i].isTight) ) {
417 >        if( ctrl.debug)  cout << "it's not tight, skipping ... " << endl;
418 >        continue;
419        }
420        
421 <      float tmpZ1Mass = (leptonPlus+leptonMinus).M();
422 <      if( ctrl.debug ) cout << "Z1 selection, tmpZ1Mass: " << tmpZ1Mass << endl;
423 <      if( tmpZ1Mass > 60 ) {
424 <        if (fabs(tmpZ1Mass - 91.1876) < fabs(BestZ1Mass - 91.1876)) {
425 <          BestZ1Mass = tmpZ1Mass;
426 <          if( ctrl.debug ) cout << "Z1 selection, new BestZ1Mass: " << BestZ1Mass
427 <               << "\tdM: " << fabs(BestZ1Mass - 91.1876)
428 <               << endl;
421 >      for(int j = i+1; j < lepvec.size(); ++j) {
422 >        if( ctrl.debug)  cout << "\t\tj: " << j
423 >                              << "\tpt: " << lepvec[j].vec->Pt()
424 >                              << "\ttype: " << lepvec[j].type
425 >                              << endl;
426 >        
427 >        if( ctrl.eleSeleScheme == "mediumloose" &&
428 >            !(lepvec[j].isTight) ) {
429 >          if( ctrl.debug)  cout << "it's not tight, skipping ... " << endl;
430 >          continue;
431 >        }
432 >        
433 >        
434 >        if (i == Z1LeptonPlusIndex || i == Z1LeptonMinusIndex) {
435 >          if( ctrl.debug)  cout << "\ti matches a Z1 index, skipping ..." << endl;
436 >          continue; //skip Z1 leptons
437 >        }
438 >        if (j == Z1LeptonPlusIndex || j == Z1LeptonMinusIndex) {
439 >          if( ctrl.debug)  cout << "\tj matches a Z1 index, skipping ..." << endl;
440 >          continue; //skip Z1 leptons
441 >        }
442 >        if (lepvec[i].charge == lepvec[j].charge) {
443 >          if( ctrl.debug)  cout << "\ti and j are same sign, skipping ..." << endl;
444 >          continue;         //require opp sign
445 >        }
446 >        if (fabs(lepvec[i].type) != fabs(lepvec[j].type)) {
447 >          if( ctrl.debug) cout << "\ti and j are not same flavor, skipping ..." << endl;
448 >          continue; //require same flavor
449 >        }
450 >        
451 >        
452 >        //Make Z2 hypothesis
453 >        TLorentzVector *leptonPlus, *leptonMinus;
454 >        
455 >        if (lepvec[i].charge > 0 ) {
456 >          leptonPlus  = lepvec[i].vec;
457 >          leptonMinus = lepvec[j].vec;
458 >        } else {
459 >          leptonPlus  = lepvec[j].vec;
460 >          leptonMinus = lepvec[i].vec;
461 >        }
462 >        
463 >        TLorentzVector dilepton = *leptonPlus + *leptonMinus;
464 >        TLorentzVector fourLepton = Z1Candidate + dilepton;
465 >        
466 >        if( ctrl.debug ) cout << "dilepton.M() : " << dilepton.M() << endl;
467 >        if( ctrl.debug ) cout << "fourLepton.M() : " << fourLepton.M() << endl;
468 >        
469 >        if (!(dilepton.M() > 12.0)) continue;
470 >        if (!(fourLepton.M() > 100.0)) continue;
471 >        
472 >        //for 4e and 4mu, require at least 1 of the other opp sign lepton pairs have mass > 12
473 >        if (fabs(lepvec[i].type) == fabs(lepvec[Z1LeptonPlusIndex].type)) {
474 >          TLorentzVector pair1 = Z1LeptonPlus + *leptonMinus;
475 >          TLorentzVector pair2 = Z1LeptonMinus + *leptonPlus;
476 >          if( ctrl.debug ) cout << "pair1: " << pair1.M() << "\tpair2: "<< pair2.M() << endl;
477 >          if (!(pair1.M() > 12 || pair2.M() > 12)) continue;
478 >        }
479 >      
480 >      
481 >        //Disambiguiation is done by choosing the pair with the largest ptMax and largest ptMin
482 >        if (Z2LeptonPlusIndex < 0) {
483            if (lepvec[i].charge > 0) {
484 <            Z1LeptonPlusIndex = i;
485 <            Z1LeptonMinusIndex = j;
484 >            Z2LeptonPlusIndex = i;
485 >            Z2LeptonMinusIndex = j;
486            } else {
487 <            Z1LeptonPlusIndex = j;
488 <            Z1LeptonMinusIndex = i;
487 >            Z2LeptonPlusIndex = j;
488 >            Z2LeptonMinusIndex = i;
489            }
273        }
274      }
275    }
276  }
277  // stop if no Z1 candidate is found
278  if( BestZ1Mass < 0 ) {
279    evtfail |= (1<<EVTFAIL_Z1);
280    return evtfail;
281  }
282  if( ctrl.debug ) cout << "\tgot a Z1 ... run: " << info->runNum << "\tevt: " << info->evtNum  << endl;
283  if( ctrl.debug ) cout << "\tZ1 plusindex: " << Z1LeptonPlusIndex << "\tminusindex: " << Z1LeptonMinusIndex << endl;
284  TLorentzVector Z1LeptonPlus  = lepvec[Z1LeptonPlusIndex].vec;
285  TLorentzVector Z1LeptonMinus = lepvec[Z1LeptonMinusIndex].vec;
286  TLorentzVector Z1Candidate   =  Z1LeptonPlus + Z1LeptonMinus;
287  if( l != NULL ) {
288    l->vecz1 = Z1Candidate;
289    l->vecl1p = Z1LeptonPlus;
290    l->vecl1m = Z1LeptonMinus;
291  }
292  
293  //******************************************************************************
294  // Z1 + l
295  //******************************************************************************
296  if( lepvec.size() < 3 ) {
297    evtfail |= (1<<EVTFAIL_Z1_PLUSL);
298    return evtfail;
299  }
300        
301  //******************************************************************************
302  // 4l/Z2 Selection
303  //******************************************************************************
304  Int_t Z2LeptonPlusIndex = -1;
305  Int_t Z2LeptonMinusIndex = -1;
306  Double_t BestZ2Mass = -1;
307  if( ctrl.debug ) cout << "looking for a Z2 ... out of " << lepvec.size() << " leptons" <<endl;
308  for(int i = 0; i < lepvec.size(); ++i) {
309    for(int j = i+1; j < lepvec.size(); ++j) {
310      //            cout << "i: " << i << "\tj: " << j << endl;
311      if (i == Z1LeptonPlusIndex || i == Z1LeptonMinusIndex) {
312        //            cout << "\ti matches a Z1 index, skipping ..." << endl;
313        continue; //skip Z1 leptons
314      }
315      if (j == Z1LeptonPlusIndex || j == Z1LeptonMinusIndex) {
316        //            cout << "\tj matches a Z1 index, skipping ..." << endl;
317              continue; //skip Z1 leptons
318            }
319            if (lepvec[i].charge == lepvec[j].charge) {
320              //              cout << "\ti and j are same sign, skipping ..." << endl;
321              continue;         //require opp sign
322            }
323            if (fabs(lepvec[i].type) != fabs(lepvec[j].type)) {
324              //              cout << "\ti and j are not same flavor, skipping ..." << endl;
325              continue; //require same flavor
326            }
327            
328            
329            //Make Z2 hypothesis
330            TLorentzVector leptonPlus, leptonMinus;
331            
332            if (lepvec[i].charge > 0 ) {
333              leptonPlus  = lepvec[i].vec;
334              leptonMinus = lepvec[j].vec;
335            } else {
336              leptonPlus  = lepvec[j].vec;
337              leptonMinus = lepvec[i].vec;
338            }
339
340            TLorentzVector dilepton = leptonPlus+leptonMinus;
341            TLorentzVector fourLepton = Z1Candidate + dilepton;
342
343            if( ctrl.debug ) cout << "dilepton.M() : " << dilepton.M() << endl;
344            if( ctrl.debug ) cout << "fourLepton.M() : " << fourLepton.M() << endl;
345
346            if (!(dilepton.M() > 12.0)) continue;
347            if (!(fourLepton.M() > 100.0)) continue;
348
349            
350            //for 4e and 4mu, require at least 1 of the other opp sign lepton pairs have mass > 12
351            if (fabs(lepvec[i].type) == fabs(lepvec[Z1LeptonPlusIndex].type)) {
352              TLorentzVector pair1 = Z1LeptonPlus+leptonMinus;
353              TLorentzVector pair2 = Z1LeptonMinus+leptonPlus;
354              if( ctrl.debug ) cout << "pair1: " << pair1.M() << "\tpair2: "<< pair2.M() << endl;
355              if (!(pair1.M() > 12 || pair2.M() > 12)) continue;
356            }
357            
358            //Disambiguiation is done by choosing the pair with the largest ptMax and largest ptMin
359            if (Z2LeptonPlusIndex < 0) {
360              if (lepvec[i].charge > 0) {
361                Z2LeptonPlusIndex = i;
362                Z2LeptonMinusIndex = j;
363              } else {
364                Z2LeptonPlusIndex = j;
365                Z2LeptonMinusIndex = i;
366              }
367            } else {
368              Double_t BestPairPtMax = lepvec[Z2LeptonPlusIndex].vec.Pt();              
369              Double_t BestPairPtMin = lepvec[Z2LeptonMinusIndex].vec.Pt();
370              if (lepvec[Z2LeptonMinusIndex].vec.Pt() > BestPairPtMax) {
371                BestPairPtMax = lepvec[Z2LeptonMinusIndex].vec.Pt();
372                BestPairPtMin = lepvec[Z2LeptonPlusIndex].vec.Pt();
373              }
374
375              Double_t CurrentPairPtMax = lepvec[i].vec.Pt();              
376              Double_t CurrentPairPtMin = lepvec[j].vec.Pt();
377              if (lepvec[j].vec.Pt() > CurrentPairPtMax) {
378                CurrentPairPtMax = lepvec[j].vec.Pt();
379                CurrentPairPtMin = lepvec[i].vec.Pt();
380              }
381
382              if (CurrentPairPtMax > BestPairPtMax) {
383                if (lepvec[i].charge > 0) {
384                  Z2LeptonPlusIndex = i;
385                  Z2LeptonMinusIndex = j;
386                } else {
387                  Z2LeptonPlusIndex = j;
388                  Z2LeptonMinusIndex = i;
389                }
390              } else if (CurrentPairPtMax  == BestPairPtMax) {
391                if (CurrentPairPtMin > BestPairPtMin) {
392                  if (lepvec[i].charge > 0) {
393                    Z2LeptonPlusIndex = i;
394                    Z2LeptonMinusIndex = j;
395                  } else {
396                    Z2LeptonPlusIndex = j;
397                    Z2LeptonMinusIndex = i;
398                  }                  
399                }
400              }
401            }            
402          }
403        }
404
405        // stop if no Z2 candidate is found
406        if (Z2LeptonPlusIndex == -1) {
407          evtfail |= ( 1<<EVTFAIL_4L );
408          return evtfail;
409          //      h_evtfail->Fill( evtfail );
410          //      cout << "evtfail: " << hex << evtfail << dec << endl;
411          //      continue;
412        }
413        if( ctrl.debug ) cout << "\tgot a Z2 ..." << endl;
414        if( ctrl.debug ) cout << "\tZ2 plusindex: " << Z2LeptonPlusIndex
415                              << "\tminusindex: " << Z2LeptonMinusIndex << endl;
416        TLorentzVector  Z2LeptonPlus  = lepvec[Z2LeptonPlusIndex].vec;
417        TLorentzVector  Z2LeptonMinus = lepvec[Z2LeptonMinusIndex].vec;
418        TLorentzVector  Z2Candidate   = Z2LeptonPlus+Z2LeptonMinus;
419        TLorentzVector  ZZSystem      = Z1Candidate + Z2Candidate;
420        if( l != NULL ) {
421          l->vecz2 = Z2Candidate;
422          l->vecl2p = Z2LeptonPlus;
423          l->vecl2m = Z2LeptonMinus;
424          l->vec4l = ZZSystem;
425        }
426        lepvec[Z1LeptonPlusIndex].is4l = true;
427        lepvec[Z1LeptonMinusIndex].is4l = true;
428        lepvec[Z2LeptonPlusIndex].is4l = true;
429        lepvec[Z2LeptonMinusIndex].is4l = true;
430
431        //***************************************************************
432        // Isolation
433        //***************************************************************
434        bool failiso=false;
435
436        if( ctrl.isoScheme = "pf" ) {
437
438          for( int i=0; i<lepvec.size(); i++ ) {
439            if( !(lepvec[i].is4l) ) continue;
440            if( abs(lepvec[i].type) == 11 ) {
441              if( (lepvec[i].isEB && lepvec[i].isoPF04 >0.13) ||
442                  (!(lepvec[i].isEB) && lepvec[i].isoPF04 >0.09) ) {
443                failiso = true;
444                break;
445              }
446            }
447            if( abs(lepvec[i].type) == 13 ) {
448              if( lepvec[i].isEB && lepvec[i].vec.Pt() > 20 && lepvec[i].isoPF04 > 0.13 ) {
449                failiso = true;
450                break;
451              }
452              if( lepvec[i].isEB && lepvec[i].vec.Pt() < 20 && lepvec[i].isoPF04 > 0.06 ) {
453                failiso = true;
454                break;
455              }
456              if( !(lepvec[i].isEB) && lepvec[i].vec.Pt() > 20 && lepvec[i].isoPF04 > 0.09 ) {
457                failiso = true;
458                break;
459              }
460              if( !(lepvec[i].isEB) && lepvec[i].vec.Pt() < 20 && lepvec[i].isoPF04 > 0.05 ) {
461                failiso = true;
462                break;
463              }
464            }
465          }  
490          } else {
491 <          float rho = info->rho;
492 <          for( int i=0; i<lepvec.size(); i++ ) {
493 <            if( !(lepvec[i].is4l) ) continue;
494 <            float effArea_ecal_i, effArea_hcal_i;
495 <            if( lepvec[i].isEB ) {
496 <              if( lepvec[i].type == 11 ) {
497 <                effArea_ecal_i = 0.101;
498 <                effArea_hcal_i = 0.021;
499 <              } else {
500 <                effArea_ecal_i = 0.074;
501 <                effArea_hcal_i = 0.022;
502 <              }
491 >          Double_t BestPairPtMax = lepvec[Z2LeptonPlusIndex].vec->Pt();              
492 >          Double_t BestPairPtMin = lepvec[Z2LeptonMinusIndex].vec->Pt();
493 >          if (lepvec[Z2LeptonMinusIndex].vec->Pt() > BestPairPtMax) {
494 >            BestPairPtMax = lepvec[Z2LeptonMinusIndex].vec->Pt();
495 >            BestPairPtMin = lepvec[Z2LeptonPlusIndex].vec->Pt();
496 >          }
497 >        
498 >          Double_t CurrentPairPtMax = lepvec[i].vec->Pt();              
499 >          Double_t CurrentPairPtMin = lepvec[j].vec->Pt();
500 >          if (lepvec[j].vec->Pt() > CurrentPairPtMax) {
501 >            CurrentPairPtMax = lepvec[j].vec->Pt();
502 >            CurrentPairPtMin = lepvec[i].vec->Pt();
503 >          }
504 >        
505 >          if (CurrentPairPtMax > BestPairPtMax) {
506 >            if (lepvec[i].charge > 0) {
507 >              Z2LeptonPlusIndex = i;
508 >              Z2LeptonMinusIndex = j;
509              } else {
510 <              if( lepvec[i].type == 11 ) {
511 <                effArea_ecal_i = 0.046;
482 <                effArea_hcal_i = 0.040;
483 <              } else {
484 <                effArea_ecal_i = 0.045;
485 <                effArea_hcal_i = 0.030;
486 <              }
510 >              Z2LeptonPlusIndex = j;
511 >              Z2LeptonMinusIndex = i;
512              }
513 <            float isoEcal_corr_i = lepvec[i].isoEcal - (effArea_ecal_i*rho);
514 <            float isoHcal_corr_i = lepvec[i].isoHcal - (effArea_hcal_i*rho);
515 <            for( int j=i+1; j<lepvec.size(); j++ ) {
516 <              if( !(lepvec[j].is4l) ) continue;
517 <              float effArea_ecal_j, effArea_hcal_j;
493 <              if( lepvec[j].isEB ) {
494 <                if( lepvec[j].type == 11 ) {
495 <                  effArea_ecal_j = 0.101;
496 <                  effArea_hcal_j = 0.021;
497 <                } else {
498 <                  effArea_ecal_j = 0.074;
499 <                  effArea_hcal_j = 0.022;
500 <                }
513 >          } else if (CurrentPairPtMax  == BestPairPtMax) {
514 >            if (CurrentPairPtMin > BestPairPtMin) {
515 >              if (lepvec[i].charge > 0) {
516 >                Z2LeptonPlusIndex = i;
517 >                Z2LeptonMinusIndex = j;
518                } else {
519 <                if( lepvec[j].type == 11 ) {
520 <                  effArea_ecal_j = 0.046;
521 <                  effArea_hcal_j = 0.040;
505 <                } else {
506 <                  effArea_ecal_j = 0.045;
507 <                  effArea_hcal_j = 0.030;
508 <                }
509 <              }
510 <              float isoEcal_corr_j = lepvec[j].isoEcal - (effArea_ecal_j*rho);
511 <              float isoHcal_corr_j = lepvec[j].isoHcal - (effArea_hcal_j*rho);
512 <              float RIso_i = (lepvec[i].isoTrk+isoEcal_corr_i+isoHcal_corr_i)/lepvec[i].vec.Pt();
513 <              float RIso_j = (lepvec[j].isoTrk+isoEcal_corr_j+isoHcal_corr_j)/lepvec[j].vec.Pt();          
514 <              float comboIso = RIso_i + RIso_j;
515 <              if( info->evtNum == 1038911933 ) {
516 <                float tmpdR = lepvec[i].vec.DrEtaPhi(lepvec[j].vec);
517 <                cout << "i: " << i
518 <                     << "\tdR: " << tmpdR
519 <                     << "\trho: " << rho
520 <                     << "\tRIso_i: " << RIso_i  
521 <                     << "\ttkrel: " << lepvec[i].isoTrk/lepvec[i].vec.Pt()
522 <                     << "\tecalrel: " << lepvec[i].isoEcal/lepvec[i].vec.Pt()
523 <                     << "\tecalrelcor: " << isoEcal_corr_i/lepvec[i].vec.Pt()
524 <                     << "\thcalrel: " << lepvec[i].isoHcal/lepvec[i].vec.Pt()
525 <                     << "\thcalrelcor: " << isoHcal_corr_i/lepvec[i].vec.Pt()
526 <                     << "\tpt_i: " << lepvec[i].vec.Pt()
527 <                     << "\tj: " << j
528 <                     << "\tRIso_j: " << RIso_j  
529 <                     << "\ttkrel: "  << lepvec[j].isoTrk/lepvec[j].vec.Pt()
530 <                     << "\tecalrel: " << lepvec[j].isoEcal/lepvec[j].vec.Pt()
531 <                     << "\tecalrelcor: " << isoEcal_corr_j/lepvec[j].vec.Pt()
532 <                     << "\thcalrel: " << lepvec[j].isoHcal/lepvec[j].vec.Pt()
533 <                     << "\thcalrelcor: " << isoHcal_corr_j/lepvec[j].vec.Pt()
534 <                     << "\tpt_j: " << lepvec[j].vec.Pt()
535 <                     << "\tcombo: " << comboIso
536 <                     << endl;
537 <                cout.flush();
538 <              }
539 <              if( comboIso > 0.35 ) {
540 <                if( ctrl.debug ) cout << "combo failing for indices: " << i << "," << j << endl;
541 <                failiso = true;
542 <                //            break;
543 <              }
519 >                Z2LeptonPlusIndex = j;
520 >                Z2LeptonMinusIndex = i;
521 >              }                  
522              }
523            }
524 <        }
525 <
524 >        }            
525 >      }
526 >    }
527 >  
528 >    // stop if no Z2 candidate is found
529 >    if (Z2LeptonPlusIndex == -1) {
530 >      evtfail |= ( 1<<EVTFAIL_4L );
531 >      //      ret.status = evtfail;
532 >      ret.status.setStatus(0);
533 >      return ret;
534 >    }
535 >    if( ctrl.debug ) cout << "\tgot a Z2 ..." << endl;
536 >    if( ctrl.debug ) cout << "\tZ2 plusindex: " << Z2LeptonPlusIndex
537 >                          << "\tminusindex: " << Z2LeptonMinusIndex << endl;
538 >    TLorentzVector  Z2LeptonPlus  = *(lepvec[Z2LeptonPlusIndex].vec);
539 >    TLorentzVector  Z2LeptonMinus = *(lepvec[Z2LeptonMinusIndex].vec);
540 >    TLorentzVector  Z2Candidate   = Z2LeptonPlus+Z2LeptonMinus;
541 >    TLorentzVector  ZZSystem      = Z1Candidate + Z2Candidate;
542 >    lepvec[Z1LeptonPlusIndex].is4l = true;
543 >    lepvec[Z1LeptonMinusIndex].is4l = true;
544 >    lepvec[Z2LeptonPlusIndex].is4l = true;
545 >    lepvec[Z2LeptonMinusIndex].is4l = true;
546 >  
547  
549        if( failiso ) {
550          evtfail |= ( 1<<EVTFAIL_ISOLATION );
551          return evtfail;
552          //h_evtfail->Fill( evtfail, eventweight );
553          //      h_evtfail->Fill( evtfail );
554          //      cout << "evtfail: " << hex << evtfail << dec << endl;
555          //      continue;
556        }
557
558        //***************************************************************
559        // IP significance
560        //***************************************************************
561        bool failip = false;
562        for( int i=0; i<lepvec.size(); i++ ) {
563          if( !(lepvec[i].is4l) ) continue;
564          if( lepvec[i].ip3dSig > 4 ) {
565            failip=true;
566            break;
567          }
568        }
569        if( failip ) {
570          evtfail |= (1<<EVTFAIL_IP );
571          return evtfail;
572          //h_evtfail->Fill( evtfail, eventweight );
573          //      h_evtfail->Fill( evtfail );
574          //      cout << "evtfail: " << hex << evtfail << dec << endl;
575          //      continue;
576        }
577
578        //***************************************************************
579        // remaining kinematic cuts
580        //***************************************************************
581        double Z2massCut=0;
582        if      ( ctrl.kinematics == "loose" ) Z2massCut = 12;
583        else if ( ctrl.kinematics == "tight" ) Z2massCut = 20;
584        else { cout <<  "error! kinematic tightness not defined!" << endl; assert(0); }
585
586        if ( Z1Candidate.M() > 120        ||
587             Z2Candidate.M() < Z2massCut  ||
588             Z2Candidate.M() > 120        ||
589             !(lepvec[Z1LeptonPlusIndex].vec.Pt() > 20.0 || lepvec[Z1LeptonMinusIndex].vec.Pt() > 20.0) ||
590             !(lepvec[Z1LeptonPlusIndex].vec.Pt() > 10.0 && lepvec[Z1LeptonMinusIndex].vec.Pt() > 10.0)
591             ) {
592          evtfail |= (1<<EVTFAIL_KINEMATICS );
593          return evtfail;
594          //h_evtfail->Fill( evtfail, eventweight );
595          //      h_evtfail->Fill( evtfail );
596          //      cout << "evtfail: " << hex << evtfail << dec << endl;
597          //      continue;
598        }
599        
600        int channel;
601        if( lepvec[Z1LeptonMinusIndex].type == 11 && lepvec[Z2LeptonMinusIndex].type == 11 ) channel=0;
602        if( lepvec[Z1LeptonMinusIndex].type == 13 && lepvec[Z2LeptonMinusIndex].type == 13 ) channel=1;
603        if( (lepvec[Z1LeptonMinusIndex].type == 11 && lepvec[Z2LeptonMinusIndex].type == 13) ||
604            (lepvec[Z1LeptonMinusIndex].type == 13 && lepvec[Z2LeptonMinusIndex].type == 11)) channel=2;
605        
606
607
608        if( passtuple != NULL ) {
609          unsigned run   = info->runNum;
610          unsigned evt   = info->evtNum;
611          unsigned lumi  = info->lumiSec;
612          unsigned chan  = channel;
613          double   w     = eventweight;
614          float mZ1      = Z1Candidate.M() ;
615          float mZ2      = Z2Candidate.M() ;
616          float m4l      = ZZSystem.M() ;
617          float pt4l     = ZZSystem.Pt() ;
618          passtuple->SetBranchAddress("run",  &run);
619          passtuple->SetBranchAddress("evt",  &evt);
620          passtuple->SetBranchAddress("lumi", &lumi);
621          passtuple->SetBranchAddress("mZ1",  &mZ1);
622          passtuple->SetBranchAddress("mZ2",  &mZ2);
623          passtuple->SetBranchAddress("m4l",  &m4l);
624          passtuple->SetBranchAddress("pt4l", &pt4l);
625          passtuple->SetBranchAddress("w",    &w);
626          passtuple->Fill( );
627        }
628
629        if( ctrl.debug ) cout  << "run: " << info->runNum  
630              << "\tevt: " << info->evtNum
631              << "\tZ1channel: " << lepvec[Z1LeptonMinusIndex].type
632              << "\tZ2channel: " << lepvec[Z2LeptonMinusIndex].type
633              << "\tmZ1: " << Z1Candidate.M()
634              << "\tmZ2: " << Z2Candidate.M()
635              << "\tm4l: " << ZZSystem.M()
636              << "\tevtfail: " << hex << evtfail << dec
637              << "\ttrigbits: " << hex << info->triggerBits << dec
638          //          << "\ttree: " << inputFiles[q][f]
639              << endl;
548  
549 <        return evtfail;
550 <          
551 < }
549 >  
550 >    //***************************************************************
551 >    // remaining kinematic cuts
552 >    //***************************************************************
553 >    double Z2massCut=0;
554 >    if      ( ctrl.kinematics == "loose" ) Z2massCut = 12;
555 >    else if ( ctrl.kinematics == "tight" ) Z2massCut = 20;
556 >    else { cout <<  "error! kinematic tightness not defined!" << endl; assert(0); }
557 >  
558 >    if ( Z1Candidate.M() > 120        ||
559 >         Z2Candidate.M() < Z2massCut  ||
560 >         Z2Candidate.M() > 120        ||
561 >         !(lepvec[Z1LeptonPlusIndex].vec->Pt() > 20.0 || lepvec[Z1LeptonMinusIndex].vec->Pt() > 20.0) ||
562 >         !(lepvec[Z1LeptonPlusIndex].vec->Pt() > 10.0 && lepvec[Z1LeptonMinusIndex].vec->Pt() > 10.0)
563 >         ) {
564 >      evtfail |= (1<<EVTFAIL_KINEMATICS );
565 >      //      ret.status = evtfail;
566 >      ret.status.setStatus(0);
567 >      return ret;
568 >    }
569 >  
570 >    unsigned channel;
571 >    if( lepvec[Z1LeptonMinusIndex].type == 11 && lepvec[Z2LeptonMinusIndex].type == 11 ) channel=0;
572 >    if( lepvec[Z1LeptonMinusIndex].type == 13 && lepvec[Z2LeptonMinusIndex].type == 13 ) channel=1;
573 >    if( (lepvec[Z1LeptonMinusIndex].type == 11 && lepvec[Z2LeptonMinusIndex].type == 13) ||
574 >        (lepvec[Z1LeptonMinusIndex].type == 13 && lepvec[Z2LeptonMinusIndex].type == 11)) channel=2;
575 >  
576 >  
577 >  
578 >  
579 >    if( ctrl.debug ) cout  << "run: " << info->RunNum()  
580 >                           << "\tevt: " << info->EvtNum()
581 >                           << "\tZ1channel: " << lepvec[Z1LeptonMinusIndex].type
582 >                           << "\tZ2channel: " << lepvec[Z2LeptonMinusIndex].type
583 >                           << "\tmZ1: " << Z1Candidate.M()
584 >                           << "\tmZ2: " << Z2Candidate.M()
585 >                           << "\tm4l: " << ZZSystem.M()
586 >                           << "\tevtfail: " << hex << evtfail << dec
587 >      //                           << "\ttrigbits: " << hex << info->triggerBits << dec
588 >      //              << "\ttree: " << inputFiles[q][f]
589 >                           << endl;
590 >  
591  
592  
593 +    //***************************************************************
594 +    // finish
595 +    //***************************************************************
596 +
597 +    if( !evtfail ) {
598 +      ret.status.setStatus(SelectionStatus::EVTPASS);
599 +      ret.Z1leptons.push_back(lepvec[Z1LeptonMinusIndex]);
600 +      ret.Z1leptons.push_back(lepvec[Z1LeptonPlusIndex]);
601 +      ret.Z2leptons.push_back(lepvec[Z2LeptonMinusIndex]);
602 +      ret.Z2leptons.push_back(lepvec[Z2LeptonPlusIndex]);
603 +    }
604  
605 +    return ret;
606 + }
607  
608  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines