ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/UHHAnalysis/SFrameTools/src/EventCalc.cxx
Revision: 1.10
Committed: Mon Mar 25 12:22:56 2013 UTC (12 years, 1 month ago) by peiffer
Content type: text/plain
Branch: MAIN
CVS Tags: Makefile, v1-00
Changes since 1.9: +1 -0 lines
Log Message:
memory leak for gen ttbar fixed

File Contents

# User Rev Content
1 rkogler 1.1 // Dear emacs, this is -*- c++ -*-
2    
3     #include "include/EventCalc.h"
4    
5     #include <iostream>
6    
7     EventCalc* EventCalc::m_instance = NULL;
8    
9     EventCalc* EventCalc::Instance()
10     {
11     // Get a pointer to the object handler.
12     // This is the only way to access this class,
13     // since it's a singleton. This method is accessible
14     // from everywhere.
15    
16     if (m_instance == NULL){
17     m_instance = new EventCalc();
18     }
19     return m_instance;
20     }
21    
22     EventCalc::EventCalc() : m_logger( "EventCalc" )
23     {
24     // constructor: initialise all variables
25     m_logger << DEBUG << "Constructor called." << SLogger::endmsg;
26     m_bcc = NULL;
27     m_lumi = NULL;
28 peiffer 1.5 m_primlep = NULL;
29 peiffer 1.6 m_ttgen = NULL;
30 rkogler 1.1 }
31    
32     EventCalc::~EventCalc()
33     {
34     // default destructor
35     }
36    
37     void EventCalc::Reset()
38     {
39     // reset: set all booleans to false
40     // this has to be done at the beginning of each event
41     // after a call to reset all quantities will be re-calculated
42     // when they are accessed
43    
44     // (re-)set the pointers using the ObjectHandler
45     ObjectHandler* objs = ObjectHandler::Instance();
46     m_bcc = objs->GetBaseCycleContainer();
47     m_lumi = objs->GetLumiHandler();
48    
49     // reset booleans
50     b_HT = false;
51 rkogler 1.2 b_HTlep = false;
52 peiffer 1.6 b_Reconstruction = false;
53 rkogler 1.2
54 mmeyer 1.7 m_TotalWeight = 1.;
55    
56 peiffer 1.5 m_primlep = NULL;
57 peiffer 1.10 delete m_ttgen;
58 peiffer 1.6 m_ttgen = NULL;
59 mmeyer 1.7
60 rkogler 1.1 }
61    
62     BaseCycleContainer* EventCalc::GetBaseCycleContainer()
63     {
64     // return the pointer to the container with all objects
65     if (!m_bcc){
66     m_logger << WARNING << "Pointer to BaseCycleContainer is NULL." << SLogger::endmsg;
67     }
68     return m_bcc;
69     }
70    
71     LuminosityHandler* EventCalc::GetLumiHandler()
72     {
73     // return the pointer to the container with all objects
74     if (!m_lumi){
75     m_logger << WARNING << "Pointer to LumiHandler is NULL." << SLogger::endmsg;
76     }
77     return m_lumi;
78     }
79    
80     double EventCalc::GetHT()
81     {
82     // calculate HT, which is defined as the scalar sum of all
83     // jets, leptons and missing transverse momentum in the event
84     if (!b_HT){
85    
86     b_HT = true;
87     m_HT = 0;
88    
89     // add lepton pt and MET
90     m_HT += GetHTlep();
91    
92     // sum over pt of all jets
93     if(m_bcc->jets){
94     for(unsigned int i=0; i<m_bcc->jets->size(); ++i){
95     m_HT += m_bcc->jets->at(i).pt();
96     }
97     }
98    
99     }
100     return m_HT;
101     }
102    
103     double EventCalc::GetHTlep()
104     {
105     // calculate HT_lep, which is defined as the scalar sum of all
106     // leptons and missing transverse momentum in the event
107     if (!b_HTlep){
108    
109     b_HTlep = true;
110     m_HTlep=0;
111    
112     // sum over pt of all electrons
113     if(m_bcc->electrons){
114     for(unsigned int i=0; i<m_bcc->electrons->size(); ++i){
115     m_HTlep += m_bcc->electrons->at(i).pt();
116     }
117     }
118    
119     // sum over pt of all muons
120     if(m_bcc->muons){
121     for(unsigned int i=0; i<m_bcc->muons->size(); ++i){
122     m_HTlep += m_bcc->muons->at(i).pt();
123     }
124     }
125    
126     // sum over pt of all taus
127     if(m_bcc->taus){
128     for(unsigned int i=0; i<m_bcc->taus->size(); ++i){
129     m_HTlep += m_bcc->taus->at(i).pt();
130     }
131     }
132    
133     // add MET
134     if(m_bcc->met) m_HTlep += m_bcc->met->pt();
135    
136     }
137     return m_HTlep;
138     }
139    
140 peiffer 1.5 Particle* EventCalc::GetPrimaryLepton(){
141    
142     if(!m_primlep){
143     double ptmax = -999;
144     for(unsigned int i=0; i<m_bcc->electrons->size(); ++i){
145     if(m_bcc->electrons->at(i).pt()>ptmax){
146     ptmax = m_bcc->electrons->at(i).pt();
147     m_primlep = &m_bcc->electrons->at(i);
148     }
149     }
150     for(unsigned int i=0; i<m_bcc->muons->size(); ++i){
151     if(m_bcc->muons->at(i).pt()>ptmax){
152     ptmax = m_bcc->muons->at(i).pt();
153     m_primlep = &m_bcc->muons->at(i);
154     }
155     }
156     }
157    
158     return m_primlep;
159     }
160    
161 peiffer 1.6 TTbarGen* EventCalc::GetTTbarGen(){
162    
163     if(!m_ttgen){
164     m_ttgen = new TTbarGen();
165     }
166    
167     return m_ttgen;
168     }
169    
170 peiffer 1.5
171     std::vector<LorentzVector> EventCalc::NeutrinoReconstruction(const LorentzVector lepton, const LorentzVector met){
172    
173    
174     TVector3 lepton_pT = toVector(lepton);
175     lepton_pT.SetZ(0);
176    
177     TVector3 neutrino_pT = toVector(met);
178     neutrino_pT.SetZ(0);
179    
180     const float mass_w = 80.399;
181     float mu = mass_w * mass_w / 2 + lepton_pT * neutrino_pT;
182    
183     float A = - (lepton_pT * lepton_pT);
184     float B = mu * lepton.pz();
185     float C = mu * mu - lepton.e() * lepton.e() * (neutrino_pT * neutrino_pT);
186    
187     float discriminant = B * B - A * C;
188    
189     std::vector<LorentzVector> solutions;
190    
191     if (0 >= discriminant)
192     {
193     // Take only real part of the solution
194     //
195     LorentzVectorXYZE solution (0,0,0,0);
196     solution.SetPx(met.Px());
197     solution.SetPy(met.Py());
198     solution.SetPz(-B / A);
199     solution.SetE(toVector(solution).Mag());
200    
201     solutions.push_back(toPtEtaPhi(solution));
202    
203     //_solutions = 0 > discriminant ? 0 : 1;
204     }
205     else
206     {
207     discriminant = sqrt(discriminant);
208    
209     LorentzVectorXYZE solution (0,0,0,0);
210     solution.SetPx(met.Px());
211     solution.SetPy(met.Py());
212     solution.SetPz((-B - discriminant) / A);
213     solution.SetE(toVector(solution).Mag());
214    
215     solutions.push_back(toPtEtaPhi(solution));
216    
217     LorentzVectorXYZE solution2 (0,0,0,0);
218     solution2.SetPx(met.Px());
219     solution2.SetPy(met.Py());
220     solution2.SetPz((-B + discriminant) / A);
221     solution2.SetE(toVector(solution2).Mag());
222    
223     solutions.push_back(toPtEtaPhi(solution2));
224    
225     //_solutions = 2;
226     }
227    
228     return solutions;
229     }
230    
231     void EventCalc::FillHighMassTTbarHypotheses(){
232    
233 peiffer 1.6 if(b_Reconstruction) return;
234     b_Reconstruction=true;
235    
236 peiffer 1.5 //clear hypothesis list
237     m_bcc->recoHyps->clear();
238    
239     //find primary charged lepton
240     Particle* lepton = GetPrimaryLepton();
241    
242     //reconstruct neutrino
243     std::vector<LorentzVector> neutrinos = NeutrinoReconstruction( lepton->v4(), m_bcc->met->v4());
244    
245     ReconstructionHypothesis hyp;
246    
247     hyp.set_lepton(*lepton);
248    
249     //loop over neutrino solutions and jet assignments to fill hyotheses
250     for(unsigned int i=0; i< neutrinos.size();++i){
251    
252     hyp.set_neutrino_v4(neutrinos[i]);
253     LorentzVector wlep_v4 = lepton->v4()+neutrinos[i];
254    
255     unsigned int n_jets = m_bcc->jets->size();
256 peiffer 1.9 if(n_jets>10) n_jets=10; //avoid crashes in events with many jets
257 peiffer 1.5 unsigned int max_j = myPow(3, n_jets);
258     for (unsigned int j=0; j < max_j; j++) {
259     LorentzVector tophad_v4(0,0,0,0);
260     LorentzVector toplep_v4 = wlep_v4;
261     int hadjets=0;
262     int lepjets=0;
263     int num = j;
264     hyp.clear_jetindices();
265     for (unsigned int k=0; k<n_jets; k++) {
266     // num is the k-th digit of j if you
267     // write j in a base-3 system. According
268     // to the value of this digit (which takes
269     // values from 0 to 2,
270     // in all possible combinations with the other digits),
271     // decide how to treat the jet.
272    
273     if(num%3==0) {
274     tophad_v4 = tophad_v4 + m_bcc->jets->at(k).v4();
275     hyp.add_tophad_jet_index(k);
276     hadjets++;
277     }
278    
279     if(num%3==1) {
280     toplep_v4 = toplep_v4 + m_bcc->jets->at(k).v4();
281     hyp.add_toplep_jet_index(k);
282     lepjets++;
283     }
284     //if(num%3==2); //do not take this jet
285    
286     //shift the trigits of num to the right:
287     num /= 3;
288     }
289    
290 peiffer 1.6 //search jet with highest pt assigned to leptonic top
291     float maxpt=-999;
292     int maxind=-1;
293     for(unsigned int i=0; i<hyp.toplep_jets_indices().size(); ++i){
294     float pt = m_bcc->jets->at(hyp.toplep_jets_indices().at(i)).pt();
295     if(pt>maxpt){
296     maxpt=pt;
297     maxind=hyp.toplep_jets_indices().at(i);
298     }
299     }
300     hyp.set_blep_index(maxind);
301    
302 peiffer 1.5
303 peiffer 1.6 //fill only hypotheses with at least one jet assigned to each top quark
304 peiffer 1.5 if(hadjets>0 && lepjets>0){
305     hyp.set_tophad_v4(tophad_v4);
306     hyp.set_toplep_v4(toplep_v4);
307     m_bcc->recoHyps->push_back(hyp);
308     }
309     }
310     }
311    
312     }
313    
314    
315 peiffer 1.3 void EventCalc::PrintEventContent(){
316 rkogler 1.1
317 peiffer 1.3 m_logger << INFO << "----------------- event content -----------------" << SLogger::endmsg;
318     m_logger << INFO << "run: " << m_bcc->run << " lumi block:" << m_bcc->luminosityBlock << " event: " << m_bcc->event << SLogger::endmsg;
319     m_logger << INFO << "MET = " << m_bcc->met->pt() << " METphi = " << m_bcc->met->phi() << " HTlep = " << GetHTlep() << SLogger::endmsg;
320     if(m_bcc->electrons){m_logger << INFO << "Electrons:" << SLogger::endmsg;
321     for(unsigned int i=0; i<m_bcc->electrons->size(); ++i){
322 peiffer 1.5 m_logger << INFO << " " << i+1 << " pt = " << m_bcc->electrons->at(i).pt() <<" eta = " << m_bcc->electrons->at(i).eta()
323     << " supercluster eta = " <<m_bcc->electrons->at(i).supercluster_eta() << " IP wrt bsp = " << fabs(m_bcc->electrons->at(i).gsfTrack_dxy_vertex(m_bcc->pvs->at(0).x(), m_bcc->pvs->at(0).y()))
324     << " pass conv veto = " << m_bcc->electrons->at(i).passconversionveto() << " mvaTrigV0 = " << m_bcc->electrons->at(i).mvaTrigV0()
325     << " dEtaIn = " << m_bcc->electrons->at(i).dEtaIn() << " sigmaIEtaIEta = " << m_bcc->electrons->at(i).sigmaIEtaIEta()
326     << " HoverE = " << m_bcc->electrons->at(i).HoverE() << " EcalEnergy = " << m_bcc->electrons->at(i).EcalEnergy()
327     << " EoverPIn = " << m_bcc->electrons->at(i).EoverPIn() << " trackMomentumAtVtx = " << m_bcc->electrons->at(i).EcalEnergy()/m_bcc->electrons->at(i).EoverPIn()
328     << SLogger::endmsg;
329 peiffer 1.3 }
330     }
331     if(m_bcc->muons){m_logger << INFO << "Muons:" << SLogger::endmsg;
332     for(unsigned int i=0; i<m_bcc->muons->size(); ++i){
333     m_logger << INFO << " " << i+1 << " pt = " << m_bcc->muons->at(i).pt() <<" eta = " << m_bcc->muons->at(i).eta() << SLogger::endmsg;
334     }
335     }
336     if(m_bcc->taus){m_logger << INFO << "Taus:" << SLogger::endmsg;
337     for(unsigned int i=0; i<m_bcc->taus->size(); ++i){
338     m_logger << INFO << " " << i+1 << " pt = " << m_bcc->taus->at(i).pt() <<" eta = " << m_bcc->taus->at(i).eta() << SLogger::endmsg;
339     }
340     }
341     if(m_bcc->jets){m_logger << INFO << "Jets:" << SLogger::endmsg;
342     for(unsigned int i=0; i<m_bcc->jets->size(); ++i){
343 peiffer 1.4 m_logger << INFO << " " << i+1 << " pt = " << m_bcc->jets->at(i).pt() <<" eta = " << m_bcc->jets->at(i).eta() << " genjet_pt = " << m_bcc->jets->at(i).genjet_pt() << " genjet_eta = " << m_bcc->jets->at(i).genjet_eta() <<SLogger::endmsg;
344 peiffer 1.3 }
345     }
346     if(m_bcc->topjets){m_logger << INFO << "TopJets:" << SLogger::endmsg;
347     for(unsigned int i=0; i<m_bcc->topjets->size(); ++i){
348     m_logger << INFO << " " << i+1 << " pt = " << m_bcc->topjets->at(i).pt() <<" eta = " << m_bcc->topjets->at(i).eta() << SLogger::endmsg;
349     }
350     }
351     if(m_bcc->photons){m_logger << INFO << "Photons:" << SLogger::endmsg;
352     for(unsigned int i=0; i<m_bcc->photons->size(); ++i){
353     m_logger << INFO << " " << i+1 << " pt = " << m_bcc->photons->at(i).pt() <<" eta = " << m_bcc->photons->at(i).eta() << SLogger::endmsg;
354     }
355     }
356     }
357 mmeyer 1.7
358    
359     void EventCalc::ProduceWeight(double weight)
360     {
361     m_TotalWeight = m_TotalWeight * weight;
362     }
363    
364     double EventCalc::GetWeight()
365     {
366    
367     return m_TotalWeight;
368     }
369