ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/Vuko/WZAnalysis/src/WZAnalyzer.cc
Revision: 1.24
Committed: Fri Dec 7 15:14:31 2007 UTC (17 years, 4 months ago) by senka
Content type: text/plain
Branch: MAIN
Changes since 1.23: +26 -90 lines
Log Message:
fixed little bug in WZAnalyzer::getMother

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