RBeaconSQLManager.cc
Go to the documentation of this file.
1 #include <iomanip>
2 #include <boost/lambda/lambda.hpp>
3 
4 #include <rdet/RBeaconSQLManager.h>
5 #include <utl/ErrorLogger.h>
6 #include <rdet/RDetector.h>
7 #include <rdet/RManagerRegister.h>
8 #include <det/VManager.h>
9 #include <utl/UTCDateTime.h>
10 #include <utl/TimeStamp.h>
11 #include <utl/LeapSeconds.h>
12 #include <utl/TabulatedFunctionComplexLgAmpPhase.h>
13 #include <utl/MathConstants.h>
14 
15 using namespace rdet;
16 using namespace std;
17 using namespace utl;
18 using namespace det;
19 
20 REGISTER_R_MANAGER("RBeaconSQLManager", RBeaconSQLManager)
21 
22 namespace rdet {
23 
24  void
25  RBeaconSQLManager::Init(const string& configLink)
26  {
27  VSQLManager::Init(configLink);
28  }
29 
31  RBeaconSQLManager::InternalGetDataVecDouble(std::vector<double>& returnData,
32  const std::string& /*componentProperty*/,
33  const std::string& componentName,
34  const VManager::IndexMap& /*componentIndex*/) const
35  {
36  if (componentName=="BeaconFreq") {
37  //if ComponentMap is not needed
38  // check if cache is available and up-to-date
39  const TimeStamp& curTime = Detector::GetInstance().GetTime();
40  if ( (fBeaconFreqs.size()) &&
41  (fBeaconFreqsBufferedStartDate < curTime) &&
42  (fBeaconFreqsBufferedStopDate > curTime) ) {
43  returnData = fBeaconFreqs;
44  return eFound;
45  } else { //else query DB
46  try {
47  returnData = GetBeaconFrequencies();
48  fBeaconFreqs = returnData;
49  if (returnData.size()) {
50  return eFound;
51  }
52  } catch (utl::DataNotFoundInDBException& err) {
53  throw (err);
54  }
55  }
56  }
57  return eNotFound;
58  }
59 
62  const std::string& /*componentProperty*/,
63  const std::string& componentName,
64  const VManager::IndexMap& componentIndex) const
65  {
66  if (componentName=="BeaconRefPhase") {
67  const TimeStamp& curTime = Detector::GetInstance().GetTime();
68  const int stationId = boost::lexical_cast<int>(componentIndex.find("RStationId")->second);
69  stringstream sBeaconFreq(componentIndex.find("BeaconFreq")->second);
70  double beaconFreq = 0;
71  sBeaconFreq >> beaconFreq;
72 
73  // check if cache is available and up-to-date
74  if ( (fBufferBeaconRefPhases.size()) &&
75  (fBufferBeaconRefPhases.find(stationId) != fBufferBeaconRefPhases.end()) &&
76  (fBufferBeaconRefPhases[stationId].find(beaconFreq) != fBufferBeaconRefPhases[stationId].end()) &&
77  (fBufferBeaconRefPhases[stationId][beaconFreq].startDate < curTime) &&
78  (fBufferBeaconRefPhases[stationId][beaconFreq].stopDate > curTime) ) {
79  returnData = fBufferBeaconRefPhases[stationId][beaconFreq].refPhase;
80  } else { //else query DB
81  try {
82  returnData = GetBeaconRefPhase(stationId, beaconFreq);
83  } catch (utl::DataNotFoundInDBException& err) {
84  throw (err);
85  }
86  }
87  return eFound;
88  }
89  return eNotFound;
90  }
91 
92 
93  std::vector<double>
95  const
96  {
97  const TimeStamp& curTime = Detector::GetInstance().GetTime();
98  ostringstream query;
99  query << "select BeaconFrequency, UNIX_TIMESTAMP(Commission), UNIX_TIMESTAMP(Decommission) from BeaconFreq where"
100  << " Commission<\"" << UTCDateTime(curTime).GetInMySQLFormat() << "\" "
101  << " and Decommission>\"" << UTCDateTime(curTime).GetInMySQLFormat() << "\";";
102  if (Query(query.str(), QueryInfoMessage("BeaconFrequency", "BeaconFreq")) == eNotFound) {
103  const string err = "No Beacon Frequency for given event-time in SQL data found! Query: " + query.str();
104  throw DataNotFoundInDBException(err);
105  }
106  vector<double> Result;
107  vector<TimeStamp> Comission;
108  vector<TimeStamp> Decomission;
109  vector<string> sqlResult;
110  utl::LeapSeconds leapSeconds;
111  unsigned long gpsSeconds=0; // dummy variable for conversion only
112  bool firstValue = true; // for all but the first value, check if the validity range in time is shorter.
113  while (FetchRow(sqlResult, false) == eFound) {
114  Result.push_back(boost::lexical_cast<double>(sqlResult[0]));
115 
116  // determine and store validity range
117  leapSeconds.ConvertUnixToGPS(boost::lexical_cast<unsigned long>(sqlResult[1]),gpsSeconds);
118  if (firstValue) {
119  fBeaconFreqsBufferedStartDate = TimeStamp(gpsSeconds);
120  } else {
121  if (fBeaconFreqsBufferedStartDate < TimeStamp(gpsSeconds))
122  fBeaconFreqsBufferedStartDate = TimeStamp(gpsSeconds);
123  }
124  leapSeconds.ConvertUnixToGPS(boost::lexical_cast<unsigned long>(sqlResult[2]),gpsSeconds);
125  if (firstValue) {
126  fBeaconFreqsBufferedStopDate = TimeStamp(gpsSeconds);
127  } else {
128  if (fBeaconFreqsBufferedStopDate > TimeStamp(gpsSeconds))
129  fBeaconFreqsBufferedStopDate = TimeStamp(gpsSeconds);
130  }
131  firstValue = false;
132  }
133 
134  return Result;
135  }
136 
137  double
139  const double& beaconFreq) const
140  {
141  const TimeStamp& curTime = Detector::GetInstance().GetTime();
142  ostringstream query;
143  query << "select BeaconRefPhase.RefPhase, UNIX_TIMESTAMP(Start), UNIX_TIMESTAMP(Stop) from BeaconFreq , BeaconRefPhase"
144  << " where BeaconFreq.BeaconFreq_id=BeaconRefPhase.BeaconFreq_id"
145  << " and BeaconRefPhase.RStationId = " << stationId
146  << " and BeaconFreq.BeaconFrequency LIKE " << beaconFreq
147  << " and Start<\"" << UTCDateTime(curTime).GetInMySQLFormat() << "\" "
148  << " and Stop>\"" << UTCDateTime(curTime).GetInMySQLFormat() << "\";";
149  const string err = "No Beacon RefPhase for event-time in SQL data found! Query: " + query.str();
150  if (Query(query.str(), QueryInfoMessage("BeaconRefPhase", "RefPhase")) == eNotFound) {
151  throw DataNotFoundInDBException(err);
152  }
153  //cout << query.str() << endl;
154  vector<string> sqlResult;
155  if (FetchRow(sqlResult, false) != eFound) {
156  throw DataNotFoundInDBException(err);
157  }
158  double Result = boost::lexical_cast<double>(sqlResult[0]);
159 
160  // store value in cache
161  fBufferBeaconRefPhases[stationId][beaconFreq].refPhase = Result;
162 
163  // store validity dates in cache
164  utl::LeapSeconds leapSeconds;
165  unsigned long gpsSeconds=0; // dummy variable for conversion only
166  leapSeconds.ConvertUnixToGPS(boost::lexical_cast<unsigned long>(sqlResult[1]),gpsSeconds);
167  fBufferBeaconRefPhases[stationId][beaconFreq].startDate = TimeStamp(gpsSeconds);
168  leapSeconds.ConvertUnixToGPS(boost::lexical_cast<unsigned long>(sqlResult[2]),gpsSeconds);
169  fBufferBeaconRefPhases[stationId][beaconFreq].stopDate = TimeStamp(gpsSeconds);
170 
171  return Result;
172  }
173 }
void Init(const std::string &configLink)
Manager Initialization. configLink is the CentralConfig hook for the configuration file...
double GetBeaconRefPhase(const int &stationId, const double &beaconFreq) const
Status InternalGetDataDouble(double &returnData, const std::string &, const std::string &componentName, const IndexMap &componentIndex) const
Manager for RD description of Station-Channels in SQL DB.
std::string GetInMySQLFormat() const
Definition: UTCDateTime.cc:107
void Init()
Initialise the registry.
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.
Status InternalGetDataVecDouble(std::vector< double > &returnData, const std::string &, const std::string &componentName, const IndexMap &) const
std::vector< double > GetBeaconFrequencies() const
std::map< std::string, std::string > IndexMap
Definition: VManager.h:133
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

, generated on Tue Sep 26 2023.