The Gaudi Framework  v36r13 (995e4364)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Base.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2023 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 "LICENSE". *
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 
12 #include <Gaudi/BaseSink.h>
13 #include <Gaudi/MonitoringHub.h>
14 #include <TFile.h>
15 #include <filesystem>
16 #include <map>
17 #include <nlohmann/json.hpp>
18 #include <string>
19 #include <vector>
20 
21 namespace Gaudi::Histograming::Sink {
22 
23  /*
24  * a Base class for Root related Sinks dealing with Histograms.
25  *
26  * provides the common method plus a generic way of registering handler for different types
27  */
28  class Base : public Monitoring::BaseSink {
29  public:
33 
34  Base( std::string name, ISvcLocator* svcloc ) : Monitoring::BaseSink( name, svcloc ) {
35  // only deal with histograms
36  setProperty( "TypesToSave", std::vector<std::string>{ "histogram:.*" } )
37  .orThrow( "Unable to set typesToSaveProperty", "Histograming::Sink::Base" );
38  }
39 
40  StatusCode initialize() override {
41  return BaseSink::initialize().andThen( [&] {
42  // empty output file if it exists, as we will update it at the end
43  // This allows multiple Sinks to write to the same ROOT file
44  std::filesystem::remove( m_fileName.value() );
45  info() << "Writing ROOT histograms to: " << m_fileName.value() << endmsg;
46  } );
47  }
48 
49  StatusCode stop() override {
50  return Service::stop().andThen( [&] {
51  // File is updated so that multiple sinks can write to the same file
52  // As we are in stop, there is no multithreading so it is safe
53  // As we dropped the file at initialization, no old data from a previous
54  // run may be mixed with new one
55  TFile histoFile( m_fileName.value().c_str(), "UPDATE" );
57  [&histoFile, this]( auto& ent ) {
58  auto j = ent.toJSON();
59  auto dim = j.at( "dimension" ).template get<unsigned int>();
60  auto type = j.at( "type" ).template get<std::string>();
61  // cut type after last ':' if there is one. The rest is precision parameter that we do not need here
62  // as ROOT anyway treats everything as doubles in histograms
63  type = type.substr( 0, type.find_last_of( ':' ) );
64  auto saver = m_registry.find( { type, dim } );
65  if ( saver != m_registry.end() ) ( saver->second )( histoFile, ent.component, ent.name, j );
66  },
67  true );
68  info() << "Completed update of ROOT histograms in: " << m_fileName.value() << endmsg;
69  return StatusCode::SUCCESS;
70  } );
71  }
72 
74  m_registry.emplace( std::piecewise_construct, std::make_tuple( id ), std::make_tuple( func ) );
75  }
76 
77  private:
80 
81  Gaudi::Property<std::string> m_fileName{ this, "FileName", "testHisto.root",
82  "Name of file where to save histograms" };
83  };
84 
85 } // namespace Gaudi::Histograming::Sink
Gaudi::Histograming::Sink::Base::m_fileName
Gaudi::Property< std::string > m_fileName
Definition: Base.h:81
Gaudi::Monitoring::BaseSink
Base class for all Sinks registering to the Monitoring Hub.
Definition: BaseSink.h:27
std::make_tuple
T make_tuple(T... args)
std::string
STL class.
StatusCode::andThen
StatusCode andThen(F &&f, ARGS &&... args) const
Chain code blocks making the execution conditional a success result.
Definition: StatusCode.h:163
MonitoringHub.h
std::pair
Gaudi::Histograming::Sink::Base::stop
StatusCode stop() override
Definition: Base.h:49
std::vector< std::string >
std::map::find
T find(T... args)
PropertyHolder< CommonMessaging< implements< IService, IProperty, IStateful > > >::setProperty
StatusCode setProperty(const Gaudi::Details::PropertyBase &p)
Set the property from a property.
Definition: IProperty.h:39
ISvcLocator
Definition: ISvcLocator.h:46
jsonFromLHCbLog.json
json
Definition: jsonFromLHCbLog.py:87
std::map::emplace
T emplace(T... args)
Gaudi::Histograming::Sink::Base::initialize
StatusCode initialize() override
Definition: Base.h:40
std::function
Service::name
const std::string & name() const override
Retrieve name of the service
Definition: Service.cpp:332
StatusCode
Definition: StatusCode.h:65
ProduceConsume.j
j
Definition: ProduceConsume.py:101
GaudiPluginService.cpluginsvc.func
func
Definition: cpluginsvc.py:236
Gaudi::Histograming::Sink
Definition: RootHistogramSink.cpp:22
Gaudi::Histograming::Sink::Base::registerHandler
void registerHandler(HistoIdentification const &id, HistoHandler const &func)
Definition: Base.h:73
Gaudi::Property::value
const ValueType & value() const
Definition: Property.h:239
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:203
std::map< HistoIdentification, HistoHandler >
Gaudi::Histograming::Sink::Base::m_registry
HistoRegistry m_registry
map of supported type and the way to handle them
Definition: Base.h:79
Gaudi::Histograming::Sink::Base::Base
Base(std::string name, ISvcLocator *svcloc)
Definition: Base.h:34
gaudirun.type
type
Definition: gaudirun.py:162
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
Service::stop
StatusCode stop() override
Definition: Service.cpp:181
Gaudi::Histograming::Sink::Base
Definition: Base.h:28
BaseSink.h
std::map::end
T end(T... args)
Gaudi::Monitoring::BaseSink::applytoAllEntities
void applytoAllEntities(Callable func, bool sortFirst=false)
applies a callable to all monitoring entities
Definition: BaseSink.h:58
Gaudi::Property< std::string >