ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/Vuko/WZAnalysis/src/WZAnalyzer.cc
Revision: 1.45
Committed: Tue Feb 19 11:50:47 2008 UTC (17 years, 2 months ago) by beaucero
Content type: text/plain
Branch: MAIN
CVS Tags: sm_21_Feb_08, SB_19Feb08
Changes since 1.44: +3 -3 lines
Log Message:
Comment cout for MET

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