ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/Vuko/WZAnalysis/src/WZAnalyzer.cc
Revision: 1.17
Committed: Fri Nov 30 12:35:25 2007 UTC (17 years, 5 months ago) by senka
Content type: text/plain
Branch: MAIN
Changes since 1.16: +21 -22 lines
Log Message:
 dr & matchedWZ & matchedB debuged

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