5#include "GaudiKernel/ISvcLocator.h"
6#include "GaudiKernel/IIncidentSvc.h"
7#include "GaudiKernel/Incident.h"
8#include "GaudiKernel/DataIncident.h"
9#include "GaudiKernel/ServiceHandle.h"
11#include "CLHEP/Random/RanecuEngine.h"
12#include "CLHEP/Random/RandGauss.h"
28 : base_class(name,svc),
46 while (i != e)
delete (i++)->second;
54 (
"Initializing " << name()
55 <<
"\n INITIALISING RANDOM NUMBER STREAMS. ");
65 static const int PRIORITY = 100;
66 pIncSvc->addListener(
this,
"EndEvent", PRIORITY);
67 pIncSvc->addListener(
this,
"AfterReseedIncident", PRIORITY);
72 pIncSvc->addListener(
this,
"BeginEvent");
73 pIncSvc->addListener(
this,
"ReseedIncident", PRIORITY);
75 pIncSvc.release().ignore();
83 return StatusCode::FAILURE;
86 while (std::getline(infile, buffer)) {
88 uint32_t seed1, seed2;
91 (
" INITIALISING " << stream <<
" stream with seeds "
92 << seed1 <<
" " << seed2
97 (
"bad line\n" << buffer
99 return StatusCode::FAILURE;
109 uint32_t seed1, seed2, offset(0);
113 (
"Seeds property: stream " << stream
114 <<
" seeds " << seed1 <<
' ' << seed2
115 <<
", reseeding offset " << offset);
118 return StatusCode::FAILURE;
122 bool not_found =
true;
126 if ((*sf).first == stream) not_found =
false;
128 }
while (sf !=
end() && not_found);
133 (
" INITIALISING " << stream <<
" stream with seeds "
134 << seed1 <<
" " << seed2);
140 " to stream " << stream);
146 return StatusCode::SUCCESS;
152 if ( inc.type() ==
"EndEvent" ||
153 inc.type() ==
"AfterReseedIncident" )
159 CLHEP::HepRandomEngine* engine =
GetEngine((*i).first);
160 const long* s = engine->getSeeds();
161 std::vector<long int> tseeds;
162 tseeds.push_back(s[0]);
163 tseeds.push_back(s[1]);
165 std::map<std::string, std::vector<long int> >::value_type( (*i).first,
169 }
else if (inc.type() ==
"BeginEvent") {
171 EventContext context = inc.context();
172 const EventIDBase& ei = context.eventID();
174 CLHEP::RandGauss::setFlag(
false);
183 throw GaudiException(
"can not reseed all streams ", name(), StatusCode::FAILURE);
186 const string& strName(*i++);
190 throw GaudiException(
string(
"can not reseed stream ") + strName,
191 name(), StatusCode::FAILURE);
193 msg() << MSG::VERBOSE <<
"Reseeded stream " << strName
194 <<
" for random service " <<
endmsg;
199 else if (inc.type() ==
"ReseedIncident") {
200 typedef ContextIncident<std::pair<unsigned,unsigned> > Ctxt;
201 const Ctxt* incident =
dynamic_cast<const Ctxt*
>(&inc);
203 throw GaudiException(
string(
"can not cast to ContextIncident "),
204 name(), StatusCode::FAILURE);
206 const std::pair<unsigned,unsigned>&
data = incident->tag();
208 CLHEP::RandGauss::setFlag(
false);
217 throw GaudiException(
"can not reseed all streams ", name(), StatusCode::FAILURE);
220 const string& strName(*i++);
224 throw GaudiException(
string(
"can not reseed stream ") + strName,
225 name(), StatusCode::FAILURE);
227 msg() << MSG::VERBOSE <<
"Reseeded stream " << strName
228 <<
" for random service " <<
endmsg;
246 for (std::map<std::string, std::vector<long int> >::const_iterator i =
m_engines_copy.begin();
249 outfile << (*i).first <<
" " << (*i).second[0] <<
" " << (*i).second[1] <<
"\n";
254 return StatusCode::SUCCESS;
257CLHEP::HepRandomEngine*
268 return (CLHEP::HepRandomEngine*)(*iter).second;
273 const std::string& streamName )
279 long seeds[3] = { (long)seed1, (
long) seed2, 0 };
280 ((*iter).second)->setSeeds( seeds, -1 );
288 if (streamName ==
"PYTHIA")
293 else if (streamName ==
"HERWIG")
304 (
" INITIALISING " << streamName <<
" stream with DEFAULT seeds "
305 << seed1 <<
" " << seed2);
307 long seeds[3] = { seed1, seed2, 0 };
308 const long* s = seeds;
310 ((*iter).second)->setSeeds( s, -1 );
323 const long* s = ((*citer).second)->getSeeds();
324 ATH_MSG_INFO (
" Stream = " << streamName <<
", Seed1 = "
325 << s[0] <<
", Seed2 = " << s[1]);
336CLHEP::HepRandomEngine*
338 const std::string& streamName)
343 uint32_t theHash(eventNumber);
346 if (hasOffset) theHash=
crc_combine(theHash, citer->second);
350 <<
" with eventNumber " << eventNumber
351 <<
" runNumber " << runNumber);
356CLHEP::HepRandomEngine*
358 const std::string& streamName){
366 new CLHEP::RanecuEngine() ) );
369 ATH_MSG_DEBUG(
"Reseeding stream " << streamName <<
" with " << theSeed);
373 ((*iter).second)->setSeed( (int32_t)theSeed, 0);
374 return (CLHEP::HepRandomEngine*)(*iter).second;
377CLHEP::HepRandomEngine*
379 const std::string& streamName)
386 long seeds[3] = { (long)(1000*runNumber + hashedStream),
387 (long)eventNumber, 0 };
388 assert( seeds[0] > 0 );
389 assert( seeds[1] > 0 );
390 const long* s = seeds;
391 ((*iter).second)->setSeeds( s, -1 );
392 return (CLHEP::HepRandomEngine*)(*iter).second;
395CLHEP::HepRandomEngine*
397 const std::string& streamName){
403 long seeds[3] = { (long)(hashedStream % (theSeed+13)), (long)theSeed, 0 };
404 assert( seeds[0] > 0 );
405 assert( seeds[1] > 0 );
406 const long* s = seeds;
407 ((*iter).second)->setSeeds( s, -1 );
408 return (CLHEP::HepRandomEngine*)(*iter).second;
A random number engine manager, based on Ranecu.
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_WARNING(x)
uint32_t crc_combine(uint32_t seed, uint32_t v)
using crc32 for architecture independence in combining the seeds
char data[hepevt_bytes_allocation_ATLAS]
long m_HERWIG_default_seed1
Gaudi::Property< bool > m_read_from_file
read engine status from file
AtRndmGenSvc(const std::string &name, ISvcLocator *svc)
Standard Constructor.
std::map< std::string, uint32_t > m_reseedingOffsets
optional offsets to combine to run/evt no when reseeding.
virtual bool setAllOnDefinedSeeds(uint32_t theSeed) override
seed all streams we manage, combining theSeed and the stream names
StringArrayProperty m_streams_seeds
Gaudi::Property< bool > m_save_to_file
should current engine status be saved to file ?
long m_HERWIG_default_seed2
Gaudi::Property< std::string > m_file_to_read
name of the file to read the engine status from
engineMap::const_iterator engineConstIter
long m_PYTHIA_default_seed1
virtual void handle(const Incident &) override
IIncidentListener implementation. Handles EndEvent incident.
virtual StatusCode finalize() override
unsigned int number_of_streams(void) const
virtual void print(void) override
engineConstIter begin(void) const
StringArrayProperty m_reseedStreamNames
streams to be reseeded for every event
engineMap::iterator engineIter
void SetStreamSeeds(const std::string &streamName)
Gaudi::Property< std::string > m_file_to_write
name of the file to save the engine status to.
virtual void CreateStream(uint32_t seed1, uint32_t seed2, const std::string &streamName) override
virtual CLHEP::HepRandomEngine * GetEngine(const std::string &streamName) override
virtual CLHEP::HepRandomEngine * setOnDefinedSeeds(uint32_t theSeed, const std::string &streamName) override
virtual StatusCode initialize() override
Gaudi::Property< bool > m_useOldBrokenSeeding
backward compatibility only, broken 32/64 bits
virtual ~AtRndmGenSvc()
Standard Destructor.
engineMap::value_type engineValType
CLHEP::HepRandomEngine * oldSetOnDefinedSeeds(uint32_t theSeed, const std::string &streamName)
broken, temporarily keep for backward compatibility
std::map< std::string, std::vector< long int > > m_engines_copy
Random engine copy (for output to a file)
long m_PYTHIA_default_seed2
engineConstIter end(void) const
Gaudi::Property< bool > m_eventReseed
reseed for every event
bool interpretSeeds(const std::string &buffer, std::string &stream, uint32_t &seed1, uint32_t &seed2, short &luxury, uint32_t &offset)
int simpleStringHash(const std::string &str, int maxInt=0xFFFF)
simple hash function derived from Sedgewick Algorithms in C++ 3rd ed