Jpp 21.0.0-rc.1-88-g0130508c4
the software that should make you happy
Loading...
Searching...
No Matches
JEditTuneHV.cc
Go to the documentation of this file.
1#include <iostream>
2#include <iomanip>
3#include <fstream>
4#include <sstream>
5#include <set>
6
8
10
11#include "JLang/JUUID.hh"
12#include "JLang/JPredicate.hh"
13
14#include "JSupport/JMeta.hh"
16
18
19#include "JDB/JUPI_t.hh"
20#include "JDB/JVendorHV.hh"
21#include "JDB/JDBSupportkit.hh"
23
24#include "JSon/JSon.hh"
25
26#include "Jeep/JPrint.hh"
27#include "Jeep/JParser.hh"
28#include "Jeep/JMessage.hh"
29#include "Jeep/JProperties.hh"
30
31namespace {
32
33 /**
34 * Auxiliary class for creating PMT high-voltage look-up table from ASCII DB file.
35 */
36 struct JHVTable
37 {
38 /**
39 * HV-table types.
40 */
41 enum JHVTableTypes {
42 VENDOR_HV = 1, //!< Vendor high-voltages
43 RUN_HV = 2 //!< Run-specific high-voltages
44 };
45
46 /**
47 * Initialise
48 *
49 * \param filename file name
50 * \param type HV-table type
51 */
52 JHVTable(const char* const filename,
53 const JHVTableTypes type)
54 {
55 using namespace std;
56 using namespace JPP;
57
58 ifstream in(filename);
59
60 switch (type) {
61 case VENDOR_HV: {
62
63 for (string buffer; getline(in, buffer); ) {
64
65 istringstream is(buffer);
66
67 JVendorHV entry;
68
69 if (is >> entry.DETID >> entry.DUID >> entry.FLOORID >> entry.CABLEPOS >> entry.PMTSERIAL >> entry.PMT_SUPPLY_VOLTAGE) {
70
71 if (entry.PMTSERIAL > 0 &&
72 entry.PMT_SUPPLY_VOLTAGE < 0) {
73
74 HVmap[entry.PMTSERIAL] = entry.PMT_SUPPLY_VOLTAGE;
75 }
76 }
77 }
78
79 break;
80 }
81
82 case RUN_HV: {
83
84 for (string buffer; getline(in, buffer); ) {
85
86 istringstream is(buffer);
87
89
90 if (is >> entry.DETOID >> entry.DETID >>
91 entry.RUN >> entry.RUNSETUPID >>
92 entry.DUID >> entry.FLOORID >> entry.PMTINTID >> entry.CABLEPOS >> entry.PMTSERIAL >>
93 entry.CG_OID >> entry.HV_INDEX >> entry.HV_VALUE) {
94
95 if (entry.PMTSERIAL > 0 &&
96 entry.HV_VALUE < 0) {
97
98 HVmap[entry.PMTSERIAL] = entry.HV_VALUE;
99 }
100 }
101 }
102
103 break;
104 }
105
106 default:
107 THROW(JValueOutOfRange, "JHVTable::JHVTable(): Invalid HV-table type: " << type);
108 }
109
110 in.close();
111 }
112
113
114 /**
115 * Get high-voltage.
116 *
117 * \param serialID PMT serial number
118 * \return high-voltage [V]
119 */
120 double operator()(const int serialID)
121 {
122 using namespace std;
123 using namespace JPP;
124
125 map<int, double>::const_iterator p = HVmap.find(serialID);
126
127 if (p != HVmap.end()) {
128 return p->second;
129 } else {
130 THROW(JValueOutOfRange, "Serial number " << serialID << " not found.");
131 }
132 }
133
134 protected:
136 };
137
138
139 /**
140 * Read PMT HV-table type from input.
141 *
142 * \param in input stream
143 * \param object PMT HV-table type
144 * \return input stream
145 */
146 inline std::istream& operator>>(std::istream& in,
147 JHVTable::JHVTableTypes& object) {
148
150
151 int type;
152
153 in >> type;
154
155 switch (type) {
156 case int(JHVTable::VENDOR_HV):
157 object = JHVTable::VENDOR_HV;
158 return in;
159 case int(JHVTable::RUN_HV):
160 object = JHVTable::RUN_HV;
161 return in;
162 default:
163 THROW(JValueOutOfRange, "operator>>(): Invalid HV-table type: " << type);
164 }
165 }
166
167
168 /**
169 * Write PMT HV-table type to output.
170 *
171 * \param out output stream
172 * \param object PMT HV-table type
173 * \return output stream
174 */
175 inline std::ostream& operator<<(std::ostream& out,
176 JHVTable::JHVTableTypes object) {
177 return out << int(object);
178 }
179}
180
181
182/**
183 * \file
184 *
185 * Auxiliary program to treat failed HV-tuning calibrations.
186 * \author bjung
187 */
188int main(int argc, char **argv)
189{
190 using namespace std;
191 using namespace JPP;
192
193 typedef JHVTable::JHVTableTypes JHVTableType_t;
194 typedef pair<JHVTableType_t, string> JHVTable_t;
195
196 string inputFile;
197 string outputFile;
198 JHVTable_t HVtable;
199
200 set<JUPI_t> pmtSet;
201
202 string login;
203 string locationID;
204 int elapsedTime = 0;
205
206 double minHV = -1500;
207 double maxHV = - 800;
208
209 int debug;
210
211 try {
212
213 JProperties properties;
214
215 properties.insert(gmake_property(login));
216 properties.insert(gmake_property(locationID));
217 properties.insert(gmake_property(elapsedTime));
218
219 JProperties settings;
220
221 settings.insert(gmake_property(minHV));
222 settings.insert(gmake_property(maxHV));
223
224 JParser<> zap("Auxiliary program to treat failed high-voltage tuning results.");
225
226 zap['f'] = make_field(inputFile, "input file");
227 zap['o'] = make_field(outputFile, "output file");
228 zap['b'] = make_field(HVtable, "ASCII HV table") = JPARSER::initialised();
229 zap['P'] = make_field(pmtSet, "Set of PMT UPIs") = JPARSER::initialised();
230 zap['#'] = make_field(properties, "database information") = JPARSER::initialised();
231 zap['@'] = make_field(settings, "HV limits") = JPARSER::initialised();
232 zap['d'] = make_field(debug, "debug") = 2;
233
234 zap(argc, argv);
235
236 } catch(const exception &error) {
237
238 FATAL(error.what() << endl);
239 }
240
241 if (login.empty() || locationID.empty()) {
242 FATAL("Missing user information (please specify via -#login and -#locationID).");
243 }
244
245
246 const JUUID& UUID = JUUID::rndm();
247
248 JDateAndTime timer;
249
250 timer.sub(elapsedTime);
251
252
253 // Edit high-voltage calibrations
254
255 JDBAPIVersion DBAPIVersion;
256 string DBTestType;
257 string metaInfoStr = MAKE_STRING(JMeta(argc, argv));
258
259 JHVCalibration toEdit;
260
261 if (isJSONFile(inputFile.c_str())) {
262
263 json js;
264
265 ifstream ifs(inputFile.c_str());
266
267 ifs >> js;
268 ifs.close();
269
270 // Extract data
271
272 json::const_iterator i0 = js.find(APIVersion_t);
273
274 if (i0 != js.cend()) {
275
276 istringstream iss(i0->get<string>());
277
278 iss >> DBAPIVersion;
279 }
280
281 JHVCalibration HVcals;
282
283 json::const_iterator i1 = js.find(Data_t);
284
285 if ((DBAPIVersion.getMajorVersion() == 2) &&
286 (i1 != js.cend() && i1->size() > 0)) {
287
288 DBTestType = (*i1)[0].at(Test_t + Type_t).get<string>();
289
290 JHVCalibration_t::setVersion(getDBVersionTuneHV(DBTestType));
291
292 HVcals = (*i1)[0].at(Tests_t).get<JHVCalibration>();
293 metaInfoStr += (*i1)[0].at(Provenance_t + Info_t).at(Configuration_t).get<string>();
294
295 } else {
296
297 DBTestType = js.at(Test_t + Type_t).get<string>();
298
299 JHVCalibration_t::setVersion(getDBVersionTuneHV(DBTestType));
300
301 HVcals = js.at(Tests_t).get<JHVCalibration>();
302 }
303
304 // Collect PMTs which need to be edited
305
306 if (pmtSet.empty()) {
307
308 for (JHVCalibration::iterator it = HVcals.begin(); it != HVcals.end(); ++it) {
309
310 if (it->result != OK_t) {
311 toEdit.push_back(*it);
312 }
313 }
314
315 } else {
316
317 for (set<JUPI_t>::const_iterator it = pmtSet.cbegin(); it != pmtSet.cend(); ++it) {
318
319 JHVCalibration::iterator pmt = find_if(HVcals.begin(), HVcals.end(),
320 make_predicate(&JHVCalibration_t::getNumber, it->getNumber()));
321 if (pmt != HVcals.end()) {
322
323 if (pmt->result == OK_t) {
324 WARNING("Editing " << OK_t << " result for " << pmt->getUPI() << endl);
325 }
326
327 toEdit.push_back(*pmt);
328 }
329 }
330 }
331
332 } else {
333
334 ERROR(inputFile << " is not a JSON file." << endl);
335 }
336
337
338 if (!HVtable.second.empty()) {
339
340 NOTICE("Setting " << (HVtable.first == JHVTableType_t::VENDOR_HV ? "vendor " : "run-specific ") <<
341 "PMT high-voltages from file " << HVtable.second << "..." << endl);
342
343 JHVTable getHV(HVtable.second.c_str(), HVtable.first);
344
345 for (JHVCalibration::iterator i = toEdit.begin(); i != toEdit.end(); ++i) {
346
347 const JUPI_t& upi = i->getUPI();
348 const double HV = getHV(upi.getNumber());
349
350 if (HV > minHV && HV < maxHV) {
351
352 i->supplyVoltage = getHV(upi.getNumber());
353 i->result = OK_t;
354
355 } else {
356
357 WARNING("Invalid high-voltage for PMT " << upi << " (" << FIXED(7,1) << HV <<
358 " not within [ " << FIXED(7,1) << minHV << ", " << FIXED(7,1) << maxHV << "]); skip." << endl);
359 }
360 }
361
362 } else {
363
364 NOTICE("Setting high-voltages manually..." << endl);
365
366 for (JHVCalibration::iterator i = toEdit.begin(); i != toEdit.end(); ++i) {
367
368 NOTICE("Please specify high-voltage for " << RIGHT(30) << i->getUPI() << ":" << endl);
369
370 double manualHV;
371 cin >> manualHV;
372
373 while (manualHV < minHV || manualHV > maxHV) {
374
375 WARNING("Specified high-voltage is not within range [" <<
376 FIXED(7,1) << minHV << ", " << FIXED(7,1) << maxHV <<
377 "]; Please specify again." << endl);
378
379 cin >> manualHV;
380 }
381
382 i->supplyVoltage = manualHV;
383 i->result = OK_t;
384 }
385 }
386
387
388 json js;
389
390 if (DBAPIVersion.getMajorVersion() == 2) {
391
392 json error = { {Message_t, "" },
393 {Code_t, OK_t },
394 {Arguments_t, json::array() } };
395
396 json metaData = { {Configuration_t, metaInfoStr },
397 {UUID_t, MAKE_STRING(UUID) } };
398
399 json data = { {Provenance_t + Info_t, json(metaData) },
400 {User_t, login },
401 {Location_t, locationID },
402 {Start_t + Time_t, timer.toString() },
403 {End_t + Time_t, timer().toString() },
404 {Test_t + Type_t, DBTestType },
405 {Tests_t, json(toEdit) } };
406
407 js[APIVersion_t] = MAKE_STRING(DBAPIVersion);
408 js[Data_t + Type_t] = MAKE_STRING("ProductTestSession");
409 js[Encoding_t] = MAKE_STRING("NativeJSON");
410 js[Error_t] = json(error);
411 js[Start_t] = timer.toString();
412 js[End_t] = timer().toString();
413 js[Data_t][0] = json(data);
414
415 } else {
416
417 js[User_t] = login;
418 js[Location_t] = locationID;
419 js[Test_t + Type_t] = DBTestType;
420 js[Start_t + Time_t] = timer.toString();
421 js[End_t + Time_t] = timer().toString();
422 js[Tests_t] = json(toEdit);
423 }
424
425
426 ofstream ofs(outputFile.c_str());
427
428 ofs << setw(2) << setprecision(8);
429 ofs << js;
430
431 ofs.close();
432
433 return 0;
434}
string outputFile
KM3NeT DAQ constants, bit handling, etc.
Date and time functions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Specifications of file name extensions.
std::istream & operator>>(std::istream &in, JAANET::JHead &header)
Read header from input.
Definition JHead.hh:1850
General purpose messaging.
#define ERROR(A)
Definition JMessage.hh:66
#define NOTICE(A)
Definition JMessage.hh:64
#define FATAL(A)
Definition JMessage.hh:67
int debug
debug level
Definition JSirene.cc:74
#define WARNING(A)
Definition JMessage.hh:65
ROOT I/O of application specific meta data.
Utility class to parse command line options.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition JParser.hh:2140
I/O formatting auxiliaries.
#define MAKE_STRING(A)
Make string.
Definition JPrint.hh:48
Utility class to parse parameter values.
#define gmake_property(A)
macros to convert (template) parameter to JPropertiesElement object
nlohmann::json json
Utility class to parse parameter values.
Exception for accessing a value in a collection that is outside of its range.
Utility class to parse command line options.
Definition JParser.hh:1697
std::ostream & operator<<(std::ostream &stream, const CLBCommonHeader &header)
int main()
std::istream & getline(std::istream &in, JString &object)
Read string from input stream until end of line.
Definition JString.hh:478
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary data structure for floating point format specification.
Definition JManip.hh:448
Universal product identifier (UPI).
Definition JUPI_t.hh:33
const JUPI_t & getUPI() const
Get UPI.
Definition JUPI_t.hh:101
int getNumber() const
Get serial number.
Definition JUPI_t.hh:134
int DETID
constraint
Definition JVendorHV.hh:21
Auxiliary data structure for general purpose version number.
version_type getMajorVersion() const
Get major version.
Simple wrapper for UUID.
Definition JUUID.hh:24
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition JParser.hh:67
Auxiliary class for ROOT I/O of application specific meta data.
Definition JMeta.hh:72
Auxiliary class for date and time.
std::string toString() const
Get ASCII formatted date and time.
void sub(const time_t t1)
Subtract given time.
Auxiliary data structure for alignment of data.
Definition JManip.hh:298