Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v38r0 (2143aa4c)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
CounterHistos.cpp
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 \***********************************************************************************/
13 #include <Gaudi/Algorithm.h>
15 
16 #include "../../../GaudiKernel/tests/src/LogHistogram.h"
17 
18 #include <deque>
19 
20 namespace Gaudi {
21  namespace Examples {
22  namespace Counter {
23 
25  template <typename Arithmetic, Gaudi::Accumulators::atomicity Atomicity>
27  public:
29  StatusCode initialize() override {
30  // must be called first
31  const StatusCode sc = Algorithm::initialize();
32  if ( sc.isFailure() ) return sc;
33 
34  // random number generator
35  auto randSvc = service<IRndmGenSvc>( "RndmGenSvc", true );
36  if ( !randSvc || !m_rand.initialize( randSvc, Rndm::Flat( 0., 1. ) ) ) {
37  error() << "Unable to create Random generator" << endmsg;
38  return StatusCode::FAILURE;
39  }
40 
41  Gaudi::Accumulators::Axis<Arithmetic> axis{ 100, 0, 1 };
42  for ( unsigned int iH = 0; iH < m_nHistos; ++iH ) {
43  std::ostringstream title;
44  title << "Histogram Number " << iH;
45  m_histos.emplace_back( this, title.str(), title.str(), axis );
46  }
47 
48  return sc;
49  }
50 
51  StatusCode execute( const EventContext& ) const override {
52  for ( unsigned int iT = 0; iT < m_nTracks; ++iT ) {
53  for ( auto& h : m_histos ) { ++h[m_rand()]; }
54  }
55  return StatusCode::SUCCESS;
56  }
57 
58  private:
60 
62 
63  Gaudi::Property<unsigned int> m_nHistos{ this, "NumHistos", 20, "" };
64  Gaudi::Property<unsigned int> m_nTracks{ this, "NumTracks", 30, "" };
65  };
67  DECLARE_COMPONENT_WITH_ID( HistoTimingAlgDA, "HistoTimingAlgDA" )
68  using HistoTimingAlgIA = HistoTimingAlg<unsigned int, Gaudi::Accumulators::atomicity::full>;
70  using HistoTimingAlgD = HistoTimingAlg<double, Gaudi::Accumulators::atomicity::none>;
72  using HistoTimingAlgI = HistoTimingAlg<unsigned int, Gaudi::Accumulators::atomicity::none>;
74 
77  public:
79 
80  StatusCode execute( const EventContext& ) const override {
81  // some random number generators, just to provide numbers
82  static Rndm::Numbers Gauss( randSvc(), Rndm::Gauss( 0.0, 1.0 ) );
83  static Rndm::Numbers Flat( randSvc(), Rndm::Flat( -10.0, 10.0 ) );
84  static Rndm::Numbers Gauss3( randSvc(), Rndm::Gauss( 5.0, 1.0 ) );
85 
86  // cache some numbers
87  const double gauss( Gauss() );
88  const double gauss2( Gauss() );
89  const double flat( Flat() );
90  const double gauss3( Gauss3() );
91 
92  // updating histograms
93  ++m_gauss[gauss];
94  ++m_gaussAbsName[gauss];
95  ++m_gaussVflat[{ flat, gauss }];
96  ++m_gaussVflatVgauss[{ flat, gauss, gauss2 }];
97  ++m_gauss_noato[gauss];
98  ++m_gaussVflat_noato[{ flat, gauss }];
99  ++m_gaussVflatVgauss_noato[{ flat, gauss, gauss2 }];
100  ++m_gauss_int[(int)gauss];
101  ++m_gaussVflat_int[{ (int)flat, (int)gauss }];
102  ++m_gaussVflatVgauss_int[{ (int)flat, (int)gauss, (int)gauss2 }];
103 
104  // weighted cases
105  m_gauss_w[gauss] += .5;
106  m_gaussVflat_w[{ flat, gauss }] += .5;
107  m_gaussVflatVgauss_w[{ flat, gauss, gauss2 }] += .5;
108 
109  // using buffers
110  auto gauss_buf = m_gauss_buf.buffer();
111  auto gaussVflat_buf = m_gaussVflat_buf.buffer();
112  auto gaussVflatVgauss_buf = m_gaussVflatVgauss_buf.buffer();
113  for ( unsigned int i = 0; i < 10; i++ ) {
114  ++gauss_buf[gauss];
115  ++gaussVflat_buf[{ flat, gauss }];
116  ++gaussVflatVgauss_buf[{ flat, gauss, gauss2 }];
117  }
118 
119  // updating profile histograms
120  m_prof_gauss[gauss] += gauss3;
121  m_prof_gaussVflat[{ flat, gauss }] += gauss3;
122  m_prof_gaussVflatVgauss[{ flat, gauss, gauss2 }] += gauss3;
123  m_prof_gauss_noato[gauss] += gauss3;
124  m_prof_gaussVflat_noato[{ flat, gauss }] += gauss3;
125  m_prof_gaussVflatVgauss_noato[{ flat, gauss, gauss2 }] += gauss3;
126  m_prof_gauss_int[(int)gauss] += (int)gauss3;
127  m_prof_gaussVflat_int[{ (int)flat, (int)gauss }] += (int)gauss3;
128  m_prof_gaussVflatVgauss_int[{ (int)flat, (int)gauss, (int)gauss2 }] += (int)gauss3;
129 
130  // weighted profile histograms
131  m_prof_gauss_w[gauss] += { gauss3, .5 };
132  m_prof_gaussVflat_w[{ flat, gauss }] += { gauss3, .5 };
133  m_prof_gaussVflatVgauss_w[{ flat, gauss, gauss2 }] += { gauss3, .5 };
134 
135  // using buffers on profile histograms
136  auto prof_gauss_buf = m_prof_gauss_buf.buffer();
137  auto prof_gaussVflat_buf = m_prof_gaussVflat_buf.buffer();
138  auto prof_gaussVflatVgauss_buf = m_prof_gaussVflatVgauss_buf.buffer();
139  for ( unsigned int i = 0; i < 10; i++ ) {
140  prof_gauss_buf[gauss] += gauss3;
141  prof_gaussVflat_buf[{ flat, gauss }] += gauss3;
142  prof_gaussVflatVgauss_buf[{ flat, gauss, gauss2 }] += gauss3;
143  }
144 
145  // using CaloHistogram
146  m_log_gauss[gauss]++;
147  m_log_gaussVflat[{ flat, gauss }]++;
148 
149  if ( m_nCalls.nEntries() == 0 ) always() << "Filling Histograms...... Please be patient !" << endmsg;
150  ++m_nCalls;
151  return StatusCode::SUCCESS;
152  }
153 
154  private:
155  mutable Gaudi::Accumulators::Counter<> m_nCalls{ this, "calls" };
156 
157  // Testing regular histograms in all dimensions and features
158 
159  // "default" case, that is bins containing doubles and atomic
161  this, "Gauss", "Gaussian mean=0, sigma=1, atomic", { 100, -5, 5, "X" } };
162  mutable Gaudi::Accumulators::Histogram<2> m_gaussVflat{
163  this, "GaussFlat", "Gaussian V Flat, atomic", { { 50, -5, 5, "X" }, { 50, -5, 5, "Y" } } };
164  mutable Gaudi::Accumulators::Histogram<3> m_gaussVflatVgauss{
165  this,
166  "GaussFlatGauss",
167  "Gaussian V Flat V Gaussian, atomic",
168  { { 10, -5, 5, "X" }, { 10, -5, 5, "Y" }, { 10, -5, 5, "Z" } } };
169 
170  // non atomic versions
172  this, "GaussNA", "Gaussian mean=0, sigma=1, non atomic", { 100, -5, 5 } };
174  this, "GaussFlatNA", "Gaussian V Flat, non atomic", { { 50, -5, 5 }, { 50, -5, 5 } } };
176  this,
177  "GaussFlatGaussNA",
178  "Gaussian V Flat V Gaussian, non atomic",
179  { { 10, -5, 5 }, { 10, -5, 5 }, { 10, -5, 5 } } };
180 
181  // using integers
183  this, "GaussInt", "Gaussian mean=0, sigma=1, integer values", { 10, -5, 5 } };
185  this, "GaussFlatInt", "Gaussian V Flat, integer values", { { 10, -5, 5 }, { 10, -5, 5 } } };
187  this,
188  "GaussFlatGaussInt",
189  "Gaussian V Flat V Gaussian, interger values",
190  { { 10, -5, 5 }, { 10, -5, 5 }, { 10, -5, 5 } } };
191 
192  // weighted version, "default" case
194  this, "GaussW", "Gaussian mean=0, sigma=1, weighted", { 100, -5, 5 } };
196  this, "GaussFlatW", "Gaussian V Flat, weighted", { { 50, -5, 5 }, { 50, -5, 5 } } };
197  mutable Gaudi::Accumulators::WeightedHistogram<3> m_gaussVflatVgauss_w{
198  this,
199  "GaussFlatGaussW",
200  "Gaussian V Flat V Gaussian, weighted",
201  { { 10, -5, 5 }, { 10, -5, 5 }, { 10, -5, 5 } } };
202 
203  // "default" case, dedicated to testing buffers
205  this, "GaussBuf", "Gaussian mean=0, sigma=1, buffered", { 100, -5, 5 } };
206  mutable Gaudi::Accumulators::Histogram<2> m_gaussVflat_buf{
207  this, "GaussFlatBuf", "Gaussian V Flat, buffered", { { 50, -5, 5 }, { 50, -5, 5 } } };
208  mutable Gaudi::Accumulators::Histogram<3> m_gaussVflatVgauss_buf{
209  this,
210  "GaussFlatGaussBuf",
211  "Gaussian V Flat V Gaussian, buffered",
212  { { 10, -5, 5 }, { 10, -5, 5 }, { 10, -5, 5 } } };
213 
214  // Testing profiling histograms in all dimensions and features
215 
216  // "default" case, that is bins containing doubles and atomic
218  this, "ProfGauss", "Profile, Gaussian mean=0, sigma=1, atomic", { 100, -5, 5 } };
219  mutable Gaudi::Accumulators::ProfileHistogram<2> m_prof_gaussVflat{
220  this, "ProfGaussFlat", "Profile, Gaussian V Flat, atomic", { { 50, -5, 5 }, { 50, -5, 5 } } };
221  mutable Gaudi::Accumulators::ProfileHistogram<3> m_prof_gaussVflatVgauss{
222  this,
223  "ProfGaussFlatGauss",
224  "Profile, Gaussian V Flat V Gaussian, atomic",
225  { { 10, -5, 5 }, { 10, -5, 5 }, { 10, -5, 5 } } };
226 
227  // non atomic versions
229  this, "ProfGaussNA", "Profile, Gaussian mean=0, sigma=1, non atomic", { 100, -5, 5 } };
231  this, "ProfGaussFlatNA", "Profile, Gaussian V Flat, non atomic", { { 50, -5, 5 }, { 50, -5, 5 } } };
233  m_prof_gaussVflatVgauss_noato{ this,
234  "ProfGaussFlatGaussNA",
235  "Profile, Gaussian V Flat V Gaussian, non atomic",
236  { { 10, -5, 5 }, { 10, -5, 5 }, { 10, -5, 5 } } };
237 
238  // using integers internally
240  this, "ProfGaussInt", "Profile, Gaussian mean=0, sigma=1, integer values", { 10, -5, 5 } };
242  m_prof_gaussVflat_int{ this,
243  "ProfGaussFlatInt",
244  "Profile, Gaussian V Flat, integer values",
245  { { 10, -5, 5 }, { 10, -5, 5 } } };
247  m_prof_gaussVflatVgauss_int{ this,
248  "ProfGaussFlatGaussInt",
249  "Profile, Gaussian V Flat V Gaussian, interger values",
250  { { 10, -5, 5 }, { 10, -5, 5 }, { 10, -5, 5 } } };
251 
252  // weighted version, "default" case
254  this, "ProfGaussW", "Profile, Gaussian mean=0, sigma=1, weighted", { 100, -5, 5 } };
256  this, "ProfGaussFlatW", "Profile, Gaussian V Flat, weighted", { { 50, -5, 5 }, { 50, -5, 5 } } };
257  mutable Gaudi::Accumulators::WeightedProfileHistogram<3> m_prof_gaussVflatVgauss_w{
258  this,
259  "ProfGaussFlatGaussW",
260  "Profile, Gaussian V Flat V Gaussian, weighted",
261  { { 10, -5, 5 }, { 10, -5, 5 }, { 10, -5, 5 } } };
262 
263  // "default" case, dedicated to testing buffers
265  this, "ProfGaussBuf", "Profile, Gaussian mean=0, sigma=1, buffered", { 100, -5, 5 } };
266  mutable Gaudi::Accumulators::ProfileHistogram<2> m_prof_gaussVflat_buf{
267  this, "ProfGaussFlatBuf", "Profile, Gaussian V Flat, buffered", { { 50, -5, 5 }, { 50, -5, 5 } } };
268  mutable Gaudi::Accumulators::ProfileHistogram<3> m_prof_gaussVflatVgauss_buf{
269  this,
270  "ProfGaussFlatGaussBuf",
271  "Profile, Gaussian V Flat V Gaussian, buffered",
272  { { 10, -5, 5 }, { 10, -5, 5 }, { 10, -5, 5 } } };
273 
274  // Custom histogram, with log or log log behavior
275  mutable Accumulators::LogHistogram<1> m_log_gauss{ this, "LogGauss", "Log, Gaussian", { 5, 0, 2 } };
276  mutable Accumulators::LogHistogram<2, Accumulators::atomicity::full, float> m_log_gaussVflat{
277  this, "LogGaussFlat", "LogLog, Gaussian V Flat", { { 5, 0, 2 }, { 5, 0, 2 } } };
278 
279  // Histogram in an absolute location
280  mutable Gaudi::Accumulators::Histogram<1> m_gaussAbsName{
281  this, "/TopDir/SubDir/Gauss", "Gaussian mean=0, sigma=1, atomic", { 100, -5, 5, "X" } };
282  };
284 
285 
287  public:
289 
290  StatusCode execute( const EventContext& ) const override {
291  // some random number generators, just to provide numbers
292  static Rndm::Numbers Gauss( randSvc(), Rndm::Gauss( 0.0, 1.0 ) );
293  static Rndm::Numbers Flat( randSvc(), Rndm::Flat( -10.0, 10.0 ) );
294 
295  // cache some numbers
296  const double gauss( Gauss() );
297  const double gauss2( Gauss() );
298  const double flat( Flat() );
299 
300  // updating histograms
301  ++m_gauss[gauss];
302  ++m_gaussVflat[{ flat, gauss }];
303  ++m_gaussVflatVgauss[{ flat, gauss, gauss2 }];
304 
305  // using buffers
306  auto gauss_buf = m_gauss_buf.buffer();
307  auto gaussVflat_buf = m_gaussVflat_buf.buffer();
308  auto gaussVflatVgauss_buf = m_gaussVflatVgauss_buf.buffer();
309  for ( unsigned int i = 0; i < 10; i++ ) {
310  ++gauss_buf[gauss];
311  ++gaussVflat_buf[{ flat, gauss }];
312  ++gaussVflatVgauss_buf[{ flat, gauss, gauss2 }];
313  }
314 
315  if ( m_nCalls.nEntries() == 0 ) always() << "Filling Histograms...... Please be patient !" << endmsg;
316  ++m_nCalls;
317  return StatusCode::SUCCESS;
318  }
319 
320  private:
321  mutable Gaudi::Accumulators::Counter<> m_nCalls{ this, "calls" };
322 
323  // Testing Root histograms in all dimensions
324 
325  // "default" case, that is bins containing doubles and atomic
327  this, "Gauss", "Gaussian mean=0, sigma=1, atomic", { 100, -5, 5, "X" } };
329  this, "GaussFlat", "Gaussian V Flat, atomic", { { 50, -5, 5, "X" }, { 50, -5, 5, "Y" } } };
330  mutable Gaudi::Accumulators::RootHistogram<3> m_gaussVflatVgauss{
331  this,
332  "GaussFlatGauss",
333  "Gaussian V Flat V Gaussian, atomic",
334  { { 10, -5, 5, "X" }, { 10, -5, 5, "Y" }, { 10, -5, 5, "Z" } } };
335 
336  // "default" case, dedicated to testing buffers
338  this, "GaussBuf", "Gaussian mean=0, sigma=1, buffered", { 100, -5, 5 } };
339  mutable Gaudi::Accumulators::RootHistogram<2> m_gaussVflat_buf{
340  this, "GaussFlatBuf", "Gaussian V Flat, buffered", { { 50, -5, 5 }, { 50, -5, 5 } } };
341  mutable Gaudi::Accumulators::RootHistogram<3> m_gaussVflatVgauss_buf{
342  this,
343  "GaussFlatGaussBuf",
344  "Gaussian V Flat V Gaussian, buffered",
345  { { 10, -5, 5 }, { 10, -5, 5 }, { 10, -5, 5 } } };
346  };
347  DECLARE_COMPONENT( GaudiRootHistoAlgorithm )
348  } // namespace Counter
349  } // namespace Examples
350 } // namespace Gaudi
Gaudi::Examples::Counter
Definition: Counter.h:32
Gaudi::Examples::Counter::HistoTimingAlg::m_rand
Rndm::Numbers m_rand
Definition: CounterHistos.cpp:59
Gaudi::Examples::Counter::GaudiRootHistoAlgorithm
Example of algorithm using root histograms accumulators.
Definition: CounterHistos.cpp:286
RndmGenerators.h
Gaudi::Algorithm::randSvc
SmartIF< IRndmGenSvc > & randSvc() const
The standard RandomGen service, Return a pointer to the service if present.
Definition: Algorithm.cpp:565
Gaudi::Accumulators::Counter<>
Histogram.h
Gaudi::Algorithm::initialize
StatusCode initialize() override
the default (empty) implementation of IStateful::initialize() method
Definition: Algorithm.h:178
Gaudi::Examples::Counter::HistoTimingAlg
Simple timing of counter based histograms filling.
Definition: CounterHistos.cpp:26
Gaudi::Examples::Counter::HistoTimingAlg::m_histos
std::deque< Gaudi::Accumulators::Histogram< 1, Atomicity, Arithmetic > > m_histos
Definition: CounterHistos.cpp:61
class
#define class
Definition: HistogramPersistencySvc.cpp:54
Rndm::Flat
Parameters for the flat random number generation within boundaries [minimum, maximum].
Definition: RndmGenerators.h:253
Gaudi::Accumulators::Axis
Definition of an Histogram Axis.
Definition: Histogram.h:154
DECLARE_COMPONENT_WITH_ID
#define DECLARE_COMPONENT_WITH_ID(type, id)
Definition: PluginServiceV1.h:47
Gaudi::Examples::Counter::GaudiRootHistoAlgorithm::execute
StatusCode execute(const EventContext &) const override
Definition: CounterHistos.cpp:290
StatusCode
Definition: StatusCode.h:65
Rndm::Gauss
Parameters for the Gauss random number generation.
Definition: RndmGenerators.h:32
Rndm::Numbers
Random number accessor This small class encapsulates the use of the random number generator.
Definition: RndmGenerators.h:359
Gaudi::Algorithm
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:90
AlgSequencer.h
h
Definition: AlgSequencer.py:31
Gaudi::Examples::Counter::GaudiHistoAlgorithm::execute
StatusCode execute(const EventContext &) const override
Definition: CounterHistos.cpp:80
GaudiHistoAlgorithm
Definition: GaudiHistoAlgorithm.h:27
Gaudi::Examples::Counter::HistoTimingAlg::m_nTracks
Gaudi::Property< unsigned int > m_nTracks
Definition: CounterHistos.cpp:64
Gaudi::Examples::Counter::HistoTimingAlg::execute
StatusCode execute(const EventContext &) const override
Definition: CounterHistos.cpp:51
Algorithm.h
std::deque
STL class.
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:203
Gaudi::Examples::Counter::HistoTimingAlg::initialize
StatusCode initialize() override
Definition: CounterHistos.cpp:29
Gaudi
Header file for std:chrono::duration-based Counters.
Definition: __init__.py:1
Rndm::Numbers::initialize
virtual StatusCode initialize(const SmartIF< IRndmGenSvc > &svc, const IRndmGen::Param &par)
Initialization.
Definition: RndmGenerators.cpp:30
std::ostringstream
STL class.
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:129
Gaudi::Units::gauss
constexpr double gauss
Definition: SystemOfUnits.h:252
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginServiceV1.h:46
EventContext
Definition: EventContext.h:34
Gaudi::Accumulators::atomicity
atomicity
Defines atomicity of the accumulators.
Definition: Accumulators.h:263
std::ostringstream::str
T str(T... args)
Gaudi::Examples::Counter::GaudiHistoAlgorithm
Example of algorithm using histograms accumulators.
Definition: CounterHistos.cpp:76
Gaudi::Algorithm::Algorithm
Algorithm(std::string name, ISvcLocator *svcloc, std::string version=PACKAGE_VERSION)
Constructor.
Definition: Algorithm.h:101
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
Gaudi::Accumulators::RootHistogramingCounterBase
Extension of the standard Gaudi histogram to provide similar functionnality as ROOT.
Definition: RootHistogram.h:266
Gaudi::Accumulators::HistogramingCounterBaseInternal
A base counter dealing with Histograms.
Definition: Histogram.h:464
Gaudi::Examples::Counter::HistoTimingAlg::m_nHistos
Gaudi::Property< unsigned int > m_nHistos
Definition: CounterHistos.cpp:63
Gaudi::Property< unsigned int >
RootHistogram.h