ATLAS Offline Software
AtRndmGenSvc.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3 */
4 
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"
10 
11 #include "CLHEP/Random/RanecuEngine.h"
12 #include "CLHEP/Random/RandGauss.h"
13 
14 #include "interpretSeeds.h"
15 #include "AtRndmGenSvc.h"
16 #include "crc_combine.h"
17 
18 #include <cassert>
19 #include <iostream>
20 
21 /*FIXME temporarily needed for old seeding scheme*/
23 
24 using namespace std;
25 
27 AtRndmGenSvc::AtRndmGenSvc(const std::string& name,ISvcLocator* svc)
28  : base_class(name,svc),
29  m_reseedingOffsets(),
30  m_engines(), m_engines_copy()
31 {
32  // Set Default values
33  m_default_seed1 = 3591;
34  m_default_seed2 = 2309736;
35  m_PYTHIA_default_seed1 = 93453591;
36  m_PYTHIA_default_seed2 = 73436;
37  m_HERWIG_default_seed1 = 355391;
38  m_HERWIG_default_seed2 = 97336;
39 }
40 
41 
44 {
45  engineIter i(m_engines.begin()), e(m_engines.end());
46  while (i != e) delete (i++)->second;
47 }
48 
49 
52 {
54  ("Initializing " << name()
55  << "\n INITIALISING RANDOM NUMBER STREAMS. ");
56 
57 
59  ServiceHandle<IIncidentSvc> pIncSvc("IncidentSvc", name());
60 
61  // set up the incident service:
62  ATH_CHECK( pIncSvc.retrieve() );
63 
64  //start listening to "EndEvent"
65  static const int PRIORITY = 100;
66  pIncSvc->addListener(this, "EndEvent", PRIORITY);
67  pIncSvc->addListener(this, "AfterReseedIncident", PRIORITY);
68 
69  //and to BeginEvent if we are reseeding
70  if (m_eventReseed) {
71  ATH_MSG_INFO ("will be reseeded for every event");
72  pIncSvc->addListener(this, "BeginEvent");
73  pIncSvc->addListener(this, "ReseedIncident", PRIORITY);
74  }
75  pIncSvc.release().ignore();
76 
77  if (m_read_from_file)
78  {
79  // Read from a file
80  ifstream infile( m_file_to_read.value().c_str() );
81  if ( !infile ) {
82  ATH_MSG_ERROR (" Unable to open: " << m_file_to_read.value());
83  return StatusCode::FAILURE;
84  } else {
85  std::string buffer;
86  while (std::getline(infile, buffer)) {
87  string stream;
91  (" INITIALISING " << stream << " stream with seeds "
92  << seed1 << " " << seed2
93  << " read from file " << m_file_to_read.value());
94  CreateStream(seed1, seed2, stream);
95  } else {
97  ("bad line\n" << buffer
98  << "\n in input file " << m_file_to_read.value());
99  return StatusCode::FAILURE;
100  }
101  }
102 
103  }
104  }
105 
106  // Create the various streams according to user's request
107  for (const auto& i : m_streams_seeds.value()) {
108  string stream;
109  uint32_t seed1, seed2, offset(0);
110  short dummy;
113  ("Seeds property: stream " << stream
114  << " seeds " << seed1 << ' ' << seed2
115  << ", reseeding offset " << offset);
116  } else {
117  ATH_MSG_ERROR ("bad Seeds property\n" << i);
118  return StatusCode::FAILURE;
119  }
120 
121  // Check if stream already generated (e.g. from reading a file)
122  bool not_found = true;
123  if ( number_of_streams() != 0 ) {
125  do {
126  if ((*sf).first == stream) not_found = false;
127  ++sf;
128  } while (sf != end() && not_found);
129  }
130 
131  if (not_found) {
133  (" INITIALISING " << stream << " stream with seeds "
134  << seed1 << " " << seed2);
135  CreateStream(seed1, seed2, stream);
136  if (m_eventReseed) {
137  m_reseedingOffsets.insert(std::make_pair(stream, offset));
138  // apply the offset we just inserted
139  ATH_MSG_DEBUG("Applying reseeding offset " << offset <<
140  " to stream " << stream);
141  this->setOnDefinedSeeds(seed1, seed2, stream);
142  }
143  }
144 
145  }
146  return StatusCode::SUCCESS;
147 }
148 
149 void
150 AtRndmGenSvc::handle(const Incident &inc) {
151 
152  if ( inc.type() == "EndEvent" ||
153  inc.type() == "AfterReseedIncident" )
154  {
155  ATH_MSG_DEBUG (" Handle EndEvent ");
156  m_engines_copy.clear();
157  for (engineConstIter i = begin(); i != end(); ++i)
158  {
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,
166  tseeds ) );
167  }
168  print();
169  } else if (inc.type() == "BeginEvent") {
170  ATH_MSG_DEBUG (" Handle BeginEvent ");
171  EventContext context = inc.context();
172  const EventIDBase& ei = context.eventID();
173  //clear static RandGauss cache (generates two numbers per call to shoot()
175  //loop over generator streams, combining the stream name to the hash
176  vector<string>::const_iterator i(m_reseedStreamNames.begin());
177  vector<string>::const_iterator e(m_reseedStreamNames.end());
178  //by default (when no streams are specified in streamNames, seed all
179  //streams
180  if (i == e) {
181  if (!(this->setAllOnDefinedSeeds(ei.event_number(),
182  ei.run_number())))
183  throw GaudiException("can not reseed all streams ", name(), StatusCode::FAILURE);
184  } else {
185  while (i != e) {
186  const string& strName(*i++);
187  if (0 == this->setOnDefinedSeeds(ei.event_number(),
188  ei.run_number(),
189  strName)) {
190  throw GaudiException(string("can not reseed stream ") + strName,
191  name(), StatusCode::FAILURE);
192  } else {
193  msg() << MSG::VERBOSE << "Reseeded stream " << strName
194  << " for random service " << endmsg;
195  }
196  }
197  }
198  }
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();
203  //clear static RandGauss cache (generates two numbers per call to shoot()
205  //loop over generator streams, combining the stream name to the hash
206  vector<string>::const_iterator i(m_reseedStreamNames.begin());
207  vector<string>::const_iterator e(m_reseedStreamNames.end());
208  //by default (when no streams are specified in streamNames, seed all
209  //streams
210  if (i == e) {
211  if (!(this->setAllOnDefinedSeeds(data.first,
212  data.second)))
213  throw GaudiException("can not reseed all streams ", name(), StatusCode::FAILURE);
214  } else {
215  while (i != e) {
216  const string& strName(*i++);
217  if (0 == this->setOnDefinedSeeds(data.first,
218  data.second,
219  strName)) {
220  throw GaudiException(string("can not reseed stream ") + strName,
221  name(), StatusCode::FAILURE);
222  } else {
223  msg() << MSG::VERBOSE << "Reseeded stream " << strName
224  << " for random service " << endmsg;
225  }
226  }
227  }
228  }
229 }
230 
231 StatusCode
233 {
234  ATH_MSG_INFO (" FINALISING ");
235 
236  if (m_save_to_file) {
237  // Write the status of the Service into a file
238  std::ofstream outfile( m_file_to_write.value().c_str() );
239  if ( !outfile ) {
240  ATH_MSG_ERROR ("error: unable to open: " << m_file_to_write.value());
241  } else {
242  for (std::map<std::string, std::vector<long int> >::const_iterator i = m_engines_copy.begin();
243  i != m_engines_copy.end();
244  ++i) {
245  outfile << (*i).first << " " << (*i).second[0] << " " << (*i).second[1] << "\n";
246  }
247  ATH_MSG_DEBUG (" wrote seeds to " << m_file_to_write.value() );
248  }
249  }
250  return StatusCode::SUCCESS;
251 }
252 
253 CLHEP::HepRandomEngine*
254 AtRndmGenSvc::GetEngine ( const std::string& streamName )
255 {
256  engineConstIter citer = m_engines.find(streamName);
257  if ( citer == m_engines.end() )
258  {
259  m_engines.insert( engineValType( streamName, new CLHEP::RanecuEngine() ) );
260  SetStreamSeeds ( streamName );
261  }
262 
263  engineIter iter = m_engines.find(streamName);
264  return (CLHEP::HepRandomEngine*)(*iter).second;
265 }
266 
267 void
269  const std::string& streamName )
270 {
271  engineConstIter citer = m_engines.find(streamName);
272  if (citer == m_engines.end())
273  m_engines.insert(engineValType(streamName, new CLHEP::RanecuEngine()));
274  engineIter iter = m_engines.find(streamName);
275  long seeds[3] = { (long)seed1, (long) seed2, 0 };
276  ((*iter).second)->setSeeds( seeds, -1 );
277 }
278 
279 void
281 {
282  long seed1;
283  long seed2;
284  if (streamName == "PYTHIA")
285  {
286  seed1 = m_PYTHIA_default_seed1;
287  seed2 = m_PYTHIA_default_seed2;
288  }
289  else if (streamName == "HERWIG")
290  {
291  seed1 = m_HERWIG_default_seed1;
292  seed2 = m_HERWIG_default_seed2;
293  }
294  else
295  {
296  seed1 = m_default_seed1;
297  seed2 = m_default_seed2;
298  }
300  (" INITIALISING " << streamName << " stream with DEFAULT seeds "
301  << seed1 << " " << seed2);
302 
303  long seeds[3] = { seed1, seed2, 0 };
304  const long* s = seeds;
305  engineIter iter = m_engines.find(streamName);
306  ((*iter).second)->setSeeds( s, -1 );
307 }
308 
309 void
310 AtRndmGenSvc::print ( const std::string& streamName )
311 {
312  engineConstIter citer = m_engines.find(streamName);
313  if ( citer == m_engines.end() )
314  {
315  ATH_MSG_WARNING (" Stream = " << streamName << " NOT FOUND");
316  }
317  else
318  {
319  const long* s = ((*citer).second)->getSeeds();
320  ATH_MSG_INFO (" Stream = " << streamName << ", Seed1 = "
321  << s[0] << ", Seed2 = " << s[1]);
322  }
323 }
324 
325 void
326 AtRndmGenSvc::print ( void )
327 {
328  for (engineConstIter i = m_engines.begin(); i != m_engines.end(); ++i)
329  print( (*i).first );
330 }
331 
332 CLHEP::HepRandomEngine*
334  const std::string& streamName)
335 {
336  if (m_useOldBrokenSeeding)
337  return oldSetOnDefinedSeeds(eventNumber, runNumber, streamName);
338  //do it properly
339  uint32_t theHash(eventNumber);
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);
343 
344  theHash=crc_combine(theHash, runNumber);
345  ATH_MSG_VERBOSE( "Reseeding stream " << streamName
346  << " with eventNumber " << eventNumber
347  << " runNumber " << runNumber);
348  if (hasOffset) ATH_MSG_VERBOSE("Applied offset " << citer->second);
349  return this->setOnDefinedSeeds(theHash, streamName);
350 }
351 
352 CLHEP::HepRandomEngine*
354  const std::string& streamName){
355  if (m_useOldBrokenSeeding)
356  return oldSetOnDefinedSeeds(theSeed, streamName);
357  //do it properly
358  engineConstIter citer = m_engines.find(streamName);
360  if ( citer == m_engines.end() )
361  m_engines.insert(engineValType(streamName,
362  new CLHEP::RanecuEngine() ) );
363  engineIter iter = m_engines.find(streamName);
364  theSeed=crc_combine(theSeed, streamName);
365  ATH_MSG_DEBUG("Reseeding stream " << streamName << " with " << theSeed);
366  //Ranecu takes a long as seed and makes a test on the sign of the seed
367  //so let's make sure that our seed is presented to Ranecu as a 32 bit
368  //signed int
369  ((*iter).second)->setSeed( (int32_t)theSeed, 0);
370  return (CLHEP::HepRandomEngine*)(*iter).second;
371 }
372 
373 CLHEP::HepRandomEngine*
375  const std::string& streamName)
376 {
377  engineConstIter citer = m_engines.find(streamName);
378  if ( citer == m_engines.end() )
379  m_engines.insert( engineValType(streamName, new CLHEP::RanecuEngine() ) );
380  engineIter iter = m_engines.find(streamName);
381  int hashedStream(SG::simpleStringHash(streamName));
382  long seeds[3] = { (long)(1000*runNumber + hashedStream),
383  (long)eventNumber, 0 };
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;
389 }
390 
391 CLHEP::HepRandomEngine*
393  const std::string& streamName){
394  engineConstIter citer = m_engines.find(streamName);
395  if ( citer == m_engines.end() )
396  m_engines.insert( engineValType(streamName, new CLHEP::RanecuEngine() ) );
397  engineIter iter = m_engines.find(streamName);
398  int hashedStream(SG::simpleStringHash(streamName));
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;
405 }
406 
407 bool
409 {
410  bool allOK(true);
411  engineIter i(m_engines.begin()), e(m_engines.end());
412  while (i!=e &&
413  (allOK=(0 != this->setOnDefinedSeeds(eventNumber,
414  runNumber,
415  (*i++).first)))) {
416  /*empty*/
417  }
418  return allOK;
419 }
420 
421 bool
423  bool allOK(true);
424  engineIter i(m_engines.begin()), e(m_engines.end());
425  while (i!=e &&
426  (allOK=(0 != this->setOnDefinedSeeds(theSeed, (*i++).first)))) {
427  /* empty */
428  }
429  return allOK;
430 }
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
python.SystemOfUnits.second
int second
Definition: SystemOfUnits.py:120
data
char data[hepevt_bytes_allocation_ATLAS]
Definition: HepEvt.cxx:11
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
run.infile
string infile
Definition: run.py:13
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
AtRndmGenSvc::finalize
virtual StatusCode finalize() override
Definition: AtRndmGenSvc.cxx:232
xAOD::uint32_t
setEventNumber uint32_t
Definition: EventInfo_v1.cxx:127
AtRndmGenSvc::GetEngine
virtual CLHEP::HepRandomEngine * GetEngine(const std::string &streamName) override
Definition: AtRndmGenSvc.cxx:254
hash_functions.h
AtRndmGenSvc::AtRndmGenSvc
AtRndmGenSvc(const std::string &name, ISvcLocator *svc)
Standard Constructor.
Definition: AtRndmGenSvc.cxx:27
PlotCalibFromCool.begin
begin
Definition: PlotCalibFromCool.py:94
AtRndmGenSvc::m_default_seed2
long m_default_seed2
Definition: AtRndmGenSvc.h:145
crc_combine.h
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
AthenaPoolTestWrite.stream
string stream
Definition: AthenaPoolTestWrite.py:12
AtRndmGenSvc::m_HERWIG_default_seed1
long m_HERWIG_default_seed1
Definition: AtRndmGenSvc.h:148
genPbPbJobOpt.seed2
seed2
Definition: genPbPbJobOpt.py:57
mergePhysValFiles.end
end
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:93
AtRndmGenSvc::setOnDefinedSeeds
virtual CLHEP::HepRandomEngine * setOnDefinedSeeds(uint32_t theSeed, const std::string &streamName) override
AtRndmGenSvc.h
A random number engine manager, based on Ranecu. Its usage is deprecated, at least when high quality ...
AtRndmGenSvc::SetStreamSeeds
void SetStreamSeeds(const std::string &streamName)
Definition: AtRndmGenSvc.cxx:280
crc_combine
uint32_t crc_combine(uint32_t seed, uint32_t v)
using crc32 for architecture independence in combining the seeds
Definition: AthenaKernel/src/crc_combine.h:11
AtRndmGenSvc::~AtRndmGenSvc
virtual ~AtRndmGenSvc()
Standard Destructor.
Definition: AtRndmGenSvc.cxx:43
AtRndmGenSvc::m_PYTHIA_default_seed1
long m_PYTHIA_default_seed1
Definition: AtRndmGenSvc.h:146
createCoolChannelIdFile.buffer
buffer
Definition: createCoolChannelIdFile.py:12
AtRndmGenSvc::m_HERWIG_default_seed2
long m_HERWIG_default_seed2
Definition: AtRndmGenSvc.h:149
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
AtRndmGenSvc::setAllOnDefinedSeeds
virtual bool setAllOnDefinedSeeds(uint32_t theSeed) override
seed all streams we manage, combining theSeed and the stream names
lumiFormat.i
int i
Definition: lumiFormat.py:85
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
AtRndmGenSvc::engineIter
engineMap::iterator engineIter
Definition: AtRndmGenSvc.h:68
python.xAODType.dummy
dummy
Definition: xAODType.py:4
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
interpretSeeds
bool interpretSeeds(const std::string &buffer, std::string &stream, uint32_t &seed1, uint32_t &seed2, short &luxury, uint32_t &offset)
Definition: interpretSeeds.cxx:28
xAOD::eventNumber
eventNumber
Definition: EventInfo_v1.cxx:124
interpretSeeds.h
Handler::svc
AthROOTErrorHandlerSvc * svc
Definition: AthROOTErrorHandlerSvc.cxx:10
HLT::setFlag
void setFlag(TrigPassFlags *flags, const T *obj, const CONTAINER *container, const std::vector< bool > &flag)
Set the flag at index position.
Definition: TrigPassFlags.h:121
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
AtRndmGenSvc::oldSetOnDefinedSeeds
CLHEP::HepRandomEngine * oldSetOnDefinedSeeds(uint32_t theSeed, const std::string &streamName)
broken, temporarily keep for backward compatibility
Definition: AtRndmGenSvc.cxx:392
DeMoAtlasDataLoss.runNumber
string runNumber
Definition: DeMoAtlasDataLoss.py:64
AtRndmGenSvc::CreateStream
virtual void CreateStream(uint32_t seed1, uint32_t seed2, const std::string &streamName) override
Definition: AtRndmGenSvc.cxx:268
AthenaPoolExample_Copy.streamName
string streamName
Definition: AthenaPoolExample_Copy.py:39
AtRndmGenSvc::print
virtual void print(void) override
mapkey::sf
@ sf
Definition: TElectronEfficiencyCorrectionTool.cxx:38
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
AtRndmGenSvc::m_default_seed1
long m_default_seed1
Definition: AtRndmGenSvc.h:144
DeMoScan.first
bool first
Definition: DeMoScan.py:536
genPbPbJobOpt.seed1
seed1
Definition: genPbPbJobOpt.py:56
convertTimingResiduals.offset
offset
Definition: convertTimingResiduals.py:71
SG::simpleStringHash
int simpleStringHash(const std::string &str, int maxInt=0xFFFF)
simple hash function derived from Sedgewick Algorithms in C++ 3rd ed
Definition: hash_functions.h:16
dbg::print
void print(std::FILE *stream, std::format_string< Args... > fmt, Args &&... args)
Definition: SGImplSvc.cxx:70
python.Constants.VERBOSE
int VERBOSE
Definition: Control/AthenaCommon/python/Constants.py:14
AtRndmGenSvc::handle
virtual void handle(const Incident &) override
IIncidentListener implementation. Handles EndEvent incident.
Definition: AtRndmGenSvc.cxx:150
AtRndmGenSvc::engineConstIter
engineMap::const_iterator engineConstIter
Definition: AtRndmGenSvc.h:69
AtRndmGenSvc::m_PYTHIA_default_seed2
long m_PYTHIA_default_seed2
Definition: AtRndmGenSvc.h:147
value_type
Definition: EDM_MasterSearch.h:11
AtRndmGenSvc::engineValType
engineMap::value_type engineValType
Definition: AtRndmGenSvc.h:70
PrepareReferenceFile.outfile
outfile
Definition: PrepareReferenceFile.py:42
python.AutoConfigFlags.msg
msg
Definition: AutoConfigFlags.py:7
AtRndmGenSvc::initialize
virtual StatusCode initialize() override
Definition: AtRndmGenSvc.cxx:51
ServiceHandle< IIncidentSvc >