ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/Vuko/WZAnalysis/src/WZAnalyzer.cc
Revision: 1.41
Committed: Mon Feb 11 17:17:43 2008 UTC (17 years, 2 months ago) by smorovic
Content type: text/plain
Branch: MAIN
CVS Tags: SB_12_Fev_08
Changes since 1.40: +34 -5 lines
Log Message:
add run number, event ID, number of electron trees from event and fix electron tree storing bug

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