Jpp  18.3.0
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/JDateAndTime.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 189 of file JEditTuneHV.cc.

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