Classes | Public Types | Public Member Functions | Private Attributes | List of all members
lariov::DBDataset Class Reference

#include <DBDataset.h>

Classes

class  DBRow
 

Public Types

typedef std::variant< long, double, std::unique_ptr< std::string > > value_type
 

Public Member Functions

 DBDataset ()
 
 DBDataset (void *dataset, bool release=false)
 
 DBDataset (const IOVTimeStamp &begin_time, const IOVTimeStamp &end_time, std::vector< std::string > &&col_names, std::vector< std::string > &&col_types, std::vector< DBChannelID_t > &&channels, std::vector< value_type > &&data)
 
const IOVTimeStampbeginTime () const
 
const IOVTimeStampendTime () const
 
size_t nrows () const
 
size_t ncols () const
 
const std::vector< std::string > & colNames () const
 
const std::vector< std::string > & colTypes () const
 
const std::vector< DBChannelID_t > & channels () const
 
const std::vector< value_type > & data () const
 
int getRowNumber (DBChannelID_t ch) const
 
int getColNumber (const std::string &name) const
 
DBRow getRow (size_t row) const
 

Private Attributes

IOVTimeStamp fBeginTime
 
IOVTimeStamp fEndTime
 
std::vector< std::stringfColNames
 
std::vector< std::stringfColTypes
 
std::vector< DBChannelID_tfChannels
 
std::vector< value_typefData
 

Detailed Description

Definition at line 60 of file DBDataset.h.

Member Typedef Documentation

typedef std::variant<long, double, std::unique_ptr<std::string> > lariov::DBDataset::value_type

Definition at line 67 of file DBDataset.h.

Constructor & Destructor Documentation

lariov::DBDataset::DBDataset ( )

Definition at line 20 of file DBDataset.cxx.

20  :
21  fBeginTime(0, 0),
22  fEndTime(0, 0)
23 {}
IOVTimeStamp fBeginTime
Definition: DBDataset.h:143
IOVTimeStamp fEndTime
Definition: DBDataset.h:144
lariov::DBDataset::DBDataset ( void *  dataset,
bool  release = false 
)

Definition at line 28 of file DBDataset.cxx.

28  :
29  fBeginTime(0, 0),
30  fEndTime(0, 0)
31 {
32  // Parse dataset and get number of rows.
33 
34  size_t nrows = getNtuples(dataset) - kNUMBER_HEADER_ROWS;
35  //mf::LogInfo("DBDataset") << "DBDataset: Number of rows = " << nrows << "\n";
36  fChannels.reserve(nrows);
37 
38  // Process header rows.
39 
40  int err = 0;
41  char buf[kBUFFER_SIZE];
42  Tuple tup;
43 
44  // Extract IOV begin time.
45 
46  tup = getTuple(dataset, 0);
47  getStringValue(tup, 0, buf, kBUFFER_SIZE, &err);
49  //mf::LogInfo log("DBDataset");
50  //log << "DBDataset: Begin time stamp input = " << buf << "\n";
51  //log << "DBDataset: Begin time stamp = " << fBeginTime.DBStamp() << "\n";
52  releaseTuple(tup);
53 
54  // Extract IOV end time.
55 
56  tup = getTuple(dataset, 1);
57  getStringValue(tup, 0, buf, kBUFFER_SIZE, &err);
58  if ( 0 == strcmp(buf,"-") )
60  else
62  //mf::LogInfo log("DBDataset");
63  //log << "DBDataset: End time stamp input = " << buf << "\n";
64  //log << "DBDataset: End time stamp = " << fEndTime.DBStamp() << "\n";
65  releaseTuple(tup);
66 
67  // Extract column names.
68 
69  tup = getTuple(dataset, 2);
70  size_t ncols = getNfields(tup);
71  //mf::LogInfo("DBDataset") << "DBDataset: Number of columns = " << ncols << "\n";
72  fColNames.reserve(ncols);
73  for (size_t col=0; col<ncols; ++col) {
74  getStringValue(tup, col, buf, kBUFFER_SIZE, &err);
75  //mf::LogInfo("DBDataset") << "DBDataset: Column name = " << buf << "\n";
76  fColNames.push_back(std::string(buf));
77  }
78  releaseTuple(tup);
79 
80  // Extract column types.
81 
82  tup = getTuple(dataset, 3);
83  fColTypes.reserve(ncols);
84  for (size_t col=0; col < ncols; ++col) {
85  getStringValue(tup, col, buf, kBUFFER_SIZE, &err);
86  //mf::LogInfo("DBDataset") << "DBDataset: Column type = " << buf << "\n";
87  fColTypes.push_back(std::string(buf));
88  }
89  releaseTuple(tup);
90 
91  // Extract data. Loop over rows.
92 
93  fData.reserve(nrows * ncols);
94  for(size_t row = 0; row < nrows; ++row) {
95  //mf::LogInfo("DBDataset") << "\nRow " << row << "\n";
96  tup = getTuple(dataset, row + kNUMBER_HEADER_ROWS);
97 
98  // Loop over columns.
99 
100  for(size_t col = 0; col < ncols; ++col) {
101  getStringValue(tup, col, buf, kBUFFER_SIZE, &err);
102 
103  // Convert string value to DBDataset::value_type (std::variant).
104 
105  if(fColTypes[col] == "integer" || fColTypes[col] == "bigint") {
106  long value = strtol(buf, 0, 10);
107  fData.push_back(value_type(value));
108  //mf::LogInfo("DBDataset") << "DBDataset: row=" << row << ", column=" << col << ", value=" << fData.back()
109  // << "\n";
110  if(col == 0) {
111  fChannels.push_back(value);
112  //mf::LogInfo("DBDataset") << "DBDataset: channel=" << fChannels.back() << "\n";
113  }
114  }
115  else if(fColTypes[col] == "real") {
116  double value = strtod(buf, 0);
117  fData.push_back(value_type(value));
118  //mf::LogInfo("DBDataset") << "DBDataset: row=" << row << ", column=" << col << ", value=" << fData.back()
119  // << "\n";
120  if(col == 0) {
121  mf::LogError("DBDataset") << "First column has wrong type real." << "\n";
122  throw cet::exception("DBDataset") << "First column has wrong type real.";
123  }
124  }
125  else if(fColTypes[col] == "text") {
126  fData.emplace_back(std::make_unique<std::string>(buf));
127  //mf::LogInfo("DBDataset") << "DBDataset: row=" << row << ", column=" << col << ", value=" << fData.back()
128  // << "\n";
129  if(col == 0) {
130  mf::LogError("DBDataset") << "First column has wrong type text." << "\n";
131  throw cet::exception("DBDataset") << "First column has wrong type text.";
132  }
133  }
134  else if(fColTypes[col] == "boolean") {
135  long value = 0;
136  std::string s = std::string(buf);
137  if(s == "true" || s == "True" || s == "TRUE" || s == "1")
138  value = 1;
139  else if(s == "false" || s == "False" || s == "FALSE" || s == "0")
140  value = 0;
141  else {
142  mf::LogError("DBDataset") << "Unknown string representation of boolean " << s << "\n";
143  throw cet::exception("DBDataset") << "Unknown string representation of boolean " << s
144  << "\n";
145  }
146  fData.push_back(value_type(value));
147  //mf::LogInfo("DBDataset") << "DBDataset: row=" << row << ", column=" << col << ", value=" << fData.back()
148  // << "\n";
149  if(col == 0) {
150  mf::LogError("DBDataset") << "First column has wrong type boolean." << "\n";
151  throw cet::exception("DBDataset") << "First column has wrong type boolean.";
152  }
153  }
154  else {
155  mf::LogError("DBDataset") << "Unknown datatype = " << fColTypes[col] << "\n";
156  throw cet::exception("DBDataset") << "Unknown datatype = " << fColTypes[col]
157  << ": " << buf << "\n";
158  }
159  }
160  releaseTuple(tup);
161  }
162 
163  // Maybe release dataset memory.
164 
165  if(release)
166  releaseDataset(dataset);
167 }
const unsigned int kNUMBER_HEADER_ROWS
std::variant< long, double, std::unique_ptr< std::string > > value_type
Definition: DBDataset.h:67
std::string string
Definition: nybbler.cc:12
size_t nrows() const
Definition: DBDataset.h:123
MaybeLogger_< ELseverityLevel::ELsev_error, false > LogError
std::vector< std::string > fColNames
Definition: DBDataset.h:145
void * Tuple
Definition: DBFolder.h:13
std::vector< std::string > fColTypes
Definition: DBDataset.h:146
IOVTimeStamp fBeginTime
Definition: DBDataset.h:143
std::vector< value_type > fData
Definition: DBDataset.h:148
void err(const char *fmt,...)
Definition: message.cpp:226
const unsigned int kBUFFER_SIZE
std::vector< DBChannelID_t > fChannels
Definition: DBDataset.h:147
string release
Definition: conf.py:24
int strcmp(const String &s1, const String &s2)
Definition: relates.cpp:14
IOVTimeStamp fEndTime
Definition: DBDataset.h:144
size_t ncols() const
Definition: DBDataset.h:124
static IOVTimeStamp GetFromString(const std::string &ts)
static IOVTimeStamp MaxTimeStamp()
static QCString * s
Definition: config.cpp:1042
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
lariov::DBDataset::DBDataset ( const IOVTimeStamp begin_time,
const IOVTimeStamp end_time,
std::vector< std::string > &&  col_names,
std::vector< std::string > &&  col_types,
std::vector< DBChannelID_t > &&  channels,
std::vector< value_type > &&  data 
)

Definition at line 171 of file DBDataset.cxx.

176  : // Calibration data.
177  fBeginTime(begin_time),
178  fEndTime(end_time),
179  fColNames(std::move(col_names)),
180  fColTypes(std::move(col_types)),
181  fChannels(std::move(channels)),
182  fData(std::move(data))
183 {}
std::vector< std::string > fColNames
Definition: DBDataset.h:145
std::vector< std::string > fColTypes
Definition: DBDataset.h:146
IOVTimeStamp fBeginTime
Definition: DBDataset.h:143
def move(depos, offset)
Definition: depos.py:107
std::vector< value_type > fData
Definition: DBDataset.h:148
std::vector< DBChannelID_t > fChannels
Definition: DBDataset.h:147
IOVTimeStamp fEndTime
Definition: DBDataset.h:144

Member Function Documentation

const IOVTimeStamp& lariov::DBDataset::beginTime ( ) const
inline

Definition at line 121 of file DBDataset.h.

121 {return fBeginTime;}
IOVTimeStamp fBeginTime
Definition: DBDataset.h:143
const std::vector<DBChannelID_t>& lariov::DBDataset::channels ( ) const
inline

Definition at line 127 of file DBDataset.h.

127 {return fChannels;}
std::vector< DBChannelID_t > fChannels
Definition: DBDataset.h:147
const std::vector<std::string>& lariov::DBDataset::colNames ( ) const
inline

Definition at line 125 of file DBDataset.h.

125 {return fColNames;}
std::vector< std::string > fColNames
Definition: DBDataset.h:145
const std::vector<std::string>& lariov::DBDataset::colTypes ( ) const
inline

Definition at line 126 of file DBDataset.h.

126 {return fColTypes;}
std::vector< std::string > fColTypes
Definition: DBDataset.h:146
const std::vector<value_type>& lariov::DBDataset::data ( ) const
inline

Definition at line 128 of file DBDataset.h.

128 {return fData;}
std::vector< value_type > fData
Definition: DBDataset.h:148
const IOVTimeStamp& lariov::DBDataset::endTime ( ) const
inline

Definition at line 122 of file DBDataset.h.

122 {return fEndTime;}
IOVTimeStamp fEndTime
Definition: DBDataset.h:144
int lariov::DBDataset::getColNumber ( const std::string name) const

Definition at line 228 of file DBDataset.cxx.

229 {
230  int result = -1;
231 
232  // Scan column names.
233 
234  for(size_t i=0; i<fColNames.size(); ++i) {
235  if(fColNames[i] == name) {
236  result = i;
237  break;
238  }
239  }
240 
241  return result;
242 }
static QCString name
Definition: declinfo.cpp:673
static QCString result
std::vector< std::string > fColNames
Definition: DBDataset.h:145
DBRow lariov::DBDataset::getRow ( size_t  row) const
inline

Definition at line 137 of file DBDataset.h.

137 {return DBRow(&fData[ncols()*row]);}
std::vector< value_type > fData
Definition: DBDataset.h:148
size_t ncols() const
Definition: DBDataset.h:124
int lariov::DBDataset::getRowNumber ( DBChannelID_t  ch) const

Definition at line 188 of file DBDataset.cxx.

189 {
190  int result = -1;
191 
192  // Do a binary search on channel numbers.
193 
194  int low = 0;
195  int high = fChannels.size() - 1;
196  while(high-low > 1) {
197  int mid = (low + high) / 2;
198  if(fChannels[mid] < ch)
199  low = mid;
200  else if(fChannels[mid] > ch)
201  high = mid;
202  else {
203 
204  // Found an exact match. Break out of loop.
205 
206  result = mid;
207  break;
208  }
209  }
210 
211  // If we fell out of loop without finding a match...
212 
213  if(result < 0) {
214  if(fChannels[low] == ch)
215  result = low;
216  else if(fChannels[high] == ch)
217  result = high;
218  }
219 
220  // Done.
221 
222  return result;
223 }
static QCString result
std::vector< DBChannelID_t > fChannels
Definition: DBDataset.h:147
size_t lariov::DBDataset::ncols ( ) const
inline

Definition at line 124 of file DBDataset.h.

124 {return fColNames.size();}
std::vector< std::string > fColNames
Definition: DBDataset.h:145
size_t lariov::DBDataset::nrows ( ) const
inline

Definition at line 123 of file DBDataset.h.

123 {return fChannels.size();}
std::vector< DBChannelID_t > fChannels
Definition: DBDataset.h:147

Member Data Documentation

IOVTimeStamp lariov::DBDataset::fBeginTime
private

Definition at line 143 of file DBDataset.h.

std::vector<DBChannelID_t> lariov::DBDataset::fChannels
private

Definition at line 147 of file DBDataset.h.

std::vector<std::string> lariov::DBDataset::fColNames
private

Definition at line 145 of file DBDataset.h.

std::vector<std::string> lariov::DBDataset::fColTypes
private

Definition at line 146 of file DBDataset.h.

std::vector<value_type> lariov::DBDataset::fData
private

Definition at line 148 of file DBDataset.h.

IOVTimeStamp lariov::DBDataset::fEndTime
private

Definition at line 144 of file DBDataset.h.


The documentation for this class was generated from the following files: