All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
EventRegistry.cxx
Go to the documentation of this file.
1 /**
2  * @file icaruscode/Utilities/EventRegistry.cxx
3  * @brief Class keeping track of _art_ event IDs (implementation file).
4  * @author Gianluca Petrillo (petrillo@slac.stanford.edu)
5  * @date January 22, 2021
6  * @see icaruscode/Utilities/EventRegistry.h
7  *
8  *
9  *
10  *
11  */
12 
13 // library header
15 
16 
17 // C/C++ standard libraries
18 #include <algorithm> // std::copy()
19 #include <iterator> // std::distance()
20 
21 
22 // -----------------------------------------------------------------------------
23 auto sbn::EventRegistry::recordSource(std::string const& fileName) -> FileID_t {
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()
32 
33 
34 // -----------------------------------------------------------------------------
35 bool sbn::EventRegistry::hasSource(FileID_t const& fileID) const
36  { return fileIDtoIndex(fileID) < fSourceFiles.size(); }
37 
38 
39 // -----------------------------------------------------------------------------
40 bool sbn::EventRegistry::hasSource(std::string const& fileName) const
41  { return findSource(fileName) != fSourceFiles.end(); }
42 
43 
44 // -----------------------------------------------------------------------------
45 auto sbn::EventRegistry::sourceID(std::string const& fileName) const
46  -> std::optional<FileID_t>
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()
54 
55 
56 // -----------------------------------------------------------------------------
57 std::optional<std::string> sbn::EventRegistry::sourceName
58  (FileID_t const& fileID) const
59 {
60  return hasSource(fileID)
61  ? std::optional{ fSourceFiles[fileIDtoIndex(fileID)] }: std::nullopt;
62 } // sbn::EventRegistry::sourceName()
63 
64 
65 // -----------------------------------------------------------------------------
67  (FileID_t const& fileID, std::string const& defName) const
68  { return sourceName(fileID).value_or(defName); }
69 
70 
71 // -----------------------------------------------------------------------------
73 
74  std::vector<EventIDandRecord_t> recordCopy;
75  copyEventRecordsInto(recordCopy);
76  return recordCopy;
77 
78 } // sbn::EventRegistry::records()
79 
80 
81 // -----------------------------------------------------------------------------
82 auto sbn::EventRegistry::eventRecord(art::EventID const& event) const
83  -> std::optional<EventRecord_t>
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()
94 
95 
96 // -----------------------------------------------------------------------------
98  (EventID_t const& event, FileID_t sourceFileID) -> EventRecord_t
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()
108 
109 
110 // -----------------------------------------------------------------------------
111 auto sbn::EventRegistry::findSource(std::string const& fileName)
112  -> FileRegistry_t::iterator
113  { return std::find(fSourceFiles.begin(), fSourceFiles.end(), fileName); }
114 
115 auto sbn::EventRegistry::findSource(std::string const& fileName) const
116  -> FileRegistry_t::const_iterator
117  { return std::find(fSourceFiles.begin(), fSourceFiles.end(), fileName); }
118 
119 
120 // -----------------------------------------------------------------------------
122  (std::vector<EventIDandRecord_t>& recordCopy) const
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()
132 
133 
134 // -----------------------------------------------------------------------------
135 std::lock_guard<std::mutex> sbn::EventRegistry::lockEventRegistry() const
136  { return std::lock_guard{ fEventRegistryLock }; }
137 
138 
139 // -----------------------------------------------------------------------------
140 auto sbn::EventRegistry::indexToFileID(std::size_t index) -> FileID_t
141  { return static_cast<FileID_t>(index); }
142 
143 
144 // -----------------------------------------------------------------------------
146  { return static_cast<std::size_t>(fileID); }
147 
148 
149 // -----------------------------------------------------------------------------
150 
double std(const std::vector< short > &wf, const double ped_mean, size_t start, size_t nsample)
Definition: UtilFunc.cxx:42
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 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::lock_guard< std::mutex > lockEventRegistry() const
Returns a lock guard around fEventRegistry.
std::optional< std::string > sourceName(FileID_t const &fileID) const
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:265
Class keeping track of art event IDs.
double distance(geo::Point_t const &point, CathodeDesc_t const &cathode)
Returns the distance of a point from the cathode.
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::size_t FileID_t
Type used to identify a source file.
Definition: EventRegistry.h:83
static FileID_t indexToFileID(std::size_t index)
Converts an internal index in file source registry into a FileID_t.
T copy(T const &v)
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