ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/Vuko/WZAnalysis/src/WZAnalyzer.cc
Revision: 1.48
Committed: Thu Feb 28 13:21:04 2008 UTC (17 years, 2 months ago) by smorovic
Content type: text/plain
Branch: MAIN
Changes since 1.47: +3 -3 lines
Log Message:
/F

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