1 #ifndef art_Framework_IO_Sources_Source_h 2 #define art_Framework_IO_Sources_Source_h 103 #include "cetlib/exempt_ptr.h" 104 #include "cetlib/metaprogramming.h" 113 #include <type_traits> 117 template <
typename T>
120 template <
typename T>
139 template <
typename T,
typename =
void>
142 template <
typename T>
145 cet::enable_if_function_exists_t<bool (T::*)(), &T::hasMoreData>>
148 template <
typename T>
151 cet::enable_if_function_exists_t<bool (T::*)() const, &T::hasMoreData>>
154 template <
typename T>
159 return t.hasMoreData();
163 template <
typename T>
174 template <
typename T,
typename =
void>
179 template <
typename T>
184 struct SourceConfig {
199 template <
typename T>
204 template <
typename U = Parameters>
205 explicit Source(std::enable_if_t<std::is_same_v<U, fhicl::ParameterSet>,
209 template <
typename U = Parameters>
211 std::enable_if_t<!std::is_same_v<U, fhicl::ParameterSet>, U>
const& p,
222 std::unique_ptr<FileBlock> readFile()
override;
223 void closeFile()
override;
227 std::unique_ptr<RunPrincipal> readRun()
override;
228 std::unique_ptr<SubRunPrincipal> readSubRun(
229 cet::exempt_ptr<RunPrincipal const> rp)
override;
231 std::unique_ptr<EventPrincipal> readEvent(
232 cet::exempt_ptr<SubRunPrincipal const> srp)
override;
234 std::unique_ptr<RangeSetHandler> runRangeSetHandler()
override;
235 std::unique_ptr<RangeSetHandler> subRunRangeSetHandler()
override;
249 void checkForNextFile_();
252 void readNextAndRequireRun_();
255 void readNextAndRefuseEvent_();
258 void throwIfInsane_(
bool result,
265 [[noreturn]]
static void throwDataCorruption_(
const char* msg);
276 std::string currentFileName_{};
278 std::unique_ptr<RunPrincipal> newRP_{};
279 std::unique_ptr<SubRunPrincipal> newSRP_{};
280 std::unique_ptr<EventPrincipal> newE_{};
284 cet::exempt_ptr<RunPrincipal> cachedRP_{
nullptr};
285 cet::exempt_ptr<SubRunPrincipal> cachedSRP_{
nullptr};
287 bool pendingSubRun_{
false};
288 bool pendingEvent_{
false};
289 bool subRunIsNew_{
false};
291 bool haveSRLimit_{
false};
293 bool haveEventLimit_{
false};
296 template <
typename T>
297 template <
typename U>
305 ,
fh_{p.template get<std::vector<std::string>>(
"fileNames", {})}
307 int64_t
const maxSubRuns_par = p.template get<int64_t>(
"maxSubRuns", -1);
308 if (maxSubRuns_par > -1) {
312 int64_t
const maxEvents_par = p.template get<int64_t>(
"maxEvents", -1);
313 if (maxEvents_par > -1) {
320 template <
typename T>
321 template <
typename U>
323 std::enable_if_t<!std::is_same_v<U, fhicl::ParameterSet>, U>
const& p,
327 , sourceHelper_{
d.moduleDescription}
328 ,
detail_{p().userConfig,
h_, sourceHelper_}
329 ,
fh_{p().sourceConfig().fileNames()}
331 if (int64_t
const maxSubRuns_par = p().sourceConfig().maxSubRuns();
332 maxSubRuns_par > -1) {
336 if (int64_t
const maxEvents_par = p().sourceConfig().maxEvents();
337 maxEvents_par > -1) {
344 template <
typename T>
351 template <
typename T>
358 std::ostringstream errMsg;
360 if (!newR && !newSR && !newE) {
362 <<
"readNext returned true but created no new data\n";
366 <<
"readNext returned true but no RunPrincipal has been set, and no " 367 "cached RunPrincipal exists.\n" 368 "This can happen if a new input file has been opened and the " 369 "RunPrincipal has not been appropriately assigned.";
373 <<
"readNext returned true but no SubRunPrincipal has been set, and " 374 "no cached SubRunPrincipal exists.\n" 375 "This can happen if a new input file has been opened and the " 376 "SubRunPrincipal has not been appropriately assigned.";
379 errMsg <<
"readNext returned a new Run which is the old Run for " 381 <<
".\nIf you don't have a new run, don't return one!\n";
384 errMsg <<
"readNext returned a new SubRun which is the old SubRun for " 386 <<
".\nIf you don't have a new subRun, don't return one!\n";
390 if (!errMsg.str().empty())
395 "that was the same as the previous " 399 "that was the same as the previous " 401 if (newR && !newSR && newE)
403 "Event without a SubRun\n");
406 "a SubRun from the wrong Run\n");
415 "readNext returned a Run with an invalid RunID.\n");
422 if (rID != srID.
runID()) {
423 errMsg <<
"readNext returned a SubRun " << srID
424 <<
" which is a mismatch to " << rID <<
'\n';
429 "readNext returned a SubRun with an invalid SubRunID.\n");
437 errMsg <<
"readNext returned an Event " << eID
438 <<
" which is a mismatch to " << srID <<
'\n';
443 "readNext returned an Event with an invalid EventID.\n");
447 if (newR || newSR || newE)
449 <<
"readNext returned false but created new data\n";
453 template <
typename T>
457 std::unique_ptr<RunPrincipal> newR{
nullptr};
458 std::unique_ptr<SubRunPrincipal> newSR{
nullptr};
459 std::unique_ptr<EventPrincipal> newE{
nullptr};
481 newSR->setRunPrincipal(rp);
486 newE->setSubRunPrincipal(srp);
487 newE_ = std::move(newE);
500 template <
typename T>
509 generatorHasMoreData;
510 if (generatorHasMoreData(
detail_)) {
521 template <
typename T>
546 <<
newE_->eventID() <<
" that belongs to no SubRun\n";
586 template <
typename T>
598 <<
" where a Run is expected\n";
606 template <
typename T>
614 <<
"' has an Event where a Run or SubRun is expected\n";
621 template <
typename T>
622 std::unique_ptr<RangeSetHandler>
625 return std::make_unique<OpenRangeSetHandler>(
cachedRP_->run());
628 template <
typename T>
629 std::unique_ptr<RangeSetHandler>
632 return std::make_unique<OpenRangeSetHandler>(
cachedSRP_->run());
635 template <
typename T>
636 std::unique_ptr<FileBlock>
643 <<
"detail_::readFile() failed to return a valid FileBlock object\n";
645 return std::unique_ptr<FileBlock>(newF);
648 template <
typename T>
659 template <
typename T>
660 std::unique_ptr<RunPrincipal>
665 <<
"Error in Source<T>\n" 666 <<
"readRun() called when no RunPrincipal exists\n" 667 <<
"Please report this to the art developers\n";
672 template <
typename T>
673 std::unique_ptr<SubRunPrincipal>
678 <<
"Error in Source<T>\n" 679 <<
"readSubRun() called when no SubRunPrincipal exists\n" 680 <<
"Please report this to the art developers\n";
691 template <
typename T>
692 std::unique_ptr<EventPrincipal>
698 return std::move(
newE_);
701 template <
typename T>
static fhicl::Name plugin_type()
SubRunID const & subRunID() const
typename T::Parameters user_config_t
std::unique_ptr< EventPrincipal > readEvent(cet::exempt_ptr< SubRunPrincipal const > srp) override
EventNumber_t remainingEvents_
static void throwDataCorruption_(const char *msg)
std::vector< BranchDescription > ProductDescriptions
std::unique_ptr< RangeSetHandler > runRangeSetHandler() override
std::unique_ptr< RangeSetHandler > subRunRangeSetHandler() override
void readNextAndRequireRun_()
void closeFile() override
RunID const & runID() const
cet::exempt_ptr< SubRunPrincipal > cachedSRP_
void registerProducts(ProductDescriptions &productsToRegister, ModuleDescription const &md)
std::unique_ptr< SubRunPrincipal > readSubRun(cet::exempt_ptr< RunPrincipal const > rp) override
std::unique_ptr< SubRunPrincipal > newSRP_
input::ItemType nextItemType() override
SourceHelper sourceHelper_
EventID const & eventID() const
cet::exempt_ptr< RunPrincipal > cachedRP_
ProductTables presentProducts_
IDNumber_t< Level::SubRun > SubRunNumber_t
Source(std::enable_if_t< std::is_same_v< U, fhicl::ParameterSet >, fhicl::ParameterSet > const &p, InputSourceDescription &d)
void invoke(ProductTables const &)
std::unique_ptr< EventPrincipal > newE_
void throwIfInsane_(bool result, RunPrincipal *newR, SubRunPrincipal *newSR, EventPrincipal *newE) const
ProcessConfiguration const & processConfiguration() const
ParameterSetID id() const
static ProductTables invalid()
fhicl::TableFragment< T > fragment_
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
void finishProductRegistration_(InputSourceDescription &d)
std::unique_ptr< RunPrincipal > newRP_
std::unique_ptr< FileBlock > readFile() override
fhicl::TableFragment< SourceConfig > sourceConfig
detail::FileNamesHandler< Source_wantFileServices< T >::value > fh_
SubRunNumber_t remainingSubRuns_
IDNumber_t< Level::Event > EventNumber_t
UpdateOutputCallbacks & outputCallbacks_
auto const & operator()() const
void readNextAndRefuseEvent_()
std::unique_ptr< RunPrincipal > readRun() override
std::string currentFileName_
typename detail::maybe_has_Parameters< T >::Parameters Parameters
SubRunID subRunID() const