FOpticalEfficiencyLossSQLManager.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/FOpticalEfficiencyLossSQLManager.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 
21 using namespace std;
22 using namespace fdet;
23 using namespace utl;
24 using namespace det;
25 using namespace fwk;
26 
27 
28 REGISTER_F_MANAGER("FOpticalEfficiencyLossSQLManager", FOpticalEfficiencyLossSQLManager);
29 
30 
31 void
32 FOpticalEfficiencyLossSQLManager::Init(const std::string& configLink)
33 {
34  VSQLManager::Init(configLink);
35 }
36 
38 FOpticalEfficiencyLossSQLManager::GetData(double& /*returnData*/,
39  const string& /*componentProperty*/,
40  const string& /*componentName*/,
41  const IndexMap& )
42  const
43 {
44  return eNotFound;
45 }
46 
47 
49 FOpticalEfficiencyLossSQLManager::GetData(map<int, utl::TabulatedFunction>& returnData,
50  const string& componentProperty,
51  const string& componentName,
52  const IndexMap& componentIndex)
53  const
54 {
55  // this manager only knows about fd_optical_efficiency
56  if (componentName != "fd_optical_efficiency")
57  return eNotFound;
58 
59  returnData.clear();
60 
61  if (componentProperty == "pixel_optical_efficiency_corrections") {
62  const fdet::FDetector& fdet = det::Detector::GetInstance().GetFDetector();
63  const double wavelength = fdet.GetReferenceLambda();
64 
65  const string& eyeIdString = componentIndex.find("eyeId")->second;
66  const string& telescopeIdString = componentIndex.find("telescopeId")->second;
67  const unsigned int eyeId = boost::lexical_cast<unsigned int>(eyeIdString);
68  const unsigned int telId = boost::lexical_cast<unsigned int>(telescopeIdString);
69 
70  double opticalEff = 0;
71  if (GetOpticalEfficiencyCorrectionForTelescope(eyeId, telId, opticalEff)) {
72 
73  for (int iPix = 1; iPix <= 440; ++iPix) {
74  TabulatedFunction effVsWavelength;
75  effVsWavelength.PushBack(wavelength, opticalEff);
76  returnData.insert(make_pair(iPix, effVsWavelength));
77  }
78 
79  return eFound;
80  }
81  }
82 
83  return eNotFound;
84 }
85 
86 
87 bool
88 FOpticalEfficiencyLossSQLManager::GetOpticalEfficiencyCorrectionForTelescope(const unsigned int eyeId,
89  const unsigned int telId,
90  double& value)
91  const
92 {
93  const TimeStamp detTime = Detector::GetInstance().GetTime();
94  const int gpsSecond = detTime.GetGPSSecond();
95  const string tel = ConvertEyeTelToTelId(eyeId, telId);
96 
97  ostringstream queryBefore;
98  ostringstream queryAfter;
99 
100  boost::tuple<double, int> before; //cal. const., GPS sec.
101  boost::tuple<double, int> after;
102 
103  queryBefore << "SELECT " << tel << ", GPSsecond FROM constants, software WHERE constants.GPSsecond <= \"" << gpsSecond
104  << "\" AND constants.software_id = software.software_id AND software.software_version = \"" << fDatabaseSoftwareVersion
105  << "\" AND constants." << tel << " IS NOT NULL ORDER BY constants.GPSsecond DESC, constants.constants_id DESC LIMIT 1";
106 
107  if ( Query(queryBefore) != eNotFound ) {
108  if ( FetchRowMany(before) == eNotFound ) {
109  before.get<1>() = 0;
110  }
111  } else {
112  before.get<1>() = 0;
113  }
114 
115  queryAfter << "SELECT " << tel << ", GPSsecond FROM constants, software WHERE constants.GPSsecond > \"" << gpsSecond
116  << "\" AND constants.software_id = software.software_id AND software.software_version = \"" << fDatabaseSoftwareVersion
117  << "\" AND constants." << tel << " IS NOT NULL ORDER BY constants.GPSsecond, constants.constants_id DESC LIMIT 1";
118 
119  if ( Query(queryAfter) != eNotFound ) {
120  if ( FetchRowMany(after) == eNotFound ) {
121  after.get<1>() = 0;
122  }
123  } else {
124  after.get<1>() = 0;
125  }
126 
127  if (before.get<1>() != 0 && after.get<1>() != 0) {
128  if ((gpsSecond - before.get<1>()) < (after.get<1>()-gpsSecond)) {
129  value = before.get<0>();
130  } else {
131  value = after.get<0>();
132  }
133  } else if (before.get<1>() != 0) {
134  value = before.get<0>();
135  } else if (after.get<1>() != 0) {
136  value = after.get<0>();
137  } else {
138  // no recalibration if we have no information in the database
139  value = 1.;
140  }
141 
142  return true;
143 }
144 
145 
147 FOpticalEfficiencyLossSQLManager::GetData(vector<int>& returnData,
148  const string& componentProperty,
149  const string& componentName,
150  const IndexMap& /*componentIndex*/)
151  const
152 {
153  // pixel status is not stored in the database
154  if (componentProperty != "status" || componentName != "fd_optical_efficiency")
155  return eNotFound;
156  returnData.clear();
157  returnData.resize(441, 0); // eGood
158  return eFound;
159 }
160 
161 
163 FOpticalEfficiencyLossSQLManager::GetData(int& returnData,
164  const string& componentProperty,
165  const string& componentName,
166  const IndexMap& componentIndex)
167  const
168 {
169  // this manager only knows about fd_optical_efficiency
170  if (componentName != "fd_optical_efficiency")
171  return eNotFound;
172 
173  const TimeStamp detTime = Detector::GetInstance().GetTime();
174  const int gpsSecond = detTime.GetGPSSecond();
175 
176  const string& eyeIdString = componentIndex.find("eyeId")->second;
177  const string& telescopeIdString = componentIndex.find("telescopeId")->second;
178  const unsigned int eyeId = boost::lexical_cast<unsigned int>(eyeIdString);
179  const unsigned int telId = boost::lexical_cast<unsigned int>(telescopeIdString);
180  string tel = ConvertEyeTelToTelId(eyeId, telId);
181 
182  ostringstream queryBefore;
183  ostringstream queryAfter;
184 
185  vector<unsigned int> GPSbefore; //unsigned needed, otherwise we are outside of range when summing
186  vector<unsigned int> GPSafter;
187 
188  queryBefore << "SELECT GPSsecond FROM constants, software WHERE constants.GPSsecond <= \"" << gpsSecond
189  << "\" AND constants.software_id = software.software_id AND software.software_version = \"" << fDatabaseSoftwareVersion
190  << "\" AND constants." << tel << " IS NOT NULL ORDER BY constants.GPSsecond DESC, constants.constants_id DESC LIMIT 2";
191 
192  if ( Query(queryBefore) != eNotFound ) {
193  FetchColumn(GPSbefore);
194  }
195 
196  queryAfter << "SELECT GPSsecond FROM constants, software WHERE constants.GPSsecond > \"" << gpsSecond
197  << "\" AND constants.software_id = software.software_id AND software.software_version = \"" << fDatabaseSoftwareVersion
198  << "\" AND constants." << tel << " IS NOT NULL ORDER BY constants.GPSsecond, constants.constants_id DESC LIMIT 2";
199 
200  if ( Query(queryAfter) != eNotFound ) {
201  FetchColumn(GPSafter);
202  }
203 
204  /* optical efficiency loss correction is valid between the SQL stored GPSseconds:
205  a) GPS_(n-2) GPS_(n-1) gpsSecond ... GPS_(n+1) GPS_(n+2)
206  -> (start,end) = ( 0.5*(GPS_(n-2)+GPS_(n-1)), 0.5*(GPS_(n-1)+GPS_(n+1))*0.5 );
207  b) GPS_(n-2) GPS_(n-1) ... gpsSecond GPS_(n+1) GPS_(n+2)
208  -> (start,end) = ( 0.5*(GPS_(n-1)+GPS_(n+1)), 0.5*(GPS_(n+1)+GPS_(n+2))*0.5 );
209  */
210 
211  int GPSstart = 0;
212  int GPSend = 0;
213 
214  const unsigned int veryEarly = UTCDateTime(2000, 1, 1).GetTimeStamp().GetGPSSecond();
215  const unsigned int veryLate = UTCDateTime(2037, 11, 30).GetTimeStamp().GetGPSSecond();
216 
217  GPSbefore.resize(2, veryEarly);
218  GPSafter.resize(2, veryLate);
219 
220  //we have two records before and after the gpsSecond -> use the scheme above
221  //and put "very late"/"very early" to appropriate place if necessary
222  if ( (gpsSecond - GPSbefore[0]) < (GPSafter[0] - gpsSecond) ) {
223  if (GPSbefore[1] == veryEarly) {
224  GPSstart = veryEarly;
225  } else {
226  GPSstart = (GPSbefore[1]+GPSbefore[0])/2;
227  }
228 
229  GPSend = (GPSbefore[0]+GPSafter[0])/2;
230  } else {
231  GPSstart = (GPSbefore[0]+GPSafter[0])/2;
232 
233  if (GPSafter[1] == veryLate) {
234  GPSend = veryLate;
235  } else {
236  GPSend = (GPSafter[0]+GPSafter[1])/2;
237  }
238  }
239 
240  if (GPSbefore[0] == veryEarly) {
241  GPSstart = veryEarly;
242  }
243  if (GPSafter[0] == veryLate) {
244  GPSend = veryLate;
245  }
246 
247  if (componentProperty == "start_time") {
248  returnData = GPSstart;
249  return eFound;
250  }
251 
252  if (componentProperty == "end_time") {
253  returnData = GPSend;
254  return eFound;
255  }
256 
257  return eNotFound;
258 }
259 
260 string
261 FOpticalEfficiencyLossSQLManager::ConvertEyeTelToTelId(const int eyeId,
262  const int telId)
263  const
264 {
265  ostringstream tel;
266  tel << "Tel" << (eyeId-1)*6+telId;
267  return tel.str();
268 }
Class to hold collection (x,y) points and provide interpolation between them.
void Init()
Initialise the registry.
void PushBack(const double x, const double y)
Detector description interface for FDetector-related data.
Definition: FDetector.h:44
A TimeStamp holds GPS second and nanosecond for some event.
Definition: TimeStamp.h:110
double GetReferenceLambda() const
Definition: FDetector.cc:332
unsigned long GetGPSSecond() const
GPS second.
Definition: TimeStamp.h:124
#define REGISTER_F_MANAGER(_name_, _Type_)
TimeStamp GetTimeStamp() const
Definition: UTCDateTime.cc:115
Status
Specifies success or (eventually) various possible failure modes.
Definition: VManager.h:127
Manager for FD optical efficiency correction constants read from SQL database.

, generated on Tue Sep 26 2023.