LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
PerEventPolicy.h
Go to the documentation of this file.
1 
12 #ifndef NUTOOLS_RANDOMUTILS_PROVIDERS_PEREVENTPOLICY_H
13 #define NUTOOLS_RANDOMUTILS_PROVIDERS_PEREVENTPOLICY_H 1
14 
15 // C/C++ standard libraries
16 #include <string>
17 #include <sstream>
18 #include <ostream> // std::endl
19 #include <type_traits> // std::make_signed<>
20 
21 // From art and its tool chain
23 #include "fhiclcpp/ParameterSet.h"
25 
26 // Some helper classes
30 
31 
32 namespace rndm {
33 
34  namespace details {
35 
44  template <typename SEED>
45  class PerEventPolicy: public RandomSeedPolicyBase<SEED> {
46  public:
49  using seed_t = typename base_t::seed_t;
50 
53 
54  typedef enum {
59  } SeedAlgo_t;
60 
63  PerEventPolicy(fhicl::ParameterSet const& pset): base_t("perEvent")
64  { this_t::configure(pset); }
65 
67  virtual bool yieldsUniqueSeeds() const override { return false; }
68 
69 
85  virtual void configure(fhicl::ParameterSet const& pset) override;
86 
88  virtual void print(std::ostream& out) const override;
89 
90 
92  static constexpr const char* DefaultVersion = "v1";
93 
94 
96  template <typename Hashable>
97  static seed_t SeedFromHash(Hashable const& info)
98  { return makeValid(std::hash<Hashable>()(info)); }
99 
101  static std::string UniqueEventIDString(EventData_t const& info);
102 
104  static std::string UniqueEventString(EventData_t const& info);
105 
106 
107  private:
109  using SeedOffset_t = typename std::make_signed<seed_t>::type;
110 
111  SeedAlgo_t algo;
112 
114 
117  { return base_t::InvalidSeed; }
118 
131  virtual seed_t createEventSeed
132  (SeedMasterHelper::EngineId const& id, EventData_t const& info)
133  override;
134 
136  template <typename T>
138 
141  (SeedMasterHelper::EngineId const& id, EventData_t const& info);
142 
143 
145  static const std::vector<std::string> algoNames;
147 
148  static std::vector<std::string> InitAlgoNames();
150  }; // class PerEventPolicy<>
151 
152 
153 
154  //--------------------------------------------------------------------------
155  //--- PerEventPolicy template implementation
156  //---
157  template <typename SEED>
158  std::vector<std::string> PerEventPolicy<SEED>::InitAlgoNames() {
159 
160  std::vector<std::string> names((size_t) NAlgos);
161 
162  names[saEventTimestamp_v1] = "EventTimestamp_v1";
163 
164  return names;
165  } // PerEventPolicy<SEED>::InitAlgoNames()
166 
167 
168  template <typename SEED>
169  const std::vector<std::string> PerEventPolicy<SEED>::algoNames
171 
172 
173  //--------------------------------------------------------------------------
174  template <typename SEED>
176  (EventData_t const& info)
177  {
178  std::ostringstream sstr;
179  sstr << "Run: " << info.runNumber << " Subrun: " << info.subRunNumber
180  << " Event: " << info.eventNumber;
181  return sstr.str();
182  } // PerEventPolicy<SEED>::UniqueEventIDString()
183 
184 
185  template <typename SEED>
187  (EventData_t const& info)
188  {
189  std::ostringstream sstr;
190  sstr << " Timestamp: " << info.time;
191  return UniqueEventIDString(info) + sstr.str();
192  } // PerEventPolicy<SEED>::UniqueEventString()
193 
194 
195  template <typename SEED>
197  (SeedMasterHelper::EngineId const& id, EventData_t const& info)
198  -> seed_t
199  {
200  if (!info.isTimeValid) {
202  << "Input event has an invalid timestamp,"
203  " random seed per-event policy EventTimestamp_v1 can't be used.\n";
204  }
205  std::string s = UniqueEventString(info)
206  + " Process: " + info.processName
207  + " Module: " + id.moduleLabel;
208  if (!id.instanceName.empty())
209  s.append(" Instance: ").append(id.instanceName);
210  seed_t seed = SeedFromHash(s);
211  LOG_DEBUG("PerEventPolicy") << "Seed from: '" << s << "': " << seed;
212  return seed;
213  } // PerEventPolicy<SEED>::EventTimestamp_v1()
214 
215 
216  //--------------------------------------------------------------------------
217  template <typename SEED>
219  // set the algorithm
220  algo = saUndefined;
221  std::string algorithm_name
222  = pset.get<std::string>("algorithm", "default");
223 
224  if (algorithm_name == "default") algo = saDefault;
225  else {
226  for (size_t iAlgo = 0; iAlgo < (size_t) NAlgos; ++iAlgo) {
227  if (algorithm_name != algoNames[iAlgo]) continue;
228  algo = (SeedAlgo_t) iAlgo;
229  break;
230  } // for
231  }
232  if (algo == saUndefined) {
234  << "No valid event random seed algorithm specified!\n";
235  }
236 
237  // read an optional overall offset
238  offset = pset.get<SeedOffset_t>("offset", 0);
239 
240  // EventTimestamp_v1 does not require specific configuration
241 
242  } // PerEventPolicy<SEED>::configure()
243 
244 
245  //--------------------------------------------------------------------------
247  template <typename SEED>
248  void PerEventPolicy<SEED>::print(std::ostream& out) const {
249  base_t::print(out);
250  out
251  << "\n algorithm version: " << algoNames[algo];
252  if (offset != 0)
253  out << "\n constant offset: " << offset;
254  } // PerEventPolicy<SEED>::print()
255 
256 
257  //--------------------------------------------------------------------------
258  template <typename SEED>
260  (SeedMasterHelper::EngineId const& id, EventData_t const& info)
261  {
263  switch (algo) {
264  case saEventTimestamp_v1:
265  seed = EventTimestamp_v1(id, info);
266  break;
267  case saUndefined:
269  << "Per-event random number seeder not configured!\n";
270  default:
272  << "Unsupported per-event random number seeder (#"
273  << ((int) algo) << ")\n";
274  } // switch
275  return seed + offset;
276  } // PerEventPolicy<SEED>::createEventSeed()
277 
278 
279  } // namespace details
280 
281 } // namespace rndm
282 
283 
284 #endif // NUTOOLS_RANDOMUTILS_PROVIDERS_PEREVENTPOLICY_H
Float_t s
Definition: plot.C:23
virtual void print(std::ostream &out) const override
Prints the details of the configuration of the random generator.
virtual seed_t createEventSeed(SeedMasterHelper::EngineId const &id, EventData_t const &info) override
Returns a seed proper for the specified event information.
static std::string UniqueEventIDString(EventData_t const &info)
Converts run, subrun and event numbers into a string.
SEED seed_t
type of the random seed
Definition: BasePolicies.h:45
static seed_t EventTimestamp_v1(SeedMasterHelper::EngineId const &id, EventData_t const &info)
Implementation of the EventTimestamp_v1 algorithm.
SeedOffset_t offset
offset added to all the seeds
Simple data structure with data needed to extract a seed from a event.
virtual seed_t createSeed(SeedMasterHelper::EngineId const &) override
Per-job seed: invalid seeds are returned.
total number of seed algorithms
typename std::make_signed< seed_t >::type SeedOffset_t
type for seed offset
virtual void print(std::ostream &out) const
Prints information on the configuration of this policy.
Definition: BasePolicies.h:76
std::string processName
name of the running process
virtual void configure(fhicl::ParameterSet const &pset) override
Configure this policy.
static seed_t SeedFromHash(Hashable const &info)
Converts some information into a valid seed by means of hash values.
typename base_t::seed_t seed_t
static std::vector< std::string > InitAlgoNames()
Algorithm name (manual) handling.
Interface for a policy implementation.
Definition: BasePolicies.h:43
long seed
Definition: chem4.cc:68
T get(std::string const &key) const
Definition: ParameterSet.h:231
static const std::vector< std::string > algoNames
Algorithm name (manual) handling.
Identifier for a engine, made of module name and optional instance name.
Definition: EngineId.h:22
SeedAlgo_t algo
the algorithm to extract the seed
Defines an interface for random seed assignment policies.
static seed_t makeValid(T value)
Renders a seed valid.
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
std::string value(boost::any const &)
Implementation of the "perEvent" policy.
Class storing a seed in the valid range.
Definition: G4Helper.h:37
static constexpr seed_t InvalidSeed
An invalid seed.
Definition: BasePolicies.h:51
#define LOG_DEBUG(id)
PerEventPolicy(fhicl::ParameterSet const &pset)
static constexpr const char * DefaultVersion
Default algorithm version.
A data object holding enough data to define a event seed.
static std::string UniqueEventString(EventData_t const &info)
Converts event ID and timestamp information into a string.
virtual bool yieldsUniqueSeeds() const override
Returns whether the returned seed should be unique: for us it "no".
An identifier for random engines.