ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/Vuko/WZAnalysis/src/WZAnalyzer.cc
Revision: 1.7
Committed: Fri Nov 23 18:01:55 2007 UTC (17 years, 5 months ago) by vuko
Content type: text/plain
Branch: MAIN
Changes since 1.6: +15 -57 lines
Log Message:
Using merged Z->ll collection

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.7 // $Id: WZAnalyzer.cc,v 1.6 2007/11/23 16:52:41 smorovic 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    
52     #include "TFile.h"
53     #include "TH1F.h"
54     #include "TH2F.h"
55     #include "TTree.h"
56    
57     #include <map>
58    
59     //
60     // constants, enums and typedefs
61     //
62    
63     //
64     // static data member definitions
65     //
66    
67     //
68     // constructors and destructor
69     //
70     WZAnalyzer::WZAnalyzer(const edm::ParameterSet& iConfig)
71    
72     {
73     //now do what ever initialization is needed
74    
75     theLooseMuonCollection = iConfig.getParameter<edm::InputTag>("LooseMuons");
76     theGoodMuonCollection = iConfig.getParameter<edm::InputTag>("GoodMuons");
77     theLooseElectronCollection = iConfig.getParameter<edm::InputTag>("LooseElectrons");
78     theGoodElectronCollection = iConfig.getParameter<edm::InputTag>("GoodElectrons");
79     theTightLeptonCollection = iConfig.getParameter<edm::InputTag>("TightLeptons");
80     // theMediumElectronCollection = iConfig.getParameter<edm::InputTag>("LooseElectrons");
81     // theTightElectronCollection = iConfig.getParameter<edm::InputTag>("TightElectrons");
82     // Z's
83     theZeeCollection = iConfig.getParameter<edm::InputTag>("ZtoEE");
84     theZmumuCollection = iConfig.getParameter<edm::InputTag>("ZtoMuMu");
85 vuko 1.3 theZllCollection = iConfig.getParameter<edm::InputTag>("ZtoLL");
86 smorovic 1.6
87     storeHLTresults=false;
88     storeHLTresults = iConfig.getParameter<bool>("storeHLTresults");
89 vuko 1.1
90 smorovic 1.6 firstTriggerResult = true;
91 vuko 1.1 }
92    
93    
94     WZAnalyzer::~WZAnalyzer()
95     {
96    
97     // do anything here that needs to be done at desctruction time
98     // (e.g. close files, deallocate resources etc.)
99    
100     }
101    
102    
103     //
104     // member functions
105     //
106    
107     // ------------ method called to for each event ------------
108     void
109     WZAnalyzer::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup)
110     {
111     using namespace edm;
112     using namespace reco;
113     using namespace std;
114    
115    
116     #ifdef THIS_IS_AN_EVENT_EXAMPLE
117     Handle<ExampleData> pIn;
118     iEvent.getByLabel("example",pIn);
119     #endif
120    
121     #ifdef THIS_IS_AN_EVENTSETUP_EXAMPLE
122     ESHandle<SetupData> pSetup;
123     iSetup.get<SetupRecord>().get(pSetup);
124     #endif
125    
126     const Candidate * theZCandidate = 0;
127     const Candidate * theWlepton = 0;
128    
129     // Z->mumu
130     Handle<CandidateCollection> zmumuCands;
131     iEvent.getByLabel( theZmumuCollection , zmumuCands);
132    
133    
134     // Z->ee
135     Handle<CandidateCollection> zeeCands;
136     iEvent.getByLabel( theZeeCollection , zeeCands);
137    
138 vuko 1.3 // Z->ee
139     Handle<CandidateCollection> zllCands;
140     iEvent.getByLabel( theZllCollection , zllCands);
141    
142 vuko 1.1 // loose electrons
143     Handle<CandidateCollection> looseElectronCands;
144     iEvent.getByLabel( theLooseElectronCollection , looseElectronCands);
145    
146     // good electrons
147     Handle<CandidateCollection> goodElectronCands;
148     iEvent.getByLabel( theGoodElectronCollection , goodElectronCands);
149    
150     // loose muons
151     Handle<CandidateCollection> looseMuonCands;
152     iEvent.getByLabel( theLooseMuonCollection , looseMuonCands);
153    
154     // good muons
155     Handle<CandidateCollection> goodMuonCands;
156     iEvent.getByLabel( theGoodMuonCollection , goodMuonCands);
157    
158    
159     // tight leptons
160     Handle<CandidateCollection> tightLeptonCands;
161     iEvent.getByLabel( theTightLeptonCollection , tightLeptonCands);
162    
163 smorovic 1.6
164     // get hold of TriggerResults
165     Handle<TriggerResults> HLTR;
166     if (storeHLTresults) {
167 vuko 1.7 iEvent.getByType(HLTR);
168     if (firstTriggerResult) {
169     firstTriggerResult = false;
170     triggerNames= HLTR->getTriggerNames();
171     numTriggers = triggerNames.size();
172     }
173 smorovic 1.6
174 vuko 1.7 }
175 vuko 1.1
176     // Reset a few things
177    
178 vuko 1.3 for (map<string,wzana::LeptonProperties* >::iterator iter = leptonProperties.begin();
179     iter!=leptonProperties.end(); iter++)
180 vuko 1.1 {
181     iter->second->ResetValues();
182     }
183    
184     // selected muons
185    
186    
187     //
188    
189     int nzee = zeeCands->size();
190     int nzmumu = zmumuCands->size();
191    
192     cout << "\t # loose mu : " << looseMuonCands->size()
193     << "\t # tight mu : " << goodMuonCands->size()
194     << "\t # loose e : " << looseElectronCands->size()
195     << "\t # tight e : " << goodElectronCands->size()
196     << "\t # tight l : " << tightLeptonCands->size()
197     << "\t # Z->mumu : " << zmumuCands->size()
198     << "\t # Z->ee : " << zeeCands->size()
199 vuko 1.7 << "\t # Z->ll : " << zllCands->size()
200 vuko 1.1 << endl;
201    
202     Handle<CandidateCollection> zCands[2] = {zeeCands,zmumuCands};
203    
204     //
205 vuko 1.7 // Find best Z (in Z->ll collection (merged Z->ee & Z->mumu)
206 vuko 1.1 //
207     ::OverlapChecker overlap;
208    
209 vuko 1.3
210     float dzmass_min = 100.;
211    
212     for(CandidateCollection::const_iterator z1 = zllCands->begin();
213     z1 != zllCands->end(); ++ z1 ) {
214     float dzmass = fabs( z1->mass() - 91.188);
215     if (dzmass < dzmass_min) {
216 vuko 1.7 theZCandidate = &(*z1);
217 vuko 1.3 dzmass_min = dzmass;
218     }
219     //
220     //Get back Electrons from Z and W
221     for(CandidateCollection::const_iterator z2 = z1;
222     z2 != zllCands->end(); ++ z2 ) {
223     if (z1 != z2) {
224     if (overlap(*z1,*z2)) {
225 vuko 1.7 cout << "Overlap between two Z of flavour "
226     << z1->daughter(0)->pdgId() << "\t"
227     << z2->daughter(0)->pdgId() << "\t"
228     << endl;
229 vuko 1.3 } else {
230     cout << "NON OVERLAPPING Zs " << endl;
231     }
232     }
233     }
234     }
235    
236    
237 vuko 1.1 zFlavour = 0;
238     wlFlavour = 0;
239    
240 vuko 1.4 /// Find W lepton
241    
242 vuko 1.3
243 vuko 1.1 int nwl_candidates=0;
244     if (theZCandidate) {
245    
246    
247     zMass = theZCandidate->mass();
248     zFlavour = abs(theZCandidate->daughter(0)->pdgId());
249     zPt = theZCandidate->pt();
250     zEta = theZCandidate->eta();
251     zPhi = theZCandidate->phi();
252    
253 vuko 1.4 if (zFlavour == 11) {
254 vuko 1.5 leptonProperties["ZEl1"]->FillProperties(theZCandidate->daughter(0), iEvent, iSetup);
255     leptonProperties["ZEl2"]->FillProperties(theZCandidate->daughter(1), iEvent, iSetup);
256 vuko 1.4 } else if (zFlavour == 13) {
257 vuko 1.5 leptonProperties["Zmu1"]->FillProperties(theZCandidate->daughter(0), iEvent, iSetup);
258     leptonProperties["Zmu2"]->FillProperties(theZCandidate->daughter(1), iEvent, iSetup);
259 vuko 1.4 }
260    
261 vuko 1.1 float max_pt = 0.;
262    
263    
264     // Now find lepton that will be associated to W
265     // among leptons not used for the Z reconstruction
266     for(CandidateCollection::const_iterator lepton = tightLeptonCands->begin();
267     lepton != tightLeptonCands->end(); lepton++) {
268    
269    
270     if ( overlap(*theZCandidate, *lepton) ) continue; // Ignore if lepton used for the Z
271    
272     nwl_candidates++;
273    
274     // If more than one candidate: choose leading pt
275     if (lepton->pt() > max_pt) {
276     theWlepton = &(*lepton);
277     max_pt = lepton->pt();
278     }
279    
280     int id = lepton->pdgId();
281     cout << "Tight lepton: " << id << endl;
282    
283     if(lepton->hasMasterClone()) {
284     cout << "SHOUT: TIGHT LEPTON IS SHALLOW COPY !!! (SHOULD NOT BE!) \n";
285     }
286     //
287     }
288     if (theWlepton) {
289     wlFlavour = theWlepton->pdgId();
290     wlCharge = theWlepton->charge();
291     wlPt = theWlepton->pt();
292     wlEta = theWlepton->eta();
293     wlPhi = theWlepton->phi();
294    
295    
296 vuko 1.3 if (abs(wlFlavour) == 11) {
297     leptonProperties["Wel"]->FillProperties(theWlepton, iEvent, iSetup);
298     } else if (abs(wlFlavour) == 13) {
299     leptonProperties["Wmu"]->FillProperties(theWlepton, iEvent, iSetup);
300     }
301    
302    
303 vuko 1.1 const reco::Muon * muon = dynamic_cast<const reco::Muon *>(&(*theWlepton));
304     if(muon != 0) { /* it's a valid muon */
305     cout << "lepton is a muon \n" << endl;
306     }
307     const reco::PixelMatchGsfElectron * electron = dynamic_cast<const reco::PixelMatchGsfElectron *>(&(*theWlepton));
308     if(electron != 0) { /* it's a valid electron */
309     cout << "lepton is an electron \n" << endl;
310     elWTree->Fill();
311     }
312     }
313    
314     }
315 smorovic 1.6
316     if (storeHLTresults) {
317    
318    
319     //def: 1:single electron 2:single relaxed 3:double electron 4: double relaxed 5,6,7,8: muon e+mu: 9
320     triggerBitmask=0;
321    
322    
323     for (size_t i=0; i<numTriggers; ++i) {
324    
325     // cout << "trigName" << triggerNames[i] << "wasRUN:" << HLTR->wasrun(i) << "error " << HLTR->error(i)
326     // << " accept" << HLTR->accept(i) << "\n";
327    
328     if (!HLTR->wasrun(i)) {
329     //cout << "trigger not run? "<< triggerNames[i] << "\n";
330     continue;
331     }
332     if (HLTR->error(i) ) {
333     //cout << "error trigger: "<< triggerNames[i] <<"\n";
334     continue;
335     }
336    
337     if (HLTR->accept(i)) {
338     //if (triggerNames[i].compare("mcpath")!=0)
339     //TRaccept=true;
340     }else {
341     continue;
342     }
343    
344     if (triggerNames[i].compare("HLT1Electron")==0) triggerBitmask |= 1;
345     if (triggerNames[i].compare("HLT1ElectronRelaxed")==0) triggerBitmask |= 2;
346     if (triggerNames[i].compare("HLT2Electron")==0) triggerBitmask |= 4;
347     if (triggerNames[i].compare("HLT2ElectronRelaxed")==0) triggerBitmask |= 8;
348    
349     if (triggerNames[i].compare("HLT1MuonIso")==0) triggerBitmask |= 16;
350     if (triggerNames[i].compare("HLT1MuonNonIso")==0) triggerBitmask |= 32;
351     if (triggerNames[i].compare("HLT2MuonNonIso")==0) triggerBitmask |= 64;
352     if (triggerNames[i].compare("HLT2MuonZ")==0) triggerBitmask |= 128;
353    
354     if (triggerNames[i].compare("HLTXElectronMuon")==0) triggerBitmask |= 256;
355     if (triggerNames[i].compare("HLTXElectronMuonRelaxed")==0) triggerBitmask |= 512;
356     }
357     }
358    
359 vuko 1.1 wzTree->Fill();
360    
361     }
362    
363    
364     // ------------ method called once each job just before starting event loop ------------
365     void
366     WZAnalyzer::beginJob(const edm::EventSetup&)
367     {
368    
369     using namespace wzana;
370    
371     theFile = new TFile( "wz.root", "RECREATE" ) ;
372    
373     wzTree = new TTree("WZTree","WZ informations");
374    
375     elWTree = new TTree("WElTree","W electron informations");
376    
377 vuko 1.3 leptonProperties["Wel"] = new ElectronProperties();
378     leptonProperties["ZEl1"] = new ElectronProperties();
379     leptonProperties["ZEl2"] = new ElectronProperties();
380    
381     leptonProperties["Wmu"] = new MuonProperties();
382     leptonProperties["Zmu1"] = new MuonProperties();
383     leptonProperties["Zmu2"] = new MuonProperties();
384 vuko 1.1
385 vuko 1.3 leptonProperties["Wel"]->CreateBranches(wzTree, "WEl_");
386     leptonProperties["Wel"]->CreateBranches(elWTree,"ElW_");
387 vuko 1.4 leptonProperties["ZEl1"]->CreateBranches(wzTree, "ZEl1_");
388     leptonProperties["ZEl2"]->CreateBranches(wzTree, "ZEl2_");
389 vuko 1.1
390     initialiseTree();
391 smorovic 1.6
392     //prepare HLT TriggerResults branch
393     if (storeHLTresults) {
394     wzTree->Branch("WZ_hltBitmask",&triggerBitmask,"WZ_hltBitmask/I");
395    
396     }
397    
398    
399 vuko 1.1 }
400    
401     // ------------ method called once each job just after ending the event loop ------------
402     void
403     WZAnalyzer::endJob() {
404    
405     theFile->Write();
406     theFile->Close();
407    
408     }
409    
410    
411     void WZAnalyzer::initialiseTree() {
412    
413     // Z properties
414     wzTree->Branch("Zmass", &zMass, "Zmass/F");
415     wzTree->Branch("ZId", &zFlavour, "Zid/I");
416     wzTree->Branch("ZPt", &zPt, "ZPt/F");
417     wzTree->Branch("ZEta", &zEta, "ZEta/F");
418     wzTree->Branch("ZPhi", &zPhi, "ZPhi/F");
419    
420    
421     // W Properties
422     wzTree->Branch("WlId", &wlFlavour, "Wlid/I");
423     wzTree->Branch("WlCharge", &wlCharge, "WlCharge/I");
424     wzTree->Branch("WlPt", &wlPt, "WlPt/F");
425    
426     }
427    
428    
429     //define this as a plug-in
430 vuko 1.2 // DEFINE_FWK_MODULE(WZAnalyzer);