14 #include "art/Framework/Services/Registry/ActivityRegistry.h"
15 #include "art/Framework/Principal/Event.h"
16 #include "art/Framework/Services/Registry/ServiceDefinitionMacros.h"
17 #include "canvas/Persistency/Provenance/EventID.h"
18 #include "messagefacility/MessageLogger/MessageLogger.h"
29 : fWarningOnly(config().WarningOnly())
30 , fSkipSummary(config().SkipSummary())
31 , fExceptionAtEnd(config().ExceptionAtEnd())
35 reg.sPostOpenFile.watch(
this, &DuplicateEventTracker::postOpenFile);
37 reg.sPostSourceEvent.watch(
this, &DuplicateEventTracker::postEventReading);
39 reg.sPostEndJob.watch(
this, &DuplicateEventTracker::postEndJob);
48 log <<
"Summary of duplicate events encountered:";
50 std::set<sbn::EventRegistry::FileID_t> duplicateFileIDs;
51 unsigned int nDuplicateEvents = 0U;
53 std::sort(eventRecords.begin(), eventRecords.end(),
54 [](
auto const&
A,
auto const& B){
return A.first < B.first; });
55 for (
auto const& [ eventID, record ]: eventRecords) {
56 if (record.sourceFiles.size() <= 1U)
continue;
59 log <<
"\n " << eventID <<
" from " << record.sourceFiles.size()
61 for (
auto const fileID: record.sourceFiles) {
62 log <<
" [" << fileID <<
"]";
63 duplicateFileIDs.insert(fileID);
67 if (!duplicateFileIDs.empty()) {
68 log <<
"\nDuplicate events from " << duplicateFileIDs.size() <<
" files:";
69 for (
auto const fileID: duplicateFileIDs) {
74 log <<
"\n [" << fileID <<
"]";
75 if (sourceName) log <<
" '" << *sourceName <<
"'";
80 if (nDuplicateEvents > 0U) {
81 log <<
"\nCounted " << nDuplicateEvents <<
" duplicate events from "
82 << duplicateFileIDs.size() <<
" source files.";
84 else log <<
"\nNo duplicate events found.";
94 fCurrentInputFileID = fEventRegistry.recordSource(fileName);
101 (art::Event
const& event, art::ScheduleContext)
105 = fEventRegistry.recordEvent(event.id(), fCurrentInputFileID);
113 <<
"WARNING: event " <<
event.id() <<
" encountered "
116 << fEventRegistry.sourceNameOr(eventInfo.
sourceFiles.back(),
"<?>")
118 <<
"\n first time from '"
119 << fEventRegistry.sourceNameOr(eventInfo.
sourceFiles.front(),
"<?>")
125 if (fExceptionAtEnd)
return;
129 <<
"Duplicate event " <<
event.id() <<
" encountered"
131 << fEventRegistry.sourceNameOr(eventInfo.
sourceFiles.front(),
"<?>") <<
"'"
133 << fEventRegistry.sourceNameOr(eventInfo.
sourceFiles.back(),
"<?>") <<
"'\n"
142 if (!fSkipSummary) printSummary();
144 if (fExceptionAtEnd && (fNDuplicateEvents > 0U)) {
146 <<
"Found " << fNDuplicateEvents <<
" duplicate events in the job.\n";
void postEndJob()
Prints the summary and throws an exception depending on the configuration.
std::string const fLogCategory
Message service category tag.
std::vector< FileID_t > sourceFiles
List of ID of source files.
Service keeping track of art event IDs.
void postEventReading(art::Event const &event, art::ScheduleContext)
Records the event and throws an exception depending on the configuration.
void printSummary() const
Prints a summary of the current duplicates.
art::ServiceTable< Config > Parameters
std::optional< std::string > sourceName(FileID_t const &fileID) const
DuplicateEventTracker(Parameters const &config, art::ActivityRegistry ®)
Constructor: reads the configuration.
std::vector< EventIDandRecord_t > records() const
Returns a copy of all event records.
sbn::EventRegistry fEventRegistry
Record of all events and their source.
services TFileService fileName
Keeps track of duplicate events in the job.
Element of the registry for an event.
void postOpenFile(std::string const &fileName)
Records the current file name.