ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/Vuko/WZAnalysis/src/WZAnalyzer.cc
Revision: 1.15
Committed: Wed Nov 28 17:17:36 2007 UTC (17 years, 5 months ago) by senka
Content type: text/plain
Branch: MAIN
Changes since 1.14: +35 -7 lines
Log Message:
ganParticle matched to recoParticle as argument in FillProperties

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.15 // $Id: WZAnalyzer.cc,v 1.14 2007/11/28 14:58:15 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 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     theFile = new TFile( "wz.root", "RECREATE" ) ;
491    
492     wzTree = new TTree("WZTree","WZ informations");
493    
494     elWTree = new TTree("WElTree","W electron informations");
495    
496 vuko 1.3 leptonProperties["Wel"] = new ElectronProperties();
497     leptonProperties["ZEl1"] = new ElectronProperties();
498     leptonProperties["ZEl2"] = new ElectronProperties();
499    
500     leptonProperties["Wmu"] = new MuonProperties();
501     leptonProperties["Zmu1"] = new MuonProperties();
502     leptonProperties["Zmu2"] = new MuonProperties();
503 vuko 1.1
504 vuko 1.3 leptonProperties["Wel"]->CreateBranches(wzTree, "WEl_");
505     leptonProperties["Wel"]->CreateBranches(elWTree,"ElW_");
506 vuko 1.4 leptonProperties["ZEl1"]->CreateBranches(wzTree, "ZEl1_");
507     leptonProperties["ZEl2"]->CreateBranches(wzTree, "ZEl2_");
508 vuko 1.1
509 senka 1.9 leptonProperties["Wmu"] ->CreateBranches(wzTree, "Wmu_");
510     leptonProperties["Zmu1"] ->CreateBranches(wzTree, "Zmul_");
511     leptonProperties["Zmu2"] ->CreateBranches(wzTree, "Zmu2_");
512    
513 vuko 1.1 initialiseTree();
514 smorovic 1.6
515     //prepare HLT TriggerResults branch
516     if (storeHLTresults) {
517     wzTree->Branch("WZ_hltBitmask",&triggerBitmask,"WZ_hltBitmask/I");
518    
519     }
520 senka 1.9
521     // MET branch
522     wzTree->Branch("MET_energy",&MET_energy,"MET_energy/F");
523     wzTree->Branch("MET_pt",&MET_pt,"MET_pt/F");
524     wzTree->Branch("MET_eta",&MET_eta,"MET_eta/F");
525     wzTree->Branch("dPhi_WlZ_reco",&dPhi_WlZ_reco,"dPhi_WlZ_reco/F");
526     wzTree->Branch("dPhi_WZ_reco",&dPhi_WZ_reco,"dPhi_WZ_reco/F");
527    
528 vuko 1.1 }
529    
530     // ------------ method called once each job just after ending the event loop ------------
531     void
532     WZAnalyzer::endJob() {
533    
534     theFile->Write();
535     theFile->Close();
536    
537     }
538    
539    
540     void WZAnalyzer::initialiseTree() {
541    
542     // Z properties
543     wzTree->Branch("Zmass", &zMass, "Zmass/F");
544     wzTree->Branch("ZId", &zFlavour, "Zid/I");
545     wzTree->Branch("ZPt", &zPt, "ZPt/F");
546     wzTree->Branch("ZEta", &zEta, "ZEta/F");
547     wzTree->Branch("ZPhi", &zPhi, "ZPhi/F");
548    
549     // W Properties
550     wzTree->Branch("WlId", &wlFlavour, "Wlid/I");
551     wzTree->Branch("WlCharge", &wlCharge, "WlCharge/I");
552     wzTree->Branch("WlPt", &wlPt, "WlPt/F");
553 smorovic 1.8
554     wzTree->Branch("MC_leptonZ1_pt", &MCleptZ1_pt,"MC_leptonZ1_pt/F");
555     wzTree->Branch("MC_leptonZ2_pt", &MCleptZ2_pt,"MC_leptonZ2_pt/F");
556     wzTree->Branch("MC_leptonW_pt", &MCleptW_pt, "MC_leptonW_pt/F");
557    
558     wzTree->Branch("MC_leptonZ1_eta", &MCleptZ1_eta,"MC_leptonZ1_eta/F");
559     wzTree->Branch("MC_leptonZ2_eta", &MCleptZ2_eta,"MC_leptonZ2_eta/F");
560     wzTree->Branch("MC_leptonW_eta", &MCleptW_eta, "MC_leptonW_eta/F");
561    
562     wzTree->Branch("MC_leptonZ1_phi", &MCleptZ1_phi,"MC_leptonZ1_phi/F");
563     wzTree->Branch("MC_leptonZ2_phi", &MCleptZ2_phi,"MC_leptonZ2_phi/F");
564     wzTree->Branch("MC_leptonW_phi", &MCleptW_phi, "MC_leptonW_phi/F");
565    
566     wzTree->Branch("MC_leptonZ1_pdgid", &MCleptZ1_pdgid,"MC_leptonZ1_pdgid/I");
567     wzTree->Branch("MC_leptonZ2_pdgid", &MCleptZ2_pdgid,"MC_leptonZ2_pdgid/I");
568     wzTree->Branch("MC_leptonW_pdgid", &MCleptW_pdgid, "MC_leptonW_pdgid/I");
569 vuko 1.14
570     /*
571     wzTree->Branch("MC_leptonW_origin", &MCleptW_pdgid, "MC_leptonW_origin/I");
572     wzTree->Branch("MC_leptonZ1_origin", &MCleptW_pdgid, "MC_leptonZ1_origin/I");
573     wzTree->Branch("MC_leptonZ2_origin", &MCleptW_pdgid, "MC_leptonZ2_origin/I");
574     */
575    
576 smorovic 1.8 wzTree->Branch("MC_TauDecayType_fromZ1", &MC_tauDecayTypeZ1,"MC_TauDecayType_fromZ1/I");
577     wzTree->Branch("MC_TauDecayType_fromZ2", &MC_tauDecayTypeZ2,"MC_TauDecayType_fromZ2/I");
578     wzTree->Branch("MC_TauDecayType_fromW", &MC_tauDecayTypeW, "MC_TauDecayType_fromW/I");
579 vuko 1.10
580     // MET properties
581    
582     // Jet properties
583     wzTree->Branch("Jet1Et", &jet1Et, "Jet1Et/F");
584     wzTree->Branch("Jet1Phi", &jet1Phi, "Jet1Phi/F");
585     wzTree->Branch("Jet1Eta", &jet1Eta, "Jet1Eta/F");
586    
587     wzTree->Branch("Jet2Et", &jet2Et, "Jet2Et/F");
588     wzTree->Branch("Jet2Phi", &jet2Phi, "Jet2Phi/F");
589     wzTree->Branch("Jet2Eta", &jet2Eta, "Jet2Eta/F");
590    
591    
592 smorovic 1.8 }
593 vuko 1.1
594 senka 1.9
595     reco::Particle::LorentzVector WZAnalyzer::computeMET(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
596    
597     using namespace edm;
598     using namespace reco;
599     using namespace std;
600    
601     Handle<CaloMETCollection> mets;
602     iEvent.getByLabel("met", mets);
603 vuko 1.14 cout << "# METs=" << mets->size() << endl;
604     for (CaloMETCollection::const_iterator met = mets->begin();
605     met!= mets->end(); met++) {
606     cout << "MET: E = " << met->energy()
607     << "\t Pt = " << met->pt()
608     <<endl;
609     }
610 senka 1.9 return mets->begin()->p4();
611    
612     }
613    
614 senka 1.12 ////////////////////////
615     // matching
616     //
617    
618     class MatchingInfo {
619     public:
620    
621     MatchingInfo(float dr, int gen, int reco) : deltaR(dr), genMuon(gen),recoMuon(reco) {}
622    
623     bool operator<(const MatchingInfo m) { return deltaR < m.deltaR;}
624    
625     float deltaR;
626     int genMuon;
627     int recoMuon;
628    
629     };
630    
631    
632     bool betterMatch(MatchingInfo m1, MatchingInfo m2)
633     { return m1.deltaR < m2.deltaR;}
634    
635    
636     // method that gives the vector<MatchingInfo> matching_Infos containing (dR,i,j); i=genParticle index, j=ParticleCollection index:
637    
638     void WZAnalyzer::matching(const edm::Event& iEvent, Handle<reco::CandidateCollection> cands, int pdgid){
639    
640     using namespace edm;
641     using namespace reco;
642     using namespace std;
643    
644     //-------------------------
645     Handle<CandidateCollection> mccands;
646     iEvent.getByLabel( theMCTruthCollection,mccands );
647    
648     vector <GenParticleCandidate*> genparticles;
649    
650     for(CandidateCollection::const_iterator p = mccands->begin();
651     p != mccands->end(); p++) {
652    
653     // reco::Candidate* p_tmp = &(*p);
654     reco::GenParticleCandidate* ptr = (reco::GenParticleCandidate*) &(*p);
655    
656     if ( (ptr)->status() == 1
657     /*&& (ptr)->momentum().perp() > 2.*/ ) { // stable particles with pt>2 only
658     if ( abs((ptr)->pdgId()) == pdgid ) {
659     genparticles.push_back((ptr));
660     //cout << "electron MCT\n";
661     }
662     }
663     }
664    
665    
666     int n1=0;
667     vector<MatchingInfo> matching_Infos;
668     for ( unsigned int i=0; i<genparticles.size(); i++ ) // po svim MC muonima
669     {
670    
671     for (unsigned int j = 0; j < cands->size(); j++)// po svim reco muonima
672     {
673    
674     double d_eta2=(*cands)[j].eta()-(genparticles)[i]->eta();
675     double d_phi=fabs((*cands)[j].phi()-(genparticles)[i]->phi());
676     double dR_byhand= sqrt(d_eta2*d_eta2+d_phi*d_phi);
677     cout << "dr by hand = " << dR_byhand <<" i="<< i<<" j="<<j <<endl;
678     double dR=dR_byhand;
679    
680     if (dR<0.15){
681     n1++;
682     matching_Infos.push_back(MatchingInfo(dR,i,j));
683     }
684    
685     }
686     }
687    
688     cout<<"TABLICA;"<< " pdgid="<<pdgid <<endl;
689     for (int i=0; i<matching_Infos.size();i++)
690     {
691     cout <<"dr="<<matching_Infos[i].deltaR << " i="<<matching_Infos[i].genMuon <<" j="<<matching_Infos[i].recoMuon <<endl;
692     }
693    
694     sort(matching_Infos.begin(),matching_Infos.end(),betterMatch); // sorting (dR,i,j)
695    
696     cout<<"TABLICA sortirana prije izbacivanja;" <<endl;
697    
698     for (int i=0; i<matching_Infos.size();i++)
699     {
700     cout <<"dr="<<matching_Infos[i].deltaR << " i="<<matching_Infos[i].genMuon <<" j="<<matching_Infos[i].recoMuon <<endl;
701     }
702    
703     cout<<"MC.size()="<<genparticles.size() <<" reco->size()="<<cands->size() <<endl;
704     if (genparticles.size()!=0 && cands->size()!=0){
705     // Now exclude combinations using same gen or rec muons
706     for (std::vector<MatchingInfo>::iterator it=matching_Infos.begin();
707     it != matching_Infos.end(); it++) {
708     for (std::vector<MatchingInfo>::iterator it2=matching_Infos.begin();
709     it2!=it; it2++) {
710     if ( (it->genMuon == it2->genMuon) || (it->recoMuon == it2->recoMuon) ) {
711     matching_Infos.erase(it);
712     it=matching_Infos.begin();
713     break;
714     }
715     }
716     }
717     }
718     cout << "sortirani vektor"<<endl;
719     for (int i=0; i<matching_Infos.size();i++)
720     {
721     cout <<"dr="<<matching_Infos[i].deltaR << " i="<<matching_Infos[i].genMuon <<" j="<<matching_Infos[i].recoMuon <<endl;
722     }
723 vuko 1.14
724     // Now put result in vector of pairs
725    
726    
727     // vector< pair<const GenParticleCandidate*,
728     // const reco::Candidate *> > * leptonMatching = 0;
729    
730     // if (pdgid == 11) {
731     // leptonMatching = &electronMatching;
732     // } else if (pdgid == 13) {
733     // leptonMatching = &muonMatching;
734     // }
735    
736     // if (leptonMatching == 0) return;
737     // leptonMatching->clear();
738    
739     leptonMatching[pdgid].clear();
740    
741     for (vector<MatchingInfo>::iterator match=matching_Infos.begin();
742     match !=matching_Infos.end(); match++) {
743    
744     pair<const GenParticleCandidate*, const reco::Candidate *> pair;
745     pair.first = genparticles[match->genMuon];
746     pair.second = & (*cands)[match->recoMuon];
747     leptonMatching[pdgid].push_back(pair);
748     }
749    
750    
751 senka 1.12 }
752    
753 senka 1.9
754 senka 1.13 // get mother of particle
755     // returns mother = 1 if mother is Z boson
756     // returns mother = 2 if mother is W boson
757     // returns mother = 4 if mother is b
758     // returns mother = 0 else
759    
760 vuko 1.14 WZAnalyzer::LeptonOrigin WZAnalyzer::getMother(const reco::Candidate* genParticle){
761    
762     int pdg_id=genParticle->pdgId();
763 senka 1.13
764     while (genParticle = genParticle->mother() ) { //izvrsava se sve dok ne dodje do nule tj. dok ne trazi majku od protona
765     cout << "Going up: ";
766     cout << "Mother id : " << genParticle->pdgId() << endl;
767 vuko 1.14 if (abs(genParticle->pdgId())!=pdg_id) {
768 senka 1.13 cout<< " good mother " <<endl;
769     if (abs(genParticle->pdgId())==23){ // mother is Z
770 vuko 1.14 return zboson;
771 senka 1.13 }
772     if (abs(genParticle->pdgId())==24) { // mother is W
773 vuko 1.14 return wboson;
774 senka 1.13 }
775     // if (abs(genParticle->pdgId())==23 || abs(genParticle->pdgId())==24) { // mother is W or Z
776     // WZ_matched=1;
777     // mother=3;
778     // }
779     if ((((abs(genParticle->pdgId()))/100)%10 ==5)||(((abs(genParticle->pdgId()))/1000)%10 ==5 )) { // mother is b
780 vuko 1.14 return bdecay;
781 senka 1.13 }
782 vuko 1.14 if ((((abs(genParticle->pdgId()))/100)%10 ==4)||(((abs(genParticle->pdgId()))/1000)%10 ==4 )) { // mother is c
783     return cdecay;
784     }
785     return other;
786 senka 1.13 }
787     }
788     }
789    
790 senka 1.15 const reco::Candidate * WZAnalyzer::MatchedGenParticle(const reco::Candidate * daughter){
791     ::OverlapChecker overlap;
792     matched_genParticle=0;
793     for (map< int,
794     std::vector< pair<const reco::GenParticleCandidate*, const reco::Candidate *> > > ::iterator iter = leptonMatching.begin();
795     iter!=leptonMatching.end(); iter++) // entire map
796     {
797     for (int j=0; j<iter->second.size(); j++){ // entire vector<pair<>>
798     const reco::Candidate * nesto=iter->second[j].second;
799     if (overlap(*nesto,*daughter)){
800     cout <<" found matched genparticle" <<endl;
801     return matched_genParticle=iter->second[j].first;
802     }
803     }
804     }
805     }
806    
807 vuko 1.11
808 vuko 1.10 void WZAnalyzer::collectMCsummary(Handle <reco::CandidateCollection> mccands) {
809     //(Handle <reco::GenParticleCandidateCollection> mccands) {
810     //
811     //void WZAnalyzer::collectMCsummary(Handle <reco::GenParticleCandidateCollection> mccands) {
812 senka 1.12
813 vuko 1.11 using namespace reco;
814    
815 smorovic 1.8 //collections
816 senka 1.12 cout<<" pocetak WZAnalyzer::collectMCsummary" <<endl;
817 smorovic 1.8 MCleptZ1_pdgid = -1;
818     MCleptZ2_pdgid = -1;
819     MCleptW_pdgid = -1;
820    
821     MCleptZ1_pt = -1;
822     MCleptZ2_pt = -1;
823     MCleptW_pt = -1;
824    
825     MCleptZ1_eta = -1;
826     MCleptZ2_eta = -1;
827     MCleptW_eta = -1;
828    
829     MCleptZ1_phi = -1;
830     MCleptZ2_phi = -1;
831     MCleptW_phi = -1;
832 senka 1.12
833 smorovic 1.8 vector<reco::GenParticleCandidate*> Tau;
834     vector<reco::GenParticleCandidate*> StableMuons;
835     vector<reco::GenParticleCandidate*> StableElectrons;
836    
837 vuko 1.11 // reco::Candidate::iterator p;
838     // for (p = ((reco::Candidate*)&mccands)->begin(); p != ((reco::Candidate*)&mccands)->end(); ++p ) {
839     for(CandidateCollection::const_iterator p = mccands->begin();
840     p != mccands->end(); p++) {
841 vuko 1.1
842 vuko 1.11 // reco::Candidate* p_tmp = &(*p);
843     reco::GenParticleCandidate* ptr = (reco::GenParticleCandidate*) &(*p);
844 smorovic 1.8
845     if ( (ptr)->status() == 1
846     /*&& (ptr)->momentum().perp() > 2.*/ ) { // stable particles with pt>2 only
847     if ( abs((ptr)->pdgId()) == 11 ) {
848 senka 1.12
849 smorovic 1.8 StableElectrons.push_back((ptr));
850     //cout << "electron MCT\n";
851     }
852     else if ( abs((ptr)->pdgId()) == 13 ) {
853 senka 1.12
854 smorovic 1.8 StableMuons.push_back((ptr)) ;
855     //cout << "muon MCT\n";
856     }
857     }
858     else if ((ptr)->status() == 2) {
859     if ( abs((ptr)->pdgId()) == 15 ) {//tau
860     Tau.push_back((ptr));
861     }
862     }
863     }
864 senka 1.12
865 vuko 1.11 std::cout << "# Electrons : " << StableElectrons.size()
866 senka 1.12 << "# Muons : " << StableMuons.size() << std::endl
867     << "# Tau : " << Tau.size() << std::endl;
868    
869 smorovic 1.8
870     vector<reco::GenParticleCandidate*> * StableLeptons[3] = {&StableElectrons, &StableMuons, &Tau};
871    
872     bool firstZlept = true;
873     MC_tauDecayTypeZ1 = 0;
874     MC_tauDecayTypeZ2 = 0;
875     MC_tauDecayTypeW = 0;
876    
877    
878     for (int i=2; i>=0; i--) {
879     while (StableLeptons[i]->size() > 0) {
880     float maxPt = 0;
881     vector<reco::GenParticleCandidate*>::iterator index = StableLeptons[i]->end();
882    
883     for ( vector<reco::GenParticleCandidate*>::iterator lepton = StableLeptons[i]->begin();
884     lepton != StableLeptons[i]->end(); lepton++ ) {
885    
886     if ((*lepton)->pt() > maxPt) {
887     maxPt = (*lepton)->pt();
888     index = lepton;
889     }
890     }
891     int parentItr=0;
892     bool Zchild = false;
893     bool Wchild = false;
894     bool Tauchild = false;
895    
896     reco::GenParticleCandidate* mcParticleRef = *index;
897    
898     //find W/Z mother
899     while ( mcParticleRef->mother()!=0) {
900    
901     if (parentItr>=2) break;
902     parentItr++;
903     mcParticleRef = (reco::GenParticleCandidate*) mcParticleRef->mother();
904     if (mcParticleRef->mother()==0) break;
905    
906     //if tau
907     if (abs((mcParticleRef)->pdgId())==15 ) {
908     Tauchild=true;
909     break;
910     }
911    
912     // cout << "mother: " << (mcParticleRef)->pdg_id()
913     // <<" pt="<< c_p4.pt() << " eta="<<c_p4.eta()
914     // <<" status=" <<mcParticleRef->status() <<"\n";
915    
916     if (abs((mcParticleRef)->pdgId())==23 ) {
917     Zchild=true;
918     break;
919     }
920     if (abs((mcParticleRef)->pdgId())==24 ) {
921     Wchild=true;
922     break;
923     }
924     }
925    
926     if (maxPt >0) {
927     if (Tauchild) {
928     parentItr = 0;
929     while (mcParticleRef->mother()!=0) {
930     if (parentItr>=2) break;
931     parentItr++;
932     mcParticleRef = (reco::GenParticleCandidate*) mcParticleRef->mother();
933     if (mcParticleRef->mother()==0) break;
934    
935     if (abs((mcParticleRef)->pdgId())==23 ) {
936     if (MCleptZ1_pt == float(mcParticleRef->pt())
937     && MCleptZ1_eta == float(mcParticleRef->eta())
938     && MCleptZ1_phi == float(mcParticleRef->phi()) )
939     MC_tauDecayTypeZ1 = i;
940     else MC_tauDecayTypeZ2 = i;
941     break;
942     }
943     if (abs((mcParticleRef)->pdgId())==24 ) {
944     MC_tauDecayTypeW = i;
945     break;
946     }
947     }
948     }
949    
950     if (Wchild) {
951     MCleptW_pdgid=(*index)->pdgId();
952     MCleptW_pt=(float)(*index)->pt();
953     MCleptW_eta=(float)(*index)->eta();
954     MCleptW_phi=(float)(*index)->phi();
955     if (abs(MCleptW_pdgid)==15) MC_tauDecayTypeW =3;//default to hadronic decay
956     }
957     if (Zchild) {
958     if (firstZlept) {
959     firstZlept=false;
960     MCleptZ1_pdgid=(*index)->pdgId();
961     MCleptZ1_pt=(float)(*index)->pt();
962     MCleptZ1_eta=(float)(*index)->eta();
963     MCleptZ1_phi=(float)(*index)->phi();
964     if (abs(MCleptZ1_pdgid)==15) MC_tauDecayTypeZ1 =3;
965     }
966     else {
967     MCleptZ2_pdgid=(*index)->pdgId();
968     MCleptZ2_pt=(float)(*index)->pt();
969     MCleptZ2_eta=(float)(*index)->eta();
970     MCleptZ2_phi=(float)(*index)->phi();
971     if (abs(MCleptZ2_pdgid)==15) MC_tauDecayTypeZ2 =3;
972     }
973     }
974     StableLeptons[i]->erase(index);
975     }
976     }
977     }
978 senka 1.12 std::cout << "# Electrons end: " << StableElectrons.size()
979     << "# Muons end: " << StableMuons.size() << std::endl
980     << "# Tau end: " << Tau.size() << std::endl;
981 vuko 1.1
982 smorovic 1.8 }
983 vuko 1.1 //define this as a plug-in
984 vuko 1.2 // DEFINE_FWK_MODULE(WZAnalyzer);