ATLAS Offline Software
Loading...
Searching...
No Matches
AthRNGSvc.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
3*/
4
5#include "AthRNGSvc.h"
8
10#include "CLHEP/Random/MixMaxRng.h"
11#include "CLHEP/Random/Ranlux64Engine.h"
12#include "CLHEP/Random/RanecuEngine.h"
13
14
15AthRNGSvc::AthRNGSvc(const std::string& name, ISvcLocator* svc)
16 : base_class(name, svc),
17 m_rngType("dSFMT")
18{
19 declareProperty("EngineType", m_rngType, "CLHEP RandomEngine type");
20}
21
23{
24 if(m_rngType == "dSFMT") {
25 m_fact = [](void)->CLHEP::HepRandomEngine*{
26 return new CLHEP::dSFMTEngine();
27 };
28 } else if(m_rngType == "Ranlux64") {
29 m_fact = [](void)->CLHEP::HepRandomEngine*{
30 return new CLHEP::Ranlux64Engine();
31 };
32 } else if(m_rngType == "Ranecu") {
33 m_fact = [](void)->CLHEP::HepRandomEngine*{
34 return new CLHEP::RanecuEngine();
35 };
36 } else if(m_rngType == "MixMax") {
37 m_fact = [](void)->CLHEP::HepRandomEngine*{
38 return new CLHEP::MixMaxRng();
39 };
40 } else {
41 ATH_MSG_WARNING("Supported Generator types are 'dSFMT', 'Ranlux64', 'Ranecu', 'MixMax'");
42 ATH_MSG_FATAL("Generator type \"" << m_rngType << "\" is not known. Check Joboptions");
43 return StatusCode::FAILURE;
44 }
45
46 return StatusCode::SUCCESS;
47}
48
49ATHRNG::RNGWrapper* AthRNGSvc::getEngine(const INamedInterface* client,
50 const std::string& streamName)
51{
52 // Retrieve number of event slots
53 size_t numSlots = SG::getNSlots();
54
55 // The name key combines the client name and optional stream name
56 std::string rngName = client->name();
57 if(!streamName.empty()) rngName += "/" + streamName;
58
59 // Look for an existing RNG stream
60 std::lock_guard<std::mutex> lock(m_mutex);
61 auto it = m_wrappers.find(rngName);
62
63 // Construct a new RNGWrapper if none exists
64 if(it == m_wrappers.end()){
65 ATH_MSG_INFO("Creating engine " << rngName);
66 auto wrp = new ATHRNG::RNGWrapper(m_fact, numSlots);
67 m_wrappers.insert( std::make_pair(rngName, wrp) );
68 return wrp;
69 }
70
71 ATH_MSG_DEBUG("Returning engine " << rngName);
72 return it->second;
73}
74
75void AthRNGSvc::printEngineState(const INamedInterface* client,
76 const std::string& streamName)
77{
78 // Retrieve the current slot's engine
79 ATHRNG::RNGWrapper* wrapper = getEngine(client, streamName);
80 CLHEP::HepRandomEngine* engine( *wrapper );
81
82 // Extract the engine state numbers
83 std::vector<unsigned long> rngStates = engine->put();
84
85 // Print the state numbers
86 std::string rngName = client->name();
87 if(!streamName.empty()) rngName += "/" + streamName;
88 std::lock_guard<std::mutex> lock(m_mutex);
89 msg(MSG::ALWAYS) << rngName << " ";
90 // We mask 32 bits because the other bits are garbage and unused
91 for(const unsigned long s : rngStates) {
92 msg() << (s & 0xffffffffu) << " ";
93 }
94 msg() << endmsg;
95}
96
98{
99 // Clean up the RNGWrappers and HepRandomEngines
100 for(auto& wrapperPair : m_wrappers) {
101 delete wrapperPair.second;
102 }
103}
#define endmsg
#define ATH_MSG_FATAL(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
Maintain a set of objects, one per slot.
A wrapper class for event-slot-local random engines.
Definition RNGWrapper.h:56
virtual void printEngineState(const INamedInterface *client, const std::string &streamName="") override final
Print engine state.
Definition AthRNGSvc.cxx:75
std::mutex m_mutex
Mutex for protecting access to the wrapper structure.
Definition AthRNGSvc.h:66
AthRNGSvc(const std::string &name, ISvcLocator *svc)
Standard constructor.
Definition AthRNGSvc.cxx:15
std::unordered_map< std::string, ATHRNG::RNGWrapper * > m_wrappers
The structure for storing the RNGWrappers.
Definition AthRNGSvc.h:59
factoryFunc m_fact
Definition AthRNGSvc.h:63
std::string m_rngType
Random number engine type (e.g. dSFMT, ranecu)
Definition AthRNGSvc.h:56
virtual ~AthRNGSvc()
Definition AthRNGSvc.cxx:97
StatusCode initialize() override final
Initialize the service.
Definition AthRNGSvc.cxx:22
virtual ATHRNG::RNGWrapper * getEngine(const INamedInterface *client, const std::string &streamName="") override final
IAthRNGSvc method to retrieve the random number wrapper.
Definition AthRNGSvc.cxx:49
size_t getNSlots()
Return the number of event slots.
MsgStream & msg
Definition testRead.cxx:32