Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JDetectorCalibration.hh
Go to the documentation of this file.
1 #ifndef __JDETECTOR__JDETECTORCALIBRATION__
2 #define __JDETECTOR__JDETECTORCALIBRATION__
3 
4 #include <string>
5 #include <vector>
6 
7 #include "JDB/JUPI_t.hh"
8 #include "JSon/JSon.hh"
9 #include "JLang/JObjectID.hh"
13 
14 
15 /**
16  * \author mdejong
17  */
18 
19 namespace JDETECTOR {}
20 namespace JPP { using namespace JDETECTOR; }
21 
22 /**
23  * Auxiliary classes and methods for detector calibration.
24  */
25 namespace JDETECTOR {
26 
27  using JSON::json;
28  using JLANG::JObjectID;
29  using JDATABASE::JUPI_t;
30  using JDATABASE::JPBS_t;
33 
34  /**
35  * Detector calibration key words for JSON I/O.
36  */
37 
38  // Headers
39  static const std::string User_t = "User";
40  static const std::string Location_t = "Location";
41  static const std::string StartTime_t = "StartTime";
42  static const std::string EndTime_t = "EndTime";
43  static const std::string Comment_t = "Comment";
44 
45  // Meta data
46  static const std::string Type_t = "Type";
47  static const std::string Parameters_t = "Parameters";
48  static const std::string Tests_t = "Tests";
49  static const std::string Data_t = "Data";
50  static const std::string RunNumber_t = "RUN_NUMBER";
51  static const std::string Name_t = "Name";
52  static const std::string Unit_t = "Unit";
53  static const std::string Values_t = "Values";
54 
55  // Calibration types
56  static const std::string PMTT0s_t = "PMTT0s";
57  static const std::string PMTSupplyVoltage_t = "PMT_Supply_Voltage";
58  static const std::string PMTThreshold_t = "PMT_Threshold";
59  static const std::string PMTGain_t = "PMT_Gain";
60  static const std::string DOMPositions_t = "DOMPositions";
61  static const std::string DOMRotations_t = "DOMRotations";
62 
63  // Prefixes
64  static const std::string Test_t = "Test";
65  static const std::string Target_t = "Target";
66  static const std::string Outcome_t = "Outcome";
67 
68  // Identifiers
69  static const std::string UPI_t = "UPI";
70  static const std::string PBS_t = "PBS";
71  static const std::string Variant_t = "Variant";
72  static const std::string PMTSerial_t = "PMTSerial";
73  static const std::string DOMSerial_t = "DOMSerial";
74  static const std::string DOMId_t = "DOMId";
75 
76  // Variable names
77  static const std::string T0_t = "T0";
78  static const std::string PX_t = "PX";
79  static const std::string PY_t = "PY";
80  static const std::string PZ_t = "PZ";
81  static const std::string Q0_t = "Q0";
82  static const std::string QX_t = "QX";
83  static const std::string QY_t = "QY";
84  static const std::string QZ_t = "QZ";
85 
86  // Units
87  static const std::string Unitless_t = "-";
88  static const std::string Volt_t = "V";
89 
90  // Status
91  static const std::string Result_t = "Result";
92  static const std::string OK_t = "OK";
93  static const std::string Failed_t = "FAILED";
94 
95 
96  /**
97  * Check validity of JSon data.
98  *
99  * \param js JSon data
100  * \return true if valid; else false
101  */
102  inline bool is_valid(const json& js)
103  {
104  return js.contains(Result_t) && js[Result_t] == OK_t;
105  }
106 
107 
108  /**
109  * Create ISO 8601 date string for output file header
110  *
111  * \param time Unix timestamp
112  * \return ISO 8601 timestamp corresponding to input time
113  */
114  inline std::string get_date_string(const std::time_t& time)
115  {
116  using namespace std;
117 
118  const int N = 256;
119 
120  char buffer[N];
121 
122  strftime(buffer, N, "%FT%T%z", localtime(&time));
123 
124  return string(buffer);
125  }
126 
127 
128  /**
129  * Cast single value to array of strings, conform the DB-format.
130  *
131  * \param value value
132  * \return std::vector containing value casted to string
133  */
134  template <class T>
136  {
137  using namespace std;
138 
139  return vector<string>{ to_string(value) };
140  }
141 
142 
143  /**
144  * Retrieve value from json array of strings.
145  *
146  * \param string_value_array array of string-casted values
147  * \return double-casted value
148  */
149  inline double retrieve_value(std::vector<std::string> string_value_array)
150  {
151  return std::stod(string_value_array[0]);
152  }
153 
154 
155  /**
156  * Auxiliary data structure for PMT time calibration.
157  */
159  public JObjectID,
160  public JCalibration
161  {
162  /**
163  * Default constructor.
164  */
166  {}
167 
168 
169  /**
170  * Constructor.
171  *
172  * \param id PMT identifier
173  * \param calibration PMT time calibration
174  */
176  const JCalibration& calibration) :
177  JObjectID (id),
178  JCalibration(calibration)
179  {}
180  };
181 
182 
183  /**
184  * Convert PMT time calibration to JSon.
185  *
186  * \param js json
187  * \param object PMT time calibration
188  */
189  inline void to_json(json& js, const JPMTCalibration_t& object)
190  {
191  js = json{ { PMTSerial_t, object.getID() },
192  { T0_t, object.getT0() } };
193  }
194 
195 
196  /**
197  * Convert JSon to PMT time calibration.
198  *
199  * \param js json
200  * \param object PMT time calibration
201  */
202  inline void from_json(const json& js, JPMTCalibration_t& object)
203  {
204  object.setID(js.at(PMTSerial_t).get<int>());
205 
206  object.setT0(js.at(T0_t).get<double>());
207  }
208 
209 
210  /**
211  * Data structure for PMT high-voltage calibration.
212  */
214  public JUPI_t
215  {
216  /**
217  * Default constructor.
218  */
220  {}
221 
222 
223  /**
224  * Constructor.
225  *
226  * \param upi %UPI
227  * \param result result
228  * \param hv HV
229  * \param gain PMT gain
230  * \param runNumbers run numbers
231  */
233  const std::string& result,
234  const double hv,
235  const double gain,
236  const std::vector<std::string>& runNumbers = std::vector<std::string>{}) :
237  JUPI_t (upi),
238  result (result),
239  supplyVoltage(hv),
240  PMTgain (gain),
241  runNumberList(runNumbers)
242  {}
243 
244  std::string result;
246  double PMTgain;
248  };
249 
250 
251  /**
252  * Convert PMT high-voltage calibration to JSon.
253  *
254  * \param js json
255  * \param object PMT high-voltage calibration
256  */
257  inline void to_json(json& js, const JHVCalibration_t& object)
258  {
259  std::ostringstream os;
260 
261  os << object.getUPI();
262 
263  js[UPI_t] = os.str();
264  js[Test_t + Result_t] = object.result;
266  { Unit_t, Volt_t },
267  { Values_t, get_string_array(object.supplyVoltage) } };
268  js[Test_t + Parameters_t][1] = json{ { Name_t, PMTGain_t },
269  { Unit_t, Unitless_t },
270  { Values_t, get_string_array(object.PMTgain) } };
271  js[Test_t + Parameters_t][2] = json{ { Name_t, RunNumber_t },
272  { Unit_t, Unitless_t },
273  { Values_t, object.runNumberList } };
274  }
275 
276 
277  /**
278  * Convert JSon to PMT high-voltage calibration.
279  *
280  * \param js json
281  * \param object PMT high-voltage calibration
282  */
283  inline void from_json(const json& js, JHVCalibration_t& object)
284  {
285  using namespace std;
286 
287  stringstream is(js.at(UPI_t).get<string>());
288 
289  is >> static_cast<JUPI_t&>(object);
290 
291  object.result = js.at(Test_t + Result_t).get<string>();
292  object.supplyVoltage = retrieve_value(js.at(Test_t + Parameters_t)[0].at(Values_t).get<vector<string>>());
293  object.PMTgain = retrieve_value(js.at(Test_t + Parameters_t)[1].at(Values_t).get<vector<string>>());
294  object.runNumberList = js.at(Test_t + Parameters_t)[2].at(Values_t).get<vector<string>>();
295  }
296 
297 
298  /**
299  * Data structure for PMT threshold calibration.
300  */
302  public JUPI_t
303  {
304  /**
305  * Default constructor.
306  */
308  {}
309 
310  /**
311  * Constructor.
312  *
313  * \param upi %UPI
314  * \param result result
315  * \param threshold TH
316  * \param runNumbers run numbers
317  */
319  const std::string& result,
320  const double th,
321  const std::vector<std::string>& runNumbers = std::vector<std::string>{}) :
322  JUPI_t (upi),
323  result (result),
324  threshold (th),
325  runNumberList(runNumbers)
326  {}
327  std::string result;
328  double threshold;
330  };
331 
332 
333  /**
334  * Convert PMT threshold calibration to JSon.
335  *
336  * \param js json
337  * \param object PMT threshold calibration
338  */
339  inline void to_json(json& js, const JTHCalibration_t& object)
340  {
341  std::ostringstream os;
342 
343  os << object.getUPI();
344 
345  js[UPI_t] = os.str();
346  js[Test_t + Result_t] = object.result;
347  js[Test_t + Parameters_t][0] = json{ { Name_t, PMTThreshold_t },
348  { Unit_t, Unitless_t },
349  { Values_t, get_string_array(object.threshold) } };
350  js[Test_t + Parameters_t][2] = json{ { Name_t, RunNumber_t },
351  { Unit_t, Unitless_t },
352  { Values_t, object.runNumberList } };
353  }
354 
355 
356  /**
357  * Convert JSon to PMT threshold calibration.
358  *
359  * \param js json
360  * \param object PMT threshold calibration
361  */
362  inline void from_json(const json& js, JTHCalibration_t& object)
363  {
364  using namespace std;
365 
366  stringstream is(js.at(UPI_t).get<string>());
367 
368  is >> static_cast<JUPI_t&>(object);
369 
370  object.result = js.at(Test_t + Result_t).get<string>();
371  object.threshold = retrieve_value(js.at(Test_t + Parameters_t)[0].at(Values_t).get<vector<string>>());
372  object.runNumberList = js.at(Test_t + Parameters_t)[2].at(Values_t).get<vector<string>>();
373  }
374 
375 
376  /**
377  * Auxiliary data structure for module position.
378  */
380  public JObjectID,
381  public JPosition3D
382  {
383  /**
384  * Default constructor.
385  */
387  {}
388 
389 
390  /**
391  * Constructor.
392  *
393  * \param id module identifier
394  * \param position module position
395  */
397  const JPosition3D& position) :
398  JObjectID (id),
399  JPosition3D(position)
400  {}
401  };
402 
403 
404  /**
405  * Convert module position to JSon.
406  *
407  * \param js json
408  * \param object module position
409  */
410  inline void to_json(json& js, const JModulePosition_t& object)
411  {
412  js = json{ { DOMId_t, object.getID() },
413  { PX_t, object.getX() },
414  { PY_t, object.getY() },
415  { PZ_t, object.getZ() } };
416  }
417 
418 
419  /**
420  * Convert JSon to module position.
421  *
422  * \param js json
423  * \param object module position
424  */
425  inline void from_json(const json& js, JModulePosition_t& object)
426  {
427  object.setID(js.at(DOMId_t).get<int>());
428 
429  object.setPosition(JPosition3D(js.at(PX_t).get<double>(),
430  js.at(PY_t).get<double>(),
431  js.at(PZ_t).get<double>()));
432  }
433 
434  /**
435  * Auxiliary data structure for module rotation.
436  */
438  public JObjectID,
439  public JQuaternion3D
440  {
441  /**
442  * Default constructor.
443  */
445  {}
446 
447 
448  /**
449  * Constructor.
450  *
451  * \param id module identifier
452  * \param rotation module rotation
453  */
455  const JQuaternion3D& rotation) :
456  JObjectID (id),
457  JQuaternion3D(rotation)
458  {}
459  };
460 
461 
462  /**
463  * Convert module rotation to JSon.
464  *
465  * \param js json
466  * \param object module rotation
467  */
468  inline void to_json(json& js, const JModuleRotation_t& object)
469  {
470  js = json{ { DOMId_t, object.getID() },
471  { Q0_t, object.getA() },
472  { QX_t, object.getB() },
473  { QY_t, object.getC() },
474  { QZ_t, object.getD() } };
475  }
476 
477 
478  /**
479  * Convert JSon to module rotation.
480  *
481  * \param js json
482  * \param object module rotation
483  */
484  inline void from_json(const json& js, JModuleRotation_t& object)
485  {
486  object.setID(js.at(DOMId_t).get<int>());
487 
488  object.setQuaternion(JQuaternion3D(js.at(Q0_t).get<double>(),
489  js.at(QX_t).get<double>(),
490  js.at(QY_t).get<double>(),
491  js.at(QZ_t).get<double>()));
492  }
493 
494  typedef std::vector<JHVCalibration_t> JHVCalibration; //!< PMT high voltage calibration
495  typedef std::vector<JTHCalibration_t> JTHCalibration; //!< PMT threshold calibration
496  typedef std::vector<JPMTCalibration_t> JPMTCalibration; //!< PMT time calibration
497  typedef std::vector<JModulePosition_t> JModulePosition; //!< Module position
498  typedef std::vector<JModuleRotation_t> JModuleRotation; //!< Module rotation
499 }
500 
501 #endif
static const std::string DOMSerial_t
static const std::string PMTGain_t
void to_json(json &js, const JPMTCalibration_t &object)
Convert PMT time calibration to JSon.
static const std::string PZ_t
static const std::string Name_t
static const std::string PMTSupplyVoltage_t
static const std::string Q0_t
JPMTCalibration_t()
Default constructor.
Time calibration (including definition of sign of time offset).
static const std::string Unit_t
static const std::string Failed_t
std::vector< std::string > runNumberList
static const std::string UPI_t
static const std::string Tests_t
static const std::string DOMPositions_t
static const std::string OK_t
JModuleRotation_t(const JObjectID &id, const JQuaternion3D &rotation)
Constructor.
static const std::string DOMId_t
JHVCalibration_t()
Default constructor.
std::vector< JModuleRotation_t > JModuleRotation
Module rotation.
static const std::string QZ_t
static const std::string Variant_t
static const std::string PX_t
static const std::string Location_t
Universal product identifier (UPI).
Definition: JUPI_t.hh:29
Data structure for time calibration.
double retrieve_value(std::vector< std::string > string_value_array)
Retrieve value from json array of strings.
JPMTCalibration_t(const JObjectID &id, const JCalibration &calibration)
Constructor.
is
Definition: JDAQCHSM.chsm:167
JTHCalibration_t(const JUPI_t &upi, const std::string &result, const double th, const std::vector< std::string > &runNumbers=std::vector< std::string >{})
Constructor.
static const std::string Target_t
std::vector< std::string > runNumberList
static const std::string Result_t
static const std::string Outcome_t
static const std::string Volt_t
void from_json(const json &js, JPMTCalibration_t &object)
Convert JSon to PMT time calibration.
static const std::string T0_t
std::vector< JHVCalibration_t > JHVCalibration
PMT high voltage calibration.
do set_variable OUTPUT_DIRECTORY $WORKDIR T
static const std::string DOMRotations_t
bool is_valid(const json &js)
Check validity of JSon data.
static const std::string PMTThreshold_t
static const std::string Test_t
static const std::string PMTSerial_t
static const std::string RunNumber_t
JModulePosition_t(const JObjectID &id, const JPosition3D &position)
Constructor.
static const std::string Comment_t
Data structure for unit quaternion in three dimensions.
static const std::string Values_t
JTHCalibration_t()
Default constructor.
static const std::string PY_t
then $JPP_DIR examples JDetector JToT o $OUTPUT_FILE n N $NPE P gain
Definition: JToT.sh:45
static const std::string EndTime_t
JModulePosition_t()
Default constructor.
static const std::string PBS_t
Auxiliary class for object identification.
Definition: JObjectID.hh:22
std::vector< JTHCalibration_t > JTHCalibration
PMT threshold calibration.
Data structure for PMT high-voltage calibration.
nlohmann::json json
static const std::string Type_t
std::string to_string(const T &value)
Convert value to string.
static const std::string StartTime_t
Auxiliary data structure for module rotation.
std::string get_date_string(const std::time_t &time)
Create ISO 8601 date string for output file header.
JModuleRotation_t()
Default constructor.
Product breakdown structure (PBS).
Definition: JPBS_t.hh:25
static const std::string User_t
Detector calibration key words for JSON I/O.
Auxiliary data structure for module position.
Data structure for position in three dimensions.
Definition: JPosition3D.hh:36
JHVCalibration_t(const JUPI_t &upi, const std::string &result, const double hv, const double gain, const std::vector< std::string > &runNumbers=std::vector< std::string >{})
Constructor.
static const std::string PMTT0s_t
std::vector< JModulePosition_t > JModulePosition
Module position.
static const std::string Parameters_t
static const std::string Data_t
Auxiliary data structure for PMT time calibration.
static const std::string QY_t
then usage $script[input file[working directory[option]]] nWhere option can be N
Definition: JMuonPostfit.sh:37
static const std::string Unitless_t
std::vector< JPMTCalibration_t > JPMTCalibration
PMT time calibration.
Data structure for PMT threshold calibration.
std::vector< std::string > get_string_array(T value)
Cast single value to array of strings, conform the DB-format.
static const std::string QX_t