14 #include "messagefacility/MessageLogger/MessageLogger.h"
15 #include "fhiclcpp/ParameterSet.h"
16 #include "art/Framework/Core/EDAnalyzer.h"
17 #include "art/Framework/Core/ModuleMacros.h"
55 using Seed_t = std::default_random_engine::result_type;
58 explicit ComputePi(fhicl::ParameterSet
const &
p);
62 virtual void analyze(
const art::Event&)
override;
94 const char* lar::ComputePi::VersionString = "1.0";
97 inline constexpr T
sqr(T v) {
return v*v; }
108 mf::LogInfo(
"ComputePi")
110 <<
" using " <<
samples <<
" samples per event, random seed " <<
seed;
118 std::uniform_real_distribution<float> flat(0.0, 1.0);
124 Counter_t local_hits = 0, tries_left = samples;
125 while (tries_left-- > 0) {
127 if (
sqr(x) +
sqr(
y) < 1.0) ++local_hits;
130 double local_pi = double(local_hits) / double(samples) * 4.0;
135 mf::LogInfo(
"ComputePi") <<
"today's pi = "
136 << std::fixed << std::setprecision(9) << local_pi
138 << std::fixed << std::setprecision(12) << best_pi()
139 <<
" after " << best_pi_tries() <<
" samples)";
Counter_t tries
total number of tries (samples)
process_name opflash particleana ie x
std::default_random_engine generator
random generator
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
process_name opflash particleana ie ie y
double best_pi() const
Returns the current best estimation of pi.
bool bFixed
whether the random sequence is always the same
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
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
ComputePi(fhicl::ParameterSet const &p)