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