EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
HelloRandomAlgorithm.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file HelloRandomAlgorithm.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2017-2019 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
10 
13 
14 #include <random>
15 
16 #include "HelloData.hpp"
17 
20  : BareAlgorithm("HelloRandom", level), m_cfg(cfg) {
21  if (!m_cfg.randomNumbers) {
22  throw std::invalid_argument("Missing random number service");
23  }
24  if (m_cfg.output.empty()) {
25  throw std::invalid_argument("Missing output collection");
26  }
27 }
28 
30  const AlgorithmContext& ctx) const {
31  ACTS_INFO("Running random number generation");
32 
33  // Create the local random number generator
34  ActsExamples::RandomEngine rng = m_cfg.randomNumbers->spawnGenerator(ctx);
35 
36  // Spawn some random number distributions
37  std::normal_distribution<double> gaussDist(m_cfg.gaussParameters[0],
38  m_cfg.gaussParameters[1]);
39  std::uniform_real_distribution<double> uniformDist(
40  m_cfg.uniformParameters[0], m_cfg.uniformParameters[1]);
41  std::gamma_distribution<double> gammaDist(m_cfg.gammaParameters[0],
42  m_cfg.gammaParameters[1]);
43  std::poisson_distribution<int> poissonDist(m_cfg.poissonParameter);
44 
45  ACTS_INFO(m_cfg.drawsPerEvent << " draws per event will be done");
46 
47  // generate collection of random numbers
48  HelloDataCollection collection;
49  for (size_t idraw = 0; idraw < m_cfg.drawsPerEvent; ++idraw) {
50  double gauss = gaussDist(rng);
51  double uniform = uniformDist(rng);
52  double gamma = gammaDist(rng);
53  int poisson = poissonDist(rng);
54 
55  ACTS_VERBOSE("Gauss : " << gauss);
56  ACTS_VERBOSE("Uniform : " << uniform);
57  ACTS_VERBOSE("Gamma : " << gamma);
58  ACTS_VERBOSE("Poisson : " << poisson);
59 
60  HelloData x;
61  x.x = gauss;
62  x.a = uniform;
63  x.b = gamma;
64  x.t = poisson;
65  collection.push_back(x);
66  }
67 
68  // transfer generated data to the event store.
69  ctx.eventStore.add(m_cfg.output, std::move(collection));
70 
72 }