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"
30 m_engines(), m_engines_copy()
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();
80 ifstream
infile( m_file_to_read.value().c_str() );
82 ATH_MSG_ERROR (
" Unable to open: " << m_file_to_read.value());
83 return StatusCode::FAILURE;
91 (
" INITIALISING " <<
stream <<
" stream with seeds "
93 <<
" read from file " << m_file_to_read.value());
98 <<
"\n in input file " << m_file_to_read.value());
99 return StatusCode::FAILURE;
107 for (
const auto&
i : m_streams_seeds.value()) {
113 (
"Seeds property: stream " <<
stream
115 <<
", reseeding offset " <<
offset);
118 return StatusCode::FAILURE;
122 bool not_found =
true;
123 if ( number_of_streams() != 0 ) {
126 if ((*sf).first ==
stream) not_found =
false;
128 }
while (
sf !=
end() && not_found);
133 (
" INITIALISING " <<
stream <<
" stream with seeds "
137 m_reseedingOffsets.insert(std::make_pair(
stream,
offset));
146 return StatusCode::SUCCESS;
152 if ( inc.type() ==
"EndEvent" ||
153 inc.type() ==
"AfterReseedIncident" )
156 m_engines_copy.clear();
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]);
164 m_engines_copy.insert(
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();
176 vector<string>::const_iterator
i(m_reseedStreamNames.begin());
177 vector<string>::const_iterator
e(m_reseedStreamNames.end());
181 if (!(this->setAllOnDefinedSeeds(ei.event_number(),
183 throw GaudiException(
"can not reseed all streams ",
name(), StatusCode::FAILURE);
186 const string& strName(*
i++);
187 if (0 == this->setOnDefinedSeeds(ei.event_number(),
190 throw GaudiException(
string(
"can not reseed stream ") + strName,
191 name(), StatusCode::FAILURE);
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);
202 const std::pair<unsigned,unsigned>&
data = incident->tag();
206 vector<string>::const_iterator
i(m_reseedStreamNames.begin());
207 vector<string>::const_iterator
e(m_reseedStreamNames.end());
211 if (!(this->setAllOnDefinedSeeds(
data.first,
213 throw GaudiException(
"can not reseed all streams ",
name(), StatusCode::FAILURE);
216 const string& strName(*
i++);
217 if (0 == this->setOnDefinedSeeds(
data.first,
220 throw GaudiException(
string(
"can not reseed stream ") + strName,
221 name(), StatusCode::FAILURE);
224 <<
" for random service " <<
endmsg;
236 if (m_save_to_file) {
238 std::ofstream
outfile( m_file_to_write.value().c_str() );
240 ATH_MSG_ERROR (
"error: unable to open: " << m_file_to_write.value());
242 for (std::map<std::string, std::vector<long int> >::const_iterator
i = m_engines_copy.begin();
243 i != m_engines_copy.end();
245 outfile << (*i).first <<
" " << (*i).second[0] <<
" " << (*i).second[1] <<
"\n";
247 ATH_MSG_DEBUG (
" wrote seeds to " << m_file_to_write.value() );
250 return StatusCode::SUCCESS;
253 CLHEP::HepRandomEngine*
257 if ( citer == m_engines.end() )
264 return (CLHEP::HepRandomEngine*)(*iter).second;
272 if (citer == m_engines.end())
275 long seeds[3] = { (long)
seed1, (
long)
seed2, 0 };
276 ((*iter).second)->setSeeds( seeds, -1 );
286 seed1 = m_PYTHIA_default_seed1;
287 seed2 = m_PYTHIA_default_seed2;
291 seed1 = m_HERWIG_default_seed1;
292 seed2 = m_HERWIG_default_seed2;
296 seed1 = m_default_seed1;
297 seed2 = m_default_seed2;
300 (
" INITIALISING " <<
streamName <<
" stream with DEFAULT seeds "
304 const long*
s = seeds;
306 ((*iter).second)->setSeeds(
s, -1 );
312 engineConstIter citer = m_engines.find(
streamName);
313 if ( citer == m_engines.end() )
319 const long*
s = ((*citer).second)->getSeeds();
321 <<
s[0] <<
", Seed2 = " <<
s[1]);
328 for (engineConstIter
i = m_engines.begin();
i != m_engines.end(); ++
i)
332 CLHEP::HepRandomEngine*
336 if (m_useOldBrokenSeeding)
340 map<string, uint32_t>::const_iterator citer(m_reseedingOffsets.find(
streamName));
341 bool hasOffset(citer != m_reseedingOffsets.end() && 0 != citer->second);
342 if (hasOffset) theHash=
crc_combine(theHash, citer->second);
349 return this->setOnDefinedSeeds(theHash,
streamName);
352 CLHEP::HepRandomEngine*
355 if (m_useOldBrokenSeeding)
356 return oldSetOnDefinedSeeds(theSeed,
streamName);
358 engineConstIter citer = m_engines.find(
streamName);
360 if ( citer == m_engines.end() )
362 new CLHEP::RanecuEngine() ) );
369 ((*iter).second)->setSeed( (int32_t)theSeed, 0);
370 return (CLHEP::HepRandomEngine*)(*iter).second;
373 CLHEP::HepRandomEngine*
378 if ( citer == m_engines.end() )
382 long seeds[3] = { (long)(1000*
runNumber + hashedStream),
384 assert( seeds[0] > 0 );
385 assert( seeds[1] > 0 );
386 const long*
s = seeds;
387 ((*iter).second)->setSeeds(
s, -1 );
388 return (CLHEP::HepRandomEngine*)(*iter).second;
391 CLHEP::HepRandomEngine*
395 if ( citer == m_engines.end() )
399 long seeds[3] = { (long)(hashedStream % (theSeed+13)), (
long)theSeed, 0 };
400 assert( seeds[0] > 0 );
401 assert( seeds[1] > 0 );
402 const long*
s = seeds;
403 ((*iter).second)->setSeeds(
s, -1 );
404 return (CLHEP::HepRandomEngine*)(*iter).second;
411 engineIter
i(m_engines.begin()),
e(m_engines.end());
424 engineIter
i(m_engines.begin()),
e(m_engines.end());
426 (allOK=(0 != this->setOnDefinedSeeds(theSeed, (*
i++).
first)))) {