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

Keeps a record of all registered events and their source. More...

#include <EventRegistry.h>

Classes

struct  EventRecord_t
 Element of the registry for an event. More...
 

Public Types

using EventID_t = art::EventID
 Type used to identify an event. More...
 
using FileID_t = std::size_t
 Type used to identify a source file. More...
 
using EventIDandRecord_t = std::pair< EventID_t, EventRecord_t >
 Type with event ID (first) and event record information (second). More...
 

Public Member Functions

Source interface
FileID_t recordSource (std::string const &fileName)
 Registers a source file and returns its ID in the registry. More...
 
bool hasSource (FileID_t const &fileID) const
 Returns whether the specified file ID is registered as a source. More...
 
bool hasSource (std::string const &fileName) const
 Returns whether the specified file name is registered as a source. More...
 
std::optional< FileID_tsourceID (std::string const &fileName) const
 
std::optional< std::string > sourceName (FileID_t const &fileID) const
 
std::string sourceNameOr (FileID_t const &fileID, std::string const &defName) const
 
Event interface
std::vector< EventIDandRecord_trecords () const
 Returns a copy of all event records. More...
 
std::optional< EventRecord_teventRecord (art::EventID const &event) const
 
EventRecord_t recordEvent (EventID_t const &event, FileID_t sourceFileID)
 Registers an event and returns a copy of its record. More...
 

Static Public Attributes

static constexpr FileID_t NoFileID = std::numeric_limits<FileID_t>::max()
 Mnemonic for no file ID. More...
 

Private Types

using FileRegistry_t = std::vector< std::string >
 Type for source file registry. More...
 

Private Member Functions

void copyEventRecordsInto (std::vector< EventIDandRecord_t > &recordCopy) const
 Copies all event records into recordCopy. More...
 
std::lock_guard< std::mutex > lockEventRegistry () const
 Returns a lock guard around fEventRegistry. More...
 
FileRegistry_t::iterator findSource (std::string const &fileName)
 Returns an iterator pointing to the specified file registry entry. More...
 
FileRegistry_t::const_iterator findSource (std::string const &fileName) const
 

Static Private Member Functions

static FileID_t indexToFileID (std::size_t index)
 Converts an internal index in file source registry into a FileID_t. More...
 
static std::size_t fileIDtoIndex (FileID_t fileID)
 Converts a FileID_t into an internal index in file source registry. More...
 

Private Attributes

std::vector< std::string > fSourceFiles
 Registered source file, by file ID key. More...
 
std::unordered_map< EventID_t,
EventRecord_t
fEventRegistry
 Registry of all events. More...
 
std::mutex fEventRegistryLock
 Lock for fEventRegistry. More...
 

Detailed Description

Keeps a record of all registered events and their source.

This registry object will keep track of every time an event is "registered". An event is represented by its ID (art::EventID), and it is associated to the input file(s) it is stored in.

Input files can be registered separately by file name, or at the same time with the event.

Thread safety

Registration of events can happen concurrently (thread-safe). Registration of source files, instead, is not protected.

It is guaranteed that the source file records are never modified once registered (i.e. neither removed from the registry, nor the path of a source record changed).

Definition at line 77 of file EventRegistry.h.

Member Typedef Documentation

using sbn::EventRegistry::EventID_t = art::EventID

Type used to identify an event.

Definition at line 81 of file EventRegistry.h.

Type with event ID (first) and event record information (second).

Definition at line 93 of file EventRegistry.h.

using sbn::EventRegistry::FileID_t = std::size_t

Type used to identify a source file.

Definition at line 83 of file EventRegistry.h.

using sbn::EventRegistry::FileRegistry_t = std::vector<std::string>
private

Type for source file registry.

Definition at line 174 of file EventRegistry.h.

Member Function Documentation

void sbn::EventRegistry::copyEventRecordsInto ( std::vector< EventIDandRecord_t > &  recordCopy) const
private

Copies all event records into recordCopy.

Definition at line 122 of file EventRegistry.cxx.

123 {
124  recordCopy.reserve(fEventRegistry.size()); // bet size does not change
125  auto const lg = lockEventRegistry();
126  // BEGIN needs lock
127  recordCopy.reserve(fEventRegistry.size()); // has size() changed already?
128  std::copy(fEventRegistry.cbegin(), fEventRegistry.cend(),
129  std::back_inserter(recordCopy));
130  // END needs lock
131 } // sbn::EventRegistry::records()
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.
T copy(T const &v)
auto sbn::EventRegistry::eventRecord ( art::EventID const &  event) const

Returns a copy of the specified event record.

Returns
the record for event, or unset if event is not registered

Definition at line 82 of file EventRegistry.cxx.

84 {
85 
86  auto const lg = lockEventRegistry();
87  // BEGIN needs lock
88  auto const iRecord = fEventRegistry.find(event);
89  return (iRecord != fEventRegistry.end())
90  ? std::optional{ iRecord->second }: std::nullopt;
91  // END needs lock
92 
93 } // sbn::EventRegistry::eventRecord()
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::size_t sbn::EventRegistry::fileIDtoIndex ( FileID_t  fileID)
staticprivate

Converts a FileID_t into an internal index in file source registry.

Definition at line 145 of file EventRegistry.cxx.

146  { return static_cast<std::size_t>(fileID); }
auto sbn::EventRegistry::findSource ( std::string const &  fileName)
private

Returns an iterator pointing to the specified file registry entry.

Definition at line 111 of file EventRegistry.cxx.

113  { return std::find(fSourceFiles.begin(), fSourceFiles.end(), fileName); }
std::vector< std::string > fSourceFiles
Registered source file, by file ID key.
auto sbn::EventRegistry::findSource ( std::string const &  fileName) const
private

Definition at line 115 of file EventRegistry.cxx.

117  { return std::find(fSourceFiles.begin(), fSourceFiles.end(), fileName); }
std::vector< std::string > fSourceFiles
Registered source file, by file ID key.
bool sbn::EventRegistry::hasSource ( FileID_t const &  fileID) const

Returns whether the specified file ID is registered as a source.

Definition at line 35 of file EventRegistry.cxx.

36  { return fileIDtoIndex(fileID) < fSourceFiles.size(); }
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.
bool sbn::EventRegistry::hasSource ( std::string const &  fileName) const

Returns whether the specified file name is registered as a source.

Definition at line 40 of file EventRegistry.cxx.

41  { return findSource(fileName) != fSourceFiles.end(); }
FileRegistry_t::iterator findSource(std::string const &fileName)
Returns an iterator pointing to the specified file registry entry.
std::vector< std::string > fSourceFiles
Registered source file, by file ID key.
auto sbn::EventRegistry::indexToFileID ( std::size_t  index)
staticprivate

Converts an internal index in file source registry into a FileID_t.

Definition at line 140 of file EventRegistry.cxx.

141  { return static_cast<FileID_t>(index); }
std::size_t FileID_t
Type used to identify a source file.
Definition: EventRegistry.h:83
std::lock_guard< std::mutex > sbn::EventRegistry::lockEventRegistry ( ) const
private

Returns a lock guard around fEventRegistry.

Definition at line 135 of file EventRegistry.cxx.

136  { return std::lock_guard{ fEventRegistryLock }; }
std::mutex fEventRegistryLock
Lock for fEventRegistry.
auto sbn::EventRegistry::recordEvent ( EventID_t const &  event,
FileID_t  sourceFileID 
)

Registers an event and returns a copy of its record.

Parameters
eventID of the event to be registered
sourceFileIDID of the source file to be associated with the event
Returns
the current copy, just updated, of the record of the event
Exceptions
cet::exception(category: "sbn::EventRegistry") if sourceFileID dose not match a registered source file
See Also
recordEvent(EventID_t const&, std::string const&)

The record of the specified event is updated adding sourceFileID among its sources. A single sourceFileID can appear multiple times for the same event, indicating a duplicate event in the same source file.

A source with sourceFileID must have been registered already (recordSource()).

Definition at line 98 of file EventRegistry.cxx.

99 {
100  auto const lg = lockEventRegistry();
101  // BEGIN needs lock
102  auto& record = fEventRegistry[event];
103  record.sourceFiles.push_back(sourceFileID);
104  return record;
105  // END needs lock
106 
107 } // sbn::EventRegistry::recordEvent()
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.
auto sbn::EventRegistry::records ( ) const

Returns a copy of all event records.

Definition at line 72 of file EventRegistry.cxx.

72  {
73 
74  std::vector<EventIDandRecord_t> recordCopy;
75  copyEventRecordsInto(recordCopy);
76  return recordCopy;
77 
78 } // sbn::EventRegistry::records()
void copyEventRecordsInto(std::vector< EventIDandRecord_t > &recordCopy) const
Copies all event records into recordCopy.
auto sbn::EventRegistry::recordSource ( std::string const &  fileName)

Registers a source file and returns its ID in the registry.

Parameters
fileNamethe name of the source file to be registered
Returns
the internal ID this registry will refer to fileName with

If fileName has already been registered, the existing ID is returned and no other action is performed.

Definition at line 23 of file EventRegistry.cxx.

23  {
24 
25  auto const fileID = sourceID(fileName);
26  if (fileID) return fileID.value();
27 
28  fSourceFiles.push_back(fileName);
29  return indexToFileID(fSourceFiles.size() - 1U);
30 
31 } // sbn::EventRegistry::recordSource()
std::vector< std::string > fSourceFiles
Registered source file, by file ID key.
static FileID_t indexToFileID(std::size_t index)
Converts an internal index in file source registry into a FileID_t.
std::optional< FileID_t > sourceID(std::string const &fileName) const
auto sbn::EventRegistry::sourceID ( std::string const &  fileName) const

Returns the ID of the source with the specified file name (slow!).

Returns
the ID of the source, or unset if fileID is not registered

Definition at line 45 of file EventRegistry.cxx.

47 {
48  auto const iSource = findSource(fileName);
49  return (iSource != fSourceFiles.end())
50  ? std::optional{indexToFileID(std::distance(fSourceFiles.begin(), iSource))}
51  : std::nullopt
52  ;
53 } // sbn::EventRegistry::sourceID()
FileRegistry_t::iterator findSource(std::string const &fileName)
Returns an iterator pointing to the specified file registry entry.
std::vector< std::string > fSourceFiles
Registered source file, by file ID key.
double distance(geo::Point_t const &point, CathodeDesc_t const &cathode)
Returns the distance of a point from the cathode.
static FileID_t indexToFileID(std::size_t index)
Converts an internal index in file source registry into a FileID_t.
std::optional< std::string > sbn::EventRegistry::sourceName ( FileID_t const &  fileID) const

Returns the name of the source associated to the specified file ID.

Returns
the name of the source, or unset if fileID is not registered

Definition at line 58 of file EventRegistry.cxx.

59 {
60  return hasSource(fileID)
61  ? std::optional{ fSourceFiles[fileIDtoIndex(fileID)] }: std::nullopt;
62 } // sbn::EventRegistry::sourceName()
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.
bool hasSource(FileID_t const &fileID) const
Returns whether the specified file ID is registered as a source.
std::string sbn::EventRegistry::sourceNameOr ( FileID_t const &  fileID,
std::string const &  defName 
) const

Returns the name of the source associated to the specified file ID.

Returns
the name of the source, or defName if fileID is not registered

Definition at line 67 of file EventRegistry.cxx.

68  { return sourceName(fileID).value_or(defName); }
std::optional< std::string > sourceName(FileID_t const &fileID) const

Member Data Documentation

std::unordered_map<EventID_t, EventRecord_t> sbn::EventRegistry::fEventRegistry
private

Registry of all events.

Definition at line 180 of file EventRegistry.h.

std::mutex sbn::EventRegistry::fEventRegistryLock
mutableprivate

Lock for fEventRegistry.

Definition at line 182 of file EventRegistry.h.

std::vector<std::string> sbn::EventRegistry::fSourceFiles
private

Registered source file, by file ID key.

Definition at line 177 of file EventRegistry.h.

constexpr FileID_t sbn::EventRegistry::NoFileID = std::numeric_limits<FileID_t>::max()
static

Mnemonic for no file ID.

Definition at line 97 of file EventRegistry.h.


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