ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/Vuko/WZAnalysis/src/WZAnalyzer.cc
Revision: 1.43
Committed: Wed Feb 13 10:50:05 2008 UTC (17 years, 2 months ago) by smorovic
Content type: text/plain
Branch: MAIN
CVS Tags: SM_13_FEB_08
Changes since 1.42: +2 -2 lines
Log Message:
add missing variable in header file
counter of electron trees was not updated with new electron selection cuts

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