ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/Vuko/WZAnalysis/src/WZAnalyzer.cc
Revision: 1.20
Committed: Tue Dec 4 14:13:24 2007 UTC (17 years, 5 months ago) by vuko
Content type: text/plain
Branch: MAIN
CVS Tags: vuko_4dec07
Changes since 1.19: +52 -34 lines
Log Message:
adding electron tree and some cleaning

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