LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
art::MixHelper Class Reference

#include "MixHelper.h"

Inheritance diagram for art::MixHelper:
art::detail::EngineCreator

Classes

struct  Config
 

Public Types

enum  Mode {
  Mode::SEQUENTIAL = 0, Mode::RANDOM_REPLACE, Mode::RANDOM_LIM_REPLACE, Mode::RANDOM_NO_REPLACE,
  Mode::UNKNOWN
}
 

Public Member Functions

 MixHelper (Config const &config, std::string const &moduleLabel, ProducesCollector &collector, std::unique_ptr< MixIOPolicy > ioHandle)
 
 MixHelper (fhicl::ParameterSet const &pset, std::string const &moduleLabel, ProducesCollector &collector, std::unique_ptr< MixIOPolicy > ioHandle)
 
 ~MixHelper ()
 
Mode readMode () const
 
void registerSecondaryFileNameProvider (ProviderFunc_ func)
 
template<class P >
void produces (std::string const &instanceName={})
 
template<class P , BranchType B>
void produces (std::string const &instanceName={})
 
template<art::BranchType B = art::InEvent, typename PROD , typename OPROD >
void declareMixOp (InputTag const &inputTag, MixFunc< PROD, OPROD > mixFunc, bool outputProduct=true)
 
template<art::BranchType B = art::InEvent, typename PROD , typename OPROD >
void declareMixOp (InputTag const &inputTag, std::string const &outputInstanceLabel, MixFunc< PROD, OPROD > mixFunc, bool outputProduct=true)
 
template<art::BranchType B = art::InEvent, typename PROD , typename OPROD , typename T >
void declareMixOp (InputTag const &inputTag, bool(T::*mixfunc)(std::vector< PROD const * > const &, OPROD &, PtrRemapper const &), T &t, bool outputProduct=true)
 
template<art::BranchType B = art::InEvent, typename PROD , typename OPROD , typename T >
void declareMixOp (InputTag const &inputTag, std::string const &outputInstanceLabel, bool(T::*mixfunc)(std::vector< PROD const * > const &, OPROD &, PtrRemapper const &), T &t, bool outputProduct=true)
 
template<art::BranchType B = art::InEvent, typename PROD , typename OPROD , typename T >
void declareMixOp (InputTag const &inputTag, bool(T::*mixfunc)(std::vector< PROD const * > const &, OPROD &, PtrRemapper const &) const, T const &t, bool outputProduct=true)
 
template<art::BranchType B = art::InEvent, typename PROD , typename OPROD , typename T >
void declareMixOp (InputTag const &inputTag, std::string const &outputInstanceLabel, bool(T::*mixfunc)(std::vector< PROD const * > const &, OPROD &, PtrRemapper const &) const, T const &t, bool outputProduct=true)
 
base_engine_tcreateEngine (seed_t seed)
 
base_engine_tcreateEngine (seed_t seed, std::string const &kind_of_engine_to_make)
 
base_engine_tcreateEngine (seed_t seed, std::string const &kind_of_engine_to_make, label_t const &engine_label)
 
bool generateEventSequence (size_t nSecondaries, EntryNumberSequence &enSeq, EventIDSequence &eIDseq)
 
EventAuxiliarySequence generateEventAuxiliarySequence (EntryNumberSequence const &)
 
void mixAndPut (EntryNumberSequence const &enSeq, EventIDSequence const &eIDseq, Event &e)
 
void setEventsToSkipFunction (std::function< size_t()> eventsToSkip)
 
template<art::BranchType B, typename PROD , typename OPROD , typename T >
void declareMixOp (InputTag const &inputTag, bool(T::*mixFunc)(std::vector< PROD const * > const &, OPROD &, PtrRemapper const &), T &t, bool outputProduct)
 
template<art::BranchType B, typename PROD , typename OPROD , typename T >
void declareMixOp (InputTag const &inputTag, std::string const &outputInstanceLabel, bool(T::*mixFunc)(std::vector< PROD const * > const &, OPROD &, PtrRemapper const &), T &t, bool outputProduct)
 
template<art::BranchType B, typename PROD , typename OPROD , typename T >
void declareMixOp (InputTag const &inputTag, bool(T::*mixFunc)(std::vector< PROD const * > const &, OPROD &, PtrRemapper const &) const, T const &t, bool outputProduct)
 
template<art::BranchType B, typename PROD , typename OPROD , typename T >
void declareMixOp (InputTag const &inputTag, std::string const &outputInstanceLabel, bool(T::*mixFunc)(std::vector< PROD const * > const &, OPROD &, PtrRemapper const &) const, T const &t, bool outputProduct)
 

Private Types

using ProviderFunc_ = std::function< std::string()>
 
using MixOpList = std::vector< std::unique_ptr< MixOpBase >>
 
using base_engine_t = CLHEP::HepRandomEngine
 
using label_t = RNGsnapshot::label_t
 
using seed_t = long
 
using engine_state_t = RNGsnapshot::engine_state_t
 

Private Member Functions

 MixHelper (MixHelper const &)=delete
 
MixHelperoperator= (MixHelper const &)=delete
 
cet::exempt_ptr< base_engine_tinitEngine_ (seed_t seed, Mode readMode)
 
std::unique_ptr< CLHEP::RandFlat > initDist_ (cet::exempt_ptr< base_engine_t > engine) const
 
bool consistentRequest_ (std::string const &kind_of_engine_to_make, label_t const &engine_label) const
 
Mode initReadMode_ (std::string const &mode) const
 
bool openNextFile_ ()
 
ProdToProdMapBuilder::ProductIDTransMap buildProductIDTransMap_ (MixOpList &mixOps)
 

Private Attributes

ProducesCollectorcollector_
 
std::string const moduleLabel_
 
std::vector< std::string > const filenames_
 
bool compactMissingProducts_
 
ProviderFunc_ providerFunc_ {}
 
MixOpList mixOps_ {}
 
PtrRemapper ptrRemapper_ {}
 
std::vector< std::string >::const_iterator fileIter_
 
Mode const readMode_
 
double const coverageFraction_
 
std::size_t nEventsReadThisFile_ {}
 
std::size_t totalEventsRead_ {}
 
bool const canWrapFiles_
 
unsigned nOpensOverThreshold_ {}
 
ProdToProdMapBuilder ptpBuilder_ {}
 
cet::exempt_ptr< base_engine_tengine_
 
std::unique_ptr< CLHEP::RandFlat > dist_
 
std::function< size_t()> eventsToSkip_ {}
 
EntryNumberSequence shuffledSequence_ {}
 
bool haveSubRunMixOps_ {false}
 
bool haveRunMixOps_ {false}
 
EventIDIndex eventIDIndex_ {}
 
std::unique_ptr< MixIOPolicyioHandle_ {nullptr}
 

Detailed Description

Definition at line 245 of file MixHelper.h.

Member Typedef Documentation

using art::detail::EngineCreator::base_engine_t = CLHEP::HepRandomEngine
inherited

Definition at line 36 of file EngineCreator.h.

Definition at line 37 of file EngineCreator.h.

using art::MixHelper::MixOpList = std::vector<std::unique_ptr<MixOpBase>>
private

Definition at line 382 of file MixHelper.h.

using art::MixHelper::ProviderFunc_ = std::function<std::string()>
private

Definition at line 246 of file MixHelper.h.

using art::detail::EngineCreator::seed_t = long
inherited

Definition at line 38 of file EngineCreator.h.

Member Enumeration Documentation

enum art::MixHelper::Mode
strong
Enumerator
SEQUENTIAL 
RANDOM_REPLACE 
RANDOM_LIM_REPLACE 
RANDOM_NO_REPLACE 
UNKNOWN 

Definition at line 249 of file MixHelper.h.

249  {
250  SEQUENTIAL = 0,
251  RANDOM_REPLACE,
252  RANDOM_LIM_REPLACE,
253  RANDOM_NO_REPLACE,
254  UNKNOWN
255  };

Constructor & Destructor Documentation

art::MixHelper::MixHelper ( Config const &  config,
std::string const &  moduleLabel,
ProducesCollector collector,
std::unique_ptr< MixIOPolicy ioHandle 
)
explicit

Definition at line 108 of file MixHelper.cc.

References canWrapFiles_, collector_, compactMissingProducts_, coverageFraction_, dist_, engine_, fileIter_, filenames_, art::ScheduleID::first(), initDist_(), initEngine_(), initReadMode_(), ioHandle_, moduleLabel_, readMode_, and ~MixHelper().

112  : EngineCreator{moduleLabel, ScheduleID::first()}
113  , collector_{collector}
114  , moduleLabel_{moduleLabel}
115  , filenames_{config.filenames()}
116  , compactMissingProducts_{config.compactMissingProducts()}
117  , fileIter_{filenames_.begin()}
118  , readMode_{initReadMode_(config.readMode())}
119  , coverageFraction_{initCoverageFraction(config.coverageFraction())}
120  , canWrapFiles_{config.wrapFiles()}
121  , engine_{initEngine_(config.seed(), readMode_)}
123  , ioHandle_{std::move(ioHandle)}
124 {}
EngineCreator(std::string const &label, ScheduleID sid)
std::vector< std::string >::const_iterator fileIter_
Definition: MixHelper.h:402
Mode initReadMode_(std::string const &mode) const
Definition: MixHelper.cc:366
cet::exempt_ptr< base_engine_t > initEngine_(seed_t seed, Mode readMode)
Definition: MixHelper.cc:463
static constexpr ScheduleID first()
Definition: ScheduleID.h:50
std::vector< std::string > const filenames_
Definition: MixHelper.h:397
std::unique_ptr< CLHEP::RandFlat > dist_
Definition: MixHelper.h:411
double const coverageFraction_
Definition: MixHelper.h:404
Mode const readMode_
Definition: MixHelper.h:403
bool const canWrapFiles_
Definition: MixHelper.h:407
ProducesCollector & collector_
Definition: MixHelper.h:395
bool compactMissingProducts_
Definition: MixHelper.h:398
std::unique_ptr< MixIOPolicy > ioHandle_
Definition: MixHelper.h:418
std::string const moduleLabel_
Definition: MixHelper.h:396
cet::exempt_ptr< base_engine_t > engine_
Definition: MixHelper.h:410
std::unique_ptr< CLHEP::RandFlat > initDist_(cet::exempt_ptr< base_engine_t > engine) const
Definition: MixHelper.cc:480
art::MixHelper::MixHelper ( fhicl::ParameterSet const &  pset,
std::string const &  moduleLabel,
ProducesCollector collector,
std::unique_ptr< MixIOPolicy ioHandle 
)
explicit

Definition at line 89 of file MixHelper.cc.

References canWrapFiles_, collector_, compactMissingProducts_, coverageFraction_, dist_, engine_, fileIter_, filenames_, art::ScheduleID::first(), initDist_(), initEngine_(), initReadMode_(), ioHandle_, moduleLabel_, and readMode_.

93  : EngineCreator{moduleLabel, ScheduleID::first()}
94  , collector_{collector}
95  , moduleLabel_{moduleLabel}
96  , filenames_{pset.get<std::vector<std::string>>("fileNames", {})}
97  , compactMissingProducts_{pset.get<bool>("compactMissingProducts", false)}
98  , fileIter_{filenames_.begin()}
99  , readMode_{initReadMode_(pset.get<std::string>("readMode", "sequential"))}
100  , coverageFraction_{initCoverageFraction(
101  pset.get<double>("coverageFraction", 1.0))}
102  , canWrapFiles_{pset.get<bool>("wrapFiles", false)}
103  , engine_{initEngine_(pset.get<long>("seed", -1), readMode_)}
105  , ioHandle_{std::move(ioHandle)}
106 {}
EngineCreator(std::string const &label, ScheduleID sid)
std::vector< std::string >::const_iterator fileIter_
Definition: MixHelper.h:402
Mode initReadMode_(std::string const &mode) const
Definition: MixHelper.cc:366
cet::exempt_ptr< base_engine_t > initEngine_(seed_t seed, Mode readMode)
Definition: MixHelper.cc:463
static constexpr ScheduleID first()
Definition: ScheduleID.h:50
std::vector< std::string > const filenames_
Definition: MixHelper.h:397
std::unique_ptr< CLHEP::RandFlat > dist_
Definition: MixHelper.h:411
double const coverageFraction_
Definition: MixHelper.h:404
Mode const readMode_
Definition: MixHelper.h:403
bool const canWrapFiles_
Definition: MixHelper.h:407
ProducesCollector & collector_
Definition: MixHelper.h:395
bool compactMissingProducts_
Definition: MixHelper.h:398
std::unique_ptr< MixIOPolicy > ioHandle_
Definition: MixHelper.h:418
std::string const moduleLabel_
Definition: MixHelper.h:396
cet::exempt_ptr< base_engine_t > engine_
Definition: MixHelper.h:410
std::unique_ptr< CLHEP::RandFlat > initDist_(cet::exempt_ptr< base_engine_t > engine) const
Definition: MixHelper.cc:480
art::MixHelper::~MixHelper ( )
default

Referenced by MixHelper().

art::MixHelper::MixHelper ( MixHelper const &  )
privatedelete

Member Function Documentation

ProdToProdMapBuilder::ProductIDTransMap art::MixHelper::buildProductIDTransMap_ ( MixOpList mixOps)
private
bool art::MixHelper::consistentRequest_ ( std::string const &  kind_of_engine_to_make,
label_t const &  engine_label 
) const
private

Definition at line 441 of file MixHelper.cc.

References art::errors::Configuration, and readMode_.

Referenced by createEngine().

443 {
444  auto const& default_engine_kind =
445  ServiceHandle<RandomNumberGenerator const>()->defaultEngineKind();
446  if (kind_of_engine_to_make == default_engine_kind && engine_label.empty()) {
447  mf::LogInfo{"RANDOM"} << "A random number engine has already been created "
448  "since the read mode is "
449  << readMode_ << '.';
450  return true;
451  }
453  "An error occurred while creating a random number engine "
454  "within a MixFilter detail class.\n"}
455  << "A random number engine with an empty label has already been created "
456  "with an engine type of "
457  << default_engine_kind << ".\n"
458  << "If you would like to use a different engine type, please supply a "
459  "different engine label.\n";
460 }
Mode const readMode_
Definition: MixHelper.h:403
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
art::MixHelper::base_engine_t & art::MixHelper::createEngine ( seed_t  seed)

Definition at line 159 of file MixHelper.cc.

References consistentRequest_(), art::detail::EngineCreator::createEngine(), and engine_.

160 {
161  if (engine_ &&
163  ServiceHandle<RandomNumberGenerator const>()->defaultEngineKind(),
164  ""s)) {
165  return *engine_;
166  }
168 }
base_engine_t & createEngine(seed_t seed)
bool consistentRequest_(std::string const &kind_of_engine_to_make, label_t const &engine_label) const
Definition: MixHelper.cc:441
long seed
Definition: chem4.cc:67
cet::exempt_ptr< base_engine_t > engine_
Definition: MixHelper.h:410
art::MixHelper::base_engine_t & art::MixHelper::createEngine ( seed_t  seed,
std::string const &  kind_of_engine_to_make 
)

Definition at line 171 of file MixHelper.cc.

References consistentRequest_(), art::detail::EngineCreator::createEngine(), and engine_.

173 {
174  if (engine_ && consistentRequest_(kind_of_engine_to_make, ""s)) {
175  return *engine_;
176  }
177  return detail::EngineCreator::createEngine(seed, kind_of_engine_to_make);
178 }
base_engine_t & createEngine(seed_t seed)
bool consistentRequest_(std::string const &kind_of_engine_to_make, label_t const &engine_label) const
Definition: MixHelper.cc:441
long seed
Definition: chem4.cc:67
cet::exempt_ptr< base_engine_t > engine_
Definition: MixHelper.h:410
art::MixHelper::base_engine_t & art::MixHelper::createEngine ( seed_t  seed,
std::string const &  kind_of_engine_to_make,
label_t const &  engine_label 
)

Definition at line 181 of file MixHelper.cc.

References consistentRequest_(), art::detail::EngineCreator::createEngine(), and engine_.

184 {
185  if (engine_ && consistentRequest_(kind_of_engine_to_make, engine_label)) {
186  return *engine_;
187  }
189  seed, kind_of_engine_to_make, engine_label);
190 }
base_engine_t & createEngine(seed_t seed)
bool consistentRequest_(std::string const &kind_of_engine_to_make, label_t const &engine_label) const
Definition: MixHelper.cc:441
long seed
Definition: chem4.cc:67
cet::exempt_ptr< base_engine_t > engine_
Definition: MixHelper.h:410
template<art::BranchType B, typename PROD , typename OPROD >
void art::MixHelper::declareMixOp ( InputTag const &  inputTag,
MixFunc< PROD, OPROD >  mixFunc,
bool  outputProduct = true 
)
inline

Definition at line 449 of file MixHelper.h.

References art::InputTag::instance().

452 {
453  declareMixOp<B>(inputTag, inputTag.instance(), mixFunc, outputProduct); // 2.
454 }
template<art::BranchType B, typename PROD , typename OPROD >
void art::MixHelper::declareMixOp ( InputTag const &  inputTag,
std::string const &  outputInstanceLabel,
MixFunc< PROD, OPROD >  mixFunc,
bool  outputProduct = true 
)

Definition at line 459 of file MixHelper.h.

References compactMissingProducts_, haveRunMixOps_, haveSubRunMixOps_, art::InRun, art::InSubRun, mixOps_, and moduleLabel_.

463 {
464  if (outputProduct) {
465  produces<OPROD>(outputInstanceLabel);
466  }
467  if (B == art::InSubRun) {
468  haveSubRunMixOps_ = true;
469  } else if (B == art::InRun) {
470  haveRunMixOps_ = true;
471  }
472  mixOps_.emplace_back(new MixOp<PROD, OPROD>(moduleLabel_,
473  inputTag,
474  outputInstanceLabel,
475  mixFunc,
476  outputProduct,
478  B));
479 }
MixOpList mixOps_
Definition: MixHelper.h:400
bool haveSubRunMixOps_
Definition: MixHelper.h:414
bool compactMissingProducts_
Definition: MixHelper.h:398
std::string const moduleLabel_
Definition: MixHelper.h:396
bool haveRunMixOps_
Definition: MixHelper.h:415
template<art::BranchType B = art::InEvent, typename PROD , typename OPROD , typename T >
void art::MixHelper::declareMixOp ( InputTag const &  inputTag,
bool(T::*)(std::vector< PROD const * > const &, OPROD &, PtrRemapper const &)  mixfunc,
T &  t,
bool  outputProduct = true 
)
template<art::BranchType B = art::InEvent, typename PROD , typename OPROD , typename T >
void art::MixHelper::declareMixOp ( InputTag const &  inputTag,
std::string const &  outputInstanceLabel,
bool(T::*)(std::vector< PROD const * > const &, OPROD &, PtrRemapper const &)  mixfunc,
T &  t,
bool  outputProduct = true 
)
template<art::BranchType B = art::InEvent, typename PROD , typename OPROD , typename T >
void art::MixHelper::declareMixOp ( InputTag const &  inputTag,
bool(T::*)(std::vector< PROD const * > const &, OPROD &, PtrRemapper const &) const  mixfunc,
T const &  t,
bool  outputProduct = true 
)
template<art::BranchType B = art::InEvent, typename PROD , typename OPROD , typename T >
void art::MixHelper::declareMixOp ( InputTag const &  inputTag,
std::string const &  outputInstanceLabel,
bool(T::*)(std::vector< PROD const * > const &, OPROD &, PtrRemapper const &) const  mixfunc,
T const &  t,
bool  outputProduct = true 
)
template<art::BranchType B, typename PROD , typename OPROD , typename T >
void art::MixHelper::declareMixOp ( InputTag const &  inputTag,
bool(T::*)(std::vector< PROD const * > const &, OPROD &, PtrRemapper const &)  mixFunc,
T &  t,
bool  outputProduct 
)
inline

Definition at line 484 of file MixHelper.h.

References art::InputTag::instance().

490 {
491  declareMixOp<B>(inputTag,
492  inputTag.instance(),
493  mixFunc,
494  t,
495  outputProduct); // 4.
496 }
template<art::BranchType B, typename PROD , typename OPROD , typename T >
void art::MixHelper::declareMixOp ( InputTag const &  inputTag,
std::string const &  outputInstanceLabel,
bool(T::*)(std::vector< PROD const * > const &, OPROD &, PtrRemapper const &)  mixFunc,
T &  t,
bool  outputProduct 
)

Definition at line 501 of file MixHelper.h.

508 {
509  using std::placeholders::_1;
510  using std::placeholders::_2;
511  using std::placeholders::_3;
512  declareMixOp<B>(
513  inputTag,
514  outputInstanceLabel,
515  static_cast<MixFunc<PROD, OPROD>>(std::bind(mixFunc, &t, _1, _2, _3)),
516  outputProduct); // 2.
517 }
template<art::BranchType B, typename PROD , typename OPROD , typename T >
void art::MixHelper::declareMixOp ( InputTag const &  inputTag,
bool(T::*)(std::vector< PROD const * > const &, OPROD &, PtrRemapper const &) const  mixFunc,
T const &  t,
bool  outputProduct 
)
inline

Definition at line 522 of file MixHelper.h.

References art::InputTag::instance().

528 {
529  declareMixOp<B>(inputTag,
530  inputTag.instance(),
531  mixFunc,
532  t,
533  outputProduct); // 6.
534 }
template<art::BranchType B, typename PROD , typename OPROD , typename T >
void art::MixHelper::declareMixOp ( InputTag const &  inputTag,
std::string const &  outputInstanceLabel,
bool(T::*)(std::vector< PROD const * > const &, OPROD &, PtrRemapper const &) const  mixFunc,
T const &  t,
bool  outputProduct 
)

Definition at line 539 of file MixHelper.h.

546 {
547  using std::placeholders::_1;
548  using std::placeholders::_2;
549  using std::placeholders::_3;
550  declareMixOp<B>(
551  inputTag,
552  outputInstanceLabel,
553  static_cast<MixFunc<PROD, OPROD>>(std::bind(mixFunc, &t, _1, _2, _3)),
554  outputProduct); // 2.
555 }
art::EventAuxiliarySequence art::MixHelper::generateEventAuxiliarySequence ( EntryNumberSequence const &  enSeq)

Definition at line 279 of file MixHelper.cc.

References ioHandle_.

Referenced by art::MixFilter< T, IOPolicy >::filter().

280 {
281  return ioHandle_->generateEventAuxiliarySequence(enSeq);
282 }
std::unique_ptr< MixIOPolicy > ioHandle_
Definition: MixHelper.h:418
bool art::MixHelper::generateEventSequence ( size_t  nSecondaries,
EntryNumberSequence enSeq,
EventIDSequence eIDseq 
)

Definition at line 193 of file MixHelper.cc.

References util::begin(), util::cbegin(), util::cend(), coverageFraction_, dist_, util::end(), eventIDIndex_, filenames_, ioHandle_, art::errors::LogicError, nEventsReadThisFile_, nOpensOverThreshold_, openNextFile_(), providerFunc_, RANDOM_LIM_REPLACE, RANDOM_NO_REPLACE, RANDOM_REPLACE, readMode_, SEQUENTIAL, shuffledSequence_, and art::errors::UnimplementedFeature.

Referenced by art::MixFilter< T, IOPolicy >::filter().

196 {
197  assert(enSeq.empty());
198  assert(eIDseq.empty());
199  if (not ioHandle_->fileOpen() and not openNextFile_()) {
200  return false;
201  }
202 
203  auto const nEventsInFile = ioHandle_->nEventsInFile();
204  bool const over_threshold =
206  ((nEventsReadThisFile_ + nSecondaries) > nEventsInFile) :
207  ((nEventsReadThisFile_ + nSecondaries) >
208  (nEventsInFile * coverageFraction_));
209  if (over_threshold) {
210  if (!providerFunc_) {
212  if (nOpensOverThreshold_ > filenames_.size()) {
214  "An error occurred while preparing product-mixing for "
215  "the current event.\n"}
216  << "The number of requested secondaries (" << nSecondaries
217  << ") exceeds the number of events in any\n"
218  << "of the files specified for product mixing. For a read mode of '"
219  << readMode_ << "',\n"
220  << "the framework does not currently allow product-mixing to span "
221  "multiple secondary\n"
222  << "input files for a given event. Please contact artists@fnal.gov "
223  "for more information.\n";
224  }
225  }
226  if (openNextFile_()) {
227  return generateEventSequence(nSecondaries, enSeq, eIDseq);
228  } else {
229  return false;
230  }
231  }
232 
234  switch (readMode_) {
235  case Mode::SEQUENTIAL:
236  enSeq.resize(nSecondaries);
237  std::iota(begin(enSeq), end(enSeq), nEventsReadThisFile_);
238  break;
240  std::generate_n(
241  std::back_inserter(enSeq), nSecondaries, [this, nEventsInFile] {
242  return dist_.get()->fireInt(nEventsInFile);
243  });
244  std::sort(enSeq.begin(), enSeq.end());
245  break;
247  std::unordered_set<EntryNumberSequence::value_type>
248  entries; // Guaranteed unique.
249  while (entries.size() < nSecondaries) {
250  std::generate_n(
251  std::inserter(entries, entries.begin()),
252  nSecondaries - entries.size(),
253  [this, nEventsInFile] { return dist_.get()->fireInt(nEventsInFile); });
254  }
255  enSeq.assign(cbegin(entries), cend(entries));
256  std::sort(begin(enSeq), end(enSeq));
257  // Since we need to sort at the end anyway, it's unclear whether
258  // unordered_set is faster than set even though inserts are
259  // approximately linear time. Since the complexity of the sort is
260  // NlogN, we'd need a profile run for it all to come out in the
261  // wash.
262  assert(enSeq.size() == nSecondaries); // Should be true by construction.
263  } break;
265  auto i = shuffledSequence_.cbegin() + nEventsReadThisFile_;
266  enSeq.assign(i, i + nSecondaries);
267  } break;
268  default:
270  << "Unrecognized read mode " << static_cast<int>(readMode_)
271  << ". Contact the art developers.\n";
272  }
273  cet::transform_all(
274  enSeq, back_inserter(eIDseq), EventIDLookup{eventIDIndex_});
275  return true;
276 }
decltype(auto) constexpr cend(T &&obj)
ADL-aware version of std::cend.
Definition: StdUtils.h:93
ProviderFunc_ providerFunc_
Definition: MixHelper.h:399
unsigned nOpensOverThreshold_
Definition: MixHelper.h:408
EntryNumberSequence shuffledSequence_
Definition: MixHelper.h:413
std::vector< std::string > const filenames_
Definition: MixHelper.h:397
std::unique_ptr< CLHEP::RandFlat > dist_
Definition: MixHelper.h:411
double const coverageFraction_
Definition: MixHelper.h:404
Mode const readMode_
Definition: MixHelper.h:403
decltype(auto) constexpr end(T &&obj)
ADL-aware version of std::end.
Definition: StdUtils.h:77
bool generateEventSequence(size_t nSecondaries, EntryNumberSequence &enSeq, EventIDSequence &eIDseq)
Definition: MixHelper.cc:193
bool openNextFile_()
Definition: MixHelper.cc:393
std::unique_ptr< MixIOPolicy > ioHandle_
Definition: MixHelper.h:418
std::size_t nEventsReadThisFile_
Definition: MixHelper.h:405
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
EventIDIndex eventIDIndex_
Definition: MixHelper.h:416
decltype(auto) constexpr cbegin(T &&obj)
ADL-aware version of std::cbegin.
Definition: StdUtils.h:85
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:69
std::unique_ptr< CLHEP::RandFlat > art::MixHelper::initDist_ ( cet::exempt_ptr< base_engine_t engine) const
private

Definition at line 480 of file MixHelper.cc.

Referenced by MixHelper().

481 {
482  return engine ? std::make_unique<CLHEP::RandFlat>(*engine) : nullptr;
483 }
cet::exempt_ptr< art::MixHelper::base_engine_t > art::MixHelper::initEngine_ ( seed_t  seed,
Mode  readMode 
)
private

Definition at line 463 of file MixHelper.cc.

References art::errors::Configuration, art::detail::EngineCreator::createEngine(), and SEQUENTIAL.

Referenced by MixHelper().

464 {
465  using namespace art;
467  if (ServiceRegistry::isAvailable<RandomNumberGenerator>()) {
468  return cet::make_exempt_ptr(&detail::EngineCreator::createEngine(seed));
469  }
470  throw Exception{errors::Configuration, "MixHelper"}
471  << "Random event mixing selected but RandomNumberGenerator service "
472  "not loaded.\n"
473  << "Ensure service is loaded with: \n"
474  << "services.RandomNumberGenerator: {}\n";
475  }
476  return nullptr;
477 }
base_engine_t & createEngine(seed_t seed)
long seed
Definition: chem4.cc:67
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
Mode readMode() const
Definition: MixHelper.h:425
Definition: MVAAlg.h:12
auto art::MixHelper::initReadMode_ ( std::string const &  mode) const
private

Definition at line 366 of file MixHelper.cc.

References art::errors::Configuration, and r.

Referenced by MixHelper().

367 {
368  // These regexes must correspond by index to the valid Mode enumerator
369  // values.
370  static std::regex const robjs[4]{
371  std::regex("^seq", std::regex_constants::icase),
372  std::regex("^random(replace)?$", std::regex_constants::icase),
373  std::regex("^randomlimreplace$", std::regex_constants::icase),
374  std::regex("^randomnoreplace$", std::regex_constants::icase)};
375  int i{0};
376  for (auto const& r : robjs) {
377  if (std::regex_search(mode, r)) {
378  return Mode(i);
379  } else {
380  ++i;
381  }
382  }
384  << "Unrecognized value of readMode parameter: \"" << mode
385  << "\". Valid values are:\n"
386  << " sequential,\n"
387  << " randomReplace (random is accepted for reasons of legacy),\n"
388  << " randomLimReplace,\n"
389  << " randomNoReplace.\n";
390 }
TRandom r
Definition: spectrum.C:23
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
void art::MixHelper::mixAndPut ( EntryNumberSequence const &  enSeq,
EventIDSequence const &  eIDseq,
Event e 
)

Definition at line 289 of file MixHelper.cc.

References util::cend(), fileIter_, art::FileIndex::findPosition(), art::ProdToProdMapBuilder::getRemapper(), haveRunMixOps_, haveSubRunMixOps_, art::InEvent, art::InRun, art::InSubRun, ioHandle_, art::errors::LogicError, mixOps_, nEventsReadThisFile_, art::errors::NotFound, ptpBuilder_, ptrRemapper_, and totalEventsRead_.

Referenced by art::MixFilter< T, IOPolicy >::filter().

292 {
293  // Create required info only if we're likely to need it.
294  EntryNumberSequence subRunEntries;
295  EntryNumberSequence runEntries;
296  auto const& fileIndex = ioHandle_->fileIndex();
297  if (haveSubRunMixOps_) {
298  subRunEntries.reserve(eIDseq.size());
299  for (auto const& eID : eIDseq) {
300  auto const it = fileIndex.findPosition(eID.subRunID(), true);
301  if (it == std::cend(fileIndex)) {
302  throw Exception(errors::NotFound, "NO_SUBRUN")
303  << "- Unable to find an entry in the SubRun tree corresponding to "
304  "event ID "
305  << eID << " in secondary mixing input file " << *fileIter_ << ".\n";
306  }
307  subRunEntries.emplace_back(it->entry);
308  }
309  }
310  if (haveRunMixOps_) {
311  runEntries.reserve(eIDseq.size());
312  for (auto const& eID : eIDseq) {
313  auto const it = fileIndex.findPosition(eID.runID(), true);
314  if (it == std::cend(fileIndex)) {
315  throw Exception(errors::NotFound, "NO_RUN")
316  << "- Unable to find an entry in the Run tree corresponding to "
317  "event ID "
318  << eID << " in secondary mixing input file " << *fileIter_ << ".\n";
319  }
320  runEntries.emplace_back(it->entry);
321  }
322  }
323 
324  // Populate the remapper in case we need to remap any Ptrs.
326 
327  // Do the branch-wise read, mix and put.
328  for (auto const& op : mixOps_) {
329  switch (op->branchType()) {
330  case InEvent: {
331  auto const inProducts = ioHandle_->readFromFile(*op, eventEntries);
332  op->mixAndPut(e, inProducts, ptrRemapper_);
333  continue;
334  }
335  case InSubRun: {
336  auto const inProducts = ioHandle_->readFromFile(*op, subRunEntries);
337  // Ptrs not supported for subrun product mixing.
338  op->mixAndPut(e, inProducts, nopRemapper);
339  continue;
340  }
341  case InRun: {
342  auto const inProducts = ioHandle_->readFromFile(*op, runEntries);
343  // Ptrs not support for run product mixing.
344  op->mixAndPut(e, inProducts, nopRemapper);
345  continue;
346  }
347  default:
348  throw Exception(errors::LogicError, "Unsupported BranchType")
349  << "- MixHelper::mixAndPut() attempted to handle unsupported branch "
350  "type "
351  << op->branchType() << ".\n";
352  }
353  }
354 
355  nEventsReadThisFile_ += eventEntries.size();
356  totalEventsRead_ += eventEntries.size();
357 }
std::vector< std::string >::const_iterator fileIter_
Definition: MixHelper.h:402
decltype(auto) constexpr cend(T &&obj)
ADL-aware version of std::cend.
Definition: StdUtils.h:93
std::size_t totalEventsRead_
Definition: MixHelper.h:406
MixOpList mixOps_
Definition: MixHelper.h:400
bool haveSubRunMixOps_
Definition: MixHelper.h:414
std::unique_ptr< MixIOPolicy > ioHandle_
Definition: MixHelper.h:418
std::vector< FileIndex::EntryNumber_t > EntryNumberSequence
Definition: MixTypes.h:27
PtrRemapper ptrRemapper_
Definition: MixHelper.h:401
std::size_t nEventsReadThisFile_
Definition: MixHelper.h:405
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
bool haveRunMixOps_
Definition: MixHelper.h:415
PtrRemapper getRemapper(Event const &e) const
ProdToProdMapBuilder ptpBuilder_
Definition: MixHelper.h:409
bool art::MixHelper::openNextFile_ ( )
private

Definition at line 393 of file MixHelper.cc.

References canWrapFiles_, eventIDIndex_, eventsToSkip_, fileIter_, filenames_, ioHandle_, mixOps_, nEventsReadThisFile_, art::ProdToProdMapBuilder::prepareTranslationTables(), providerFunc_, ptpBuilder_, RANDOM_NO_REPLACE, readMode_, SEQUENTIAL, shuffledSequence_, and totalEventsRead_.

Referenced by generateEventSequence().

394 {
395  std::string filename;
396  if (providerFunc_) {
397  filename = providerFunc_();
398  if (filename.empty()) {
399  return false;
400  }
401  } else if (filenames_.empty()) {
402  return false;
403  } else {
404  if (ioHandle_->fileOpen()) { // Already seen one file.
405  ++fileIter_;
406  }
407  if (fileIter_ == filenames_.end()) {
408  if (canWrapFiles_) {
409  mf::LogWarning("MixingInputWrap")
410  << "Wrapping around to initial input file for mixing after "
411  << totalEventsRead_ << " secondary events read.";
412  fileIter_ = filenames_.begin();
413  } else {
414  return false;
415  }
416  }
417  filename = *fileIter_;
418  }
420  eventsToSkip_() :
421  0; // Reset for this file.
422  ioHandle_->openAndReadMetaData(filename, mixOps_);
423 
424  eventIDIndex_ = buildEventIDIndex(ioHandle_->fileIndex());
425  auto transMap = buildProductIDTransMap(mixOps_);
427 
429  // Prepare shuffled event sequence.
430  shuffledSequence_.resize(ioHandle_->nEventsInFile());
431  std::iota(shuffledSequence_.begin(), shuffledSequence_.end(), 0);
432  std::random_device rd;
433  std::mt19937 g{rd()};
434  std::shuffle(shuffledSequence_.begin(), shuffledSequence_.end(), g);
435  }
436 
437  return true;
438 }
std::vector< std::string >::const_iterator fileIter_
Definition: MixHelper.h:402
std::function< size_t()> eventsToSkip_
Definition: MixHelper.h:412
ProviderFunc_ providerFunc_
Definition: MixHelper.h:399
std::size_t totalEventsRead_
Definition: MixHelper.h:406
MixOpList mixOps_
Definition: MixHelper.h:400
EntryNumberSequence shuffledSequence_
Definition: MixHelper.h:413
std::vector< std::string > const filenames_
Definition: MixHelper.h:397
Mode const readMode_
Definition: MixHelper.h:403
bool const canWrapFiles_
Definition: MixHelper.h:407
void prepareTranslationTables(ProductIDTransMap &transMap)
std::unique_ptr< MixIOPolicy > ioHandle_
Definition: MixHelper.h:418
std::size_t nEventsReadThisFile_
Definition: MixHelper.h:405
EventIDIndex eventIDIndex_
Definition: MixHelper.h:416
MaybeLogger_< ELseverityLevel::ELsev_warning, false > LogWarning
ProdToProdMapBuilder ptpBuilder_
Definition: MixHelper.h:409
MixHelper& art::MixHelper::operator= ( MixHelper const &  )
privatedelete
template<class P >
void art::MixHelper::produces ( std::string const &  instanceName = {})
inline

Definition at line 433 of file MixHelper.h.

References collector_, and art::ProducesCollector::produces().

434 {
435  collector_.produces<P>(instanceName);
436 }
void produces(std::string const &instanceName={}, Persistable const persistable=Persistable::Yes)
ProducesCollector & collector_
Definition: MixHelper.h:395
template<class P , art::BranchType B>
void art::MixHelper::produces ( std::string const &  instanceName = {})
inline

Definition at line 441 of file MixHelper.h.

References collector_, and art::ProducesCollector::produces().

442 {
443  collector_.produces<P, B>(instanceName);
444 }
void produces(std::string const &instanceName={}, Persistable const persistable=Persistable::Yes)
ProducesCollector & collector_
Definition: MixHelper.h:395
auto art::MixHelper::readMode ( ) const
inline

Definition at line 425 of file MixHelper.h.

References readMode_.

426 {
427  return readMode_;
428 }
Mode const readMode_
Definition: MixHelper.h:403
void art::MixHelper::registerSecondaryFileNameProvider ( ProviderFunc_  func)

Definition at line 148 of file MixHelper.cc.

References art::errors::Configuration, filenames_, and providerFunc_.

149 {
150  if (!filenames_.empty()) {
152  << "Provision of a secondary file name provider is incompatible"
153  << " with a\nnon-empty fileNames parameter to the mix filter.\n";
154  }
155  providerFunc_ = func;
156 }
ProviderFunc_ providerFunc_
Definition: MixHelper.h:399
std::vector< std::string > const filenames_
Definition: MixHelper.h:397
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
void art::MixHelper::setEventsToSkipFunction ( std::function< size_t()>  eventsToSkip)

Definition at line 360 of file MixHelper.cc.

References eventsToSkip_.

Referenced by art::MixFilter< T, IOPolicy >::MixFilter().

361 {
362  eventsToSkip_ = eventsToSkip;
363 }
std::function< size_t()> eventsToSkip_
Definition: MixHelper.h:412

Member Data Documentation

bool const art::MixHelper::canWrapFiles_
private

Definition at line 407 of file MixHelper.h.

Referenced by MixHelper(), and openNextFile_().

ProducesCollector& art::MixHelper::collector_
private

Definition at line 395 of file MixHelper.h.

Referenced by MixHelper(), and produces().

bool art::MixHelper::compactMissingProducts_
private

Definition at line 398 of file MixHelper.h.

Referenced by declareMixOp(), and MixHelper().

double const art::MixHelper::coverageFraction_
private

Definition at line 404 of file MixHelper.h.

Referenced by generateEventSequence(), and MixHelper().

std::unique_ptr<CLHEP::RandFlat> art::MixHelper::dist_
private

Definition at line 411 of file MixHelper.h.

Referenced by generateEventSequence(), and MixHelper().

cet::exempt_ptr<base_engine_t> art::MixHelper::engine_
private

Definition at line 410 of file MixHelper.h.

Referenced by createEngine(), and MixHelper().

EventIDIndex art::MixHelper::eventIDIndex_ {}
private

Definition at line 416 of file MixHelper.h.

Referenced by generateEventSequence(), and openNextFile_().

std::function<size_t()> art::MixHelper::eventsToSkip_ {}
private

Definition at line 412 of file MixHelper.h.

Referenced by openNextFile_(), and setEventsToSkipFunction().

std::vector<std::string>::const_iterator art::MixHelper::fileIter_
private

Definition at line 402 of file MixHelper.h.

Referenced by mixAndPut(), MixHelper(), and openNextFile_().

std::vector<std::string> const art::MixHelper::filenames_
private
bool art::MixHelper::haveRunMixOps_ {false}
private

Definition at line 415 of file MixHelper.h.

Referenced by declareMixOp(), and mixAndPut().

bool art::MixHelper::haveSubRunMixOps_ {false}
private

Definition at line 414 of file MixHelper.h.

Referenced by declareMixOp(), and mixAndPut().

std::unique_ptr<MixIOPolicy> art::MixHelper::ioHandle_ {nullptr}
private
MixOpList art::MixHelper::mixOps_ {}
private

Definition at line 400 of file MixHelper.h.

Referenced by declareMixOp(), mixAndPut(), and openNextFile_().

std::string const art::MixHelper::moduleLabel_
private

Definition at line 396 of file MixHelper.h.

Referenced by declareMixOp(), and MixHelper().

std::size_t art::MixHelper::nEventsReadThisFile_ {}
private

Definition at line 405 of file MixHelper.h.

Referenced by generateEventSequence(), mixAndPut(), and openNextFile_().

unsigned art::MixHelper::nOpensOverThreshold_ {}
private

Definition at line 408 of file MixHelper.h.

Referenced by generateEventSequence().

ProviderFunc_ art::MixHelper::providerFunc_ {}
private
ProdToProdMapBuilder art::MixHelper::ptpBuilder_ {}
private

Definition at line 409 of file MixHelper.h.

Referenced by mixAndPut(), and openNextFile_().

PtrRemapper art::MixHelper::ptrRemapper_ {}
private

Definition at line 401 of file MixHelper.h.

Referenced by mixAndPut().

Mode const art::MixHelper::readMode_
private
EntryNumberSequence art::MixHelper::shuffledSequence_ {}
private

Definition at line 413 of file MixHelper.h.

Referenced by generateEventSequence(), and openNextFile_().

std::size_t art::MixHelper::totalEventsRead_ {}
private

Definition at line 406 of file MixHelper.h.

Referenced by mixAndPut(), and openNextFile_().


The documentation for this class was generated from the following files: