The Gaudi Framework  v39r0 (5b8b5eda)
HistogramWrapper.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-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 "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 #pragma once
12 
14 
15 namespace Gaudi::Accumulators {
16 
38  template <typename HistogramType,
39  typename Seq =
40  std::make_integer_sequence<unsigned int, std::tuple_size_v<typename HistogramType::AxisTupleType>>>
42  template <typename HistogramType, unsigned int... ND>
43  class HistogramWrapperInternal<HistogramType, std::integer_sequence<unsigned int, ND...>> {
44  public:
45  using AxisTupleType = typename HistogramType::AxisTupleType;
46  using AxisArithmeticType = typename HistogramType::AxisArithmeticType;
47  template <unsigned int I>
48  using AxisType = std::tuple_element_t<I, AxisTupleType>;
49 
51  template <typename OWNER>
52  HistogramWrapperInternal( OWNER* owner, std::string const& name, std::string const& title = "",
53  typename HistogramType::AxisTupleType axis = {}, bool doNotInitialize = false )
54  : m_name{ name }, m_title{ title }, m_axis{ axis } {
55  // Create associated properties
56  owner->declareProperty( titlePropertyName(), m_title, fmt::format( "Title of histogram {}", name ) )
57  ->template setOwnerType<OWNER>();
58  ( owner
59  ->declareProperty( axisPropertyName<ND>(), std::get<ND>( m_axis ),
60  fmt::format( "Axis {} of histogram {}", ND, name ) )
61  ->template setOwnerType<OWNER>(),
62  ... );
63  // register creation of the Histogram at initialization time
64  if ( !doNotInitialize ) {
65  if ( owner->FSMState() >= Gaudi::StateMachine::INITIALIZED ) {
66  // if the owner is already initialized (e.g. the histogram is being created during `start()`)
67  // it is too late to register the callback
68  createHistogram( *owner );
69  } else {
70  owner->registerCallBack( StateMachine::INITIALIZE, [this, owner]() { createHistogram( *owner ); } );
71  }
72  }
73  }
76  template <typename OWNER>
77  HistogramWrapperInternal( OWNER const* owner, std::string const& name, std::string const& title = "",
78  typename HistogramType::AxisTupleType axis = {} )
79  : m_name{ name }, m_title{ title }, m_axis{ axis } {
80  createHistogram( *owner );
81  }
83  template <typename OWNER>
84  HistogramWrapperInternal( OWNER* owner, std::string const& name, std::string const& title, AxisType<ND>... allAxis )
85  : HistogramWrapperInternal( owner, name, title, std::make_tuple( allAxis... ) ) {}
86 
88  [[nodiscard]] auto operator[]( typename HistogramType::AxisTupleArithmeticType&& v ) {
89  if ( !m_histo ) {
90  throw std::logic_error( fmt::format( "Histogram {} is used before being initialized", m_name ) );
91  }
92  return m_histo.value()[std::forward<typename HistogramType::AxisTupleArithmeticType>( v )];
93  }
94 
96  template <typename OWNER>
97  void createHistogram( OWNER& owner ) {
98  m_histo.emplace( &owner, m_name, m_title, m_axis );
99  }
100 
102  if ( !h.m_histo ) {
103  throw std::logic_error( fmt::format( "Histogram {} is converted to json before being initialized", h.m_name ) );
104  }
105  j = h.m_histo.value();
106  }
107 
108  // set directly some properties, only if histogram was not yet created
109  void setTitle( std::string const& title ) {
110  if ( m_histo )
111  throw std::logic_error(
112  fmt::format( "Cannot modify title of histogram {} after it has been initialized", m_name ) );
113  m_title = title;
114  }
115  template <unsigned int N>
116  void setAxis( std::tuple_element_t<N, typename HistogramType::AxisTupleType> const& axis ) {
117  if ( m_histo )
118  throw std::logic_error(
119  fmt::format( "Cannot modify axis {} of histogram {} after it has been initialized", N, m_name ) );
120  std::get<N>( m_axis ) = axis;
121  }
122 
123  void reset() { m_histo.reset(); }
124 
125  // wrapping some methods of the underlyoing histogram
126  auto buffer() {
127  if ( !m_histo )
128  throw std::logic_error( fmt::format( "`buffer()` called on histogram {} before being initialized", m_name ) );
129  return m_histo->buffer();
130  }
131 
132  private:
134  // properties are used as identifiers in python and thus cannot anything alse than _, letters and numbers
135  // we thus replace anything else with '_' in the property names
136  std::string name = m_name;
138  begin( name ), end( name ), []( auto& c ) { return !std::isalnum( c ); }, '_' );
139  return name;
140  }
141  std::string titlePropertyName() const { return fmt::format( "{}_Title", basePropertyName() ); }
142  template <unsigned int N>
144  return fmt::format( "{}_Axis{}", basePropertyName(), N );
145  }
146 
147  // Members of the custom histogrem
148  std::string m_name{};
149  std::string m_title{};
150  typename HistogramType::AxisTupleType m_axis{};
151  std::optional<HistogramType> m_histo{};
152  };
153 
154  template <typename HistogramType>
156 
157 } // namespace Gaudi::Accumulators
Gaudi::Accumulators::HistogramWrapperInternal< HistogramType, std::integer_sequence< unsigned int, ND... > >::to_json
friend void to_json(nlohmann::json &j, HistogramWrapperInternal const &h)
Definition: HistogramWrapper.h:101
std::string
STL class.
IOTest.N
N
Definition: IOTest.py:112
Gaudi::Accumulators::HistogramWrapperInternal< HistogramType, std::integer_sequence< unsigned int, ND... > >::titlePropertyName
std::string titlePropertyName() const
Definition: HistogramWrapper.h:141
Gaudi::Accumulators::HistogramWrapperInternal< HistogramType, std::integer_sequence< unsigned int, ND... > >::AxisTupleType
typename HistogramType::AxisTupleType AxisTupleType
Definition: HistogramWrapper.h:45
jsonFromLHCbLog.json
json
Definition: jsonFromLHCbLog.py:86
Gaudi::Accumulators::HistogramWrapperInternal< HistogramType, std::integer_sequence< unsigned int, ND... > >::HistogramWrapperInternal
HistogramWrapperInternal(OWNER *owner, std::string const &name, std::string const &title, AxisType< ND >... allAxis)
constructor with more natural syntax for axis
Definition: HistogramWrapper.h:84
Gaudi::Accumulators::HistogramWrapperInternal< HistogramType, std::integer_sequence< unsigned int, ND... > >::HistogramWrapperInternal
HistogramWrapperInternal(OWNER *owner, std::string const &name, std::string const &title="", typename HistogramType::AxisTupleType axis={}, bool doNotInitialize=false)
constructor, only creates a set of Properties
Definition: HistogramWrapper.h:52
std::isalnum
T isalnum(T... args)
gaudirun.c
c
Definition: gaudirun.py:525
Gaudi::Accumulators::HistogramWrapperInternal< HistogramType, std::integer_sequence< unsigned int, ND... > >::setTitle
void setTitle(std::string const &title)
Definition: HistogramWrapper.h:109
std::replace_if
T replace_if(T... args)
Gaudi::StateMachine::INITIALIZE
@ INITIALIZE
Definition: StateMachine.h:35
Gaudi::Utils::begin
AttribStringParser::Iterator begin(const AttribStringParser &parser)
Definition: AttribStringParser.h:136
ProduceConsume.j
j
Definition: ProduceConsume.py:101
AlgSequencer.h
h
Definition: AlgSequencer.py:31
Gaudi::Accumulators::HistogramWrapperInternal< HistogramType, std::integer_sequence< unsigned int, ND... > >::reset
void reset()
Definition: HistogramWrapper.h:123
std::logic_error
STL class.
format
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:119
Gaudi::Accumulators::HistogramWrapperInternal< HistogramType, std::integer_sequence< unsigned int, ND... > >::HistogramWrapperInternal
HistogramWrapperInternal(OWNER const *owner, std::string const &name, std::string const &title="", typename HistogramType::AxisTupleType axis={})
constructor with const owner (i.e.
Definition: HistogramWrapper.h:77
Gaudi::Accumulators::HistogramWrapperInternal< HistogramType, std::integer_sequence< unsigned int, ND... > >::basePropertyName
std::string basePropertyName() const
Definition: HistogramWrapper.h:133
Gaudi::Accumulators
Definition: CounterArray.h:18
Gaudi::Accumulators::HistogramWrapperInternal< HistogramType, std::integer_sequence< unsigned int, ND... > >::operator[]
auto operator[](typename HistogramType::AxisTupleArithmeticType &&v)
override of operator[] with extra checking that initialization happened
Definition: HistogramWrapper.h:88
ConditionsStallTest.name
name
Definition: ConditionsStallTest.py:77
Gaudi::Accumulators::HistogramWrapperInternal
A Wrapper of a static Histogram base class using Properties to define title and axis.
Definition: HistogramWrapper.h:41
std
STL namespace.
Gaudi::Accumulators::HistogramWrapperInternal< HistogramType, std::integer_sequence< unsigned int, ND... > >::setAxis
void setAxis(std::tuple_element_t< N, typename HistogramType::AxisTupleType > const &axis)
Definition: HistogramWrapper.h:116
Gaudi::StateMachine::INITIALIZED
@ INITIALIZED
Definition: StateMachine.h:25
Gaudi::Accumulators::HistogramWrapperInternal< HistogramType, std::integer_sequence< unsigned int, ND... > >::createHistogram
void createHistogram(OWNER &owner)
creation of the internal histogram, from the properties
Definition: HistogramWrapper.h:97
Properties.v
v
Definition: Properties.py:122
IOTest.end
end
Definition: IOTest.py:125
Gaudi::Accumulators::HistogramWrapperInternal< HistogramType, std::integer_sequence< unsigned int, ND... > >::AxisType
std::tuple_element_t< I, AxisTupleType > AxisType
Definition: HistogramWrapper.h:48
Gaudi::Accumulators::HistogramWrapperInternal< HistogramType, std::integer_sequence< unsigned int, ND... > >::buffer
auto buffer()
Definition: HistogramWrapper.h:126
Gaudi::Accumulators::HistogramWrapperInternal< HistogramType, std::integer_sequence< unsigned int, ND... > >::axisPropertyName
std::string axisPropertyName() const
Definition: HistogramWrapper.h:143
StaticHistogram.h
Gaudi::Accumulators::HistogramWrapperInternal< HistogramType, std::integer_sequence< unsigned int, ND... > >::AxisArithmeticType
typename HistogramType::AxisArithmeticType AxisArithmeticType
Definition: HistogramWrapper.h:46