ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/Vuko/WZAnalysis/src/WZAnalyzer.cc
Revision: 1.25
Committed: Wed Dec 12 11:02:10 2007 UTC (17 years, 4 months ago) by vuko
Content type: text/plain
Branch: MAIN
Changes since 1.24: +38 -32 lines
Log Message:
commenting couts and x-check that MET collection is not empty

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.25 // $Id: WZAnalyzer.cc,v 1.24 2007/12/07 15:14:31 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.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 vuko 1.25 // 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 vuko 1.18 noverlaps++;
318     }
319     }
320 vuko 1.25 // cout << "Z Flavour : " << zFlavour
321     // << "\t # overlaps for daughter " << i << " : " << noverlaps
322     // << "\t (out of " << looseLeptonCands->size() << " possible) "
323     // << endl;
324 vuko 1.18 }
325    
326 senka 1.15 matching(iEvent,looseMuonCands, 13);
327     matching(iEvent,looseElectronCands, 11);
328    
329 vuko 1.4 if (zFlavour == 11) {
330 vuko 1.25 // 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 vuko 1.25 // cout << " filling Z lepton muon properties \n";
340     // if (theZCandidate->daughter(0)) cout << "found Z daughter \n";
341 senka 1.19 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 vuko 1.25 // cout << "Tight lepton: " << id << endl;
379 vuko 1.1
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 vuko 1.25 // 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.25 // cout << "# METs=" << mets->size() << endl;
676    
677     if (mets->size() != 1) {
678     cout << "ALARM: # METS is not 1: !" << endl;
679     return reco::Particle::LorentzVector(0.,0.,0.,0.);
680     }
681    
682 vuko 1.14 for (CaloMETCollection::const_iterator met = mets->begin();
683     met!= mets->end(); met++) {
684 vuko 1.25 // cout << "MET: E = " << met->energy()
685     // << "\t Pt = " << met->pt()
686     // <<endl;
687 vuko 1.14 }
688 senka 1.9 return mets->begin()->p4();
689    
690     }
691    
692 senka 1.12 ////////////////////////
693     // matching
694     //
695    
696     class MatchingInfo {
697     public:
698    
699     MatchingInfo(float dr, int gen, int reco) : deltaR(dr), genMuon(gen),recoMuon(reco) {}
700    
701     bool operator<(const MatchingInfo m) { return deltaR < m.deltaR;}
702    
703     float deltaR;
704     int genMuon;
705     int recoMuon;
706    
707     };
708    
709    
710     bool betterMatch(MatchingInfo m1, MatchingInfo m2)
711     { return m1.deltaR < m2.deltaR;}
712    
713    
714     // method that gives the vector<MatchingInfo> matching_Infos containing (dR,i,j); i=genParticle index, j=ParticleCollection index:
715    
716     void WZAnalyzer::matching(const edm::Event& iEvent, Handle<reco::CandidateCollection> cands, int pdgid){
717    
718     using namespace edm;
719     using namespace reco;
720     using namespace std;
721    
722     //-------------------------
723     Handle<CandidateCollection> mccands;
724     iEvent.getByLabel( theMCTruthCollection,mccands );
725    
726     vector <GenParticleCandidate*> genparticles;
727    
728     for(CandidateCollection::const_iterator p = mccands->begin();
729     p != mccands->end(); p++) {
730    
731     // reco::Candidate* p_tmp = &(*p);
732     reco::GenParticleCandidate* ptr = (reco::GenParticleCandidate*) &(*p);
733    
734     if ( (ptr)->status() == 1
735     /*&& (ptr)->momentum().perp() > 2.*/ ) { // stable particles with pt>2 only
736     if ( abs((ptr)->pdgId()) == pdgid ) {
737     genparticles.push_back((ptr));
738     //cout << "electron MCT\n";
739     }
740     }
741     }
742    
743    
744     int n1=0;
745     vector<MatchingInfo> matching_Infos;
746     for ( unsigned int i=0; i<genparticles.size(); i++ ) // po svim MC muonima
747     {
748    
749     for (unsigned int j = 0; j < cands->size(); j++)// po svim reco muonima
750     {
751    
752     double d_eta2=(*cands)[j].eta()-(genparticles)[i]->eta();
753     double d_phi=fabs((*cands)[j].phi()-(genparticles)[i]->phi());
754     double dR_byhand= sqrt(d_eta2*d_eta2+d_phi*d_phi);
755     double dR=dR_byhand;
756    
757     if (dR<0.15){
758     n1++;
759     matching_Infos.push_back(MatchingInfo(dR,i,j));
760     }
761    
762     }
763     }
764 senka 1.24
765 senka 1.12 sort(matching_Infos.begin(),matching_Infos.end(),betterMatch); // sorting (dR,i,j)
766 senka 1.24
767     if (genparticles.size()!=0 && cands->size()!=0){
768     // Now exclude combinations using same gen or rec muons
769     for (std::vector<MatchingInfo>::iterator it=matching_Infos.begin();
770     it != matching_Infos.end(); it++) {
771     for (std::vector<MatchingInfo>::iterator it2=matching_Infos.begin();
772     it2!=it; it2++) {
773     if ( (it->genMuon == it2->genMuon) || (it->recoMuon == it2->recoMuon) ) {
774     matching_Infos.erase(it);
775     it=matching_Infos.begin();
776     break;
777     }
778     }
779     }
780     }
781    
782 vuko 1.14 // Now put result in vector of pairs
783    
784     leptonMatching[pdgid].clear();
785    
786     for (vector<MatchingInfo>::iterator match=matching_Infos.begin();
787 senka 1.16 match !=matching_Infos.end(); match++) { // po tablici dr,i,j
788 vuko 1.14
789     pair<const GenParticleCandidate*, const reco::Candidate *> pair;
790 senka 1.16 // vector<pair,float > matchedParticles;
791 vuko 1.14 pair.first = genparticles[match->genMuon];
792     pair.second = & (*cands)[match->recoMuon];
793 senka 1.16
794     matchedParticles cestice(genparticles[match->genMuon] ,& (*cands)[match->recoMuon] , match->deltaR);
795    
796     leptonMatching[pdgid].push_back(cestice);
797 senka 1.24
798 vuko 1.14 }
799 senka 1.24
800 senka 1.12 }
801    
802 senka 1.9
803 senka 1.13 // get mother of particle
804     // returns mother = 1 if mother is Z boson
805     // returns mother = 2 if mother is W boson
806     // returns mother = 4 if mother is b
807     // returns mother = 0 else
808    
809 vuko 1.14 WZAnalyzer::LeptonOrigin WZAnalyzer::getMother(const reco::Candidate* genParticle){
810    
811     int pdg_id=genParticle->pdgId();
812 vuko 1.25 // cout << "particle id : " << pdg_id << endl;
813 senka 1.13
814     while (genParticle = genParticle->mother() ) { //izvrsava se sve dok ne dodje do nule tj. dok ne trazi majku od protona
815 vuko 1.25 // cout << "Going up: ";
816     // cout << "Mother id : " << genParticle->pdgId() << endl;
817 senka 1.24 if (abs(genParticle->pdgId())!=abs(pdg_id)) {
818 vuko 1.25 // cout<< " good mother " <<endl;
819 senka 1.13 if (abs(genParticle->pdgId())==23){ // mother is Z
820 vuko 1.14 return zboson;
821 senka 1.13 }
822     if (abs(genParticle->pdgId())==24) { // mother is W
823 vuko 1.14 return wboson;
824 senka 1.13 }
825     // if (abs(genParticle->pdgId())==23 || abs(genParticle->pdgId())==24) { // mother is W or Z
826     // WZ_matched=1;
827     // mother=3;
828     // }
829     if ((((abs(genParticle->pdgId()))/100)%10 ==5)||(((abs(genParticle->pdgId()))/1000)%10 ==5 )) { // mother is b
830 vuko 1.14 return bdecay;
831 senka 1.13 }
832 vuko 1.14 if ((((abs(genParticle->pdgId()))/100)%10 ==4)||(((abs(genParticle->pdgId()))/1000)%10 ==4 )) { // mother is c
833     return cdecay;
834     }
835     return other;
836 senka 1.13 }
837 vuko 1.20 }
838    
839     return other;
840 senka 1.13 }
841    
842 senka 1.15 const reco::Candidate * WZAnalyzer::MatchedGenParticle(const reco::Candidate * daughter){
843     ::OverlapChecker overlap;
844     matched_genParticle=0;
845 senka 1.16
846     for (map< int,
847     std::vector< matchedParticles > > ::iterator iter = leptonMatching.begin();
848     iter!=leptonMatching.end(); iter++) // entire map
849     {
850 vuko 1.20 for (unsigned int j=0; j<iter->second.size(); j++){ // entire vector<class<>>
851 senka 1.16 const reco::Candidate * nesto=iter->second[j].pair.second;
852     if (overlap(*nesto,*daughter)){
853     matched_genParticle=iter->second[j].pair.first;
854     }
855     }
856     }
857     return matched_genParticle;
858     }
859    
860    
861     float WZAnalyzer::dR(const reco::Candidate * daughter){
862 vuko 1.18
863 senka 1.16 ::OverlapChecker overlap;
864 vuko 1.18
865 senka 1.19 float delta_r = -10;
866 senka 1.15 for (map< int,
867 senka 1.16 std::vector< matchedParticles > > ::iterator iter = leptonMatching.begin();
868 senka 1.15 iter!=leptonMatching.end(); iter++) // entire map
869     {
870 vuko 1.20 for (unsigned int j=0; j<iter->second.size(); j++){ // entire vector<class<>>
871 senka 1.16 const reco::Candidate * nesto=iter->second[j].pair.second;
872 senka 1.15 if (overlap(*nesto,*daughter)){
873 senka 1.16 delta_r=iter->second[j].delta_R;
874 senka 1.15 }
875     }
876     }
877 senka 1.16 return delta_r;
878 senka 1.15 }
879    
880 vuko 1.11
881 senka 1.16
882 vuko 1.10 void WZAnalyzer::collectMCsummary(Handle <reco::CandidateCollection> mccands) {
883 senka 1.12
884 vuko 1.11 using namespace reco;
885    
886 smorovic 1.8 //collections
887 senka 1.24
888 smorovic 1.8 MCleptZ1_pdgid = -1;
889     MCleptZ2_pdgid = -1;
890     MCleptW_pdgid = -1;
891    
892     MCleptZ1_pt = -1;
893     MCleptZ2_pt = -1;
894     MCleptW_pt = -1;
895    
896     MCleptZ1_eta = -1;
897     MCleptZ2_eta = -1;
898     MCleptW_eta = -1;
899    
900     MCleptZ1_phi = -1;
901     MCleptZ2_phi = -1;
902     MCleptW_phi = -1;
903 senka 1.12
904 smorovic 1.8 vector<reco::GenParticleCandidate*> Tau;
905     vector<reco::GenParticleCandidate*> StableMuons;
906     vector<reco::GenParticleCandidate*> StableElectrons;
907 senka 1.24
908 vuko 1.11 for(CandidateCollection::const_iterator p = mccands->begin();
909     p != mccands->end(); p++) {
910 vuko 1.1
911 vuko 1.11 // reco::Candidate* p_tmp = &(*p);
912     reco::GenParticleCandidate* ptr = (reco::GenParticleCandidate*) &(*p);
913 smorovic 1.8
914     if ( (ptr)->status() == 1
915     /*&& (ptr)->momentum().perp() > 2.*/ ) { // stable particles with pt>2 only
916     if ( abs((ptr)->pdgId()) == 11 ) {
917 senka 1.12
918 smorovic 1.8 StableElectrons.push_back((ptr));
919     //cout << "electron MCT\n";
920     }
921     else if ( abs((ptr)->pdgId()) == 13 ) {
922 senka 1.12
923 smorovic 1.8 StableMuons.push_back((ptr)) ;
924     //cout << "muon MCT\n";
925     }
926     }
927     else if ((ptr)->status() == 2) {
928     if ( abs((ptr)->pdgId()) == 15 ) {//tau
929     Tau.push_back((ptr));
930     }
931     }
932     }
933 senka 1.12
934 vuko 1.25 // std::cout << "# Electrons : " << StableElectrons.size()
935     // << "# Muons : " << StableMuons.size() << std::endl
936     // << "# Tau : " << Tau.size() << std::endl;
937 senka 1.12
938 smorovic 1.8
939     vector<reco::GenParticleCandidate*> * StableLeptons[3] = {&StableElectrons, &StableMuons, &Tau};
940    
941     bool firstZlept = true;
942     MC_tauDecayTypeZ1 = 0;
943     MC_tauDecayTypeZ2 = 0;
944     MC_tauDecayTypeW = 0;
945    
946    
947     for (int i=2; i>=0; i--) {
948     while (StableLeptons[i]->size() > 0) {
949     float maxPt = 0;
950     vector<reco::GenParticleCandidate*>::iterator index = StableLeptons[i]->end();
951    
952     for ( vector<reco::GenParticleCandidate*>::iterator lepton = StableLeptons[i]->begin();
953     lepton != StableLeptons[i]->end(); lepton++ ) {
954    
955     if ((*lepton)->pt() > maxPt) {
956     maxPt = (*lepton)->pt();
957     index = lepton;
958     }
959     }
960     int parentItr=0;
961     bool Zchild = false;
962     bool Wchild = false;
963     bool Tauchild = false;
964    
965     reco::GenParticleCandidate* mcParticleRef = *index;
966    
967     //find W/Z mother
968     while ( mcParticleRef->mother()!=0) {
969    
970     if (parentItr>=2) break;
971     parentItr++;
972     mcParticleRef = (reco::GenParticleCandidate*) mcParticleRef->mother();
973     if (mcParticleRef->mother()==0) break;
974    
975     //if tau
976     if (abs((mcParticleRef)->pdgId())==15 ) {
977     Tauchild=true;
978     break;
979     }
980    
981     if (abs((mcParticleRef)->pdgId())==23 ) {
982     Zchild=true;
983     break;
984     }
985     if (abs((mcParticleRef)->pdgId())==24 ) {
986     Wchild=true;
987     break;
988     }
989     }
990    
991     if (maxPt >0) {
992     if (Tauchild) {
993     parentItr = 0;
994     while (mcParticleRef->mother()!=0) {
995     if (parentItr>=2) break;
996     parentItr++;
997     mcParticleRef = (reco::GenParticleCandidate*) mcParticleRef->mother();
998     if (mcParticleRef->mother()==0) break;
999    
1000     if (abs((mcParticleRef)->pdgId())==23 ) {
1001     if (MCleptZ1_pt == float(mcParticleRef->pt())
1002     && MCleptZ1_eta == float(mcParticleRef->eta())
1003     && MCleptZ1_phi == float(mcParticleRef->phi()) )
1004     MC_tauDecayTypeZ1 = i;
1005     else MC_tauDecayTypeZ2 = i;
1006     break;
1007     }
1008     if (abs((mcParticleRef)->pdgId())==24 ) {
1009     MC_tauDecayTypeW = i;
1010     break;
1011     }
1012     }
1013     }
1014    
1015     if (Wchild) {
1016     MCleptW_pdgid=(*index)->pdgId();
1017     MCleptW_pt=(float)(*index)->pt();
1018     MCleptW_eta=(float)(*index)->eta();
1019     MCleptW_phi=(float)(*index)->phi();
1020     if (abs(MCleptW_pdgid)==15) MC_tauDecayTypeW =3;//default to hadronic decay
1021     }
1022     if (Zchild) {
1023     if (firstZlept) {
1024     firstZlept=false;
1025     MCleptZ1_pdgid=(*index)->pdgId();
1026     MCleptZ1_pt=(float)(*index)->pt();
1027     MCleptZ1_eta=(float)(*index)->eta();
1028     MCleptZ1_phi=(float)(*index)->phi();
1029     if (abs(MCleptZ1_pdgid)==15) MC_tauDecayTypeZ1 =3;
1030     }
1031     else {
1032     MCleptZ2_pdgid=(*index)->pdgId();
1033     MCleptZ2_pt=(float)(*index)->pt();
1034     MCleptZ2_eta=(float)(*index)->eta();
1035     MCleptZ2_phi=(float)(*index)->phi();
1036     if (abs(MCleptZ2_pdgid)==15) MC_tauDecayTypeZ2 =3;
1037     }
1038     }
1039     StableLeptons[i]->erase(index);
1040     }
1041     }
1042     }
1043 vuko 1.25 // std::cout << "# Electrons end: " << StableElectrons.size()
1044     // << "# Muons end: " << StableMuons.size() << std::endl
1045     // << "# Tau end: " << Tau.size() << std::endl;
1046 vuko 1.1
1047 smorovic 1.8 }
1048 vuko 1.1 //define this as a plug-in
1049 vuko 1.2 // DEFINE_FWK_MODULE(WZAnalyzer);