ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/Vuko/WZAnalysis/src/WZAnalyzer.cc
Revision: 1.4
Committed: Fri Nov 23 15:47:41 2007 UTC (17 years, 5 months ago) by vuko
Content type: text/plain
Branch: MAIN
Changes since 1.3: +13 -1 lines
Log Message:
more changes

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