ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/UHHAnalysis/SFrameAnalysis/src/AnalysisCycle.cxx
Revision: 1.1
Committed: Tue Jun 5 14:42:34 2012 UTC (12 years, 11 months ago) by rkogler
Content type: text/plain
Branch: MAIN
Log Message:
Base class for every analysis cycle

File Contents

# User Rev Content
1 rkogler 1.1 // $Id: CycleCreators.py 159 2010-04-13 09:44:22Z krasznaa $
2    
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/ExampleHists.h"
11     #include "include/ObjectHandler.h"
12     #include "include/EventCalc.h"
13    
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     DeclareProperty( "PrunedJetCollection", m_PrunedJetCollection );
42     DeclareProperty( "addGenInfo", m_addGenInfo);
43     DeclareProperty( "GenParticleCollection", m_GenParticleCollection);
44    
45     // steerable properties for the Pile-up reweighting
46     DeclareProperty( "PU_Filename_MC" , m_PUFilenameMC);
47     DeclareProperty( "PU_Filename_Data" , m_PUFilenameData);
48     DeclareProperty( "PU_Histname_MC" , m_PUHistnameMC);
49     DeclareProperty( "PU_Histname_Data" , m_PUHistnameData);
50    
51     }
52    
53     AnalysisCycle::~AnalysisCycle()
54     {
55     // destructor
56    
57     if (m_puwp){
58     delete m_puwp;
59     }
60    
61     }
62    
63     void AnalysisCycle::BeginCycle() throw( SError )
64     {
65     // Start of the job, general set-up and definition of
66     // objects are done here
67    
68     // Set-Up LumiHandler
69     LuminosityHandler *lumiHandler = new LuminosityHandler();
70    
71     lumiHandler->SetGRLPath( m_lumifile_path );
72     lumiHandler->SetLumiFileName( m_lumifile_name );
73     lumiHandler->SetTrigger( m_lumi_trigger );
74     lumiHandler->SetIntLumiPerBin( m_int_lumi_per_bin );
75    
76     // Initialise, checks also if LumiFile is specified and readable
77     lumiHandler->Initialise();
78     // dump lumi information in text file
79     //lumiHandler->DumpLumiInfoIntoTxtFile();
80    
81    
82     // adding luminosity handler to gloabl config
83     AddConfigObject( lumiHandler );
84    
85     return;
86    
87     }
88    
89     void AnalysisCycle::EndCycle() throw( SError )
90     {
91     // clean-up, info messages and final calculations after the analysis
92    
93     PrintSelectionSummary();
94    
95     FinaliseHistos();
96    
97     return;
98    
99     }
100    
101     void AnalysisCycle::BeginInputData( const SInputData& ) throw( SError )
102     {
103     // declaration of variables and histograms
104    
105     // check if LumiHandler is set up correctly
106     if( LumiHandler() == NULL ){
107     m_logger << FATAL << "Luminosity Handler not properly added to Configuration!" << SLogger::endmsg;
108     exit(-1);
109     }
110    
111     // Overwrite target Luminosity
112     // has to be done for every input data
113     if( LumiHandler()->IsLumiCalc() ){
114     m_logger << WARNING << "--- Not a WARNING! --- Overwrite Target Lumi with Lumi from LumiFile: "
115     << LumiHandler()->GetTargetLumi() << " (pb^-1)!" << SLogger::endmsg;
116     GetConfig().SetTargetLumi( LumiHandler()->GetTargetLumi() );
117     }
118    
119     // pile-up reweighting
120     if(m_PUFilenameMC.size()>0 && m_PUFilenameData.size()>0 && m_PUHistnameMC.size()>0 && m_PUHistnameData.size()>0){
121     m_logger << INFO << "PU Reweighting will be performed. File(data) = " << m_PUFilenameData
122     << " File(MC) = " << m_PUFilenameMC << SLogger::endmsg;
123     m_logger << INFO << "PU, histograms: Hist(data) = " << m_PUHistnameData
124     << " Hist(MC) = " << m_PUHistnameMC << SLogger::endmsg;
125     m_puwp = new PUWeightProducer(m_PUFilenameMC, m_PUFilenameData, m_PUHistnameMC, m_PUHistnameData);
126     }
127    
128     // output Ntuple for preselection
129     // if (OutputTree){ // defined in XML file?
130     // DeclareVariable( m_bcc.run, "run" );
131     // DeclareVariable( m_bcc.luminosityBlock, "luminosityBlock" );
132     // DeclareVariable( m_bcc.event, "event" );
133     // DeclareVariable( m_bcc.isRealData, "isRealData" );
134     // DeclareVariable( m_bcc.HBHENoiseFilterResult, "HBHENoiseFilterResult" );
135     // DeclareVariable( m_bcc.beamspot_x0, "beamspot_x0");
136     // DeclareVariable( m_bcc.beamspot_y0, "beamspot_y0");
137     // DeclareVariable( m_bcc.beamspot_z0, "beamspot_z0");
138     // if(m_ElectronCollection.size()>0) DeclareVariable( o_electrons, ElectronCollection.c_str() );
139     // if(m_MuonCollection.size()>0) DeclareVariable( o_muons, MuonCollection.c_str() );
140     // if(m_TauCollection.size()>0) DeclareVariable( o_taus, TauCollection.c_str() );
141     // if(JetCollection.size()>0) DeclareVariable( o_jets, JetCollection.c_str() );
142     // if(PhotonCollection.size()>0) DeclareVariable( o_photons, PhotonCollection.c_str() );
143     // if(METName.size()>0) DeclareVariable( o_met, METName.c_str() );
144     // if(PrimaryVertexCollection.size()>0) DeclareVariable( o_pvs, PrimaryVertexCollection.c_str());
145     // if(TopJetCollection.size()>0) DeclareVariable( o_topjets, TopJetCollection.c_str());
146     // if(PrunedJetCollection.size()>0) DeclareVariable( o_prunedjets, PrunedJetCollection.c_str());
147     // if(GenParticleCollection.size()>0) DeclareVariable( o_genparticles, GenParticleCollection.c_str());
148     // if(addGenInfo) DeclareVariable( o_genInfo, "genInfo" );
149     // DeclareVariable( o_triggerNames, "triggerNames");
150     // DeclareVariable( o_triggerResults, "triggerResults");
151     // }
152    
153     return;
154    
155     }
156    
157    
158     void AnalysisCycle::RegisterSelection(Selection* sel)
159     {
160     // register a selection in the list of selections
161    
162     m_selections.push_back(sel);
163     return;
164     }
165    
166    
167     void AnalysisCycle::RegisterHistCollection(BaseHists* hists)
168     {
169     // register a collection of histograms in the list
170    
171     m_histcollections.push_back(hists);
172     return;
173     }
174    
175    
176     Selection* AnalysisCycle::GetSelection(const std::string name)
177     {
178     // get a selection from the list of selections
179    
180     TString InName(name.c_str());
181     for (unsigned int i=0; i<m_selections.size(); ++i){
182     TString SelName = m_selections[i]->GetName();
183     if (InName == SelName){
184     return m_selections[i];
185     }
186     }
187     m_logger << WARNING << "Could not find selection with name " << InName << "." << SLogger::endmsg;
188     return NULL;
189     }
190    
191    
192     BaseHists* AnalysisCycle::GetHistCollection(const std::string name)
193     {
194     // get a histogram collection from the list
195    
196     TString InName(name.c_str());
197     for (unsigned int i=0; i<m_histcollections.size(); ++i){
198     TString HistName = m_histcollections[i]->GetName();
199     if (InName == HistName){
200     return m_histcollections[i];
201     }
202     }
203     m_logger << WARNING << "Could not find hist collection with name " << InName << "." << SLogger::endmsg;
204     return NULL;
205     }
206    
207    
208    
209     void AnalysisCycle::InitHistos()
210     {
211     // initialise all registered histograms and set the
212     // correct output path
213    
214     for (unsigned int i=0; i<m_histcollections.size(); ++i){
215     m_histcollections[i]->SetHistOutput(this->GetHistOutput());
216     m_histcollections[i]->Init();
217     }
218    
219     return;
220    
221     }
222    
223     void AnalysisCycle::FinaliseHistos()
224     {
225     // finalise all registered histograms
226    
227     for (unsigned int i=0; i<m_histcollections.size(); ++i){
228     m_histcollections[i]->Finish();
229     }
230    
231     return;
232    
233     }
234    
235     void AnalysisCycle::PrintSelectionSummary()
236     {
237     // print a summary of all selections
238    
239     for (unsigned int i=0; i<m_selections.size(); ++i){
240     m_selections[i]->printCutFlow();
241     }
242    
243     }
244    
245    
246     void AnalysisCycle::EndInputData( const SInputData& ) throw( SError )
247     {
248    
249     return;
250    
251     }
252    
253     void AnalysisCycle::BeginInputFile( const SInputData& ) throw( SError )
254     {
255     // Connect all variables from the Ntuple file with the ones needed for the analysis.
256     // The different collections that should be loaded are steerable through the XML file.
257     // The variables are commonly stored in the BaseCycleContaincer and can be
258     // accessed afterwards through the ObjectHandler
259    
260     ConnectVariable( "AnalysisTree", "triggerResults" , m_bcc.triggerResults);
261     ConnectVariable( "AnalysisTree", "triggerNames" , m_bcc.triggerNames);
262     if(m_ElectronCollection.size()>0) ConnectVariable( "AnalysisTree", m_ElectronCollection.c_str() ,m_bcc. electrons);
263     if(m_MuonCollection.size()>0) ConnectVariable( "AnalysisTree", m_MuonCollection.c_str() , m_bcc.muons);
264     if(m_TauCollection.size()>0) ConnectVariable( "AnalysisTree", m_TauCollection.c_str() , m_bcc.taus);
265     if(m_JetCollection.size()>0) ConnectVariable( "AnalysisTree", m_JetCollection.c_str() , m_bcc.jets);
266     if(m_PhotonCollection.size()>0) ConnectVariable( "AnalysisTree", m_PhotonCollection.c_str() , m_bcc.photons);
267     if(m_METName.size()>0) ConnectVariable( "AnalysisTree", m_METName.c_str() , m_bcc.met);
268     if(m_PrimaryVertexCollection.size()>0) ConnectVariable( "AnalysisTree", m_PrimaryVertexCollection.c_str() , m_bcc.pvs);
269     if(m_TopJetCollection.size()>0) ConnectVariable( "AnalysisTree", m_TopJetCollection.c_str() , m_bcc.topjets);
270     if(m_PrunedJetCollection.size()>0) ConnectVariable( "AnalysisTree", m_PrunedJetCollection.c_str() , m_bcc.prunedjets);
271     if(m_GenParticleCollection.size()>0) ConnectVariable( "AnalysisTree", m_GenParticleCollection.c_str() , m_bcc.genparticles);
272     if(m_addGenInfo) ConnectVariable( "AnalysisTree", "genInfo" , m_bcc.genInfo);
273     ConnectVariable( "AnalysisTree", "run" , m_bcc.run);
274     ConnectVariable( "AnalysisTree", "luminosityBlock" , m_bcc.luminosityBlock);
275     ConnectVariable( "AnalysisTree" ,"event" ,m_bcc.event);
276     ConnectVariable( "AnalysisTree" ,"isRealData", m_bcc.isRealData);
277     //ConnectVariable( "AnalysisTree" ,"HBHENoiseFilterResult", m_bcc.HBHENoiseFilterResult);
278     ConnectVariable( "AnalysisTree" ,"beamspot_x0", m_bcc.beamspot_x0);
279     ConnectVariable( "AnalysisTree" ,"beamspot_y0", m_bcc.beamspot_y0);
280     ConnectVariable( "AnalysisTree" ,"beamspot_z0", m_bcc.beamspot_z0);
281    
282     ObjectHandler* objs = ObjectHandler::Instance();
283     objs->SetBaseCycleContainer(&m_bcc);
284     objs->SetLumiHandler( LumiHandler() );
285    
286     return;
287    
288     }
289    
290     void AnalysisCycle::ExecuteEvent( const SInputData&, Double_t weight) throw( SError )
291     {
292     // This method performs basic consistency checks, resets the event calculator,
293     // calculates the pile-up weight and performs the good-run selection.
294     // It should always be the first thing to be called in each user analysis.
295    
296     // first thing to do: call reset of event calc
297     EventCalc* calc = EventCalc::Instance();
298     calc->Reset();
299    
300     if(m_bcc.isRealData && m_addGenInfo){
301     m_logger << WARNING<< "Running over real data, but addGenInfo=True in config file?!" << SLogger::endmsg;
302     }
303    
304     //fill list of trigger names
305     if(m_bcc.triggerNames->size()!=0){
306     m_bcc.triggerNames_actualrun = *m_bcc.triggerNames;
307     m_newrun=true;
308     }
309    
310     // generate random run Nr for MC samples (consider luminosity of each run)
311     // e.g. for proper OTX cut in MC, and needs to be done only once per event
312     if( !m_bcc.isRealData && LumiHandler()->IsLumiCalc() ){
313     m_bcc.run = LumiHandler()->GetRandomRunNr();
314     }
315    
316    
317     // store the weight (lumiweight) in the eventcalc class and use it
318    
319     if(m_bcc.genInfo){
320     if(m_puwp){
321     double pu_weight = m_puwp->produceWeight(m_bcc.genInfo);
322     // set the weight in the eventcalc
323     }
324     }
325    
326     //select only good runs
327     if(m_bcc.isRealData && LumiHandler()->IsLumiCalc() ){
328     if( !LumiHandler()->PassGoodRunsList( m_bcc.run, m_bcc.luminosityBlock )) throw SError( SError::SkipEvent );
329     }
330    
331    
332     return;
333    
334     }
335