Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:11:49

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 #include "HelloRandomAlgorithm.hpp"
0010 
0011 #include "ActsExamples/Framework/RandomNumbers.hpp"
0012 #include "ActsExamples/Framework/WhiteBoard.hpp"
0013 
0014 #include <random>
0015 
0016 #include "HelloData.hpp"
0017 
0018 ActsExamples::HelloRandomAlgorithm::HelloRandomAlgorithm(
0019     const HelloRandomAlgorithm::Config& cfg, Acts::Logging::Level level)
0020     : IAlgorithm("HelloRandom", level), m_cfg(cfg) {
0021   if (!m_cfg.randomNumbers) {
0022     throw std::invalid_argument("Missing random number service");
0023   }
0024   if (m_cfg.output.empty()) {
0025     throw std::invalid_argument("Missing output collection");
0026   }
0027   m_writeHandle.initialize(m_cfg.output);
0028 }
0029 
0030 ActsExamples::ProcessCode ActsExamples::HelloRandomAlgorithm::execute(
0031     const AlgorithmContext& ctx) const {
0032   ACTS_INFO("Running random number generation");
0033 
0034   // Create the local random number generator
0035   ActsExamples::RandomEngine rng = m_cfg.randomNumbers->spawnGenerator(ctx);
0036 
0037   // Spawn some random number distributions
0038   std::normal_distribution<double> gaussDist(m_cfg.gaussParameters[0],
0039                                              m_cfg.gaussParameters[1]);
0040   std::uniform_real_distribution<double> uniformDist(
0041       m_cfg.uniformParameters[0], m_cfg.uniformParameters[1]);
0042   std::gamma_distribution<double> gammaDist(m_cfg.gammaParameters[0],
0043                                             m_cfg.gammaParameters[1]);
0044   std::poisson_distribution<int> poissonDist(m_cfg.poissonParameter);
0045 
0046   ACTS_INFO(m_cfg.drawsPerEvent << " draws per event will be done");
0047 
0048   // generate collection of random numbers
0049   HelloDataCollection collection;
0050   for (std::size_t idraw = 0; idraw < m_cfg.drawsPerEvent; ++idraw) {
0051     double gauss = gaussDist(rng);
0052     double uniform = uniformDist(rng);
0053     double gamma = gammaDist(rng);
0054     int poisson = poissonDist(rng);
0055 
0056     ACTS_VERBOSE("Gauss   : " << gauss);
0057     ACTS_VERBOSE("Uniform : " << uniform);
0058     ACTS_VERBOSE("Gamma   : " << gamma);
0059     ACTS_VERBOSE("Poisson : " << poisson);
0060 
0061     HelloData x{};
0062     x.x = gauss;
0063     x.a = uniform;
0064     x.b = gamma;
0065     x.t = poisson;
0066     collection.push_back(x);
0067   }
0068 
0069   // transfer generated data to the event store.
0070   m_writeHandle(ctx, std::move(collection));
0071 
0072   return ActsExamples::ProcessCode::SUCCESS;
0073 }