LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
art::MixHelper Class Reference

#include "MixHelper.h"

Public Types

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

Public Member Functions

 MixHelper (fhicl::ParameterSet const &pset, ProducerBase &producesProvider)
 
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)
 
bool generateEventSequence (size_t nSecondaries, EntryNumberSequence &enSeq, EventIDSequence &eIDseq)
 
void generateEventAuxiliarySequence (EntryNumberSequence const &, EventAuxiliarySequence &)
 
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

typedef std::function< std::string()> ProviderFunc_
 
using MixOpList = std::vector< std::unique_ptr< MixOpBase >>
 

Private Member Functions

 MixHelper (MixHelper const &)=delete
 
MixHelperoperator= (MixHelper const &)=delete
 
Mode initReadMode_ (std::string const &mode) const
 
void openAndReadMetaData_ (std::string fileName)
 
bool openNextFile_ ()
 
void buildProductIDTransMap_ (ProdToProdMapBuilder::ProductIDTransMap &transMap)
 

Private Attributes

ProducerBaseproducesProvider_
 
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_
 
Long64_t nEventsReadThisFile_ {}
 
Long64_t nEventsInFile_ {}
 
Long64_t totalEventsRead_ {}
 
bool const canWrapFiles_
 
FileFormatVersion ffVersion_ {}
 
std::unique_ptr< art::BranchIDLists > branchIDLists_
 
ProdToProdMapBuilder ptpBuilder_ {}
 
std::unique_ptr< CLHEP::RandFlat > dist_
 
std::function< size_t()> eventsToSkip_ {}
 
EntryNumberSequence shuffledSequence_ {}
 
bool haveSubRunMixOps_ {false}
 
bool haveRunMixOps_ {false}
 
cet::value_ptr< TFile > currentFile_ {}
 
cet::exempt_ptr< TTree > currentMetaDataTree_ {nullptr}
 
std::array< cet::exempt_ptr< TTree >, art::BranchType::NumBranchTypes > currentDataTrees_ {{nullptr}}
 
FileIndex currentFileIndex_ {}
 
std::array< RootBranchInfoList, art::BranchType::NumBranchTypes > dataBranches_
 
EventIDIndex eventIDIndex_ {}
 

Detailed Description

Definition at line 244 of file MixHelper.h.

Member Typedef Documentation

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

Definition at line 355 of file MixHelper.h.

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

Definition at line 246 of file MixHelper.h.

Member Enumeration Documentation

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

Definition at line 249 of file MixHelper.h.

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

Constructor & Destructor Documentation

art::MixHelper::MixHelper ( fhicl::ParameterSet const &  pset,
ProducerBase producesProvider 
)

Definition at line 119 of file MixHelper.cc.

References canWrapFiles_, compactMissingProducts_, coverageFraction_, dist_, fileIter_, filenames_, initReadMode_(), and readMode_.

121  : producesProvider_{producesProvider}
122  , filenames_{pset.get<std::vector<std::string>>("fileNames", {})}
123  , compactMissingProducts_{pset.get<bool>("compactMissingProducts", false)}
124  , fileIter_{filenames_.begin()}
125  , readMode_{initReadMode_(pset.get<std::string>("readMode", "sequential"))}
126  , coverageFraction_{initCoverageFraction(pset)}
127  , canWrapFiles_{pset.get<bool>("wrapFiles", false)}
128  , dist_{initDist(readMode_)}
129 {}
std::vector< std::string >::const_iterator fileIter_
Definition: MixHelper.h:370
ProducerBase & producesProvider_
Definition: MixHelper.h:364
Mode initReadMode_(std::string const &mode) const
Definition: MixHelper.cc:316
std::vector< std::string > const filenames_
Definition: MixHelper.h:365
std::unique_ptr< CLHEP::RandFlat > dist_
Definition: MixHelper.h:381
double const coverageFraction_
Definition: MixHelper.h:372
Mode const readMode_
Definition: MixHelper.h:371
bool const canWrapFiles_
Definition: MixHelper.h:376
bool compactMissingProducts_
Definition: MixHelper.h:366
art::MixHelper::MixHelper ( MixHelper const &  )
privatedelete

Member Function Documentation

void art::MixHelper::buildProductIDTransMap_ ( ProdToProdMapBuilder::ProductIDTransMap transMap)
private

Definition at line 456 of file MixHelper.cc.

References dataBranches_, art::InEvent, and mixOps_.

Referenced by openAndReadMetaData_().

458 {
459  for (auto& mixOp : mixOps_) {
460  auto const bt = mixOp->branchType();
461  mixOp->initializeBranchInfo(dataBranches_[bt]);
462 #if ART_DEBUG_PTRREMAPPER
463  std::cerr << "BranchIDTransMap: " << std::hex << std::setfill('0')
464  << std::setw(8) << mixOp->incomingProductID() << " -> "
465  << std::setw(8) << mixOp->outgoingProductID() << std::dec
466  << ".\n";
467 #endif
468  if (bt == InEvent) {
469  transMap[mixOp->incomingProductID()] = mixOp->outgoingProductID();
470  }
471  }
472 }
MixOpList mixOps_
Definition: MixHelper.h:368
std::array< RootBranchInfoList, art::BranchType::NumBranchTypes > dataBranches_
Definition: MixHelper.h:393
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 423 of file MixHelper.h.

References art::InputTag::instance().

426 {
427  declareMixOp<B>(inputTag, inputTag.instance(), mixFunc, outputProduct); // 2.
428 }
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 433 of file MixHelper.h.

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

437 {
438  if (outputProduct) {
439  produces<OPROD>(outputInstanceLabel);
440  }
441  if (B == art::InSubRun) {
442  haveSubRunMixOps_ = true;
443  } else if (B == art::InRun) {
444  haveRunMixOps_ = true;
445  }
446  mixOps_.emplace_back(new MixOp<PROD, OPROD>(inputTag,
447  outputInstanceLabel,
448  mixFunc,
449  outputProduct,
451  B));
452 }
Int_t B
Definition: plot.C:25
MixOpList mixOps_
Definition: MixHelper.h:368
bool haveSubRunMixOps_
Definition: MixHelper.h:384
bool compactMissingProducts_
Definition: MixHelper.h:366
bool haveRunMixOps_
Definition: MixHelper.h:385
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 457 of file MixHelper.h.

References art::InputTag::instance().

463 {
464  declareMixOp<B>(
465  inputTag, inputTag.instance(), mixFunc, t, outputProduct); // 4.
466 }
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 471 of file MixHelper.h.

478 {
479  using std::placeholders::_1;
480  using std::placeholders::_2;
481  using std::placeholders::_3;
482  declareMixOp<B>(
483  inputTag,
484  outputInstanceLabel,
485  static_cast<MixFunc<PROD, OPROD>>(std::bind(mixFunc, &t, _1, _2, _3)),
486  outputProduct); // 2.
487 }
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 492 of file MixHelper.h.

References art::InputTag::instance().

498 {
499  declareMixOp<B>(
500  inputTag, inputTag.instance(), mixFunc, t, outputProduct); // 6.
501 }
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 506 of file MixHelper.h.

513 {
514  using std::placeholders::_1;
515  using std::placeholders::_2;
516  using std::placeholders::_3;
517  declareMixOp<B>(
518  inputTag,
519  outputInstanceLabel,
520  static_cast<MixFunc<PROD, OPROD>>(std::bind(mixFunc, &t, _1, _2, _3)),
521  outputProduct); // 2.
522 }
void art::MixHelper::generateEventAuxiliarySequence ( EntryNumberSequence const &  enseq,
EventAuxiliarySequence auxseq 
)

Definition at line 212 of file MixHelper.cc.

References art::BranchTypeToAuxiliaryBranchName(), currentDataTrees_, art::input::getEntry(), and art::InEvent.

Referenced by art::detail::call_processEventAuxiliaries< T >::operator()().

214 {
215  auto const eventTree = currentDataTrees_[InEvent];
216  auto auxBranch =
217  eventTree->GetBranch(BranchTypeToAuxiliaryBranchName(InEvent).c_str());
218  auto aux = std::make_unique<EventAuxiliary>();
219  auto pAux = aux.get();
220  auxBranch->SetAddress(&pAux);
221  for (auto const entry : enseq) {
222  auto err = eventTree->LoadTree(entry);
223  if (err == -2) {
224  // FIXME: Throw an error here, taking care to disconnect the
225  // branch from the i/o buffer.
226  // FIXME: -2 means entry number too big.
227  }
228  // Note: Root will overwrite the old event
229  // auxiliary with the new one.
230  input::getEntry(auxBranch, entry);
231  // Note: We are intentionally making a copy here
232  // of the fetched event auxiliary!
233  auxseq.push_back(*pAux);
234  }
235  // Disconnect the branch from the i/o buffer.
236  auxBranch->SetAddress(nullptr);
237 }
std::array< cet::exempt_ptr< TTree >, art::BranchType::NumBranchTypes > currentDataTrees_
Definition: MixHelper.h:391
std::string const & BranchTypeToAuxiliaryBranchName(BranchType const bt)
Definition: BranchType.cc:83
Int_t getEntry(TBranch *branch, EntryNumber entryNumber)
Definition: getEntry.cc:12
bool art::MixHelper::generateEventSequence ( size_t  nSecondaries,
EntryNumberSequence enSeq,
EventIDSequence eIDseq 
)

Definition at line 143 of file MixHelper.cc.

References evd::details::begin(), coverageFraction_, dist_, evd::details::end(), eventIDIndex_, ffVersion_, art::FileFormatVersion::isValid(), art::errors::LogicError, nEventsInFile_, nEventsReadThisFile_, openNextFile_(), RANDOM_LIM_REPLACE, RANDOM_NO_REPLACE, RANDOM_REPLACE, readMode_, SEQUENTIAL, and shuffledSequence_.

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

146 {
147  assert(enSeq.empty());
148  assert(eIDseq.empty());
149  assert(nEventsInFile_ >= 0);
150  bool over_threshold =
152  ((nEventsReadThisFile_ + nSecondaries) >
153  static_cast<size_t>(nEventsInFile_)) :
154  ((nEventsReadThisFile_ + nSecondaries) >
156  if (over_threshold || (!ffVersion_.isValid())) {
157  if (openNextFile_()) {
158  return generateEventSequence(nSecondaries, enSeq, eIDseq);
159  } else {
160  return false;
161  }
162  }
163  switch (readMode_) {
164  case Mode::SEQUENTIAL:
165  enSeq.reserve(nSecondaries);
166  for (size_t i = nEventsReadThisFile_,
167  end = nEventsReadThisFile_ + nSecondaries;
168  i < end;
169  ++i) {
170  enSeq.push_back(i);
171  }
172  break;
174  std::generate_n(std::back_inserter(enSeq), nSecondaries, [this] {
175  return dist_.get()->fireInt(nEventsInFile_);
176  });
177  std::sort(enSeq.begin(), enSeq.end());
178  break;
180  std::unordered_set<EntryNumberSequence::value_type>
181  entries; // Guaranteed unique.
182  while (entries.size() < nSecondaries) {
183  std::generate_n(
184  std::inserter(entries, entries.begin()),
185  nSecondaries - entries.size(),
186  [this] { return dist_.get()->fireInt(nEventsInFile_); });
187  }
188  enSeq.assign(cbegin(entries), cend(entries));
189  std::sort(begin(enSeq), end(enSeq));
190  // Since we need to sort at the end anyway, it's unclear whether
191  // unordered_set is faster than set even though inserts are
192  // approximately linear time. Since the complexity of the sort is
193  // NlogN, we'd need a profile run for it all to come out in the
194  // wash.
195  assert(enSeq.size() == nSecondaries); // Should be true by construction.
196  } break;
198  auto i = cbegin(shuffledSequence_) + nEventsReadThisFile_;
199  enSeq.assign(i, i + nSecondaries);
200  } break;
201  default:
203  << "Unrecognized read mode " << static_cast<int>(readMode_)
204  << ". Contact the art developers.\n";
205  }
206  cet::transform_all(
207  enSeq, std::back_inserter(eIDseq), EventIDLookup{eventIDIndex_});
208  return true;
209 }
FileFormatVersion ffVersion_
Definition: MixHelper.h:377
EntryNumberSequence shuffledSequence_
Definition: MixHelper.h:383
std::unique_ptr< CLHEP::RandFlat > dist_
Definition: MixHelper.h:381
double const coverageFraction_
Definition: MixHelper.h:372
Mode const readMode_
Definition: MixHelper.h:371
Long64_t nEventsReadThisFile_
Definition: MixHelper.h:373
bool generateEventSequence(size_t nSecondaries, EntryNumberSequence &enSeq, EventIDSequence &eIDseq)
Definition: MixHelper.cc:143
bool openNextFile_()
Definition: MixHelper.cc:422
std::vector< evd::details::RawDigitInfo_t >::const_iterator begin(RawDigitCacheDataClass const &cache)
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
EventIDIndex eventIDIndex_
Definition: MixHelper.h:395
Long64_t nEventsInFile_
Definition: MixHelper.h:374
std::vector< evd::details::RawDigitInfo_t >::const_iterator end(RawDigitCacheDataClass const &cache)
auto art::MixHelper::initReadMode_ ( std::string const &  mode) const
private

Definition at line 316 of file MixHelper.cc.

References art::errors::Configuration.

Referenced by MixHelper().

317 {
318  // These regexes must correspond by index to the valid Mode enumerator
319  // values.
320  static std::regex const robjs[4]{
321  std::regex("^seq", std::regex_constants::icase),
322  std::regex("^random(replace)?$", std::regex_constants::icase),
323  std::regex("^randomlimreplace$", std::regex_constants::icase),
324  std::regex("^randomnoreplace$", std::regex_constants::icase)};
325  int i{0};
326  for (auto const& r : robjs) {
327  if (std::regex_search(mode, r)) {
328  return Mode(i);
329  } else {
330  ++i;
331  }
332  }
334  << "Unrecognized value of readMode parameter: \"" << mode
335  << "\". Valid values are:\n"
336  << " sequential,\n"
337  << " randomReplace (random is accepted for reasons of legacy),\n"
338  << " randomLimReplace,\n"
339  << " randomNoReplace.\n";
340 }
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 240 of file MixHelper.cc.

References branchIDLists_, art::FileIndex::cend(), currentFile_, currentFileIndex_, art::FileIndex::findPosition(), haveRunMixOps_, haveSubRunMixOps_, art::InEvent, art::InRun, art::InSubRun, art::errors::LogicError, mixOps_, nEventsReadThisFile_, art::errors::NotFound, art::ProdToProdMapBuilder::populateRemapper(), ptpBuilder_, ptrRemapper_, and totalEventsRead_.

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

243 {
244  // Populate the remapper in case we need to remap any Ptrs.
246  // Dummy remapper in case we need it.
247  static PtrRemapper const nopRemapper;
248  // Create required info only if we're likely to need it:
249  EntryNumberSequence subRunEntries;
250  EntryNumberSequence runEntries;
251  if (haveSubRunMixOps_) {
252  subRunEntries.reserve(eIDseq.size());
253  for (auto const& eID : eIDseq) {
254  auto const it = currentFileIndex_.findPosition(eID.subRunID(), true);
255  if (it != currentFileIndex_.cend()) {
256  subRunEntries.emplace_back(it->entry_);
257  } else {
258  throw Exception(errors::NotFound, "NO_SUBRUN")
259  << "- Unable to find an entry in the SubRun tree corresponding to "
260  "event ID "
261  << eID << " in secondary mixing input file "
262  << currentFile_->GetName() << ".\n";
263  }
264  }
265  }
266  if (haveRunMixOps_) {
267  runEntries.reserve(eIDseq.size());
268  for (auto const& eID : eIDseq) {
269  auto const it = currentFileIndex_.findPosition(eID.runID(), true);
270  if (it != currentFileIndex_.cend()) {
271  runEntries.emplace_back(it->entry_);
272  } else {
273  throw Exception(errors::NotFound, "NO_RUN")
274  << "- Unable to find an entry in the Run tree corresponding to event "
275  "ID "
276  << eID << " in secondary mixing input file "
277  << currentFile_->GetName() << ".\n";
278  }
279  }
280  }
281  // Do the branch-wise read, mix and put.
282  for (auto const& op : mixOps_) {
283  switch (op->branchType()) {
284  case InEvent:
285  op->readFromFile(eventEntries, branchIDLists_.get());
286  op->mixAndPut(e, ptrRemapper_);
287  break;
288  case InSubRun:
289  op->readFromFile(subRunEntries, nullptr);
290  // No Ptrs in subrun products.
291  op->mixAndPut(e, nopRemapper);
292  break;
293  case InRun:
294  op->readFromFile(runEntries, nullptr);
295  // No Ptrs in run products.
296  op->mixAndPut(e, nopRemapper);
297  break;
298  default:
299  throw Exception(errors::LogicError, "Unsupported BranchType")
300  << "- MixHelper::mixAndPut() attempted to handle unsupported branch "
301  "type "
302  << op->branchType() << ".\n";
303  }
304  }
305  nEventsReadThisFile_ += eventEntries.size();
306  totalEventsRead_ += eventEntries.size();
307 }
cet::value_ptr< TFile > currentFile_
Definition: MixHelper.h:388
MixOpList mixOps_
Definition: MixHelper.h:368
bool haveSubRunMixOps_
Definition: MixHelper.h:384
std::unique_ptr< art::BranchIDLists > branchIDLists_
Definition: MixHelper.h:378
Long64_t nEventsReadThisFile_
Definition: MixHelper.h:373
FileIndex currentFileIndex_
Definition: MixHelper.h:392
const_iterator cend() const
Definition: FileIndex.h:119
const_iterator findPosition(EventID const &eID) const
Definition: FileIndex.cc:123
std::vector< FileIndex::EntryNumber_t > EntryNumberSequence
Definition: MixTypes.h:23
PtrRemapper ptrRemapper_
Definition: MixHelper.h:369
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
bool haveRunMixOps_
Definition: MixHelper.h:385
ProdToProdMapBuilder ptpBuilder_
Definition: MixHelper.h:380
void populateRemapper(PtrRemapper &mapper, Event &e) const
Long64_t totalEventsRead_
Definition: MixHelper.h:375
void art::MixHelper::openAndReadMetaData_ ( std::string  fileName)
private

Definition at line 343 of file MixHelper.cc.

References branchIDLists_, buildProductIDTransMap_(), currentDataTrees_, currentFile_, currentFileIndex_, currentMetaDataTree_, dataBranches_, dist_, e, art::FileFormatVersion::era_, eventIDIndex_, ffVersion_, art::errors::FileOpenError, art::errors::FileReadError, art::getFileFormatEra(), art::InEvent, art::rootNames::metaDataTreeName(), n, nEventsInFile_, art::Open, art::ProdToProdMapBuilder::prepareTranslationTables(), ptpBuilder_, RANDOM_NO_REPLACE, art::detail::readFileIndex(), art::detail::readMetadata(), readMode_, and shuffledSequence_.

Referenced by openNextFile_().

344 {
345  // Open file.
346  try {
347  currentFile_.reset(TFile::Open(filename.c_str()));
348  }
349  catch (std::exception const& e) {
350  throw Exception(errors::FileOpenError, e.what())
351  << "Unable to open specified secondary event stream file " << filename
352  << ".\n";
353  }
354  if (!currentFile_ || currentFile_->IsZombie()) {
356  << "Unable to open specified secondary event stream file " << filename
357  << ".\n";
358  }
359  // Obtain meta data tree.
360  currentMetaDataTree_.reset(static_cast<TTree*>(
361  currentFile_->Get(rootNames::metaDataTreeName().c_str())));
362  if (currentMetaDataTree_.get() == nullptr) {
364  << "Unable to read meta data tree from secondary event stream file "
365  << filename << ".\n";
366  }
367  currentDataTrees_ = initDataTrees(currentFile_);
368  nEventsInFile_ = currentDataTrees_[InEvent]->GetEntries();
369 
370  ffVersion_ =
371  detail::readMetadata<FileFormatVersion>(currentMetaDataTree_.get());
372 
373  // Read file index
374  FileIndex* fileIndexPtr = &currentFileIndex_;
376  currentFile_.get(), currentMetaDataTree_.get(), fileIndexPtr);
377 
378  // To support files that contain BranchIDLists
379  BranchIDLists branchIDLists{};
380  if (detail::readMetadata(currentMetaDataTree_.get(), branchIDLists)) {
381  branchIDLists_ = std::make_unique<BranchIDLists>(std::move(branchIDLists));
382  configureProductIDStreamer(branchIDLists_.get());
383  }
384 
385  // Check file format era.
386  std::string const expected_era = getFileFormatEra();
387  if (ffVersion_.era_ != expected_era) {
389  << "Can only read files written during the \"" << expected_era
390  << "\" era: "
391  << "Era of "
392  << "\"" << filename << "\" was "
393  << (ffVersion_.era_.empty() ? "not set" :
394  ("set to \"" + ffVersion_.era_ + "\" "))
395  << ".\n";
396  }
397  auto dbCount = 0;
398  for (auto const tree : currentDataTrees_) {
399  if (tree.get()) {
400  dataBranches_[dbCount].reset(tree.get());
401  }
402  ++dbCount;
403  }
404 
405  eventIDIndex_ = buildEventIDIndex(currentFileIndex_);
407  buildProductIDTransMap_(transMap);
410  // Prepare shuffled event sequence.
411  shuffledSequence_.resize(static_cast<size_t>(nEventsInFile_));
412  std::iota(shuffledSequence_.begin(), shuffledSequence_.end(), 0);
413  std::random_shuffle(shuffledSequence_.begin(),
414  shuffledSequence_.end(),
415  [this](EntryNumberSequence::difference_type const& n) {
416  return dist_.get()->fireInt(n);
417  });
418  }
419 }
cet::value_ptr< TFile > currentFile_
Definition: MixHelper.h:388
void readFileIndex(TFile *file, TTree *metaDataTree, FileIndex *&findexPtr)
Definition: readFileIndex.h:20
std::array< cet::exempt_ptr< TTree >, art::BranchType::NumBranchTypes > currentDataTrees_
Definition: MixHelper.h:391
std::map< ProductID, ProductID > ProductIDTransMap
FileFormatVersion ffVersion_
Definition: MixHelper.h:377
EntryNumberSequence shuffledSequence_
Definition: MixHelper.h:383
std::unique_ptr< art::BranchIDLists > branchIDLists_
Definition: MixHelper.h:378
std::unique_ptr< CLHEP::RandFlat > dist_
Definition: MixHelper.h:381
Mode const readMode_
Definition: MixHelper.h:371
std::array< RootBranchInfoList, art::BranchType::NumBranchTypes > dataBranches_
Definition: MixHelper.h:393
std::string const & metaDataTreeName()
Definition: rootNames.cc:41
FileIndex currentFileIndex_
Definition: MixHelper.h:392
cet::exempt_ptr< TTree > currentMetaDataTree_
Definition: MixHelper.h:389
void prepareTranslationTables(ProductIDTransMap &transMap)
void buildProductIDTransMap_(ProdToProdMapBuilder::ProductIDTransMap &transMap)
Definition: MixHelper.cc:456
std::string const & getFileFormatEra()
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
EventIDIndex eventIDIndex_
Definition: MixHelper.h:395
Long64_t nEventsInFile_
Definition: MixHelper.h:374
Char_t n[5]
Float_t e
Definition: plot.C:34
ProdToProdMapBuilder ptpBuilder_
Definition: MixHelper.h:380
T readMetadata(TTree *md, bool const requireDict=true)
Definition: readMetadata.h:13
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
bool art::MixHelper::openNextFile_ ( )
private

Definition at line 422 of file MixHelper.cc.

References canWrapFiles_, eventsToSkip_, ffVersion_, fileIter_, filenames_, art::FileFormatVersion::isValid(), nEventsReadThisFile_, openAndReadMetaData_(), providerFunc_, readMode_, SEQUENTIAL, and totalEventsRead_.

Referenced by generateEventSequence().

423 {
424  std::string filename;
425  if (providerFunc_) {
426  filename = providerFunc_();
427  if (filename.empty()) {
428  return false;
429  }
430  } else if (filenames_.empty()) {
431  return false;
432  } else {
433  if (ffVersion_.isValid()) { // Already seen one file.
434  ++fileIter_;
435  }
436  if (fileIter_ == filenames_.end()) {
437  if (canWrapFiles_) {
438  mf::LogWarning("MixingInputWrap")
439  << "Wrapping around to initial input file for mixing after "
440  << totalEventsRead_ << " secondary events read.";
441  fileIter_ = filenames_.begin();
442  } else {
443  return false;
444  }
445  }
446  filename = *fileIter_;
447  }
449  eventsToSkip_() :
450  0; // Reset for this file.
451  openAndReadMetaData_(filename);
452  return true;
453 }
std::vector< std::string >::const_iterator fileIter_
Definition: MixHelper.h:370
std::function< size_t()> eventsToSkip_
Definition: MixHelper.h:382
ProviderFunc_ providerFunc_
Definition: MixHelper.h:367
FileFormatVersion ffVersion_
Definition: MixHelper.h:377
std::vector< std::string > const filenames_
Definition: MixHelper.h:365
Mode const readMode_
Definition: MixHelper.h:371
Long64_t nEventsReadThisFile_
Definition: MixHelper.h:373
bool const canWrapFiles_
Definition: MixHelper.h:376
void openAndReadMetaData_(std::string fileName)
Definition: MixHelper.cc:343
MaybeLogger_< ELseverityLevel::ELsev_warning, false > LogWarning
Long64_t totalEventsRead_
Definition: MixHelper.h:375
MixHelper& art::MixHelper::operator= ( MixHelper const &  )
privatedelete
template<class P >
void art::MixHelper::produces ( std::string const &  instanceName = {})
inline

Definition at line 407 of file MixHelper.h.

References art::ProductRegistryHelper::produces(), and producesProvider_.

408 {
409  producesProvider_.produces<P>(instanceName);
410 }
ProducerBase & producesProvider_
Definition: MixHelper.h:364
void produces(std::string const &instanceName={}, Persistable const persistable=Persistable::Yes)
template<class P , art::BranchType B>
void art::MixHelper::produces ( std::string const &  instanceName = {})
inline

Definition at line 415 of file MixHelper.h.

References B, art::ProductRegistryHelper::produces(), and producesProvider_.

416 {
417  producesProvider_.produces<P, B>(instanceName);
418 }
ProducerBase & producesProvider_
Definition: MixHelper.h:364
Int_t B
Definition: plot.C:25
void produces(std::string const &instanceName={}, Persistable const persistable=Persistable::Yes)
auto art::MixHelper::readMode ( ) const
inline

Definition at line 399 of file MixHelper.h.

References readMode_.

400 {
401  return readMode_;
402 }
Mode const readMode_
Definition: MixHelper.h:371
void art::MixHelper::registerSecondaryFileNameProvider ( ProviderFunc_  func)

Definition at line 132 of file MixHelper.cc.

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

133 {
134  if (!filenames_.empty()) {
136  << "Provision of a secondary file name provider is incompatible"
137  << " with a\nnon-empty fileNames parameter to the mix filter.\n";
138  }
139  providerFunc_ = func;
140 }
ProviderFunc_ providerFunc_
Definition: MixHelper.h:367
std::vector< std::string > const filenames_
Definition: MixHelper.h:365
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
void art::MixHelper::setEventsToSkipFunction ( std::function< size_t()>  eventsToSkip)

Definition at line 310 of file MixHelper.cc.

References eventsToSkip_.

Referenced by art::detail::setup_eventsToSkip< T >::setup_eventsToSkip().

311 {
312  eventsToSkip_ = eventsToSkip;
313 }
std::function< size_t()> eventsToSkip_
Definition: MixHelper.h:382

Member Data Documentation

std::unique_ptr<art::BranchIDLists> art::MixHelper::branchIDLists_
private
Initial value:
{
nullptr}

Definition at line 378 of file MixHelper.h.

Referenced by mixAndPut(), and openAndReadMetaData_().

bool const art::MixHelper::canWrapFiles_
private

Definition at line 376 of file MixHelper.h.

Referenced by MixHelper(), and openNextFile_().

bool art::MixHelper::compactMissingProducts_
private

Definition at line 366 of file MixHelper.h.

Referenced by declareMixOp(), and MixHelper().

double const art::MixHelper::coverageFraction_
private

Definition at line 372 of file MixHelper.h.

Referenced by generateEventSequence(), and MixHelper().

std::array<cet::exempt_ptr<TTree>, art::BranchType::NumBranchTypes> art::MixHelper::currentDataTrees_ {{nullptr}}
private

Definition at line 391 of file MixHelper.h.

Referenced by generateEventAuxiliarySequence(), and openAndReadMetaData_().

cet::value_ptr<TFile> art::MixHelper::currentFile_ {}
private

Definition at line 388 of file MixHelper.h.

Referenced by mixAndPut(), and openAndReadMetaData_().

FileIndex art::MixHelper::currentFileIndex_ {}
private

Definition at line 392 of file MixHelper.h.

Referenced by mixAndPut(), and openAndReadMetaData_().

cet::exempt_ptr<TTree> art::MixHelper::currentMetaDataTree_ {nullptr}
private

Definition at line 389 of file MixHelper.h.

Referenced by openAndReadMetaData_().

std::array<RootBranchInfoList, art::BranchType::NumBranchTypes> art::MixHelper::dataBranches_
private
Initial value:
{
{}}

Definition at line 393 of file MixHelper.h.

Referenced by buildProductIDTransMap_(), and openAndReadMetaData_().

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

Definition at line 381 of file MixHelper.h.

Referenced by generateEventSequence(), MixHelper(), and openAndReadMetaData_().

EventIDIndex art::MixHelper::eventIDIndex_ {}
private

Definition at line 395 of file MixHelper.h.

Referenced by generateEventSequence(), and openAndReadMetaData_().

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

Definition at line 382 of file MixHelper.h.

Referenced by openNextFile_(), and setEventsToSkipFunction().

FileFormatVersion art::MixHelper::ffVersion_ {}
private

Definition at line 377 of file MixHelper.h.

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

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

Definition at line 370 of file MixHelper.h.

Referenced by MixHelper(), and openNextFile_().

std::vector<std::string> const art::MixHelper::filenames_
private

Definition at line 365 of file MixHelper.h.

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

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

Definition at line 385 of file MixHelper.h.

Referenced by declareMixOp(), and mixAndPut().

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

Definition at line 384 of file MixHelper.h.

Referenced by declareMixOp(), and mixAndPut().

MixOpList art::MixHelper::mixOps_ {}
private

Definition at line 368 of file MixHelper.h.

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

Long64_t art::MixHelper::nEventsInFile_ {}
private

Definition at line 374 of file MixHelper.h.

Referenced by generateEventSequence(), and openAndReadMetaData_().

Long64_t art::MixHelper::nEventsReadThisFile_ {}
private

Definition at line 373 of file MixHelper.h.

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

ProducerBase& art::MixHelper::producesProvider_
private

Definition at line 364 of file MixHelper.h.

Referenced by produces().

ProviderFunc_ art::MixHelper::providerFunc_ {}
private

Definition at line 367 of file MixHelper.h.

Referenced by openNextFile_(), and registerSecondaryFileNameProvider().

ProdToProdMapBuilder art::MixHelper::ptpBuilder_ {}
private

Definition at line 380 of file MixHelper.h.

Referenced by mixAndPut(), and openAndReadMetaData_().

PtrRemapper art::MixHelper::ptrRemapper_ {}
private

Definition at line 369 of file MixHelper.h.

Referenced by mixAndPut().

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

Definition at line 383 of file MixHelper.h.

Referenced by generateEventSequence(), and openAndReadMetaData_().

Long64_t art::MixHelper::totalEventsRead_ {}
private

Definition at line 375 of file MixHelper.h.

Referenced by mixAndPut(), and openNextFile_().


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