CDPStorage_service.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 /// \brief Service to store calibration data products (CDP) in the
3 /// SQLite3 metadatabase of a file
4 /// \author brebel@fnal.gov
5 ////////////////////////////////////////////////////////////////////////
7 
13 #include "TFile.h"
14 
15 namespace calib
16 {
17  //----------------------------------------------------------------------------
20  {
21  this->reconfigure(pset);
23  reg.sPostOpenFile.watch(this, &CDPStorage::postOpenFile);
24  }
25 
26  //----------------------------------------------------------------------------
28  {
29  }
30 
31  //----------------------------------------------------------------------------
33  {
34  fRetrieveDriftCalFromFile = pset.get<bool>("RetrieveDriftCalFromFile");
35 
36  return;
37  }
38 
39  //----------------------------------------------------------------------------
41  {
42  // test the input flags to see if we are to get the
43  // stored calibrations from the file - currently only
44  // using this functionality for the drift calibration, but
45  // could use it for others in the future by including ||
46  // with each flag in the following line
48 
49  std::shared_ptr<TFile> file(TFile::Open(fn.c_str(), "READ"));
50 
51  // get the wrapper to the SQLite3 DB
52  art::SQLite3Wrapper sql3w(file.get(),
53  "NOvACalibrations",
54  SQLITE_OPEN_READONLY);
55 
57  }
58 
59  return;
60  }
61 
62  //----------------------------------------------------------------------------
64  {
65  std::shared_ptr<TFile> file(TFile::Open(ofi.fileName().c_str(), "UPDATE"));
66 
67  // get the wrapper to the SQLite3 DB
68  art::SQLite3Wrapper sql3w(file.get(),
69  "NOvACalibrations",
70  SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE);
71 
72  this->WriteDriftResponse(sql3w);
73 
74  // write the DB
75  sql3w.reset();
76 
77  // close the ROOT file
78  file->Close();
79 
80  return;
81  }
82 
83  //----------------------------------------------------------------------------
85  {
87 
88  // make the table for the drift response, one column for each of the important
89  // quantities
90  sqlite3_exec(sql3w,
91  "BEGIN TRANSACTION; " // +
92  "DROP TABLE IF EXISTS DriftResponse; " // +
93  "CREATE TABLE DriftResponse(ID PRIMARY KEY, " // +
94  " Channel INT, " // +
95  " Mean REAL, " // +
96  " MeanErr REAL, " // +
97  " RMS REAL, " // +
98  " NHits INT, " // +
99  " StartTime INT, " // +
100  " EndTime INT );" // +
101  "COMMIT;",
102  0,
103  0,
104  errMsg);
105 
106  // make sure there were no problems
107  errMsg.throwIfError();
108 
109  sqlite3_exec(sql3w, "BEGIN TRANSACTION;", 0, 0, errMsg);
110  sqlite3_stmt *stmt = 0;
111  sqlite3_prepare_v2(sql3w,
112  "INSERT INTO DriftResponse(Channel, Mean, "
113  "MeanErr, RMS, NHits, StartTime, "
114  "EndTime) "
115  "VALUES(?, ?, ?, ?, ?, ?, ?);",
116  -1,
117  &stmt,
118  NULL);
119 
120  LOG_DEBUG("CDPStorage") << " vector size: " << fDriftResponseVec.size();
121 
122 
123  for( auto const& dr : fDriftResponseVec){
124 
125  LOG_DEBUG("CDPStorage") << dr;
126 
127  // get the map of means for each channel number for each
128  // dr
129  auto const& meanMap = dr.ReturnMap();
130 
131  LOG_DEBUG("CDPStorage") << "map size: " << meanMap.size()
132  << " ReturnMap size: " << dr.ReturnMap().size();
133 
134  for(auto const& mitr : meanMap){
135 
136  int channel = mitr.first;
137  caldp::MeanStruct mean = mitr.second;
138 
139  sqlite3_bind_int (stmt, 1, channel );
140  sqlite3_bind_double(stmt, 2, mean.Mean() );
141  sqlite3_bind_double(stmt, 3, mean.MeanErr() );
142  sqlite3_bind_double(stmt, 4, mean.RMS() );
143  sqlite3_bind_int (stmt, 5, mean.NHits() );
144  sqlite3_bind_int (stmt, 6, dr.StartTime() );
145  sqlite3_bind_int (stmt, 7, dr.EndTime() );
146  if( sqlite3_step(stmt) != SQLITE_DONE)
147  throw cet::exception("CDPStorage") << "problem writing to SQLite3 DB";
148  sqlite3_reset(stmt);
149  sqlite3_clear_bindings(stmt);
150 
151  } // end loop over channel to MeanStruct map
152 
153  } // end loop over DriftResponse data products
154 
155  sqlite3_finalize(stmt);
156 
157  sqlite3_exec(sql3w,
158  "END TRANSACTION;",
159  0,
160  0,
161  art::SQLErrMsg());
162 
163  return;
164  }
165 
166  //----------------------------------------------------------------------------
167  void CDPStorage::DriftResponseToStore(std::vector<caldp::DriftResponse>& drvec)
168  {
169  fDriftResponseVec.swap(drvec);
170 
171  return;
172  }
173 
174  //----------------------------------------------------------------------------
176  {
177 
178  // Read the ParameterSets into memory.
179  sqlite3_stmt *stmt = 0;
180 
181  std::string query("SELECT ID, Channel, Mean, MeanErr, RMS, NHits, StartTime, EndTime from DriftResponse;");
182 
183  LOG_DEBUG("CDPStorage") << query;
184 
185  sqlite3_prepare_v2(sql3w,
186  query.c_str(),
187  -1,
188  &stmt,
189  NULL);
190 
191 
192  while(sqlite3_step(stmt) == SQLITE_ROW){
193  LOG_DEBUG("CDPStorage") << "row returned with " << sqlite3_column_count(stmt) << " columns";
194  for(int c = 0; c < sqlite3_column_count(stmt); ++c)
195  LOG_DEBUG("CDPStorage") << c << " " << sqlite3_column_text(stmt, c);
196  }
197 
198  }
199 
201 
202 }// namespace
#define LOG_DEBUG(stream)
Definition: Messenger.h:149
int NHits() const
Return rms of distribution.
Definition: DriftResponse.h:29
#define DEFINE_ART_SERVICE(svc)
Definition: ServiceMacros.h:93
void RetrieveDriftCalFromFile(art::SQLite3Wrapper &sql3w)
::xsd::cxx::tree::exception< char > exception
Definition: Database.h:225
void reconfigure(const fhicl::ParameterSet &pset)
CDPStorage(fhicl::ParameterSet const &pset, art::ActivityRegistry &reg)
CDPStorage service.
static const char *const errMsg[]
Definition: Error.h:69
std::string const & fileName() const
double RMS() const
Return error on mean.
Definition: DriftResponse.h:28
void postOpenFile(std::string const &fn)
T get(std::string const &key) const
Definition: ParameterSet.h:231
void WriteDriftResponse(art::SQLite3Wrapper &sql3w)
void postCloseOutputFile(art::OutputFileInfo const &ofi)
double Mean() const
Number of hits in mean calculation.
Definition: DriftResponse.h:26
std::vector< caldp::DriftResponse > fDriftResponseVec
vector of DriftResponse objects to store
Definition: CDPStorage.h:45
void DriftResponseToStore(std::vector< caldp::DriftResponse > &drvec)
double MeanErr() const
Return mean.
Definition: DriftResponse.h:27
GlobalSignal< detail::SignalResponseType::LIFO, void(std::string const &)> sPostOpenFile
TFile * file
Definition: cellShifts.C:17
GlobalSignal< detail::SignalResponseType::LIFO, void(OutputFileInfo const &)> sPostCloseOutputFile
void throwIfError()
bool fRetrieveDriftCalFromFile
Definition: CDPStorage.h:46
enum BeamMode string