ATLAS Offline Software
AnaAlgorithm.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 
7 
8 
9 //
10 // includes
11 //
12 
14 
16 #include <TH1.h>
17 #include <TH2.h>
18 #include <TH3.h>
19 #include <TEfficiency.h>
20 #include <stdexcept>
21 
22 #ifdef XAOD_STANDALONE
26 #endif
27 
28 #ifndef XAOD_STANDALONE
29 #include <GaudiKernel/IIncidentSvc.h>
30 #include <GaudiKernel/ServiceHandle.h>
31 #endif
32 
33 //
34 // method implementations
35 //
36 
37 namespace EL
38 {
40  AnaAlgorithm (const std::string& name,
41  ISvcLocator*
42 #ifndef XAOD_STANDALONE
43  pSvcLocator
44 #endif
45  )
46 #ifdef XAOD_STANDALONE
47  : AsgComponent (name)
48  , m_inputMetaStore (asg::SgTEventMeta::InputStore, nullptr)
49  , m_outputMetaStore (asg::SgTEventMeta::OutputStore, nullptr)
50 #else
51  : AthHistogramAlgorithm (name, pSvcLocator)
52  , m_inputMetaStore ("StoreGateSvc/InputMetaDataStore", name)
53  , m_outputMetaStore ("StoreGateSvc/MetaDataStore", name)
54 #endif
55  {
56 #ifdef XAOD_STANDALONE
57  declareProperty ("RootStreamName", m_treeStreamName = "ANALYSIS",
58  "Name of the stream to put trees into");
59 #endif
60 
61  ANA_MSG_DEBUG ("AnaAlgorithm: " << name);
62  }
63 
64 
65 
67  ~AnaAlgorithm () noexcept
68  {}
69 
70 
71 
73  {
74 #ifdef XAOD_STANDALONE
75  return &m_inputMetaStore;
76 #else
77  return m_inputMetaStore;
78 #endif // XAOD_STANDALONE
79  }
80 
82  {
83 #ifdef XAOD_STANDALONE
84  return &m_inputMetaStore;
85 #else
86  return m_inputMetaStore;
87 #endif // XAOD_STANDALONE
88  }
89 
90 
91 
93  {
94 #ifdef XAOD_STANDALONE
95  return &m_outputMetaStore;
96 #else
97  return m_outputMetaStore;
98 #endif // XAOD_STANDALONE
99  }
100 
102  {
103 #ifdef XAOD_STANDALONE
104  return &m_outputMetaStore;
105 #else
106  return m_outputMetaStore;
107 #endif // XAOD_STANDALONE
108  }
109 
110 
111 
112 #ifdef XAOD_STANDALONE
114  evtStore () const
115  {
116  if (!m_evtStore)
117  throw std::logic_error ("no evtStore set on algorithm " + name());
118  return m_evtStore;
119  }
120 
121 
122 
124  book (const TH1& hist)
125  {
126  histogramWorker()->addOutput (hist.Clone());
127  return ::StatusCode::SUCCESS;
128  }
129 
130 
131 
133  book (const TEfficiency& hist)
134  {
135  histogramWorker()->addOutput (hist.Clone());
136  return ::StatusCode::SUCCESS;
137  }
138 
139 
140 
141  template<> TObject *AnaAlgorithm ::
142  hist<TObject> (const std::string& name) const
143  {
144  return histogramWorker()->getOutputHist (name);
145  }
146 
147 
148 
150  hist2d (const std::string& name) const
151  {
152  return hist<TH2>(name);
153  }
154 
155 
156 
158  hist3d (const std::string& name) const
159  {
160  return hist<TH3>(name);
161  }
162 
163 
164 
165  TEfficiency *AnaAlgorithm ::
166  histeff (const std::string& name) const
167  {
168  return hist<TEfficiency>(name);
169  }
170 
171 
172 
173  IHistogramWorker *AnaAlgorithm ::
174  histogramWorker () const
175  {
176  if (!m_histogramWorker)
177  throw std::logic_error ("no histogram worker set on algorithm " + name());
178  return m_histogramWorker;
179  }
180 
181 
182 
184  book (const TTree& tree)
185  {
187  ANA_CHECK( treeWorker()->addTree( tree, m_treeStreamName ) );
188  return ::StatusCode::SUCCESS;
189  }
190 
191 
192 
193  TTree *AnaAlgorithm ::
194  tree (const std::string& name) const
195  {
196  return treeWorker()->getOutputTree( name, m_treeStreamName );
197  }
198 
199 
200 
201  ITreeWorker *AnaAlgorithm ::
202  treeWorker () const
203  {
204  if( ! m_treeWorker ) {
205  throw std::logic_error( "no tree worker set on algorithm " + name() );
206  }
207  return m_treeWorker;
208  }
209 
210 
211 
212  bool AnaAlgorithm ::
213  filterPassed() const
214  {
215  return filterWorker()->filterPassed();
216  }
217 
218 
219 
220  void AnaAlgorithm ::
221  setFilterPassed (bool val_filterPassed)
222  {
223  filterWorker()->setFilterPassed (val_filterPassed);
224  }
225 
226 
227 
228  IFilterWorker *AnaAlgorithm ::
229  filterWorker () const
230  {
231  if (!m_filterWorker)
232  throw std::logic_error ("no filter worker set on algorithm " + name());
233  return m_filterWorker;
234  }
235 
236 
237 
238  IWorker *AnaAlgorithm ::
239  wk () const
240  {
241  if (!m_wk)
242  throw std::logic_error ("no worker set on algorithm " + name());
243  return m_wk;
244  }
245 #endif
246 
247 
248 
251  {
252  m_hasFileExecute = true;
253 
254 #ifndef XAOD_STANDALONE
255  // Connect to the IncidentSvc:
256  ServiceHandle< IIncidentSvc > incSvc( "IncidentSvc", name() );
257  ATH_CHECK( incSvc.retrieve() );
258 
259  // Set up the right callback, but ensure we don't double-register
260  // if we are called twice
261  incSvc->removeListener( this, IncidentType::BeginInputFile );
262  incSvc->addListener( this, IncidentType::BeginInputFile, 0, true );
263 #endif
264 
265  return StatusCode::SUCCESS;
266  }
267 
268 
269 
272  {
273  m_hasBeginInputFile = true;
274 
275 #ifndef XAOD_STANDALONE
276  // Connect to the IncidentSvc:
277  ServiceHandle< IIncidentSvc > incSvc( "IncidentSvc", name() );
278  ATH_CHECK( incSvc.retrieve() );
279 
280  // Set up the right callback, but ensure we don't double-register
281  // if we are called twice
282  incSvc->removeListener( this, IncidentType::BeginInputFile );
283  incSvc->addListener( this, IncidentType::BeginInputFile, 0, true );
284 #endif
285 
286  return StatusCode::SUCCESS;
287  }
288 
289 
290 
293  {
294  m_hasEndInputFile = true;
295 
296 #ifndef XAOD_STANDALONE
297  // Connect to the IncidentSvc:
298  ServiceHandle< IIncidentSvc > incSvc( "IncidentSvc", name() );
299  ATH_CHECK( incSvc.retrieve() );
300 
301  // Set up the right callback, but ensure we don't double-register
302  // if we are called twice
303  incSvc->removeListener( this, IncidentType::EndInputFile );
304  incSvc->addListener( this, IncidentType::EndInputFile, 0, true );
305 #endif
306 
307  return StatusCode::SUCCESS;
308  }
309 
310 
311 
313  initialize ()
314  {
315  return StatusCode::SUCCESS;
316  }
317 
318 
319 
321  execute ()
322  {
323  return StatusCode::SUCCESS;
324  }
325 
326 
327 
329  finalize ()
330  {
331  return StatusCode::SUCCESS;
332  }
333 
334 
335 
337  print () const
338  {}
339 
340 
341 
343  fileExecute ()
344  {
345  return StatusCode::SUCCESS;
346  }
347 
348 
349 
352  {
353  return StatusCode::SUCCESS;
354  }
355 
356 
357 
359  endInputFile ()
360  {
361  return StatusCode::SUCCESS;
362  }
363 
364 
365 
366 #ifdef XAOD_STANDALONE
369  {
370  return initialize ();
371  }
372 
373 
374 
375  ::StatusCode AnaAlgorithm ::
376  sysExecute ()
377  {
378  return execute ();
379  }
380 
381 
382 
383  ::StatusCode AnaAlgorithm ::
384  sysFinalize ()
385  {
386  return finalize ();
387  }
388 
389 
390 
391  void AnaAlgorithm ::
392  sysPrint ()
393  {
394  print ();
395  }
396 
397 
398 
399  ::StatusCode AnaAlgorithm ::
400  sysFileExecute ()
401  {
402  if (m_hasFileExecute == false)
403  {
404  ANA_MSG_FATAL ("called fileExecute(), though it was not registered");
405  return StatusCode::FAILURE;
406  }
407  return fileExecute ();
408  }
409 
410 
411 
412  ::StatusCode AnaAlgorithm ::
413  sysBeginInputFile ()
414  {
415  if (m_hasBeginInputFile == false)
416  {
417  ANA_MSG_FATAL ("called beginInputFile(), though it was not registered");
418  return StatusCode::FAILURE;
419  }
420  return beginInputFile ();
421  }
422 
423 
424 
425  ::StatusCode AnaAlgorithm ::
426  sysEndInputFile ()
427  {
428  if (m_hasEndInputFile == false)
429  {
430  ANA_MSG_FATAL ("called endInputFile(), though it was not registered");
431  return StatusCode::FAILURE;
432  }
433  return endInputFile ();
434  }
435 
436 
437 
438  void AnaAlgorithm ::
439  setEvtStore (asg::SgTEvent *val_evtStore)
440  {
441  if (m_evtStore)
442  throw std::logic_error ("set evtStore twice on algorithm " + name());
443  m_evtStore = val_evtStore;
445  val_evtStore->event());
447  val_evtStore->event());
448  }
449 
450 
451 
452  void AnaAlgorithm ::
453  setHistogramWorker (IHistogramWorker *val_histogramWorker)
454  {
455  if (m_histogramWorker)
456  throw std::logic_error ("set histogram worker twice on algorithm " + name());
457  m_histogramWorker = val_histogramWorker;
458  }
459 
460 
461 
462  void AnaAlgorithm ::
463  setTreeWorker (ITreeWorker *val_treeWorker)
464  {
465  if( m_treeWorker ) {
466  throw std::logic_error( "set tree worker twice on algorithm " +
467  name() );
468  }
469  m_treeWorker = val_treeWorker;
470  }
471 
472 
473 
474  void AnaAlgorithm ::
475  setFilterWorker (IFilterWorker *val_filterWorker)
476  {
477  if (m_filterWorker)
478  throw std::logic_error ("set filter worker twice on algorithm " + name());
479  m_filterWorker = val_filterWorker;
480  }
481 
482 
483 
484  void AnaAlgorithm ::
485  setWk (IWorker *val_wk)
486  {
487  if (m_wk)
488  throw std::logic_error ("set wk twice on algorithm " + name());
489  m_wk = val_wk;
490  }
491 
492 
493 
494  bool AnaAlgorithm ::
495  hasFileExecute () const noexcept
496  {
497  return m_hasFileExecute;
498  }
499 
500 
501 
502  bool AnaAlgorithm ::
503  hasBeginInputFile () const noexcept
504  {
505  return m_hasBeginInputFile;
506  }
507 
508 
509 
510  bool AnaAlgorithm ::
511  hasEndInputFile () const noexcept
512  {
513  return m_hasEndInputFile;
514  }
515 #endif
516 
517 
518 
519 #ifndef XAOD_STANDALONE
521  handle (const Incident& inc)
522  {
523  if (inc.type() == IncidentType::BeginInputFile)
524  {
527  if (m_hasFileExecute)
529  } else if (inc.type() == IncidentType::EndInputFile)
530  {
531  if (m_hasEndInputFile)
533  } else
534  {
535  ATH_MSG_WARNING( "Unknown incident type received: " << inc.type() );
536  }
537  }
538 #endif
539 }
EL::AnaAlgorithm::requestFileExecute
::StatusCode requestFileExecute()
register this algorithm to have an implementation of fileexecute
Definition: AnaAlgorithm.cxx:250
asg::SgTEventMeta::OutputStore
@ OutputStore
This store is used to access the output metadata.
Definition: SgTEventMeta.h:50
EL::AnaAlgorithm::m_hasBeginInputFile
bool m_hasBeginInputFile
the value of hasBeginInputFile
Definition: AnaAlgorithm.h:590
AthHistogramming::hist2d
TH2 * hist2d(const std::string &histName, const std::string &tDir="", const std::string &stream="")
Simplify the retrieval of registered 2-d histograms.
Definition: AthHistogramming.h:369
EL::AnaAlgorithm::endInputFile
virtual ::StatusCode endInputFile()
perform the action for the end of an input file
Definition: AnaAlgorithm.cxx:359
AthHistogramming::book
StatusCode book(const TH1 &hist, const std::string &tDir="", const std::string &stream="")
Simplify the booking and registering (into THistSvc) of histograms.
Definition: AthHistogramming.h:303
EL::AnaAlgorithm::m_inputMetaStore
MetaStore_t m_inputMetaStore
Object accessing the input metadata store.
Definition: AnaAlgorithm.h:551
EL::AnaAlgorithm::m_outputMetaStore
MetaStore_t m_outputMetaStore
Object accessing the output metadata store.
Definition: AnaAlgorithm.h:555
EL::AnaAlgorithm::inputMetaStore
ConstMetaStorePtr_t inputMetaStore() const
Definition: AnaAlgorithm.cxx:72
plotmaker.hist
hist
Definition: plotmaker.py:148
tree
TChain * tree
Definition: tile_monitor.h:30
ITreeWorker.h
EL::AnaAlgorithm::execute
virtual ::StatusCode execute()
execute this algorithm
Definition: AnaAlgorithm.cxx:321
EL::AnaAlgorithm::beginInputFile
virtual ::StatusCode beginInputFile()
perform the action for the beginning of an input file
Definition: AnaAlgorithm.cxx:351
AthCommonDataStore< AthCommonMsg< Algorithm > >::m_evtStore
StoreGateSvc_t m_evtStore
Pointer to StoreGate (event store by default)
Definition: AthCommonDataStore.h:390
EL::AnaAlgorithm::~AnaAlgorithm
virtual ~AnaAlgorithm() noexcept
standard (virtual) destructor
Definition: AnaAlgorithm.cxx:67
ANA_CHECK
#define ANA_CHECK(EXP)
check whether the given expression was successful
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:324
EL::AnaAlgorithm::print
virtual void print() const
print the state of the algorithm
Definition: AnaAlgorithm.cxx:337
const
bool const RAWDATA *ch2 const
Definition: LArRodBlockPhysicsV0.cxx:562
EL::AnaAlgorithm::AnaAlgorithm
AnaAlgorithm(const std::string &name, ISvcLocator *pSvcLocator)
constructor with parameters
Definition: AnaAlgorithm.cxx:40
EL::AnaAlgorithm::fileExecute
virtual ::StatusCode fileExecute()
perform the action exactly once for each file in the dataset
Definition: AnaAlgorithm.cxx:343
ANA_CHECK_THROW
#define ANA_CHECK_THROW(EXP)
check whether the given expression was successful, throwing an exception on failure
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:339
AnaAlgorithm.h
AthCommonDataStore< AthCommonMsg< Algorithm > >::evtStore
ServiceHandle< StoreGateSvc > & evtStore()
The standard StoreGateSvc (event store) Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:85
AthHistogramming::hist3d
TH3 * hist3d(const std::string &histName, const std::string &tDir="", const std::string &stream="")
Simplify the retrieval of registered 3-d histograms.
Definition: AthHistogramming.h:386
EL::AnaAlgorithm::handle
void handle(const Incident &inc)
receive the given incident
Definition: AnaAlgorithm.cxx:521
EL::AnaAlgorithm::finalize
virtual ::StatusCode finalize()
finalize this algorithm
Definition: AnaAlgorithm.cxx:329
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
EL::AnaAlgorithm::requestBeginInputFile
::StatusCode requestBeginInputFile()
register this algorithm to have an implementation of beginInputFile
Definition: AnaAlgorithm.cxx:271
MessageCheck.h
macros for messaging and checking status codes
TH3
Definition: rootspy.cxx:440
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
EL
This module defines the arguments passed from the BATCH driver to the BATCH worker.
Definition: AlgorithmWorkerData.h:24
EL::AnaAlgorithm::m_hasFileExecute
bool m_hasFileExecute
the value of hasFileExecute
Definition: AnaAlgorithm.h:586
IFilterWorker.h
AthHistogramAlgorithm
Definition: AthHistogramAlgorithm.h:32
TH2
Definition: rootspy.cxx:373
ANA_MSG_FATAL
#define ANA_MSG_FATAL(xmsg)
Macro printing fatal messages.
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:296
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:192
EL::AnaAlgorithm::requestEndInputFile
::StatusCode requestEndInputFile()
register this algorithm to have an implementation of endInputFile
Definition: AnaAlgorithm.cxx:292
AthHistogramAlgorithm::sysInitialize
virtual StatusCode sysInitialize()
Initialization method invoked by the framework.
Definition: AthHistogramAlgorithm.cxx:75
asg::SgTEvent
Wrapper for TEvent to make it look like StoreGate.
Definition: SgTEvent.h:44
asg::SgTEventMeta::InputStore
@ InputStore
This store is used to access the input metadata.
Definition: SgTEventMeta.h:49
asg::SgTEventMeta
Wrapper class providing StoreGate-like access to metadata in ROOT.
Definition: SgTEventMeta.h:44
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
ANA_CHECK_SET_TYPE
#define ANA_CHECK_SET_TYPE(TYPE)
set the type for ANA_CHECK to report failures
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:314
TH1
Definition: rootspy.cxx:268
EL::AnaAlgorithm::m_hasEndInputFile
bool m_hasEndInputFile
the value of hasEndInputFile
Definition: AnaAlgorithm.h:594
test_pyathena.filterPassed
filterPassed
now, add a sequencer: it will only execute seqalg1 and 2 and never 3
Definition: test_pyathena.py:38
declareProperty
#define declareProperty(n, p, h)
Definition: BaseFakeBkgTool.cxx:15
AthHistogramming::hist
TH1 * hist(const std::string &histName, const std::string &tDir="", const std::string &stream="")
Simplify the retrieval of registered histograms of any type.
Definition: AthHistogramming.cxx:198
asg::SgTEvent::event
xAOD::TEvent * event() const
Return the underlying event manager.
Definition: SgTEvent.cxx:24
EL::AnaAlgorithm::initialize
virtual ::StatusCode initialize()
initialize this algorithm
Definition: AnaAlgorithm.cxx:313
EL::AnaAlgorithm::outputMetaStore
ConstMetaStorePtr_t outputMetaStore() const
Definition: AnaAlgorithm.cxx:92
AthHistogramming::tree
TTree * tree(const std::string &treeName, const std::string &tDir="", const std::string &stream="")
Simplify the retrieval of registered TTrees.
Definition: AthHistogramming.cxx:378
IHistogramWorker.h
ServiceHandle< StoreGateSvc >
ANA_MSG_DEBUG
#define ANA_MSG_DEBUG(xmsg)
Macro printing debug messages.
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:288