The Gaudi Framework  master (37c0b60a)
HistogramWriterAlg.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 2024 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "COPYING". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #include <Gaudi/Algorithm.h>
13 #include <TH1F.h>
14 
17  public:
19 
20  StatusCode initialize() override {
21  return Gaudi::Algorithm::initialize().andThen( [this]() {
22  m_fileSvc = service<Gaudi::Interfaces::IFileSvc>( "FileSvc" );
23  if ( !m_fileSvc ) {
24  error() << "Failed to retrieve FileSvc" << endmsg;
25  return StatusCode::FAILURE;
26  }
27 
28  m_file = m_fileSvc->getFile( "Histogram" );
29  if ( !m_file ) {
30  error() << "Failed to retrieve TFile" << endmsg;
31  return StatusCode::FAILURE;
32  }
33 
34  m_hist = std::make_unique<TH1F>( "FileSvcRandomHist", "Random Numbers", 100, 0, 100 );
35 
36  return StatusCode::SUCCESS;
37  } );
38  }
39 
40  StatusCode execute( const EventContext& ) const override {
41  m_hist->Fill( rand() % 100 );
42  return StatusCode::SUCCESS;
43  }
44 
45  StatusCode finalize() override {
46  m_hist->Write();
48  }
49 
50  private:
54  };
55 
57 } // namespace Gaudi::TestSuite::FileSvc
std::string
STL class.
std::shared_ptr< TFile >
Gaudi::TestSuite::FileSvc::HistogramWriterAlg::initialize
StatusCode initialize() override
Definition: HistogramWriterAlg.cpp:20
StatusCode::andThen
StatusCode andThen(F &&f, ARGS &&... args) const
Chain code blocks making the execution conditional a success result.
Definition: StatusCode.h:163
Gaudi::TestSuite::FileSvc::HistogramWriterAlg
Definition: HistogramWriterAlg.cpp:16
ISvcLocator
Definition: ISvcLocator.h:46
Gaudi::Algorithm::initialize
StatusCode initialize() override
the default (empty) implementation of IStateful::initialize() method
Definition: Algorithm.h:178
Gaudi::TestSuite::FileSvc
Definition: HistogramWriterAlg.cpp:15
Gaudi::TestSuite::FileSvc::HistogramWriterAlg::execute
StatusCode execute(const EventContext &) const override
Definition: HistogramWriterAlg.cpp:40
Gaudi::TestSuite::FileSvc::HistogramWriterAlg::m_fileSvc
Gaudi::Interfaces::IFileSvc * m_fileSvc
Definition: HistogramWriterAlg.cpp:51
StatusCode
Definition: StatusCode.h:65
IFileSvc.h
Gaudi::Algorithm
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:90
Algorithm.h
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
cpluginsvc.n
n
Definition: cpluginsvc.py:234
Gaudi::Algorithm::finalize
StatusCode finalize() override
the default (empty) implementation of IStateful::finalize() method
Definition: Algorithm.h:184
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
gaudirun.l
dictionary l
Definition: gaudirun.py:583
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginServiceV1.h:46
EventContext
Definition: EventContext.h:34
Gaudi::TestSuite::FileSvc::HistogramWriterAlg::HistogramWriterAlg
HistogramWriterAlg(const std::string &n, ISvcLocator *l)
Definition: HistogramWriterAlg.cpp:18
Gaudi::TestSuite::FileSvc::HistogramWriterAlg::m_hist
std::unique_ptr< TH1F > m_hist
Definition: HistogramWriterAlg.cpp:53
Gaudi::Interfaces::IFileSvc::getFile
virtual std::shared_ptr< TFile > getFile(const std::string &identifier)=0
Gaudi::Interfaces::IFileSvc
Interface for a component that manages file access within Gaudi applications.
Definition: IFileSvc.h:27
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
std::unique_ptr< TH1F >
Gaudi::TestSuite::FileSvc::HistogramWriterAlg::finalize
StatusCode finalize() override
Definition: HistogramWriterAlg.cpp:45
Gaudi::TestSuite::FileSvc::HistogramWriterAlg::m_file
std::shared_ptr< TFile > m_file
Definition: HistogramWriterAlg.cpp:52