ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/Vuko/WZAnalysis/src/WZAnalyzer.cc
Revision: 1.26
Committed: Fri Dec 14 17:53:14 2007 UTC (17 years, 4 months ago) by vuko
Content type: text/plain
Branch: MAIN
Changes since 1.25: +25 -3 lines
Log Message:
adding event weights

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