Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  master (d98a2936)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
HistogramMap.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2025 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 #include <fmt/format.h>
16 
17 #include <map>
18 #include <mutex>
19 
20 namespace Gaudi::Accumulators {
21 
22  namespace details {
23 
28  template <typename Key>
30  std::string text;
31  FormatHistDefaultT( std::string_view t ) : text{ t } {}
32  auto operator()( Key k ) { return fmt::format( fmt::runtime( text ), k ); }
33  };
34 
40  template <typename Key, typename Histo>
42  public:
44  template <typename OWNER, std::invocable<Key const&> FormatName, std::invocable<Key const&> FormatTitle>
45  HistogramMapInternal( OWNER* owner, FormatName&& fname, FormatTitle&& ftitle,
46  typename Histo::AxisTupleType&& allAxis )
47  : m_makeHisto{ [owner, fname, ftitle, allAxis, this]( Key const& k ) -> Histo& {
48  return m_map
49  .emplace( std::piecewise_construct, std::forward_as_tuple( k ),
50  std::forward_as_tuple( owner, fname( k ), ftitle( k ), allAxis ) )
51  .first->second;
52  } } {}
54  template <typename OWNER>
55  HistogramMapInternal( OWNER* owner, std::string_view name, std::string_view title,
56  typename Histo::AxisTupleType&& allAxis )
57  : m_makeHisto{ [owner, name, title, allAxis, this]( Key const& k ) -> Histo& {
58  return m_map
59  .emplace( std::piecewise_construct, std::forward_as_tuple( k ),
60  std::forward_as_tuple( owner, FormatHistDefaultT<Key>( name )( k ),
61  FormatHistDefaultT<Key>( title )( k ), allAxis ) )
62  .first->second;
63  } } {}
65  Histo& operator[]( Key const& k ) const {
66  std::scoped_lock lock{ m_mapLock };
67  auto it = m_map.find( k );
68  if ( it != m_map.end() ) return it->second;
69  return m_makeHisto( k );
70  }
71 
72  private:
73  mutable std::map<Key, Histo> m_map;
74  mutable std::mutex m_mapLock{};
75  std::function<Histo&( Key const& )> m_makeHisto;
76  // operator[],
77  };
78  } // namespace details
79 
123  template <typename Key, typename Histo,
124  typename Seq = std::make_integer_sequence<unsigned int, std::tuple_size_v<typename Histo::AxisTupleType>>>
125  struct HistogramMap;
126  template <typename Key, typename Histo, unsigned int... ND>
127  struct HistogramMap<Key, Histo, std::integer_sequence<unsigned int, ND...>>
128  : details::HistogramMapInternal<Key, Histo> {
129  template <typename OWNER, typename FormatName, typename FormatTitle>
130  HistogramMap( OWNER* owner, FormatName&& fname, FormatTitle&& ftitle, typename Histo::AxisTupleType&& allAxis )
131  : details::HistogramMapInternal<Key, Histo>( owner, fname, ftitle,
132  std::forward<typename Histo::AxisTupleType>( allAxis ) ) {}
133 
134  template <unsigned int I>
135  using AxisType = std::tuple_element_t<I, typename Histo::AxisTupleType>;
136 
137  template <typename OWNER, typename FormatName, typename FormatTitle>
138  HistogramMap( OWNER* owner, FormatName&& fname, FormatTitle&& ftitle, AxisType<ND>... allAxis )
139  : HistogramMap( owner, fname, ftitle, std::make_tuple( allAxis... ) ) {}
140  };
141 
142 } // namespace Gaudi::Accumulators
Gaudi::Accumulators::details::HistogramMapInternal::m_makeHisto
std::function< Histo &(Key const &)> m_makeHisto
Definition: HistogramMap.h:75
GaudiPartProp.decorators.std
std
Definition: decorators.py:32
Gaudi::Accumulators::HistogramMap< Key, Histo, std::integer_sequence< unsigned int, ND... > >::HistogramMap
HistogramMap(OWNER *owner, FormatName &&fname, FormatTitle &&ftitle, AxisType< ND >... allAxis)
Definition: HistogramMap.h:138
StringKeyEx.Key
Key
Definition: StringKeyEx.py:33
Gaudi::Accumulators::details::HistogramMapInternal::HistogramMapInternal
HistogramMapInternal(OWNER *owner, std::string_view name, std::string_view title, typename Histo::AxisTupleType &&allAxis)
constructor for strings, FormatHistDefaultT is used as the default callable
Definition: HistogramMap.h:55
bug_34121.t
t
Definition: bug_34121.py:31
Gaudi::Accumulators::details::HistogramMapInternal::operator[]
Histo & operator[](Key const &k) const
operator[] method, made thread safe and thus declared const
Definition: HistogramMap.h:65
Gaudi::Accumulators::HistogramMap
generic class implementing a thread safe map of histograms
Definition: HistogramMap.h:125
details
Definition: AnyDataWrapper.h:19
Gaudi::Accumulators::HistogramMap< Key, Histo, std::integer_sequence< unsigned int, ND... > >::HistogramMap
HistogramMap(OWNER *owner, FormatName &&fname, FormatTitle &&ftitle, typename Histo::AxisTupleType &&allAxis)
Definition: HistogramMap.h:130
Gaudi::Accumulators::details::HistogramMapInternal::m_mapLock
std::mutex m_mapLock
Definition: HistogramMap.h:74
Gaudi::Accumulators::details::HistogramMapInternal::m_map
std::map< Key, Histo > m_map
Definition: HistogramMap.h:73
format
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:93
Gaudi::Accumulators::details::FormatHistDefaultT::FormatHistDefaultT
FormatHistDefaultT(std::string_view t)
Definition: HistogramMap.h:31
Gaudi::Accumulators
Definition: CounterArray.h:18
Gaudi::Accumulators::details::FormatHistDefaultT::text
std::string text
Definition: HistogramMap.h:30
ConditionsStallTest.name
name
Definition: ConditionsStallTest.py:77
Gaudi::Accumulators::details::FormatHistDefaultT
Default formating for histogram names and title, only calling fmt::format on the text given at constr...
Definition: HistogramMap.h:29
Gaudi::Accumulators::details::HistogramMapInternal::HistogramMapInternal
HistogramMapInternal(OWNER *owner, FormatName &&fname, FormatTitle &&ftitle, typename Histo::AxisTupleType &&allAxis)
constructor with callables for FormatName and FormatTitle
Definition: HistogramMap.h:45
Gaudi::Accumulators::HistogramMap< Key, Histo, std::integer_sequence< unsigned int, ND... > >::AxisType
std::tuple_element_t< I, typename Histo::AxisTupleType > AxisType
Definition: HistogramMap.h:135
Gaudi::Accumulators::details::FormatHistDefaultT::operator()
auto operator()(Key k)
Definition: HistogramMap.h:32
StaticHistogram.h
Gaudi::Accumulators::details::HistogramMapInternal
internal class implementing a map of histograms
Definition: HistogramMap.h:41