ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/Vuko/WZAnalysis/src/WZAnalyzer.cc
Revision: 1.1
Committed: Wed Nov 21 15:45:45 2007 UTC (17 years, 5 months ago) by vuko
Content type: text/plain
Branch: MAIN
Log Message:
Commiting my WZ analysis framework

File Contents

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