ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/Vuko/WZAnalysis/src/WZAnalyzer.cc
Revision: 1.32
Committed: Thu Jan 24 13:21:11 2008 UTC (17 years, 3 months ago) by smorovic
Content type: text/plain
Branch: MAIN
Changes since 1.31: +97 -67 lines
Log Message:
MC leptons: better tau and tau child recognition

File Contents

# User Rev Content
1 vuko 1.1 // -*- C++ -*-
2     //
3     // Package: WZAnalyzer
4     // Class: WZAnalyzer
5     //
6 vuko 1.2 /**\class WZAnalyzer WZAnalyzer.cc Vuko/WZAnalysis/src/WZAnalyzer.cc
7 vuko 1.1
8     Description: <one line class summary>
9    
10     Implementation:
11     <Notes on implementation>
12     */
13     //
14     // Original Author: Vuko Brigljevic
15     // Created: Fri Nov 9 11:07:27 CET 2007
16 smorovic 1.32 // $Id: WZAnalyzer.cc,v 1.29 2008/01/21 13:29:26 smorovic Exp $
17 vuko 1.1 //
18     //
19    
20    
21 senka 1.19
22 vuko 1.1 // system include files
23     #include <memory>
24    
25     // user include files
26     #include "FWCore/Framework/interface/Frameworkfwd.h"
27     #include "FWCore/Framework/interface/EDAnalyzer.h"
28    
29     #include "FWCore/Framework/interface/Event.h"
30     #include "FWCore/Framework/interface/MakerMacros.h"
31    
32     #include "FWCore/ParameterSet/interface/ParameterSet.h"
33    
34 vuko 1.2 #include "Vuko/WZAnalysis/interface/WZAnalyzer.h"
35 vuko 1.1 #include "DataFormats/Candidate/interface/OverlapChecker.h"
36    
37 vuko 1.2 #include "Vuko/WZAnalysis/interface/ElectronProperties.h"
38 vuko 1.3 #include "Vuko/WZAnalysis/interface/MuonProperties.h"
39 vuko 1.1
40 smorovic 1.6 #include "DataFormats/Common/interface/TriggerResults.h"
41 vuko 1.1
42     //--- muon AOD:
43     #include "DataFormats/JetReco/interface/CaloJetCollection.h"
44     #include "DataFormats/EgammaCandidates/interface/Electron.h"
45     #include "DataFormats/EgammaCandidates/interface/ElectronFwd.h"
46     #include "DataFormats/MuonReco/interface/MuonFwd.h"
47     #include "DataFormats/MuonReco/interface/Muon.h"
48 senka 1.28 #include "DataFormats/MuonReco/interface/MuIsoDeposit.h"
49 vuko 1.1 #include "DataFormats/EgammaReco/interface/BasicCluster.h"
50     #include "DataFormats/EgammaReco/interface/BasicClusterFwd.h"
51     #include "DataFormats/EgammaCandidates/interface/PixelMatchGsfElectron.h"
52    
53 senka 1.9 #include "DataFormats/METReco/interface/CaloMET.h"
54    
55 vuko 1.1
56     #include "TFile.h"
57     #include "TH1F.h"
58     #include "TH2F.h"
59     #include "TTree.h"
60    
61     #include <map>
62    
63     //
64     // constants, enums and typedefs
65     //
66    
67     //
68     // static data member definitions
69     //
70    
71     //
72     // constructors and destructor
73     //
74     WZAnalyzer::WZAnalyzer(const edm::ParameterSet& iConfig)
75    
76     {
77     //now do what ever initialization is needed
78    
79 senka 1.16 fOutputFileName = iConfig.getUntrackedParameter<string>("HistOutFile");
80 vuko 1.10 theMCTruthCollection = iConfig.getParameter<edm::InputTag>("genParticles");
81    
82 senka 1.12 // theMCTruthCollection = iConfig.getParameter<edm::InputTag>("genParticleCollection");
83    
84 vuko 1.1 theLooseMuonCollection = iConfig.getParameter<edm::InputTag>("LooseMuons");
85     theGoodMuonCollection = iConfig.getParameter<edm::InputTag>("GoodMuons");
86     theLooseElectronCollection = iConfig.getParameter<edm::InputTag>("LooseElectrons");
87     theGoodElectronCollection = iConfig.getParameter<edm::InputTag>("GoodElectrons");
88     theTightLeptonCollection = iConfig.getParameter<edm::InputTag>("TightLeptons");
89     // theMediumElectronCollection = iConfig.getParameter<edm::InputTag>("LooseElectrons");
90     // theTightElectronCollection = iConfig.getParameter<edm::InputTag>("TightElectrons");
91     // Z's
92     theZeeCollection = iConfig.getParameter<edm::InputTag>("ZtoEE");
93     theZmumuCollection = iConfig.getParameter<edm::InputTag>("ZtoMuMu");
94 vuko 1.3 theZllCollection = iConfig.getParameter<edm::InputTag>("ZtoLL");
95 vuko 1.10
96     theJetCollection = iConfig.getParameter<edm::InputTag>("Jets");
97 vuko 1.26
98     theWeightCollection = iConfig.getParameter<edm::InputTag>("EventWeight");
99 smorovic 1.6
100     storeHLTresults=false;
101     storeHLTresults = iConfig.getParameter<bool>("storeHLTresults");
102 vuko 1.1
103 vuko 1.26 getEventWeight = false;
104     getEventWeight = iConfig.getParameter<bool>("getEventWeight");
105    
106 smorovic 1.23 triggerResultsTag = iConfig.getParameter<edm::InputTag>("TriggerResults");
107 smorovic 1.6 firstTriggerResult = true;
108 vuko 1.1 }
109    
110    
111     WZAnalyzer::~WZAnalyzer()
112     {
113    
114     // do anything here that needs to be done at desctruction time
115     // (e.g. close files, deallocate resources etc.)
116    
117     }
118    
119    
120     //
121     // member functions
122     //
123    
124     // ------------ method called to for each event ------------
125     void
126     WZAnalyzer::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup)
127     {
128     using namespace edm;
129     using namespace reco;
130     using namespace std;
131    
132 vuko 1.11 ///////////////////////////////////////
133     //
134     /// EVENT INITIALISATION
135     ///
136    
137     // Reset a few things
138    
139     for (map<string,wzana::LeptonProperties* >::iterator iter = leptonProperties.begin();
140     iter!=leptonProperties.end(); iter++)
141     {
142     iter->second->ResetValues();
143     }
144    
145    
146 vuko 1.1 const Candidate * theZCandidate = 0;
147     const Candidate * theWlepton = 0;
148    
149 vuko 1.11
150 senka 1.12 // Tau.clear();
151     // StableMuons.clear();
152     // StableElectrons.clear();
153    
154 vuko 1.11
155     ////////////////////////////////////////
156     // Get lists
157    
158 vuko 1.1 // Z->mumu
159     Handle<CandidateCollection> zmumuCands;
160     iEvent.getByLabel( theZmumuCollection , zmumuCands);
161    
162    
163     // Z->ee
164     Handle<CandidateCollection> zeeCands;
165     iEvent.getByLabel( theZeeCollection , zeeCands);
166    
167 vuko 1.3 // Z->ee
168     Handle<CandidateCollection> zllCands;
169     iEvent.getByLabel( theZllCollection , zllCands);
170    
171 vuko 1.1 // loose electrons
172     Handle<CandidateCollection> looseElectronCands;
173     iEvent.getByLabel( theLooseElectronCollection , looseElectronCands);
174    
175     // good electrons
176     Handle<CandidateCollection> goodElectronCands;
177     iEvent.getByLabel( theGoodElectronCollection , goodElectronCands);
178    
179     // loose muons
180     Handle<CandidateCollection> looseMuonCands;
181     iEvent.getByLabel( theLooseMuonCollection , looseMuonCands);
182    
183     // good muons
184     Handle<CandidateCollection> goodMuonCands;
185     iEvent.getByLabel( theGoodMuonCollection , goodMuonCands);
186    
187    
188     // tight leptons
189     Handle<CandidateCollection> tightLeptonCands;
190     iEvent.getByLabel( theTightLeptonCollection , tightLeptonCands);
191    
192 vuko 1.10 // jets
193 vuko 1.14
194     Handle<CaloJetCollection> jetCands;
195     //CandidateCollection> jetCands;
196 vuko 1.10 iEvent.getByLabel( theJetCollection , jetCands);
197    
198 vuko 1.26
199    
200     // event weights (CSA07 soups...)
201     eventWeight = -1;
202    
203     Handle< double> weightHandle;
204 smorovic 1.31 Handle<int> genProcessID;
205     Handle<double> genEventScale;
206    
207 vuko 1.26 if (getEventWeight) {
208     iEvent.getByLabel (theWeightCollection, weightHandle);
209     if (weightHandle.isValid()) {
210     eventWeight = * weightHandle;
211     }
212 smorovic 1.31 iEvent.getByLabel( "genEventProcID", genProcessID );
213 smorovic 1.32 if (genProcessID.isValid()) {
214     processID = *genProcessID;
215 smorovic 1.31 }
216     iEvent.getByLabel( "genEventScale", genEventScale );
217 smorovic 1.32 if (genEventScale.isValid()) {
218     eventScale = *genEventScale;
219 smorovic 1.31 }
220 vuko 1.26 }
221    
222    
223 smorovic 1.6 // get hold of TriggerResults
224 senka 1.27 Handle<TriggerResults> HLTR; //moja promjena: ovo prebaceno 2 reda nize!
225 smorovic 1.6 if (storeHLTresults) {
226 senka 1.27 // Handle<TriggerResults> HLTR;
227 smorovic 1.23 iEvent.getByLabel(triggerResultsTag,HLTR);
228 vuko 1.7 if (firstTriggerResult) {
229     firstTriggerResult = false;
230     triggerNames= HLTR->getTriggerNames();
231     numTriggers = triggerNames.size();
232     }
233 smorovic 1.6
234 vuko 1.7 }
235 vuko 1.1
236 senka 1.9
237 senka 1.28 // MET corrected (P_muons, P_muons_calo_deposit)
238 vuko 1.1
239 senka 1.28 reco::Particle::LorentzVector MET=computeMET(iEvent, iSetup, looseMuonCands);
240     MET_energy=MET.energy();
241     MET_pt=MET.pt();
242     MET_eta=MET.eta();
243    
244 vuko 1.1
245 senka 1.28 // selected muons
246 vuko 1.1
247     cout << "\t # loose mu : " << looseMuonCands->size()
248     << "\t # tight mu : " << goodMuonCands->size()
249     << "\t # loose e : " << looseElectronCands->size()
250     << "\t # tight e : " << goodElectronCands->size()
251     << "\t # tight l : " << tightLeptonCands->size()
252     << "\t # Z->mumu : " << zmumuCands->size()
253     << "\t # Z->ee : " << zeeCands->size()
254 vuko 1.7 << "\t # Z->ll : " << zllCands->size()
255 vuko 1.1 << endl;
256    
257 senka 1.27 N_looseMuons=0;
258     N_looseElectrons=0;
259     N_looseMuons = looseMuonCands->size();
260     N_looseElectrons = looseElectronCands->size();
261    
262    
263 vuko 1.1 //
264 vuko 1.7 // Find best Z (in Z->ll collection (merged Z->ee & Z->mumu)
265 vuko 1.1 //
266     ::OverlapChecker overlap;
267    
268 vuko 1.3
269     float dzmass_min = 100.;
270    
271 senka 1.27 n=1; // number of non overlaping Zs
272 vuko 1.3 for(CandidateCollection::const_iterator z1 = zllCands->begin();
273     z1 != zllCands->end(); ++ z1 ) {
274 vuko 1.21
275     // Check that two leptons used to build the Z are not overlapping
276     if (overlap(*(z1->daughter(0)),*(z1->daughter(1))) ) {
277     cout << "Reject Z candidate: built with overlapping leptons - flavour : "
278     << z1->daughter(0)->pdgId() << endl;
279 smorovic 1.32 continue;
280 vuko 1.21 }
281    
282 vuko 1.3 float dzmass = fabs( z1->mass() - 91.188);
283     if (dzmass < dzmass_min) {
284 vuko 1.7 theZCandidate = &(*z1);
285 vuko 1.3 dzmass_min = dzmass;
286     }
287     //
288     //Get back Electrons from Z and W
289     for(CandidateCollection::const_iterator z2 = z1;
290     z2 != zllCands->end(); ++ z2 ) {
291     if (z1 != z2) {
292     if (overlap(*z1,*z2)) {
293 vuko 1.7 cout << "Overlap between two Z of flavour "
294     << z1->daughter(0)->pdgId() << "\t"
295     << z2->daughter(0)->pdgId() << "\t"
296     << endl;
297 vuko 1.3 } else {
298     cout << "NON OVERLAPPING Zs " << endl;
299 senka 1.27 n++;
300 vuko 1.3 }
301     }
302     }
303     }
304    
305    
306 vuko 1.1 zFlavour = 0;
307     wlFlavour = 0;
308    
309 senka 1.9 dPhi_WlZ_reco=-15.;
310     dPhi_WZ_reco=-15.;
311 vuko 1.4
312 vuko 1.3
313 senka 1.15 //---------------
314     // cout<<"??????????????????? before MatchedGenParticle(&(*looseMuonCands)[0]);" <<endl;
315     // matching(iEvent,looseMuonCands, 13);
316     // matching(iEvent,looseElectronCands, 11);
317     // MatchedGenParticle(&(*looseMuonCands)[0]);
318     //-------------------
319    
320 vuko 1.1 int nwl_candidates=0;
321     if (theZCandidate) {
322    
323    
324     zMass = theZCandidate->mass();
325     zFlavour = abs(theZCandidate->daughter(0)->pdgId());
326     zPt = theZCandidate->pt();
327     zEta = theZCandidate->eta();
328     zPhi = theZCandidate->phi();
329    
330 vuko 1.18 // Sanity check: can we identify the Z daughters with leptons from the loose lists
331    
332     Handle<CandidateCollection> looseLeptonCands;
333     if (zFlavour == 11) {
334     looseLeptonCands = looseElectronCands;
335     } else if (zFlavour == 13) {
336     looseLeptonCands = looseMuonCands;
337     }
338     for (int i=0; i<2; i++) {
339     int noverlaps=0;
340     for(CandidateCollection::const_iterator l = looseLeptonCands->begin();
341     l != looseLeptonCands->end(); l++) {
342     if (overlap(*(theZCandidate->daughter(i)), *l) ) {
343 vuko 1.25 // cout << "found overlap: pt = " << l->pt()
344     // << "\t eta = " << l->eta()
345     // << "\t phi = " << l->phi()
346     // << "\t compared to : pt " << theZCandidate->daughter(i)->pt()
347     // << "\t eta = " << theZCandidate->daughter(i)->eta()
348     // << "\t phi = " << theZCandidate->daughter(i)->phi()
349     // << endl;
350 vuko 1.18 noverlaps++;
351     }
352     }
353 vuko 1.25 // cout << "Z Flavour : " << zFlavour
354     // << "\t # overlaps for daughter " << i << " : " << noverlaps
355     // << "\t (out of " << looseLeptonCands->size() << " possible) "
356     // << endl;
357 vuko 1.18 }
358    
359 senka 1.15 matching(iEvent,looseMuonCands, 13);
360     matching(iEvent,looseElectronCands, 11);
361    
362 vuko 1.4 if (zFlavour == 11) {
363 vuko 1.25 // cout << " filling Z lepton electron properties \n";
364     // if (theZCandidate->daughter(0)) cout << " found Z daughter \n";
365 senka 1.17 //--------------
366 vuko 1.18 leptonProperties["ZEl1"]->FillProperties(theZCandidate->daughter(0), iEvent, iSetup,
367     MatchedGenParticle(theZCandidate->daughter(0)),dR(theZCandidate->daughter(0)));
368     leptonProperties["ZEl2"]->FillProperties(theZCandidate->daughter(1), iEvent, iSetup,
369     MatchedGenParticle(theZCandidate->daughter(1)),dR(theZCandidate->daughter(1)));
370 senka 1.17 } else if (zFlavour == 13) {
371     //--------------
372 vuko 1.25 // cout << " filling Z lepton muon properties \n";
373     // if (theZCandidate->daughter(0)) cout << "found Z daughter \n";
374 senka 1.19 const reco::Candidate * mcpart = MatchedGenParticle(theZCandidate->daughter(0));
375     if (mcpart) {
376 senka 1.22 // cout << "GenPt = genParticle->pt()="<< mcpart->pt() <<"\n";
377 senka 1.19 } else {
378     cout << "NO MATCHED GEN PARTICLE \n";
379     }
380 senka 1.24 // cout << "dR="<< dR(theZCandidate->daughter(0))<<"\n";
381 senka 1.17 //--------------
382 vuko 1.18 leptonProperties["Zmu1"]->FillProperties(theZCandidate->daughter(0), iEvent, iSetup,
383     MatchedGenParticle(theZCandidate->daughter(0)),dR(theZCandidate->daughter(0)));
384     leptonProperties["Zmu2"]->FillProperties(theZCandidate->daughter(1), iEvent, iSetup,
385     MatchedGenParticle(theZCandidate->daughter(1)),dR(theZCandidate->daughter(1)));
386 senka 1.15
387 vuko 1.4 }
388    
389 vuko 1.1 float max_pt = 0.;
390    
391 senka 1.9 /// Find W lepton
392    
393 vuko 1.1
394     // Now find lepton that will be associated to W
395     // among leptons not used for the Z reconstruction
396     for(CandidateCollection::const_iterator lepton = tightLeptonCands->begin();
397     lepton != tightLeptonCands->end(); lepton++) {
398    
399    
400     if ( overlap(*theZCandidate, *lepton) ) continue; // Ignore if lepton used for the Z
401    
402     nwl_candidates++;
403    
404     // If more than one candidate: choose leading pt
405     if (lepton->pt() > max_pt) {
406     theWlepton = &(*lepton);
407     max_pt = lepton->pt();
408     }
409    
410     if(lepton->hasMasterClone()) {
411     cout << "SHOUT: TIGHT LEPTON IS SHALLOW COPY !!! (SHOULD NOT BE!) \n";
412     }
413     //
414     }
415     if (theWlepton) {
416     wlFlavour = theWlepton->pdgId();
417     wlCharge = theWlepton->charge();
418     wlPt = theWlepton->pt();
419     wlEta = theWlepton->eta();
420     wlPhi = theWlepton->phi();
421    
422 senka 1.9 cout << "found W lepton \n";
423 vuko 1.1
424 vuko 1.3 if (abs(wlFlavour) == 11) {
425 senka 1.17 leptonProperties["Wel"]->FillProperties(theWlepton, iEvent, iSetup, MatchedGenParticle(theWlepton),dR(theWlepton));
426 vuko 1.3 } else if (abs(wlFlavour) == 13) {
427 vuko 1.25 // cout << "found W lepton: it is a muon \n";
428 senka 1.17 leptonProperties["Wmu"]->FillProperties(theWlepton, iEvent, iSetup, MatchedGenParticle(theWlepton),dR(theWlepton));
429 vuko 1.3 }
430 senka 1.24
431 senka 1.9 dPhi_WlZ_reco=acos(cos(theWlepton->phi()-theZCandidate->phi()));
432     dPhi_WZ_reco=acos(cos((theWlepton->p4()+MET).phi()-theZCandidate->phi()));
433 vuko 1.3
434 vuko 1.20 // const reco::Muon * muon = dynamic_cast<const reco::Muon *>(&(*theWlepton));
435     // if(muon != 0) { /* it's a valid muon */
436     // cout << "lepton is a muon \n" << endl;
437     // }
438     // const reco::PixelMatchGsfElectron * electron = dynamic_cast<const reco::PixelMatchGsfElectron *>(&(*theWlepton));
439     // if (electron != 0) {
440     // cout << "lepton is an electron \n" << endl;
441     // }
442 vuko 1.1 }
443    
444     }
445 smorovic 1.6
446 vuko 1.10 // Handle <reco::GenParticleCandidateCollection> mccands;
447     Handle<CandidateCollection> mccands;
448     iEvent.getByLabel( theMCTruthCollection,mccands );
449 smorovic 1.6
450 vuko 1.11 collectMCsummary(mccands);
451 smorovic 1.6
452 smorovic 1.8 if (storeHLTresults) {
453    
454 smorovic 1.6 //def: 1:single electron 2:single relaxed 3:double electron 4: double relaxed 5,6,7,8: muon e+mu: 9
455 smorovic 1.8
456 smorovic 1.6 triggerBitmask=0;
457 smorovic 1.8
458 smorovic 1.6 for (size_t i=0; i<numTriggers; ++i) {
459    
460     // cout << "trigName" << triggerNames[i] << "wasRUN:" << HLTR->wasrun(i) << "error " << HLTR->error(i)
461     // << " accept" << HLTR->accept(i) << "\n";
462    
463     if (!HLTR->wasrun(i)) {
464     //cout << "trigger not run? "<< triggerNames[i] << "\n";
465     continue;
466     }
467     if (HLTR->error(i) ) {
468     //cout << "error trigger: "<< triggerNames[i] <<"\n";
469     continue;
470     }
471     if (HLTR->accept(i)) {
472     //if (triggerNames[i].compare("mcpath")!=0)
473     //TRaccept=true;
474     }else {
475     continue;
476     }
477     if (triggerNames[i].compare("HLT1Electron")==0) triggerBitmask |= 1;
478     if (triggerNames[i].compare("HLT1ElectronRelaxed")==0) triggerBitmask |= 2;
479     if (triggerNames[i].compare("HLT2Electron")==0) triggerBitmask |= 4;
480     if (triggerNames[i].compare("HLT2ElectronRelaxed")==0) triggerBitmask |= 8;
481    
482     if (triggerNames[i].compare("HLT1MuonIso")==0) triggerBitmask |= 16;
483     if (triggerNames[i].compare("HLT1MuonNonIso")==0) triggerBitmask |= 32;
484     if (triggerNames[i].compare("HLT2MuonNonIso")==0) triggerBitmask |= 64;
485     if (triggerNames[i].compare("HLT2MuonZ")==0) triggerBitmask |= 128;
486    
487     if (triggerNames[i].compare("HLTXElectronMuon")==0) triggerBitmask |= 256;
488     if (triggerNames[i].compare("HLTXElectronMuonRelaxed")==0) triggerBitmask |= 512;
489     }
490     }
491 vuko 1.10
492     // Jet properties
493    
494     float max_et = 0.;
495 vuko 1.14 float max2_et = 0.;
496     const Candidate * leadingJet = 0;
497     const Candidate * secondJet = 0;
498 smorovic 1.29
499     bool matchedLept[3]={false,false,false};
500    
501 vuko 1.14 // for(CandidateCollection::const_iterator jet = jetCands->begin();
502     for(CaloJetCollection::const_iterator jet = jetCands->begin();
503 vuko 1.10 jet != jetCands->end(); jet++) {
504 smorovic 1.29
505     //ignore jets which are within dR 0.15 to a WZ lepton
506     {
507     float dPhi[3]={-1.0,-1.0,-1.0};
508     float dEta[3]={-1.0,-1.0,-1.0};
509     bool skipJet=false;
510    
511 smorovic 1.32 for (int ct=0;ct<2;ct++) {
512     if (theZCandidate)
513 smorovic 1.29 if (theZCandidate->daughter(ct)->pt()>0
514     && (abs(theZCandidate->daughter(ct)->pdgId())==13
515 smorovic 1.32 || abs(theZCandidate->daughter(ct)->pdgId())==11) ) {
516 smorovic 1.29 dPhi[ct]=fabs(theZCandidate->daughter(ct)->phi() - jet->phi());
517 smorovic 1.32 dEta[ct]=fabs(theZCandidate->daughter(ct)->eta() - jet->eta());
518     }
519 smorovic 1.29 }
520 smorovic 1.30 if (theWlepton)
521 smorovic 1.32 if (theWlepton->pt()>0 && (abs(theWlepton->pdgId())==13 || abs(theWlepton->pdgId())==11 )) {
522 smorovic 1.29 dPhi[2]=fabs(theWlepton->phi() - jet->phi());
523     dEta[2]=fabs(theWlepton->eta() - jet->eta());
524     }
525     for (int k=0;k<3;k++) {
526     if (matchedLept[k] || dPhi[k]<0.0 || dEta[k]<0.0) continue;
527    
528     while (dPhi[k]>6.28318531) dPhi[k]-= 6.28318531;
529     if (dPhi[k]>3.14159265) dPhi[k]= 6.28318531-dPhi[k];
530    
531     float deltaR=sqrt(dPhi[k]*dPhi[k]+dEta[k]*dEta[k]);
532     if (deltaR<0.15) {
533     skipJet=true;
534     matchedLept[k]=true;
535     break;
536     }
537     }
538     if (skipJet) continue;
539     }
540    
541 vuko 1.10 if (jet->et() > max_et ) {
542 vuko 1.14 if (leadingJet) {
543     secondJet = leadingJet;
544     max2_et = max_et;
545     }
546 vuko 1.10 leadingJet = &(*jet);
547     max_et = jet->et();
548 smorovic 1.29
549 vuko 1.14 } else {
550     if (jet->et() > max2_et ) {
551     secondJet = &(*jet);
552     max2_et = jet->et();
553     }
554 vuko 1.10 }
555     }
556     if (leadingJet) {
557     jet1Et = leadingJet->et();
558 vuko 1.14 jet1Phi = leadingJet->phi();
559 vuko 1.10 jet1Eta = leadingJet->eta();
560    
561     } else {
562     jet1Et = -10;
563     jet1Phi = 0.;
564     jet1Eta = 0.;
565     }
566 vuko 1.14 if (secondJet) {
567     jet2Et = secondJet->et();
568     jet2Phi = secondJet->phi();
569     jet2Eta = secondJet->eta();
570     } else {
571     jet2Et = -10;
572     jet2Phi = 0.;
573     jet2Eta = 0.;
574    
575     }
576 senka 1.9
577 senka 1.12 // matching:
578    
579     matching(iEvent,looseMuonCands, 13);
580     matching(iEvent,looseElectronCands, 11);
581    
582 senka 1.13 getMother(&(*mccands)[1]);
583 vuko 1.10
584 vuko 1.1 wzTree->Fill();
585    
586 vuko 1.20
587    
588     // Fill electrons in a tree
589    
590     for(CandidateCollection::const_iterator el = looseElectronCands->begin();
591     el != looseElectronCands->end(); ++ el ) {
592    
593     electronUse = 0;
594    
595     if (theZCandidate) {
596    
597     if (overlap(*theZCandidate, *el) ) {
598     electronUse = 23;
599     } else if (theWlepton) {
600     if (overlap(*theWlepton, *el) ) {
601     electronUse = 24;
602     }
603     leptonProperties["electron"]->FillProperties(&(*el),
604     iEvent, iSetup,
605     MatchedGenParticle(&(*el)),dR(&(*el)));
606     electronTree->Fill();
607    
608     }
609     }
610     }
611    
612    
613 vuko 1.1 }
614    
615    
616     // ------------ method called once each job just before starting event loop ------------
617     void
618     WZAnalyzer::beginJob(const edm::EventSetup&)
619     {
620    
621     using namespace wzana;
622    
623 senka 1.16 // theFile = new TFile( "wz.root", "RECREATE" ) ;
624     theFile = new TFile( fOutputFileName.c_str(), "RECREATE" ) ;
625    
626 vuko 1.1
627     wzTree = new TTree("WZTree","WZ informations");
628    
629 vuko 1.20 electronTree = new TTree("ElTree","electron informations");
630    
631     leptonProperties["electron"] = new ElectronProperties();
632 vuko 1.1
633 vuko 1.3 leptonProperties["Wel"] = new ElectronProperties();
634     leptonProperties["ZEl1"] = new ElectronProperties();
635     leptonProperties["ZEl2"] = new ElectronProperties();
636    
637     leptonProperties["Wmu"] = new MuonProperties();
638     leptonProperties["Zmu1"] = new MuonProperties();
639     leptonProperties["Zmu2"] = new MuonProperties();
640 vuko 1.1
641 vuko 1.20
642 vuko 1.3 leptonProperties["Wel"]->CreateBranches(wzTree, "WEl_");
643 vuko 1.4 leptonProperties["ZEl1"]->CreateBranches(wzTree, "ZEl1_");
644     leptonProperties["ZEl2"]->CreateBranches(wzTree, "ZEl2_");
645 vuko 1.1
646 senka 1.9 leptonProperties["Wmu"] ->CreateBranches(wzTree, "Wmu_");
647     leptonProperties["Zmu1"] ->CreateBranches(wzTree, "Zmul_");
648     leptonProperties["Zmu2"] ->CreateBranches(wzTree, "Zmu2_");
649 vuko 1.20 leptonProperties["electron"] ->CreateBranches(electronTree);
650 senka 1.9
651 vuko 1.1 initialiseTree();
652 smorovic 1.6
653     //prepare HLT TriggerResults branch
654     if (storeHLTresults) {
655     wzTree->Branch("WZ_hltBitmask",&triggerBitmask,"WZ_hltBitmask/I");
656    
657     }
658 senka 1.9
659     // MET branch
660     wzTree->Branch("MET_energy",&MET_energy,"MET_energy/F");
661     wzTree->Branch("MET_pt",&MET_pt,"MET_pt/F");
662     wzTree->Branch("MET_eta",&MET_eta,"MET_eta/F");
663     wzTree->Branch("dPhi_WlZ_reco",&dPhi_WlZ_reco,"dPhi_WlZ_reco/F");
664     wzTree->Branch("dPhi_WZ_reco",&dPhi_WZ_reco,"dPhi_WZ_reco/F");
665 senka 1.27
666     // # of non overlaping Zs
667     wzTree->Branch("N_nonOverlaping_Z",&n,"N_nonOverlaping_Z/I");
668     wzTree->Branch("N_looseMuons",&N_looseMuons,"N_looseMuons/I");
669     wzTree->Branch("N_looseElectrons",&N_looseElectrons,"N_looseElectrons/I");
670    
671 vuko 1.1 }
672    
673     // ------------ method called once each job just after ending the event loop ------------
674     void
675     WZAnalyzer::endJob() {
676    
677     theFile->Write();
678     theFile->Close();
679    
680     }
681    
682    
683     void WZAnalyzer::initialiseTree() {
684 vuko 1.26 // Event properties
685     wzTree->Branch("weight", &eventWeight, "weight/F");
686 smorovic 1.31 wzTree->Branch("processID", &processID, "processID/I");
687     wzTree->Branch("genEventScale", &eventScale, "genEventScale/I");
688 vuko 1.26
689 vuko 1.1 // Z properties
690     wzTree->Branch("Zmass", &zMass, "Zmass/F");
691     wzTree->Branch("ZId", &zFlavour, "Zid/I");
692     wzTree->Branch("ZPt", &zPt, "ZPt/F");
693     wzTree->Branch("ZEta", &zEta, "ZEta/F");
694     wzTree->Branch("ZPhi", &zPhi, "ZPhi/F");
695    
696     // W Properties
697     wzTree->Branch("WlId", &wlFlavour, "Wlid/I");
698     wzTree->Branch("WlCharge", &wlCharge, "WlCharge/I");
699     wzTree->Branch("WlPt", &wlPt, "WlPt/F");
700 smorovic 1.8
701     wzTree->Branch("MC_leptonZ1_pt", &MCleptZ1_pt,"MC_leptonZ1_pt/F");
702     wzTree->Branch("MC_leptonZ2_pt", &MCleptZ2_pt,"MC_leptonZ2_pt/F");
703     wzTree->Branch("MC_leptonW_pt", &MCleptW_pt, "MC_leptonW_pt/F");
704    
705     wzTree->Branch("MC_leptonZ1_eta", &MCleptZ1_eta,"MC_leptonZ1_eta/F");
706     wzTree->Branch("MC_leptonZ2_eta", &MCleptZ2_eta,"MC_leptonZ2_eta/F");
707     wzTree->Branch("MC_leptonW_eta", &MCleptW_eta, "MC_leptonW_eta/F");
708    
709     wzTree->Branch("MC_leptonZ1_phi", &MCleptZ1_phi,"MC_leptonZ1_phi/F");
710     wzTree->Branch("MC_leptonZ2_phi", &MCleptZ2_phi,"MC_leptonZ2_phi/F");
711     wzTree->Branch("MC_leptonW_phi", &MCleptW_phi, "MC_leptonW_phi/F");
712    
713     wzTree->Branch("MC_leptonZ1_pdgid", &MCleptZ1_pdgid,"MC_leptonZ1_pdgid/I");
714     wzTree->Branch("MC_leptonZ2_pdgid", &MCleptZ2_pdgid,"MC_leptonZ2_pdgid/I");
715     wzTree->Branch("MC_leptonW_pdgid", &MCleptW_pdgid, "MC_leptonW_pdgid/I");
716 vuko 1.14
717     /*
718     wzTree->Branch("MC_leptonW_origin", &MCleptW_pdgid, "MC_leptonW_origin/I");
719     wzTree->Branch("MC_leptonZ1_origin", &MCleptW_pdgid, "MC_leptonZ1_origin/I");
720     wzTree->Branch("MC_leptonZ2_origin", &MCleptW_pdgid, "MC_leptonZ2_origin/I");
721     */
722    
723 smorovic 1.8 wzTree->Branch("MC_TauDecayType_fromZ1", &MC_tauDecayTypeZ1,"MC_TauDecayType_fromZ1/I");
724     wzTree->Branch("MC_TauDecayType_fromZ2", &MC_tauDecayTypeZ2,"MC_TauDecayType_fromZ2/I");
725     wzTree->Branch("MC_TauDecayType_fromW", &MC_tauDecayTypeW, "MC_TauDecayType_fromW/I");
726 vuko 1.10
727     // MET properties
728    
729     // Jet properties
730     wzTree->Branch("Jet1Et", &jet1Et, "Jet1Et/F");
731     wzTree->Branch("Jet1Phi", &jet1Phi, "Jet1Phi/F");
732     wzTree->Branch("Jet1Eta", &jet1Eta, "Jet1Eta/F");
733    
734     wzTree->Branch("Jet2Et", &jet2Et, "Jet2Et/F");
735     wzTree->Branch("Jet2Phi", &jet2Phi, "Jet2Phi/F");
736     wzTree->Branch("Jet2Eta", &jet2Eta, "Jet2Eta/F");
737    
738 vuko 1.20 //
739     electronTree->Branch("used", &electronUse, "used/I");
740 vuko 1.10
741 smorovic 1.8 }
742 vuko 1.1
743 senka 1.9
744 senka 1.28 //////////////////////////////////////////
745     // MET & MET corrections
746     // MET=MET-sum_p(muons)-sum_p(muons calos depositions)
747    
748     reco::Particle::LorentzVector WZAnalyzer::computeMET(const edm::Event& iEvent, const edm::EventSetup& iSetup, Handle<reco::CandidateCollection> MuonCands) {
749 senka 1.9
750     using namespace edm;
751     using namespace reco;
752     using namespace std;
753    
754     Handle<CaloMETCollection> mets;
755     iEvent.getByLabel("met", mets);
756 vuko 1.25 // cout << "# METs=" << mets->size() << endl;
757    
758     if (mets->size() != 1) {
759     cout << "ALARM: # METS is not 1: !" << endl;
760     return reco::Particle::LorentzVector(0.,0.,0.,0.);
761     }
762    
763 vuko 1.14 for (CaloMETCollection::const_iterator met = mets->begin();
764     met!= mets->end(); met++) {
765 vuko 1.25 // cout << "MET: E = " << met->energy()
766     // << "\t Pt = " << met->pt()
767     // <<endl;
768 vuko 1.14 }
769 senka 1.28
770     // sum of p of muons
771     reco::Particle::LorentzVector Muon_p(0,0,0,0); // sum of muons 4-vectors
772     reco::Particle::LorentzVector Muon_p_4v(0,0,0,0); // sum of muons 4-vectors in (px,py,0,Et), since MET looks like that
773     double px=0;
774     double py=0;
775     double pz=0;
776     double Et=0;
777    
778     for (unsigned int i=0; i<MuonCands->size();i++){
779     Muon_p=Muon_p+(*MuonCands)[i].p4();
780     px=(*MuonCands)[i].px();
781     py=(*MuonCands)[i].py();
782     pz=(*MuonCands)[i].pz();
783     Et=(*MuonCands)[i].et();
784     Muon_p_4v=Muon_p_4v+reco::Particle::LorentzVector(px,py,0,Et);
785     }
786     // cout<< "####### sum Muon_p="<< Muon_p<<endl;
787     // cout<< "####### sum Muon_p correct="<< Muon_p_4v<<endl;
788     // sum of p of muons left in calorimeters
789     reco::Particle::LorentzVector Muon_p_cal(0,0,0,0); // sum of muons depositions 4-vectors
790     reco::Particle::LorentzVector Muon_p_cal_4v(0,0,0,0); // sum of muons depositions 4-vectors in (px,py,0,Et), since MET looks like that
791     edm::Handle<reco::MuIsoDepositAssociationMap> ecalIso;
792     iEvent.getByLabel("muGlobalIsoDepositCalByAssociatorTowers","ecal", ecalIso);
793     edm::Handle<reco::MuIsoDepositAssociationMap> hcalIso;
794     iEvent.getByLabel("muGlobalIsoDepositCalByAssociatorTowers","hcal", hcalIso);
795     edm::Handle<reco::MuIsoDepositAssociationMap> hocalIso;
796     iEvent.getByLabel("muGlobalIsoDepositCalByAssociatorTowers","ho", hocalIso);
797    
798     float Et_ecal;
799     float Et_hcal;
800     float Et_ho;
801     double eta_ecal;
802     double eta_hcal;
803     double eta_ho;
804     double phi_ecal;
805     double phi_hcal;
806     double phi_ho;
807     double tg_theta_pola_ecal;
808     double tg_theta_pola_hcal;
809     double tg_theta_pola_ho;
810     double tg_theta_ecal;
811     double tg_theta_hcal;
812     double tg_theta_ho;
813     double E_ecal;
814     double E_hcal;
815     double E_ho;
816     double me=0.0005;
817     double sin_theta_ecal, sin_theta_hcal, sin_theta_ho, cos_theta_ecal,cos_theta_hcal,cos_theta_ho;
818     double p_ecal, p_hcal,p_ho, pt_ecal,pt_hcal,pt_ho, px_ecal,px_hcal,px_ho, py_ecal,py_hcal,py_ho, pz_ecal,pz_hcal,pz_ho;
819    
820     for (unsigned int i=0; i<MuonCands->size();i++){
821     Et_ecal=-2.;
822     Et_hcal=-2.;
823     Et_ho=-2.;
824     eta_ecal=-2.;
825     eta_hcal=-2.;
826     eta_ho=-2.;
827     phi_ecal=-2.;
828     phi_hcal=-2.;
829     phi_ho=-2.;
830     const reco::Muon* muons=dynamic_cast<const reco::Muon *> (&(*MuonCands)[i]);
831     const reco::MuIsoDeposit& depEcal = (*ecalIso)[muons->combinedMuon()];
832     const reco::MuIsoDeposit& depHcal = (*hcalIso)[muons->combinedMuon()];
833     const reco::MuIsoDeposit& depHOcal = (*hocalIso)[muons->combinedMuon()];
834     Et_ecal=depEcal.muonEnergy();
835     Et_hcal=depHcal.muonEnergy();
836     Et_ho=depHOcal.muonEnergy();
837     eta_ecal=depEcal.eta();
838     eta_hcal=depHcal.eta();
839     eta_ho=depHOcal.eta();
840     phi_ecal=depEcal.phi();
841     phi_hcal=depHcal.phi();
842     phi_ho=depHOcal.phi();
843     tg_theta_pola_ecal=TMath::Exp(-eta_ecal);
844     tg_theta_pola_hcal=TMath::Exp(-eta_hcal);
845     tg_theta_pola_ho=TMath::Exp(-eta_ho);
846     tg_theta_ecal=2*tg_theta_pola_ecal/(1-tg_theta_pola_ecal*tg_theta_pola_ecal);
847     tg_theta_hcal=2*tg_theta_pola_hcal/(1-tg_theta_pola_hcal*tg_theta_pola_hcal);
848     tg_theta_ho=2*tg_theta_pola_ho/(1-tg_theta_pola_ho*tg_theta_pola_ho);
849     E_ecal=TMath::Abs(Et_ecal*TMath::Sqrt(1+tg_theta_ecal*tg_theta_ecal)/tg_theta_ecal);
850     E_hcal=TMath::Abs(Et_hcal*TMath::Sqrt(1+tg_theta_hcal*tg_theta_hcal)/tg_theta_hcal);
851     E_ho=TMath::Abs(Et_ho*TMath::Sqrt(1+tg_theta_ho*tg_theta_ho)/tg_theta_ho);
852     sin_theta_ecal=tg_theta_ecal/TMath::Sqrt(1+tg_theta_ecal*tg_theta_ecal);
853     sin_theta_hcal=tg_theta_hcal/TMath::Sqrt(1+tg_theta_hcal*tg_theta_hcal);
854     sin_theta_ho=tg_theta_ho/TMath::Sqrt(1+tg_theta_ho*tg_theta_ho);
855     cos_theta_ecal=TMath::Sqrt(1-sin_theta_ecal*sin_theta_ecal);
856     cos_theta_hcal=TMath::Sqrt(1-sin_theta_hcal*sin_theta_hcal);
857     cos_theta_ho=TMath::Sqrt(1-sin_theta_ho*sin_theta_ho);
858     if (E_ecal<me){
859     p_ecal=0;
860     }
861     else {
862     p_ecal=TMath::Sqrt(E_ecal*E_ecal-me*me);
863     }
864     if (E_hcal<me){
865     p_hcal=0;
866     }
867     else {
868     p_hcal=TMath::Sqrt(E_hcal*E_hcal-me*me);
869     }
870     if (E_ho<me){
871     p_ho=0;
872     }
873     else {
874     p_ho=TMath::Sqrt(E_ho*E_ho-me*me);
875     }
876    
877     pz_ecal=p_ecal*cos_theta_ecal;
878     pz_hcal=p_hcal*cos_theta_hcal;
879     pz_ho=p_ho*cos_theta_ho;
880     pt_ecal=TMath::Abs(p_ecal*sin_theta_ecal);
881     pt_hcal=TMath::Abs(p_hcal*sin_theta_hcal);
882     pt_ho=TMath::Abs(p_ho*sin_theta_ho);
883     px_ecal=pt_ecal*TMath::Cos(phi_ecal);
884     px_hcal=pt_hcal*TMath::Cos(phi_hcal);
885     px_ho=pt_ho*TMath::Cos(phi_ho);
886     py_ecal=pt_ecal*TMath::Sin(phi_ecal);
887     py_hcal=pt_hcal*TMath::Sin(phi_hcal);
888     py_ho=pt_ho*TMath::Sin(phi_ho);
889    
890     Muon_p_cal=Muon_p_cal + reco::Particle::LorentzVector(px_ecal+px_hcal+px_ho,py_ecal+py_hcal+py_ho,pz_ecal+pz_hcal+pz_ho,E_ecal+E_hcal+E_ho);
891     Muon_p_cal_4v=Muon_p_cal_4v + reco::Particle::LorentzVector(px_ecal+px_hcal+px_ho,py_ecal+py_hcal+py_ho,0,Et_ecal+Et_hcal+Et_ho);
892     }
893    
894     reco::Particle::LorentzVector MET=mets->begin()->p4()-Muon_p_4v-Muon_p_cal_4v; // substract p from muons and p of calo muon deposit from MET
895     // cout <<" MET no corrections"<<mets->begin()->p4() << endl;
896     // cout <<" MET with corrections"<< MET << endl;
897    
898     return MET;
899 senka 1.9
900     }
901    
902 senka 1.12 ////////////////////////
903     // matching
904     //
905    
906     class MatchingInfo {
907     public:
908    
909     MatchingInfo(float dr, int gen, int reco) : deltaR(dr), genMuon(gen),recoMuon(reco) {}
910    
911     bool operator<(const MatchingInfo m) { return deltaR < m.deltaR;}
912    
913     float deltaR;
914     int genMuon;
915     int recoMuon;
916    
917     };
918    
919    
920     bool betterMatch(MatchingInfo m1, MatchingInfo m2)
921     { return m1.deltaR < m2.deltaR;}
922    
923    
924     // method that gives the vector<MatchingInfo> matching_Infos containing (dR,i,j); i=genParticle index, j=ParticleCollection index:
925    
926     void WZAnalyzer::matching(const edm::Event& iEvent, Handle<reco::CandidateCollection> cands, int pdgid){
927    
928     using namespace edm;
929     using namespace reco;
930     using namespace std;
931    
932     //-------------------------
933     Handle<CandidateCollection> mccands;
934     iEvent.getByLabel( theMCTruthCollection,mccands );
935    
936     vector <GenParticleCandidate*> genparticles;
937    
938     for(CandidateCollection::const_iterator p = mccands->begin();
939     p != mccands->end(); p++) {
940    
941     // reco::Candidate* p_tmp = &(*p);
942     reco::GenParticleCandidate* ptr = (reco::GenParticleCandidate*) &(*p);
943    
944     if ( (ptr)->status() == 1
945     /*&& (ptr)->momentum().perp() > 2.*/ ) { // stable particles with pt>2 only
946     if ( abs((ptr)->pdgId()) == pdgid ) {
947     genparticles.push_back((ptr));
948     //cout << "electron MCT\n";
949     }
950     }
951     }
952    
953    
954     int n1=0;
955     vector<MatchingInfo> matching_Infos;
956     for ( unsigned int i=0; i<genparticles.size(); i++ ) // po svim MC muonima
957     {
958    
959     for (unsigned int j = 0; j < cands->size(); j++)// po svim reco muonima
960     {
961    
962     double d_eta2=(*cands)[j].eta()-(genparticles)[i]->eta();
963     double d_phi=fabs((*cands)[j].phi()-(genparticles)[i]->phi());
964     double dR_byhand= sqrt(d_eta2*d_eta2+d_phi*d_phi);
965     double dR=dR_byhand;
966    
967     if (dR<0.15){
968     n1++;
969     matching_Infos.push_back(MatchingInfo(dR,i,j));
970     }
971    
972     }
973     }
974 senka 1.24
975 senka 1.12 sort(matching_Infos.begin(),matching_Infos.end(),betterMatch); // sorting (dR,i,j)
976 senka 1.24
977     if (genparticles.size()!=0 && cands->size()!=0){
978     // Now exclude combinations using same gen or rec muons
979     for (std::vector<MatchingInfo>::iterator it=matching_Infos.begin();
980     it != matching_Infos.end(); it++) {
981     for (std::vector<MatchingInfo>::iterator it2=matching_Infos.begin();
982     it2!=it; it2++) {
983     if ( (it->genMuon == it2->genMuon) || (it->recoMuon == it2->recoMuon) ) {
984     matching_Infos.erase(it);
985     it=matching_Infos.begin();
986     break;
987     }
988     }
989     }
990     }
991    
992 vuko 1.14 // Now put result in vector of pairs
993    
994     leptonMatching[pdgid].clear();
995    
996     for (vector<MatchingInfo>::iterator match=matching_Infos.begin();
997 senka 1.16 match !=matching_Infos.end(); match++) { // po tablici dr,i,j
998 vuko 1.14
999     pair<const GenParticleCandidate*, const reco::Candidate *> pair;
1000 senka 1.16 // vector<pair,float > matchedParticles;
1001 vuko 1.14 pair.first = genparticles[match->genMuon];
1002     pair.second = & (*cands)[match->recoMuon];
1003 senka 1.16
1004     matchedParticles cestice(genparticles[match->genMuon] ,& (*cands)[match->recoMuon] , match->deltaR);
1005    
1006     leptonMatching[pdgid].push_back(cestice);
1007 senka 1.24
1008 vuko 1.14 }
1009 senka 1.24
1010 senka 1.12 }
1011    
1012 senka 1.9
1013 senka 1.13 // get mother of particle
1014     // returns mother = 1 if mother is Z boson
1015     // returns mother = 2 if mother is W boson
1016     // returns mother = 4 if mother is b
1017     // returns mother = 0 else
1018    
1019 vuko 1.14 WZAnalyzer::LeptonOrigin WZAnalyzer::getMother(const reco::Candidate* genParticle){
1020    
1021     int pdg_id=genParticle->pdgId();
1022 vuko 1.25 // cout << "particle id : " << pdg_id << endl;
1023 senka 1.13
1024     while (genParticle = genParticle->mother() ) { //izvrsava se sve dok ne dodje do nule tj. dok ne trazi majku od protona
1025 vuko 1.25 // cout << "Going up: ";
1026     // cout << "Mother id : " << genParticle->pdgId() << endl;
1027 senka 1.24 if (abs(genParticle->pdgId())!=abs(pdg_id)) {
1028 vuko 1.25 // cout<< " good mother " <<endl;
1029 senka 1.13 if (abs(genParticle->pdgId())==23){ // mother is Z
1030 vuko 1.14 return zboson;
1031 senka 1.13 }
1032     if (abs(genParticle->pdgId())==24) { // mother is W
1033 vuko 1.14 return wboson;
1034 senka 1.13 }
1035     // if (abs(genParticle->pdgId())==23 || abs(genParticle->pdgId())==24) { // mother is W or Z
1036     // WZ_matched=1;
1037     // mother=3;
1038     // }
1039     if ((((abs(genParticle->pdgId()))/100)%10 ==5)||(((abs(genParticle->pdgId()))/1000)%10 ==5 )) { // mother is b
1040 vuko 1.14 return bdecay;
1041 senka 1.13 }
1042 vuko 1.14 if ((((abs(genParticle->pdgId()))/100)%10 ==4)||(((abs(genParticle->pdgId()))/1000)%10 ==4 )) { // mother is c
1043     return cdecay;
1044     }
1045     return other;
1046 senka 1.13 }
1047 vuko 1.20 }
1048    
1049     return other;
1050 senka 1.13 }
1051    
1052 senka 1.15 const reco::Candidate * WZAnalyzer::MatchedGenParticle(const reco::Candidate * daughter){
1053     ::OverlapChecker overlap;
1054     matched_genParticle=0;
1055 senka 1.16
1056     for (map< int,
1057     std::vector< matchedParticles > > ::iterator iter = leptonMatching.begin();
1058     iter!=leptonMatching.end(); iter++) // entire map
1059     {
1060 vuko 1.20 for (unsigned int j=0; j<iter->second.size(); j++){ // entire vector<class<>>
1061 senka 1.16 const reco::Candidate * nesto=iter->second[j].pair.second;
1062     if (overlap(*nesto,*daughter)){
1063     matched_genParticle=iter->second[j].pair.first;
1064     }
1065     }
1066     }
1067     return matched_genParticle;
1068     }
1069    
1070    
1071     float WZAnalyzer::dR(const reco::Candidate * daughter){
1072 vuko 1.18
1073 senka 1.16 ::OverlapChecker overlap;
1074 vuko 1.18
1075 senka 1.19 float delta_r = -10;
1076 senka 1.15 for (map< int,
1077 senka 1.16 std::vector< matchedParticles > > ::iterator iter = leptonMatching.begin();
1078 senka 1.15 iter!=leptonMatching.end(); iter++) // entire map
1079     {
1080 vuko 1.20 for (unsigned int j=0; j<iter->second.size(); j++){ // entire vector<class<>>
1081 senka 1.16 const reco::Candidate * nesto=iter->second[j].pair.second;
1082 senka 1.15 if (overlap(*nesto,*daughter)){
1083 senka 1.16 delta_r=iter->second[j].delta_R;
1084 senka 1.15 }
1085     }
1086     }
1087 senka 1.16 return delta_r;
1088 senka 1.15 }
1089    
1090 vuko 1.11
1091 senka 1.16
1092 vuko 1.10 void WZAnalyzer::collectMCsummary(Handle <reco::CandidateCollection> mccands) {
1093 senka 1.12
1094 vuko 1.11 using namespace reco;
1095    
1096 smorovic 1.8 //collections
1097 senka 1.24
1098 smorovic 1.8 MCleptZ1_pdgid = -1;
1099     MCleptZ2_pdgid = -1;
1100     MCleptW_pdgid = -1;
1101    
1102     MCleptZ1_pt = -1;
1103     MCleptZ2_pt = -1;
1104     MCleptW_pt = -1;
1105    
1106     MCleptZ1_eta = -1;
1107     MCleptZ2_eta = -1;
1108     MCleptW_eta = -1;
1109    
1110     MCleptZ1_phi = -1;
1111     MCleptZ2_phi = -1;
1112     MCleptW_phi = -1;
1113 senka 1.12
1114 smorovic 1.8 vector<reco::GenParticleCandidate*> Tau;
1115     vector<reco::GenParticleCandidate*> StableMuons;
1116     vector<reco::GenParticleCandidate*> StableElectrons;
1117 senka 1.24
1118 smorovic 1.32
1119    
1120 vuko 1.11 for(CandidateCollection::const_iterator p = mccands->begin();
1121     p != mccands->end(); p++) {
1122 vuko 1.1
1123 vuko 1.11 // reco::Candidate* p_tmp = &(*p);
1124     reco::GenParticleCandidate* ptr = (reco::GenParticleCandidate*) &(*p);
1125 smorovic 1.8
1126     if ( (ptr)->status() == 1
1127     /*&& (ptr)->momentum().perp() > 2.*/ ) { // stable particles with pt>2 only
1128     if ( abs((ptr)->pdgId()) == 11 ) {
1129 senka 1.12
1130 smorovic 1.8 StableElectrons.push_back((ptr));
1131     //cout << "electron MCT\n";
1132     }
1133     else if ( abs((ptr)->pdgId()) == 13 ) {
1134 senka 1.12
1135 smorovic 1.8 StableMuons.push_back((ptr)) ;
1136     //cout << "muon MCT\n";
1137     }
1138     }
1139     else if ((ptr)->status() == 2) {
1140     if ( abs((ptr)->pdgId()) == 15 ) {//tau
1141     Tau.push_back((ptr));
1142     }
1143     }
1144     }
1145 senka 1.12
1146 vuko 1.25 // std::cout << "# Electrons : " << StableElectrons.size()
1147     // << "# Muons : " << StableMuons.size() << std::endl
1148     // << "# Tau : " << Tau.size() << std::endl;
1149 senka 1.12
1150 smorovic 1.8
1151     vector<reco::GenParticleCandidate*> * StableLeptons[3] = {&StableElectrons, &StableMuons, &Tau};
1152    
1153 smorovic 1.32 vector<reco::GenParticleCandidate*> TauChildLeptons;
1154    
1155    
1156     //erase electrons and muons from tau from GenparticleCandidate Vector and put to Tau Vector
1157     for (int i=1; i>=0; i--) {
1158     for ( vector<reco::GenParticleCandidate*>::iterator lepton = StableLeptons[i]->begin();
1159     lepton != StableLeptons[i]->end();lepton++) {
1160    
1161     reco::GenParticleCandidate* mcParticleRef = *lepton;
1162    
1163     int parentItr=0;
1164     while ( mcParticleRef->mother()!=0 && parentItr<2) {
1165    
1166     parentItr++;
1167    
1168     mcParticleRef = (reco::GenParticleCandidate*) mcParticleRef->mother();
1169     if (mcParticleRef==0) break;
1170    
1171     //if tau
1172     if (abs((mcParticleRef)->pdgId())==15 ) {
1173     //put into collection
1174     TauChildLeptons.push_back(*lepton);
1175     StableLeptons[i]->erase(lepton);
1176     lepton--;
1177     break;
1178     }
1179     }
1180     }
1181     }
1182    
1183    
1184 smorovic 1.8 bool firstZlept = true;
1185 smorovic 1.32 int MC_tauDecayTypeZ1 = 0;
1186     int MC_tauDecayTypeZ2 = 0;
1187     int MC_tauDecayTypeW = 0;
1188    
1189    
1190 smorovic 1.8 for (int i=2; i>=0; i--) {
1191     while (StableLeptons[i]->size() > 0) {
1192     float maxPt = 0;
1193     vector<reco::GenParticleCandidate*>::iterator index = StableLeptons[i]->end();
1194    
1195     for ( vector<reco::GenParticleCandidate*>::iterator lepton = StableLeptons[i]->begin();
1196     lepton != StableLeptons[i]->end(); lepton++ ) {
1197 smorovic 1.32
1198 smorovic 1.8 if ((*lepton)->pt() > maxPt) {
1199     maxPt = (*lepton)->pt();
1200     index = lepton;
1201     }
1202     }
1203     bool Zchild = false;
1204     bool Wchild = false;
1205 smorovic 1.32 bool isTau = false;
1206    
1207    
1208 smorovic 1.8 reco::GenParticleCandidate* mcParticleRef = *index;
1209    
1210 smorovic 1.32 if (abs((*index)->pdgId()) == 15) isTau = true;
1211    
1212 smorovic 1.8 //find W/Z mother
1213 smorovic 1.32 int parentItr=0;
1214     while ( mcParticleRef->mother()!=0 && parentItr<2) {
1215    
1216 smorovic 1.8 if (parentItr>=2) break;
1217     parentItr++;
1218     mcParticleRef = (reco::GenParticleCandidate*) mcParticleRef->mother();
1219 smorovic 1.32 if (mcParticleRef==0) break;
1220 smorovic 1.8
1221     if (abs((mcParticleRef)->pdgId())==23 ) {
1222     Zchild=true;
1223     break;
1224     }
1225     if (abs((mcParticleRef)->pdgId())==24 ) {
1226     Wchild=true;
1227     break;
1228     }
1229     }
1230    
1231 smorovic 1.32
1232 smorovic 1.8 if (maxPt >0) {
1233 smorovic 1.32 int * MCtauDecayTypePtr = 0;
1234    
1235 smorovic 1.8 if (Wchild) {
1236     MCleptW_pdgid=(*index)->pdgId();
1237     MCleptW_pt=(float)(*index)->pt();
1238     MCleptW_eta=(float)(*index)->eta();
1239     MCleptW_phi=(float)(*index)->phi();
1240 smorovic 1.32 if (isTau) {
1241     MCtauDecayTypePtr = &MC_tauDecayTypeW;
1242     MC_tauDecayTypeW =3;//default to hadronic decay
1243     }
1244    
1245 smorovic 1.8 }
1246     if (Zchild) {
1247     if (firstZlept) {
1248     firstZlept=false;
1249     MCleptZ1_pdgid=(*index)->pdgId();
1250     MCleptZ1_pt=(float)(*index)->pt();
1251     MCleptZ1_eta=(float)(*index)->eta();
1252     MCleptZ1_phi=(float)(*index)->phi();
1253 smorovic 1.32 if (isTau) {
1254     MCtauDecayTypePtr = &MC_tauDecayTypeZ1;
1255     MC_tauDecayTypeZ1 =3;
1256     }
1257    
1258 smorovic 1.8 }
1259     else {
1260     MCleptZ2_pdgid=(*index)->pdgId();
1261     MCleptZ2_pt=(float)(*index)->pt();
1262     MCleptZ2_eta=(float)(*index)->eta();
1263     MCleptZ2_phi=(float)(*index)->phi();
1264 smorovic 1.32 if (isTau) {
1265     MCtauDecayTypePtr = &MC_tauDecayTypeZ2;
1266     MC_tauDecayTypeZ2 =3;
1267     }
1268    
1269     }
1270     }
1271     //check type of tau decay
1272     if (MCtauDecayTypePtr) {
1273     for( vector<reco::GenParticleCandidate*>::iterator p = TauChildLeptons.begin();p != TauChildLeptons.end(); p++) {
1274     int pitr = 0;
1275     reco::GenParticleCandidate* mcParticleRef = *p;
1276     while (mcParticleRef->mother() && pitr<2) {
1277     pitr++;
1278     mcParticleRef =(reco::GenParticleCandidate*) mcParticleRef->mother();
1279     if (mcParticleRef==0) break;
1280     if (mcParticleRef == *index) {
1281     if (abs((*p)->pdgId()) == 11) *MCtauDecayTypePtr = 1;
1282     if (abs((*p)->pdgId()) == 13) *MCtauDecayTypePtr = 2;
1283     }
1284     }
1285 smorovic 1.8 }
1286 smorovic 1.32 }
1287 smorovic 1.8 }
1288 smorovic 1.32 StableLeptons[i]->erase(index);
1289 smorovic 1.8 }
1290     }
1291 vuko 1.25 // std::cout << "# Electrons end: " << StableElectrons.size()
1292     // << "# Muons end: " << StableMuons.size() << std::endl
1293     // << "# Tau end: " << Tau.size() << std::endl;
1294 vuko 1.1
1295 smorovic 1.8 }
1296 vuko 1.1 //define this as a plug-in
1297 vuko 1.2 // DEFINE_FWK_MODULE(WZAnalyzer);
1298 smorovic 1.32