ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/UHHAnalysis/SFrameAnalysis/src/AnalysisCycle.cxx
Revision: 1.8
Committed: Mon Jul 2 15:06:24 2012 UTC (12 years, 10 months ago) by mmeyer
Content type: text/plain
Branch: MAIN
Changes since 1.7: +17 -3 lines
Log Message:
weights

File Contents

# User Rev Content
1 mmeyer 1.8 // $Id: AnalysisCycle.cxx,v 1.7 2012/06/29 13:05:18 peiffer Exp $
2 rkogler 1.1
3     #include <iostream>
4    
5     using namespace std;
6    
7     // Local include(s):
8     #include "include/AnalysisCycle.h"
9     #include "include/SelectionModules.h"
10     #include "include/ObjectHandler.h"
11     #include "include/EventCalc.h"
12 peiffer 1.2 #include "STreeType.h"
13 rkogler 1.1
14     ClassImp( AnalysisCycle );
15    
16     AnalysisCycle::AnalysisCycle()
17     : SCycleBase() {
18    
19     // constructor, declare variables that should be obtained
20     // from the steering-xml file
21    
22     SetLogName( GetName() );
23    
24     m_puwp = NULL;
25     m_newrun = false;
26    
27     // declare variables for lumi file
28     DeclareProperty( "LumiFilePath" , m_lumifile_path);
29     DeclareProperty( "LumiFileName" , m_lumifile_name);
30     DeclareProperty( "LumiTrigger" , m_lumi_trigger);
31    
32     // steerable properties of the Ntuple files
33     DeclareProperty( "JetCollection", m_JetCollection );
34     DeclareProperty( "ElectronCollection", m_ElectronCollection );
35     DeclareProperty( "MuonCollection", m_MuonCollection );
36     DeclareProperty( "TauCollection", m_TauCollection );
37     DeclareProperty( "PhotonCollection", m_PhotonCollection );
38     DeclareProperty( "PrimaryVertexCollection", m_PrimaryVertexCollection );
39     DeclareProperty( "METName", m_METName );
40     DeclareProperty( "TopJetCollection", m_TopJetCollection );
41 peiffer 1.2 DeclareProperty( "TopJetCollectionGen", m_TopJetCollectionGen );
42 rkogler 1.1 DeclareProperty( "PrunedJetCollection", m_PrunedJetCollection );
43 peiffer 1.2 //DeclareProperty( "addGenInfo", m_addGenInfo);
44 rkogler 1.1 DeclareProperty( "GenParticleCollection", m_GenParticleCollection);
45 peiffer 1.6 DeclareProperty( "readTTbarReco", m_readTTbarReco);
46     DeclareProperty( "writeTTbarReco", m_writeTTbarReco);
47 rkogler 1.1
48     // steerable properties for the Pile-up reweighting
49     DeclareProperty( "PU_Filename_MC" , m_PUFilenameMC);
50     DeclareProperty( "PU_Filename_Data" , m_PUFilenameData);
51     DeclareProperty( "PU_Histname_MC" , m_PUHistnameMC);
52     DeclareProperty( "PU_Histname_Data" , m_PUHistnameData);
53    
54     }
55    
56     AnalysisCycle::~AnalysisCycle()
57     {
58     // destructor
59    
60     if (m_puwp){
61     delete m_puwp;
62     }
63    
64     }
65    
66     void AnalysisCycle::BeginCycle() throw( SError )
67     {
68     // Start of the job, general set-up and definition of
69     // objects are done here
70    
71     // Set-Up LumiHandler
72     LuminosityHandler *lumiHandler = new LuminosityHandler();
73    
74     lumiHandler->SetGRLPath( m_lumifile_path );
75     lumiHandler->SetLumiFileName( m_lumifile_name );
76     lumiHandler->SetTrigger( m_lumi_trigger );
77     lumiHandler->SetIntLumiPerBin( m_int_lumi_per_bin );
78    
79     // Initialise, checks also if LumiFile is specified and readable
80     lumiHandler->Initialise();
81     // dump lumi information in text file
82     //lumiHandler->DumpLumiInfoIntoTxtFile();
83    
84    
85     // adding luminosity handler to gloabl config
86     AddConfigObject( lumiHandler );
87    
88     return;
89    
90     }
91    
92     void AnalysisCycle::EndCycle() throw( SError )
93     {
94    
95     return;
96    
97     }
98    
99 peiffer 1.2 void AnalysisCycle::BeginInputData( const SInputData& inputData) throw( SError )
100 rkogler 1.1 {
101     // declaration of variables and histograms
102    
103     // check if LumiHandler is set up correctly
104     if( LumiHandler() == NULL ){
105     m_logger << FATAL << "Luminosity Handler not properly added to Configuration!" << SLogger::endmsg;
106     exit(-1);
107     }
108    
109 peiffer 1.2 //determine whether running on MC or data
110     m_addGenInfo=true;
111     if(inputData.GetType()=="DATA" || inputData.GetType()=="Data" || inputData.GetType()=="data") m_addGenInfo=false;
112    
113 rkogler 1.1 // Overwrite target Luminosity
114     // has to be done for every input data
115     if( LumiHandler()->IsLumiCalc() ){
116     m_logger << WARNING << "--- Not a WARNING! --- Overwrite Target Lumi with Lumi from LumiFile: "
117     << LumiHandler()->GetTargetLumi() << " (pb^-1)!" << SLogger::endmsg;
118     GetConfig().SetTargetLumi( LumiHandler()->GetTargetLumi() );
119     }
120    
121     // pile-up reweighting
122     if(m_PUFilenameMC.size()>0 && m_PUFilenameData.size()>0 && m_PUHistnameMC.size()>0 && m_PUHistnameData.size()>0){
123     m_logger << INFO << "PU Reweighting will be performed. File(data) = " << m_PUFilenameData
124     << " File(MC) = " << m_PUFilenameMC << SLogger::endmsg;
125     m_logger << INFO << "PU, histograms: Hist(data) = " << m_PUHistnameData
126     << " Hist(MC) = " << m_PUHistnameMC << SLogger::endmsg;
127     m_puwp = new PUWeightProducer(m_PUFilenameMC, m_PUFilenameData, m_PUHistnameMC, m_PUHistnameData);
128     }
129 peiffer 1.2
130     // output Ntuple
131     if (inputData.GetTrees(STreeType::OutputSimpleTree)){
132     m_logger << INFO << "adding output tree" << SLogger::endmsg;
133     DeclareVariable( m_bcc.run, "run" );
134     DeclareVariable( m_bcc.rho, "rho" );
135     DeclareVariable( m_bcc.luminosityBlock, "luminosityBlock" );
136     DeclareVariable( m_bcc.event, "event" );
137     DeclareVariable( m_bcc.isRealData, "isRealData" );
138     //DeclareVariable( m_bcc.HBHENoiseFilterResult, "HBHENoiseFilterResult" );
139     DeclareVariable( m_bcc.beamspot_x0, "beamspot_x0");
140     DeclareVariable( m_bcc.beamspot_y0, "beamspot_y0");
141     DeclareVariable( m_bcc.beamspot_z0, "beamspot_z0");
142     if(m_ElectronCollection.size()>0) DeclareVariable(m_output_electrons, m_ElectronCollection.c_str() );
143     if(m_MuonCollection.size()>0) DeclareVariable(m_output_muons, m_MuonCollection.c_str() );
144     if(m_TauCollection.size()>0) DeclareVariable(m_output_taus, m_TauCollection.c_str() );
145     if(m_JetCollection.size()>0) DeclareVariable(m_output_jets, m_JetCollection.c_str() );
146     if(m_PhotonCollection.size()>0) DeclareVariable(m_output_photons, m_PhotonCollection.c_str() );
147     if(m_METName.size()>0) DeclareVariable(m_output_met, m_METName.c_str() );
148     if(m_PrimaryVertexCollection.size()>0) DeclareVariable(m_output_pvs, m_PrimaryVertexCollection.c_str());
149     if(m_TopJetCollection.size()>0) DeclareVariable(m_output_topjets, m_TopJetCollection.c_str());
150     if(m_addGenInfo && m_TopJetCollectionGen.size()>0) DeclareVariable(m_output_topjetsgen, m_TopJetCollectionGen.c_str());
151     if(m_PrunedJetCollection.size()>0) DeclareVariable(m_output_prunedjets, m_PrunedJetCollection.c_str());
152     if(m_addGenInfo && m_GenParticleCollection.size()>0) DeclareVariable(m_output_genparticles, m_GenParticleCollection.c_str());
153     if(m_addGenInfo) DeclareVariable(m_output_genInfo, "genInfo" );
154 peiffer 1.6 if(m_writeTTbarReco) DeclareVariable(m_output_recoHyps, "recoHyps");
155 peiffer 1.2 DeclareVariable(m_output_triggerNames, "triggerNames");
156     DeclareVariable(m_output_triggerResults, "triggerResults");
157     }
158 rkogler 1.1 return;
159    
160     }
161    
162    
163     void AnalysisCycle::RegisterSelection(Selection* sel)
164     {
165     // register a selection in the list of selections
166    
167 rkogler 1.4 if (!sel){
168     m_logger << WARNING << "Got NULL pointer, can not register selection." << SLogger::endmsg;
169     return;
170     }
171    
172     // check if selection already exists, only register it if not
173     TString InName(sel->GetName());
174     bool exists = false;
175     for (unsigned int i=0; i<m_selections.size(); ++i){
176     TString SelName = m_selections[i]->GetName();
177     if (InName == SelName){
178     exists = true;
179     }
180     }
181     if (!exists){
182     m_selections.push_back(sel);
183     }
184    
185 rkogler 1.1 return;
186     }
187    
188    
189     void AnalysisCycle::RegisterHistCollection(BaseHists* hists)
190     {
191     // register a collection of histograms in the list
192    
193 rkogler 1.4 if (!hists){
194     m_logger << WARNING << "Got NULL pointer, can not register histogram collection." << SLogger::endmsg;
195     return;
196     }
197 peiffer 1.5
198    
199 rkogler 1.1 m_histcollections.push_back(hists);
200 peiffer 1.5
201    
202 rkogler 1.1 return;
203     }
204    
205    
206     Selection* AnalysisCycle::GetSelection(const std::string name)
207     {
208     // get a selection from the list of selections
209    
210     TString InName(name.c_str());
211     for (unsigned int i=0; i<m_selections.size(); ++i){
212     TString SelName = m_selections[i]->GetName();
213     if (InName == SelName){
214     return m_selections[i];
215     }
216     }
217 rkogler 1.4 m_logger << DEBUG << "Could not find selection with name " << InName << "." << SLogger::endmsg;
218 rkogler 1.1 return NULL;
219     }
220    
221    
222     BaseHists* AnalysisCycle::GetHistCollection(const std::string name)
223     {
224     // get a histogram collection from the list
225    
226     TString InName(name.c_str());
227     for (unsigned int i=0; i<m_histcollections.size(); ++i){
228     TString HistName = m_histcollections[i]->GetName();
229     if (InName == HistName){
230     return m_histcollections[i];
231     }
232     }
233     m_logger << WARNING << "Could not find hist collection with name " << InName << "." << SLogger::endmsg;
234     return NULL;
235     }
236    
237    
238    
239     void AnalysisCycle::InitHistos()
240     {
241     // initialise all registered histograms and set the
242     // correct output path
243    
244     for (unsigned int i=0; i<m_histcollections.size(); ++i){
245     m_histcollections[i]->SetHistOutput(this->GetHistOutput());
246     m_histcollections[i]->Init();
247     }
248    
249     return;
250    
251     }
252    
253     void AnalysisCycle::FinaliseHistos()
254     {
255     // finalise all registered histograms
256    
257     for (unsigned int i=0; i<m_histcollections.size(); ++i){
258     m_histcollections[i]->Finish();
259     }
260    
261 peiffer 1.5 m_histcollections.clear();
262    
263 rkogler 1.1 return;
264    
265     }
266    
267     void AnalysisCycle::PrintSelectionSummary()
268     {
269     // print a summary of all selections
270    
271     for (unsigned int i=0; i<m_selections.size(); ++i){
272     m_selections[i]->printCutFlow();
273     }
274    
275     }
276    
277 rkogler 1.4 void AnalysisCycle::ResetSelectionStats()
278     {
279     // set all selection statistics to zero
280    
281     for (unsigned int i=0; i<m_selections.size(); ++i){
282     m_selections[i]->resetCutFlow();
283     }
284    
285     }
286    
287 rkogler 1.1
288     void AnalysisCycle::EndInputData( const SInputData& ) throw( SError )
289     {
290 mmeyer 1.8
291 peiffer 1.3 // clean-up, info messages and final calculations after the analysis
292 rkogler 1.1
293 peiffer 1.3 PrintSelectionSummary();
294    
295     FinaliseHistos();
296    
297 rkogler 1.4 ResetSelectionStats();
298 peiffer 1.3
299     return;
300 rkogler 1.1
301 mmeyer 1.8
302 rkogler 1.1 }
303    
304     void AnalysisCycle::BeginInputFile( const SInputData& ) throw( SError )
305     {
306     // Connect all variables from the Ntuple file with the ones needed for the analysis.
307     // The different collections that should be loaded are steerable through the XML file.
308     // The variables are commonly stored in the BaseCycleContaincer and can be
309     // accessed afterwards through the ObjectHandler
310    
311     ConnectVariable( "AnalysisTree", "triggerResults" , m_bcc.triggerResults);
312     ConnectVariable( "AnalysisTree", "triggerNames" , m_bcc.triggerNames);
313     if(m_ElectronCollection.size()>0) ConnectVariable( "AnalysisTree", m_ElectronCollection.c_str() ,m_bcc. electrons);
314     if(m_MuonCollection.size()>0) ConnectVariable( "AnalysisTree", m_MuonCollection.c_str() , m_bcc.muons);
315     if(m_TauCollection.size()>0) ConnectVariable( "AnalysisTree", m_TauCollection.c_str() , m_bcc.taus);
316     if(m_JetCollection.size()>0) ConnectVariable( "AnalysisTree", m_JetCollection.c_str() , m_bcc.jets);
317     if(m_PhotonCollection.size()>0) ConnectVariable( "AnalysisTree", m_PhotonCollection.c_str() , m_bcc.photons);
318     if(m_METName.size()>0) ConnectVariable( "AnalysisTree", m_METName.c_str() , m_bcc.met);
319     if(m_PrimaryVertexCollection.size()>0) ConnectVariable( "AnalysisTree", m_PrimaryVertexCollection.c_str() , m_bcc.pvs);
320     if(m_TopJetCollection.size()>0) ConnectVariable( "AnalysisTree", m_TopJetCollection.c_str() , m_bcc.topjets);
321 peiffer 1.2 if(m_addGenInfo && m_TopJetCollectionGen.size()>0) ConnectVariable( "AnalysisTree", m_TopJetCollectionGen.c_str() , m_bcc.topjetsgen);
322 rkogler 1.1 if(m_PrunedJetCollection.size()>0) ConnectVariable( "AnalysisTree", m_PrunedJetCollection.c_str() , m_bcc.prunedjets);
323 rkogler 1.4 if(m_addGenInfo && m_GenParticleCollection.size()>0) ConnectVariable( "AnalysisTree", m_GenParticleCollection.c_str() , m_bcc.genparticles);
324 rkogler 1.1 if(m_addGenInfo) ConnectVariable( "AnalysisTree", "genInfo" , m_bcc.genInfo);
325 peiffer 1.6 if(m_readTTbarReco) ConnectVariable( "AnalysisTree", "recoHyps", m_bcc.recoHyps);
326 rkogler 1.1 ConnectVariable( "AnalysisTree", "run" , m_bcc.run);
327 peiffer 1.2 ConnectVariable( "AnalysisTree", "rho" , m_bcc.rho);
328 rkogler 1.1 ConnectVariable( "AnalysisTree", "luminosityBlock" , m_bcc.luminosityBlock);
329     ConnectVariable( "AnalysisTree" ,"event" ,m_bcc.event);
330     ConnectVariable( "AnalysisTree" ,"isRealData", m_bcc.isRealData);
331     //ConnectVariable( "AnalysisTree" ,"HBHENoiseFilterResult", m_bcc.HBHENoiseFilterResult);
332     ConnectVariable( "AnalysisTree" ,"beamspot_x0", m_bcc.beamspot_x0);
333     ConnectVariable( "AnalysisTree" ,"beamspot_y0", m_bcc.beamspot_y0);
334     ConnectVariable( "AnalysisTree" ,"beamspot_z0", m_bcc.beamspot_z0);
335    
336 mmeyer 1.8 //if(m_caTopTagGen.size()>0) ConnectVariable("AnalysisTree", m_caTopTagGen.c_str(), m_bcc.topjets);
337    
338    
339 rkogler 1.1 ObjectHandler* objs = ObjectHandler::Instance();
340     objs->SetBaseCycleContainer(&m_bcc);
341     objs->SetLumiHandler( LumiHandler() );
342    
343     return;
344    
345     }
346    
347     void AnalysisCycle::ExecuteEvent( const SInputData&, Double_t weight) throw( SError )
348     {
349     // This method performs basic consistency checks, resets the event calculator,
350     // calculates the pile-up weight and performs the good-run selection.
351     // It should always be the first thing to be called in each user analysis.
352    
353     // first thing to do: call reset of event calc
354     EventCalc* calc = EventCalc::Instance();
355     calc->Reset();
356    
357     if(m_bcc.isRealData && m_addGenInfo){
358 peiffer 1.2 m_logger << WARNING<< "Running over real data, but addGenInfo=True?!" << SLogger::endmsg;
359 rkogler 1.1 }
360    
361     //fill list of trigger names
362     if(m_bcc.triggerNames->size()!=0){
363     m_bcc.triggerNames_actualrun = *m_bcc.triggerNames;
364     m_newrun=true;
365     }
366    
367     // generate random run Nr for MC samples (consider luminosity of each run)
368     // e.g. for proper OTX cut in MC, and needs to be done only once per event
369     if( !m_bcc.isRealData && LumiHandler()->IsLumiCalc() ){
370     m_bcc.run = LumiHandler()->GetRandomRunNr();
371     }
372    
373    
374     // store the weight (lumiweight) in the eventcalc class and use it
375 mmeyer 1.8 calc -> ProduceWeight(weight);
376    
377 rkogler 1.1
378     if(m_bcc.genInfo){
379 mmeyer 1.8
380     std::vector<float> genweights = m_bcc.genInfo->weights();
381     for(unsigned int i=0; i< genweights.size(); ++i){
382     calc -> ProduceWeight(genweights[i]);
383     }
384    
385 rkogler 1.1 if(m_puwp){
386 mmeyer 1.8 double pu_weight = m_puwp->produceWeight(m_bcc.genInfo);
387 rkogler 1.1 // set the weight in the eventcalc
388 mmeyer 1.8 calc -> ProduceWeight(pu_weight);
389 rkogler 1.1 }
390     }
391 mmeyer 1.8
392 rkogler 1.1 //select only good runs
393     if(m_bcc.isRealData && LumiHandler()->IsLumiCalc() ){
394     if( !LumiHandler()->PassGoodRunsList( m_bcc.run, m_bcc.luminosityBlock )) throw SError( SError::SkipEvent );
395     }
396    
397 peiffer 1.7 //create new pointer to recoHyps if no recoHyps were read in
398 peiffer 1.6 //note: list of recoHyps is still empty, has to be filled in the user cycle
399 peiffer 1.7 if(!m_readTTbarReco) m_bcc.recoHyps = new std::vector<ReconstructionHypothesis>;
400 rkogler 1.1
401     return;
402    
403     }
404    
405 peiffer 1.2 void AnalysisCycle::WriteOutputTree() throw( SError)
406     {
407     //write out all objects
408    
409     m_output_photons.clear();
410     m_output_jets.clear();
411     m_output_electrons.clear();
412     m_output_muons.clear();
413     m_output_taus.clear();
414     m_output_pvs.clear();
415     m_output_topjets.clear();
416     m_output_topjetsgen.clear();
417     m_output_prunedjets.clear();
418     m_output_genparticles.clear();
419     m_output_triggerNames.clear();
420     m_output_triggerResults.clear();
421 peiffer 1.6 m_output_recoHyps.clear();
422 peiffer 1.2
423     if(m_PhotonCollection.size()>0) m_output_photons=*m_bcc.photons;
424     if(m_JetCollection.size()>0) m_output_jets=*m_bcc.jets;
425     if(m_ElectronCollection.size()>0) m_output_electrons=*m_bcc.electrons;
426     if(m_MuonCollection.size()>0) m_output_muons=*m_bcc.muons;
427     if(m_TauCollection.size()>0) m_output_taus=*m_bcc.taus;
428     if(m_PrimaryVertexCollection.size()>0) m_output_pvs=*m_bcc.pvs;
429     if(m_METName.size()>0) m_output_met = *m_bcc.met;
430     if(m_addGenInfo) m_output_genInfo = *m_bcc.genInfo;
431     if(m_TopJetCollection.size()>0) m_output_topjets=*m_bcc.topjets;
432     if(m_addGenInfo && m_TopJetCollectionGen.size()>0) m_output_topjetsgen=*m_bcc.topjetsgen;
433     if(m_PrunedJetCollection.size()>0) m_output_prunedjets=*m_bcc.prunedjets;
434     if(m_addGenInfo && m_GenParticleCollection.size()>0) m_output_genparticles=*m_bcc.genparticles;
435 peiffer 1.6 if(m_writeTTbarReco) m_output_recoHyps=*m_bcc.recoHyps;
436    
437 peiffer 1.2 if(m_newrun) m_output_triggerNames = m_bcc.triggerNames_actualrun;//store trigger names only for new runs
438     m_newrun=false;
439    
440     m_output_triggerResults = *m_bcc.triggerResults;
441    
442     }
443