All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
EventRegistry.h
Go to the documentation of this file.
1 /**
2  * @file icaruscode/Utilities/EventRegistry.h
3  * @brief Class keeping track of _art_ event IDs.
4  * @author Gianluca Petrillo (petrillo@slac.stanford.edu)
5  * @date January 22, 2021
6  * @see icaruscode/Utilities/EventRegistry.cxx
7  *
8  *
9  *
10  *
11  */
12 
13 #ifndef ICARUSCODE_UTILITIES_EVENTREGISTRY_H
14 #define ICARUSCODE_UTILITIES_EVENTREGISTRY_H
15 
16 
17 // framework libraries
18 #include "canvas/Persistency/Provenance/EventID.h"
19 #include "cetlib_except/exception.h"
20 
21 // C/C++ standard libraries
22 #include <unordered_map>
23 #include <vector>
24 #include <mutex>
25 #include <utility> // std::pair<>
26 #include <string>
27 #include <functional> // std::hash<>
28 #include <optional>
29 #include <limits> // std::numeric_limits<>
30 #include <cstddef> // std::size_t
31 
32 
33 // -----------------------------------------------------------------------------
34 namespace std {
35 
36  template <>
37  struct hash<art::EventID> {
38 
39  std::size_t operator() (art::EventID const& ID) const noexcept
40  {
41  return std::hash<std::uint64_t>{}(
42  (std::uint64_t{ ID.run() } << 40U) // run: 24 bits (16M)
43  + (std::uint64_t{ ID.subRun() } << 22U) // subrun: 18 bits (256k)
44  + (std::uint64_t{ ID.event() }) // event: 22 bits (4M)
45  );
46  }
47 
48  }; // hash<art::EventID>
49 
50 } // namespace std
51 
52 
53 // -----------------------------------------------------------------------------
54 namespace sbn { class EventRegistry; }
55 /**
56  * @brief Keeps a record of all registered events and their source.
57  *
58  * This registry object will keep track of every time an event is "registered".
59  * An event is represented by its ID (`art::EventID`), and it is associated to
60  * the input file(s) it is stored in.
61  *
62  * Input files can be registered separately by file name, or at the same time
63  * with the event.
64  *
65  *
66  * Thread safety
67  * --------------
68  *
69  * Registration of events can happen concurrently (thread-safe).
70  * Registration of source files, instead, is not protected.
71  *
72  * It is guaranteed that the source file records are never modified once
73  * registered (i.e. neither removed from the registry, nor the path of a source
74  * record changed).
75  *
76  */
78 
79  public:
80 
81  using EventID_t = art::EventID; ///< Type used to identify an event.
82 
83  using FileID_t = std::size_t; ///< Type used to identify a source file.
84 
85  /// Element of the registry for an event.
86  struct EventRecord_t {
87 
88  std::vector<FileID_t> sourceFiles; ///< List of ID of source files.
89 
90  }; // EventRecord_t
91 
92  /// Type with event ID (`first`) and event record information (`second`).
93  using EventIDandRecord_t = std::pair<EventID_t, EventRecord_t>;
94 
95 
96  /// Mnemonic for no file ID.
97  static constexpr FileID_t NoFileID = std::numeric_limits<FileID_t>::max();
98 
99 
100  // -- BEGIN -- Source interface ----------------------------------------------
101  /// @name Source interface
102  /// @{
103 
104  /**
105  * @brief Registers a source file and returns its ID in the registry.
106  * @param fileName the name of the source file to be registered
107  * @return the internal ID this registry will refer to `fileName` with
108  *
109  * If `fileName` has already been registered, the existing ID is returned and
110  * no other action is performed.
111  */
112  FileID_t recordSource(std::string const& fileName);
113 
114  /// Returns whether the specified file ID is registered as a source.
115  bool hasSource(FileID_t const& fileID) const;
116 
117  /// Returns whether the specified file name is registered as a source.
118  bool hasSource(std::string const& fileName) const;
119 
120  /// Returns the ID of the source with the specified file name (slow!).
121  /// @return the ID of the source, or unset if `fileID` is not registered
122  std::optional<FileID_t> sourceID(std::string const& fileName) const;
123 
124  /// Returns the name of the source associated to the specified file ID.
125  /// @return the name of the source, or unset if `fileID` is not registered
126  std::optional<std::string> sourceName(FileID_t const& fileID) const;
127 
128  /// Returns the name of the source associated to the specified file ID.
129  /// @return the name of the source, or `defName` if `fileID` is not registered
130  std::string sourceNameOr
131  (FileID_t const& fileID, std::string const& defName) const;
132 
133  /// @}
134  // -- END -- Source interface ------------------------------------------------
135 
136 
137  // -- BEGIN -- Event interface -----------------------------------------------
138  /// @name Event interface
139  /// @{
140 
141  /// Returns a copy of all event records.
142  std::vector<EventIDandRecord_t> records() const;
143 
144  /// Returns a copy of the specified event record.
145  /// @return the record for `event`, or unset if `event` is not registered
146  std::optional<EventRecord_t> eventRecord(art::EventID const& event) const;
147 
148  /**
149  * @brief Registers an event and returns a copy of its record.
150  * @param event ID of the event to be registered
151  * @param sourceFileID ID of the source file to be associated with the `event`
152  * @return the current copy, just updated, of the record of the `event`
153  * @throw cet::exception (category: `"sbn::EventRegistry"`) if `sourceFileID`
154  * dose not match a registered source file
155  * @see `recordEvent(EventID_t const&, std::string const&)`
156  *
157  * The record of the specified `event` is updated adding `sourceFileID` among
158  * its sources. A single `sourceFileID` can appear multiple times for the same
159  * event, indicating a duplicate event in the same source file.
160  *
161  * A source with `sourceFileID` must have been registered already
162  * (`recordSource()`).
163  *
164  */
165  EventRecord_t recordEvent(EventID_t const& event, FileID_t sourceFileID);
166 
167  /// @}
168  // -- END -- Event interface -------------------------------------------------
169 
170 
171  private:
172 
173  /// Type for source file registry.
174  using FileRegistry_t = std::vector<std::string>;
175 
176  /// Registered source file, by file ID key.
177  std::vector<std::string> fSourceFiles;
178 
179  /// Registry of all events.
180  std::unordered_map<EventID_t, EventRecord_t> fEventRegistry;
181 
182  mutable std::mutex fEventRegistryLock; ///< Lock for `fEventRegistry`.
183 
184  //@{
185  /// Returns an iterator pointing to the specified file registry entry.
186  FileRegistry_t::iterator findSource(std::string const& fileName);
187  FileRegistry_t::const_iterator findSource(std::string const& fileName) const;
188  //@}
189 
190  /// Copies all event records into `recordCopy`.
191  void copyEventRecordsInto(std::vector<EventIDandRecord_t>& recordCopy) const;
192 
193  /// Returns a lock guard around `fEventRegistry`.
194  std::lock_guard<std::mutex> lockEventRegistry() const;
195 
196 
197  /// Converts an internal index in file source registry into a `FileID_t`.
198  static FileID_t indexToFileID(std::size_t index);
199 
200  /// Converts a `FileID_t` into an internal index in file source registry.
201  static std::size_t fileIDtoIndex(FileID_t fileID);
202 
203 }; // sbn::EventRegistry
204 
205 
206 // -----------------------------------------------------------------------------
207 
208 
209 #endif // ICARUSCODE_UTILITIES_EVENTREGISTRY_H
double std(const std::vector< short > &wf, const double ped_mean, size_t start, size_t nsample)
Definition: UtilFunc.cxx:42
std::vector< FileID_t > sourceFiles
List of ID of source files.
Definition: EventRegistry.h:88
std::optional< EventRecord_t > eventRecord(art::EventID const &event) const
FileRegistry_t::iterator findSource(std::string const &fileName)
Returns an iterator pointing to the specified file registry entry.
static constexpr FileID_t NoFileID
Mnemonic for no file ID.
Definition: EventRegistry.h:97
static std::size_t fileIDtoIndex(FileID_t fileID)
Converts a FileID_t into an internal index in file source registry.
std::vector< std::string > fSourceFiles
Registered source file, by file ID key.
std::unordered_map< EventID_t, EventRecord_t > fEventRegistry
Registry of all events.
std::lock_guard< std::mutex > lockEventRegistry() const
Returns a lock guard around fEventRegistry.
std::optional< std::string > sourceName(FileID_t const &fileID) const
EventRecord_t recordEvent(EventID_t const &event, FileID_t sourceFileID)
Registers an event and returns a copy of its record.
std::pair< EventID_t, EventRecord_t > EventIDandRecord_t
Type with event ID (first) and event record information (second).
Definition: EventRegistry.h:93
std::vector< EventIDandRecord_t > records() const
Returns a copy of all event records.
std::mutex fEventRegistryLock
Lock for fEventRegistry.
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
static FileID_t indexToFileID(std::size_t index)
Converts an internal index in file source registry into a FileID_t.
std::vector< std::string > FileRegistry_t
Type for source file registry.
bool hasSource(FileID_t const &fileID) const
Returns whether the specified file ID is registered as a source.
std::string sourceNameOr(FileID_t const &fileID, std::string const &defName) const
FileID_t recordSource(std::string const &fileName)
Registers a source file and returns its ID in the registry.
void copyEventRecordsInto(std::vector< EventIDandRecord_t > &recordCopy) const
Copies all event records into recordCopy.
art::EventID EventID_t
Type used to identify an event.
Definition: EventRegistry.h:81
Element of the registry for an event.
Definition: EventRegistry.h:86
std::optional< FileID_t > sourceID(std::string const &fileName) const