LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
ComputePi_module.cc
Go to the documentation of this file.
1 
8 // C/C++ standard libraries
9 #include <random> // std::default_random_engine, std::uniform_real_distribution
10 #include <ios> // std::fixed
11 #include <iomanip> // std::setprecision
12 
13 // art libraries
14 #include "fhiclcpp/ParameterSet.h"
17 
18 
19 namespace lar {
20 
51  class ComputePi: public art::EDAnalyzer {
52  public:
53  using Counter_t = unsigned long long;
54  using Seed_t = std::default_random_engine::result_type;
56 
57  explicit ComputePi(fhicl::ParameterSet const & p);
58 
59  virtual ~ComputePi() = default;
60 
61  virtual void analyze(const art::Event&) override;
62 
64  double best_pi() const
65  { return tries? 4. * double(hits) / double(tries): 3.0; }
66 
68  Counter_t best_pi_tries() const { return tries; }
69 
70 
71  static const char* VersionString;
72 
73  private:
76  bool bFixed;
77  bool bVerbose;
78 
79  std::default_random_engine generator;
82 
83 
84  }; // class ComputePi
85 
86 } // namespace lar
87 
88 
89 //------------------------------------------------------------------------------
90 
92 
93 const char* lar::ComputePi::VersionString = "1.0";
94 
95 template <typename T>
96 inline constexpr T sqr(T v) { return v*v; }
97 
98 
100  EDAnalyzer(p),
101  samples(p.get<Counter_t>("Ksamples", 10000) * 1000),
102  seed(p.get<Seed_t>("Seed", 314159)),
103  bFixed(p.get<bool>("Fixed", false)),
104  bVerbose(p.get<bool>("Verbose", false)),
105  generator(seed)
106 {
107  mf::LogInfo("ComputePi")
108  << "version " << VersionString
109  << " using " << samples << " samples per event, random seed " << seed;
110 } // lar::ComputePi::ComputePi()
111 
112 
114 
115  // prepare our personal pseudo-random engine;
116  // we'll use always the same sequence!
117  std::uniform_real_distribution<float> flat(0.0, 1.0);
118 
119  // if we want to fix the random sequence, we reseed the generator
120  // with the same value over and over again
121  if (bFixed) generator.seed(seed);
122 
123  Counter_t local_hits = 0, tries_left = samples;
124  while (tries_left-- > 0) {
125  float x = flat(generator), y = flat(generator);
126  if (sqr(x) + sqr(y) < 1.0) ++local_hits;
127  } // while
128 
129  double local_pi = double(local_hits) / double(samples) * 4.0;
130  hits += local_hits;
131  tries += samples;
132 
133  if (bVerbose) {
134  mf::LogInfo("ComputePi") << "today's pi = "
135  << std::fixed << std::setprecision(9) << local_pi
136  << " (pi = "
137  << std::fixed << std::setprecision(12) << best_pi()
138  << " after " << best_pi_tries() << " samples)";
139  } // if verbose
140 
141 } // lar::ComputePi::analyze()
142 
Float_t x
Definition: compare.C:6
Counter_t tries
total number of tries (samples)
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
std::default_random_engine generator
random generator
Float_t y
Definition: compare.C:6
Counter_t best_pi_tries() const
Returns the current best estimation of pi.
unsigned long long Counter_t
type used for integral counters
Counter_t hits
total number of hits
virtual ~ComputePi()=default
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:42
double best_pi() const
Returns the current best estimation of pi.
bool bFixed
whether the random sequence is always the same
constexpr T sqr(T v)
Seed_t seed
number of digits to compute
Counter_t samples
number of samples to try on each event
virtual void analyze(const art::Event &) override
EDAnalyzer(Table< Config > const &config)
Definition: EDAnalyzer.h:100
Computes pi (but it does not make it available)
static const char * VersionString
version of the algorithm
bool bVerbose
whether to put stuff on screen
std::default_random_engine::result_type Seed_t
type for seed and random numbers
LArSoft-specific namespace.
ComputePi(fhicl::ParameterSet const &p)