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 // framework libraries
15 
16 // C/C++ standard libraries
17 #include <string> // std::to_string()
18 #include <type_traits> // std::enable_if_t
19 
20 
21 namespace util {
22 
23  /** **************************************************************************
24  * @brief Detects the presence of a new event
25  *
26  * The state of this class describes the current event by its ID.
27  */
29  public:
30  /// Default constructor: no current event, next event is a new one
31  EventChangeTracker_t() = default;
32 
33  /// Constructor: current event as specified
35 
36  /// Constructor: current event as specified by the event ID
37  EventChangeTracker_t(art::EventID const& evt_id): state{evt_id} {}
38 
39  /// @name State query
40  /// @{
41  /// Returns whether this tracker is in the same state as another
42  bool same(EventChangeTracker_t const& as) const
43  { return as.eventID() == eventID(); }
44 
45  /// Returns whether there is a current event
46  bool isValid() const { return eventID() != art::EventID(); }
47 
48  /// Returns whether this tracker is in the same state as another
49  bool operator== (EventChangeTracker_t const& as) const { return same(as); }
50 
51  /// Returns whether this tracker is in a different state than another
52  bool operator!= (EventChangeTracker_t const& than) const
53  { return !same(than); }
54  /// @}
55 
56  /// @name State change
57  /// @{
58  /// Forgets the current event
59  void clear() { set(art::EventID()); }
60 
61  /// Sets the current event ID
62  void set(art::EventID const& evt_id) { state.event_id = evt_id; }
63 
64  /// Sets the current event ID from the given event
65  void set(art::Event const& evt) { set(evt.id()); }
66 
67  /// Sets the current event, and returns true if it is changed
69  {
70  if (same(trk)) return false;
71  *this = trk;
72  return true;
73  }
74  /// @}
75 
76  /// Returns a string representing the current state
77  operator std::string() const
78  {
79  return "R:" + std::to_string(eventID().run())
80  + " S:" + std::to_string(eventID().subRun())
81  + " E:" + std::to_string(eventID().event());
82  }
83 
84 
85  protected:
86  /// Returns the current event ID (it might be made public...)
87  art::EventID const& eventID() const { return state.event_id; }
88 
89 
90  private:
91  struct LocalState_t {
92  art::EventID event_id; ///< ID of the current event
93  };
94 
95  LocalState_t state; ///< local state of the tracker (may inherit some more)
96 
97  }; // EventChangeTracker_t
98 
99 
100  template <typename Stream>
101  decltype(auto) operator<<
102  (Stream&& out, EventChangeTracker_t const& trk)
103  { out << std::string(trk); return std::forward<Stream>(out); }
104 
105 
106 
107  /** **************************************************************************
108  * @brief Detects the presence of a new event or data product
109  *
110  * The state of this class describes the current data product input as the
111  * event it belongs to (by its ID) and the input tag.
112  */
114  public:
115 
116  /// Default constructor: no current data product
117  DataProductChangeTracker_t() = default;
118 
119  /// Constructor: specifies current event and data product label
121  (art::Event const& evt, art::InputTag const& label):
122  EventChangeTracker_t(evt), state{label}
123  {}
124 
125  /// Constructor: specifies current event ID and data product label
127  (art::EventID const& evt_id, art::InputTag const& label):
128  EventChangeTracker_t(evt_id), state{label}
129  {}
130 
131 
132  /// @name State query
133  /// @{
134  /// Returns the current input label
135  art::InputTag const& inputLabel() const { return state.input_label; }
136 
137  /// Returns whether we are in the same event (the rest could differ)
139  { return EventChangeTracker_t::same(as); }
140 
141  /// Returns whether we have same data product as in "as"
142  bool same(DataProductChangeTracker_t const& as) const
143  { return sameEvent(as) && (inputLabel() == as.inputLabel()); }
144 
145  /// Returns whether there is a current event and data product
146  bool isValid() const
147  {
148  return
149  EventChangeTracker_t::isValid() && !inputLabel().label().empty();
150  }
151 
152  /// Returns whether the event and input label are the same as in "as"
154  { return same(as); }
155 
156  /// Returns whether the event or input label are different than in "than"
157  bool operator!= (DataProductChangeTracker_t const& than) const
158  { return !same(than); }
159  /// @}
160 
161 
162  /// @name State change
163  /// @{
164  /// Forget the current data product
165  void clear()
166  { EventChangeTracker_t::clear(); SetInputLabel(art::InputTag()); }
167 
168  /// Set a new event and data product label as current
169  void set(art::Event const& evt, art::InputTag const& label)
170  { EventChangeTracker_t::set(evt); SetInputLabel(label); }
171 
172  /// Update to a new data product, return true if it has changed
173  bool update(DataProductChangeTracker_t const& new_prod)
174  {
175  if (same(new_prod)) return false;
176  *this = new_prod;
177  return true;
178  }
179 
180  /// @}
181 
182  /// Returns a string representation of event and data product label
183  operator std::string() const
184  {
185  return EventChangeTracker_t::operator std::string()
186  + " I{" + inputLabel().encode() + "}";
187  }
188 
189  private:
190  struct LocalState_t {
192  }; // LocalState_t
193 
195 
196  void SetInputLabel(art::InputTag const& label)
197  { state.input_label = label; }
198 
199  }; // DataProductChangeTracker_t
200 
201 
202  template <typename Stream>
203  decltype(auto) operator<<
204  (Stream&& out, DataProductChangeTracker_t const& trk)
205  { out << std::string(trk); return std::forward<Stream>(out); }
206 
207 
208 } // namespace util
209 
210 #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.
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.
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)
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.
LocalState_t state
local state of the tracker (may inherit some more)
EventChangeTracker_t(art::EventID const &evt_id)
Constructor: current event as specified by the event ID.
bool same(EventChangeTracker_t const &as) const
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
EventID id() const
Definition: Event.cc:34
bool isValid() const
Returns whether there is a current event and data product.
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.