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"
16#include "CLHEP/Random/RandGauss.h"
28 : base_class(name,svc),
45 while (i != e)
delete (i++)->second;
51 (
"Initializing " << name() <<
"\n INITIALISING RANDOM NUMBER STREAMS. ");
60 static const int PRIORITY = 100;
61 pIncSvc->addListener(
this,
"EndEvent", PRIORITY);
62 pIncSvc->addListener(
this,
"AfterReseedIncident", PRIORITY);
67 pIncSvc->addListener(
this,
"BeginEvent", PRIORITY);
68 pIncSvc->addListener(
this,
"ReseedIncident", PRIORITY);
70 pIncSvc.release().ignore();
77 return StatusCode::FAILURE;
80 while (std::getline(infile, buffer)) {
82 std::vector<uint32_t> seeds;
86 <<
" INITIALISING " << stream <<
" stream with seeds ";
87 for (std::vector<uint32_t>::const_iterator i = seeds.begin(); i != seeds.end(); ++i){
95 msg() << ((*i) & 0xffffffffu) <<
" ";
100 << stream <<
" stream initialized succesfully" <<
endmsg;
103 << stream <<
" stream FAILED to initialize" <<
endmsg;
104 return StatusCode::FAILURE;
108 <<
"bad line\n" << buffer
110 return StatusCode::FAILURE;
119 uint32_t seed1, seed2, offset(0);
123 <<
" seeds " << seed1 <<
' ' << seed2
124 <<
", reseeding offset " << offset);
127 return StatusCode::FAILURE;
131 bool not_found(
true);
134 while (sf !=
end() && (not_found=((*sf).first != stream))) ++sf;
139 (
" INITIALISING " << stream <<
" stream with seeds "
140 << seed1 <<
" " << seed2);
146 " to stream " << stream);
152 return StatusCode::SUCCESS;
158 if ( inc.type() ==
"EndEvent" ||
159 inc.type() ==
"AfterReseedIncident" )
164 CLHEP::HepRandomEngine* engine =
GetEngine(iE->first);
165 std::vector<unsigned long> v = engine->put();
166 std::vector<uint32_t> tseeds(v.size());
167 for (
size_t i=0; i<v.size(); ++i) {
175 tseeds[i] = (v[i] & 0xffffffffu);
182 }
else if (inc.type() ==
"BeginEvent") {
184 EventContext context = inc.context();
185 const EventIDBase& ei = context.eventID();
187 CLHEP::RandGauss::setFlag(
false);
196 throw GaudiException(
"can not reseed all streams ", name(), StatusCode::FAILURE);
199 const string& strName(*i++);
203 throw GaudiException(
string(
"can not reseed stream ") + strName,
204 name(), StatusCode::FAILURE);
206 msg() << MSG::VERBOSE <<
"Reseeded stream " << strName
207 <<
" for random service " <<
endmsg;
212 else if (inc.type() ==
"ReseedIncident") {
213 typedef ContextIncident<std::pair<unsigned,unsigned> > Ctxt;
214 const Ctxt* incident =
dynamic_cast<const Ctxt*
>(&inc);
216 throw GaudiException(
string(
"can not cast to ContextIncident "),
217 name(), StatusCode::FAILURE);
219 const std::pair<unsigned,unsigned>&
data = incident->tag();
221 CLHEP::RandGauss::setFlag(
false);
230 throw GaudiException(
"can not reseed all streams ", name(), StatusCode::FAILURE);
233 const string& strName(*i++);
237 throw GaudiException(
string(
"can not reseed stream ") + strName,
238 name(), StatusCode::FAILURE);
240 msg() << MSG::VERBOSE <<
"Reseeded stream " << strName
241 <<
" for random service " <<
endmsg;
258 for (std::map<std::string, std::vector<uint32_t> >::const_iterator i =
m_engines_copy.begin();
261 outfile << (*i).first <<
" ";
262 for (std::vector<uint32_t>::const_iterator j = (*i).second.begin(); j !=(*i).second.end(); ++j){
263 outfile << (*j) <<
" ";
270 return StatusCode::SUCCESS;
283 return (CLHEP::HepRandomEngine*)(*iter).second;
288 long seeds[3] = { (long)seed1, (
long)seed2, 0 };
289 const long* s = seeds;
294 ((*iter).second)->setSeeds(s, 0);
302 std::vector<unsigned long> longSeeds(seeds.size());
303 for (
size_t i=0; i<seeds.size(); ++i) longSeeds[i]=seeds[i];
304 return (((*iter).second)->getState( longSeeds ));
311 if (StreamName ==
"PYTHIA")
316 else if (StreamName ==
"HERWIG")
327 (
" INITIALISING " << StreamName <<
" stream with DEFAULT seeds "
328 << seed1 <<
" " << seed2);
330 long seeds[3] = { (long)seed1, (
long)seed2, 0 };
331 const long* s = seeds;
333 ((*iter).second)->setSeeds(s,0);
342 std::vector<unsigned long> v = ((*citer).second)->put();
343 msg(MSG::DEBUG) <<
" Stream = " << StreamName <<
" ";
344 for (std::vector<unsigned long>::const_iterator i = v.begin(); i != v.end(); ++i){
352 msg() << ((*i) & 0xffffffffu) <<
" ";
365 const std::string& streamName)
367 uint32_t theHash(eventNumber);
370 if (hasOffset) theHash=
crc_combine(theHash, citer->second);
374 <<
" with eventNumber " << eventNumber
375 <<
" runNumber " << runNumber);
381 const std::string& streamName){
389 ATH_MSG_DEBUG(
"Reseeding stream " << streamName <<
" with " << theSeed);
390 CLHEP::HepRandomEngine* eng = (*iter).second;
391 eng->setSeed( theSeed, 0 );
A random number engine manager, based on dSFMT.
#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]
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 handle(const Incident &) override
IIncidentListener implementation. Handles EndEvent incident.
engineConstIter end(void) const
unsigned int number_of_streams(void) const
long m_PYTHIA_default_seed1
virtual StatusCode initialize() override
engineMap::const_iterator engineConstIter
virtual void print(void) override
engineMap::value_type engineValType
Gaudi::Property< bool > m_eventReseed
reseed for every event
virtual void CreateStream(uint32_t seed1, uint32_t seed2, const std::string &streamName) override
long m_PYTHIA_default_seed2
virtual CLHEP::HepRandomEngine * setOnDefinedSeeds(uint32_t theSeed, const std::string &streamName) override
virtual ~AtDSFMTGenSvc()
Standard Destructor.
virtual bool setAllOnDefinedSeeds(uint32_t theSeed) override
seed all streams we manage, combining theSeed and the stream names
std::map< std::string, uint32_t > m_reseedingOffsets
optional offsets to combine to run/evt no when reseeding.
virtual StatusCode finalize() override
Gaudi::Property< bool > m_read_from_file
read engine status from file
Gaudi::Property< bool > m_save_to_file
should current engine status be saved to file ?
virtual CLHEP::HepRandomEngine * GetEngine(const std::string &streamName) override
engineMap::iterator engineIter
long m_HERWIG_default_seed2
StringArrayProperty m_reseedStreamNames
streams to be reseeded for every event
engineConstIter begin(void) const
std::map< std::string, std::vector< uint32_t > > m_engines_copy
Random engine copy (for output to a file)
StringArrayProperty m_streams_seeds
seeds for the engines, this is a vector of strings of the form "EnginName Seed1 Seed2"
Gaudi::Property< std::string > m_file_to_read
name of the file to read the engine status from
long m_HERWIG_default_seed1
AtDSFMTGenSvc(const std::string &name, ISvcLocator *svc)
Standard Constructor.
bool interpretSeeds(const std::string &buffer, std::string &stream, uint32_t &seed1, uint32_t &seed2, short &luxury, uint32_t &offset)