All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Classes | Public Types | Public Member Functions | Protected Attributes | List of all members
sbn::DuplicateEventTracker Class Reference

Keeps track of duplicate events in the job. More...

#include <DuplicateEventTracker.h>

Classes

struct  Config
 Service configuration. More...
 

Public Types

using Parameters = art::ServiceTable< Config >
 

Public Member Functions

 DuplicateEventTracker (Parameters const &config, art::ActivityRegistry &reg)
 Constructor: reads the configuration. More...
 
void printSummary () const
 Prints a summary of the current duplicates. More...
 

Protected Member Functions

Callback functions
void postEventReading (art::Event const &event, art::ScheduleContext)
 Records the event and throws an exception depending on the configuration. More...
 
void postOpenFile (std::string const &fileName)
 Records the current file name. More...
 
void postEndJob ()
 Prints the summary and throws an exception depending on the configuration. More...
 

Protected Attributes

bool const fWarningOnly
 Only warning, no exception. More...
 
bool const fSkipSummary
 Do not print the summary of duplicate events. More...
 
bool const fExceptionAtEnd
 Throw exception as late as possible. More...
 
std::string const fLogCategory
 Message service category tag. More...
 
sbn::EventRegistry::FileID_t fCurrentInputFileID = sbn::EventRegistry::NoFileID
 ID in the event registry of the current input file. More...
 
sbn::EventRegistry fEventRegistry
 Record of all events and their source. More...
 
std::atomic< unsigned int > fNDuplicateEvents { 0U }
 Duplicate event count. More...
 

Detailed Description

Keeps track of duplicate events in the job.

This art service maintains a record of all the processed events, and it takes action when one is processed more than once. The main purpose of the service is to detect the duplication as soon as possible and in such case to interrupt the job with an error, so that the input can be amended.

The default action is to immediately throw an exception. Alternatively, a warning message can be emitted each time an event is reprocessed, and a summary of all duplication can be emitted at the end of the job.

The service also offers an interface, to receive the current list of events that have been processed so far.

See sbn::EventRegistry for a description of the (large) memory usage of this service.

Configuration parameters

Multi-threading notes

This service is designed to work with the concurrent processing of events from the same file. Reading multiple files is not supported (but art 3 doesn't do that anyway).

Definition at line 99 of file DuplicateEventTracker.h.

Member Typedef Documentation

using sbn::DuplicateEventTracker::Parameters = art::ServiceTable<Config>

Definition at line 139 of file DuplicateEventTracker.h.

Constructor & Destructor Documentation

sbn::DuplicateEventTracker::DuplicateEventTracker ( Parameters const &  config,
art::ActivityRegistry &  reg 
)

Constructor: reads the configuration.

Definition at line 27 of file DuplicateEventTracker_service.cc.

29  : fWarningOnly(config().WarningOnly())
30  , fSkipSummary(config().SkipSummary())
31  , fExceptionAtEnd(config().ExceptionAtEnd())
32  , fLogCategory(config().LogCategory())
33 {
34 
35  reg.sPostOpenFile.watch(this, &DuplicateEventTracker::postOpenFile);
36 
37  reg.sPostSourceEvent.watch(this, &DuplicateEventTracker::postEventReading);
38 
39  reg.sPostEndJob.watch(this, &DuplicateEventTracker::postEndJob);
40 
41 } // sbn::DuplicateEventTracker::DuplicateEventTracker()
void postEndJob()
Prints the summary and throws an exception depending on the configuration.
std::string const fLogCategory
Message service category tag.
void postEventReading(art::Event const &event, art::ScheduleContext)
Records the event and throws an exception depending on the configuration.
bool const fSkipSummary
Do not print the summary of duplicate events.
bool const fWarningOnly
Only warning, no exception.
bool const fExceptionAtEnd
Throw exception as late as possible.
void postOpenFile(std::string const &fileName)
Records the current file name.

Member Function Documentation

void sbn::DuplicateEventTracker::postEndJob ( )
protected

Prints the summary and throws an exception depending on the configuration.

Definition at line 140 of file DuplicateEventTracker_service.cc.

140  {
141 
142  if (!fSkipSummary) printSummary();
143 
144  if (fExceptionAtEnd && (fNDuplicateEvents > 0U)) {
145  throw cet::exception(fLogCategory)
146  << "Found " << fNDuplicateEvents << " duplicate events in the job.\n";
147  }
148 
149 } // sbn::DuplicateEventTracker::postEndJob()
std::string const fLogCategory
Message service category tag.
std::atomic< unsigned int > fNDuplicateEvents
Duplicate event count.
void printSummary() const
Prints a summary of the current duplicates.
bool const fSkipSummary
Do not print the summary of duplicate events.
bool const fExceptionAtEnd
Throw exception as late as possible.
void sbn::DuplicateEventTracker::postEventReading ( art::Event const &  event,
art::ScheduleContext   
)
protected

Records the event and throws an exception depending on the configuration.

Definition at line 101 of file DuplicateEventTracker_service.cc.

102 {
103 
104  sbn::EventRegistry::EventRecord_t const eventInfo
106  assert(eventInfo.sourceFiles.size() > 0U);
107  if (eventInfo.sourceFiles.size() == 1U) return; // all done, no new duplicates
108 
110 
111  if (fWarningOnly) {
112  mf::LogWarning(fLogCategory)
113  << "WARNING: event " << event.id() << " encountered "
114  << eventInfo.sourceFiles.size() << " times,"
115  << "\n now from '"
116  << fEventRegistry.sourceNameOr(eventInfo.sourceFiles.back(), "<?>")
117  << "',"
118  << "\n first time from '"
119  << fEventRegistry.sourceNameOr(eventInfo.sourceFiles.front(), "<?>")
120  << "'"
121  ;
122  return;
123  }
124 
125  if (fExceptionAtEnd) return;
126 
127  assert(eventInfo.sourceFiles.size() == 2U);
128  throw cet::exception(fLogCategory)
129  << "Duplicate event " << event.id() << " encountered"
130  << "\n first in '"
131  << fEventRegistry.sourceNameOr(eventInfo.sourceFiles.front(), "<?>") << "'"
132  << "\n and now in '"
133  << fEventRegistry.sourceNameOr(eventInfo.sourceFiles.back(), "<?>") << "'\n"
134  ;
135 
136 } // sbn::DuplicateEventTracker::postEventReading()
std::string const fLogCategory
Message service category tag.
std::vector< FileID_t > sourceFiles
List of ID of source files.
Definition: EventRegistry.h:88
std::atomic< unsigned int > fNDuplicateEvents
Duplicate event count.
EventRecord_t recordEvent(EventID_t const &event, FileID_t sourceFileID)
Registers an event and returns a copy of its record.
bool const fWarningOnly
Only warning, no exception.
sbn::EventRegistry fEventRegistry
Record of all events and their source.
bool const fExceptionAtEnd
Throw exception as late as possible.
std::string sourceNameOr(FileID_t const &fileID, std::string const &defName) const
sbn::EventRegistry::FileID_t fCurrentInputFileID
ID in the event registry of the current input file.
Element of the registry for an event.
Definition: EventRegistry.h:86
void sbn::DuplicateEventTracker::postOpenFile ( std::string const &  fileName)
protected

Records the current file name.

Definition at line 90 of file DuplicateEventTracker_service.cc.

90  {
91 
92  // register the current input file; if it exists already,
93  // we just get its ID again (but that's troublesome anyway)
95 
96 } // sbn::DuplicateEventTracker::postOpenFile()
sbn::EventRegistry fEventRegistry
Record of all events and their source.
FileID_t recordSource(std::string const &fileName)
Registers a source file and returns its ID in the registry.
sbn::EventRegistry::FileID_t fCurrentInputFileID
ID in the event registry of the current input file.
void sbn::DuplicateEventTracker::printSummary ( ) const

Prints a summary of the current duplicates.

Definition at line 45 of file DuplicateEventTracker_service.cc.

45  {
46 
47  mf::LogInfo log(fLogCategory);
48  log << "Summary of duplicate events encountered:";
49 
50  std::set<sbn::EventRegistry::FileID_t> duplicateFileIDs;
51  unsigned int nDuplicateEvents = 0U;
52  auto eventRecords = fEventRegistry.records();
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;
57  ++nDuplicateEvents;
58 
59  log << "\n " << eventID << " from " << record.sourceFiles.size()
60  << " sources:";
61  for (auto const fileID: record.sourceFiles) {
62  log << " [" << fileID << "]";
63  duplicateFileIDs.insert(fileID);
64  }
65  } // for
66 
67  if (!duplicateFileIDs.empty()) {
68  log << "\nDuplicate events from " << duplicateFileIDs.size() << " files:";
69  for (auto const fileID: duplicateFileIDs) {
70  // a bit cheating multithreading: we assume that between the copy of the
71  // event records and the source file list no source has been moved or
72  // removed; with the current interface, this is assured
73  auto const sourceName = fEventRegistry.sourceName(fileID);
74  log << "\n [" << fileID << "]";
75  if (sourceName) log << " '" << *sourceName << "'";
76  else log << " N/A"; // should not happen though
77  } // for sources
78  } // if duplicates
79 
80  if (nDuplicateEvents > 0U) {
81  log << "\nCounted " << nDuplicateEvents << " duplicate events from "
82  << duplicateFileIDs.size() << " source files.";
83  }
84  else log << "\nNo duplicate events found.";
85 
86 } // sbn::DuplicateEventTracker::printSummary()
std::string const fLogCategory
Message service category tag.
std::optional< std::string > sourceName(FileID_t const &fileID) const
std::vector< EventIDandRecord_t > records() const
Returns a copy of all event records.
sbn::EventRegistry fEventRegistry
Record of all events and their source.
float A
Definition: dedx.py:137

Member Data Documentation

sbn::EventRegistry::FileID_t sbn::DuplicateEventTracker::fCurrentInputFileID = sbn::EventRegistry::NoFileID
protected

ID in the event registry of the current input file.

Definition at line 163 of file DuplicateEventTracker.h.

sbn::EventRegistry sbn::DuplicateEventTracker::fEventRegistry
protected

Record of all events and their source.

Definition at line 165 of file DuplicateEventTracker.h.

bool const sbn::DuplicateEventTracker::fExceptionAtEnd
protected

Throw exception as late as possible.

Definition at line 155 of file DuplicateEventTracker.h.

std::string const sbn::DuplicateEventTracker::fLogCategory
protected

Message service category tag.

Definition at line 157 of file DuplicateEventTracker.h.

std::atomic<unsigned int> sbn::DuplicateEventTracker::fNDuplicateEvents { 0U }
protected

Duplicate event count.

Definition at line 167 of file DuplicateEventTracker.h.

bool const sbn::DuplicateEventTracker::fSkipSummary
protected

Do not print the summary of duplicate events.

Definition at line 154 of file DuplicateEventTracker.h.

bool const sbn::DuplicateEventTracker::fWarningOnly
protected

Only warning, no exception.

Definition at line 153 of file DuplicateEventTracker.h.


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