#include #include #include #include #include #include #include #include "TROOT.h" #include "TFile.h" #include "TGraph.h" #include "TNamed.h" #include "TString.h" #include "TRegexp.h" #include "JROOT/JGraph.hh" #include "JDB/JDB.hh" #include "JDB/JSelector.hh" #include "JDB/JSelectorSupportkit.hh" #include "JDB/JDBToolkit.hh" #include "JDB/JDetectorIntegration_t.hh" #include "JDB/JProductRouter.hh" #include "JDB/JAllParams.hh" #include "JDB/JDatalogNumbers.hh" #include "JDB/JDatalog.hh" #include "JDB/JSupport.hh" #include "JTools/JRange.hh" #include "JSupport/JFileRecorder.hh" #include "JLang/JLangToolkit.hh" #include "Jeep/JPrint.hh" #include "Jeep/JParser.hh" #include "Jeep/JMessage.hh" namespace { const char FILTER = '!'; //!< negate filter option } /** * \file * * Auxiliary program to convert slow control data from the database to ROOT TTree. * \author mdejong */ int main(int argc, char **argv) { using namespace std; using namespace JPP; typedef JRange JRange_t; JFileRecorder::typelist> outputFile; JServer server; string usr; string pwd; string cookie; string detid; JRange_t runs = JRange_t::DEFAULT_RANGE(); long long int numberOfRows; vector filter; set locate; int prescale; bool graph; JSelector selection; int debug; locate.insert("pmt_id"); locate.insert("pmt_threshold"); locate.insert("pmt_threshold@"); locate.insert("pmt_highvolt"); locate.insert("pmt_highvolt@"); try { JParser<> zap("Auxiliary program to convert slow control data from the database to ROOT TTree."); zap['s'] = make_field(server) = getServernames(); zap['u'] = make_field(usr) = ""; zap['!'] = make_field(pwd) = ""; zap['C'] = make_field(cookie) = ""; zap['o'] = make_field(outputFile); zap['D'] = make_field(detid, "Detector identifier"); zap['R'] = make_field(runs, "Run range"); zap['n'] = make_field(numberOfRows, "Maximal number of rows") = numeric_limits::max(); zap['F'] = make_field(filter, "Filter parameter") = JPARSER::initialised(); zap['L'] = make_field(locate, "Locate parameter") = JPARSER::initialised(); zap['P'] = make_field(prescale, "Prescale sampling of data") = 1; zap['G'] = make_field(graph, "Write TGraph instead of TTree"); zap['@'] = make_field(selection, "Possible selections: " << JEEPZ() << getColumns()) = JPARSER::initialised(); zap['d'] = make_field(debug) = 2; zap(argc, argv); } catch(const exception &error) { FATAL(error.what() << endl); } filter.push_back(MAKE_STRING(FILTER << "CLBControlTimeoutMS")); if (runs.getUpperLimit() < runs.getLowerLimit()) { runs.setUpperLimit(runs.getLowerLimit()); } if (prescale < 1) { WARNING("Setting prescale to one." << endl); prescale = 1; } typedef map map_type; vector< vector< map_type > > data; map > > counts; set strings; set floors; map warnings; map errors; map unit; outputFile.open(); if (!outputFile.is_open()) { FATAL("Error opening file: " << outputFile << endl); } try { JDB::reset(usr, pwd, cookie); detid = getDetector(detid); JDetectorIntegration_t detector; { DEBUG("Reading database table " << getTable() << endl); ResultSet& rs = getResultSet(getTable()); if (! (rs >> detector)) { THROW(JDatabaseException, "Error reading " << getTable()); } } DEBUG("Number of integrated products (before) " << right << detector.size() << endl); detector.configure(detid); DEBUG("Number of integrated products (after) " << right << detector.size() << endl); const JProductRouter router(detector, PBS::CLB_SEQUENCES); { DEBUG("Reading database table " << getTable() << endl); ResultSet& rs = getResultSet(getTable()); for (JAllParams parameters; rs >> parameters; ) { // "INTEGER_UNIT_SCALE // For real and string parameters, this is meaningless and must be NULL. // For integer parameters, this is 0 if the value is an index to TB_PARAMETER_DISCRETE_REAL; // otherwise this is the positive factor to multiply by the value to obtain the parameter value." if (to_upper(parameters.TYPE) == "INTEGER" && parameters.INTEGER_UNIT_SCALE > 0.0) { unit[to_upper(parameters.NAME)] = parameters.INTEGER_UNIT_SCALE; } } rs.Close(); } { DEBUG("Reading database table " << getTable() << endl); selection += getSelector(detid, runs.getLowerLimit(), runs.getUpperLimit()); ResultSet& rs = getResultSet(getTable(), selection); long long int counter = 0; for (JDatalogNumbers parameters; rs >> parameters && counter != numberOfRows; ++counter) { STATUS(setw(10) << counter << '\r'); DEBUG(endl); const JUPI_t upi = parameters.SOURCE_NAME; const JLocation_t& location = router.getLocation(upi); if (location != JLocation_t()) { strings.insert(location.string); floors .insert(location.floor); bool status = true; for (vector::const_iterator i = filter.begin(); i != filter.end(); ++i) { if (*i != "") { bool nos = (*i)[0] == FILTER; TRegexp regexp = (nos ? i->substr(1).c_str() : i->c_str()); if (TString(parameters.PARAMETER_NAME).Contains(regexp)) { status = !nos; } } } if (status) { if (location.string >= (int) data.size()) { data.resize(location.string + 1); } if (location.floor >= (int) data[location.string].size()) { data[location.string].resize(location.floor + 1); } string buffer = parameters.PARAMETER_NAME; if (locate.count(buffer) != 0) { buffer += MAKE_STRING("[" << FILL(2,'0') << location.position << "]"); } JGraph_t& g1 = data[location.string][location.floor][buffer]; double factor = 1.0; map::const_iterator p = unit.find(to_upper(parameters.PARAMETER_NAME)); if (p != unit.end()) factor = p->second; else errors[parameters.PARAMETER_NAME] += 1; if ((counts[location.string][location.floor][buffer]++)%prescale == 0) { if (!graph) { outputFile.put(JDatalog(parameters.RUN, location.string, location.floor, location.position, parameters.PARAMETER_NAME, parameters.UNIXTIME, parameters.DATA_VALUE * factor)); } else { g1.put(parameters.getTime(), parameters.DATA_VALUE * factor); } } } } else { warnings[parameters.PARAMETER_NAME] += 1; } } STATUS(endl); } } catch(const exception& error) { ERROR(error.what() << endl); } if (debug >= warning_t && !warnings.empty()) { cout << endl << "Parameters without location in detector." << endl; for (map::const_iterator i = warnings.begin(); i != warnings.end(); ++i) { cout << left << setw(48) << i->first << ' ' << setw(6) << i->second << endl; } } if (!errors.empty()) { cout << endl << "Parameters without scale." << endl; for (map::const_iterator i = errors.begin(); i != errors.end(); ++i) { cout << left << setw(48) << i->first << ' ' << setw(6) << i->second << endl; } } if (graph) { for (size_t string = 0; string != data.size(); ++string) { for (size_t floor = 0; floor != data[string].size(); ++floor) { for (map_type::iterator i = data[string][floor].begin(); i != data[string][floor].end(); ++i) { JGraph g1(i->second, MAKE_CSTRING(FILL(4,'0') << string << '.' << FILL(2,'0') << floor << '.' << setfill(' ') << i->first)); const JRange range(i->second.Y.begin(), i->second.Y.end()); g1.SetMinimum(range.getLowerLimit()); g1.SetMaximum(range.getUpperLimit()); outputFile.put(g1); } } } ostringstream os; os << "set_variable NUMBER_OF_STRINGS " << setw(4) << strings.size() << ";" << endl; os << "set_variable NUMBER_OF_FLOORS " << setw(4) << floors. size() << ";" << endl; if (!strings.empty()) { os << "set_variable FIRST_STRING " << setw(4) << *strings. begin() << ";" << endl; os << "set_variable LAST_STRING " << setw(4) << *strings.rbegin() << ";" << endl; } if (!floors.empty()) { os << "set_variable FIRST_FLOOR " << setw(4) << *floors. begin() << ";" << endl; os << "set_variable LAST_FLOOR " << setw(4) << *floors. rbegin() << ";" << endl; } os << "set_array STRINGS "; copy(strings.begin(), strings.end(), ostream_iterator(os, " ")); os << endl; outputFile.put(TNamed("TUNA", os.str().c_str())); } outputFile.close(); }