LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
Snapshot.h
Go to the documentation of this file.
1 
14 #ifndef IOVDATA_SNAPSHOT_H
15 #define IOVDATA_SNAPSHOT_H
16 
17 #include "ChData.h"
18 #include "IOVDataConstants.h"
19 #include "IOVDataError.h"
20 #include "IOVTimeStamp.h"
21 #include <algorithm>
22 #include <sstream>
23 #include <vector>
24 
25 namespace lariov {
26 
30  template <class T>
31  class Snapshot {
32 
33  public:
35  Snapshot() : fStart(0, 0), fEnd(0, 0) {}
36 
38  ~Snapshot() {}
39 
40  void Clear();
41 
42  const IOVTimeStamp& Start() const { return fStart; }
43  const IOVTimeStamp& End() const { return fEnd; }
44  void SetIoV(const IOVTimeStamp& start, const IOVTimeStamp& end);
45 
46  bool IsValid(const IOVTimeStamp& ts) const;
47 
48  size_t NChannels() const { return fData.size(); }
49 
50  const std::vector<T>& Data() const { return fData; }
51 
53  template <class U = T,
55  bool HasChannel(unsigned int ch) const
56  {
57 
58  typename std::vector<T>::const_iterator it = std::lower_bound(fData.begin(), fData.end(), ch);
59  if (it == fData.end() || it->Channel() != ch) { return false; }
60  else
61  return true;
62  }
63 
64  template <class U = T,
65  typename std::enable_if<std::is_base_of<ChData, U>::value, int>::type = 0>
66  const T& GetRow(unsigned int ch) const
67  {
68 
69  typename std::vector<T>::const_iterator it = std::lower_bound(fData.begin(), fData.end(), ch);
70 
71  if (it == fData.end() || it->Channel() != ch) {
72  std::string msg("Channel not found: ");
73  msg += std::to_string(ch);
74  throw IOVDataError(msg);
75  }
76 
77  return *it;
78  }
79 
80  template <class U = T,
81  typename std::enable_if<std::is_base_of<ChData, U>::value, int>::type = 0>
82  void AddOrReplaceRow(const T& data)
83  {
84  typename std::vector<T>::iterator it =
85  std::lower_bound(fData.begin(), fData.end(), data.Channel());
86  if (it == fData.end() || data.Channel() != it->Channel()) {
87  bool sort = (!(fData.empty()) && data < fData.back());
88  fData.push_back(data);
89  if (sort) std::sort(fData.begin(), fData.end());
90  }
91  else {
92  *it = data;
93  }
94  }
95 
96  private:
99  std::vector<T> fData;
100  };
101 
102  //=============================================
103  // Class implementation
104  //=============================================
105  template <class T>
107  {
108  fData.clear();
111  }
112 
113  template <class T>
115  {
116  if (start >= end) {
117  throw IOVDataError("Called Snapshot::SetIoV with start timestamp >= end timestamp!");
118  }
119 
120  fStart = start;
121  fEnd = end;
122  }
123 
124  template <class T>
125  bool Snapshot<T>::IsValid(const IOVTimeStamp& ts) const
126  {
127  return (ts >= fStart && ts < fEnd);
128  }
129 
130 } //end namespace lariov
131 #endif
132  // end of doxygen group
intermediate_table::iterator iterator
void SetStamp(unsigned long stamp, unsigned int substamp=0)
Definition: IOVTimeStamp.h:41
~Snapshot()
Default destructor.
Definition: Snapshot.h:38
std::vector< T > fData
Definition: Snapshot.h:99
void AddOrReplaceRow(const T &data)
Definition: Snapshot.h:82
IOVTimeStamp fEnd
Definition: Snapshot.h:98
bool HasChannel(unsigned int ch) const
Only included with class if T has base class ChData.
Definition: Snapshot.h:55
intermediate_table::const_iterator const_iterator
const T & GetRow(unsigned int ch) const
Definition: Snapshot.h:66
Class def header for a class IOVTimeStamp.
unsigned long SubStamp() const
Definition: IOVTimeStamp.h:38
decltype(auto) constexpr end(T &&obj)
ADL-aware version of std::end.
Definition: StdUtils.h:77
decltype(auto) constexpr to_string(T &&obj)
ADL-aware version of std::to_string.
IOVTimeStamp fStart
Definition: Snapshot.h:97
Snapshot()
Default constructor.
Definition: Snapshot.h:35
size_t NChannels() const
Definition: Snapshot.h:48
double value
Definition: spectrum.C:18
const IOVTimeStamp & Start() const
Definition: Snapshot.h:42
Filters for channels, events, etc.
void SetIoV(const IOVTimeStamp &start, const IOVTimeStamp &end)
Definition: Snapshot.h:114
const IOVTimeStamp & End() const
Definition: Snapshot.h:43
unsigned long Stamp() const
Definition: IOVTimeStamp.h:37
const std::vector< T > & Data() const
Definition: Snapshot.h:50
Collection of exception classes for IOVData.
Class def header for a class ChData.
static IOVTimeStamp MaxTimeStamp()
bool IsValid(const IOVTimeStamp &ts) const
Definition: Snapshot.h:125