ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/Vuko/WZAnalysis/src/WZAnalyzer.cc
Revision: 1.16
Committed: Thu Nov 29 17:56:57 2007 UTC (17 years, 5 months ago) by senka
Content type: text/plain
Branch: MAIN
Changes since 1.15: +79 -10 lines
Log Message:
gen info to Electron and Muon Properties

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