#include "RandomNumberGenerator.h"
Definition at line 158 of file RandomNumberGenerator.h.
Definition at line 118 of file RandomNumberGenerator.cc.
References data_, debug_, art::Globals::instance(), nPrint_, postBeginJob(), postEndJob(), preProcessEvent(), restoreFromFilename_, restoreStateLabel_, and saveToFilename_.
124 ,
debug_{config().debug()}
129 actReg.sPreProcessEvent.watch(
this,
std::string const restoreFromFilename_
PerScheduleContainer< ScheduleData > data_
std::string const saveToFilename_
void preProcessEvent(Event const &, ScheduleContext)
std::string const defaultEngineKind_
static Globals * instance()
std::string const restoreStateLabel_
CLHEP::HepRandomEngine & art::RandomNumberGenerator::createEngine |
( |
ScheduleID |
sid, |
|
|
std::string const & |
module_label, |
|
|
long |
seed, |
|
|
std::string const & |
kind_of_engine_to_make, |
|
|
std::string const & |
engine_label = {} |
|
) |
| |
|
private |
Definition at line 135 of file RandomNumberGenerator.cc.
References data_, defaultEngineKind_, engine_creation_is_okay_, art::ScheduleID::id(), invariant_holds_(), mutex_, Seed, useDefaultSeed, and validate_().
141 std::lock_guard sentry{
mutex_};
144 <<
"RNGservice::createEngine():\n" 145 <<
"Attempt to create engine \"" << engine_label <<
"\" is too late.\n";
147 if (sid.id() >=
data_.size()) {
149 <<
"RNGservice::createEngine():\n" 150 <<
"Attempt to create engine with out-of-range ScheduleID: " << sid
153 string const& label = qualify_engine_label(sid, module_label, engine_label);
154 if (
data_[sid].tracker_.find(label) !=
data_[sid].tracker_.cend()) {
156 <<
"RNGservice::createEngine():\n" 157 <<
"Engine \"" << label <<
"\" has already been created.\n";
159 string engineKind{requested_engine_kind};
160 if (requested_engine_kind.empty()) {
166 shared_ptr<CLHEP::HepRandomEngine> eptr;
167 if (engineKind ==
"G4Engine"s) {
171 CLHEP::HepRandom::setTheEngine(eptr.get());
173 CLHEP::HepRandom::setTheSeed(
seed);
175 }
else if (engineKind ==
"NonRandomEngine"s) {
176 eptr = std::make_shared<CLHEP::NonRandomEngine>();
178 eptr = engine_factory(engineKind,
seed);
182 <<
"RNGservice::createEngine():\n" 183 <<
"Engine \"" << label <<
"\" could not be created.\n";
185 data_[sid].dict_[label] = eptr;
187 data_[sid].kind_[label] = engineKind;
188 mf::LogInfo{
"RANDOM"} <<
"Instantiated " << engineKind <<
" engine \"" 189 << label <<
"\" with " 194 "RNGservice::createEngine() invariant failed");
PerScheduleContainer< ScheduleData > data_
static long constexpr useDefaultSeed
std::recursive_mutex mutex_
bool invariant_holds_(ScheduleID)
void validate_(std::string const &user_specified_engine_kind, long user_specified_seed) noexcept(false)
std::string const defaultEngineKind_
bool engine_creation_is_okay_
cet::coded_exception< error, detail::translate > exception
std::string const& art::RandomNumberGenerator::defaultEngineKind |
( |
| ) |
const |
|
inlinenoexcept |
bool art::RandomNumberGenerator::invariant_holds_ |
( |
ScheduleID |
sid | ) |
|
|
private |
void art::RandomNumberGenerator::postBeginJob |
( |
| ) |
|
|
private |
void art::RandomNumberGenerator::postEndJob |
( |
| ) |
|
|
private |
void art::RandomNumberGenerator::print_ |
( |
| ) |
const |
|
private |
Definition at line 235 of file RandomNumberGenerator.cc.
References d, data_, debug_, mutex_, and nPrint_.
237 std::lock_guard sentry{
mutex_};
238 static unsigned ncalls = 0;
242 auto print_per_stream = [](
size_t const i,
auto const&
d) {
244 if (
d.snapshot_.empty()) {
245 log <<
"No snapshot has yet been made.\n";
248 log <<
"Snapshot information:";
249 for (
auto const& ss :
d.snapshot_) {
250 log <<
"\nEngine: " << ss.label() <<
" Kind: " << ss.ekind()
251 <<
" Schedule ID: " << i <<
" State size: " << ss.state().size();
254 cet::for_all_with_index(
data_, print_per_stream);
PerScheduleContainer< ScheduleData > data_
std::recursive_mutex mutex_
void art::RandomNumberGenerator::restoreFromFile_ |
( |
| ) |
|
|
private |
Definition at line 355 of file RandomNumberGenerator.cc.
References art::errors::Configuration, d, data_, File, invariant_holds_(), art::errors::LogicError, mutex_, restoreFromFilename_, and Seed.
Referenced by postBeginJob().
357 std::lock_guard sentry{
mutex_};
361 HEP_CONCURRENCY_ASSERT_ONLY_ONE_THREAD();
366 <<
"RNGservice::restoreFromFile_():\n" 368 <<
"\" to initialize engines\n";
371 for (
string label{}; infile >> label;) {
373 assert(count(label.cbegin(), label.cend(),
':') == 2u);
374 auto const p1 = label.find_first_of(
':');
375 auto const p2 = label.find_last_of(
':');
376 ScheduleID
const sid{
378 auto d =
data_[sid].dict_.find(label);
379 if (
d ==
data_[sid].dict_.end()) {
381 <<
"Attempt to restore an engine with label " << label
382 <<
" not configured in this job.\n";
384 assert((
data_[sid].tracker_.find(label) !=
data_[sid].tracker_.cend()) &&
385 "RNGservice::restoreFromFile_()");
388 auto& eptr =
d->second;
389 assert(eptr &&
"RNGservice::restoreFromFile_()");
390 if (!eptr->get(infile)) {
392 <<
"RNGservice::restoreFromFile_():\n" 393 <<
"Failed during restore of state of engine for label " << label
399 <<
"Engine state file contains two engine states with the same " 404 <<
"Internal error: attempt to restore an engine state " << label
406 <<
"which was originally initialized via an unknown or impossible " 410 "RNGservice::restoreFromFile_() invariant failure");
std::string const restoreFromFilename_
PerScheduleContainer< ScheduleData > data_
std::recursive_mutex mutex_
bool invariant_holds_(ScheduleID)
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
cet::coded_exception< error, detail::translate > exception
void art::RandomNumberGenerator::restoreSnapshot_ |
( |
ScheduleID |
sid, |
|
|
Event const & |
event |
|
) |
| |
|
private |
Definition at line 281 of file RandomNumberGenerator.cc.
References data_, File, invariant_holds_(), mutex_, Product, and restoreStateLabel_.
Referenced by preProcessEvent().
284 std::lock_guard sentry{
mutex_};
292 for (
auto const& snapshot : saved) {
293 string const& label = snapshot.label();
295 log <<
"RNGservice::restoreSnapshot_(): label \"" << label <<
"\"";
296 auto t =
data_[sid].tracker_.find(label);
297 if (t ==
data_[sid].tracker_.end()) {
298 log <<
" could not be restored;\n" 299 <<
"no established engine bears this label.\n";
304 <<
"RNGservice::restoreSnapshot_():\n" 305 <<
"The state of engine \"" << label
306 <<
"\" has been previously read from a file;\n" 307 <<
"it is therefore not restorable from a snapshot product.\n";
309 shared_ptr<CLHEP::HepRandomEngine> ep{
data_[sid].dict_[label]};
310 assert(ep &&
"RNGservice::restoreSnapshot_()");
312 auto const& est = snapshot.restoreState();
314 log <<
" successfully restored.\n";
317 <<
"RNGservice::restoreSnapshot_():\n" 318 <<
"Failed during restore of state of engine for \"" << label
PerScheduleContainer< ScheduleData > data_
std::recursive_mutex mutex_
bool invariant_holds_(ScheduleID)
cet::coded_exception< error, detail::translate > exception
std::string const restoreStateLabel_
void art::RandomNumberGenerator::saveToFile_ |
( |
| ) |
|
|
private |
Definition at line 326 of file RandomNumberGenerator.cc.
References d, data_, mutex_, and saveToFilename_.
Referenced by postEndJob().
328 std::lock_guard sentry{
mutex_};
332 HEP_CONCURRENCY_ASSERT_ONLY_ONE_THREAD();
340 for (
auto const&
d :
data_) {
341 for (
auto const& [label, eptr] :
d.dict_) {
342 outfile << label <<
'\n';
343 assert(eptr &&
"RNGservice::saveToFile_()");
347 <<
"This module's engine has not been saved;\n" PerScheduleContainer< ScheduleData > data_
std::recursive_mutex mutex_
std::string const saveToFilename_
MaybeLogger_< ELseverityLevel::ELsev_warning, false > LogWarning
void art::RandomNumberGenerator::takeSnapshot_ |
( |
ScheduleID |
sid | ) |
|
|
private |
Definition at line 265 of file RandomNumberGenerator.cc.
References data_, and mutex_.
Referenced by postEndJob(), and preProcessEvent().
267 std::lock_guard sentry{
mutex_};
269 log <<
"RNGservice::takeSnapshot_() of the following engine labels:\n";
270 data_[sid].snapshot_.clear();
271 for (
auto const& [label, eptr] :
data_[sid].dict_) {
272 assert(eptr &&
"RNGservice::takeSnapshot_()");
273 data_[sid].snapshot_.emplace_back(
274 data_[sid].kind_[label], label, eptr->put());
275 log <<
" | " << label;
PerScheduleContainer< ScheduleData > data_
std::recursive_mutex mutex_
void art::RandomNumberGenerator::validate_ |
( |
std::string const & |
user_specified_engine_kind, |
|
|
long |
user_specified_seed |
|
) |
| |
|
privatenoexcept |
Definition at line 199 of file RandomNumberGenerator.cc.
References defaultEngineKind_, maxCLHEPSeed, and useDefaultSeed.
Referenced by createEngine().
209 if (user_specified_seed < 0) {
210 throw cet::exception(
"RANGE") <<
"RNGservice::throw_if_invalid_seed():\n" 211 <<
"Seed " << user_specified_seed
212 <<
" is not permitted to be negative.\n";
220 if (user_specified_engine_kind ==
"MixMaxRng"s)
223 if (user_specified_engine_kind ==
"G4Engine"s &&
228 <<
"RNGservice::throw_if_invalid_seed():\n" 229 <<
"Seed " << user_specified_seed <<
" exceeds permitted maximum of " 230 <<
maxCLHEPSeed <<
" for engine type " << user_specified_engine_kind
static long constexpr maxCLHEPSeed
static long constexpr useDefaultSeed
std::string const defaultEngineKind_
cet::coded_exception< error, detail::translate > exception
bool const art::RandomNumberGenerator::debug_ |
|
private |
std::string const art::RandomNumberGenerator::defaultEngineKind_ |
|
private |
bool art::RandomNumberGenerator::engine_creation_is_okay_ {true} |
|
private |
long constexpr art::RandomNumberGenerator::maxCLHEPSeed {900000000} |
|
static |
std::recursive_mutex art::RandomNumberGenerator::mutex_ {} |
|
mutableprivate |
unsigned const art::RandomNumberGenerator::nPrint_ |
|
private |
std::string const art::RandomNumberGenerator::restoreFromFilename_ |
|
private |
std::string const art::RandomNumberGenerator::restoreStateLabel_ |
|
private |
std::string const art::RandomNumberGenerator::saveToFilename_ |
|
private |
long constexpr art::RandomNumberGenerator::useDefaultSeed {-1} |
|
static |
The documentation for this class was generated from the following files: