DBDataset.h
Go to the documentation of this file.
1 #ifndef DBDATASET_H
2 #define DBDATASET_H
3 //=================================================================================
4 //
5 // Name: DBDataset.h
6 //
7 // Purpose: Header for class DBDataset.
8 // This class represents data extracted from the opaque wda struct Dataset,
9 // which struct represents the result of a calibration database query on a
10 // particular database table in an IOV database. Data is extracted and
11 // copied from the Dataset struct using the wda api.
12 //
13 // Database data are essentially a rectangular array of values, indexed by
14 // (row, column). Accessors are provided to access data as string, long,
15 // or double.
16 //
17 // Rows are labeled by channel number. Columns are labeled by name and type.
18 //
19 // Binary values, which are stored as strings by wda, are stored as
20 // std::variant<long, double, unique_ptr<std::string> > > in this class.
21 //
22 // Columns are labeled by column name and type.
23 //
24 // Data members:
25 //
26 // fBeginTime - IOV begin validity time.
27 // fEndTime - IOV end validity time.
28 // fColNames - Names of columns.
29 // fColTypes - Data types of columns.
30 // fChannels - Channel numbers (indexed by row number).
31 // fData - Calibration data.
32 //
33 // Normally, the first element of each row is an integer channel number.
34 // Furthermore, it can be assumed that rows are ordered by increasing channel number.
35 //
36 // Calibration data is contained in data member fData, which is a rectangular array
37 // of strings of dimension # channels x # cols. For efficiency, calibration data are
38 // stored using a single std::vector, which array is allocated once at
39 // construction or update time. Elements are accessed columnwise as follows.
40 //
41 // value = fData[ncols*row + column]
42 //
43 // Or use the provided accessors.
44 //
45 // Nested class DBRow provides access to data from a single database row.
46 //
47 // Created: 26-Oct-2020 - H. Greenlee
48 //
49 //=================================================================================
50 
51 #include <string>
52 #include <vector>
53 #include <variant>
54 #include <memory>
57 
58 namespace lariov
59 {
60  class DBDataset
61  {
62 
63  public:
64 
65  // Typedef
66 
67  typedef std::variant<long, double, std::unique_ptr<std::string> > value_type;
68 
69  // Nested class representing data from one row.
70 
71  class DBRow
72  {
73  public:
74 
75  // Constructors.
76 
77  DBRow() : fData(nullptr) {}
78  DBRow(const value_type* s) : fData(s) {}
79 
80  // Accessors.
81 
82  bool isValid() const {return fData != nullptr;}
83  const value_type& getData(size_t col) const {return fData[col];}
84  const std::string& getStringData(size_t col) const {
85  return *std::get<std::unique_ptr<std::string> >(fData[col]);}
86  long getLongData(size_t col) const {return std::get<long>(fData[col]);}
87  double getDoubleData(size_t col) const {return std::get<double>(fData[col]);}
88 
89  private:
90 
91  // Data member.
92 
93  const value_type* fData; // Borrowed referenced from enclosing class.
94  };
95 
96  // Back to main class.
97 
98  public:
99 
100  // Constructors.
101 
102  DBDataset(); // Default constructor.
103 
104  // Initializing constructor based on libwda struct.
105 
106  DBDataset(void* dataset, bool release=false);
107 
108  // Initializing move constructor.
109  // This constructor is used to initialize sqlite data.
110 
111  DBDataset(const IOVTimeStamp& begin_time, // IOV begin time.
112  const IOVTimeStamp& end_time, // IOV end time.
113  std::vector<std::string>&& col_names, // Column names.
114  std::vector<std::string>&& col_types, // Column types.
115  std::vector<DBChannelID_t>&& channels, // Channels.
116  std::vector<value_type>&& data); // Calibration data (length nchan*ncol).
117 
118 
119  // Simple accessors.
120 
121  const IOVTimeStamp& beginTime() const {return fBeginTime;}
122  const IOVTimeStamp& endTime() const {return fEndTime;}
123  size_t nrows() const {return fChannels.size();}
124  size_t ncols() const {return fColNames.size();}
125  const std::vector<std::string>& colNames() const {return fColNames;}
126  const std::vector<std::string>& colTypes() const {return fColTypes;}
127  const std::vector<DBChannelID_t>& channels() const {return fChannels;}
128  const std::vector<value_type>& data() const {return fData;}
129 
130  // Determine row and column numbers.
131 
132  int getRowNumber(DBChannelID_t ch) const;
133  int getColNumber(const std::string& name) const;
134 
135  // Access one row.
136 
137  DBRow getRow(size_t row) const {return DBRow(&fData[ncols()*row]);}
138 
139  private:
140 
141  // Data members.
142 
143  IOVTimeStamp fBeginTime; // IOV begin time.
144  IOVTimeStamp fEndTime; // IOV end time.
145  std::vector<std::string> fColNames; // Column names.
146  std::vector<std::string> fColTypes; // Column types.
147  std::vector<DBChannelID_t> fChannels; // Channels.
148  std::vector<value_type> fData; // Calibration data (length nchan*ncols).
149  };
150 }
151 
152 #endif
static QCString name
Definition: declinfo.cpp:673
DBRow(const value_type *s)
Definition: DBDataset.h:78
std::variant< long, double, std::unique_ptr< std::string > > value_type
Definition: DBDataset.h:67
double getDoubleData(size_t col) const
Definition: DBDataset.h:87
const value_type & getData(size_t col) const
Definition: DBDataset.h:83
std::string string
Definition: nybbler.cc:12
std::uint32_t DBChannelID_t
size_t nrows() const
Definition: DBDataset.h:123
const std::vector< std::string > & colNames() const
Definition: DBDataset.h:125
const std::string & getStringData(size_t col) const
Definition: DBDataset.h:84
DBRow getRow(size_t row) const
Definition: DBDataset.h:137
std::vector< std::string > fColNames
Definition: DBDataset.h:145
int getRowNumber(DBChannelID_t ch) const
Definition: DBDataset.cxx:188
Class def header for a class IOVTimeStamp.
const std::vector< value_type > & data() const
Definition: DBDataset.h:128
std::vector< std::string > fColTypes
Definition: DBDataset.h:146
IOVTimeStamp fBeginTime
Definition: DBDataset.h:143
const value_type * fData
Definition: DBDataset.h:93
std::vector< value_type > fData
Definition: DBDataset.h:148
const IOVTimeStamp & endTime() const
Definition: DBDataset.h:122
const std::vector< std::string > & colTypes() const
Definition: DBDataset.h:126
const IOVTimeStamp & beginTime() const
Definition: DBDataset.h:121
std::vector< DBChannelID_t > fChannels
Definition: DBDataset.h:147
Filters for channels, events, etc.
const std::vector< DBChannelID_t > & channels() const
Definition: DBDataset.h:127
string release
Definition: conf.py:24
IOVTimeStamp fEndTime
Definition: DBDataset.h:144
bool isValid() const
Definition: DBDataset.h:82
long getLongData(size_t col) const
Definition: DBDataset.h:86
size_t ncols() const
Definition: DBDataset.h:124
int getColNumber(const std::string &name) const
Definition: DBDataset.cxx:228
static QCString * s
Definition: config.cpp:1042