The Gaudi Framework  v33r1 (b1225454)
Histogram.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 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 
13 #include <Gaudi/Accumulators.h>
14 #include <ROOT/RHist.hxx>
15 #include <boost/format.hpp>
16 #include <boost/histogram/algorithm/sum.hpp>
17 #include <boost/histogram/fwd.hpp>
18 
20 
21  namespace Details {
23  template <typename HistogramType>
24  struct HistogramAdapter {};
25 
27  template <int DIMENSIONS, class PRECISION, template <int D_, class P_> class... STAT>
28  struct HistogramAdapter<ROOT::Experimental::RHist<DIMENSIONS, PRECISION, STAT...>> {
29  using histogram_t = ROOT::Experimental::RHist<DIMENSIONS, PRECISION, STAT...>;
30  using increment_t = typename histogram_t::CoordArray_t;
31 
32  HistogramAdapter( histogram_t&& h ) : hist{std::move( h )} {}
34 
37  template <class... Ts>
38  auto operator()( const Ts&... ts ) {
39  return hist.Fill( ts... );
40  }
41  template <class... Ts>
42  auto fill( const Ts&... ts ) {
43  return hist.Fill( ts... );
44  }
45  auto operator+=( const increment_t& x ) { return hist.Fill( x ); }
47 
49  template <typename stream>
50  stream& printImpl( stream& o, bool tableFormat ) const {
51  auto fmt = boost::format{tableFormat ? "H%|10d| |" : "#=%|-7lu|"};
52  return o << fmt % hist.GetEntries();
53  }
55  operator bool() const { return hist.GetEntries(); }
56  };
57 
59  template <class Axes, class Storage>
60  struct HistogramAdapter<boost::histogram::histogram<Axes, Storage>> {
61  using histogram_t = boost::histogram::histogram<Axes, Storage>;
62 
63  HistogramAdapter( histogram_t&& h ) : hist{std::move( h )} {}
65 
68  template <class... Ts>
69  auto operator()( const Ts&... ts ) {
70  filled = true;
71  return hist( ts... );
72  }
73  template <class... Ts>
74  auto fill( const Ts&... ts ) {
75  filled = true;
76  return hist( ts... );
77  }
78  template <class T>
79  auto operator+=( const T& x ) {
80  filled = true;
81  return hist( x );
82  }
84 
86  template <typename stream>
87  stream& printImpl( stream& o, bool tableFormat ) const {
88  auto fmt = boost::format{tableFormat ? "H%|10d| |" : "#=%|-7lu|"};
89  return o << fmt % boost::histogram::algorithm::sum( hist );
90  }
92  operator bool() const { return filled; }
93 
94  private:
95  // flag to record if anything was added to the histogram (information not available in Boost histograms)
96  bool filled = false;
97  };
98  } // namespace Details
99 
131  template <typename HistogramType>
132  struct Histogram : PrintableCounter {
134 
135  Histogram( HistogramType h ) : m_hist{std::move( h )} {}
136 
137  template <class OWNER>
138  Histogram( OWNER* o, const std::string& tag, HistogramType h ) : Histogram{std::move( h )} {
139  o->declareCounter( tag, *this );
140  }
141 
144  template <class... Ts>
145  Histogram& operator()( const Ts&... ts ) {
146  m_hist( ts... );
147  return *this;
148  }
149  template <typename T = HistogramType, typename INC = typename Details::HistogramAdapter<T>::increment_t>
150  Histogram& operator+=( const INC& x ) {
151  m_hist += x;
152  return *this;
153  }
154  template <class... Ts>
155  Histogram& fill( const Ts&... ts ) {
156  m_hist.fill( ts... );
157  return *this;
158  }
160 
163  HistogramType& hist() { return m_hist.hist; }
164  const HistogramType& hist() const { return m_hist.hist; }
166 
167  // PrintableCounter specialization.
168  using PrintableCounter::print;
169  std::ostream& print( std::ostream& s, bool tableFormat = false ) const override {
170  return m_hist.printImpl( s, tableFormat );
171  }
172  MsgStream& print( MsgStream& s, bool tableFormat = true ) const override {
173  return m_hist.printImpl( s, tableFormat );
174  }
175  bool toBePrinted() const override { return m_hist; }
176 
177  private:
179  };
180 } // namespace Gaudi::Accumulators
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:34
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:119
std::ostream & print(std::ostream &s, bool tableFormat=false) const override
prints the counter to a stream
Definition: Histogram.h:169
bool toBePrinted() const override
hint whether we should print that counter or not.
Definition: Histogram.h:175
Histogram & operator()(const Ts &... ts)
Definition: Histogram.h:145
const HistogramType & hist() const
Definition: Histogram.h:164
STL class.
HistogramType & hist()
Definition: Histogram.h:163
Histogram(HistogramType h)
Definition: Histogram.h:135
stream & printImpl(stream &o, bool tableFormat) const
Basic formatting showing only total count.
Definition: Histogram.h:87
stream & printImpl(stream &o, bool tableFormat) const
Basic formatting showing only total count.
Definition: Histogram.h:50
T move(T... args)
Histogram(OWNER *o, const std::string &tag, HistogramType h)
Definition: Histogram.h:138
string s
Definition: gaudirun.py:328
Counter class for histogram storage.
Definition: Histogram.h:132
Histogram & fill(const Ts &... ts)
Definition: Histogram.h:155
STL class.
Efficient counter implementations for Gaudi.
Definition: Histogram.h:19
Histogram & operator+=(const INC &x)
Definition: Histogram.h:150
Helper to provide uniform interface to different histograms implementations, at the moment ROOT7 and ...
Definition: Histogram.h:24
MsgStream & print(MsgStream &s, bool tableFormat=true) const override
Definition: Histogram.h:172