ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/Vuko/WZAnalysis/src/WZAnalyzer.cc
Revision: 1.27
Committed: Fri Dec 14 18:43:56 2007 UTC (17 years, 4 months ago) by senka
Content type: text/plain
Branch: MAIN
Changes since 1.26: +17 -3 lines
Log Message:
number of loose electrons/muons and number of nonoverlaping Zs added to Tree

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