Jpp 21.0.0-rc.1-88-g0130508c4
the software that should make you happy
Loading...
Searching...
No Matches
JMergeTuneHV.cc
Go to the documentation of this file.
1#include <iostream>
2#include <iomanip>
3#include <vector>
4#include <set>
5
7
8#include "JLang/JUUID.hh"
9#include "JLang/JPredicate.hh"
10
11#include "JSupport/JMeta.hh"
13
15
16#include "JDB/JDBSupportkit.hh"
17
18#include "JSon/JSon.hh"
19
20#include "Jeep/JPrint.hh"
21#include "Jeep/JParser.hh"
22#include "Jeep/JMessage.hh"
23#include "Jeep/JProperties.hh"
24
25
26/**
27 * \file
28 *
29 * Auxiliary program to merge multiple HV tuning calibrations.
30 *
31 * Note that only the calibration in the first given file will be maintained\n
32 * in case a PMT appears in multiple files.
33 * \author bjung
34 */
35int main(int argc, char **argv)
36{
37 using namespace std;
38 using namespace JPP;
39
40 string login;
41 string locationID;
42 int elapsedTime = 0;
43
44 vector<string> inputFiles;
45 string outputFile;
46
47 int debug;
48
49 try {
50
51 JProperties properties;
52
53 properties.insert(gmake_property(login));
54 properties.insert(gmake_property(locationID));
55 properties.insert(gmake_property(elapsedTime));
56
57 JParser<> zap("Auxiliary program to merge high-voltage calibration results.");
58
59 zap['f'] = make_field(inputFiles, "input files");
60 zap['o'] = make_field(outputFile, "output file");
61 zap['#'] = make_field(properties, "database information") = JPARSER::initialised();
62 zap['d'] = make_field(debug, "debug") = 2;
63
64 zap(argc, argv);
65
66 } catch(const exception &error) {
67
68 FATAL(error.what() << endl);
69 }
70
71 if (login.empty() || locationID.empty()) {
72 FATAL("Missing user information (please specify via -#login and -#locationID)." << endl);
73 }
74
75
76 const JUUID& UUID = JUUID::rndm();
77
78 JDateAndTime timer;
79
80 timer.sub(elapsedTime);
81
82
83 // Merge high-voltage calibrations
84
85 JDBAPIVersion DBAPIVersion;
86 set<string> testTypes;
87 string metaInfoStr = MAKE_STRING(JMeta(argc, argv));
88
89 JHVCalibration merged;
90
91 for (vector<string>::const_iterator fileIt = inputFiles.cbegin(); fileIt != inputFiles.cend(); ++fileIt) {
92
93 NOTICE("Merging " << (*fileIt) << endl);
94
95 if (isJSONFile(fileIt->c_str())) {
96
97 json js;
98 ifstream ifs(fileIt->c_str());
99
100 ifs >> js;
101 ifs.close();
102
103 // Extract data
104
105 json::const_iterator i0 = js.find(APIVersion_t);
106
107 if (i0 != js.cend()) {
108
109 istringstream iss(i0->get<string>());
110
111 iss >> DBAPIVersion;
112 }
113
114 JHVCalibration HVcals;
115
116 json::const_iterator i1 = js.find(Data_t);
117
118 if ( (DBAPIVersion.getMajorVersion() == 2) &&
119 (i1 != js.cend() && i1->size() > 0) ) {
120
121 set<string>::const_iterator j = testTypes.insert(testTypes.cbegin(), (*i1)[0].at(Test_t + Type_t).get<string>());
122
123 JHVCalibration_t::setVersion(getDBVersionTuneHV(*j));
124
125 HVcals = (*i1)[0].at(Tests_t).get<JHVCalibration>();
126 metaInfoStr += (*i1)[0].at(Provenance_t + Info_t).at(Configuration_t).get<string>();
127
128 } else {
129
130 set<string>::const_iterator j = testTypes.insert(testTypes.cbegin(), js.at(Test_t + Type_t).get<string>());
131
132 JHVCalibration_t::setVersion(getDBVersionTuneHV(*j));
133
134 HVcals = js.at(Tests_t).get<JHVCalibration>();
135 }
136
137 // Merge calibrations
138
139 for (JHVCalibration::const_iterator cal = HVcals.cbegin(); cal != HVcals.cend(); ++cal) {
140
141 if (find_if(merged.cbegin(), merged.cend(), make_predicate(&JHVCalibration_t::getNumber, cal->getNumber())) == merged.cend()) {
142 merged.push_back(*cal);
143 }
144 }
145
146 } else {
147
148 WARNING(*fileIt << " is not a JSON file; skip" << endl);
149 continue;
150 }
151 }
152
153 if (testTypes.size() > 1) {
154
155 WARNING("Ambiguous database test types; Assuming " << *testTypes.crbegin() << endl);
156
157 } else if (testTypes.size() == 0) {
158
159 FATAL("No database test type specified.");
160 }
161
162
163 json js;
164
165 if (DBAPIVersion.getMajorVersion() == 2) {
166
167 json error = { {Message_t, "" },
168 {Code_t, OK_t },
169 {Arguments_t, json::array() } };
170
171 json metaData = { {Configuration_t, metaInfoStr },
172 {UUID_t, MAKE_STRING(UUID) } };
173
174 json data = { {Provenance_t + Info_t, json(metaData) },
175 {User_t, login },
176 {Location_t, locationID },
177 {Start_t + Time_t, timer.toString() },
178 {End_t + Time_t, timer().toString() },
179 {Test_t + Type_t, *(testTypes.crbegin()) },
180 {Tests_t, json(merged) } };
181
182 js[APIVersion_t] = MAKE_STRING(DBAPIVersion);
183 js[Data_t + Type_t] = MAKE_STRING("ProductTestSession");
184 js[Encoding_t] = MAKE_STRING("NativeJSON");
185 js[Error_t] = json(error);
186 js[Start_t] = timer.toString();
187 js[End_t] = timer().toString();
188 js[Data_t][0] = json(data);
189
190 } else {
191
192 js[User_t] = login;
193 js[Location_t] = locationID;
194 js[Test_t + Type_t] = *(testTypes.crbegin());
195 js[Start_t + Time_t] = timer.toString();
196 js[End_t + Time_t] = timer().toString();
197 js[Tests_t] = json(merged);
198 }
199
200 ofstream ofs(outputFile.c_str());
201
202 ofs << setw(2) << setprecision(8);
203 ofs << js;
204
205 ofs.close();
206
207 return 0;
208}
string outputFile
Date and time functions.
Specifications of file name extensions.
General purpose messaging.
#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.
Utility class to parse command line options.
Definition JParser.hh:1697
int main()
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
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.