All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DuplicateEventTracker.h
Go to the documentation of this file.
1 /**
2  * @file icaruscode/Utilities/DuplicateEventTracker.h
3  * @brief Service keeping track of _art_ event IDs.
4  * @author Gianluca Petrillo (petrillo@slac.stanford.edu)
5  * @date January 22, 2021
6  * @see icaruscode/Utilities/DuplicateEventTracker_service.cc
7  *
8  * Usage of the basic service functionality does not require this header:
9  * it suffices to configure the service for execution in _art_ configuration
10  * file.
11  */
12 
13 #ifndef ICARUSCODE_UTILITIES_DUPLICATEEVENTTRACKER_H
14 #define ICARUSCODE_UTILITIES_DUPLICATEEVENTTRACKER_H
15 
16 
17 // library header
19 
20 // framework libraries
21 #include "art/Framework/Services/Registry/ServiceDeclarationMacros.h"
22 #include "art/Framework/Services/Registry/ServiceTable.h"
23 #include "fhiclcpp/types/Atom.h"
24 #include "cetlib_except/exception.h" // courtesy
25 
26 // C/C++ standard libraries
27 #include <string>
28 #include <atomic>
29 
30 
31 // -----------------------------------------------------------------------------
32 // forward declarations
33 namespace art {
34  class ActivityRegistry;
35  class ScheduleContext; // actually unused
36  class Event;
37 } // namespace art
38 
39 // -----------------------------------------------------------------------------
40 namespace sbn { class DuplicateEventTracker; }
41 /**
42  * @brief Keeps track of duplicate events in the job.
43  *
44  * This _art_ service maintains a record of all the processed events, and it
45  * takes action when one is processed more than once.
46  * The main purpose of the service is to detect the duplication as soon as
47  * possible and in such case to interrupt the job with an error, so that the
48  * input can be amended.
49  *
50  * The default action is to immediately throw an exception.
51  * Alternatively, a warning message can be emitted each time an event is
52  * reprocessed, and a summary of all duplication can be emitted at the end of
53  * the job.
54  *
55  * The service also offers an interface, to receive the current list of events
56  * that have been processed so far.
57  *
58  * See `sbn::EventRegistry` for a description of the (large) memory usage of
59  * this service.
60  *
61  *
62  * Configuration parameters
63  * =========================
64  *
65  * * **WarningOnly** (flag, default: `false`): this flag has effect on warning
66  * messages and on exception throwing:
67  * * warning messages: if this flag is set to `true`, the service will emit
68  * a warning on each duplicate event encountered, otherwise it will not
69  * emit any warning (summary will still describe the duplicates if not
70  * skipped);
71  * * exception in case of duplicate events: if set to `false`, an exception
72  * will be thrown at the first duplicate event, unless `ExceptionAtEnd` is
73  * set, in which case the exception will be thrown at the end; if the flag
74  * is set to `true`, an exception is thrown at the end if `ExceptionAtEnd`
75  * is set, otherwise no exception is thrown at all
76  * * **SkipSummary** (flag, default: `false`): when set to `true`, it disables
77  * the printout of the duplicate events at the end of the job; unless
78  * `WarningOnly` is also set, this summary is going to be one-entry only
79  * * **ExceptionAtEnd** (flag, default: `false`): if duplicate events are
80  * detected, an exception is thrown _at the end of the job_ instead of
81  * at the first duplicate event; this allows to assess all the duplicate
82  * events at once; if set, it is recommended that `SkipSummary` be disabled;
83  * note that with this option enabled, if duplicate events are detected an
84  * exception is _always_ thrown (at the end of the job), regardless the
85  * setting of `WarningOnly`
86  * end of the job)
87  * * **LogCategory** (text, default: `"DuplicateEventTracker"`): tag of the
88  * output category used by this service to the message facility
89  *
90  *
91  * Multi-threading notes
92  * ======================
93  *
94  * This service is designed to work with the concurrent processing of events
95  * _from the same file_. Reading multiple files is not supported (but _art_ 3
96  * doesn't do that anyway).
97  *
98  */
100 
101  public:
102 
103  /// Service configuration.
104  struct Config {
105 
106  using Name = fhicl::Name;
107  using Comment = fhicl::Comment;
108 
109 
110  fhicl::Atom<bool> WarningOnly {
111  Name("WarningOnly"),
112  Comment("just emit a warning on screen instead of throwing an exception"),
113  false // default
114  };
115 
116  fhicl::Atom<bool> SkipSummary {
117  Name("SkipSummary"),
118  Comment("do not print a summary of the duplicate events at end of job"),
119  false // default
120  };
121 
122  fhicl::Atom<bool> ExceptionAtEnd {
123  Name("ExceptionAtEnd"),
124  Comment
125  ("in case of duplicate, wait until the end of job to throw an exception"),
126  false // default
127  };
128 
129  fhicl::Atom<std::string> LogCategory {
130  Name("LogCategory"),
131  Comment
132  ("tag of output category used by this service to the message facility"),
133  "DuplicateEventTracker" // default
134  };
135 
136 
137  }; // Config
138 
139  using Parameters = art::ServiceTable<Config>;
140 
141  /// Constructor: reads the configuration.
142  DuplicateEventTracker(Parameters const& config, art::ActivityRegistry& reg);
143 
144 
145  /// Prints a summary of the current duplicates.
146  void printSummary() const;
147 
148 
149  protected:
150 
151  // --- BEGIN -- Configuration variables --------------------------------------
152 
153  bool const fWarningOnly; ///< Only warning, no exception.
154  bool const fSkipSummary; ///< Do not print the summary of duplicate events.
155  bool const fExceptionAtEnd; ///< Throw exception as late as possible.
156 
157  std::string const fLogCategory; ///< Message service category tag.
158 
159  // --- END -- Configuration variables ----------------------------------------
160 
161  /// ID in the event registry of the current input file.
164 
165  sbn::EventRegistry fEventRegistry; ///< Record of all events and their source.
166 
167  std::atomic<unsigned int> fNDuplicateEvents{ 0U }; ///< Duplicate event count.
168 
169 
170  // --- BEGIN -- Callback functions -------------------------------------------
171  /// @name Callback functions
172  /// @{
173 
174  /// Records the event and throws an exception depending on the configuration.
175  void postEventReading(art::Event const& event, art::ScheduleContext);
176 
177  /// Records the current file name.
178  void postOpenFile(std::string const& fileName);
179 
180  /// Prints the summary and throws an exception depending on the configuration.
181  void postEndJob();
182 
183  /// @}
184  // --- END -- Callback functions ---------------------------------------------
185 
186 
187 }; // sbn::DuplicateEventTracker
188 
189 
190 // -----------------------------------------------------------------------------
191 DECLARE_ART_SERVICE(sbn::DuplicateEventTracker, SHARED)
192 
193 
194 // -----------------------------------------------------------------------------
195 
196 #endif // ICARUSCODE_UTILITIES_DUPLICATEEVENTTRACKER_H
void postEndJob()
Prints the summary and throws an exception depending on the configuration.
std::string const fLogCategory
Message service category tag.
fhicl::Atom< std::string > LogCategory
std::atomic< unsigned int > fNDuplicateEvents
Duplicate event count.
static constexpr FileID_t NoFileID
Mnemonic for no file ID.
Definition: EventRegistry.h:97
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
Class keeping track of art event IDs.
DuplicateEventTracker(Parameters const &config, art::ActivityRegistry &reg)
Constructor: reads the configuration.
bool const fSkipSummary
Do not print the summary of duplicate events.
bool const fWarningOnly
Only warning, no exception.
BEGIN_PROLOG vertical distance to the surface Name
sbn::EventRegistry fEventRegistry
Record of all events and their source.
bool const fExceptionAtEnd
Throw exception as late as possible.
std::size_t FileID_t
Type used to identify a source file.
Definition: EventRegistry.h:83
Keeps a record of all registered events and their source.
Definition: EventRegistry.h:77
Keeps track of duplicate events in the job.
sbn::EventRegistry::FileID_t fCurrentInputFileID
ID in the event registry of the current input file.
void postOpenFile(std::string const &fileName)
Records the current file name.