FCalibSQLManager.cc
Go to the documentation of this file.
1 #include <string>
2 #include <sstream>
3 
4 #include <boost/lexical_cast.hpp>
5 
6 #include <fdet/FCalibSQLManager.h>
7 #include <fwk/CentralConfig.h>
8 #include <det/Detector.h>
9 
10 #include <utl/ErrorLogger.h>
11 #include <utl/TimeStamp.h>
12 #include <utl/UTCDateTime.h>
13 #include <utl/Reader.h>
14 #include <utl/TabulatedFunction.h>
15 #include <utl/AugerException.h>
16 #include <utl/ShadowPtr.h>
17 #include <utl/StringCompare.h>
18 
19 #include <fdet/FManagerRegister.h>
20 #include <fdet/FDetector.h>
21 #include <fdet/Eye.h>
22 #include <fdet/Telescope.h>
23 
24 using namespace std;
25 using namespace fdet;
26 using namespace utl;
27 using namespace det;
28 using namespace fwk;
29 
30 
31 REGISTER_F_MANAGER("FCalibSQLManager", FCalibSQLManager);
32 
33 void
34 FCalibSQLManager::Init(const std::string& configLink)
35 {
36  VSQLManager::Init(configLink);
37 
38  fEyesToIgnore.clear();
39 
40  if (fBranch.GetChild("eyesToIgnore"))
41  fBranch.GetChild("eyesToIgnore").GetData(fEyesToIgnore);
42 }
43 
45 FCalibSQLManager::GetData(double& returnData,
46  const string& componentProperty,
47  const string& componentName,
48  const IndexMap& componentIndex)
49  const
50 {
51  // ATTENTION to be implemented with caching.
52 
53  if (componentProperty == "pixel_t_offset") {
54 
55  const int pixeltoffsetid = GetPixelTOffsetId(componentIndex);
56  if (!pixeltoffsetid) {
57  ERROR("Could not retrieve pixel_t_offset_id");
58  return eNotFound;
59  }
60 
61  ostringstream query;
62  query << "SELECT pixel_t_offset FROM pixel_t_offset "
63  "WHERE pixel_t_offset_id = " << pixeltoffsetid;
64 
65  return (Query(query, QueryInfoMessage(componentProperty, componentName)) == eNotFound) ?
66  eNotFound : FetchRow(returnData);
67 
68  } else if (componentProperty == "elecgain" || componentProperty == "status") {
69 
70  const IndexMap::const_iterator pIt = componentIndex.find("pixelId");
71  if (pIt == componentIndex.end()) {
72  if (IsReportingErrors())
73  ERROR("'pixelId' not found for " + QueryInfoMessage(componentProperty, componentName));
74  return eNotFound;
75  }
76 
77  const string& pixelId = pIt->second;
78 
79  ostringstream query;
80 
81  if (componentName == "fd_calib") {
82 
83  query << "SELECT " << componentProperty << " FROM pixel_calib "
84  "WHERE working_calib_id = " << GetWorkingCalibId(componentIndex) << " "
85  "AND pixel_number = " << pixelId;
86 
87  } else if (componentName == "low_gain_calib") {
88 
89  const int pixel = boost::lexical_cast<int>(pixelId);
90 
91  const unsigned int telId = boost::lexical_cast<unsigned int>(componentIndex.find("telescopeId")->second);
92  const fdet::Eye& detEye =
93  det::Detector::GetInstance().GetFDetector().GetEye(componentIndex.find("eyeId")->second);
94  const fdet::Telescope& detTel = detEye.GetTelescope(telId);
95 
96  query << "SELECT elec_gain FROM low_gain_channel "
97  "WHERE working_calib_id = " << GetWorkingCalibId(componentIndex) << " "
98  "AND channel_number = " << (pixel - detTel.GetLastPixelId());
99  // how are virtual channel mapped in the DB?
100 
101  } else {
102  ERROR(string("Can not handle call for ") + QueryInfoMessage(componentProperty, componentName));
103  return eNotFound;
104  }
105 
106  return (Query(query, QueryInfoMessage(componentProperty, componentName)) == eNotFound) ?
107  eNotFound : FetchRow(returnData);
108 
109  }
110  return eNotFound;
111 }
112 
113 
115 FCalibSQLManager::GetData(map<int, utl::TabulatedFunction>& returnData,
116  const string& componentProperty,
117  const string& componentName,
118  const IndexMap& componentIndex)
119  const
120 {
121  returnData.clear();
122 
123  if (componentProperty != "pixel_calibs") {
124  ERROR(string("Can not handle call for ") + QueryInfoMessage(componentProperty, componentName));
125  return eNotFound;
126  }
127 
128  if (fEyesToIgnore.size() > 0) {
129  unsigned int eyeId;
130  istringstream eyeIds(componentIndex.find("eyeId")->second);
131  eyeIds >> eyeId;
132 
133  for (vector<unsigned int>::const_iterator it = fEyesToIgnore.begin(); it != fEyesToIgnore.end(); it++) {
134  if (*it == eyeId)
135  return eNotFound;
136  }
137  }
138 
139  const int workingCalibId = GetWorkingCalibId(componentIndex);
140 
141  if (workingCalibId < 0) {
142  ERROR("Could not retrieve working_calib_id");
143  return eNotFound;
144  }
145 
146  ostringstream query;
147 
148  query << "SELECT pixel_number, wavelength, cal_const FROM pixel_wavelength_calib, pixel_calib "
149  "WHERE pixel_calib.working_calib_id = " << workingCalibId << " "
150  "AND pixel_wavelength_calib.pixel_calib_id = pixel_calib.pixel_calib_id";
151 
152  if (Query(query, QueryInfoMessage(componentProperty, componentName)) == eNotFound)
153  return eNotFound;
154 
155  const unsigned int numCols = NumFields();
156 
157  if (numCols != 3) {
158  ERROR("Found wrong number of rows in the pixel calibration query!");
159  return eNotFound;
160  }
161 
162  int curPix = -1;
163 
164  // prevent memory leaks
165  ShadowPtr<TabulatedFunction> calVsWavelength;
166 
167  boost::tuple<int, double, double> pixelNumber_wavelength_calConst;
168 
169  while (FetchNextRowMany(pixelNumber_wavelength_calConst) == eFound) {
170 
171  const int& pixelNumber = pixelNumber_wavelength_calConst.get<0>();
172  // Untangle the stream of data from the query into navigable structure
173  if (pixelNumber != curPix) {
174  if (calVsWavelength)
175  returnData.insert(make_pair(curPix, *calVsWavelength));
176 
177  calVsWavelength = new TabulatedFunction;
178  curPix = pixelNumber;
179  }
180 
181  const double& wavelength = pixelNumber_wavelength_calConst.get<1>();
182  const double& calConst = pixelNumber_wavelength_calConst.get<2>();
183  calVsWavelength->PushBack(wavelength*nanometer, calConst);
184 
185  }
186 
187  if (calVsWavelength)
188  returnData.insert(make_pair(curPix, *calVsWavelength));
189 
190  return eFound;
191 }
192 
193 
195 FCalibSQLManager::GetData(int& returnData,
196  const string& componentProperty,
197  const string& componentName,
198  const IndexMap& componentIndex)
199  const
200 {
201  // Retrieve validity interval for data
202  if (componentProperty != "start_time" && componentProperty != "end_time") {
203  ERROR(string("Can not handle call for ") + QueryInfoMessage(componentProperty, componentName));
204  return eNotFound;
205  }
206 
207  ostringstream query;
208  query << "SELECT " << componentProperty << " FROM working_calib "
209  "WHERE working_calib_id = " << GetWorkingCalibId(componentIndex);
210 
211  if (Query(query) == eNotFound)
212  return eNotFound;
213 
214  if (FetchRow(returnData) == eFound)
215  return eFound;
216 
217  query.str("");
218  query << "SELECT start_time "
219  "FROM working_calib, software, building_camera "
220  "WHERE working_calib.building_camera_id = building_camera.building_camera_id "
221  "AND building_camera.building_number = " << componentIndex.find("eyeId")->second << " "
222  "AND building_camera.camera_number = " << componentIndex.find("telescopeId")->second;
223 
224  if (!utl::StringEquivalent(fDatabaseSoftwareVersion, "default"))
225  query << " "
226  "AND working_calib.software_id = software.software_id "
227  "AND software.software_version = '" << fDatabaseSoftwareVersion << '\'';
228 
229  const TimeStamp detTime = Detector::GetInstance().GetTime();
230  query << " "
231  "AND working_calib.start_time > '"<< detTime.GetGPSSecond()<< "' "
232  "ORDER BY working_calib.start_time, working_calib.last_modified DESC "
233  "LIMIT 1";
234 
235  if (Query(query, QueryInfoMessage(componentProperty, componentName)) == eNotFound)
236  return eNotFound;
237 
238  if (FetchRow(returnData) == eNotFound)
239  // set time to "infinity"
240  returnData = UTCDateTime(2030, 1, 1).GetTimeStamp().GetGPSSecond();
241 
242  return eFound;
243 }
244 
245 
247 FCalibSQLManager::GetData(vector<int>& returnData,
248  const string& componentProperty,
249  const string& componentName,
250  const IndexMap& componentIndex)
251  const
252 {
253  if (componentProperty != "status" || componentName != "fd_calib")
254  return eNotFound;
255 
256  returnData.clear();
257 
258  ostringstream query;
259 
260  query << "SELECT pixel_number, status FROM pixel_calib "
261  "WHERE working_calib_id = " << GetWorkingCalibId(componentIndex)
262  << " ORDER BY pixel_number DESC";
263 
264  if (Query(query) == eNotFound)
265  return eNotFound;
266 
267  boost::tuple<int, int> pixelId_status;
268  if (FetchRowMany(pixelId_status, false) != eFound)
269  return eNotFound;
270 
271  const int maxPixelId = pixelId_status.get<0>();
272 
273  // missing pixels will have status -1
274  returnData.resize(maxPixelId + 1, -1);
275  returnData[maxPixelId] = pixelId_status.get<1>();
276 
277  while (FetchRowMany(pixelId_status, false) == eFound)
278  returnData[pixelId_status.get<0>()] = pixelId_status.get<1>();
279 
280  FreeResult();
281 
282  return eFound;
283 }
284 
285 
286 int
287 FCalibSQLManager::GetWorkingCalibId(const IndexMap& componentIndex)
288  const
289 {
290  const TimeStamp detTime = Detector::GetInstance().GetTime();
291 
292  // Compose the SQL query
293 
294  const string& eyeId = componentIndex.find("eyeId")->second;
295  const string& telescopeId = componentIndex.find("telescopeId")->second;
296 
297  // first get the working_calib_id
298 
299  ostringstream query;
300  query << "SELECT working_calib_id "
301  "FROM working_calib LEFT JOIN building_camera "
302  "ON working_calib.building_camera_id=building_camera.building_camera_id ";
303 
304  if (!utl::StringEquivalent(fDatabaseSoftwareVersion, "default"))
305  query << " LEFT JOIN software "
306  "ON working_calib.software_id = software.software_id ";
307 
308  query << "WHERE building_camera.building_number = " << eyeId << " "
309  "AND building_camera.camera_number = " << telescopeId <<" "
310  "AND working_calib.start_time <= \""<< detTime.GetGPSSecond() << "\" "
311  "AND (working_calib.end_time > \"" << detTime.GetGPSSecond() << "\" "
312  "OR working_calib.end_time IS NULL) ";
313 
314  if (!utl::StringEquivalent(fDatabaseSoftwareVersion, "default"))
315  query << "AND software.software_version = \"" << fDatabaseSoftwareVersion << '"';
316 
317  query << " ORDER BY working_calib.last_modified DESC, working_calib.start_time DESC, working_calib_id DESC "
318  "LIMIT 1";
319 
320  int ret = -1;
321 
322  /*
323  if (Query(query) == eNotFound || FetchRow(ret) == eNotFound)
324  throw DataNotFoundInDBException("working_calib_id", "working_calib",
325  componentIndex, boost::lexical_cast<string>(detTime));
326  */
327 
328 
329  if ( Query(query) != eNotFound )
330  FetchRow(ret);
331 
332  return ret;
333 }
334 
335 
336 // ATTENTION: to be modified.
337 // returns 0 if query fails
338 int
339 FCalibSQLManager::GetPixelTOffsetId(const IndexMap& componentIndex)
340  const
341 {
342  const int workingCalibId = GetWorkingCalibId(componentIndex);
343  const string& pixelId = componentIndex.find("pixelId")->second;
344 
345  ostringstream query;
346  query << "SELECT pixel_t_offset_id FROM pixel_calib "
347  "WHERE working_calib_id = " << workingCalibId << " "
348  "AND pixel_number = " << pixelId;
349 
350  int result = 0;
351  if (Query(query, QueryInfoMessage("pixel_calib", "pixel_t_offset_id")) == eFound)
352  FetchRow(result);
353 
354  return result;
355 }
356 
357 
358 std::vector<double>
359 FCalibSQLManager::FindAvailableWavelengths(const int pixelCalibId)
360  const
361 {
362  ostringstream query;
363  query << "SELECT wavelength FROM pixel_wavelength_calib "
364  "WHERE pixel_calib_id = " << pixelCalibId;
365 
366  vector<double> retWavelengths;
367 
368  if (Query(query, QueryInfoMessage("pixel_wavelength_calib", "wavelength")) == eFound)
369  FetchColumn(retWavelengths);
370 
371  return retWavelengths;
372 }
373 
374 
375 // Configure (x)emacs for this file ...
376 // Local Variables:
377 // mode: c++
378 // compile-command: "make -C .. FDetector/FCalibSQLManager.o -k"
379 // End:
pointer with built-in initialization, deletion, deep copying
Definition: ShadowPtr.h:163
Class to hold collection (x,y) points and provide interpolation between them.
void Init()
Initialise the registry.
Detector description interface for Eye-related data.
Definition: FDetector/Eye.h:45
void PushBack(const double x, const double y)
bool StringEquivalent(const std::string &a, const std::string &b, Predicate p)
Utility to compare strings for equivalence. It takes a predicate to determine the equivalence of indi...
Definition: StringCompare.h:38
A TimeStamp holds GPS second and nanosecond for some event.
Definition: TimeStamp.h:110
constexpr double nanometer
Definition: AugerUnits.h:102
unsigned int GetLastPixelId() const
const Data result[]
Detector description interface for Telescope-related data.
unsigned long GetGPSSecond() const
GPS second.
Definition: TimeStamp.h:124
#define REGISTER_F_MANAGER(_name_, _Type_)
std::map< std::string, std::string > IndexMap
Definition: VManager.h:133
#define ERROR(message)
Macro for logging error messages.
Definition: ErrorLogger.h:165
TimeStamp GetTimeStamp() const
Definition: UTCDateTime.cc:115
Status
Specifies success or (eventually) various possible failure modes.
Definition: VManager.h:127
Manager for FD calibration and monitoring data residing in MySQL database.

, generated on Tue Sep 26 2023.