ATLAS Offline Software
RNGWrapper.h
Go to the documentation of this file.
1 // -*- c++ -*-
2 /*
3  Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
4 */
5 #ifndef ATHENAKERNEL_RNGWRAPPER_H
6 #define ATHENAKERNEL_RNGWRAPPER_H
7 
8 #include <string>
9 #include <cstdint>
10 #include <vector>
11 #include <functional>
12 
13 #include "GaudiKernel/EventContext.h"
14 #include "GaudiKernel/ThreadLocalContext.h"
15 
16 namespace CLHEP{
17  class HepRandomEngine;
18 }
19 
20 namespace ATHRNG{
21 
23 
28  size_t calculateSeedMC16(const std::string& algName, uint64_t ev, uint64_t run, uint32_t offset=0);
29 
32  size_t calculateSeedMC20(const std::string& algName, uint64_t ev, uint64_t run);
33 
36  void calculateSeedsMC21(long* seeds, const std::string& algName, uint64_t ev, uint64_t run, uint64_t offset=0);
37 
38  long calculateSeedsPython(const std::string& algName, uint64_t ev, uint64_t run, uint64_t offset=0);
55  class RNGWrapper
56  {
57 
58  typedef std::function<CLHEP::HepRandomEngine*(void)> factoryFunc;
59 
60  public:
61 
64  RNGWrapper(const factoryFunc& genFact, size_t nSlots);
65  ~RNGWrapper();
66 
69  inline void setSeed(const std::string& algName, const EventContext& ctx);
70 
73  inline void setSeed(const std::string& algName, size_t slot, uint64_t ev, uint64_t run,
74  EventContext::ContextEvt_t evt = EventContext::INVALID_CONTEXT_EVT);
75 
80  void setSeedMC16(const std::string& algName, size_t slot, uint64_t ev, uint64_t run, uint32_t offset=0,
81  EventContext::ContextEvt_t evt = EventContext::INVALID_CONTEXT_EVT);
82 
85  void setSeedMC20(const std::string& algName, size_t slot, uint64_t ev, uint64_t run,
86  EventContext::ContextEvt_t evt = EventContext::INVALID_CONTEXT_EVT);
87 
90  void setSeedMC21(const std::string& algName, size_t slot, uint64_t ev, uint64_t run, uint64_t offset=0,
91  EventContext::ContextEvt_t evt = EventContext::INVALID_CONTEXT_EVT);
92 
102  };
103  static constexpr std::initializer_list<SeedingOptionType> all_SeedingOptions = {MC20Seeding, MC16Seeding, MC21Seeding};
104 
108  void setSeedLegacy(const std::string& algName, size_t slot, uint64_t ev, uint64_t run, uint64_t offset, SeedingOptionType seeding,
109  EventContext::ContextEvt_t evt = EventContext::INVALID_CONTEXT_EVT);
110 
115  inline void setSeedLegacy(const std::string& algName, const EventContext& ctx, uint32_t offset, SeedingOptionType seeding,
116  EventContext::ContextEvt_t evt = EventContext::INVALID_CONTEXT_EVT);
117 
119  void setSeed(size_t slot, size_t seed,
120  EventContext::ContextEvt_t evt = EventContext::INVALID_CONTEXT_EVT);
121 
123  void setSeeds(size_t slot, const long * seeds,
124  EventContext::ContextEvt_t evt = EventContext::INVALID_CONTEXT_EVT);
125 
129  operator CLHEP::HepRandomEngine*() const {
130  return m_engines[Gaudi::Hive::currentContext().slot()];
131  }
132 
134  CLHEP::HepRandomEngine* getEngine(const EventContext &ctx) const {
135  return m_engines[ctx.slot()];
136  }
137 
139  EventContext::ContextEvt_t evtSeeded() const
140  {
141  return m_evtSeeded[Gaudi::Hive::currentContext().slot()];
142  }
143 
145  EventContext::ContextEvt_t evtSeeded(const EventContext &ctx) const
146  {
147  return m_evtSeeded[ctx.slot()];
148  }
149 
150  private:
151 
153  std::vector<CLHEP::HepRandomEngine*> m_engines;
154 
156  std::vector<EventContext::ContextEvt_t> m_evtSeeded;
157 
158  };
159 
160 }
161 
162 //Default to MC21 seeding for now
163 inline void ATHRNG::RNGWrapper::setSeed(const std::string& algName, size_t slot, uint64_t ev, uint64_t run,
164  EventContext::ContextEvt_t evt /*= EventContext::INVALID_CONTEXT_EVT*/)
165 {
166  setSeedMC21(algName,slot,ev,run,0,evt);
167 }
168 
169 inline void ATHRNG::RNGWrapper::setSeed(const std::string& algName, const EventContext& ctx)
170 {
171  setSeed( algName, ctx.slot(),
172  ctx.eventID().event_number(),
173  ctx.eventID().run_number(),
174  ctx.evt() );
175 }
176 
177 inline void ATHRNG::RNGWrapper::setSeedLegacy(const std::string& algName, const EventContext& ctx, uint32_t offset, SeedingOptionType seeding,
178  EventContext::ContextEvt_t evt /*= EventContext::INVALID_CONTEXT_EVT*/)
179 {
180  setSeedLegacy( algName, ctx.slot(),
181  ctx.eventID().event_number(),
182  ctx.eventID().run_number(),
183  offset, seeding, evt);
184 }
185 
186 
187 
188 #endif
ATHRNG::RNGWrapper::setSeed
void setSeed(const std::string &algName, const EventContext &ctx)
Set the random seed using a string (e.g.
Definition: RNGWrapper.h:169
ATHRNG::RNGWrapper::SeedingOptionType
SeedingOptionType
Options for seeding option=0 is setSeed as in MC20 option=1 is setSeedLegacy as in MC16 option=2 is s...
Definition: RNGWrapper.h:97
xAOD::uint32_t
setEventNumber uint32_t
Definition: EventInfo_v1.cxx:127
ATHRNG
Definition: IAthRNGSvc.h:23
ATHRNG::RNGWrapper::setSeedLegacy
void setSeedLegacy(const std::string &algName, size_t slot, uint64_t ev, uint64_t run, uint64_t offset, SeedingOptionType seeding, EventContext::ContextEvt_t evt=EventContext::INVALID_CONTEXT_EVT)
Set the random seed using a string (e.g.
Definition: RNGWrapper.cxx:104
xAOD::JetAlgorithmType::algName
const std::string & algName(ID id)
Converts a JetAlgorithmType::ID into a string.
Definition: JetContainerInfo.cxx:67
ATHRNG::RNGWrapper::setSeedMC21
void setSeedMC21(const std::string &algName, size_t slot, uint64_t ev, uint64_t run, uint64_t offset=0, EventContext::ContextEvt_t evt=EventContext::INVALID_CONTEXT_EVT)
Set the random seed using a string (e.g.
Definition: RNGWrapper.cxx:95
ATHRNG::RNGWrapper::SeedingDefault
@ SeedingDefault
Definition: RNGWrapper.h:101
LArG4FSStartPointFilter.evt
evt
Definition: LArG4FSStartPointFilter.py:42
ATHRNG::RNGWrapper::MC20Seeding
@ MC20Seeding
Definition: RNGWrapper.h:98
ATHRNG::RNGWrapper::RNGWrapper
RNGWrapper(const factoryFunc &genFact, size_t nSlots)
Constructor takes a factory function which can instantiate a CLHEP::HepRandomEngine and the number of...
Definition: RNGWrapper.cxx:69
ATHRNG::RNGWrapper::setSeeds
void setSeeds(size_t slot, const long *seeds, EventContext::ContextEvt_t evt=EventContext::INVALID_CONTEXT_EVT)
Set the seed value(s) directly for a specified slot.
Definition: RNGWrapper.cxx:139
ATHRNG::RNGWrapper::evtSeeded
EventContext::ContextEvt_t evtSeeded(const EventContext &ctx) const
Return the event count at which the current slot of CTX was last seeded.
Definition: RNGWrapper.h:145
ev
int ev
Definition: globals.cxx:25
ATHRNG::RNGWrapper::m_engines
std::vector< CLHEP::HepRandomEngine * > m_engines
Vector of random engines, ordered by slot number.
Definition: RNGWrapper.h:153
ATHRNG::calculateSeedMC16
size_t calculateSeedMC16(const std::string &algName, uint64_t ev, uint64_t run, uint32_t offset=0)
Helper methods.
Definition: RNGWrapper.cxx:15
CLHEP
STD'S.
Definition: IAtRndmGenSvc.h:19
xAOD::uint64_t
uint64_t
Definition: EventInfo_v1.cxx:123
run
Definition: run.py:1
ATHRNG::RNGWrapper::~RNGWrapper
~RNGWrapper()
Definition: RNGWrapper.cxx:79
ATHRNG::RNGWrapper::MC21Seeding
@ MC21Seeding
Definition: RNGWrapper.h:100
ATHRNG::RNGWrapper::setSeedMC20
void setSeedMC20(const std::string &algName, size_t slot, uint64_t ev, uint64_t run, EventContext::ContextEvt_t evt=EventContext::INVALID_CONTEXT_EVT)
Set the random seed using a string (e.g.
Definition: RNGWrapper.cxx:87
ATHRNG::RNGWrapper
A wrapper class for event-slot-local random engines.
Definition: RNGWrapper.h:56
ATHRNG::RNGWrapper::getEngine
CLHEP::HepRandomEngine * getEngine(const EventContext &ctx) const
Retrieve the random engine corresponding to the provided EventContext.
Definition: RNGWrapper.h:134
ATHRNG::calculateSeedMC20
size_t calculateSeedMC20(const std::string &algName, uint64_t ev, uint64_t run)
Set the random seed using a string (e.g.
Definition: RNGWrapper.cxx:26
convertTimingResiduals.offset
offset
Definition: convertTimingResiduals.py:71
ATHRNG::RNGWrapper::m_evtSeeded
std::vector< EventContext::ContextEvt_t > m_evtSeeded
Event counter when the engine was last seeded.
Definition: RNGWrapper.h:156
ATHRNG::calculateSeedsMC21
void calculateSeedsMC21(long *seeds, const std::string &algName, uint64_t ev, uint64_t run, uint64_t offset=0)
Set the random seed using a string (e.g.
Definition: RNGWrapper.cxx:37
ATHRNG::RNGWrapper::factoryFunc
std::function< CLHEP::HepRandomEngine *(void)> factoryFunc
Definition: RNGWrapper.h:58
ATHRNG::calculateSeedsPython
long calculateSeedsPython(const std::string &algName, uint64_t ev, uint64_t run, uint64_t offset=0)
Definition: RNGWrapper.cxx:63
ATHRNG::RNGWrapper::setSeedMC16
void setSeedMC16(const std::string &algName, size_t slot, uint64_t ev, uint64_t run, uint32_t offset=0, EventContext::ContextEvt_t evt=EventContext::INVALID_CONTEXT_EVT)
Set the random seed using a string (e.g.
Definition: RNGWrapper.cxx:122
ATHRNG::RNGWrapper::evtSeeded
EventContext::ContextEvt_t evtSeeded() const
Return the event count at which the current slot was last seeded.
Definition: RNGWrapper.h:139
ATHRNG::RNGWrapper::MC16Seeding
@ MC16Seeding
Definition: RNGWrapper.h:99
ATHRNG::RNGWrapper::all_SeedingOptions
static constexpr std::initializer_list< SeedingOptionType > all_SeedingOptions
Definition: RNGWrapper.h:103