ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/Vuko/WZAnalysis/src/WZAnalyzer.cc
Revision: 1.22
Committed: Wed Dec 5 09:23:55 2007 UTC (17 years, 5 months ago) by senka
Content type: text/plain
Branch: MAIN
Changes since 1.21: +10 -10 lines
Log Message:
 less couts

File Contents

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