ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/Vuko/WZAnalysis/src/WZAnalyzer.cc
Revision: 1.12
Committed: Tue Nov 27 18:27:51 2007 UTC (17 years, 5 months ago) by senka
Content type: text/plain
Branch: MAIN
Changes since 1.11: +140 -10 lines
Log Message:
lepton matching

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.12 // $Id: WZAnalyzer.cc,v 1.11 2007/11/27 15:54:13 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     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     cout<<"prije tree->Fill.. dphi()= "<<dPhi_WlZ_reco<<"\n" <<endl;
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    
629     double d_phi=fabs((*cands)[j].phi()-(genparticles)[i]->phi());
630     // cout<<"d_phi="<< d_phi <<endl;
631     double dR_byhand= sqrt(d_eta2*d_eta2+d_phi*d_phi);
632    
633     cout << "dr by hand = " << dR_byhand <<" i="<< i<<" j="<<j <<endl;
634     double dR=dR_byhand;
635    
636     if (dR<0.15){
637     n1++;
638     matching_Infos.push_back(MatchingInfo(dR,i,j));
639     }
640    
641     }
642     }
643    
644     cout<<"TABLICA;"<< " pdgid="<<pdgid <<endl;
645     for (int i=0; i<matching_Infos.size();i++)
646     {
647     cout <<"dr="<<matching_Infos[i].deltaR << " i="<<matching_Infos[i].genMuon <<" j="<<matching_Infos[i].recoMuon <<endl;
648     }
649    
650     sort(matching_Infos.begin(),matching_Infos.end(),betterMatch); // sorting (dR,i,j)
651    
652     cout<<"TABLICA sortirana prije izbacivanja;" <<endl;
653    
654     for (int i=0; i<matching_Infos.size();i++)
655     {
656     cout <<"dr="<<matching_Infos[i].deltaR << " i="<<matching_Infos[i].genMuon <<" j="<<matching_Infos[i].recoMuon <<endl;
657     }
658    
659     cout<<"MC.size()="<<genparticles.size() <<" reco->size()="<<cands->size() <<endl;
660     if (genparticles.size()!=0 && cands->size()!=0){
661     // Now exclude combinations using same gen or rec muons
662     for (std::vector<MatchingInfo>::iterator it=matching_Infos.begin();
663     it != matching_Infos.end(); it++) {
664     for (std::vector<MatchingInfo>::iterator it2=matching_Infos.begin();
665     it2!=it; it2++) {
666     if ( (it->genMuon == it2->genMuon) || (it->recoMuon == it2->recoMuon) ) {
667     matching_Infos.erase(it);
668     it=matching_Infos.begin();
669     break;
670     }
671     }
672     }
673     }
674     cout << "sortirani vektor"<<endl;
675     for (int i=0; i<matching_Infos.size();i++)
676     {
677     cout <<"dr="<<matching_Infos[i].deltaR << " i="<<matching_Infos[i].genMuon <<" j="<<matching_Infos[i].recoMuon <<endl;
678     }
679     }
680    
681 senka 1.9
682 vuko 1.11
683 vuko 1.10 void WZAnalyzer::collectMCsummary(Handle <reco::CandidateCollection> mccands) {
684     //(Handle <reco::GenParticleCandidateCollection> mccands) {
685     //
686     //void WZAnalyzer::collectMCsummary(Handle <reco::GenParticleCandidateCollection> mccands) {
687 senka 1.12
688 vuko 1.11 using namespace reco;
689    
690 smorovic 1.8 //collections
691 senka 1.12 cout<<" pocetak WZAnalyzer::collectMCsummary" <<endl;
692 smorovic 1.8 MCleptZ1_pdgid = -1;
693     MCleptZ2_pdgid = -1;
694     MCleptW_pdgid = -1;
695    
696     MCleptZ1_pt = -1;
697     MCleptZ2_pt = -1;
698     MCleptW_pt = -1;
699    
700     MCleptZ1_eta = -1;
701     MCleptZ2_eta = -1;
702     MCleptW_eta = -1;
703    
704     MCleptZ1_phi = -1;
705     MCleptZ2_phi = -1;
706     MCleptW_phi = -1;
707 senka 1.12
708 smorovic 1.8 vector<reco::GenParticleCandidate*> Tau;
709     vector<reco::GenParticleCandidate*> StableMuons;
710     vector<reco::GenParticleCandidate*> StableElectrons;
711    
712 vuko 1.11 // reco::Candidate::iterator p;
713     // for (p = ((reco::Candidate*)&mccands)->begin(); p != ((reco::Candidate*)&mccands)->end(); ++p ) {
714     for(CandidateCollection::const_iterator p = mccands->begin();
715     p != mccands->end(); p++) {
716 vuko 1.1
717 vuko 1.11 // reco::Candidate* p_tmp = &(*p);
718     reco::GenParticleCandidate* ptr = (reco::GenParticleCandidate*) &(*p);
719 smorovic 1.8
720     if ( (ptr)->status() == 1
721     /*&& (ptr)->momentum().perp() > 2.*/ ) { // stable particles with pt>2 only
722     if ( abs((ptr)->pdgId()) == 11 ) {
723 senka 1.12
724 smorovic 1.8 StableElectrons.push_back((ptr));
725     //cout << "electron MCT\n";
726     }
727     else if ( abs((ptr)->pdgId()) == 13 ) {
728 senka 1.12
729 smorovic 1.8 StableMuons.push_back((ptr)) ;
730     //cout << "muon MCT\n";
731     }
732     }
733     else if ((ptr)->status() == 2) {
734     if ( abs((ptr)->pdgId()) == 15 ) {//tau
735     Tau.push_back((ptr));
736     }
737     }
738     }
739 senka 1.12
740 vuko 1.11 std::cout << "# Electrons : " << StableElectrons.size()
741 senka 1.12 << "# Muons : " << StableMuons.size() << std::endl
742     << "# Tau : " << Tau.size() << std::endl;
743    
744 smorovic 1.8
745     vector<reco::GenParticleCandidate*> * StableLeptons[3] = {&StableElectrons, &StableMuons, &Tau};
746    
747     bool firstZlept = true;
748     MC_tauDecayTypeZ1 = 0;
749     MC_tauDecayTypeZ2 = 0;
750     MC_tauDecayTypeW = 0;
751    
752    
753     for (int i=2; i>=0; i--) {
754     while (StableLeptons[i]->size() > 0) {
755     float maxPt = 0;
756     vector<reco::GenParticleCandidate*>::iterator index = StableLeptons[i]->end();
757    
758     for ( vector<reco::GenParticleCandidate*>::iterator lepton = StableLeptons[i]->begin();
759     lepton != StableLeptons[i]->end(); lepton++ ) {
760    
761     if ((*lepton)->pt() > maxPt) {
762     maxPt = (*lepton)->pt();
763     index = lepton;
764     }
765     }
766     int parentItr=0;
767     bool Zchild = false;
768     bool Wchild = false;
769     bool Tauchild = false;
770    
771     reco::GenParticleCandidate* mcParticleRef = *index;
772    
773     //find W/Z mother
774     while ( mcParticleRef->mother()!=0) {
775    
776     if (parentItr>=2) break;
777     parentItr++;
778     mcParticleRef = (reco::GenParticleCandidate*) mcParticleRef->mother();
779     if (mcParticleRef->mother()==0) break;
780    
781     //if tau
782     if (abs((mcParticleRef)->pdgId())==15 ) {
783     Tauchild=true;
784     break;
785     }
786    
787     // cout << "mother: " << (mcParticleRef)->pdg_id()
788     // <<" pt="<< c_p4.pt() << " eta="<<c_p4.eta()
789     // <<" status=" <<mcParticleRef->status() <<"\n";
790    
791     if (abs((mcParticleRef)->pdgId())==23 ) {
792     Zchild=true;
793     break;
794     }
795     if (abs((mcParticleRef)->pdgId())==24 ) {
796     Wchild=true;
797     break;
798     }
799     }
800    
801     if (maxPt >0) {
802     if (Tauchild) {
803     parentItr = 0;
804     while (mcParticleRef->mother()!=0) {
805     if (parentItr>=2) break;
806     parentItr++;
807     mcParticleRef = (reco::GenParticleCandidate*) mcParticleRef->mother();
808     if (mcParticleRef->mother()==0) break;
809    
810     if (abs((mcParticleRef)->pdgId())==23 ) {
811     if (MCleptZ1_pt == float(mcParticleRef->pt())
812     && MCleptZ1_eta == float(mcParticleRef->eta())
813     && MCleptZ1_phi == float(mcParticleRef->phi()) )
814     MC_tauDecayTypeZ1 = i;
815     else MC_tauDecayTypeZ2 = i;
816     break;
817     }
818     if (abs((mcParticleRef)->pdgId())==24 ) {
819     MC_tauDecayTypeW = i;
820     break;
821     }
822     }
823     }
824    
825     if (Wchild) {
826     MCleptW_pdgid=(*index)->pdgId();
827     MCleptW_pt=(float)(*index)->pt();
828     MCleptW_eta=(float)(*index)->eta();
829     MCleptW_phi=(float)(*index)->phi();
830     if (abs(MCleptW_pdgid)==15) MC_tauDecayTypeW =3;//default to hadronic decay
831     }
832     if (Zchild) {
833     if (firstZlept) {
834     firstZlept=false;
835     MCleptZ1_pdgid=(*index)->pdgId();
836     MCleptZ1_pt=(float)(*index)->pt();
837     MCleptZ1_eta=(float)(*index)->eta();
838     MCleptZ1_phi=(float)(*index)->phi();
839     if (abs(MCleptZ1_pdgid)==15) MC_tauDecayTypeZ1 =3;
840     }
841     else {
842     MCleptZ2_pdgid=(*index)->pdgId();
843     MCleptZ2_pt=(float)(*index)->pt();
844     MCleptZ2_eta=(float)(*index)->eta();
845     MCleptZ2_phi=(float)(*index)->phi();
846     if (abs(MCleptZ2_pdgid)==15) MC_tauDecayTypeZ2 =3;
847     }
848     }
849     StableLeptons[i]->erase(index);
850     }
851     }
852     }
853 senka 1.12 std::cout << "# Electrons end: " << StableElectrons.size()
854     << "# Muons end: " << StableMuons.size() << std::endl
855     << "# Tau end: " << Tau.size() << std::endl;
856 vuko 1.1
857 smorovic 1.8 }
858 vuko 1.1 //define this as a plug-in
859 vuko 1.2 // DEFINE_FWK_MODULE(WZAnalyzer);