All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NonRandomCounter.h
Go to the documentation of this file.
1 /**
2  * @file icarusalg/Utilities/NonRandomCounter.h
3  * @brief Non-random number engine for profiling purposes.
4  * @author Gianluca Petrillo (petrillo@slac.stanford.edu)
5  * @date January 28, 2020
6  *
7  */
8 
9 #ifndef ICARUSALG_UTILITIES_NONRANDOMCOUNTER_H
10 #define ICARUSALG_UTILITIES_NONRANDOMCOUNTER_H
11 
12 // CLHEP
13 #include "CLHEP/Random/defs.h"
14 #include "CLHEP/Random/RandomEngine.h"
15 
16 // C/C++ standard library
17 #include <istream>
18 #include <string>
19 #include <limits> // std::numeric_limits<>
20 
21 
22 // -----------------------------------------------------------------------------
23 namespace util { class NonRandomCounter; }
24 
25 /**
26  * @brief Fast random engine which returns sequential numbers.
27  *
28  * This generator does **not** produce pseudorandom numbers.
29  * It is used only as a replacement of a real engine for profiling purposes.
30  *
31  * The range of values spans [ 0, 1 [.
32  */
33 class util::NonRandomCounter: public CLHEP::HepRandomEngine {
34 
35  public:
36 
37  NonRandomCounter() = default;
38 
39  NonRandomCounter(long seed): count(seed) {}
40 
41  NonRandomCounter(std::istream& is): NonRandomCounter(readLong(is)) {}
42 
43  virtual double flat() override { return doFlat(); }
44 
45  virtual void flatArray(const int size, double* vect) override
46  { double* end = vect + size; while (vect != end) *(vect++) = doFlat(); }
47 
48  virtual void setSeed(long seed, int) override
49  { count = static_cast<unsigned long>(seed); }
50 
51  virtual void setSeeds(long const* seeds, int _) override
52  { setSeed(*seeds, _); }
53 
54  virtual void saveStatus(const char filename[] = "NonRandomCounter.conf") const
55  override;
56 
57  virtual void restoreStatus(const char filename[] = "NonRandomCounter.conf")
58  override;
59 
60  virtual void showStatus() const override;
61 
62  virtual std::string name() const override { return "NonRandomCounter"; }
63 
64  private:
65 
66  unsigned long count = 0U;
67 
68  double doFlat()
69  {
70  return static_cast<double>(++count)
71  / std::numeric_limits<unsigned long>::max();
72  }
73 
74 
75  static long readLong(std::istream& is) { unsigned long l; is >> l; return l; }
76 
77 }; // util::NonRandomCounter
78 
79 
80 // -----------------------------------------------------------------------------
81 // --- inline implementation
82 // -----------------------------------------------------------------------------
83 
84 
85 // -----------------------------------------------------------------------------
86 
87 
88 #endif // ICARUSALG_UTILITIES_NONRANDOMCOUNTER_H
virtual void saveStatus(const char filename[]="NonRandomCounter.conf") const override
virtual void setSeeds(long const *seeds, int _) override
Fast random engine which returns sequential numbers.
static long readLong(std::istream &is)
BEGIN_PROLOG could also be dds filename
virtual void flatArray(const int size, double *vect) override
virtual void showStatus() const override
NonRandomCounter(std::istream &is)
virtual double flat() override
decltype(auto) constexpr end(T &&obj)
ADL-aware version of std::end.
Definition: StdUtils.h:77
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:92
virtual void restoreStatus(const char filename[]="NonRandomCounter.conf") override
unsigned int seed
std::vector< TrajPoint > seeds
Definition: DataStructs.cxx:14
virtual std::string name() const override
virtual void setSeed(long seed, int) override