ATLAS Offline Software
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 
15 AthRNGSvc::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 
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 
75 void AthRNGSvc::printEngineState(const INamedInterface* client,
76  const std::string& streamName)
77 {
78  // Retrieve the current slot's engine
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 }
AthRNGSvc::AthRNGSvc
AthRNGSvc(const std::string &name, ISvcLocator *svc)
Standard constructor.
Definition: AthRNGSvc.cxx:15
dSFMTEngine.h
CLHEP::dSFMTEngine
Definition: dSFMTEngine.h:31
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
skel.it
it
Definition: skel.GENtoEVGEN.py:423
AthRNGSvc::getEngine
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
AthRNGSvc.h
AthRNGSvc::m_fact
factoryFunc m_fact
Definition: AthRNGSvc.h:63
Trk::u
@ u
Enums for curvilinear frames.
Definition: ParamDefs.h:83
rerun_display.client
client
Definition: rerun_display.py:31
AthRNGSvc::~AthRNGSvc
virtual ~AthRNGSvc()
Definition: AthRNGSvc.cxx:97
TrigConf::MSGTC::ALWAYS
@ ALWAYS
Definition: Trigger/TrigConfiguration/TrigConfBase/TrigConfBase/MsgStream.h:29
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
AthRNGSvc::initialize
StatusCode initialize() override final
Initialize the service.
Definition: AthRNGSvc.cxx:22
AthRNGSvc::m_rngType
std::string m_rngType
Random number engine type (e.g. dSFMT, ranecu)
Definition: AthRNGSvc.h:56
Handler::svc
AthROOTErrorHandlerSvc * svc
Definition: AthROOTErrorHandlerSvc.cxx:10
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:192
ATHRNG::RNGWrapper
A wrapper class for event-slot-local random engines.
Definition: RNGWrapper.h:56
RNGWrapper.h
AthenaPoolExample_Copy.streamName
string streamName
Definition: AthenaPoolExample_Copy.py:39
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
AthRNGSvc::m_wrappers
std::unordered_map< std::string, ATHRNG::RNGWrapper * > m_wrappers
The structure for storing the RNGWrappers.
Definition: AthRNGSvc.h:59
AthRNGSvc::m_mutex
std::mutex m_mutex
Mutex for protecting access to the wrapper structure.
Definition: AthRNGSvc.h:66
SG::getNSlots
size_t getNSlots()
Return the number of event slots.
Definition: SlotSpecificObj.cxx:64
declareProperty
#define declareProperty(n, p, h)
Definition: BaseFakeBkgTool.cxx:15
SlotSpecificObj.h
Maintain a set of objects, one per slot.
python.AutoConfigFlags.msg
msg
Definition: AutoConfigFlags.py:7
AthRNGSvc::printEngineState
virtual void printEngineState(const INamedInterface *client, const std::string &streamName="") override final
Print engine state.
Definition: AthRNGSvc.cxx:75