Jpp
 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/JTime.hh"
7 
8 #include "Jeep/JPrint.hh"
9 #include "Jeep/JParser.hh"
10 #include "Jeep/JMessage.hh"
11 
12 #include "JLang/JPredicate.hh"
13 
15 
17 
18 #include "JDB/JDB.hh"
19 #include "JDB/JDBToolkit.hh"
20 #include "JDB/JDBincludes.hh"
21 #include "JDB/JSelector.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 usr;
42  string pwd;
43  string cookie;
44 
45  vector<string> inputFiles;
46  string outputFile;
47 
48  int debug;
49 
50  try {
51 
52  JParser<> zap("Auxiliary program to merge high-voltage calibration results.");
53 
54  zap['u'] = make_field(usr) = "";
55  zap['!'] = make_field(pwd) = "";
56  zap['C'] = make_field(cookie) = "";
57  zap['f'] = make_field(inputFiles, "input files");
58  zap['o'] = make_field(outputFile, "output file");
59  zap['d'] = make_field(debug, "debug") = 2;
60 
61  zap(argc, argv);
62 
63  } catch(const exception &error) {
64 
65  FATAL(error.what() << endl);
66  }
67 
68  JDateAndTime timer;
69 
70  // Retrieve personalia
71  JPersons person;
72 
73  try {
74 
75  JDB::reset(usr, pwd, cookie);
76 
77  JSelector selector = getSelector<JPersons>(JDB::get()->User());
78  ResultSet& rs = getResultSet(getTable<JPersons>(), selector);
79  rs >> person;
80  rs.Close();
81 
82  } catch (const exception& error) {
83 
84  FATAL(error.what() << endl);
85  }
86 
87  // Merge high-voltage calibrations
88  JHVCalibration merged;
89  set<string> testTypes;
90 
91  for (vector<string>::const_iterator fileIt = inputFiles.cbegin(); fileIt != inputFiles.cend(); ++fileIt) {
92 
93  if (isJSONFile(fileIt->c_str())) {
94 
95  json js;
96  ifstream ifs(fileIt->c_str());
97 
98  ifs >> js;
99  ifs.close();
100 
101  json::const_iterator DBtype = js.find(Test_t + Type_t);
102 
103  if (DBtype != js.end()) {
104 
105  testTypes.insert(DBtype->get<string>());
106 
107  } else {
108 
109  WARNING("No database test type specified for " << *fileIt << endl);
110  }
111 
112  JHVCalibration HVcals;
113 
114  json::const_iterator data = js.find(Tests_t);
115 
116  if (data != js.end()) {
117 
118  HVcals = data->get<JHVCalibration>();
119 
120  } else {
121 
122  WARNING("HV calibration data for " << *fileIt << "could not be found; skip." << endl);
123  continue;
124  }
125 
126  for (JHVCalibration::const_iterator cal = HVcals.cbegin(); cal != HVcals.cend(); ++cal) {
127 
128  if (find_if(merged.cbegin(), merged.cend(), make_predicate(&JHVCalibration_t::getNumber, cal->getNumber())) == merged.cend()) {
129  merged.push_back(*cal);
130  }
131  }
132 
133  } else {
134 
135  WARNING(*fileIt << " is not a JSON file; skip" << endl);
136  continue;
137  }
138  }
139 
140  if (testTypes.size() > 1) {
141 
142  WARNING("Database test type is ambiguous; Assuming " << *testTypes.cbegin() << endl);
143 
144  } else if (testTypes.size() == 0) {
145 
146  ERROR("No database test type specified.");
147  }
148 
149  json js;
150 
151  js[User_t] = person.LOGIN;
152  js[Location_t] = person.LOCATIONID;
153  js[StartTime_t] = timer.toString();
154  js[EndTime_t] = timer().toString();
155  js[Test_t + Type_t] = *testTypes.cbegin();
156  js[Tests_t] = json(merged);
157 
158  ofstream ofs(outputFile.c_str());
159 
160  ofs << setw(2) << setprecision(8);
161  ofs << js;
162 
163  ofs.close();
164 
165  return 0;
166 }
Utility class to parse command line options.
Definition: JParser.hh:1500
#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 Tests_t
T get(const JHead &header)
Get object from header.
static const std::string Location_t
string outputFile
I/O formatting auxiliaries.
#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 ERROR(A)
Definition: JMessage.hh:66
static const std::string Test_t
int debug
debug level
Definition: JSirene.cc:63
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:67
void reset(T &value)
Reset value.
static const std::string EndTime_t
Utility class to parse command line options.
nlohmann::json json
static const std::string Type_t
ResultSet & getResultSet(const std::string &query)
Get result set.
Definition: JDB.hh:269
static const std::string StartTime_t
static const std::string User_t
Detector calibration key words for JSON I/O.
System time information.
Specifications of file name extensions.
bool isJSONFile(const char *file_name)
Check file format.
int main(int argc, char *argv[])
Definition: Main.cpp:15