ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/Vuko/WZAnalysis/src/WZAnalyzer.cc
Revision: 1.13
Committed: Wed Nov 28 09:29:13 2007 UTC (17 years, 5 months ago) by senka
Content type: text/plain
Branch: MAIN
Changes since 1.12: +37 -5 lines
Log Message:
 mother of gen particle

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.13 // $Id: WZAnalyzer.cc,v 1.12 2007/11/27 18:27:51 senka 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 senka 1.12 // theMCTruthCollection = iConfig.getParameter<edm::InputTag>("genParticleCollection");
81    
82 vuko 1.1 theLooseMuonCollection = iConfig.getParameter<edm::InputTag>("LooseMuons");
83     theGoodMuonCollection = iConfig.getParameter<edm::InputTag>("GoodMuons");
84     theLooseElectronCollection = iConfig.getParameter<edm::InputTag>("LooseElectrons");
85     theGoodElectronCollection = iConfig.getParameter<edm::InputTag>("GoodElectrons");
86     theTightLeptonCollection = iConfig.getParameter<edm::InputTag>("TightLeptons");
87     // theMediumElectronCollection = iConfig.getParameter<edm::InputTag>("LooseElectrons");
88     // theTightElectronCollection = iConfig.getParameter<edm::InputTag>("TightElectrons");
89     // Z's
90     theZeeCollection = iConfig.getParameter<edm::InputTag>("ZtoEE");
91     theZmumuCollection = iConfig.getParameter<edm::InputTag>("ZtoMuMu");
92 vuko 1.3 theZllCollection = iConfig.getParameter<edm::InputTag>("ZtoLL");
93 vuko 1.10
94     theJetCollection = iConfig.getParameter<edm::InputTag>("Jets");
95 smorovic 1.6
96     storeHLTresults=false;
97     storeHLTresults = iConfig.getParameter<bool>("storeHLTresults");
98 vuko 1.1
99 smorovic 1.6 firstTriggerResult = true;
100 vuko 1.1 }
101    
102    
103     WZAnalyzer::~WZAnalyzer()
104     {
105    
106     // do anything here that needs to be done at desctruction time
107     // (e.g. close files, deallocate resources etc.)
108    
109     }
110    
111    
112     //
113     // member functions
114     //
115    
116     // ------------ method called to for each event ------------
117     void
118     WZAnalyzer::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup)
119     {
120     using namespace edm;
121     using namespace reco;
122     using namespace std;
123    
124     #ifdef THIS_IS_AN_EVENT_EXAMPLE
125     Handle<ExampleData> pIn;
126     iEvent.getByLabel("example",pIn);
127     #endif
128    
129     #ifdef THIS_IS_AN_EVENTSETUP_EXAMPLE
130     ESHandle<SetupData> pSetup;
131     iSetup.get<SetupRecord>().get(pSetup);
132     #endif
133    
134 vuko 1.11 ///////////////////////////////////////
135     //
136     /// EVENT INITIALISATION
137     ///
138    
139     // Reset a few things
140    
141     for (map<string,wzana::LeptonProperties* >::iterator iter = leptonProperties.begin();
142     iter!=leptonProperties.end(); iter++)
143     {
144     iter->second->ResetValues();
145     }
146    
147    
148 vuko 1.1 const Candidate * theZCandidate = 0;
149     const Candidate * theWlepton = 0;
150    
151 vuko 1.11
152 senka 1.12 // Tau.clear();
153     // StableMuons.clear();
154     // StableElectrons.clear();
155    
156 vuko 1.11
157     ////////////////////////////////////////
158     // Get lists
159    
160 vuko 1.1 // Z->mumu
161     Handle<CandidateCollection> zmumuCands;
162     iEvent.getByLabel( theZmumuCollection , zmumuCands);
163    
164    
165     // Z->ee
166     Handle<CandidateCollection> zeeCands;
167     iEvent.getByLabel( theZeeCollection , zeeCands);
168    
169 vuko 1.3 // Z->ee
170     Handle<CandidateCollection> zllCands;
171     iEvent.getByLabel( theZllCollection , zllCands);
172    
173 vuko 1.1 // loose electrons
174     Handle<CandidateCollection> looseElectronCands;
175     iEvent.getByLabel( theLooseElectronCollection , looseElectronCands);
176    
177     // good electrons
178     Handle<CandidateCollection> goodElectronCands;
179     iEvent.getByLabel( theGoodElectronCollection , goodElectronCands);
180    
181     // loose muons
182     Handle<CandidateCollection> looseMuonCands;
183     iEvent.getByLabel( theLooseMuonCollection , looseMuonCands);
184    
185     // good muons
186     Handle<CandidateCollection> goodMuonCands;
187     iEvent.getByLabel( theGoodMuonCollection , goodMuonCands);
188    
189    
190     // tight leptons
191     Handle<CandidateCollection> tightLeptonCands;
192     iEvent.getByLabel( theTightLeptonCollection , tightLeptonCands);
193    
194 vuko 1.10 // jets
195     Handle<CandidateCollection> jetCands;
196     iEvent.getByLabel( theJetCollection , jetCands);
197    
198 smorovic 1.6
199     // get hold of TriggerResults
200     Handle<TriggerResults> HLTR;
201     if (storeHLTresults) {
202 vuko 1.7 iEvent.getByType(HLTR);
203     if (firstTriggerResult) {
204     firstTriggerResult = false;
205     triggerNames= HLTR->getTriggerNames();
206     numTriggers = triggerNames.size();
207     }
208 smorovic 1.6
209 vuko 1.7 }
210 vuko 1.1
211 senka 1.9 // MET
212     reco::Particle::LorentzVector Muon_p(0,0,0,0);
213     for (int i=0; i<looseMuonCands->size();i++){
214     Muon_p=Muon_p+(*looseMuonCands)[i].p4();
215     }
216     reco::Particle::LorentzVector MET=computeMET(iEvent, iSetup)-Muon_p; // substract p from muons from MET
217     MET_energy=MET.energy();
218     MET_pt=MET.pt();
219     MET_eta=MET.eta();
220    
221    
222 vuko 1.1 // selected muons
223    
224    
225     //
226    
227     int nzee = zeeCands->size();
228     int nzmumu = zmumuCands->size();
229    
230     cout << "\t # loose mu : " << looseMuonCands->size()
231     << "\t # tight mu : " << goodMuonCands->size()
232     << "\t # loose e : " << looseElectronCands->size()
233     << "\t # tight e : " << goodElectronCands->size()
234     << "\t # tight l : " << tightLeptonCands->size()
235     << "\t # Z->mumu : " << zmumuCands->size()
236     << "\t # Z->ee : " << zeeCands->size()
237 vuko 1.7 << "\t # Z->ll : " << zllCands->size()
238 vuko 1.1 << endl;
239    
240     Handle<CandidateCollection> zCands[2] = {zeeCands,zmumuCands};
241    
242     //
243 vuko 1.7 // Find best Z (in Z->ll collection (merged Z->ee & Z->mumu)
244 vuko 1.1 //
245     ::OverlapChecker overlap;
246    
247 vuko 1.3
248     float dzmass_min = 100.;
249    
250     for(CandidateCollection::const_iterator z1 = zllCands->begin();
251     z1 != zllCands->end(); ++ z1 ) {
252     float dzmass = fabs( z1->mass() - 91.188);
253     if (dzmass < dzmass_min) {
254 vuko 1.7 theZCandidate = &(*z1);
255 vuko 1.3 dzmass_min = dzmass;
256     }
257     //
258     //Get back Electrons from Z and W
259     for(CandidateCollection::const_iterator z2 = z1;
260     z2 != zllCands->end(); ++ z2 ) {
261     if (z1 != z2) {
262     if (overlap(*z1,*z2)) {
263 vuko 1.7 cout << "Overlap between two Z of flavour "
264     << z1->daughter(0)->pdgId() << "\t"
265     << z2->daughter(0)->pdgId() << "\t"
266     << endl;
267 vuko 1.3 } else {
268     cout << "NON OVERLAPPING Zs " << endl;
269     }
270     }
271     }
272     }
273    
274    
275 vuko 1.1 zFlavour = 0;
276     wlFlavour = 0;
277    
278 senka 1.9 dPhi_WlZ_reco=-15.;
279     dPhi_WZ_reco=-15.;
280 vuko 1.4
281 vuko 1.3
282 vuko 1.1 int nwl_candidates=0;
283     if (theZCandidate) {
284    
285    
286     zMass = theZCandidate->mass();
287     zFlavour = abs(theZCandidate->daughter(0)->pdgId());
288     zPt = theZCandidate->pt();
289     zEta = theZCandidate->eta();
290     zPhi = theZCandidate->phi();
291    
292 vuko 1.4 if (zFlavour == 11) {
293 vuko 1.5 leptonProperties["ZEl1"]->FillProperties(theZCandidate->daughter(0), iEvent, iSetup);
294     leptonProperties["ZEl2"]->FillProperties(theZCandidate->daughter(1), iEvent, iSetup);
295 vuko 1.4 } else if (zFlavour == 13) {
296 vuko 1.5 leptonProperties["Zmu1"]->FillProperties(theZCandidate->daughter(0), iEvent, iSetup);
297     leptonProperties["Zmu2"]->FillProperties(theZCandidate->daughter(1), iEvent, iSetup);
298 vuko 1.4 }
299    
300 vuko 1.1 float max_pt = 0.;
301    
302 senka 1.9 /// Find W lepton
303    
304 vuko 1.1
305     // Now find lepton that will be associated to W
306     // among leptons not used for the Z reconstruction
307     for(CandidateCollection::const_iterator lepton = tightLeptonCands->begin();
308     lepton != tightLeptonCands->end(); lepton++) {
309    
310    
311     if ( overlap(*theZCandidate, *lepton) ) continue; // Ignore if lepton used for the Z
312    
313     nwl_candidates++;
314    
315     // If more than one candidate: choose leading pt
316     if (lepton->pt() > max_pt) {
317     theWlepton = &(*lepton);
318     max_pt = lepton->pt();
319     }
320    
321     int id = lepton->pdgId();
322     cout << "Tight lepton: " << id << endl;
323    
324     if(lepton->hasMasterClone()) {
325     cout << "SHOUT: TIGHT LEPTON IS SHALLOW COPY !!! (SHOULD NOT BE!) \n";
326     }
327     //
328     }
329     if (theWlepton) {
330     wlFlavour = theWlepton->pdgId();
331     wlCharge = theWlepton->charge();
332     wlPt = theWlepton->pt();
333     wlEta = theWlepton->eta();
334     wlPhi = theWlepton->phi();
335    
336 senka 1.9 cout << "found W lepton \n";
337 vuko 1.1
338 vuko 1.3 if (abs(wlFlavour) == 11) {
339     leptonProperties["Wel"]->FillProperties(theWlepton, iEvent, iSetup);
340     } else if (abs(wlFlavour) == 13) {
341 senka 1.9 cout << "found W lepton: it is a muon \n";
342 vuko 1.3 leptonProperties["Wmu"]->FillProperties(theWlepton, iEvent, iSetup);
343     }
344 senka 1.9
345     // dPhi(W,Z)
346     cout<<"theWlepton->phi()="<<theWlepton->phi() <<endl;
347     cout<<"theZCandidate->phi()="<<theZCandidate->phi() <<endl;
348     cout<<"dphi()="<<theWlepton->phi()-theZCandidate->phi() <<endl;
349     dPhi_WlZ_reco=acos(cos(theWlepton->phi()-theZCandidate->phi()));
350     dPhi_WZ_reco=acos(cos((theWlepton->p4()+MET).phi()-theZCandidate->phi()));
351 vuko 1.3
352 vuko 1.1 const reco::Muon * muon = dynamic_cast<const reco::Muon *>(&(*theWlepton));
353     if(muon != 0) { /* it's a valid muon */
354     cout << "lepton is a muon \n" << endl;
355     }
356     const reco::PixelMatchGsfElectron * electron = dynamic_cast<const reco::PixelMatchGsfElectron *>(&(*theWlepton));
357     if(electron != 0) { /* it's a valid electron */
358     cout << "lepton is an electron \n" << endl;
359     elWTree->Fill();
360     }
361     }
362    
363     }
364 smorovic 1.6
365 vuko 1.10 // Handle <reco::GenParticleCandidateCollection> mccands;
366     Handle<CandidateCollection> mccands;
367     iEvent.getByLabel( theMCTruthCollection,mccands );
368 smorovic 1.6
369 vuko 1.11 collectMCsummary(mccands);
370 smorovic 1.6
371 smorovic 1.8 if (storeHLTresults) {
372    
373 smorovic 1.6 //def: 1:single electron 2:single relaxed 3:double electron 4: double relaxed 5,6,7,8: muon e+mu: 9
374 smorovic 1.8
375 smorovic 1.6 triggerBitmask=0;
376 smorovic 1.8
377 smorovic 1.6 for (size_t i=0; i<numTriggers; ++i) {
378    
379     // cout << "trigName" << triggerNames[i] << "wasRUN:" << HLTR->wasrun(i) << "error " << HLTR->error(i)
380     // << " accept" << HLTR->accept(i) << "\n";
381    
382     if (!HLTR->wasrun(i)) {
383     //cout << "trigger not run? "<< triggerNames[i] << "\n";
384     continue;
385     }
386     if (HLTR->error(i) ) {
387     //cout << "error trigger: "<< triggerNames[i] <<"\n";
388     continue;
389     }
390     if (HLTR->accept(i)) {
391     //if (triggerNames[i].compare("mcpath")!=0)
392     //TRaccept=true;
393     }else {
394     continue;
395     }
396    
397     if (triggerNames[i].compare("HLT1Electron")==0) triggerBitmask |= 1;
398     if (triggerNames[i].compare("HLT1ElectronRelaxed")==0) triggerBitmask |= 2;
399     if (triggerNames[i].compare("HLT2Electron")==0) triggerBitmask |= 4;
400     if (triggerNames[i].compare("HLT2ElectronRelaxed")==0) triggerBitmask |= 8;
401    
402     if (triggerNames[i].compare("HLT1MuonIso")==0) triggerBitmask |= 16;
403     if (triggerNames[i].compare("HLT1MuonNonIso")==0) triggerBitmask |= 32;
404     if (triggerNames[i].compare("HLT2MuonNonIso")==0) triggerBitmask |= 64;
405     if (triggerNames[i].compare("HLT2MuonZ")==0) triggerBitmask |= 128;
406    
407     if (triggerNames[i].compare("HLTXElectronMuon")==0) triggerBitmask |= 256;
408     if (triggerNames[i].compare("HLTXElectronMuonRelaxed")==0) triggerBitmask |= 512;
409     }
410     }
411 vuko 1.10
412    
413     // Jet properties
414    
415     float max_et = 0.;
416     const Candidate * leadingJet = 0;
417     for(CandidateCollection::const_iterator jet = jetCands->begin();
418     jet != jetCands->end(); jet++) {
419     if (jet->et() > max_et ) {
420     leadingJet = &(*jet);
421     max_et = jet->et();
422     }
423     }
424     if (leadingJet) {
425     jet1Et = leadingJet->et();
426     jet1Phi = leadingJet->et();;
427     jet1Eta = leadingJet->eta();
428    
429     } else {
430     jet1Et = -10;
431     jet1Phi = 0.;
432     jet1Eta = 0.;
433     }
434 senka 1.9
435 senka 1.12
436     // matching:
437    
438     matching(iEvent,looseMuonCands, 13);
439     matching(iEvent,looseElectronCands, 11);
440    
441 senka 1.13 getMother(&(*mccands)[1]);
442 vuko 1.10
443 vuko 1.1 wzTree->Fill();
444    
445     }
446    
447    
448     // ------------ method called once each job just before starting event loop ------------
449     void
450     WZAnalyzer::beginJob(const edm::EventSetup&)
451     {
452    
453     using namespace wzana;
454    
455     theFile = new TFile( "wz.root", "RECREATE" ) ;
456    
457     wzTree = new TTree("WZTree","WZ informations");
458    
459     elWTree = new TTree("WElTree","W electron informations");
460    
461 vuko 1.3 leptonProperties["Wel"] = new ElectronProperties();
462     leptonProperties["ZEl1"] = new ElectronProperties();
463     leptonProperties["ZEl2"] = new ElectronProperties();
464    
465     leptonProperties["Wmu"] = new MuonProperties();
466     leptonProperties["Zmu1"] = new MuonProperties();
467     leptonProperties["Zmu2"] = new MuonProperties();
468 vuko 1.1
469 vuko 1.3 leptonProperties["Wel"]->CreateBranches(wzTree, "WEl_");
470     leptonProperties["Wel"]->CreateBranches(elWTree,"ElW_");
471 vuko 1.4 leptonProperties["ZEl1"]->CreateBranches(wzTree, "ZEl1_");
472     leptonProperties["ZEl2"]->CreateBranches(wzTree, "ZEl2_");
473 vuko 1.1
474 senka 1.9 leptonProperties["Wmu"] ->CreateBranches(wzTree, "Wmu_");
475     leptonProperties["Zmu1"] ->CreateBranches(wzTree, "Zmul_");
476     leptonProperties["Zmu2"] ->CreateBranches(wzTree, "Zmu2_");
477    
478 vuko 1.1 initialiseTree();
479 smorovic 1.6
480     //prepare HLT TriggerResults branch
481     if (storeHLTresults) {
482     wzTree->Branch("WZ_hltBitmask",&triggerBitmask,"WZ_hltBitmask/I");
483    
484     }
485 senka 1.9
486     // MET branch
487     wzTree->Branch("MET_energy",&MET_energy,"MET_energy/F");
488     wzTree->Branch("MET_pt",&MET_pt,"MET_pt/F");
489     wzTree->Branch("MET_eta",&MET_eta,"MET_eta/F");
490     wzTree->Branch("dPhi_WlZ_reco",&dPhi_WlZ_reco,"dPhi_WlZ_reco/F");
491     wzTree->Branch("dPhi_WZ_reco",&dPhi_WZ_reco,"dPhi_WZ_reco/F");
492    
493 vuko 1.1 }
494    
495     // ------------ method called once each job just after ending the event loop ------------
496     void
497     WZAnalyzer::endJob() {
498    
499     theFile->Write();
500     theFile->Close();
501    
502     }
503    
504    
505     void WZAnalyzer::initialiseTree() {
506    
507     // Z properties
508     wzTree->Branch("Zmass", &zMass, "Zmass/F");
509     wzTree->Branch("ZId", &zFlavour, "Zid/I");
510     wzTree->Branch("ZPt", &zPt, "ZPt/F");
511     wzTree->Branch("ZEta", &zEta, "ZEta/F");
512     wzTree->Branch("ZPhi", &zPhi, "ZPhi/F");
513    
514     // W Properties
515     wzTree->Branch("WlId", &wlFlavour, "Wlid/I");
516     wzTree->Branch("WlCharge", &wlCharge, "WlCharge/I");
517     wzTree->Branch("WlPt", &wlPt, "WlPt/F");
518 smorovic 1.8
519     wzTree->Branch("MC_leptonZ1_pt", &MCleptZ1_pt,"MC_leptonZ1_pt/F");
520     wzTree->Branch("MC_leptonZ2_pt", &MCleptZ2_pt,"MC_leptonZ2_pt/F");
521     wzTree->Branch("MC_leptonW_pt", &MCleptW_pt, "MC_leptonW_pt/F");
522    
523     wzTree->Branch("MC_leptonZ1_eta", &MCleptZ1_eta,"MC_leptonZ1_eta/F");
524     wzTree->Branch("MC_leptonZ2_eta", &MCleptZ2_eta,"MC_leptonZ2_eta/F");
525     wzTree->Branch("MC_leptonW_eta", &MCleptW_eta, "MC_leptonW_eta/F");
526    
527     wzTree->Branch("MC_leptonZ1_phi", &MCleptZ1_phi,"MC_leptonZ1_phi/F");
528     wzTree->Branch("MC_leptonZ2_phi", &MCleptZ2_phi,"MC_leptonZ2_phi/F");
529     wzTree->Branch("MC_leptonW_phi", &MCleptW_phi, "MC_leptonW_phi/F");
530    
531     wzTree->Branch("MC_leptonZ1_pdgid", &MCleptZ1_pdgid,"MC_leptonZ1_pdgid/I");
532     wzTree->Branch("MC_leptonZ2_pdgid", &MCleptZ2_pdgid,"MC_leptonZ2_pdgid/I");
533     wzTree->Branch("MC_leptonW_pdgid", &MCleptW_pdgid, "MC_leptonW_pdgid/I");
534    
535     wzTree->Branch("MC_TauDecayType_fromZ1", &MC_tauDecayTypeZ1,"MC_TauDecayType_fromZ1/I");
536     wzTree->Branch("MC_TauDecayType_fromZ2", &MC_tauDecayTypeZ2,"MC_TauDecayType_fromZ2/I");
537     wzTree->Branch("MC_TauDecayType_fromW", &MC_tauDecayTypeW, "MC_TauDecayType_fromW/I");
538 vuko 1.10
539     // MET properties
540    
541     // Jet properties
542     wzTree->Branch("Jet1Et", &jet1Et, "Jet1Et/F");
543     wzTree->Branch("Jet1Phi", &jet1Phi, "Jet1Phi/F");
544     wzTree->Branch("Jet1Eta", &jet1Eta, "Jet1Eta/F");
545    
546     wzTree->Branch("Jet2Et", &jet2Et, "Jet2Et/F");
547     wzTree->Branch("Jet2Phi", &jet2Phi, "Jet2Phi/F");
548     wzTree->Branch("Jet2Eta", &jet2Eta, "Jet2Eta/F");
549    
550    
551 smorovic 1.8 }
552 vuko 1.1
553 senka 1.9
554     reco::Particle::LorentzVector WZAnalyzer::computeMET(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
555    
556     using namespace edm;
557     using namespace reco;
558     using namespace std;
559    
560     Handle<CaloMETCollection> mets;
561     iEvent.getByLabel("met", mets);
562     cout<<"# METs="<< mets->size() <<endl;
563     return mets->begin()->p4();
564    
565     }
566    
567 senka 1.12 ////////////////////////
568     // matching
569     //
570    
571     class MatchingInfo {
572     public:
573    
574     MatchingInfo(float dr, int gen, int reco) : deltaR(dr), genMuon(gen),recoMuon(reco) {}
575    
576     bool operator<(const MatchingInfo m) { return deltaR < m.deltaR;}
577    
578     float deltaR;
579     int genMuon;
580     int recoMuon;
581    
582     };
583    
584    
585     bool betterMatch(MatchingInfo m1, MatchingInfo m2)
586     { return m1.deltaR < m2.deltaR;}
587    
588    
589     // method that gives the vector<MatchingInfo> matching_Infos containing (dR,i,j); i=genParticle index, j=ParticleCollection index:
590    
591     void WZAnalyzer::matching(const edm::Event& iEvent, Handle<reco::CandidateCollection> cands, int pdgid){
592    
593     using namespace edm;
594     using namespace reco;
595     using namespace std;
596    
597     //-------------------------
598     Handle<CandidateCollection> mccands;
599     iEvent.getByLabel( theMCTruthCollection,mccands );
600    
601     vector <GenParticleCandidate*> genparticles;
602    
603     for(CandidateCollection::const_iterator p = mccands->begin();
604     p != mccands->end(); p++) {
605    
606     // reco::Candidate* p_tmp = &(*p);
607     reco::GenParticleCandidate* ptr = (reco::GenParticleCandidate*) &(*p);
608    
609     if ( (ptr)->status() == 1
610     /*&& (ptr)->momentum().perp() > 2.*/ ) { // stable particles with pt>2 only
611     if ( abs((ptr)->pdgId()) == pdgid ) {
612     genparticles.push_back((ptr));
613     //cout << "electron MCT\n";
614     }
615     }
616     }
617    
618    
619     int n1=0;
620     vector<MatchingInfo> matching_Infos;
621     for ( unsigned int i=0; i<genparticles.size(); i++ ) // po svim MC muonima
622     {
623    
624     for (unsigned int j = 0; j < cands->size(); j++)// po svim reco muonima
625     {
626    
627     double d_eta2=(*cands)[j].eta()-(genparticles)[i]->eta();
628     double d_phi=fabs((*cands)[j].phi()-(genparticles)[i]->phi());
629     double dR_byhand= sqrt(d_eta2*d_eta2+d_phi*d_phi);
630     cout << "dr by hand = " << dR_byhand <<" i="<< i<<" j="<<j <<endl;
631     double dR=dR_byhand;
632    
633     if (dR<0.15){
634     n1++;
635     matching_Infos.push_back(MatchingInfo(dR,i,j));
636     }
637    
638     }
639     }
640    
641     cout<<"TABLICA;"<< " pdgid="<<pdgid <<endl;
642     for (int i=0; i<matching_Infos.size();i++)
643     {
644     cout <<"dr="<<matching_Infos[i].deltaR << " i="<<matching_Infos[i].genMuon <<" j="<<matching_Infos[i].recoMuon <<endl;
645     }
646    
647     sort(matching_Infos.begin(),matching_Infos.end(),betterMatch); // sorting (dR,i,j)
648    
649     cout<<"TABLICA sortirana prije izbacivanja;" <<endl;
650    
651     for (int i=0; i<matching_Infos.size();i++)
652     {
653     cout <<"dr="<<matching_Infos[i].deltaR << " i="<<matching_Infos[i].genMuon <<" j="<<matching_Infos[i].recoMuon <<endl;
654     }
655    
656     cout<<"MC.size()="<<genparticles.size() <<" reco->size()="<<cands->size() <<endl;
657     if (genparticles.size()!=0 && cands->size()!=0){
658     // Now exclude combinations using same gen or rec muons
659     for (std::vector<MatchingInfo>::iterator it=matching_Infos.begin();
660     it != matching_Infos.end(); it++) {
661     for (std::vector<MatchingInfo>::iterator it2=matching_Infos.begin();
662     it2!=it; it2++) {
663     if ( (it->genMuon == it2->genMuon) || (it->recoMuon == it2->recoMuon) ) {
664     matching_Infos.erase(it);
665     it=matching_Infos.begin();
666     break;
667     }
668     }
669     }
670     }
671     cout << "sortirani vektor"<<endl;
672     for (int i=0; i<matching_Infos.size();i++)
673     {
674     cout <<"dr="<<matching_Infos[i].deltaR << " i="<<matching_Infos[i].genMuon <<" j="<<matching_Infos[i].recoMuon <<endl;
675     }
676     }
677    
678 senka 1.9
679 senka 1.13 // get mother of particle
680     // returns mother = 1 if mother is Z boson
681     // returns mother = 2 if mother is W boson
682     // returns mother = 4 if mother is b
683     // returns mother = 0 else
684    
685     void WZAnalyzer::getMother(const reco::Candidate* genParticle){
686     mother=0;
687     pdg_id=genParticle->pdgId();
688    
689     while (genParticle = genParticle->mother() ) { //izvrsava se sve dok ne dodje do nule tj. dok ne trazi majku od protona
690     cout << "Going up: ";
691     cout << "Mother id : " << genParticle->pdgId() << endl;
692     if (abs(genParticle->pdgId())!=pdg_id){
693     cout<< " good mother " <<endl;
694     if (abs(genParticle->pdgId())==23){ // mother is Z
695     mother=1;
696     }
697     if (abs(genParticle->pdgId())==24) { // mother is W
698     mother=2;
699     }
700     // if (abs(genParticle->pdgId())==23 || abs(genParticle->pdgId())==24) { // mother is W or Z
701     // WZ_matched=1;
702     // mother=3;
703     // }
704     if ((((abs(genParticle->pdgId()))/100)%10 ==5)||(((abs(genParticle->pdgId()))/1000)%10 ==5 )) { // mother is b
705     mother=4;
706     }
707     break;
708     }
709    
710     }
711    
712     }
713    
714 vuko 1.11
715 vuko 1.10 void WZAnalyzer::collectMCsummary(Handle <reco::CandidateCollection> mccands) {
716     //(Handle <reco::GenParticleCandidateCollection> mccands) {
717     //
718     //void WZAnalyzer::collectMCsummary(Handle <reco::GenParticleCandidateCollection> mccands) {
719 senka 1.12
720 vuko 1.11 using namespace reco;
721    
722 smorovic 1.8 //collections
723 senka 1.12 cout<<" pocetak WZAnalyzer::collectMCsummary" <<endl;
724 smorovic 1.8 MCleptZ1_pdgid = -1;
725     MCleptZ2_pdgid = -1;
726     MCleptW_pdgid = -1;
727    
728     MCleptZ1_pt = -1;
729     MCleptZ2_pt = -1;
730     MCleptW_pt = -1;
731    
732     MCleptZ1_eta = -1;
733     MCleptZ2_eta = -1;
734     MCleptW_eta = -1;
735    
736     MCleptZ1_phi = -1;
737     MCleptZ2_phi = -1;
738     MCleptW_phi = -1;
739 senka 1.12
740 smorovic 1.8 vector<reco::GenParticleCandidate*> Tau;
741     vector<reco::GenParticleCandidate*> StableMuons;
742     vector<reco::GenParticleCandidate*> StableElectrons;
743    
744 vuko 1.11 // reco::Candidate::iterator p;
745     // for (p = ((reco::Candidate*)&mccands)->begin(); p != ((reco::Candidate*)&mccands)->end(); ++p ) {
746     for(CandidateCollection::const_iterator p = mccands->begin();
747     p != mccands->end(); p++) {
748 vuko 1.1
749 vuko 1.11 // reco::Candidate* p_tmp = &(*p);
750     reco::GenParticleCandidate* ptr = (reco::GenParticleCandidate*) &(*p);
751 smorovic 1.8
752     if ( (ptr)->status() == 1
753     /*&& (ptr)->momentum().perp() > 2.*/ ) { // stable particles with pt>2 only
754     if ( abs((ptr)->pdgId()) == 11 ) {
755 senka 1.12
756 smorovic 1.8 StableElectrons.push_back((ptr));
757     //cout << "electron MCT\n";
758     }
759     else if ( abs((ptr)->pdgId()) == 13 ) {
760 senka 1.12
761 smorovic 1.8 StableMuons.push_back((ptr)) ;
762     //cout << "muon MCT\n";
763     }
764     }
765     else if ((ptr)->status() == 2) {
766     if ( abs((ptr)->pdgId()) == 15 ) {//tau
767     Tau.push_back((ptr));
768     }
769     }
770     }
771 senka 1.12
772 vuko 1.11 std::cout << "# Electrons : " << StableElectrons.size()
773 senka 1.12 << "# Muons : " << StableMuons.size() << std::endl
774     << "# Tau : " << Tau.size() << std::endl;
775    
776 smorovic 1.8
777     vector<reco::GenParticleCandidate*> * StableLeptons[3] = {&StableElectrons, &StableMuons, &Tau};
778    
779     bool firstZlept = true;
780     MC_tauDecayTypeZ1 = 0;
781     MC_tauDecayTypeZ2 = 0;
782     MC_tauDecayTypeW = 0;
783    
784    
785     for (int i=2; i>=0; i--) {
786     while (StableLeptons[i]->size() > 0) {
787     float maxPt = 0;
788     vector<reco::GenParticleCandidate*>::iterator index = StableLeptons[i]->end();
789    
790     for ( vector<reco::GenParticleCandidate*>::iterator lepton = StableLeptons[i]->begin();
791     lepton != StableLeptons[i]->end(); lepton++ ) {
792    
793     if ((*lepton)->pt() > maxPt) {
794     maxPt = (*lepton)->pt();
795     index = lepton;
796     }
797     }
798     int parentItr=0;
799     bool Zchild = false;
800     bool Wchild = false;
801     bool Tauchild = false;
802    
803     reco::GenParticleCandidate* mcParticleRef = *index;
804    
805     //find W/Z mother
806     while ( mcParticleRef->mother()!=0) {
807    
808     if (parentItr>=2) break;
809     parentItr++;
810     mcParticleRef = (reco::GenParticleCandidate*) mcParticleRef->mother();
811     if (mcParticleRef->mother()==0) break;
812    
813     //if tau
814     if (abs((mcParticleRef)->pdgId())==15 ) {
815     Tauchild=true;
816     break;
817     }
818    
819     // cout << "mother: " << (mcParticleRef)->pdg_id()
820     // <<" pt="<< c_p4.pt() << " eta="<<c_p4.eta()
821     // <<" status=" <<mcParticleRef->status() <<"\n";
822    
823     if (abs((mcParticleRef)->pdgId())==23 ) {
824     Zchild=true;
825     break;
826     }
827     if (abs((mcParticleRef)->pdgId())==24 ) {
828     Wchild=true;
829     break;
830     }
831     }
832    
833     if (maxPt >0) {
834     if (Tauchild) {
835     parentItr = 0;
836     while (mcParticleRef->mother()!=0) {
837     if (parentItr>=2) break;
838     parentItr++;
839     mcParticleRef = (reco::GenParticleCandidate*) mcParticleRef->mother();
840     if (mcParticleRef->mother()==0) break;
841    
842     if (abs((mcParticleRef)->pdgId())==23 ) {
843     if (MCleptZ1_pt == float(mcParticleRef->pt())
844     && MCleptZ1_eta == float(mcParticleRef->eta())
845     && MCleptZ1_phi == float(mcParticleRef->phi()) )
846     MC_tauDecayTypeZ1 = i;
847     else MC_tauDecayTypeZ2 = i;
848     break;
849     }
850     if (abs((mcParticleRef)->pdgId())==24 ) {
851     MC_tauDecayTypeW = i;
852     break;
853     }
854     }
855     }
856    
857     if (Wchild) {
858     MCleptW_pdgid=(*index)->pdgId();
859     MCleptW_pt=(float)(*index)->pt();
860     MCleptW_eta=(float)(*index)->eta();
861     MCleptW_phi=(float)(*index)->phi();
862     if (abs(MCleptW_pdgid)==15) MC_tauDecayTypeW =3;//default to hadronic decay
863     }
864     if (Zchild) {
865     if (firstZlept) {
866     firstZlept=false;
867     MCleptZ1_pdgid=(*index)->pdgId();
868     MCleptZ1_pt=(float)(*index)->pt();
869     MCleptZ1_eta=(float)(*index)->eta();
870     MCleptZ1_phi=(float)(*index)->phi();
871     if (abs(MCleptZ1_pdgid)==15) MC_tauDecayTypeZ1 =3;
872     }
873     else {
874     MCleptZ2_pdgid=(*index)->pdgId();
875     MCleptZ2_pt=(float)(*index)->pt();
876     MCleptZ2_eta=(float)(*index)->eta();
877     MCleptZ2_phi=(float)(*index)->phi();
878     if (abs(MCleptZ2_pdgid)==15) MC_tauDecayTypeZ2 =3;
879     }
880     }
881     StableLeptons[i]->erase(index);
882     }
883     }
884     }
885 senka 1.12 std::cout << "# Electrons end: " << StableElectrons.size()
886     << "# Muons end: " << StableMuons.size() << std::endl
887     << "# Tau end: " << Tau.size() << std::endl;
888 vuko 1.1
889 smorovic 1.8 }
890 vuko 1.1 //define this as a plug-in
891 vuko 1.2 // DEFINE_FWK_MODULE(WZAnalyzer);