FdUpTimeSQLManager.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/FdUpTimeSQLManager.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 
18 #include <fdet/FManagerRegister.h>
19 #include <fdet/FDetector.h>
20 #include <fdet/Eye.h>
21 #include <fdet/Telescope.h>
22 
23 using namespace std;
24 using namespace fdet;
25 using namespace utl;
26 using namespace det;
27 using namespace fwk;
28 
29 
30 REGISTER_F_MANAGER("FdUpTimeSQLManager", FdUpTimeSQLManager);
31 
32 
33 void
34 FdUpTimeSQLManager::Init(const std::string& configLink)
35 {
36 
37  VManager::Init(configLink);
38 
39  fVerbosity = 0;
40  if (fBranch.GetChild("verbosity")) {
41  fBranch.GetChild("verbosity").GetData(fVerbosity);
42  cout << " FdUpTimeSQLeManager verbosity level = " << fVerbosity << endl;
43  }
44 
45 
46  // list of properties for telescope
47  fAvailableComponents.insert("has_fduptime");
48  fAvailableComponents.insert("uptime_gpsStart");
49  fAvailableComponents.insert("uptime_gpsStop");
50  fAvailableComponents.insert("uptime_fraction");
51  fAvailableComponents.insert("status");
52  fAvailableComponents.insert("ADCVariance"); // for channel
53  fAvailableComponents.insert("Threshold"); // for channel
54  fAvailableComponents.insert("Baseline"); // for channel
55 
56 
57 }
58 
59 VManager::Status FdUpTimeSQLManager::GetData(double& returnData,
60  const std::string& componentProperty,
61  const std::string& componentName,
62  const IndexMap& componentIndex) const {
63 
64 
65  // check if property can be provided by manager
66  if (fAvailableComponents.find(componentProperty) == fAvailableComponents.end())
67  return VManager::eNotFound;
68 
69  if (!ReadData()) {
70  ERROR("******************************************");
71  ERROR("* Index not found (gps-second) *");
72  ERROR("******************************************");
73  return VManager::eNotFound;
74  }
75 
76  if (fVerbosity>2) {
77  ostringstream info;
78  info << "called GetData(double) for componentName=" << componentName
79  << " componentProperty=" << componentProperty << " and index map: \n" ;
80  for (IndexMap::const_iterator imap = componentIndex.begin();
81  imap!=componentIndex.end(); imap++) {
82  info << " index=\"" << imap->first << "\" -> \"" << imap->second << "\"\n";
83  }
84  INFO(info);
85  }
86 
87  if (componentProperty == "has_fduptime") {
88  returnData = 1;
89  return VManager::eFound;
90  }
91  if (componentName=="channel") {
92 
93  // channel property
94  return GetChannelData(returnData, componentProperty, componentName, componentIndex);
95 
96  } else if(componentName=="telescope" || componentName=="eye" || componentName=="CDAS") {
97 
98  // eye, tel, cdas property
99  return GetOnTimeFraction(returnData, componentProperty, componentName, componentIndex);
100 
101  }
102 
103  return VManager::eNotFound;
104 }
105 
106 
107 VManager::Status FdUpTimeSQLManager::GetOnTimeFraction(double& returnData,
108  const std::string& componentProperty,
109  const std::string& componentName,
110  const IndexMap& componentIndex) const {
111 
112 
113  if (fVerbosity>2) {
114  ostringstream info;
115  info << "called GetOnTimeFraction for componentName=" << componentName
116  << " componentProperty=" << componentProperty << " and index map: \n" ;
117  for (IndexMap::const_iterator imap = componentIndex.begin();
118  imap!=componentIndex.end(); imap++) {
119  info << " index=\"" << imap->first << "\" -> \"" << imap->second << "\"\n";
120  }
121  INFO(info);
122  }
123 
124  if (componentProperty=="uptime_fraction") {
125 
126  if (componentName=="telescope") {
127 
128  int dataId = GetTelIndex(componentIndex);
129  map<int, double>::const_iterator iData = fTelescopeOnTimeFraction.find(dataId);
130  if (iData == fTelescopeOnTimeFraction.end()) {
131  INFO_TERSE("Requested property of FdUpTimeSQLManager not available!");
132  return VManager::eNotFound;
133  }
134 
135  returnData = iData->second;
136  return VManager::eFound;
137  }
138 
139 
140  if (componentName=="eye") {
141 
142  int eyeId = GetEyeIndex(componentIndex);
143  map<int, double>::const_iterator iData = fEyeOnTimeFraction.find(eyeId);
144  if (iData == fEyeOnTimeFraction.end()) {
145  INFO_TERSE("Requested property of FdUpTimeSQLManager not available!");
146  return VManager::eNotFound;
147  }
148 
149  returnData = iData->second;
150  return VManager::eFound;
151  }
152 
153 
154  if (componentName=="CDAS") {
155 
156  returnData = fCDASOnTimeFraction;
157  return VManager::eFound;
158  }
159  }
160 
161  return VManager::eNotFound;
162 }
163 
164 
165 
166 int FdUpTimeSQLManager::GetEyeIndex(const IndexMap& componentIndex) const {
167 
168  IndexMap::const_iterator iEyeId;
169  iEyeId = componentIndex.find("eyeId");
170  if (iEyeId == componentIndex.end()) {
171 
172  INFO_TERSE("Called FdUpTimeSQLManager with wrong number of arguments!");
173  return -1;
174  }
175 
176  int eyeId;
177  istringstream ssEyeId(iEyeId->second);
178  ssEyeId >> eyeId;
179 
180  return eyeId;
181 }
182 
183 
184 int FdUpTimeSQLManager::GetTelIndex(const IndexMap& componentIndex) const {
185 
186  IndexMap::const_iterator iEyeId, iTelId;
187  iEyeId = componentIndex.find("eyeId");
188  iTelId = componentIndex.find("telescopeId");
189  if (iEyeId == componentIndex.end() ||
190  iTelId == componentIndex.end()) {
191 
192  INFO_TERSE("Called FdUpTimeSQLManager with wrong number of arguments!");
193  return -1;
194  }
195 
196  int eyeId, telId;
197  istringstream ssEyeId(iEyeId->second);
198  ssEyeId >> eyeId;
199  istringstream ssTelId(iTelId->second);
200  ssTelId >> telId;
201  int dataId = (eyeId-1) * 6 + telId;
202 
203  return dataId;
204 }
205 
206 int FdUpTimeSQLManager::GetChannelIndex(const IndexMap& componentIndex) const {
207 
208  IndexMap::const_iterator iChannelId;
209  iChannelId = componentIndex.find("channelId");
210  if (iChannelId == componentIndex.end()) {
211  INFO_TERSE("Called FdUpTimeSQLManager with wrong number of arguments!");
212  return -1;
213  }
214 
215  int channelId;
216  istringstream ssChannelId(iChannelId->second);
217  ssChannelId >> channelId;
218 
219  return channelId;
220 }
221 
222 
223 
225 FdUpTimeSQLManager::GetChannelData(double& returnData,
226  const std::string& componentProperty,
227  const std::string&,
228  const IndexMap& componentIndex)
229  const
230 {
231  // index is linear --> ((eyeId-1) * 6 + telId) * 440 + channelId;
232  // int dataId = GetChannelIndex(componentIndex);
233 
234  if (fVerbosity>2) {
235  ostringstream info;
236  info << "called GetChannelData with componentProperty=" << componentProperty << " and index map: \n" ;
237  for (IndexMap::const_iterator imap = componentIndex.begin();
238  imap!=componentIndex.end(); imap++) {
239  info << " index=\"" << imap->first << "\" -> \"" << imap->second << "\"\n";
240  }
241  INFO(info);
242  }
243 
244  int dataId = GetTelIndex(componentIndex);
245  int channelId = GetChannelIndex(componentIndex);
246 
247  if (fVerbosity>2) {
248  ostringstream info;
249  info << " (eye/tel) dataId=" << dataId << " channelId=" << channelId
250  << " fChannelInfo.size()=" << fChannelInfo.size() << " # tels " << endl;
251  INFO(info);
252  }
253 
254  map<int, vector<ChannelInfo > >::const_iterator iData = fChannelInfo.find(dataId);
255  if (iData == fChannelInfo.end()) {
256  INFO_TERSE("Requested property of FdUpTimeSQLManager not available!");
257  return VManager::eNotFound;
258  }
259 
260  const vector<ChannelInfo>& info = iData->second;
261 
262  if (fVerbosity>2) {
263  cout << " channel vector size=" << info.size() << endl;
264  }
265 
266  channelId -= 1; // index starts at 0
267  returnData = 0;
268  if( (unsigned int)channelId < info.size() ) {
269 
270  if (componentProperty=="ADCVariance") returnData = info[channelId].Variance;
271  else if (componentProperty=="Threshold") returnData = info[channelId].Threshold;
272  else if (componentProperty=="Baseline") returnData = info[channelId].Baseline;
273  else return VManager::eNotFound;
274  }
275 
276  if (fVerbosity>2) {
277  cout << " VAR=" << returnData << endl;
278  }
279 
280  return VManager::eFound;
281 
282 }
283 
284 
285 
286 
287 
288  bool FdUpTimeSQLManager::ReadData() const {
289 
290 
291  if (fVerbosity>2) {
292  INFO("enter ReadData ");
293  }
294 
295  if (fDataValidity.IsValid()) {
296  if (fVerbosity>2) {
297  INFO("Data is still valid ");
298  }
299  return true;
300  }
301 
302  unsigned int now = det::Detector::GetInstance().GetTime().GetGPSSecond();
303 
304  // really get the data from the TTree
305  fEyeOnTimeFraction.clear();
306  fTelescopeOnTimeFraction.clear();
307  fEyeStatus.clear();
308  fTelescopeStatus.clear();
309  fCDASStatus=0;
310  fCDASOnTimeFraction=0.;
311  fChannelInfo.clear();
312 
313 
314  bool foundit = true;
315 
316  ostringstream query;
317  query << "SELECT * FROM UptimeTelescope as a, UptimeBin as b WHERE b.GPSStart >= " << now << " AND b.GPSStop < " << now;
318 
319  //Query(query);
320 
321 
322 
323  // eNotFound : FetchRow(returnData
324 
325 // MYSQL *connMonit = mysql_init(NULL);
326 // if (!mysql_real_connect(connMonit, server,
327 // user, password,dbnames[4], 0, NULL, 0)) {
328 // fprintf(stderr, "%s\n", mysql_error(connMonit));
329 // exit(0);
330 // }
331 
332 // ostringstream query;
333 // query << "SELECT * FROM upTimes WHERE " << "uptime_start_time <= \"" << now << "\" AND " << "uptime_end_time > \"" << now << "\"";
334 
335 // if (fVerbosity>2) cout << "Query:\t" << query << endl;
336 
337 // if ( mysql_query(conn[ieye], queryFirst) ) {
338 // if (verbose) cout << "Error with query:\t" << queryFirst << endl;
339 // } else {
340 // totRes = mysql_store_result(conn[ieye]);
341 // if ( ! mysql_num_rows(totRes) )
342 // if (verbose) {
343 // cout << "Warning: query returned empty result \t" << queryFirst << endl;
344 // lastTime[currt] = start + TIMEWINDOW;
345 // }
346 
347 // while ((dtRow = mysql_fetch_row(totRes)) != NULL) {
348 
349 // countme++;
350 
351 
352 // lastDT[currt] = atof(dtRow[0]);
353 // lastRun[currt] = atoi(dtRow[1]);
354 // lastVeto[currt] = atof(dtRow[4]);
355 
356 
357 
358 // TSQLResult *result = fDataTree->Query(query.str().c_str());
359 // if (result->GetRowCount()) {
360 // INFO_TERSE("FdUpTimeSQLManager cannot identify row");
361 // }
362 
363 // TSQLRow *row = result->Next();
364 
365 
366 // row
367 
368 
369 
370 
371 
372 // for (int iEye=1; iEye<=5; iEye++) {
373 
374 // int nTel = 6;
375 // if (iEye==5) nTel = 3;
376 
377 // for (int iTel=1; iTel<=nTel; iTel++) {
378 
379 // int telIndex = (iEye-1)*6 + iTel;
380 
381 // fTelescopeOnTimeFraction[telIndex] = tel_up_time_fraction[telIndex-1];
382 // fTelescopeStatus[telIndex] = tel_status[telIndex-1];
383 
384 // fChannelInfo[telIndex].clear();
385 // for(unsigned int channel=0; channel<kNPixelPerTel; channel++) {
386 // fChannelInfo[telIndex].push_back(ChannelInfo(channel_var[telIndex-1][channel], // VAR
387 // channel_threshold[telIndex-1][channel], // threshold
388 // channel_baseline[telIndex-1][channel])); // baseline
389 // }
390 // }
391 
392 // fEyeStatus[iEye] = eye_status[iEye-1];
393 // fEyeOnTimeFraction[iEye] = eye_up_time_fraction[iEye-1];
394 // }
395 
396 // fCDASStatus=CDAS_status;
397 // fCDASOnTimeFraction=CDAS_up_time_fraction;
398 
399 // fDataValidity.SetValidityInterval(TimeStamp(time_start), TimeStamp(time_stop));
400 
401 // }
402 
403 // if (fVerbosity>2) {
404 // cout << " ReadData result is valid from start: "
405 // << fDataValidity.GetStartTime().GetGPSSecond()
406 // << " end: " << fDataValidity.GetStopTime().GetGPSSecond()
407 // << endl;
408 // }
409 
410 
411  return foundit;
412 
413 // /*
414 // */
415  }
416 
417 // Configure (x)emacs for this file ...
418 // Local Variables:
419 // mode: c++
420 // compile-command: "make -C .. FDetector/FdUpTimeSQLManager.o -k"
421 // End:
#define INFO_TERSE(message)
Macro for logging informational messages.
Definition: ErrorLogger.h:175
#define INFO(message)
Macro for logging informational messages.
Definition: ErrorLogger.h:161
void Init()
Initialise the registry.
#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
Status
Specifies success or (eventually) various possible failure modes.
Definition: VManager.h:127

, generated on Tue Sep 26 2023.