ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/Vuko/WZAnalysis/src/WZAnalyzer.cc
Revision: 1.11
Committed: Tue Nov 27 15:54:13 2007 UTC (17 years, 5 months ago) by vuko
Content type: text/plain
Branch: MAIN
Changes since 1.10: +33 -17 lines
Log Message:
fixing MC truth collection

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.11 // $Id: WZAnalyzer.cc,v 1.10 2007/11/27 10:17:19 vuko Exp $
17 vuko 1.1 //
18     //
19    
20    
21     // system include files
22     #include <memory>
23    
24     // user include files
25     #include "FWCore/Framework/interface/Frameworkfwd.h"
26     #include "FWCore/Framework/interface/EDAnalyzer.h"
27    
28     #include "FWCore/Framework/interface/Event.h"
29     #include "FWCore/Framework/interface/MakerMacros.h"
30    
31     #include "FWCore/ParameterSet/interface/ParameterSet.h"
32    
33 vuko 1.2 #include "Vuko/WZAnalysis/interface/WZAnalyzer.h"
34 vuko 1.1 #include "DataFormats/Candidate/interface/OverlapChecker.h"
35    
36 vuko 1.2 #include "Vuko/WZAnalysis/interface/ElectronProperties.h"
37 vuko 1.3 #include "Vuko/WZAnalysis/interface/MuonProperties.h"
38 vuko 1.1
39 smorovic 1.6 #include "DataFormats/Common/interface/TriggerResults.h"
40 vuko 1.1
41     //--- muon AOD:
42     #include "DataFormats/JetReco/interface/CaloJetCollection.h"
43     #include "DataFormats/EgammaCandidates/interface/Electron.h"
44     #include "DataFormats/EgammaCandidates/interface/ElectronFwd.h"
45     #include "DataFormats/MuonReco/interface/MuonFwd.h"
46     #include "DataFormats/MuonReco/interface/Muon.h"
47     #include "DataFormats/EgammaReco/interface/BasicCluster.h"
48     #include "DataFormats/EgammaReco/interface/BasicClusterFwd.h"
49     #include "DataFormats/EgammaCandidates/interface/PixelMatchGsfElectron.h"
50    
51 senka 1.9 #include "DataFormats/METReco/interface/CaloMET.h"
52    
53 vuko 1.1
54     #include "TFile.h"
55     #include "TH1F.h"
56     #include "TH2F.h"
57     #include "TTree.h"
58    
59     #include <map>
60    
61     //
62     // constants, enums and typedefs
63     //
64    
65     //
66     // static data member definitions
67     //
68    
69     //
70     // constructors and destructor
71     //
72     WZAnalyzer::WZAnalyzer(const edm::ParameterSet& iConfig)
73    
74     {
75     //now do what ever initialization is needed
76    
77 vuko 1.10
78     theMCTruthCollection = iConfig.getParameter<edm::InputTag>("genParticles");
79    
80 vuko 1.1 theLooseMuonCollection = iConfig.getParameter<edm::InputTag>("LooseMuons");
81     theGoodMuonCollection = iConfig.getParameter<edm::InputTag>("GoodMuons");
82     theLooseElectronCollection = iConfig.getParameter<edm::InputTag>("LooseElectrons");
83     theGoodElectronCollection = iConfig.getParameter<edm::InputTag>("GoodElectrons");
84     theTightLeptonCollection = iConfig.getParameter<edm::InputTag>("TightLeptons");
85     // theMediumElectronCollection = iConfig.getParameter<edm::InputTag>("LooseElectrons");
86     // theTightElectronCollection = iConfig.getParameter<edm::InputTag>("TightElectrons");
87     // Z's
88     theZeeCollection = iConfig.getParameter<edm::InputTag>("ZtoEE");
89     theZmumuCollection = iConfig.getParameter<edm::InputTag>("ZtoMuMu");
90 vuko 1.3 theZllCollection = iConfig.getParameter<edm::InputTag>("ZtoLL");
91 vuko 1.10
92     theJetCollection = iConfig.getParameter<edm::InputTag>("Jets");
93 smorovic 1.6
94     storeHLTresults=false;
95     storeHLTresults = iConfig.getParameter<bool>("storeHLTresults");
96 vuko 1.1
97 smorovic 1.6 firstTriggerResult = true;
98 vuko 1.1 }
99    
100    
101     WZAnalyzer::~WZAnalyzer()
102     {
103    
104     // do anything here that needs to be done at desctruction time
105     // (e.g. close files, deallocate resources etc.)
106    
107     }
108    
109    
110     //
111     // member functions
112     //
113    
114     // ------------ method called to for each event ------------
115     void
116     WZAnalyzer::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup)
117     {
118     using namespace edm;
119     using namespace reco;
120     using namespace std;
121    
122    
123     #ifdef THIS_IS_AN_EVENT_EXAMPLE
124     Handle<ExampleData> pIn;
125     iEvent.getByLabel("example",pIn);
126     #endif
127    
128     #ifdef THIS_IS_AN_EVENTSETUP_EXAMPLE
129     ESHandle<SetupData> pSetup;
130     iSetup.get<SetupRecord>().get(pSetup);
131     #endif
132    
133 vuko 1.11 ///////////////////////////////////////
134     //
135     /// EVENT INITIALISATION
136     ///
137    
138     // Reset a few things
139    
140     for (map<string,wzana::LeptonProperties* >::iterator iter = leptonProperties.begin();
141     iter!=leptonProperties.end(); iter++)
142     {
143     iter->second->ResetValues();
144     }
145    
146    
147 vuko 1.1 const Candidate * theZCandidate = 0;
148     const Candidate * theWlepton = 0;
149    
150 vuko 1.11
151    
152     ////////////////////////////////////////
153     // Get lists
154    
155 vuko 1.1 // Z->mumu
156     Handle<CandidateCollection> zmumuCands;
157     iEvent.getByLabel( theZmumuCollection , zmumuCands);
158    
159    
160     // Z->ee
161     Handle<CandidateCollection> zeeCands;
162     iEvent.getByLabel( theZeeCollection , zeeCands);
163    
164 vuko 1.3 // Z->ee
165     Handle<CandidateCollection> zllCands;
166     iEvent.getByLabel( theZllCollection , zllCands);
167    
168 vuko 1.1 // loose electrons
169     Handle<CandidateCollection> looseElectronCands;
170     iEvent.getByLabel( theLooseElectronCollection , looseElectronCands);
171    
172     // good electrons
173     Handle<CandidateCollection> goodElectronCands;
174     iEvent.getByLabel( theGoodElectronCollection , goodElectronCands);
175    
176     // loose muons
177     Handle<CandidateCollection> looseMuonCands;
178     iEvent.getByLabel( theLooseMuonCollection , looseMuonCands);
179    
180     // good muons
181     Handle<CandidateCollection> goodMuonCands;
182     iEvent.getByLabel( theGoodMuonCollection , goodMuonCands);
183    
184    
185     // tight leptons
186     Handle<CandidateCollection> tightLeptonCands;
187     iEvent.getByLabel( theTightLeptonCollection , tightLeptonCands);
188    
189 vuko 1.10 // jets
190     Handle<CandidateCollection> jetCands;
191     iEvent.getByLabel( theJetCollection , jetCands);
192    
193 smorovic 1.6
194     // get hold of TriggerResults
195     Handle<TriggerResults> HLTR;
196     if (storeHLTresults) {
197 vuko 1.7 iEvent.getByType(HLTR);
198     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     for (int i=0; i<looseMuonCands->size();i++){
209     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     int nzee = zeeCands->size();
223     int nzmumu = zmumuCands->size();
224    
225     cout << "\t # loose mu : " << looseMuonCands->size()
226     << "\t # tight mu : " << goodMuonCands->size()
227     << "\t # loose e : " << looseElectronCands->size()
228     << "\t # tight e : " << goodElectronCands->size()
229     << "\t # tight l : " << tightLeptonCands->size()
230     << "\t # Z->mumu : " << zmumuCands->size()
231     << "\t # Z->ee : " << zeeCands->size()
232 vuko 1.7 << "\t # Z->ll : " << zllCands->size()
233 vuko 1.1 << endl;
234    
235     Handle<CandidateCollection> zCands[2] = {zeeCands,zmumuCands};
236    
237     //
238 vuko 1.7 // Find best Z (in Z->ll collection (merged Z->ee & Z->mumu)
239 vuko 1.1 //
240     ::OverlapChecker overlap;
241    
242 vuko 1.3
243     float dzmass_min = 100.;
244    
245     for(CandidateCollection::const_iterator z1 = zllCands->begin();
246     z1 != zllCands->end(); ++ z1 ) {
247     float dzmass = fabs( z1->mass() - 91.188);
248     if (dzmass < dzmass_min) {
249 vuko 1.7 theZCandidate = &(*z1);
250 vuko 1.3 dzmass_min = dzmass;
251     }
252     //
253     //Get back Electrons from Z and W
254     for(CandidateCollection::const_iterator z2 = z1;
255     z2 != zllCands->end(); ++ z2 ) {
256     if (z1 != z2) {
257     if (overlap(*z1,*z2)) {
258 vuko 1.7 cout << "Overlap between two Z of flavour "
259     << z1->daughter(0)->pdgId() << "\t"
260     << z2->daughter(0)->pdgId() << "\t"
261     << endl;
262 vuko 1.3 } else {
263     cout << "NON OVERLAPPING Zs " << endl;
264     }
265     }
266     }
267     }
268    
269    
270 vuko 1.1 zFlavour = 0;
271     wlFlavour = 0;
272    
273 senka 1.9 dPhi_WlZ_reco=-15.;
274     dPhi_WZ_reco=-15.;
275 vuko 1.4
276 vuko 1.3
277 vuko 1.1 int nwl_candidates=0;
278     if (theZCandidate) {
279    
280    
281     zMass = theZCandidate->mass();
282     zFlavour = abs(theZCandidate->daughter(0)->pdgId());
283     zPt = theZCandidate->pt();
284     zEta = theZCandidate->eta();
285     zPhi = theZCandidate->phi();
286    
287 vuko 1.4 if (zFlavour == 11) {
288 vuko 1.5 leptonProperties["ZEl1"]->FillProperties(theZCandidate->daughter(0), iEvent, iSetup);
289     leptonProperties["ZEl2"]->FillProperties(theZCandidate->daughter(1), iEvent, iSetup);
290 vuko 1.4 } else if (zFlavour == 13) {
291 vuko 1.5 leptonProperties["Zmu1"]->FillProperties(theZCandidate->daughter(0), iEvent, iSetup);
292     leptonProperties["Zmu2"]->FillProperties(theZCandidate->daughter(1), iEvent, iSetup);
293 vuko 1.4 }
294    
295 vuko 1.1 float max_pt = 0.;
296    
297 senka 1.9 /// Find W lepton
298    
299 vuko 1.1
300     // Now find lepton that will be associated to W
301     // among leptons not used for the Z reconstruction
302     for(CandidateCollection::const_iterator lepton = tightLeptonCands->begin();
303     lepton != tightLeptonCands->end(); lepton++) {
304    
305    
306     if ( overlap(*theZCandidate, *lepton) ) continue; // Ignore if lepton used for the Z
307    
308     nwl_candidates++;
309    
310     // If more than one candidate: choose leading pt
311     if (lepton->pt() > max_pt) {
312     theWlepton = &(*lepton);
313     max_pt = lepton->pt();
314     }
315    
316     int id = lepton->pdgId();
317     cout << "Tight lepton: " << id << endl;
318    
319     if(lepton->hasMasterClone()) {
320     cout << "SHOUT: TIGHT LEPTON IS SHALLOW COPY !!! (SHOULD NOT BE!) \n";
321     }
322     //
323     }
324     if (theWlepton) {
325     wlFlavour = theWlepton->pdgId();
326     wlCharge = theWlepton->charge();
327     wlPt = theWlepton->pt();
328     wlEta = theWlepton->eta();
329     wlPhi = theWlepton->phi();
330    
331 senka 1.9 cout << "found W lepton \n";
332 vuko 1.1
333 vuko 1.3 if (abs(wlFlavour) == 11) {
334     leptonProperties["Wel"]->FillProperties(theWlepton, iEvent, iSetup);
335     } else if (abs(wlFlavour) == 13) {
336 senka 1.9 cout << "found W lepton: it is a muon \n";
337 vuko 1.3 leptonProperties["Wmu"]->FillProperties(theWlepton, iEvent, iSetup);
338     }
339 senka 1.9
340     // dPhi(W,Z)
341     cout<<"theWlepton->phi()="<<theWlepton->phi() <<endl;
342     cout<<"theZCandidate->phi()="<<theZCandidate->phi() <<endl;
343     cout<<"dphi()="<<theWlepton->phi()-theZCandidate->phi() <<endl;
344     dPhi_WlZ_reco=acos(cos(theWlepton->phi()-theZCandidate->phi()));
345     dPhi_WZ_reco=acos(cos((theWlepton->p4()+MET).phi()-theZCandidate->phi()));
346 vuko 1.3
347 vuko 1.1 const reco::Muon * muon = dynamic_cast<const reco::Muon *>(&(*theWlepton));
348     if(muon != 0) { /* it's a valid muon */
349     cout << "lepton is a muon \n" << endl;
350     }
351     const reco::PixelMatchGsfElectron * electron = dynamic_cast<const reco::PixelMatchGsfElectron *>(&(*theWlepton));
352     if(electron != 0) { /* it's a valid electron */
353     cout << "lepton is an electron \n" << endl;
354     elWTree->Fill();
355     }
356     }
357    
358     }
359 smorovic 1.6
360 vuko 1.10 // Handle <reco::GenParticleCandidateCollection> mccands;
361     Handle<CandidateCollection> mccands;
362     iEvent.getByLabel( theMCTruthCollection,mccands );
363 smorovic 1.6
364 vuko 1.11 collectMCsummary(mccands);
365 smorovic 1.6
366 smorovic 1.8 if (storeHLTresults) {
367    
368 smorovic 1.6 //def: 1:single electron 2:single relaxed 3:double electron 4: double relaxed 5,6,7,8: muon e+mu: 9
369 smorovic 1.8
370 smorovic 1.6 triggerBitmask=0;
371 smorovic 1.8
372 smorovic 1.6 for (size_t i=0; i<numTriggers; ++i) {
373    
374     // cout << "trigName" << triggerNames[i] << "wasRUN:" << HLTR->wasrun(i) << "error " << HLTR->error(i)
375     // << " accept" << HLTR->accept(i) << "\n";
376    
377     if (!HLTR->wasrun(i)) {
378     //cout << "trigger not run? "<< triggerNames[i] << "\n";
379     continue;
380     }
381     if (HLTR->error(i) ) {
382     //cout << "error trigger: "<< triggerNames[i] <<"\n";
383     continue;
384     }
385     if (HLTR->accept(i)) {
386     //if (triggerNames[i].compare("mcpath")!=0)
387     //TRaccept=true;
388     }else {
389     continue;
390     }
391    
392     if (triggerNames[i].compare("HLT1Electron")==0) triggerBitmask |= 1;
393     if (triggerNames[i].compare("HLT1ElectronRelaxed")==0) triggerBitmask |= 2;
394     if (triggerNames[i].compare("HLT2Electron")==0) triggerBitmask |= 4;
395     if (triggerNames[i].compare("HLT2ElectronRelaxed")==0) triggerBitmask |= 8;
396    
397     if (triggerNames[i].compare("HLT1MuonIso")==0) triggerBitmask |= 16;
398     if (triggerNames[i].compare("HLT1MuonNonIso")==0) triggerBitmask |= 32;
399     if (triggerNames[i].compare("HLT2MuonNonIso")==0) triggerBitmask |= 64;
400     if (triggerNames[i].compare("HLT2MuonZ")==0) triggerBitmask |= 128;
401    
402     if (triggerNames[i].compare("HLTXElectronMuon")==0) triggerBitmask |= 256;
403     if (triggerNames[i].compare("HLTXElectronMuonRelaxed")==0) triggerBitmask |= 512;
404     }
405     }
406 vuko 1.10
407    
408     // Jet properties
409    
410     float max_et = 0.;
411     const Candidate * leadingJet = 0;
412     for(CandidateCollection::const_iterator jet = jetCands->begin();
413     jet != jetCands->end(); jet++) {
414     if (jet->et() > max_et ) {
415     leadingJet = &(*jet);
416     max_et = jet->et();
417     }
418     }
419     if (leadingJet) {
420     jet1Et = leadingJet->et();
421     jet1Phi = leadingJet->et();;
422     jet1Eta = leadingJet->eta();
423    
424     } else {
425     jet1Et = -10;
426     jet1Phi = 0.;
427     jet1Eta = 0.;
428     }
429 senka 1.9
430     cout<<"dphi()="<<dPhi_WlZ_reco <<endl;
431 vuko 1.10
432 vuko 1.1 wzTree->Fill();
433    
434     }
435    
436    
437     // ------------ method called once each job just before starting event loop ------------
438     void
439     WZAnalyzer::beginJob(const edm::EventSetup&)
440     {
441    
442     using namespace wzana;
443    
444     theFile = new TFile( "wz.root", "RECREATE" ) ;
445    
446     wzTree = new TTree("WZTree","WZ informations");
447    
448     elWTree = new TTree("WElTree","W electron informations");
449    
450 vuko 1.3 leptonProperties["Wel"] = new ElectronProperties();
451     leptonProperties["ZEl1"] = new ElectronProperties();
452     leptonProperties["ZEl2"] = new ElectronProperties();
453    
454     leptonProperties["Wmu"] = new MuonProperties();
455     leptonProperties["Zmu1"] = new MuonProperties();
456     leptonProperties["Zmu2"] = new MuonProperties();
457 vuko 1.1
458 vuko 1.3 leptonProperties["Wel"]->CreateBranches(wzTree, "WEl_");
459     leptonProperties["Wel"]->CreateBranches(elWTree,"ElW_");
460 vuko 1.4 leptonProperties["ZEl1"]->CreateBranches(wzTree, "ZEl1_");
461     leptonProperties["ZEl2"]->CreateBranches(wzTree, "ZEl2_");
462 vuko 1.1
463 senka 1.9 leptonProperties["Wmu"] ->CreateBranches(wzTree, "Wmu_");
464     leptonProperties["Zmu1"] ->CreateBranches(wzTree, "Zmul_");
465     leptonProperties["Zmu2"] ->CreateBranches(wzTree, "Zmu2_");
466    
467 vuko 1.1 initialiseTree();
468 smorovic 1.6
469     //prepare HLT TriggerResults branch
470     if (storeHLTresults) {
471     wzTree->Branch("WZ_hltBitmask",&triggerBitmask,"WZ_hltBitmask/I");
472    
473     }
474 senka 1.9
475     // MET branch
476     wzTree->Branch("MET_energy",&MET_energy,"MET_energy/F");
477     wzTree->Branch("MET_pt",&MET_pt,"MET_pt/F");
478     wzTree->Branch("MET_eta",&MET_eta,"MET_eta/F");
479     wzTree->Branch("dPhi_WlZ_reco",&dPhi_WlZ_reco,"dPhi_WlZ_reco/F");
480     wzTree->Branch("dPhi_WZ_reco",&dPhi_WZ_reco,"dPhi_WZ_reco/F");
481    
482 vuko 1.1 }
483    
484     // ------------ method called once each job just after ending the event loop ------------
485     void
486     WZAnalyzer::endJob() {
487    
488     theFile->Write();
489     theFile->Close();
490    
491     }
492    
493    
494     void WZAnalyzer::initialiseTree() {
495    
496     // Z properties
497     wzTree->Branch("Zmass", &zMass, "Zmass/F");
498     wzTree->Branch("ZId", &zFlavour, "Zid/I");
499     wzTree->Branch("ZPt", &zPt, "ZPt/F");
500     wzTree->Branch("ZEta", &zEta, "ZEta/F");
501     wzTree->Branch("ZPhi", &zPhi, "ZPhi/F");
502    
503     // W Properties
504     wzTree->Branch("WlId", &wlFlavour, "Wlid/I");
505     wzTree->Branch("WlCharge", &wlCharge, "WlCharge/I");
506     wzTree->Branch("WlPt", &wlPt, "WlPt/F");
507 smorovic 1.8
508     wzTree->Branch("MC_leptonZ1_pt", &MCleptZ1_pt,"MC_leptonZ1_pt/F");
509     wzTree->Branch("MC_leptonZ2_pt", &MCleptZ2_pt,"MC_leptonZ2_pt/F");
510     wzTree->Branch("MC_leptonW_pt", &MCleptW_pt, "MC_leptonW_pt/F");
511    
512     wzTree->Branch("MC_leptonZ1_eta", &MCleptZ1_eta,"MC_leptonZ1_eta/F");
513     wzTree->Branch("MC_leptonZ2_eta", &MCleptZ2_eta,"MC_leptonZ2_eta/F");
514     wzTree->Branch("MC_leptonW_eta", &MCleptW_eta, "MC_leptonW_eta/F");
515    
516     wzTree->Branch("MC_leptonZ1_phi", &MCleptZ1_phi,"MC_leptonZ1_phi/F");
517     wzTree->Branch("MC_leptonZ2_phi", &MCleptZ2_phi,"MC_leptonZ2_phi/F");
518     wzTree->Branch("MC_leptonW_phi", &MCleptW_phi, "MC_leptonW_phi/F");
519    
520     wzTree->Branch("MC_leptonZ1_pdgid", &MCleptZ1_pdgid,"MC_leptonZ1_pdgid/I");
521     wzTree->Branch("MC_leptonZ2_pdgid", &MCleptZ2_pdgid,"MC_leptonZ2_pdgid/I");
522     wzTree->Branch("MC_leptonW_pdgid", &MCleptW_pdgid, "MC_leptonW_pdgid/I");
523    
524     wzTree->Branch("MC_TauDecayType_fromZ1", &MC_tauDecayTypeZ1,"MC_TauDecayType_fromZ1/I");
525     wzTree->Branch("MC_TauDecayType_fromZ2", &MC_tauDecayTypeZ2,"MC_TauDecayType_fromZ2/I");
526     wzTree->Branch("MC_TauDecayType_fromW", &MC_tauDecayTypeW, "MC_TauDecayType_fromW/I");
527 vuko 1.10
528     // MET properties
529    
530     // Jet properties
531     wzTree->Branch("Jet1Et", &jet1Et, "Jet1Et/F");
532     wzTree->Branch("Jet1Phi", &jet1Phi, "Jet1Phi/F");
533     wzTree->Branch("Jet1Eta", &jet1Eta, "Jet1Eta/F");
534    
535     wzTree->Branch("Jet2Et", &jet2Et, "Jet2Et/F");
536     wzTree->Branch("Jet2Phi", &jet2Phi, "Jet2Phi/F");
537     wzTree->Branch("Jet2Eta", &jet2Eta, "Jet2Eta/F");
538    
539    
540 smorovic 1.8 }
541 vuko 1.1
542 senka 1.9
543     reco::Particle::LorentzVector WZAnalyzer::computeMET(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
544    
545     using namespace edm;
546     using namespace reco;
547     using namespace std;
548    
549     Handle<CaloMETCollection> mets;
550     iEvent.getByLabel("met", mets);
551     cout<<"# METs="<< mets->size() <<endl;
552     return mets->begin()->p4();
553    
554     }
555    
556    
557 vuko 1.11
558 vuko 1.10 void WZAnalyzer::collectMCsummary(Handle <reco::CandidateCollection> mccands) {
559     //(Handle <reco::GenParticleCandidateCollection> mccands) {
560     //
561     //void WZAnalyzer::collectMCsummary(Handle <reco::GenParticleCandidateCollection> mccands) {
562 smorovic 1.8
563 vuko 1.11 using namespace reco;
564    
565 smorovic 1.8 //collections
566     MCleptZ1_pdgid = -1;
567     MCleptZ2_pdgid = -1;
568     MCleptW_pdgid = -1;
569    
570     MCleptZ1_pt = -1;
571     MCleptZ2_pt = -1;
572     MCleptW_pt = -1;
573    
574     MCleptZ1_eta = -1;
575     MCleptZ2_eta = -1;
576     MCleptW_eta = -1;
577    
578     MCleptZ1_phi = -1;
579     MCleptZ2_phi = -1;
580     MCleptW_phi = -1;
581    
582    
583    
584    
585     vector<reco::GenParticleCandidate*> Tau;
586     vector<reco::GenParticleCandidate*> StableMuons;
587     vector<reco::GenParticleCandidate*> StableElectrons;
588    
589 vuko 1.11 // reco::Candidate::iterator p;
590     // for (p = ((reco::Candidate*)&mccands)->begin(); p != ((reco::Candidate*)&mccands)->end(); ++p ) {
591     for(CandidateCollection::const_iterator p = mccands->begin();
592     p != mccands->end(); p++) {
593 vuko 1.1
594 vuko 1.11 // reco::Candidate* p_tmp = &(*p);
595     reco::GenParticleCandidate* ptr = (reco::GenParticleCandidate*) &(*p);
596 smorovic 1.8
597     if ( (ptr)->status() == 1
598     /*&& (ptr)->momentum().perp() > 2.*/ ) { // stable particles with pt>2 only
599     if ( abs((ptr)->pdgId()) == 11 ) {
600     StableElectrons.push_back((ptr));
601     //cout << "electron MCT\n";
602     }
603     else if ( abs((ptr)->pdgId()) == 13 ) {
604     StableMuons.push_back((ptr)) ;
605     //cout << "muon MCT\n";
606     }
607     }
608     else if ((ptr)->status() == 2) {
609     if ( abs((ptr)->pdgId()) == 15 ) {//tau
610     Tau.push_back((ptr));
611     }
612     }
613     }
614 vuko 1.11 std::cout << "# Electrons : " << StableElectrons.size()
615     << "\t# Muons : " << StableMuons.size()
616     << "\t# Tau : " << Tau.size() << std::endl;
617 smorovic 1.8
618     vector<reco::GenParticleCandidate*> * StableLeptons[3] = {&StableElectrons, &StableMuons, &Tau};
619    
620     bool firstZlept = true;
621     MC_tauDecayTypeZ1 = 0;
622     MC_tauDecayTypeZ2 = 0;
623     MC_tauDecayTypeW = 0;
624    
625    
626     for (int i=2; i>=0; i--) {
627     while (StableLeptons[i]->size() > 0) {
628     float maxPt = 0;
629     vector<reco::GenParticleCandidate*>::iterator index = StableLeptons[i]->end();
630    
631     for ( vector<reco::GenParticleCandidate*>::iterator lepton = StableLeptons[i]->begin();
632     lepton != StableLeptons[i]->end(); lepton++ ) {
633    
634     if ((*lepton)->pt() > maxPt) {
635     maxPt = (*lepton)->pt();
636     index = lepton;
637     }
638     }
639     int parentItr=0;
640     bool Zchild = false;
641     bool Wchild = false;
642     bool Tauchild = false;
643    
644     reco::GenParticleCandidate* mcParticleRef = *index;
645    
646     //find W/Z mother
647     while ( mcParticleRef->mother()!=0) {
648    
649     if (parentItr>=2) break;
650     parentItr++;
651     mcParticleRef = (reco::GenParticleCandidate*) mcParticleRef->mother();
652     if (mcParticleRef->mother()==0) break;
653    
654     //if tau
655     if (abs((mcParticleRef)->pdgId())==15 ) {
656     Tauchild=true;
657     break;
658     }
659    
660     // cout << "mother: " << (mcParticleRef)->pdg_id()
661     // <<" pt="<< c_p4.pt() << " eta="<<c_p4.eta()
662     // <<" status=" <<mcParticleRef->status() <<"\n";
663    
664     if (abs((mcParticleRef)->pdgId())==23 ) {
665     Zchild=true;
666     break;
667     }
668     if (abs((mcParticleRef)->pdgId())==24 ) {
669     Wchild=true;
670     break;
671     }
672     }
673    
674     if (maxPt >0) {
675     if (Tauchild) {
676     parentItr = 0;
677     while (mcParticleRef->mother()!=0) {
678     if (parentItr>=2) break;
679     parentItr++;
680     mcParticleRef = (reco::GenParticleCandidate*) mcParticleRef->mother();
681     if (mcParticleRef->mother()==0) break;
682    
683     if (abs((mcParticleRef)->pdgId())==23 ) {
684     if (MCleptZ1_pt == float(mcParticleRef->pt())
685     && MCleptZ1_eta == float(mcParticleRef->eta())
686     && MCleptZ1_phi == float(mcParticleRef->phi()) )
687     MC_tauDecayTypeZ1 = i;
688     else MC_tauDecayTypeZ2 = i;
689     break;
690     }
691     if (abs((mcParticleRef)->pdgId())==24 ) {
692     MC_tauDecayTypeW = i;
693     break;
694     }
695     }
696     }
697    
698     if (Wchild) {
699     MCleptW_pdgid=(*index)->pdgId();
700     MCleptW_pt=(float)(*index)->pt();
701     MCleptW_eta=(float)(*index)->eta();
702     MCleptW_phi=(float)(*index)->phi();
703     if (abs(MCleptW_pdgid)==15) MC_tauDecayTypeW =3;//default to hadronic decay
704     }
705     if (Zchild) {
706     if (firstZlept) {
707     firstZlept=false;
708     MCleptZ1_pdgid=(*index)->pdgId();
709     MCleptZ1_pt=(float)(*index)->pt();
710     MCleptZ1_eta=(float)(*index)->eta();
711     MCleptZ1_phi=(float)(*index)->phi();
712     if (abs(MCleptZ1_pdgid)==15) MC_tauDecayTypeZ1 =3;
713     }
714     else {
715     MCleptZ2_pdgid=(*index)->pdgId();
716     MCleptZ2_pt=(float)(*index)->pt();
717     MCleptZ2_eta=(float)(*index)->eta();
718     MCleptZ2_phi=(float)(*index)->phi();
719     if (abs(MCleptZ2_pdgid)==15) MC_tauDecayTypeZ2 =3;
720     }
721     }
722     StableLeptons[i]->erase(index);
723     }
724     }
725     }
726 vuko 1.1
727 smorovic 1.8 }
728 vuko 1.1 //define this as a plug-in
729 vuko 1.2 // DEFINE_FWK_MODULE(WZAnalyzer);