The Gaudi Framework  master (181af51f)
Loading...
Searching...
No Matches
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
20namespace 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 } } {}
53
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 } } {}
64
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>>>
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
internal class implementing a map of histograms
HistogramMapInternal(OWNER *owner, FormatName &&fname, FormatTitle &&ftitle, typename Histo::AxisTupleType &&allAxis)
constructor with callables for FormatName and FormatTitle
Histo & operator[](Key const &k) const
operator[] method, made thread safe and thus declared const
std::function< Histo &(Key const &)> m_makeHisto
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
Efficient counter implementations for Gaudi.
STL namespace.
generic class implementing a thread safe map of histograms
HistogramMap(OWNER *owner, FormatName &&fname, FormatTitle &&ftitle, AxisType< ND >... allAxis)
HistogramMap(OWNER *owner, FormatName &&fname, FormatTitle &&ftitle, typename Histo::AxisTupleType &&allAxis)
Default formating for histogram names and title, only calling fmt::format on the text given at constr...