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.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;
30  using JDATABASE::JPBS;
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 PMTGain_t = "PMT_Gain";
59  static const std::string DOMPositions_t = "DOMPositions";
60  static const std::string DOMRotations_t = "DOMRotations";
61 
62  // Prefixes
63  static const std::string Test_t = "Test";
64  static const std::string Target_t = "Target";
65  static const std::string Outcome_t = "Outcome";
66 
67  // Identifiers
68  static const std::string UPI_t = "UPI";
69  static const std::string PBS_t = "PBS";
70  static const std::string Variant_t = "Variant";
71  static const std::string PMTSerial_t = "PMTSerial";
72  static const std::string DOMSerial_t = "DOMSerial";
73  static const std::string DOMId_t = "DOMId";
74 
75  // Variable names
76  static const std::string T0_t = "T0";
77  static const std::string PX_t = "PX";
78  static const std::string PY_t = "PY";
79  static const std::string PZ_t = "PZ";
80  static const std::string Q0_t = "Q0";
81  static const std::string QX_t = "QX";
82  static const std::string QY_t = "QY";
83  static const std::string QZ_t = "QZ";
84 
85  // Units
86  static const std::string Unitless_t = "-";
87  static const std::string Volt_t = "V";
88 
89  // Status
90  static const std::string Result_t = "Result";
91  static const std::string OK_t = "OK";
92  static const std::string Failed_t = "FAILED";
93 
94 
95  /**
96  * Check validity of JSon data.
97  *
98  * \param js JSon data
99  * \return true if valid; else false
100  */
101  inline bool is_valid(const json& js)
102  {
103  return js.contains(Result_t) && js[Result_t] == OK_t;
104  }
105 
106 
107  /**
108  * Create ISO 8601 date string for output file header
109  *
110  * \param time Unix timestamp
111  * \return ISO 8601 timestamp corresponding to input time
112  */
113  inline std::string get_date_string(const std::time_t& time)
114  {
115  using namespace std;
116 
117  const int N = 256;
118 
119  char buffer[N];
120 
121  strftime(buffer, N, "%FT%T%z", localtime(&time));
122 
123  return string(buffer);
124  }
125 
126 
127  /**
128  * Cast single value to array of strings, conform the DB-format.
129  *
130  * \param value value
131  * \return std::vector containing value casted to string
132  */
133  template <class T>
135  {
136  using namespace std;
137 
138  return vector<string>{ to_string(value) };
139  }
140 
141 
142  /**
143  * Retrieve value from json array of strings.
144  *
145  * \param string_value_array array of string-casted values
146  * \return double-casted value
147  */
148  inline double retrieve_value(std::vector<std::string> string_value_array)
149  {
150  return std::stod(string_value_array[0]);
151  }
152 
153 
154  /**
155  * Auxiliary data structure for PMT time calibration.
156  */
158  public JObjectID,
159  public JCalibration
160  {
161  /**
162  * Default constructor.
163  */
165  {}
166 
167 
168  /**
169  * Constructor.
170  *
171  * \param id PMT identifier
172  * \param calibration PMT time calibration
173  */
175  const JCalibration& calibration) :
176  JObjectID (id),
177  JCalibration(calibration)
178  {}
179  };
180 
181 
182  /**
183  * Convert PMT time calibration to JSon.
184  *
185  * \param js json
186  * \param object PMT time calibration
187  */
188  inline void to_json(json& js, const JPMTCalibration_t& object)
189  {
190  js = json{ { PMTSerial_t, object.getID() },
191  { T0_t, object.getT0() } };
192  }
193 
194 
195  /**
196  * Convert JSon to PMT time calibration.
197  *
198  * \param js json
199  * \param object PMT time calibration
200  */
201  inline void from_json(const json& js, JPMTCalibration_t& object)
202  {
203  object.setID(js.at(PMTSerial_t).get<int>());
204 
205  object.setT0(js.at(T0_t).get<double>());
206  }
207 
208 
209  /**
210  * Data structure for PMT high-voltage calibration.
211  */
213  public JUPI
214  {
215  /**
216  * Default constructor.
217  */
219  {}
220 
221 
222  /**
223  * Constructor.
224  *
225  * \param upi %UPI
226  * \param result result
227  * \param hv HV
228  * \param gain PMT gain
229  * \param runNumbers run numbers
230  */
231  JHVCalibration_t(const JUPI& upi,
232  const std::string& result,
233  const double hv,
234  const double gain,
235  const std::vector<std::string>& runNumbers = std::vector<std::string>{}) :
236  JUPI (upi),
237  result (result),
238  supplyVoltage(hv),
239  PMTgain (gain),
240  runNumberList(runNumbers)
241  {}
242 
243  std::string result;
245  double PMTgain;
247  };
248 
249 
250  /**
251  * Convert PMT high-voltage calibration to JSon.
252  *
253  * \param js json
254  * \param object PMT high-voltage calibration
255  */
256  inline void to_json(json& js, const JHVCalibration_t& object)
257  {
258  std::ostringstream os;
259 
260  os << object.getUPI();
261 
262  js[UPI_t] = os.str();
263  js[Test_t + Result_t] = object.result;
265  { Unit_t, Volt_t },
266  { Values_t, get_string_array(object.supplyVoltage) } };
267  js[Test_t + Parameters_t][1] = json{ { Name_t, PMTGain_t },
268  { Unit_t, Unitless_t },
269  { Values_t, get_string_array(object.PMTgain) } };
270  js[Test_t + Parameters_t][2] = json{ { Name_t, RunNumber_t },
271  { Unit_t, Unitless_t },
272  { Values_t, object.runNumberList } };
273  }
274 
275 
276  /**
277  * Convert JSon to PMT high-voltage calibration.
278  *
279  * \param js json
280  * \param object PMT high-voltage calibration
281  */
282  inline void from_json(const json& js, JHVCalibration_t& object)
283  {
284  using namespace std;
285 
286  stringstream is(js.at(UPI_t).get<string>());
287 
288  is >> static_cast<JUPI&>(object);
289 
290  object.result = js.at(Test_t + Result_t).get<string>();
291  object.supplyVoltage = retrieve_value(js.at(Test_t + Parameters_t)[0].at(Values_t).get<vector<string>>());
292  object.PMTgain = retrieve_value(js.at(Test_t + Parameters_t)[1].at(Values_t).get<vector<string>>());
293  object.runNumberList = js.at(Test_t + Parameters_t)[2].at(Values_t).get<vector<string>>();
294  }
295 
296 
297  /**
298  * Auxiliary data structure for module position.
299  */
301  public JObjectID,
302  public JPosition3D
303  {
304  /**
305  * Default constructor.
306  */
308  {}
309 
310 
311  /**
312  * Constructor.
313  *
314  * \param id module identifier
315  * \param position module position
316  */
318  const JPosition3D& position) :
319  JObjectID (id),
320  JPosition3D(position)
321  {}
322  };
323 
324 
325  /**
326  * Convert module position to JSon.
327  *
328  * \param js json
329  * \param object module position
330  */
331  inline void to_json(json& js, const JModulePosition_t& object)
332  {
333  js = json{ { DOMId_t, object.getID() },
334  { PX_t, object.getX() },
335  { PY_t, object.getY() },
336  { PZ_t, object.getZ() } };
337  }
338 
339 
340  /**
341  * Convert JSon to module position.
342  *
343  * \param js json
344  * \param object module position
345  */
346  inline void from_json(const json& js, JModulePosition_t& object)
347  {
348  object.setID(js.at(DOMId_t).get<int>());
349 
350  object.setPosition(JPosition3D(js.at(PX_t).get<double>(),
351  js.at(PY_t).get<double>(),
352  js.at(PZ_t).get<double>()));
353  }
354 
355  /**
356  * Auxiliary data structure for module rotation.
357  */
359  public JObjectID,
360  public JQuaternion3D
361  {
362  /**
363  * Default constructor.
364  */
366  {}
367 
368 
369  /**
370  * Constructor.
371  *
372  * \param id module identifier
373  * \param rotation module rotation
374  */
376  const JQuaternion3D& rotation) :
377  JObjectID (id),
378  JQuaternion3D(rotation)
379  {}
380  };
381 
382 
383  /**
384  * Convert module rotation to JSon.
385  *
386  * \param js json
387  * \param object module rotation
388  */
389  inline void to_json(json& js, const JModuleRotation_t& object)
390  {
391  js = json{ { DOMId_t, object.getID() },
392  { Q0_t, object.getA() },
393  { QX_t, object.getB() },
394  { QY_t, object.getC() },
395  { QZ_t, object.getD() } };
396  }
397 
398 
399  /**
400  * Convert JSon to module rotation.
401  *
402  * \param js json
403  * \param object module rotation
404  */
405  inline void from_json(const json& js, JModuleRotation_t& object)
406  {
407  object.setID(js.at(DOMId_t).get<int>());
408 
409  object.setQuaternion(JQuaternion3D(js.at(Q0_t).get<double>(),
410  js.at(QX_t).get<double>(),
411  js.at(QY_t).get<double>(),
412  js.at(QZ_t).get<double>()));
413  }
414 
415  typedef std::vector<JHVCalibration_t> JHVCalibration; //!< PMT high voltage calibration
416  typedef std::vector<JPMTCalibration_t> JPMTCalibration; //!< PMT time calibration
417  typedef std::vector<JModulePosition_t> JModulePosition; //!< Module position
418  typedef std::vector<JModuleRotation_t> JModuleRotation; //!< Module rotation
419 }
420 
421 #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.
PMT 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
Data structure for PMT 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
static const std::string Target_t
then echo Variable JPP_DIR undefined exit fi source $JPP_DIR setenv sh $JPP_DIR set_variable DEBUG set_variable NPE set_variable FIT_RANGE set_variable OUTPUT_DIR tmp set_variable OUTPUT_JSON $OUTPUT_DIR HVtuning json set_variable OUTPUT_ROOT $OUTPUT_DIR HVtuning root set_variable FIT_OPTIONS RME set_variable PMT_DEFAULT gain
Definition: JTuneHV.sh:30
static const std::string Result_t
static const std::string Outcome_t
static const std::string Volt_t
JHVCalibration_t(const JUPI &upi, const std::string &result, const double hv, const double gain, const std::vector< std::string > &runNumbers=std::vector< std::string >{})
Constructor.
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 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
Product breakdown structure (PBS).
Definition: JPBS.hh:27
Data structure for quaternion in three dimensions.
static const std::string Values_t
static const std::string PY_t
Universal product identifier (UPI).
Definition: JUPI.hh:30
static const std::string EndTime_t
JModulePosition_t()
Default constructor.
static const std::string PBS_t
Auxiliary class for object identification.
Definition: JObjectID.hh:27
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.
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:35
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.
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