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/Ranlux64Engine.h"
12 #include "CLHEP/Random/RandGauss.h"
32 m_engines(), m_engines_copy(),
57 (
"Initializing " <<
name()
58 <<
"\n INITIALISING RANDOM NUMBER STREAMS. ");
67 static const int PRIORITY = 100;
68 pIncSvc->addListener(
this,
"EndEvent", PRIORITY);
69 pIncSvc->addListener(
this,
"AfterReseedIncident", PRIORITY);
74 pIncSvc->addListener(
this,
"BeginEvent", PRIORITY);
75 pIncSvc->addListener(
this,
"ReseedIncident", PRIORITY);
77 pIncSvc.release().ignore();
81 if (m_useOldBrokenSeeding) m_defaultLuxLevel = 3;
83 if (m_read_from_file) {
85 ifstream
infile( m_file_to_read.value().c_str() );
87 ATH_MSG_ERROR (
" Unable to open: " << m_file_to_read.value());
88 return StatusCode::FAILURE;
93 std::vector<uint32_t> seeds;
97 <<
" INITIALISING " <<
stream <<
" stream with seeds ";
98 for (std::vector<uint32_t>::const_iterator
i = seeds.begin();
i != seeds.end(); ++
i){
106 msg() << ((*i) & 0xffffffff
u) <<
" ";
108 msg() <<
" read from file " << m_file_to_read.value() <<
endmsg;
109 if (CreateStream(seeds,
stream)) {
111 <<
stream <<
" stream initialized succesfully" <<
endmsg;
115 return StatusCode::FAILURE;
120 <<
"\n in input file " << m_file_to_read.value() <<
endmsg;
121 return StatusCode::FAILURE;
129 for (
const auto&
i : m_streams_seeds) {
133 short ll(m_defaultLuxLevel);
137 <<
", luxury level " <<
ll
138 <<
", reseeding offset " <<
offset);
141 return StatusCode::FAILURE;
145 bool not_found(
true);
146 if ( number_of_streams() != 0 ) {
148 while (
sf !=
end() && (not_found=((*sf).first !=
stream))) ++
sf;
153 (
" INITIALISING " <<
stream <<
" stream with seeds "
157 m_reseedingOffsets.insert(std::make_pair(
stream,
offset));
166 return StatusCode::SUCCESS;
173 if ( inc.type() ==
"EndEvent" ||
174 inc.type() ==
"AfterReseedIncident" )
177 m_engines_copy.clear();
180 CLHEP::HepRandomEngine* engine = GetEngine(iE->first);
181 std::vector<unsigned long>
v = engine->put();
182 std::vector<uint32_t> tseeds(
v.size());
183 for (
size_t i=0;
i<
v.size(); ++
i) {
191 tseeds[
i] = (
v[
i] & 0xffffffff
u);
193 m_engines_copy.insert(std::make_pair(iE->first, tseeds));
198 }
else if (inc.type() ==
"BeginEvent") {
200 EventContext context = inc.context();
201 const EventIDBase& ei = context.eventID();
205 vector<string>::const_iterator
i(m_reseedStreamNames.begin());
206 vector<string>::const_iterator
e(m_reseedStreamNames.end());
210 if (!(this->setAllOnDefinedSeeds(ei.event_number(),
212 throw GaudiException(
"can not reseed all streams ",
name(), StatusCode::FAILURE);
215 const string& strName(*
i++);
216 if (0 == this->setOnDefinedSeeds(ei.event_number(),
219 throw GaudiException(
string(
"can not reseed stream ") + strName,
220 name(), StatusCode::FAILURE);
223 <<
" for random service " <<
endmsg;
228 else if (inc.type() ==
"ReseedIncident") {
229 typedef ContextIncident<std::pair<unsigned,unsigned> > Ctxt;
230 const Ctxt* incident =
dynamic_cast<const Ctxt*
>(&inc);
231 const std::pair<unsigned,unsigned>&
data = incident->tag();
235 vector<string>::const_iterator
i(m_reseedStreamNames.begin());
236 vector<string>::const_iterator
e(m_reseedStreamNames.end());
240 if (!(this->setAllOnDefinedSeeds(
data.first,
242 throw GaudiException(
"can not reseed all streams ",
name(), StatusCode::FAILURE);
245 const string& strName(*
i++);
246 if (0 == this->setOnDefinedSeeds(
data.first,
249 throw GaudiException(
string(
"can not reseed stream ") + strName,
250 name(), StatusCode::FAILURE);
253 <<
" for random service " <<
endmsg;
265 if (m_save_to_file) {
267 std::ofstream
outfile( m_file_to_write.value().c_str() );
269 ATH_MSG_ERROR (
"error: unable to open: " << m_file_to_write.value());
271 for (std::map<std::string, std::vector<uint32_t> >::const_iterator
i = m_engines_copy.begin();
272 i != m_engines_copy.end();
275 for (std::vector<uint32_t>::const_iterator j = (*i).second.begin(); j !=(*i).second.end(); ++j){
280 ATH_MSG_DEBUG (
" wrote seeds to " << m_file_to_write.value() );
284 return StatusCode::SUCCESS;
287 CLHEP::HepRandomEngine*
291 if ( citer == m_engines.end() )
298 return (CLHEP::HepRandomEngine*)(*iter).second;
309 const std::string&
streamName,
short luxLevel )
314 if ( citer == m_engines.end() )
316 new CLHEP::Ranlux64Engine(
s[0], luxLevel)));
318 ((*iter).second)->setSeed(
s[0], luxLevel);
326 if ( citer == m_engines.end() )
328 new CLHEP::Ranlux64Engine() ) );
330 std::vector<unsigned long> longSeeds(seeds.size());
331 for (
size_t i=0;
i<seeds.size(); ++
i) longSeeds[
i]=seeds[
i];
332 return (((*iter).second)->getState( longSeeds ));
342 seed1 = m_PYTHIA_default_seed1;
343 seed2 = m_PYTHIA_default_seed2;
347 seed1 = m_HERWIG_default_seed1;
348 seed2 = m_HERWIG_default_seed2;
352 seed1 = m_default_seed1;
353 seed2 = m_default_seed2;
356 (
" INITIALISING " <<
StreamName <<
" stream with DEFAULT seeds "
362 ((*iter).second)->setSeed(
s[0]);
368 engineConstIter citer = m_engines.find(
StreamName);
369 if ( citer == m_engines.end() ) {
372 std::vector<unsigned long>
v = ((*citer).second)->put();
374 for (std::vector<unsigned long>::const_iterator
i =
v.begin();
i !=
v.end(); ++
i){
382 msg() << ((*i) & 0xffffffff
u) <<
" ";
391 for (engineConstIter
i = m_engines.begin();
i != m_engines.end(); ++
i)
395 CLHEP::HepRandomEngine*
399 if (m_useOldBrokenSeeding)
403 map<string, uint32_t>::const_iterator citer(m_reseedingOffsets.find(
streamName));
404 bool hasOffset(citer != m_reseedingOffsets.end() && 0 != citer->second);
405 if (hasOffset) theHash=
crc_combine(theHash, citer->second);
412 return this->setOnDefinedSeeds(theHash,
streamName);
415 CLHEP::HepRandomEngine*
418 if (m_useOldBrokenSeeding)
419 return oldSetOnDefinedSeeds(theSeed,
streamName);
421 engineConstIter citer = m_engines.find(
streamName);
423 if ( citer == m_engines.end() )
425 new CLHEP::Ranlux64Engine() ) );
430 CLHEP::Ranlux64Engine* eng = (*iter).second;
434 eng->setSeed( (int32_t)theSeed, eng->getLuxury() );
435 return (CLHEP::HepRandomEngine*)eng;
439 CLHEP::HepRandomEngine*
444 if ( citer == m_engines.end() )
448 long seeds[2] = { (long)(1000*
runNumber + hashedStream),
450 assert( seeds[0] > 0 );
451 assert( seeds[1] > 0 );
452 const long*
s = seeds;
453 ((*iter).second)->setSeed(
s[0], m_defaultLuxLevel);
454 return (CLHEP::HepRandomEngine*)(*iter).second;
457 CLHEP::HepRandomEngine*
461 if ( citer == m_engines.end() )
465 long seeds[2] = { (long)(hashedStream % (theSeed+13)),
467 assert( seeds[0] > 0 );
468 assert( seeds[1] > 0 );
469 const long*
s = seeds;
470 ((*iter).second)->setSeed(
s[0], m_defaultLuxLevel );
471 return (CLHEP::HepRandomEngine*)(*iter).second;
479 engineIter
i(m_engines.begin()),
e(m_engines.end());
492 engineIter
i(m_engines.begin()),
e(m_engines.end());
494 (allOK=(0 != this->setOnDefinedSeeds(theSeed, (*
i++).
first)))) {