ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/Vuko/WZAnalysis/src/WZAnalyzer.cc
Revision: 1.19
Committed: Tue Dec 4 12:38:22 2007 UTC (17 years, 5 months ago) by senka
Content type: text/plain
Branch: MAIN
Changes since 1.18: +16 -22 lines
Log Message:
segmentation fault solved

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