ChangeTrackers.h
Go to the documentation of this file.
1 /**
2  * @file ChangeTrackers.h
3  * @brief Classes detecting configuration changes
4  * @author Gianluca Petrillo (petrillo@fnal.gov)
5  * @date September 23rd, 2015
6  */
7 
8 #ifndef UTIL_CHANGETRACKERS_H
9 #define UTIL_CHANGETRACKERS_H
10 
11 // LArSoft libraries
12 #include "larcoreobj/SimpleTypesAndConstants/geo_types.h" // geo::PlaneID
13 
14 // framework libraries
18 
19 // C/C++ standard libraries
20 #include <string> // std::to_string()
21 #include <ostream>
22 #include <type_traits> // std::enable_if_t
23 
24 
25 namespace util {
26 
27  /** **************************************************************************
28  * @brief Detects the presence of a new event
29  *
30  * The state of this class describes the current event by its ID.
31  */
32  class EventChangeTracker_t {
33  public:
34  /// Default constructor: no current event, next event is a new one
35  EventChangeTracker_t() = default;
36 
37  /// Constructor: current event as specified
39 
40  /// Constructor: current event as specified by the event ID
41  EventChangeTracker_t(art::EventID const& evt_id): state{evt_id} {}
42 
43  /// @name State query
44  /// @{
45  /// Returns whether this tracker is in the same state as another
46  bool same(EventChangeTracker_t const& as) const
47  { return as.eventID() == eventID(); }
48 
49  /// Returns whether there is a current event
50  bool isValid() const { return eventID() != art::EventID(); }
51 
52  /// Returns whether this tracker is in the same state as another
53  bool operator== (EventChangeTracker_t const& as) const { return same(as); }
54 
55  /// Returns whether this tracker is in a different state than another
56  bool operator!= (EventChangeTracker_t const& than) const
57  { return !same(than); }
58  /// @}
59 
60  /// @name State change
61  /// @{
62  /// Forgets the current event
63  void clear() { set(art::EventID()); }
64 
65  /// Sets the current event ID
66  void set(art::EventID const& evt_id) { state.event_id = evt_id; }
67 
68  /// Sets the current event ID from the given event
69  void set(art::Event const& evt) { set(evt.id()); }
70 
71  /// Sets the current event, and returns true if it is changed
73  {
74  if (same(trk)) return false;
75  *this = trk;
76  return true;
77  }
78  /// @}
79 
80  /// Returns a string representing the current state
81  operator std::string() const
82  {
83  return "R:" + std::to_string(eventID().run())
84  + " S:" + std::to_string(eventID().subRun())
85  + " E:" + std::to_string(eventID().event());
86  }
87 
88 
89  protected:
90  /// Returns the current event ID (it might be made public...)
91  art::EventID const& eventID() const { return state.event_id; }
92 
93 
94  private:
95  struct LocalState_t {
96  art::EventID event_id; ///< ID of the current event
97  };
98 
99  LocalState_t state; ///< local state of the tracker (may inherit some more)
100 
101  }; // EventChangeTracker_t
102 
103 
104  inline std::ostream& operator<<
105  (std::ostream& out, EventChangeTracker_t const& trk)
106  { out << std::string(trk); return out; }
107 
108 
109 
110  /** **************************************************************************
111  * @brief Detects the presence of a new event or data product
112  *
113  * The state of this class describes the current data product input as the
114  * event it belongs to (by its ID) and the input tag.
115  */
117  public:
118 
119  /// Default constructor: no current data product
120  DataProductChangeTracker_t() = default;
121 
122  /// Constructor: specifies current event and data product label
124  (art::Event const& evt, art::InputTag const& label):
125  EventChangeTracker_t(evt), state{label}
126  {}
127 
128  /// Constructor: specifies current event ID and data product label
130  (art::EventID const& evt_id, art::InputTag const& label):
131  EventChangeTracker_t(evt_id), state{label}
132  {}
133 
134 
135  /// @name State query
136  /// @{
137  /// Returns the current input label
138  art::InputTag const& inputLabel() const { return state.input_label; }
139 
140  /// Returns whether we are in the same event (the rest could differ)
142  { return EventChangeTracker_t::same(as); }
143 
144  /// Returns whether we have same data product as in "as"
145  bool same(DataProductChangeTracker_t const& as) const
146  { return sameEvent(as) && (inputLabel() == as.inputLabel()); }
147 
148  /// Returns whether there is a current event and data product
149  bool isValid() const
150  {
151  return
152  EventChangeTracker_t::isValid() && !inputLabel().label().empty();
153  }
154 
155  /// Returns whether the event and input label are the same as in "as"
157  { return same(as); }
158 
159  /// Returns whether the event or input label are different than in "than"
160  bool operator!= (DataProductChangeTracker_t const& than) const
161  { return !same(than); }
162  /// @}
163 
164 
165  /// @name State change
166  /// @{
167  /// Forget the current data product
168  void clear()
169  { EventChangeTracker_t::clear(); SetInputLabel(art::InputTag()); }
170 
171  /// Set a new event and data product label as current
172  void set(art::Event const& evt, art::InputTag const& label)
173  { EventChangeTracker_t::set(evt); SetInputLabel(label); }
174 
175  /// Update to a new data product, return true if it has changed
176  bool update(DataProductChangeTracker_t const& new_prod)
177  {
178  if (same(new_prod)) return false;
179  *this = new_prod;
180  return true;
181  }
182 
183  /// @}
184 
185  /// Returns a string representation of event and data product label
186  operator std::string() const
187  {
188  return EventChangeTracker_t::operator std::string()
189  + " I{" + inputLabel().encode() + "}";
190  }
191 
192  private:
193  struct LocalState_t {
194  art::InputTag input_label;
195  }; // LocalState_t
196 
198 
199  void SetInputLabel(art::InputTag const& label)
200  { state.input_label = label; }
201 
202  }; // DataProductChangeTracker_t
203 
204 
205  inline std::ostream& operator<<
206  (std::ostream& out, DataProductChangeTracker_t const& trk)
207  { out << std::string(trk); return out; }
208 
209 
210 
211  /** **************************************************************************
212  * @brief Detects the presence of a new event, data product or wire plane
213  *
214  * The state of this class describes the current data product input as the
215  * event it belongs to (by its ID) and the input tag, and the current wire
216  * plane in the TPC.
217  */
219  public:
220 
221  /// Default constructor: no current plane data
222  PlaneDataChangeTracker_t() = default;
223 
224  /// Constructor: specifies current data product and TPC plane
226  art::Event const& evt, art::InputTag const& label, geo::PlaneID const& pid
227  ):
228  DataProductChangeTracker_t(evt, label), state{pid}
229  {}
230 
231  /// Constructor: specifies current data product and TPC plane
233  art::EventID const& evt_id, art::InputTag const& label,
234  geo::PlaneID const& pid
235  ):
236  DataProductChangeTracker_t(evt_id, label), state{pid}
237  {}
238 
239 
240  /// @name State query
241  /// @{
242  /// Returns the current input label
244 
245  /// Returns the current plane ID
246  geo::PlaneID const& planeID() const { return state.plane_id; }
247 
248  /// Returns whether we are in the same event (the rest could differ)
250  { return DataProductChangeTracker_t::same(as); }
251 
252  /// Returns whether we have the same data product and TPC as "as"
254  {
255  return sameEvent(as)
256  && (static_cast<geo::TPCID const&>(planeID()) == as.planeID());
257  }
258 
259  /// Returns whether we have the same plane data as "as"
260  bool same(PlaneDataChangeTracker_t const& as) const
261  { return sameEvent(as) && (planeID() == as.planeID()); }
262 
263  /// Returns whether there is a data product and plane
264  bool isValid() const
265  { return DataProductChangeTracker_t::isValid() && planeID().isValid; }
266 
267  /// Returns whether data product and TPC plane are the same as in "as"
269  { return same(as); }
270 
271  /// Returns whether data product or TPC plane are different than in "than"
272  bool operator!= (PlaneDataChangeTracker_t const& than) const
273  { return !same(than); }
274  /// @}
275 
276 
277  /// @name State change
278  /// @{
279  /// Forget the current data product
280  void clear()
281  { DataProductChangeTracker_t::clear(); SetPlaneID(geo::PlaneID()); }
282 
283  /// Set a new event and data product label as current
284  void set(
285  art::Event const& evt, art::InputTag const& label, geo::PlaneID const& pid
286  )
287  { DataProductChangeTracker_t::set(evt, label); SetPlaneID(pid); }
288 
289  /// Update to a new data product, return true if it has changed
290  bool update(PlaneDataChangeTracker_t const& new_prod)
291  {
292  if (same(new_prod)) return false;
293  *this = new_prod;
294  return true;
295  }
296 
297  /// @}
298 
299  /// Returns a string representation of event and data product label
300  operator std::string() const
301  {
302  return DataProductChangeTracker_t::operator std::string()
303  + " " + std::string(planeID());
304  }
305 
306  private:
307  struct LocalState_t {
309  }; // LocalState_t
310 
312 
313  void SetPlaneID(geo::PlaneID const& pid) { state.plane_id = pid; }
314 
315  }; // PlaneDataChangeTracker_t
316 
317 
318  inline std::ostream& operator<<
319  (std::ostream& out, PlaneDataChangeTracker_t const& trk)
320  { out << std::string(trk); return out; }
321 
322 
323 } // namespace util
324 
325 #endif // UTIL_CHANGETRACKERS_H
EventChangeTracker_t(art::Event const &evt)
Constructor: current event as specified.
void set(art::EventID const &evt_id)
Sets the current event ID.
art::EventID event_id
ID of the current event.
bool isValid() const
Returns whether there is a current event.
Namespace for general, non-LArSoft-specific utilities.
bool sameProduct(PlaneDataChangeTracker_t const &as) const
Returns whether we are in the same event (the rest could differ)
PlaneDataChangeTracker_t(art::EventID const &evt_id, art::InputTag const &label, geo::PlaneID const &pid)
Constructor: specifies current data product and TPC plane.
art::EventID const & eventID() const
Returns the current event ID (it might be made public...)
std::string string
Definition: nybbler.cc:12
Detects the presence of a new event or data product.
The data type to uniquely identify a Plane.
Definition: geo_types.h:472
bool sameTPC(PlaneDataChangeTracker_t const &as) const
Returns whether we have the same data product and TPC as "as".
Detects the presence of a new event.
art::InputTag const & inputLabel() const
static constexpr double as
Definition: Units.h:101
bool update(EventChangeTracker_t const &trk)
Sets the current event, and returns true if it is changed.
void SetInputLabel(art::InputTag const &label)
void SetPlaneID(geo::PlaneID const &pid)
bool operator!=(EventChangeTracker_t const &than) const
Returns whether this tracker is in a different state than another.
EventChangeTracker_t()=default
Default constructor: no current event, next event is a new one.
bool isValid() const
Returns whether there is a data product and plane.
The data type to uniquely identify a TPC.
Definition: geo_types.h:386
Definition of data types for geometry description.
LocalState_t state
local state of the tracker (may inherit some more)
Detects the presence of a new event, data product or wire plane.
EventChangeTracker_t(art::EventID const &evt_id)
Constructor: current event as specified by the event ID.
bool update(PlaneDataChangeTracker_t const &new_prod)
Update to a new data product, return true if it has changed.
bool same(EventChangeTracker_t const &as) const
void set(art::Event const &evt, art::InputTag const &label)
Set a new event and data product label as current.
bool same(PlaneDataChangeTracker_t const &as) const
Returns whether we have the same plane data as "as".
bool same(DataProductChangeTracker_t const &as) const
Returns whether we have same data product as in "as".
TCEvent evt
Definition: DataStructs.cxx:7
bool update(DataProductChangeTracker_t const &new_prod)
Update to a new data product, return true if it has changed.
std::string to_string(ModuleType const mt)
Definition: ModuleType.h:34
geo::PlaneID const & planeID() const
Returns the current plane ID.
EventID id() const
Definition: Event.cc:34
bool isValid() const
Returns whether there is a current event and data product.
PlaneDataChangeTracker_t(art::Event const &evt, art::InputTag const &label, geo::PlaneID const &pid)
Constructor: specifies current data product and TPC plane.
bool sameEvent(DataProductChangeTracker_t const &as) const
Returns whether we are in the same event (the rest could differ)
bool operator==(EventChangeTracker_t const &as) const
Returns whether this tracker is in the same state as another.