LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
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 
52 #include "larevt/CalibrationDBI/Interface/CalibrationDBIFwd.h"
53 #include <memory>
54 #include <string>
55 #include <variant>
56 #include <vector>
57 
58 namespace lariov {
59  class DBDataset {
60 
61  public:
62  // Typedef
63 
64  typedef std::variant<long, double, std::unique_ptr<std::string>> value_type;
65 
66  // Nested class representing data from one row.
67 
68  class DBRow {
69  public:
70  // Constructors.
71 
72  DBRow() : fData(nullptr) {}
73  DBRow(const value_type* s) : fData(s) {}
74 
75  // Accessors.
76 
77  bool isValid() const { return fData != nullptr; }
78  const value_type& getData(size_t col) const { return fData[col]; }
79  const std::string& getStringData(size_t col) const
80  {
81  return *std::get<std::unique_ptr<std::string>>(fData[col]);
82  }
83  long getLongData(size_t col) const { return std::get<long>(fData[col]); }
84  double getDoubleData(size_t col) const { return std::get<double>(fData[col]); }
85 
86  private:
87  // Data member.
88 
89  const value_type* fData; // Borrowed referenced from enclosing class.
90  };
91 
92  // Back to main class.
93 
94  public:
95  // Constructors.
96 
97  DBDataset(); // Default constructor.
98 
99  // Initializing constructor based on libwda struct.
100 
101  DBDataset(void* dataset, bool release = false);
102 
103  // Initializing move constructor.
104  // This constructor is used to initialize sqlite data.
105 
106  DBDataset(const IOVTimeStamp& begin_time, // IOV begin time.
107  const IOVTimeStamp& end_time, // IOV end time.
108  std::vector<std::string>&& col_names, // Column names.
109  std::vector<std::string>&& col_types, // Column types.
110  std::vector<DBChannelID_t>&& channels, // Channels.
111  std::vector<value_type>&& data); // Calibration data (length nchan*ncol).
112 
113  // Simple accessors.
114 
115  const IOVTimeStamp& beginTime() const { return fBeginTime; }
116  const IOVTimeStamp& endTime() const { return fEndTime; }
117  size_t nrows() const { return fChannels.size(); }
118  size_t ncols() const { return fColNames.size(); }
119  const std::vector<std::string>& colNames() const { return fColNames; }
120  const std::vector<std::string>& colTypes() const { return fColTypes; }
121  const std::vector<DBChannelID_t>& channels() const { return fChannels; }
122  const std::vector<value_type>& data() const { return fData; }
123 
124  // Determine row and column numbers.
125 
126  int getRowNumber(DBChannelID_t ch) const;
127  int getColNumber(const std::string& name) const;
128 
129  // Access one row.
130 
131  DBRow getRow(size_t row) const { return DBRow(&fData[ncols() * row]); }
132 
133  private:
134  // Data members.
135 
136  IOVTimeStamp fBeginTime; // IOV begin time.
137  IOVTimeStamp fEndTime; // IOV end time.
138  std::vector<std::string> fColNames; // Column names.
139  std::vector<std::string> fColTypes; // Column types.
140  std::vector<DBChannelID_t> fChannels; // Channels.
141  std::vector<value_type> fData; // Calibration data (length nchan*ncols).
142  };
143 }
144 
145 #endif
DBRow(const value_type *s)
Definition: DBDataset.h:73
std::variant< long, double, std::unique_ptr< std::string > > value_type
Definition: DBDataset.h:64
double getDoubleData(size_t col) const
Definition: DBDataset.h:84
const value_type & getData(size_t col) const
Definition: DBDataset.h:78
size_t nrows() const
Definition: DBDataset.h:117
const std::vector< std::string > & colNames() const
Definition: DBDataset.h:119
const std::string & getStringData(size_t col) const
Definition: DBDataset.h:79
DBRow getRow(size_t row) const
Definition: DBDataset.h:131
std::vector< std::string > fColNames
Definition: DBDataset.h:138
int getRowNumber(DBChannelID_t ch) const
Definition: DBDataset.cxx:185
Class def header for a class IOVTimeStamp.
const std::vector< value_type > & data() const
Definition: DBDataset.h:122
std::vector< std::string > fColTypes
Definition: DBDataset.h:139
IOVTimeStamp fBeginTime
Definition: DBDataset.h:136
const value_type * fData
Definition: DBDataset.h:89
Int_t col[ntarg]
Definition: Style.C:29
std::vector< value_type > fData
Definition: DBDataset.h:141
const IOVTimeStamp & endTime() const
Definition: DBDataset.h:116
const std::vector< std::string > & colTypes() const
Definition: DBDataset.h:120
const IOVTimeStamp & beginTime() const
Definition: DBDataset.h:115
std::vector< DBChannelID_t > fChannels
Definition: DBDataset.h:140
Filters for channels, events, etc.
const std::vector< DBChannelID_t > & channels() const
Definition: DBDataset.h:121
IOVTimeStamp fEndTime
Definition: DBDataset.h:137
bool isValid() const
Definition: DBDataset.h:77
long getLongData(size_t col) const
Definition: DBDataset.h:83
size_t ncols() const
Definition: DBDataset.h:118
int getColNumber(const std::string &name) const
Definition: DBDataset.cxx:225