Analysis Software
Documentation for sPHENIX simulation 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  : IAlgorithm("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  }
28 }
29 
31  const AlgorithmContext& ctx) const {
32  ACTS_INFO("Running random number generation");
33 
34  // Create the local random number generator
35  ActsExamples::RandomEngine rng = m_cfg.randomNumbers->spawnGenerator(ctx);
36 
37  // Spawn some random number distributions
38  std::normal_distribution<double> gaussDist(m_cfg.gaussParameters[0],
39  m_cfg.gaussParameters[1]);
40  std::uniform_real_distribution<double> uniformDist(
41  m_cfg.uniformParameters[0], m_cfg.uniformParameters[1]);
42  std::gamma_distribution<double> gammaDist(m_cfg.gammaParameters[0],
43  m_cfg.gammaParameters[1]);
44  std::poisson_distribution<int> poissonDist(m_cfg.poissonParameter);
45 
46  ACTS_INFO(m_cfg.drawsPerEvent << " draws per event will be done");
47 
48  // generate collection of random numbers
49  HelloDataCollection collection;
50  for (size_t idraw = 0; idraw < m_cfg.drawsPerEvent; ++idraw) {
51  double gauss = gaussDist(rng);
52  double uniform = uniformDist(rng);
53  double gamma = gammaDist(rng);
54  int poisson = poissonDist(rng);
55 
56  ACTS_VERBOSE("Gauss : " << gauss);
57  ACTS_VERBOSE("Uniform : " << uniform);
58  ACTS_VERBOSE("Gamma : " << gamma);
59  ACTS_VERBOSE("Poisson : " << poisson);
60 
61  HelloData x{};
62  x.x = gauss;
63  x.a = uniform;
64  x.b = gamma;
65  x.t = poisson;
66  collection.push_back(x);
67  }
68 
69  // transfer generated data to the event store.
70  m_writeHandle(ctx, std::move(collection));
71 
73 }