ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/Vuko/WZAnalysis/src/WZAnalyzer.cc
Revision: 1.23
Committed: Thu Dec 6 15:06:48 2007 UTC (17 years, 4 months ago) by smorovic
Content type: text/plain
Branch: MAIN
Changes since 1.22: +4 -2 lines
Log Message:
replaced getByLabel, new InputTag TriggerResults

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 smorovic 1.23 // $Id: WZAnalyzer.cc,v 1.22 2007/12/05 09:23:55 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     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.17 cout << "dR="<< dR(theZCandidate->daughter(0))<<"\n";
348     //--------------
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.9
401     // dPhi(W,Z)
402     cout<<"theWlepton->phi()="<<theWlepton->phi() <<endl;
403     cout<<"theZCandidate->phi()="<<theZCandidate->phi() <<endl;
404     cout<<"dphi()="<<theWlepton->phi()-theZCandidate->phi() <<endl;
405     dPhi_WlZ_reco=acos(cos(theWlepton->phi()-theZCandidate->phi()));
406     dPhi_WZ_reco=acos(cos((theWlepton->p4()+MET).phi()-theZCandidate->phi()));
407 vuko 1.3
408 vuko 1.20 // const reco::Muon * muon = dynamic_cast<const reco::Muon *>(&(*theWlepton));
409     // if(muon != 0) { /* it's a valid muon */
410     // cout << "lepton is a muon \n" << endl;
411     // }
412     // const reco::PixelMatchGsfElectron * electron = dynamic_cast<const reco::PixelMatchGsfElectron *>(&(*theWlepton));
413     // if (electron != 0) {
414     // cout << "lepton is an electron \n" << endl;
415     // }
416 vuko 1.1 }
417    
418     }
419 smorovic 1.6
420 vuko 1.10 // Handle <reco::GenParticleCandidateCollection> mccands;
421     Handle<CandidateCollection> mccands;
422     iEvent.getByLabel( theMCTruthCollection,mccands );
423 smorovic 1.6
424 vuko 1.11 collectMCsummary(mccands);
425 smorovic 1.6
426 smorovic 1.8 if (storeHLTresults) {
427    
428 smorovic 1.6 //def: 1:single electron 2:single relaxed 3:double electron 4: double relaxed 5,6,7,8: muon e+mu: 9
429 smorovic 1.8
430 smorovic 1.6 triggerBitmask=0;
431 smorovic 1.8
432 smorovic 1.6 for (size_t i=0; i<numTriggers; ++i) {
433    
434     // cout << "trigName" << triggerNames[i] << "wasRUN:" << HLTR->wasrun(i) << "error " << HLTR->error(i)
435     // << " accept" << HLTR->accept(i) << "\n";
436    
437     if (!HLTR->wasrun(i)) {
438     //cout << "trigger not run? "<< triggerNames[i] << "\n";
439     continue;
440     }
441     if (HLTR->error(i) ) {
442     //cout << "error trigger: "<< triggerNames[i] <<"\n";
443     continue;
444     }
445     if (HLTR->accept(i)) {
446     //if (triggerNames[i].compare("mcpath")!=0)
447     //TRaccept=true;
448     }else {
449     continue;
450     }
451    
452     if (triggerNames[i].compare("HLT1Electron")==0) triggerBitmask |= 1;
453     if (triggerNames[i].compare("HLT1ElectronRelaxed")==0) triggerBitmask |= 2;
454     if (triggerNames[i].compare("HLT2Electron")==0) triggerBitmask |= 4;
455     if (triggerNames[i].compare("HLT2ElectronRelaxed")==0) triggerBitmask |= 8;
456    
457     if (triggerNames[i].compare("HLT1MuonIso")==0) triggerBitmask |= 16;
458     if (triggerNames[i].compare("HLT1MuonNonIso")==0) triggerBitmask |= 32;
459     if (triggerNames[i].compare("HLT2MuonNonIso")==0) triggerBitmask |= 64;
460     if (triggerNames[i].compare("HLT2MuonZ")==0) triggerBitmask |= 128;
461    
462     if (triggerNames[i].compare("HLTXElectronMuon")==0) triggerBitmask |= 256;
463     if (triggerNames[i].compare("HLTXElectronMuonRelaxed")==0) triggerBitmask |= 512;
464     }
465     }
466 vuko 1.10
467    
468     // Jet properties
469    
470     float max_et = 0.;
471 vuko 1.14 float max2_et = 0.;
472     const Candidate * leadingJet = 0;
473     const Candidate * secondJet = 0;
474     // for(CandidateCollection::const_iterator jet = jetCands->begin();
475     for(CaloJetCollection::const_iterator jet = jetCands->begin();
476 vuko 1.10 jet != jetCands->end(); jet++) {
477     if (jet->et() > max_et ) {
478 vuko 1.14 if (leadingJet) {
479     secondJet = leadingJet;
480     max2_et = max_et;
481     }
482 vuko 1.10 leadingJet = &(*jet);
483     max_et = jet->et();
484 vuko 1.14 } else {
485     if (jet->et() > max2_et ) {
486     secondJet = &(*jet);
487     max2_et = jet->et();
488     }
489 vuko 1.10 }
490     }
491     if (leadingJet) {
492     jet1Et = leadingJet->et();
493 vuko 1.14 jet1Phi = leadingJet->phi();
494 vuko 1.10 jet1Eta = leadingJet->eta();
495    
496     } else {
497     jet1Et = -10;
498     jet1Phi = 0.;
499     jet1Eta = 0.;
500     }
501 vuko 1.14 if (secondJet) {
502     jet2Et = secondJet->et();
503     jet2Phi = secondJet->phi();
504     jet2Eta = secondJet->eta();
505     } else {
506     jet2Et = -10;
507     jet2Phi = 0.;
508     jet2Eta = 0.;
509    
510     }
511 senka 1.9
512 senka 1.12
513     // matching:
514    
515     matching(iEvent,looseMuonCands, 13);
516     matching(iEvent,looseElectronCands, 11);
517    
518 senka 1.13 getMother(&(*mccands)[1]);
519 vuko 1.10
520 vuko 1.1 wzTree->Fill();
521    
522 vuko 1.20
523    
524     // Fill electrons in a tree
525    
526     for(CandidateCollection::const_iterator el = looseElectronCands->begin();
527     el != looseElectronCands->end(); ++ el ) {
528    
529     electronUse = 0;
530    
531     if (theZCandidate) {
532    
533     if (overlap(*theZCandidate, *el) ) {
534     electronUse = 23;
535     } else if (theWlepton) {
536     if (overlap(*theWlepton, *el) ) {
537     electronUse = 24;
538     }
539     leptonProperties["electron"]->FillProperties(&(*el),
540     iEvent, iSetup,
541     MatchedGenParticle(&(*el)),dR(&(*el)));
542     electronTree->Fill();
543    
544     }
545     }
546     }
547    
548    
549 vuko 1.1 }
550    
551    
552     // ------------ method called once each job just before starting event loop ------------
553     void
554     WZAnalyzer::beginJob(const edm::EventSetup&)
555     {
556    
557     using namespace wzana;
558    
559 senka 1.16 // theFile = new TFile( "wz.root", "RECREATE" ) ;
560     theFile = new TFile( fOutputFileName.c_str(), "RECREATE" ) ;
561    
562 vuko 1.1
563     wzTree = new TTree("WZTree","WZ informations");
564    
565 vuko 1.20 electronTree = new TTree("ElTree","electron informations");
566    
567     leptonProperties["electron"] = new ElectronProperties();
568 vuko 1.1
569 vuko 1.3 leptonProperties["Wel"] = new ElectronProperties();
570     leptonProperties["ZEl1"] = new ElectronProperties();
571     leptonProperties["ZEl2"] = new ElectronProperties();
572    
573     leptonProperties["Wmu"] = new MuonProperties();
574     leptonProperties["Zmu1"] = new MuonProperties();
575     leptonProperties["Zmu2"] = new MuonProperties();
576 vuko 1.1
577 vuko 1.20
578 vuko 1.3 leptonProperties["Wel"]->CreateBranches(wzTree, "WEl_");
579 vuko 1.4 leptonProperties["ZEl1"]->CreateBranches(wzTree, "ZEl1_");
580     leptonProperties["ZEl2"]->CreateBranches(wzTree, "ZEl2_");
581 vuko 1.1
582 senka 1.9 leptonProperties["Wmu"] ->CreateBranches(wzTree, "Wmu_");
583     leptonProperties["Zmu1"] ->CreateBranches(wzTree, "Zmul_");
584     leptonProperties["Zmu2"] ->CreateBranches(wzTree, "Zmu2_");
585 vuko 1.20 leptonProperties["electron"] ->CreateBranches(electronTree);
586 senka 1.9
587 vuko 1.1 initialiseTree();
588 smorovic 1.6
589     //prepare HLT TriggerResults branch
590     if (storeHLTresults) {
591     wzTree->Branch("WZ_hltBitmask",&triggerBitmask,"WZ_hltBitmask/I");
592    
593     }
594 senka 1.9
595     // MET branch
596     wzTree->Branch("MET_energy",&MET_energy,"MET_energy/F");
597     wzTree->Branch("MET_pt",&MET_pt,"MET_pt/F");
598     wzTree->Branch("MET_eta",&MET_eta,"MET_eta/F");
599     wzTree->Branch("dPhi_WlZ_reco",&dPhi_WlZ_reco,"dPhi_WlZ_reco/F");
600     wzTree->Branch("dPhi_WZ_reco",&dPhi_WZ_reco,"dPhi_WZ_reco/F");
601    
602 vuko 1.1 }
603    
604     // ------------ method called once each job just after ending the event loop ------------
605     void
606     WZAnalyzer::endJob() {
607    
608     theFile->Write();
609     theFile->Close();
610    
611     }
612    
613    
614     void WZAnalyzer::initialiseTree() {
615    
616     // Z properties
617     wzTree->Branch("Zmass", &zMass, "Zmass/F");
618     wzTree->Branch("ZId", &zFlavour, "Zid/I");
619     wzTree->Branch("ZPt", &zPt, "ZPt/F");
620     wzTree->Branch("ZEta", &zEta, "ZEta/F");
621     wzTree->Branch("ZPhi", &zPhi, "ZPhi/F");
622    
623     // W Properties
624     wzTree->Branch("WlId", &wlFlavour, "Wlid/I");
625     wzTree->Branch("WlCharge", &wlCharge, "WlCharge/I");
626     wzTree->Branch("WlPt", &wlPt, "WlPt/F");
627 smorovic 1.8
628     wzTree->Branch("MC_leptonZ1_pt", &MCleptZ1_pt,"MC_leptonZ1_pt/F");
629     wzTree->Branch("MC_leptonZ2_pt", &MCleptZ2_pt,"MC_leptonZ2_pt/F");
630     wzTree->Branch("MC_leptonW_pt", &MCleptW_pt, "MC_leptonW_pt/F");
631    
632     wzTree->Branch("MC_leptonZ1_eta", &MCleptZ1_eta,"MC_leptonZ1_eta/F");
633     wzTree->Branch("MC_leptonZ2_eta", &MCleptZ2_eta,"MC_leptonZ2_eta/F");
634     wzTree->Branch("MC_leptonW_eta", &MCleptW_eta, "MC_leptonW_eta/F");
635    
636     wzTree->Branch("MC_leptonZ1_phi", &MCleptZ1_phi,"MC_leptonZ1_phi/F");
637     wzTree->Branch("MC_leptonZ2_phi", &MCleptZ2_phi,"MC_leptonZ2_phi/F");
638     wzTree->Branch("MC_leptonW_phi", &MCleptW_phi, "MC_leptonW_phi/F");
639    
640     wzTree->Branch("MC_leptonZ1_pdgid", &MCleptZ1_pdgid,"MC_leptonZ1_pdgid/I");
641     wzTree->Branch("MC_leptonZ2_pdgid", &MCleptZ2_pdgid,"MC_leptonZ2_pdgid/I");
642     wzTree->Branch("MC_leptonW_pdgid", &MCleptW_pdgid, "MC_leptonW_pdgid/I");
643 vuko 1.14
644     /*
645     wzTree->Branch("MC_leptonW_origin", &MCleptW_pdgid, "MC_leptonW_origin/I");
646     wzTree->Branch("MC_leptonZ1_origin", &MCleptW_pdgid, "MC_leptonZ1_origin/I");
647     wzTree->Branch("MC_leptonZ2_origin", &MCleptW_pdgid, "MC_leptonZ2_origin/I");
648     */
649    
650 smorovic 1.8 wzTree->Branch("MC_TauDecayType_fromZ1", &MC_tauDecayTypeZ1,"MC_TauDecayType_fromZ1/I");
651     wzTree->Branch("MC_TauDecayType_fromZ2", &MC_tauDecayTypeZ2,"MC_TauDecayType_fromZ2/I");
652     wzTree->Branch("MC_TauDecayType_fromW", &MC_tauDecayTypeW, "MC_TauDecayType_fromW/I");
653 vuko 1.10
654     // MET properties
655    
656     // Jet properties
657     wzTree->Branch("Jet1Et", &jet1Et, "Jet1Et/F");
658     wzTree->Branch("Jet1Phi", &jet1Phi, "Jet1Phi/F");
659     wzTree->Branch("Jet1Eta", &jet1Eta, "Jet1Eta/F");
660    
661     wzTree->Branch("Jet2Et", &jet2Et, "Jet2Et/F");
662     wzTree->Branch("Jet2Phi", &jet2Phi, "Jet2Phi/F");
663     wzTree->Branch("Jet2Eta", &jet2Eta, "Jet2Eta/F");
664    
665 vuko 1.20 //
666     electronTree->Branch("used", &electronUse, "used/I");
667 vuko 1.10
668 smorovic 1.8 }
669 vuko 1.1
670 senka 1.9
671     reco::Particle::LorentzVector WZAnalyzer::computeMET(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
672    
673     using namespace edm;
674     using namespace reco;
675     using namespace std;
676    
677     Handle<CaloMETCollection> mets;
678     iEvent.getByLabel("met", mets);
679 vuko 1.14 cout << "# METs=" << mets->size() << endl;
680     for (CaloMETCollection::const_iterator met = mets->begin();
681     met!= mets->end(); met++) {
682     cout << "MET: E = " << met->energy()
683     << "\t Pt = " << met->pt()
684     <<endl;
685     }
686 senka 1.9 return mets->begin()->p4();
687    
688     }
689    
690 senka 1.12 ////////////////////////
691     // matching
692     //
693    
694     class MatchingInfo {
695     public:
696    
697     MatchingInfo(float dr, int gen, int reco) : deltaR(dr), genMuon(gen),recoMuon(reco) {}
698    
699     bool operator<(const MatchingInfo m) { return deltaR < m.deltaR;}
700    
701     float deltaR;
702     int genMuon;
703     int recoMuon;
704    
705     };
706    
707    
708     bool betterMatch(MatchingInfo m1, MatchingInfo m2)
709     { return m1.deltaR < m2.deltaR;}
710    
711    
712     // method that gives the vector<MatchingInfo> matching_Infos containing (dR,i,j); i=genParticle index, j=ParticleCollection index:
713    
714     void WZAnalyzer::matching(const edm::Event& iEvent, Handle<reco::CandidateCollection> cands, int pdgid){
715    
716     using namespace edm;
717     using namespace reco;
718     using namespace std;
719    
720     //-------------------------
721     Handle<CandidateCollection> mccands;
722     iEvent.getByLabel( theMCTruthCollection,mccands );
723    
724     vector <GenParticleCandidate*> genparticles;
725    
726     for(CandidateCollection::const_iterator p = mccands->begin();
727     p != mccands->end(); p++) {
728    
729     // reco::Candidate* p_tmp = &(*p);
730     reco::GenParticleCandidate* ptr = (reco::GenParticleCandidate*) &(*p);
731    
732     if ( (ptr)->status() == 1
733     /*&& (ptr)->momentum().perp() > 2.*/ ) { // stable particles with pt>2 only
734     if ( abs((ptr)->pdgId()) == pdgid ) {
735     genparticles.push_back((ptr));
736     //cout << "electron MCT\n";
737     }
738     }
739     }
740    
741    
742     int n1=0;
743     vector<MatchingInfo> matching_Infos;
744     for ( unsigned int i=0; i<genparticles.size(); i++ ) // po svim MC muonima
745     {
746    
747     for (unsigned int j = 0; j < cands->size(); j++)// po svim reco muonima
748     {
749    
750     double d_eta2=(*cands)[j].eta()-(genparticles)[i]->eta();
751     double d_phi=fabs((*cands)[j].phi()-(genparticles)[i]->phi());
752     double dR_byhand= sqrt(d_eta2*d_eta2+d_phi*d_phi);
753 senka 1.22 // cout << "dr by hand = " << dR_byhand <<" i="<< i<<" j="<<j <<endl;
754 senka 1.12 double dR=dR_byhand;
755    
756     if (dR<0.15){
757     n1++;
758     matching_Infos.push_back(MatchingInfo(dR,i,j));
759     }
760    
761     }
762     }
763    
764 senka 1.22 // cout<<"table for matching reco&gen;"<< " pdgid="<<pdgid <<endl;
765 vuko 1.20 for (unsigned int i=0; i<matching_Infos.size();i++)
766 senka 1.12 {
767 senka 1.22 // cout <<"dr="<<matching_Infos[i].deltaR << " i="<<matching_Infos[i].genMuon <<" j="<<matching_Infos[i].recoMuon <<endl;
768 senka 1.12 }
769    
770     sort(matching_Infos.begin(),matching_Infos.end(),betterMatch); // sorting (dR,i,j)
771    
772 senka 1.22 // cout<<"sorted table;" <<endl;
773 senka 1.12
774 vuko 1.20 for (unsigned int i=0; i<matching_Infos.size();i++)
775 senka 1.12 {
776 senka 1.22 // cout <<"dr="<<matching_Infos[i].deltaR << " i="<<matching_Infos[i].genMuon <<" j="<<matching_Infos[i].recoMuon <<endl;
777 senka 1.12 }
778    
779 senka 1.22 // cout<<"MC.size()="<<genparticles.size() <<" reco->size()="<<cands->size() <<endl;
780 senka 1.12 if (genparticles.size()!=0 && cands->size()!=0){
781     // Now exclude combinations using same gen or rec muons
782     for (std::vector<MatchingInfo>::iterator it=matching_Infos.begin();
783     it != matching_Infos.end(); it++) {
784     for (std::vector<MatchingInfo>::iterator it2=matching_Infos.begin();
785     it2!=it; it2++) {
786     if ( (it->genMuon == it2->genMuon) || (it->recoMuon == it2->recoMuon) ) {
787     matching_Infos.erase(it);
788     it=matching_Infos.begin();
789     break;
790     }
791     }
792     }
793     }
794 senka 1.22 // cout << "final table:"<<endl;
795 vuko 1.20 for (unsigned int i=0; i<matching_Infos.size();i++)
796 senka 1.12 {
797 senka 1.22 // cout <<"dr="<<matching_Infos[i].deltaR << " i="<<matching_Infos[i].genMuon <<" j="<<matching_Infos[i].recoMuon <<endl;
798 senka 1.12 }
799 vuko 1.14
800     // Now put result in vector of pairs
801    
802    
803     // vector< pair<const GenParticleCandidate*,
804     // const reco::Candidate *> > * leptonMatching = 0;
805    
806     // if (pdgid == 11) {
807     // leptonMatching = &electronMatching;
808     // } else if (pdgid == 13) {
809     // leptonMatching = &muonMatching;
810     // }
811    
812     // if (leptonMatching == 0) return;
813     // leptonMatching->clear();
814    
815     leptonMatching[pdgid].clear();
816 senka 1.16
817     // class matchedParticles{
818     // private:
819     // pair<const GenParticleCandidate*, const reco::Candidate *> pair;
820     // float delta_R;
821     // public:
822     // matchedParticles();
823     // // matchedParticles( pair<const GenParticleCandidate*, const reco::Candidate *> pair2, float delta_R2){ pair=pair2;delta_R2=delta_R;};
824     // matchedParticles( const GenParticleCandidate* genPar, const reco::Candidate * recoPar, float delta_R2) {pair.first= genPar;pair.second= recoPar ;delta_R=delta_R2;}
825     // };
826    
827     // matchedParticles::matchedParticles();
828 vuko 1.14
829     for (vector<MatchingInfo>::iterator match=matching_Infos.begin();
830 senka 1.16 match !=matching_Infos.end(); match++) { // po tablici dr,i,j
831 vuko 1.14
832     pair<const GenParticleCandidate*, const reco::Candidate *> pair;
833 senka 1.16 // vector<pair,float > matchedParticles;
834 vuko 1.14 pair.first = genparticles[match->genMuon];
835     pair.second = & (*cands)[match->recoMuon];
836 senka 1.16
837     matchedParticles cestice(genparticles[match->genMuon] ,& (*cands)[match->recoMuon] , match->deltaR);
838    
839     leptonMatching[pdgid].push_back(cestice);
840    
841     // leptonMatching[pdgid].push_back(pair);
842 vuko 1.14 }
843    
844    
845 senka 1.12 }
846    
847 senka 1.9
848 senka 1.13 // get mother of particle
849     // returns mother = 1 if mother is Z boson
850     // returns mother = 2 if mother is W boson
851     // returns mother = 4 if mother is b
852     // returns mother = 0 else
853    
854 vuko 1.14 WZAnalyzer::LeptonOrigin WZAnalyzer::getMother(const reco::Candidate* genParticle){
855    
856     int pdg_id=genParticle->pdgId();
857 senka 1.13
858     while (genParticle = genParticle->mother() ) { //izvrsava se sve dok ne dodje do nule tj. dok ne trazi majku od protona
859     cout << "Going up: ";
860     cout << "Mother id : " << genParticle->pdgId() << endl;
861 vuko 1.14 if (abs(genParticle->pdgId())!=pdg_id) {
862 senka 1.13 cout<< " good mother " <<endl;
863     if (abs(genParticle->pdgId())==23){ // mother is Z
864 vuko 1.14 return zboson;
865 senka 1.13 }
866     if (abs(genParticle->pdgId())==24) { // mother is W
867 vuko 1.14 return wboson;
868 senka 1.13 }
869     // if (abs(genParticle->pdgId())==23 || abs(genParticle->pdgId())==24) { // mother is W or Z
870     // WZ_matched=1;
871     // mother=3;
872     // }
873     if ((((abs(genParticle->pdgId()))/100)%10 ==5)||(((abs(genParticle->pdgId()))/1000)%10 ==5 )) { // mother is b
874 vuko 1.14 return bdecay;
875 senka 1.13 }
876 vuko 1.14 if ((((abs(genParticle->pdgId()))/100)%10 ==4)||(((abs(genParticle->pdgId()))/1000)%10 ==4 )) { // mother is c
877     return cdecay;
878     }
879     return other;
880 senka 1.13 }
881 vuko 1.20 }
882    
883     return other;
884 senka 1.13 }
885    
886 senka 1.15 const reco::Candidate * WZAnalyzer::MatchedGenParticle(const reco::Candidate * daughter){
887     ::OverlapChecker overlap;
888     matched_genParticle=0;
889 senka 1.16
890     for (map< int,
891     std::vector< matchedParticles > > ::iterator iter = leptonMatching.begin();
892     iter!=leptonMatching.end(); iter++) // entire map
893     {
894 vuko 1.20 for (unsigned int j=0; j<iter->second.size(); j++){ // entire vector<class<>>
895 senka 1.16 const reco::Candidate * nesto=iter->second[j].pair.second;
896     if (overlap(*nesto,*daughter)){
897     cout <<" found matched genparticle from vector<class<..>>" <<endl;
898     // return matched_genParticle=iter->second[j].pair.first;
899     matched_genParticle=iter->second[j].pair.first;
900     // delta_r=iter->second[j].delta_R;
901     }
902     }
903     }
904     return matched_genParticle;
905     }
906    
907    
908     float WZAnalyzer::dR(const reco::Candidate * daughter){
909 vuko 1.18
910 senka 1.16 ::OverlapChecker overlap;
911 vuko 1.18
912 senka 1.19 float delta_r = -10;
913 senka 1.15 for (map< int,
914 senka 1.16 std::vector< matchedParticles > > ::iterator iter = leptonMatching.begin();
915 senka 1.15 iter!=leptonMatching.end(); iter++) // entire map
916     {
917 vuko 1.20 for (unsigned int j=0; j<iter->second.size(); j++){ // entire vector<class<>>
918 senka 1.16 const reco::Candidate * nesto=iter->second[j].pair.second;
919 senka 1.15 if (overlap(*nesto,*daughter)){
920 senka 1.17 // cout <<" found matched genparticle from vector<class<..>>" <<endl;
921 senka 1.16 // return matched_genParticle=iter->second[j].pair.first;
922     delta_r=iter->second[j].delta_R;
923 senka 1.15 }
924     }
925     }
926 senka 1.16 return delta_r;
927 senka 1.15 }
928    
929 vuko 1.11
930 senka 1.16
931 vuko 1.10 void WZAnalyzer::collectMCsummary(Handle <reco::CandidateCollection> mccands) {
932     //(Handle <reco::GenParticleCandidateCollection> mccands) {
933     //
934     //void WZAnalyzer::collectMCsummary(Handle <reco::GenParticleCandidateCollection> mccands) {
935 senka 1.12
936 vuko 1.11 using namespace reco;
937    
938 smorovic 1.8 //collections
939 senka 1.12 cout<<" pocetak WZAnalyzer::collectMCsummary" <<endl;
940 smorovic 1.8 MCleptZ1_pdgid = -1;
941     MCleptZ2_pdgid = -1;
942     MCleptW_pdgid = -1;
943    
944     MCleptZ1_pt = -1;
945     MCleptZ2_pt = -1;
946     MCleptW_pt = -1;
947    
948     MCleptZ1_eta = -1;
949     MCleptZ2_eta = -1;
950     MCleptW_eta = -1;
951    
952     MCleptZ1_phi = -1;
953     MCleptZ2_phi = -1;
954     MCleptW_phi = -1;
955 senka 1.12
956 smorovic 1.8 vector<reco::GenParticleCandidate*> Tau;
957     vector<reco::GenParticleCandidate*> StableMuons;
958     vector<reco::GenParticleCandidate*> StableElectrons;
959    
960 vuko 1.11 // reco::Candidate::iterator p;
961     // for (p = ((reco::Candidate*)&mccands)->begin(); p != ((reco::Candidate*)&mccands)->end(); ++p ) {
962     for(CandidateCollection::const_iterator p = mccands->begin();
963     p != mccands->end(); p++) {
964 vuko 1.1
965 vuko 1.11 // reco::Candidate* p_tmp = &(*p);
966     reco::GenParticleCandidate* ptr = (reco::GenParticleCandidate*) &(*p);
967 smorovic 1.8
968     if ( (ptr)->status() == 1
969     /*&& (ptr)->momentum().perp() > 2.*/ ) { // stable particles with pt>2 only
970     if ( abs((ptr)->pdgId()) == 11 ) {
971 senka 1.12
972 smorovic 1.8 StableElectrons.push_back((ptr));
973     //cout << "electron MCT\n";
974     }
975     else if ( abs((ptr)->pdgId()) == 13 ) {
976 senka 1.12
977 smorovic 1.8 StableMuons.push_back((ptr)) ;
978     //cout << "muon MCT\n";
979     }
980     }
981     else if ((ptr)->status() == 2) {
982     if ( abs((ptr)->pdgId()) == 15 ) {//tau
983     Tau.push_back((ptr));
984     }
985     }
986     }
987 senka 1.12
988 vuko 1.11 std::cout << "# Electrons : " << StableElectrons.size()
989 senka 1.12 << "# Muons : " << StableMuons.size() << std::endl
990     << "# Tau : " << Tau.size() << std::endl;
991    
992 smorovic 1.8
993     vector<reco::GenParticleCandidate*> * StableLeptons[3] = {&StableElectrons, &StableMuons, &Tau};
994    
995     bool firstZlept = true;
996     MC_tauDecayTypeZ1 = 0;
997     MC_tauDecayTypeZ2 = 0;
998     MC_tauDecayTypeW = 0;
999    
1000    
1001     for (int i=2; i>=0; i--) {
1002     while (StableLeptons[i]->size() > 0) {
1003     float maxPt = 0;
1004     vector<reco::GenParticleCandidate*>::iterator index = StableLeptons[i]->end();
1005    
1006     for ( vector<reco::GenParticleCandidate*>::iterator lepton = StableLeptons[i]->begin();
1007     lepton != StableLeptons[i]->end(); lepton++ ) {
1008    
1009     if ((*lepton)->pt() > maxPt) {
1010     maxPt = (*lepton)->pt();
1011     index = lepton;
1012     }
1013     }
1014     int parentItr=0;
1015     bool Zchild = false;
1016     bool Wchild = false;
1017     bool Tauchild = false;
1018    
1019     reco::GenParticleCandidate* mcParticleRef = *index;
1020    
1021     //find W/Z mother
1022     while ( mcParticleRef->mother()!=0) {
1023    
1024     if (parentItr>=2) break;
1025     parentItr++;
1026     mcParticleRef = (reco::GenParticleCandidate*) mcParticleRef->mother();
1027     if (mcParticleRef->mother()==0) break;
1028    
1029     //if tau
1030     if (abs((mcParticleRef)->pdgId())==15 ) {
1031     Tauchild=true;
1032     break;
1033     }
1034    
1035     // cout << "mother: " << (mcParticleRef)->pdg_id()
1036     // <<" pt="<< c_p4.pt() << " eta="<<c_p4.eta()
1037     // <<" status=" <<mcParticleRef->status() <<"\n";
1038    
1039     if (abs((mcParticleRef)->pdgId())==23 ) {
1040     Zchild=true;
1041     break;
1042     }
1043     if (abs((mcParticleRef)->pdgId())==24 ) {
1044     Wchild=true;
1045     break;
1046     }
1047     }
1048    
1049     if (maxPt >0) {
1050     if (Tauchild) {
1051     parentItr = 0;
1052     while (mcParticleRef->mother()!=0) {
1053     if (parentItr>=2) break;
1054     parentItr++;
1055     mcParticleRef = (reco::GenParticleCandidate*) mcParticleRef->mother();
1056     if (mcParticleRef->mother()==0) break;
1057    
1058     if (abs((mcParticleRef)->pdgId())==23 ) {
1059     if (MCleptZ1_pt == float(mcParticleRef->pt())
1060     && MCleptZ1_eta == float(mcParticleRef->eta())
1061     && MCleptZ1_phi == float(mcParticleRef->phi()) )
1062     MC_tauDecayTypeZ1 = i;
1063     else MC_tauDecayTypeZ2 = i;
1064     break;
1065     }
1066     if (abs((mcParticleRef)->pdgId())==24 ) {
1067     MC_tauDecayTypeW = i;
1068     break;
1069     }
1070     }
1071     }
1072    
1073     if (Wchild) {
1074     MCleptW_pdgid=(*index)->pdgId();
1075     MCleptW_pt=(float)(*index)->pt();
1076     MCleptW_eta=(float)(*index)->eta();
1077     MCleptW_phi=(float)(*index)->phi();
1078     if (abs(MCleptW_pdgid)==15) MC_tauDecayTypeW =3;//default to hadronic decay
1079     }
1080     if (Zchild) {
1081     if (firstZlept) {
1082     firstZlept=false;
1083     MCleptZ1_pdgid=(*index)->pdgId();
1084     MCleptZ1_pt=(float)(*index)->pt();
1085     MCleptZ1_eta=(float)(*index)->eta();
1086     MCleptZ1_phi=(float)(*index)->phi();
1087     if (abs(MCleptZ1_pdgid)==15) MC_tauDecayTypeZ1 =3;
1088     }
1089     else {
1090     MCleptZ2_pdgid=(*index)->pdgId();
1091     MCleptZ2_pt=(float)(*index)->pt();
1092     MCleptZ2_eta=(float)(*index)->eta();
1093     MCleptZ2_phi=(float)(*index)->phi();
1094     if (abs(MCleptZ2_pdgid)==15) MC_tauDecayTypeZ2 =3;
1095     }
1096     }
1097     StableLeptons[i]->erase(index);
1098     }
1099     }
1100     }
1101 senka 1.12 std::cout << "# Electrons end: " << StableElectrons.size()
1102     << "# Muons end: " << StableMuons.size() << std::endl
1103     << "# Tau end: " << Tau.size() << std::endl;
1104 vuko 1.1
1105 smorovic 1.8 }
1106 vuko 1.1 //define this as a plug-in
1107 vuko 1.2 // DEFINE_FWK_MODULE(WZAnalyzer);