RChannelSQLManager.cc
Go to the documentation of this file.
1 #include <boost/lambda/lambda.hpp>
2 
3 #include <det/VManager.h>
4 
5 #include <rdet/RChannelSQLManager.h>
6 #include <rdet/RDetector.h>
7 #include <rdet/RManagerRegister.h>
8 #include <rdet/RSimulationStationListManager.h>
9 
10 #include <utl/ErrorLogger.h>
11 #include <utl/UTCDateTime.h>
12 #include <utl/TimeStamp.h>
13 #include <utl/TabulatedFunctionComplexLgAmpPhase.h>
14 #include <utl/MathConstants.h>
15 #include <utl/LeapSeconds.h>
16 
17 using namespace rdet;
18 using namespace std;
19 using namespace utl;
20 using namespace det;
21 
22 
23 REGISTER_R_MANAGER("RChannelSQLManager", RChannelSQLManager);
24 
25 
26 namespace rdet {
27 
28  void
29  RChannelSQLManager::Init(const string& configLink)
30  {
31  VSQLManager::Init(configLink);
32  }
33 
34 
35  void
37  const
38  {
39  const TimeStamp& curTime = Detector::GetInstance().GetTime();
40 
41  ostringstream query;
42  query << "select MaxVoltage, MinVoltage, SamplingFrequency, BitDepth, ChannelType, PositionX, PositionY, PositionZ, "
43  "DesignUpperFreq, DesignLowerFreq, AntennaType, OrientationAzimuth, OrientationZenith, OrientationZeta, RADCId "
44  "from RHardWareAssociation left join RADC using (RADCId) left join RChannel using (RChannelId) "
45  "where RStationId=" << componentIndex.find("station")->second << " "
46  "and ChannelId=" << componentIndex.find("channel")->second << " "
47  "and Commission<\"" << UTCDateTime(curTime).GetInMySQLFormat() << "\" "
48  "and Decommission>\"" << UTCDateTime(curTime).GetInMySQLFormat() << "\";";
49 
50  if (Query(query.str(), QueryInfoMessage("Channel", "Channel")) == eNotFound) {
51  const string err = "No station_list SQL data found! Query: " + query.str();
52  ERROR(err);
53  throw DataNotFoundInDBException(err);
54  }
55  //station.fEllipsoid = IdElZoBaNoEaAlNaIGNCFCLCCTDT[1];
56  fComponentMap.clear();
57 
58  // have to get the field-names as well! Therefore, cannot directly fill the map.
59  // workaround with explicit list for now, since a general solution need change in Interface
60  ostringstream out;
61  vector<string> sqlResult;
62  while (FetchRow(sqlResult, false) == eFound) {
63  fComponentMap.insert(pair<string, string>("MaxVoltage", sqlResult[0]));
64  fComponentMap.insert(pair<string, string>("MinVoltage", sqlResult[1]));
65  fComponentMap.insert(pair<string, string>("SamplingFrequency", sqlResult[2]));
66  fComponentMap.insert(pair<string, string>("BitDepth", sqlResult[3]));
67  fComponentMap.insert(pair<string, string>("ChannelType", sqlResult[4]));
68  fComponentMap.insert(pair<string, string>("PositionX", sqlResult[5]));
69  fComponentMap.insert(pair<string, string>("PositionY", sqlResult[6]));
70  fComponentMap.insert(pair<string, string>("PositionZ", sqlResult[7]));
71  fComponentMap.insert(pair<string, string>("DesignUpperFreq", sqlResult[8]));
72  fComponentMap.insert(pair<string, string>("DesignLowerFreq", sqlResult[9]));
73  fComponentMap.insert(pair<string, string>("AntennaType", sqlResult[10]));
74  fComponentMap.insert(pair<string, string>("OrientationAzimuth", sqlResult[11]));
75  fComponentMap.insert(pair<string, string>("OrientationZenith", sqlResult[12]));
76  fComponentMap.insert(pair<string, string>("OrientationZeta", sqlResult[13]));
77  fComponentMap.insert(pair<string, string>("ADCType", sqlResult[14]));
78 
79  fIndexMap = componentIndex;
80  }
81  }
82 
83 
84  void
86  const
87  {
88  const TimeStamp& curTime = Detector::GetInstance().GetTime();
89  ostringstream info;
90  info << "Request RChannel component SQL Data for time: " << curTime;
91  INFO(info);
92 
93  ostringstream query;
94  query << "select MaxVoltage, MinVoltage, SamplingFrequency, BitDepth, ChannelType, PositionX, PositionY, PositionZ, "
95  "DesignUpperFreq, DesignLowerFreq, AntennaType, OrientationAzimuth, OrientationZenith, OrientationZeta, RADCId, "
96  "RStationId, ChannelId, UNIX_TIMESTAMP(Commission), UNIX_TIMESTAMP(Decommission) "
97  "from RHardWareAssociation left join RADC using (RADCId) left join RChannel using (RChannelId) "
98  "where Commission<\"" << UTCDateTime(curTime).GetInMySQLFormat() << "\" "
99  "and Decommission>\"" << UTCDateTime(curTime).GetInMySQLFormat() << "\";";
100 
101  if (Query(query.str(), QueryInfoMessage("Channel", "Channel")) == eNotFound) {
102  const string err = "No station_list SQL data found! Query: " + query.str();
103  ERROR(err);
104  throw DataNotFoundInDBException(err);
105  }
106  fBuffer.clear();
107 
108  vector<string> sqlResult;
109  while (FetchRow(sqlResult, false) == eFound) {
110  fComponentMap.clear();
111  fComponentMap.insert(pair<string, string>("MaxVoltage", sqlResult[0]));
112  fComponentMap.insert(pair<string, string>("MinVoltage", sqlResult[1]));
113  fComponentMap.insert(pair<string, string>("SamplingFrequency", sqlResult[2]));
114  fComponentMap.insert(pair<string, string>("BitDepth", sqlResult[3]));
115  fComponentMap.insert(pair<string, string>("ChannelType", sqlResult[4]));
116  fComponentMap.insert(pair<string, string>("PositionX", sqlResult[5]));
117  fComponentMap.insert(pair<string, string>("PositionY", sqlResult[6]));
118  fComponentMap.insert(pair<string, string>("PositionZ", sqlResult[7]));
119  fComponentMap.insert(pair<string, string>("DesignUpperFreq", sqlResult[8]));
120  fComponentMap.insert(pair<string, string>("DesignLowerFreq", sqlResult[9]));
121  fComponentMap.insert(pair<string, string>("AntennaType", sqlResult[10]));
122  fComponentMap.insert(pair<string, string>("OrientationAzimuth", sqlResult[11]));
123  fComponentMap.insert(pair<string, string>("OrientationZenith", sqlResult[12]));
124  fComponentMap.insert(pair<string, string>("OrientationZeta", sqlResult[13]));
125  fComponentMap.insert(pair<string, string>("ADCType", sqlResult[14]));
126 
127  fComponentMap.insert(pair<string, string>("Commission", sqlResult[17]));
128  fComponentMap.insert(pair<string, string>("Decommission", sqlResult[18]));
129 
130  Index componentIndex (sqlResult[15], sqlResult[16]);
131  fBuffer.insert(std::pair<Index, ComponentMap>(componentIndex, fComponentMap));
132  }
133  }
134 
135 
138  const Index& currentComponent,
139  const std::string& componentProperty)
140  const
141  {
142  // search for current channel in buffer
143  std::map<Index, ComponentMap>::const_iterator it = fBuffer.find(currentComponent);
144  if (it == fBuffer.end()) {
145  ostringstream out;
146  out << "Channel " <<currentComponent.second << " of station " << currentComponent.first
147  << " not contained in buffer -> buffering ...";
148  INFO(out);
149  BufferComponentMap();
150  it = fBuffer.find(currentComponent);
151  }
152 
153  if (it != fBuffer.end()) {
154  const utl::TimeStamp& curTime = det::Detector::GetInstance().GetTime();
155  ComponentMap componentMapofCurrentChannel = it->second;
156 
157  utl::LeapSeconds leapSeconds;
158  unsigned long gpsSecondsComission = 0;
159  unsigned long gpsSecondsDecomission = 0;
160  leapSeconds.ConvertUnixToGPS(
161  long(boost::lexical_cast<double>(componentMapofCurrentChannel.find("Commission")->second)),
162  gpsSecondsComission
163  );
164  leapSeconds.ConvertUnixToGPS(
165  long(boost::lexical_cast<double>(componentMapofCurrentChannel.find("Decommission")->second)),
166  gpsSecondsDecomission
167  );
168 
169  if (!(gpsSecondsComission <= curTime.GetGPSSecond() &&
170  gpsSecondsDecomission > curTime.GetGPSSecond())) {
171  ostringstream out;
172  out << "time of channel " <<currentComponent.second << " of station " << currentComponent.first
173  << " not valid anymore -> rebuffer" << std::endl;
174  INFO(out);
175  BufferComponentMap();
176  }
177 
178  it = fBuffer.find(currentComponent);
179  if (it != fBuffer.end()) {
180  componentMapofCurrentChannel = it->second;
181  ComponentMap::const_iterator itProperty = componentMapofCurrentChannel.find(componentProperty);
182  if (itProperty != componentMapofCurrentChannel.end()) {
183  returnData = itProperty->second;
184  return eFound;
185  }
186  }
187  }
188  ostringstream out;
189  out << "time of channel " <<currentComponent.second << " of station " << currentComponent.first
190  << "no " << componentProperty << " found";
191  INFO(out);
192  return eNotFound;
193  }
194 
195 
196  void
198  const
199  {
200  const TimeStamp& curTime = Detector::GetInstance().GetTime();
201  ostringstream info;
202  info << "Request RChannel SQL Data for time: " << curTime;
203  INFO(info);
204 
205  ostringstream query;
206  query << "select RStationId, ChannelId, UNIX_TIMESTAMP(Commission), UNIX_TIMESTAMP(Decommission) "
207  "from RHardWareAssociation left join RChannel using (RChannelId) "
208  "where Commission<\"" << UTCDateTime(curTime).GetInMySQLFormat() << "\" "
209  "and Decommission>\"" << UTCDateTime(curTime).GetInMySQLFormat() << "\" "
210  "ORDER BY RStationId asc, ChannelId asc;";
211 
212  if (Query(query.str(), QueryInfoMessage("Channel", "Channel")) == eNotFound) {
213  const string err = "No RChannel SQL data found! Query: " + query.str();
214  ERROR(err);
215  throw DataNotFoundInDBException(err);
216  }
217 
218  fBufferChannelIdLists.clear();
219 
220  vector<string> sqlResult;
221  while (FetchRow(sqlResult, false) == eFound) {
222  const int stationId = boost::lexical_cast<int>(sqlResult[0]);
223  const int channelId = boost::lexical_cast<int>(sqlResult[1]);
224  utl::LeapSeconds leapSeconds;
225  unsigned long gpsSecondsComission = 0;
226  unsigned long gpsSecondsDecomission = 0;
227  leapSeconds.ConvertUnixToGPS(boost::lexical_cast<unsigned long>(sqlResult[2]), gpsSecondsComission);
228  leapSeconds.ConvertUnixToGPS(boost::lexical_cast<unsigned long>(sqlResult[3]), gpsSecondsDecomission);
229 
230  ChannelIdList& ch = fBufferChannelIdLists[stationId];
231  ch.fGpsSecondComission = gpsSecondsComission;
232  ch.fGpsSecondDecomission = gpsSecondsDecomission;
233  ch.fChannelIds.push_back(channelId);
234  }
235  }
236 
237 
239  RChannelSQLManager::GetChannelListFromBuffer(std::vector<int>& returnData,
240  const std::string& componentProperty,
241  const std::string& componentName,
242  const IndexMap& componentIndex)
243  const
244  {
245  if (!(componentName == "RChannel" && componentProperty == "listOfChannelIds"))
246  return eNotFound;
247 
248  // returns eNotFound if the station description is not suppose to be from SQL
249  if (RSimulationStationListManager::GetSourceType(componentIndex.find("station")->second) != "SQL")
250  return eNotFound;
251 
252  const int stationId = boost::lexical_cast<int>(componentIndex.find("station")->second);
253  map<int, ChannelIdList>::const_iterator it = fBufferChannelIdLists.find(stationId);
254  if (it == fBufferChannelIdLists.end()) {
255  BufferChannelList();
256  it = fBufferChannelIdLists.find(stationId);
257  }
258  if (it != fBufferChannelIdLists.end()) {
259  const TimeStamp& curTime = Detector::GetInstance().GetTime();
260 
261  if (!(it->second.fGpsSecondComission <= curTime.GetGPSSecond() &&
262  it->second.fGpsSecondDecomission > curTime.GetGPSSecond())) {
263  INFO("Time of station list not valid -> rebuffering");
264  BufferChannelList();
265  it = fBufferChannelIdLists.find(stationId);
266  }
267  if (it != fBufferChannelIdLists.end()) {
268  returnData = it->second.fChannelIds;
269  //std::cout << "Channels for station " << stationId << ": ";
270  //for (int i = 0; i<returnData.size(); ++i) {
271  // std::cout << returnData[i] << " ";
272  //}
273  //std::cout << std::endl;
274  return eFound;
275  }
276  }
277 
278  ostringstream out;
279  out << "Station " << stationId << " not found";
280  INFO(out);
281  return eNotFound;
282  }
283 
284 
286  RChannelSQLManager::GetDataMap(std::map<std::string, double>& returnData,
287  const std::string& /*componentProperty*/,
288  const std::string& /*componentName*/,
289  const VManager::IndexMap& componentIndex)
290  const
291  {
292  // returns eNotFound if the station description is not suppose to be from SQL
293  if (RSimulationStationListManager::GetSourceType(componentIndex.find("station")->second) != "SQL")
294  return VManager::eNotFound;
295 
296  const TimeStamp& curTime = Detector::GetInstance().GetTime();
297 
298  //jr RResponseMapList has indefinit number of entries.
299  // Since RHardWareAssociation has also indefinit entries for one station/channel
300  // we need two queries, first retrieving the right RResponseMapListId, then the Map.
301  ostringstream query;
302  query << "select RResponseMapListId from RHardWareAssociation "
303  "where RStationId=" << componentIndex.find("station")->second << " "
304  "and ChannelId=" << componentIndex.find("channel")->second << " "
305  "and Commission<\"" << UTCDateTime(curTime).GetInMySQLFormat() << "\" "
306  "and Decommission>\"" << UTCDateTime(curTime).GetInMySQLFormat() << "\";";
307 
308  if (Query(query.str(), QueryInfoMessage("RResponseMapListId", "ResponseMap")) == eNotFound) {
309  const string err = "No station_list SQL data found! Query: " + query.str();
310  throw DataNotFoundInDBException(err);
311  }
312 
313  vector<string> sqlResult;
314  FetchRow(sqlResult, false);
315 
316  // second query -> map
317  query.str("");
318  query << "select ResponseId, ResponseWeight from ResponseMapList "
319  "where RResponseMapListId=" << "\"" << sqlResult[0] << "\";";
320  if (Query(query.str(), QueryInfoMessage("ResponseId, ResponseWeight", "ResponseMap")) == eNotFound) {
321  const string err = "No station_list SQL data found! Query: " + query.str();
322  throw DataNotFoundInDBException(err);
323  }
324 
325  returnData.clear();
326  while (FetchRow(sqlResult, false) == eFound) {
327  returnData.insert(pair<string, double>(sqlResult[0], boost::lexical_cast<double>(sqlResult[1])));
328  }
329 
330  if (returnData.empty())
331  return VManager::eNotFound;
332 
333  return VManager::eFound;
334  }
335 
336 
339  const std::string& componentProperty,
340  const std::string& componentName,
341  const IndexMap& /*componentIndex*/)
342  const
343  {
344  // jr so far only used for RResponse
345  if (componentProperty != "Response")
346  return eNotFound;
347 
348  returnData = TabulatedFunctionComplexLgAmpPhase();
349  ostringstream query;
350  query << "select x, dB, Phase from RResponse "
351  "where ResponseId=\"" << componentName << "\" "
352  "ORDER BY x asc;";
353  if (Query(query.str(), QueryInfoMessage("RResponse", "RResponse")) == eNotFound) {
354  const string err = "No station_list SQL data found! Query: " + query.str();
355  throw DataNotFoundInDBException(err);
356  }
357  vector<string> sqlResult;
358  while (FetchRow(sqlResult, false) == eFound) {
359  //jr: in DB stored in dB and rad (auger-unit), so need to convert to log10(Amp)
360  returnData.PushBack(boost::lexical_cast<float>(sqlResult[0]),
361  ComplexLgAmpPhase(boost::lexical_cast<float>(sqlResult[1])*0.05,
362  boost::lexical_cast<float>(sqlResult[2])));
363  }
364  if (returnData.GetNPoints())
365  return VManager::eFound;
366  //std::cout << "TFCLAPData not found" << std::endl;
367  return VManager::eNotFound;
368  }
369 
370 }
371 
372 
std::pair< std::string, std::string > Index
#define INFO(message)
Macro for logging informational messages.
Definition: ErrorLogger.h:161
Status GetDataMap(std::map< std::string, double > &returnData, const std::string &componentProperty, const std::string &componentName, const IndexMap &componentIndex) const
std::string GetInMySQLFormat() const
Definition: UTCDateTime.cc:107
void Init()
Initialise the registry.
vector< t2list > out
output of the algorithm: a list of clusters
Definition: XbAlgo.cc:32
A TimeStamp holds GPS second and nanosecond for some event.
Definition: TimeStamp.h:110
#define REGISTER_R_MANAGER(_name_, _Type_)
Exception to use in case requested data not found in the database with detailed printout.
VManager::Status GetComponentFromBuffer(std::string &returnData, const Index &componentIndex, const std::string &componentProperty) const
Class to hold collection (x,y) points and provide interpolation between them, where y are complex num...
void Init(const std::string &configLink)
Manager Initialization. configLink is the CentralConfig hook for the configuration file...
Status GetTFCLAPData(utl::TabulatedFunctionComplexLgAmpPhase &returnData, const std::string &componentProperty, const std::string &componentName, const IndexMap &componentIndex) const
std::map< std::string, std::string > ComponentMap
A class to store complex numbers which are internally represented by log10(amplitude) and phase (and ...
void FillComponentMap(const IndexMap &componentIndex) const
unsigned long GetGPSSecond() const
GPS second.
Definition: TimeStamp.h:124
void PushBack(const double x, const utl::ComplexLgAmpPhase &y)
std::map< std::string, std::string > IndexMap
Definition: VManager.h:133
Status GetChannelListFromBuffer(std::vector< int > &returnData, const std::string &componentProperty, const std::string &componentName, const IndexMap &componentIndex) const
Manager for RD description of Station-Channels in SQL DB.
#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
void ConvertUnixToGPS(const time_t unixSecond, unsigned long &gpsSecond) const
Definition: LeapSeconds.cc:25
static std::string GetSourceType(const std::string stationIdstring)

, generated on Tue Sep 26 2023.