#include #include #include #include #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" /** * \file * * Auxiliary program to merge multiple HV tuning calibrations. * * Note that only the calibration in the first given file will be maintained\n * in case a PMT appears in multiple files. * \author bjung */ int main(int argc, char **argv) { using namespace std; using namespace JPP; string login; string locationID; int elapsedTime = 0; vector inputFiles; string outputFile; int debug; try { JProperties properties; properties.insert(gmake_property(login)); properties.insert(gmake_property(locationID)); properties.insert(gmake_property(elapsedTime)); JParser<> zap("Auxiliary program to merge high-voltage calibration results."); zap['f'] = make_field(inputFiles, "input files"); zap['o'] = make_field(outputFile, "output file"); zap['#'] = make_field(properties, "database information") = JPARSER::initialised(); zap['d'] = make_field(debug, "debug") = 2; zap(argc, argv); } catch(const exception &error) { FATAL(error.what() << endl); } if (login.empty() || locationID.empty()) { FATAL("Missing user information (please specify via -#login and -#locationID)." << endl); } const JUUID& UUID = JUUID::rndm(); JDateAndTime timer; timer.sub(elapsedTime); // Merge high-voltage calibrations JDBAPIVersion DBAPIVersion; set testTypes; string metaInfoStr = MAKE_STRING(JMeta(argc, argv)); JHVCalibration merged; for (vector::const_iterator fileIt = inputFiles.cbegin(); fileIt != inputFiles.cend(); ++fileIt) { NOTICE("Merging " << (*fileIt) << endl); if (isJSONFile(fileIt->c_str())) { json js; ifstream ifs(fileIt->c_str()); ifs >> js; ifs.close(); // Extract data json::const_iterator i0 = js.find(APIVersion_t); if (i0 != js.cend()) { istringstream iss(i0->get()); iss >> DBAPIVersion; } JHVCalibration HVcals; json::const_iterator i1 = js.find(Data_t); if ( (DBAPIVersion.getMajorVersion() == 2) && (i1 != js.cend() && i1->size() > 0) ) { set::const_iterator j = testTypes.insert(testTypes.cbegin(), (*i1)[0].at(Test_t + Type_t).get()); JHVCalibration_t::setVersion(getDBVersionTuneHV(*j)); HVcals = (*i1)[0].at(Tests_t).get(); metaInfoStr += (*i1)[0].at(Provenance_t + Info_t).at(Configuration_t).get(); } else { set::const_iterator j = testTypes.insert(testTypes.cbegin(), js.at(Test_t + Type_t).get()); JHVCalibration_t::setVersion(getDBVersionTuneHV(*j)); HVcals = js.at(Tests_t).get(); } // Merge calibrations for (JHVCalibration::const_iterator cal = HVcals.cbegin(); cal != HVcals.cend(); ++cal) { if (find_if(merged.cbegin(), merged.cend(), make_predicate(&JHVCalibration_t::getNumber, cal->getNumber())) == merged.cend()) { merged.push_back(*cal); } } } else { WARNING(*fileIt << " is not a JSON file; skip" << endl); continue; } } if (testTypes.size() > 1) { WARNING("Ambiguous database test types; Assuming " << *testTypes.crbegin() << endl); } else if (testTypes.size() == 0) { FATAL("No database test type specified."); } json js; if (DBAPIVersion.getMajorVersion() == 2) { json error = { {Message_t, "" }, {Code_t, OK_t }, {Arguments_t, json::array() } }; json metaData = { {Configuration_t, metaInfoStr }, {UUID_t, MAKE_STRING(UUID) } }; json data = { {Provenance_t + Info_t, json(metaData) }, {User_t, login }, {Location_t, locationID }, {Start_t + Time_t, timer.toString() }, {End_t + Time_t, timer().toString() }, {Test_t + Type_t, *(testTypes.crbegin()) }, {Tests_t, json(merged) } }; js[APIVersion_t] = MAKE_STRING(DBAPIVersion); js[Data_t + Type_t] = MAKE_STRING("ProductTestSession"); js[Encoding_t] = MAKE_STRING("NativeJSON"); js[Error_t] = json(error); js[Start_t] = timer.toString(); js[End_t] = timer().toString(); js[Data_t][0] = json(data); } else { js[User_t] = login; js[Location_t] = locationID; js[Test_t + Type_t] = *(testTypes.crbegin()); js[Start_t + Time_t] = timer.toString(); js[End_t + Time_t] = timer().toString(); js[Tests_t] = json(merged); } ofstream ofs(outputFile.c_str()); ofs << setw(2) << setprecision(8); ofs << js; ofs.close(); return 0; }