Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v38r0 (2143aa4c)
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:
31  using HistoHandler = std::function<void( TFile& file, std::string, std::string, nlohmann::json const& )>;
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  void flush( bool ) override {
50  // File is updated so that multiple sinks can write to the same file
51  // As we are in stop, there is no multithreading so it is safe
52  // As we dropped the file at initialization, no old data from a previous
53  // run may be mixed with new one
54  TFile histoFile( m_fileName.value().c_str(), "UPDATE" );
55  // get all entities, sorted by component and name
57  [this, &histoFile]( std::string const& component, std::string const& name, nlohmann::json const& j ) {
58  auto dim = j.at( "dimension" ).template get<unsigned int>();
59  auto type = j.at( "type" ).template get<std::string>();
60  // cut type after last ':' if there is one. The rest is precision parameter that we do not need here
61  // as ROOT anyway treats everything as doubles in histograms
62  type = type.substr( 0, type.find_last_of( ':' ) );
63  auto saver = m_registry.find( { type, dim } );
64  if ( saver != m_registry.end() ) ( saver->second )( histoFile, component, name, j );
65  } );
66  info() << "Completed update of ROOT histograms in: " << m_fileName.value() << endmsg;
67  }
68 
70  m_registry.emplace( std::piecewise_construct, std::make_tuple( id ), std::make_tuple( func ) );
71  }
72 
73  private:
76 
77  Gaudi::Property<std::string> m_fileName{ this, "FileName", "testHisto.root",
78  "Name of file where to save histograms" };
79  };
80 
81 } // namespace Gaudi::Histograming::Sink
Gaudi::Histograming::Sink::Base::m_fileName
Gaudi::Property< std::string > m_fileName
Definition: Base.h:77
Gaudi::Monitoring::BaseSink
Base class for all Sinks registering to the Monitoring Hub Should be extended by actual Sinks.
Definition: BaseSink.h:40
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
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:86
std::map::emplace
T emplace(T... args)
Gaudi::Histograming::Sink::Base::initialize
StatusCode initialize() override
Definition: Base.h:40
Gaudi::Histograming::Sink::Base::flush
void flush(bool) override
pure virtual method to be defined by children and responsible for flushing current data of the Sink.
Definition: Base.h:49
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:235
Gaudi::Histograming::Sink
Definition: RootHistogramSink.cpp:22
Gaudi::Histograming::Sink::Base::registerHandler
void registerHandler(HistoIdentification const &id, HistoHandler const &func)
Definition: Base.h:69
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
Gaudi::Monitoring::BaseSink::applyToAllSortedEntities
void applyToAllSortedEntities(Callable func) const
applies a callable to all monitoring entities ordered by component the callable will be called once p...
Definition: BaseSink.h:110
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:75
Gaudi::Histograming::Sink::Base::Base
Base(std::string name, ISvcLocator *svcloc)
Definition: Base.h:34
gaudirun.type
type
Definition: gaudirun.py:160
Gaudi::Histograming::Sink::Base
Definition: Base.h:28
BaseSink.h
std::map::end
T end(T... args)
Gaudi::Property< std::string >