Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v36r16 (ea80daf8)
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 \***********************************************************************************/
12 #include <Gaudi/Algorithm.h>
14 
15 #include "../../../GaudiKernel/tests/src/LogHistogram.h"
16 
17 #include <deque>
18 
19 namespace Gaudi {
20  namespace Examples {
21  namespace Counter {
22 
24  template <typename Arithmetic, Gaudi::Accumulators::atomicity Atomicity>
26  public:
28  StatusCode initialize() override {
29  // must be called first
30  const StatusCode sc = Algorithm::initialize();
31  if ( sc.isFailure() ) return sc;
32 
33  // random number generator
34  auto randSvc = service<IRndmGenSvc>( "RndmGenSvc", true );
35  if ( !randSvc || !m_rand.initialize( randSvc, Rndm::Flat( 0., 1. ) ) ) {
36  error() << "Unable to create Random generator" << endmsg;
37  return StatusCode::FAILURE;
38  }
39 
40  Gaudi::Accumulators::Axis<Arithmetic> axis{ 100, 0, 1 };
41  for ( unsigned int iH = 0; iH < m_nHistos; ++iH ) {
42  std::ostringstream title;
43  title << "Histogram Number " << iH;
44  m_histos.emplace_back( this, title.str(), title.str(), axis );
45  }
46 
47  return sc;
48  }
49 
50  StatusCode execute( const EventContext& ) const override {
51  for ( unsigned int iT = 0; iT < m_nTracks; ++iT ) {
52  for ( auto& h : m_histos ) { ++h[m_rand()]; }
53  }
54  return StatusCode::SUCCESS;
55  }
56 
57  private:
59 
61 
62  Gaudi::Property<unsigned int> m_nHistos{ this, "NumHistos", 20, "" };
63  Gaudi::Property<unsigned int> m_nTracks{ this, "NumTracks", 30, "" };
64  };
66  DECLARE_COMPONENT_WITH_ID( HistoTimingAlgDA, "HistoTimingAlgDA" )
67  using HistoTimingAlgIA = HistoTimingAlg<unsigned int, Gaudi::Accumulators::atomicity::full>;
69  using HistoTimingAlgD = HistoTimingAlg<double, Gaudi::Accumulators::atomicity::none>;
71  using HistoTimingAlgI = HistoTimingAlg<unsigned int, Gaudi::Accumulators::atomicity::none>;
73 
76  public:
78 
79  StatusCode execute( const EventContext& ) const override {
80  // some random number generators, just to provide numbers
81  static Rndm::Numbers Gauss( randSvc(), Rndm::Gauss( 0.0, 1.0 ) );
82  static Rndm::Numbers Flat( randSvc(), Rndm::Flat( -10.0, 10.0 ) );
83  static Rndm::Numbers Gauss3( randSvc(), Rndm::Gauss( 5.0, 1.0 ) );
84 
85  // cache some numbers
86  const double gauss( Gauss() );
87  const double gauss2( Gauss() );
88  const double flat( Flat() );
89  const double gauss3( Gauss3() );
90 
91  // updating histograms
92  ++m_gauss[gauss];
93  ++m_gaussAbsName[gauss];
94  ++m_gaussVflat[{ flat, gauss }];
95  ++m_gaussVflatVgauss[{ flat, gauss, gauss2 }];
96  ++m_gauss_noato[gauss];
97  ++m_gaussVflat_noato[{ flat, gauss }];
98  ++m_gaussVflatVgauss_noato[{ flat, gauss, gauss2 }];
99  ++m_gauss_int[(int)gauss];
100  ++m_gaussVflat_int[{ (int)flat, (int)gauss }];
101  ++m_gaussVflatVgauss_int[{ (int)flat, (int)gauss, (int)gauss2 }];
102 
103  // weighted cases
104  m_gauss_w[gauss] += .5;
105  m_gaussVflat_w[{ flat, gauss }] += .5;
106  m_gaussVflatVgauss_w[{ flat, gauss, gauss2 }] += .5;
107 
108  // using buffers
109  auto gauss_buf = m_gauss_buf.buffer();
110  auto gaussVflat_buf = m_gaussVflat_buf.buffer();
111  auto gaussVflatVgauss_buf = m_gaussVflatVgauss_buf.buffer();
112  for ( unsigned int i = 0; i < 10; i++ ) {
113  ++gauss_buf[gauss];
114  ++gaussVflat_buf[{ flat, gauss }];
115  ++gaussVflatVgauss_buf[{ flat, gauss, gauss2 }];
116  }
117 
118  // updating profile histograms
119  m_prof_gauss[gauss] += gauss3;
120  m_prof_gaussVflat[{ flat, gauss }] += gauss3;
121  m_prof_gaussVflatVgauss[{ flat, gauss, gauss2 }] += gauss3;
122  m_prof_gauss_noato[gauss] += gauss3;
123  m_prof_gaussVflat_noato[{ flat, gauss }] += gauss3;
124  m_prof_gaussVflatVgauss_noato[{ flat, gauss, gauss2 }] += gauss3;
125  m_prof_gauss_int[(int)gauss] += (int)gauss3;
126  m_prof_gaussVflat_int[{ (int)flat, (int)gauss }] += (int)gauss3;
127  m_prof_gaussVflatVgauss_int[{ (int)flat, (int)gauss, (int)gauss2 }] += (int)gauss3;
128 
129  // weighted profile histograms
130  m_prof_gauss_w[gauss] += { gauss3, .5 };
131  m_prof_gaussVflat_w[{ flat, gauss }] += { gauss3, .5 };
132  m_prof_gaussVflatVgauss_w[{ flat, gauss, gauss2 }] += { gauss3, .5 };
133 
134  // using buffers on profile histograms
135  auto prof_gauss_buf = m_prof_gauss_buf.buffer();
136  auto prof_gaussVflat_buf = m_prof_gaussVflat_buf.buffer();
137  auto prof_gaussVflatVgauss_buf = m_prof_gaussVflatVgauss_buf.buffer();
138  for ( unsigned int i = 0; i < 10; i++ ) {
139  prof_gauss_buf[gauss] += gauss3;
140  prof_gaussVflat_buf[{ flat, gauss }] += gauss3;
141  prof_gaussVflatVgauss_buf[{ flat, gauss, gauss2 }] += gauss3;
142  }
143 
144  // using CaloHistogram
145  m_log_gauss[gauss]++;
146  m_log_gaussVflat[{ flat, gauss }]++;
147 
148  if ( m_nCalls.nEntries() == 0 ) always() << "Filling Histograms...... Please be patient !" << endmsg;
149  ++m_nCalls;
150  return StatusCode::SUCCESS;
151  }
152 
153  private:
154  mutable Gaudi::Accumulators::Counter<> m_nCalls{ this, "calls" };
155 
156  // Testing regular histograms in all dimensions and features
157 
158  // "default" case, that is bins containing doubles and atomic
160  this, "Gauss", "Gaussian mean=0, sigma=1, atomic", { 100, -5, 5, "X" } };
161  mutable Gaudi::Accumulators::Histogram<2> m_gaussVflat{
162  this, "GaussFlat", "Gaussian V Flat, atomic", { { 50, -5, 5, "X" }, { 50, -5, 5, "Y" } } };
163  mutable Gaudi::Accumulators::Histogram<3> m_gaussVflatVgauss{
164  this,
165  "GaussFlatGauss",
166  "Gaussian V Flat V Gaussian, atomic",
167  { { 10, -5, 5, "X" }, { 10, -5, 5, "Y" }, { 10, -5, 5, "Z" } } };
168 
169  // non atomic versions
171  this, "GaussNA", "Gaussian mean=0, sigma=1, non atomic", { 100, -5, 5 } };
173  this, "GaussFlatNA", "Gaussian V Flat, non atomic", { { 50, -5, 5 }, { 50, -5, 5 } } };
175  this,
176  "GaussFlatGaussNA",
177  "Gaussian V Flat V Gaussian, non atomic",
178  { { 10, -5, 5 }, { 10, -5, 5 }, { 10, -5, 5 } } };
179 
180  // using integers
182  this, "GaussInt", "Gaussian mean=0, sigma=1, integer values", { 10, -5, 5 } };
184  this, "GaussFlatInt", "Gaussian V Flat, integer values", { { 10, -5, 5 }, { 10, -5, 5 } } };
186  this,
187  "GaussFlatGaussInt",
188  "Gaussian V Flat V Gaussian, interger values",
189  { { 10, -5, 5 }, { 10, -5, 5 }, { 10, -5, 5 } } };
190 
191  // weighted version, "default" case
193  this, "GaussW", "Gaussian mean=0, sigma=1, weighted", { 100, -5, 5 } };
195  this, "GaussFlatW", "Gaussian V Flat, weighted", { { 50, -5, 5 }, { 50, -5, 5 } } };
196  mutable Gaudi::Accumulators::WeightedHistogram<3> m_gaussVflatVgauss_w{
197  this,
198  "GaussFlatGaussW",
199  "Gaussian V Flat V Gaussian, weighted",
200  { { 10, -5, 5 }, { 10, -5, 5 }, { 10, -5, 5 } } };
201 
202  // "default" case, dedicated to testing buffers
204  this, "GaussBuf", "Gaussian mean=0, sigma=1, buffered", { 100, -5, 5 } };
205  mutable Gaudi::Accumulators::Histogram<2> m_gaussVflat_buf{
206  this, "GaussFlatBuf", "Gaussian V Flat, buffered", { { 50, -5, 5 }, { 50, -5, 5 } } };
207  mutable Gaudi::Accumulators::Histogram<3> m_gaussVflatVgauss_buf{
208  this,
209  "GaussFlatGaussBuf",
210  "Gaussian V Flat V Gaussian, buffered",
211  { { 10, -5, 5 }, { 10, -5, 5 }, { 10, -5, 5 } } };
212 
213  // Testing profiling histograms in all dimensions and features
214 
215  // "default" case, that is bins containing doubles and atomic
217  this, "ProfGauss", "Profile, Gaussian mean=0, sigma=1, atomic", { 100, -5, 5 } };
218  mutable Gaudi::Accumulators::ProfileHistogram<2> m_prof_gaussVflat{
219  this, "ProfGaussFlat", "Profile, Gaussian V Flat, atomic", { { 50, -5, 5 }, { 50, -5, 5 } } };
220  mutable Gaudi::Accumulators::ProfileHistogram<3> m_prof_gaussVflatVgauss{
221  this,
222  "ProfGaussFlatGauss",
223  "Profile, Gaussian V Flat V Gaussian, atomic",
224  { { 10, -5, 5 }, { 10, -5, 5 }, { 10, -5, 5 } } };
225 
226  // non atomic versions
228  this, "ProfGaussNA", "Profile, Gaussian mean=0, sigma=1, non atomic", { 100, -5, 5 } };
230  this, "ProfGaussFlatNA", "Profile, Gaussian V Flat, non atomic", { { 50, -5, 5 }, { 50, -5, 5 } } };
232  m_prof_gaussVflatVgauss_noato{ this,
233  "ProfGaussFlatGaussNA",
234  "Profile, Gaussian V Flat V Gaussian, non atomic",
235  { { 10, -5, 5 }, { 10, -5, 5 }, { 10, -5, 5 } } };
236 
237  // using integers internally
239  this, "ProfGaussInt", "Profile, Gaussian mean=0, sigma=1, integer values", { 10, -5, 5 } };
241  m_prof_gaussVflat_int{ this,
242  "ProfGaussFlatInt",
243  "Profile, Gaussian V Flat, integer values",
244  { { 10, -5, 5 }, { 10, -5, 5 } } };
246  m_prof_gaussVflatVgauss_int{ this,
247  "ProfGaussFlatGaussInt",
248  "Profile, Gaussian V Flat V Gaussian, interger values",
249  { { 10, -5, 5 }, { 10, -5, 5 }, { 10, -5, 5 } } };
250 
251  // weighted version, "default" case
253  this, "ProfGaussW", "Profile, Gaussian mean=0, sigma=1, weighted", { 100, -5, 5 } };
255  this, "ProfGaussFlatW", "Profile, Gaussian V Flat, weighted", { { 50, -5, 5 }, { 50, -5, 5 } } };
256  mutable Gaudi::Accumulators::WeightedProfileHistogram<3> m_prof_gaussVflatVgauss_w{
257  this,
258  "ProfGaussFlatGaussW",
259  "Profile, Gaussian V Flat V Gaussian, weighted",
260  { { 10, -5, 5 }, { 10, -5, 5 }, { 10, -5, 5 } } };
261 
262  // "default" case, dedicated to testing buffers
264  this, "ProfGaussBuf", "Profile, Gaussian mean=0, sigma=1, buffered", { 100, -5, 5 } };
265  mutable Gaudi::Accumulators::ProfileHistogram<2> m_prof_gaussVflat_buf{
266  this, "ProfGaussFlatBuf", "Profile, Gaussian V Flat, buffered", { { 50, -5, 5 }, { 50, -5, 5 } } };
267  mutable Gaudi::Accumulators::ProfileHistogram<3> m_prof_gaussVflatVgauss_buf{
268  this,
269  "ProfGaussFlatGaussBuf",
270  "Profile, Gaussian V Flat V Gaussian, buffered",
271  { { 10, -5, 5 }, { 10, -5, 5 }, { 10, -5, 5 } } };
272 
273  // Custom histogram, with log or log log behavior
274  mutable Accumulators::LogHistogram<1> m_log_gauss{ this, "LogGauss", "Log, Gaussian", { 5, 0, 2 } };
275  mutable Accumulators::LogHistogram<2, Accumulators::atomicity::full, float> m_log_gaussVflat{
276  this, "LogGaussFlat", "LogLog, Gaussian V Flat", { { 5, 0, 2 }, { 5, 0, 2 } } };
277 
278  // Histogram in an absolute location
279  mutable Gaudi::Accumulators::Histogram<1> m_gaussAbsName{
280  this, "/TopDir/SubDir/Gauss", "Gaussian mean=0, sigma=1, atomic", { 100, -5, 5, "X" } };
281  };
283  } // namespace Counter
284  } // namespace Examples
285 } // namespace Gaudi
Gaudi::Examples::Counter::HistoTimingAlg::m_rand
Rndm::Numbers m_rand
Definition: CounterHistos.cpp:58
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:25
Gaudi::Examples::Counter::HistoTimingAlg::m_histos
std::deque< Gaudi::Accumulators::Histogram< 1, Atomicity, Arithmetic > > m_histos
Definition: CounterHistos.cpp:60
class
#define class
Definition: HistogramPersistencySvc.cpp:54
Rndm::Flat
Parameters for the flat random number generation within boundaries [minimum, maximum].
Definition: RndmGenerators.h:253
Counter
Definition: Counter.py:1
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
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:32
Gaudi::Examples::Counter::GaudiHistoAlgorithm::execute
StatusCode execute(const EventContext &) const override
Definition: CounterHistos.cpp:79
HistoUtilsEx.gauss
gauss
Definition: HistoUtilsEx.py:66
GaudiHistoAlgorithm
Definition: GaudiHistoAlgorithm.h:27
Gaudi::Examples::Counter::HistoTimingAlg::m_nTracks
Gaudi::Property< unsigned int > m_nTracks
Definition: CounterHistos.cpp:63
Gaudi::Examples::Counter::HistoTimingAlg::execute
StatusCode execute(const EventContext &) const override
Definition: CounterHistos.cpp:50
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:28
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
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:236
std::ostringstream::str
T str(T... args)
Gaudi::Examples::Counter::GaudiHistoAlgorithm
Example of algorithm using histograms accumulators.
Definition: CounterHistos.cpp:75
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::HistogramingCounterBaseInternal
A base counter dealing with Histograms.
Definition: Histogram.h:450
Gaudi::Examples::Counter::HistoTimingAlg::m_nHistos
Gaudi::Property< unsigned int > m_nHistos
Definition: CounterHistos.cpp:62
Gaudi::Property< unsigned int >