ECCE @ 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 
11 #include <random>
12 
15 #include "HelloData.hpp"
16 
19  : BareAlgorithm("HelloRandom", level), m_cfg(cfg) {
20  if (!m_cfg.randomNumbers) {
21  throw std::invalid_argument("Missing random number service");
22  }
23  if (m_cfg.output.empty()) {
24  throw std::invalid_argument("Missing output collection");
25  }
26 }
27 
29  const AlgorithmContext& ctx) const {
30  ACTS_INFO("Running random number generation");
31 
32  // Create the local random number generator
33  FW::RandomEngine rng = m_cfg.randomNumbers->spawnGenerator(ctx);
34 
35  // Spawn some random number distributions
36  std::normal_distribution<double> gaussDist(m_cfg.gaussParameters[0],
37  m_cfg.gaussParameters[1]);
38  std::uniform_real_distribution<double> uniformDist(
39  m_cfg.uniformParameters[0], m_cfg.uniformParameters[1]);
40  std::gamma_distribution<double> gammaDist(m_cfg.gammaParameters[0],
41  m_cfg.gammaParameters[1]);
42  std::poisson_distribution<int> poissonDist(m_cfg.poissonParameter);
43 
44  ACTS_INFO(m_cfg.drawsPerEvent << " draws per event will be done");
45 
46  // generate collection of random numbers
47  HelloDataCollection collection;
48  for (size_t idraw = 0; idraw < m_cfg.drawsPerEvent; ++idraw) {
49  double gauss = gaussDist(rng);
50  double uniform = uniformDist(rng);
51  double gamma = gammaDist(rng);
52  int poisson = poissonDist(rng);
53 
54  ACTS_VERBOSE("Gauss : " << gauss);
55  ACTS_VERBOSE("Uniform : " << uniform);
56  ACTS_VERBOSE("Gamma : " << gamma);
57  ACTS_VERBOSE("Poisson : " << poisson);
58 
59  HelloData x;
60  x.x = gauss;
61  x.a = uniform;
62  x.b = gamma;
63  x.t = poisson;
64  collection.push_back(x);
65  }
66 
67  // transfer generated data to the event store.
68  ctx.eventStore.add(m_cfg.output, std::move(collection));
69 
71 }