Jpp  18.2.0-rc.1
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Functions
JMergeTuneHV.cc File Reference

Auxiliary program to merge multiple HV tuning calibrations. More...

#include <iostream>
#include <iomanip>
#include <vector>
#include <set>
#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/JDBSupportkit.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 merge multiple HV tuning calibrations.

Note that only the calibration in the first given file will be maintained
in case a PMT appears in multiple files.

Author
bjung

Definition in file JMergeTuneHV.cc.

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 36 of file JMergeTuneHV.cc.

37 {
38  using namespace std;
39  using namespace JPP;
40 
41  string login;
42  string locationID;
43  int elapsedTime = 0;
44 
45  vector<string> inputFiles;
46  string outputFile;
47 
48  int debug;
49 
50  try {
51 
52  JProperties properties;
53 
54  properties.insert(gmake_property(login));
55  properties.insert(gmake_property(locationID));
56  properties.insert(gmake_property(elapsedTime));
57 
58  JParser<> zap("Auxiliary program to merge high-voltage calibration results.");
59 
60  zap['f'] = make_field(inputFiles, "input files");
61  zap['o'] = make_field(outputFile, "output file");
62  zap['#'] = make_field(properties, "database information") = JPARSER::initialised();
63  zap['d'] = make_field(debug, "debug") = 2;
64 
65  zap(argc, argv);
66 
67  } catch(const exception &error) {
68 
69  FATAL(error.what() << endl);
70  }
71 
72  if (login.empty() || locationID.empty()) {
73  FATAL("Missing user information (please specify via -#login and -#locationID)." << endl);
74  }
75 
76 
77  const JUUID& UUID = JUUID::rndm();
78 
79  JDateAndTime timer;
80 
81  timer.sub(elapsedTime);
82 
83 
84  // Merge high-voltage calibrations
85 
86  JDBAPIVersion DBAPIVersion;
87  set<string> testTypes;
88  string metaInfoStr = MAKE_STRING(JMeta(argc, argv));
89 
90  JHVCalibration merged;
91 
92  for (vector<string>::const_iterator fileIt = inputFiles.cbegin(); fileIt != inputFiles.cend(); ++fileIt) {
93 
94  NOTICE("Merging " << (*fileIt) << endl);
95 
96  if (isJSONFile(fileIt->c_str())) {
97 
98  json js;
99  ifstream ifs(fileIt->c_str());
100 
101  ifs >> js;
102  ifs.close();
103 
104  // Extract data
105 
106  json::const_iterator i0 = js.find(APIVersion_t);
107 
108  if (i0 != js.cend()) {
109 
110  istringstream iss(i0->get<string>());
111 
112  iss >> DBAPIVersion;
113  }
114 
115  JHVCalibration HVcals;
116 
117  json::const_iterator i1 = js.find(Data_t);
118 
119  if ( (DBAPIVersion.getMajorVersion() == 2) &&
120  (i1 != js.cend() && i1->size() > 0) ) {
121 
122  set<string>::const_iterator j = testTypes.insert(testTypes.cbegin(), (*i1)[0].at(Test_t + Type_t).get<string>());
123 
124  JHVCalibration_t::setVersion(getDBVersionTuneHV(*j));
125 
126  HVcals = (*i1)[0].at(Tests_t).get<JHVCalibration>();
127  metaInfoStr += (*i1)[0].at(Provenance_t + Info_t).at(Configuration_t).get<string>();
128 
129  } else {
130 
131  set<string>::const_iterator j = testTypes.insert(testTypes.cbegin(), js.at(Test_t + Type_t).get<string>());
132 
133  JHVCalibration_t::setVersion(getDBVersionTuneHV(*j));
134 
135  HVcals = js.at(Tests_t).get<JHVCalibration>();
136  }
137 
138  // Merge calibrations
139 
140  for (JHVCalibration::const_iterator cal = HVcals.cbegin(); cal != HVcals.cend(); ++cal) {
141 
142  if (find_if(merged.cbegin(), merged.cend(), make_predicate(&JHVCalibration_t::getNumber, cal->getNumber())) == merged.cend()) {
143  merged.push_back(*cal);
144  }
145  }
146 
147  } else {
148 
149  WARNING(*fileIt << " is not a JSON file; skip" << endl);
150  continue;
151  }
152  }
153 
154  if (testTypes.size() > 1) {
155 
156  WARNING("Ambiguous database test types; Assuming " << *testTypes.crbegin() << endl);
157 
158  } else if (testTypes.size() == 0) {
159 
160  FATAL("No database test type specified.");
161  }
162 
163 
164  json js;
165 
166  if (DBAPIVersion.getMajorVersion() == 2) {
167 
168  json error = { {Message_t, "" },
169  {Code_t, OK_t },
170  {Arguments_t, json::array() } };
171 
172  json metaData = { {Configuration_t, metaInfoStr },
173  {UUID_t, MAKE_STRING(UUID) } };
174 
175  json data = { {Provenance_t + Info_t, json(metaData) },
176  {User_t, login },
177  {Location_t, locationID },
178  {Start_t + Time_t, timer.toString() },
179  {End_t + Time_t, timer().toString() },
180  {Test_t + Type_t, *(testTypes.crbegin()) },
181  {Tests_t, json(merged) } };
182 
183  js[APIVersion_t] = MAKE_STRING(DBAPIVersion);
184  js[Data_t + Type_t] = MAKE_STRING("ProductTestSession");
185  js[Encoding_t] = MAKE_STRING("NativeJSON");
186  js[Error_t] = json(error);
187  js[Start_t] = timer.toString();
188  js[End_t] = timer().toString();
189  js[Data_t][0] = json(data);
190 
191  } else {
192 
193  js[User_t] = login;
194  js[Location_t] = locationID;
195  js[Test_t + Type_t] = *(testTypes.crbegin());
196  js[Start_t + Time_t] = timer.toString();
197  js[End_t + Time_t] = timer().toString();
198  js[Tests_t] = json(merged);
199  }
200 
201  ofstream ofs(outputFile.c_str());
202 
203  ofs << setw(2) << setprecision(8);
204  ofs << js;
205 
206  ofs.close();
207 
208  return 0;
209 }
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
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
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
int j
Definition: JPolint.hh:703
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