Jpp  17.2.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JMergeTuneHV.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <iomanip>
3 #include <vector>
4 #include <set>
5 
6 #include "JSystem/JDate.hh"
7 
8 #include "JLang/JUUID.hh"
9 
10 #include "Jeep/JPrint.hh"
11 #include "Jeep/JParser.hh"
12 #include "Jeep/JMessage.hh"
13 #include "Jeep/JProperties.hh"
14 
15 #include "JLang/JPredicate.hh"
16 
17 #include "JSupport/JMeta.hh"
19 
21 
22 #include "JDB/JDBSupportkit.hh"
23 
24 #include "JSon/JSon.hh"
25 
26 
27 /**
28  * \file
29  *
30  * Auxiliary program to merge multiple HV tuning calibrations.
31  *
32  * Note that only the calibration in the first given file will be maintained\n
33  * in case a PMT appears in multiple files.
34  * \author bjung
35  */
36 int main(int argc, char **argv)
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  JDateAndTimeFormat format = ISO8601;
80  JDateAndTime timer;
81 
82  timer->tm_sec -= elapsedTime;
83 
84 
85  // Merge high-voltage calibrations
86 
87  JDBAPIVersion DBAPIVersion;
88  set<string> testTypes;
89  string metaInfoStr = MAKE_STRING(JMeta(argc, argv));
90 
91  JHVCalibration merged;
92 
93  for (vector<string>::const_iterator fileIt = inputFiles.cbegin(); fileIt != inputFiles.cend(); ++fileIt) {
94 
95  NOTICE("Merging " << (*fileIt) << endl);
96 
97  if (isJSONFile(fileIt->c_str())) {
98 
99  json js;
100  ifstream ifs(fileIt->c_str());
101 
102  ifs >> js;
103  ifs.close();
104 
105  // Extract data
106 
107  json::const_iterator i0 = js.find(APIVersion_t);
108 
109  if (i0 != js.cend()) {
110 
111  istringstream iss(i0->get<string>());
112 
113  iss >> DBAPIVersion;
114  }
115 
116  JHVCalibration HVcals;
117 
118  json::const_iterator i1 = js.find(Data_t);
119 
120  if ( (DBAPIVersion.getMajorVersion() == 2) &&
121  (i1 != js.cend() && i1->size() > 0) ) {
122 
123  set<string>::const_iterator j = testTypes.insert(testTypes.cbegin(), (*i1)[0].at(Test_t + Type_t).get<string>());
124 
125  JHVCalibration_t::setVersion(getDBVersionTuneHV(*j));
126 
127  HVcals = (*i1)[0].at(Tests_t).get<JHVCalibration>();
128  metaInfoStr += (*i1)[0].at(Provenance_t + Info_t).at(Configuration_t).get<string>();
129 
130  } else {
131 
132  set<string>::const_iterator j = testTypes.insert(testTypes.cbegin(), js.at(Test_t + Type_t).get<string>());
133 
134  JHVCalibration_t::setVersion(getDBVersionTuneHV(*j));
135 
136  HVcals = js.at(Tests_t).get<JHVCalibration>();
137  }
138 
139  // Merge calibrations
140 
141  for (JHVCalibration::const_iterator cal = HVcals.cbegin(); cal != HVcals.cend(); ++cal) {
142 
143  if (find_if(merged.cbegin(), merged.cend(), make_predicate(&JHVCalibration_t::getNumber, cal->getNumber())) == merged.cend()) {
144  merged.push_back(*cal);
145  }
146  }
147 
148  } else {
149 
150  WARNING(*fileIt << " is not a JSON file; skip" << endl);
151  continue;
152  }
153  }
154 
155  if (testTypes.size() > 1) {
156 
157  WARNING("Ambiguous database test types; Assuming " << *testTypes.crbegin() << endl);
158 
159  } else if (testTypes.size() == 0) {
160 
161  FATAL("No database test type specified.");
162  }
163 
164 
165  json js;
166 
167  if (DBAPIVersion.getMajorVersion() == 2) {
168 
169  json error = { {Message_t, "" },
170  {Code_t, OK_t },
171  {Arguments_t, json::array() } };
172 
173  json metaData = { {Configuration_t, metaInfoStr },
174  {UUID_t, MAKE_STRING(UUID) } };
175 
176  json data = { {Provenance_t + Info_t, json(metaData) },
177  {User_t, login },
178  {Location_t, locationID },
179  {Start_t + Time_t, timer.toString (format)},
180  {End_t + Time_t, timer().toString(format)},
181  {Test_t + Type_t, *(testTypes.crbegin()) },
182  {Tests_t, json(merged) } };
183 
184  js[APIVersion_t] = MAKE_STRING(DBAPIVersion);
185  js[Data_t + Type_t] = MAKE_STRING("ProductTestSession");
186  js[Encoding_t] = MAKE_STRING("NativeJSON");
187  js[Error_t] = json(error);
188  js[Start_t] = timer.toString(format);
189  js[End_t] = timer().toString(format);
190  js[Data_t][0] = json(data);
191 
192  } else {
193 
194  js[User_t] = login;
195  js[Location_t] = locationID;
196  js[Test_t + Type_t] = *(testTypes.crbegin());
197  js[Start_t + Time_t] = timer.toString(format);
198  js[End_t + Time_t] = timer().toString(format);
199  js[Tests_t] = json(merged);
200  }
201 
202  ofstream ofs(outputFile.c_str());
203 
204  ofs << setw(2) << setprecision(8);
205  ofs << js;
206 
207  ofs.close();
208 
209  return 0;
210 }
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:1517
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
int main(int argc, char *argv[])
Definition: Main.cc:15
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:83
JDateAndTimeFormat
Date and time formats.
string outputFile
static const std::string Encoding_t
Utility class to parse parameter values.
#define MAKE_STRING(A)
Make string.
Definition: JPrint.hh:127
static const std::string Provenance_t
Date and time functions.
static const std::string Arguments_t
static const std::string Configuration_t
I/O formatting auxiliaries.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1993
std::vector< JHVCalibration_t > JHVCalibration
PMT high voltage calibration.
ROOT I/O of application specific meta data.
#define NOTICE(A)
Definition: JMessage.hh:64
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
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:67
Utility class to parse command line options.
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.
int j
Definition: JPolint.hh:703
static const std::string Error_t
static const std::string Data_t
static const std::string End_t
static const std::string Start_t
Specifications of file name extensions.
bool isJSONFile(const char *file_name)
Check file format.
int debug
debug level