ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/Vuko/WZAnalysis/src/WZAnalyzer.cc
Revision: 1.18
Committed: Fri Nov 30 17:24:50 2007 UTC (17 years, 5 months ago) by vuko
Content type: text/plain
Branch: MAIN
CVS Tags: vuko_3dec07
Changes since 1.17: +40 -7 lines
Log Message:
modifying MC truth information

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