Jpp  16.0.2
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Functions
JEditTuneHV.cc File Reference

Auxiliary program to treat failed HV-tuning calibrations. More...

#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <set>
#include "km3net-dataformat/online/JDAQ.hh"
#include "JSystem/JDate.hh"
#include "JLang/JUUID.hh"
#include "Jeep/JPrint.hh"
#include "Jeep/JParser.hh"
#include "Jeep/JMessage.hh"
#include "Jeep/JProperties.hh"
#include "JLang/JPredicate.hh"
#include "JSupport/JMeta.hh"
#include "JSupport/JFilenameSupportkit.hh"
#include "JDetector/JDetectorCalibration.hh"
#include "JDB/JUPI_t.hh"
#include "JDB/JVendorHV.hh"
#include "JDB/JDBSupportkit.hh"
#include "JDB/JPMTHVRunSettings.hh"
#include "JSon/JSon.hh"

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Detailed Description

Auxiliary program to treat failed HV-tuning calibrations.

Author
bjung

Definition in file JEditTuneHV.cc.

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 190 of file JEditTuneHV.cc.

191 {
192  using namespace std;
193  using namespace JPP;
194 
195  typedef JHVTable::JHVTableTypes JHVTableType_t;
196  typedef pair<JHVTableType_t, string> JHVTable_t;
197 
198  string inputFile;
199  string outputFile;
200  JHVTable_t HVtable;
201 
202  set<JUPI_t> pmtSet;
203 
204  string login;
205  string locationID;
206  int elapsedTime = 0;
207 
208  double minHV = -1500;
209  double maxHV = - 800;
210 
211  int debug;
212 
213  try {
214 
215  JProperties properties;
216 
217  properties.insert(gmake_property(login));
218  properties.insert(gmake_property(locationID));
219  properties.insert(gmake_property(elapsedTime));
220 
221  JProperties settings;
222 
223  settings.insert(gmake_property(minHV));
224  settings.insert(gmake_property(maxHV));
225 
226  JParser<> zap("Auxiliary program to treat failed high-voltage tuning results.");
227 
228  zap['f'] = make_field(inputFile, "input file");
229  zap['o'] = make_field(outputFile, "output file");
230  zap['b'] = make_field(HVtable, "ASCII HV table") = JPARSER::initialised();
231  zap['P'] = make_field(pmtSet, "Set of PMT UPIs") = JPARSER::initialised();
232  zap['#'] = make_field(properties, "database information") = JPARSER::initialised();
233  zap['@'] = make_field(settings, "HV limits") = JPARSER::initialised();
234  zap['d'] = make_field(debug, "debug") = 2;
235 
236  zap(argc, argv);
237 
238  } catch(const exception &error) {
239 
240  FATAL(error.what() << endl);
241  }
242 
243  if (login.empty() || locationID.empty()) {
244  FATAL("Missing user information (please specify via -#login and -#locationID).");
245  }
246 
247 
248  const JUUID& UUID = JUUID::rndm();
249 
250  JDateAndTimeFormat format = ISO8601;
251  JDateAndTime timer;
252 
253  timer->tm_sec -= elapsedTime;
254 
255 
256  // Edit high-voltage calibrations
257 
258  JDBAPIVersion DBAPIVersion;
259  string DBTestType;
260  string metaInfoStr = MAKE_STRING(JMeta(argc, argv));
261 
262  JHVCalibration toEdit;
263 
264  if (isJSONFile(inputFile.c_str())) {
265 
266  json js;
267 
268  ifstream ifs(inputFile.c_str());
269 
270  ifs >> js;
271  ifs.close();
272 
273  // Extract data
274 
275  json::const_iterator i0 = js.find(APIVersion_t);
276 
277  if (i0 != js.cend()) {
278 
279  istringstream iss(i0->get<string>());
280 
281  iss >> DBAPIVersion;
282  }
283 
284  JHVCalibration HVcals;
285 
286  json::const_iterator i1 = js.find(Data_t);
287 
288  if ((DBAPIVersion.getMajorVersion() == 2) &&
289  (i1 != js.cend() && i1->size() > 0)) {
290 
291  DBTestType = (*i1)[0].at(Test_t + Type_t).get<string>();
292 
293  JHVCalibration_t::setVersion(getDBVersionTuneHV(DBTestType));
294 
295  HVcals = (*i1)[0].at(Tests_t).get<JHVCalibration>();
296  metaInfoStr += (*i1)[0].at(Provenance_t + Info_t).at(Configuration_t).get<string>();
297 
298  } else {
299 
300  DBTestType = js.at(Test_t + Type_t).get<string>();
301 
302  JHVCalibration_t::setVersion(getDBVersionTuneHV(DBTestType));
303 
304  HVcals = js.at(Tests_t).get<JHVCalibration>();
305  }
306 
307  // Collect PMTs which need to be edited
308 
309  if (pmtSet.empty()) {
310 
311  for (JHVCalibration::iterator it = HVcals.begin(); it != HVcals.end(); ++it) {
312 
313  if (it->result != OK_t) {
314  toEdit.push_back(*it);
315  }
316  }
317 
318  } else {
319 
320  for (set<JUPI_t>::const_iterator it = pmtSet.cbegin(); it != pmtSet.cend(); ++it) {
321 
322  JHVCalibration::iterator pmt = find_if(HVcals.begin(), HVcals.end(),
323  make_predicate(&JHVCalibration_t::getNumber, it->getNumber()));
324  if (pmt != HVcals.end()) {
325 
326  if (pmt->result == OK_t) {
327  WARNING("Editing " << OK_t << " result for " << pmt->getUPI() << endl);
328  }
329 
330  toEdit.push_back(*pmt);
331  }
332  }
333  }
334 
335  } else {
336 
337  ERROR(inputFile << " is not a JSON file." << endl);
338  }
339 
340 
341  if (!HVtable.second.empty()) {
342 
343  NOTICE("Setting " << (HVtable.first == JHVTableType_t::VENDOR_HV ? "vendor " : "run-specific ") <<
344  "PMT high-voltages from file " << HVtable.second << "..." << endl);
345 
346  JHVTable getHV(HVtable.second.c_str(), HVtable.first);
347 
348  for (JHVCalibration::iterator i = toEdit.begin(); i != toEdit.end(); ++i) {
349 
350  const JUPI_t& upi = i->getUPI();
351  const double HV = getHV(upi.getNumber());
352 
353  if (HV > minHV && HV < maxHV) {
354 
355  i->supplyVoltage = getHV(upi.getNumber());
356  i->result = OK_t;
357 
358  } else {
359 
360  WARNING("Invalid high-voltage for PMT " << upi << " (" << FIXED(7,1) << HV <<
361  " not within [ " << FIXED(7,1) << minHV << ", " << FIXED(7,1) << maxHV << "]); skip." << endl);
362  }
363  }
364 
365  } else {
366 
367  NOTICE("Setting high-voltages manually..." << endl);
368 
369  for (JHVCalibration::iterator i = toEdit.begin(); i != toEdit.end(); ++i) {
370 
371  NOTICE("Please specify high-voltage for " << RIGHT(30) << i->getUPI() << ":" << endl);
372 
373  double manualHV;
374  cin >> manualHV;
375 
376  while (manualHV < minHV || manualHV > maxHV) {
377 
378  WARNING("Specified high-voltage is not within range [" <<
379  FIXED(7,1) << minHV << ", " << FIXED(7,1) << maxHV <<
380  "]; Please specify again." << endl);
381 
382  cin >> manualHV;
383  }
384 
385  i->supplyVoltage = manualHV;
386  i->result = OK_t;
387  }
388  }
389 
390 
391  json js;
392 
393  if (DBAPIVersion.getMajorVersion() == 2) {
394 
395  json error = { {Message_t, "" },
396  {Code_t, OK_t },
397  {Arguments_t, json::array() } };
398 
399  json metaData = { {Configuration_t, metaInfoStr },
400  {UUID_t, MAKE_STRING(UUID) } };
401 
402  json data = { {Provenance_t + Info_t, json(metaData) },
403  {User_t, login },
404  {Location_t, locationID },
405  {Start_t + Time_t, timer.toString (format)},
406  {End_t + Time_t, timer().toString(format)},
407  {Test_t + Type_t, DBTestType },
408  {Tests_t, json(toEdit) } };
409 
410  js[APIVersion_t] = MAKE_STRING(DBAPIVersion);
411  js[Data_t + Type_t] = MAKE_STRING("ProductTestSession");
412  js[Encoding_t] = MAKE_STRING("NativeJSON");
413  js[Error_t] = json(error);
414  js[Start_t] = timer.toString(format);
415  js[End_t] = timer().toString(format);
416  js[Data_t][0] = json(data);
417 
418  } else {
419 
420  js[User_t] = login;
421  js[Location_t] = locationID;
422  js[Test_t + Type_t] = DBTestType;
423  js[Start_t + Time_t] = timer.toString(format);
424  js[End_t + Time_t] = timer().toString(format);
425  js[Tests_t] = json(toEdit);
426  }
427 
428 
429  ofstream ofs(outputFile.c_str());
430 
431  ofs << setw(2) << setprecision(8);
432  ofs << js;
433 
434  ofs.close();
435 
436  return 0;
437 }
static JDBTestTypesTuneHV & getDBVersionTuneHV
Function object for looking up the HV-tuning database version number corresponding to a specific test...
Utility class to parse command line options.
Definition: JParser.hh:1500
static const std::string UUID_t
#define WARNING(A)
Definition: JMessage.hh:65
JPredicate< JResult_t T::*, JComparison::eq > make_predicate(JResult_t T::*member, const JResult_t value)
Helper method to create predicate for data member.
Definition: JPredicate.hh:128
static const std::string Time_t
#define gmake_property(A)
macro to convert (template) parameter to JPropertiesElement object
static const std::string Tests_t
static const std::string OK_t
Utility class to parse parameter values.
Definition: JProperties.hh:496
static const std::string Location_t
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:66
JDateAndTimeFormat
Date and time formats.
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:446
string outputFile
static const std::string Encoding_t
#define MAKE_STRING(A)
Make string.
Definition: JPrint.hh:142
static const std::string Provenance_t
static const std::string Arguments_t
static const std::string Configuration_t
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1961
std::vector< JHVCalibration_t > JHVCalibration
PMT high voltage calibration.
#define NOTICE(A)
Definition: JMessage.hh:64
#define ERROR(A)
Definition: JMessage.hh:66
static const std::string Code_t
static const std::string Test_t
ISO-8601 standard.
static const std::string Info_t
static const std::string Message_t
int debug
debug level
Definition: JSirene.cc:63
#define FATAL(A)
Definition: JMessage.hh:67
nlohmann::json json
static const std::string Type_t
static const std::string APIVersion_t
static const std::string User_t
Detector calibration key words for JSON I/O.
static const std::string Error_t
static const std::string Data_t
static const std::string End_t
static const std::string Start_t
bool isJSONFile(const char *file_name)
Check file format.