Jpp  master_rocky-43-ge265d140c
the software that should make you happy
JTuna.cc
Go to the documentation of this file.
1 #include <string>
2 #include <iostream>
3 #include <iomanip>
4 #include <limits>
5 #include <vector>
6 #include <set>
7 #include <map>
8 
9 #include "TROOT.h"
10 #include "TFile.h"
11 #include "TGraph.h"
12 #include "TNamed.h"
13 #include "TString.h"
14 #include "TRegexp.h"
15 
16 #include "JROOT/JGraph.hh"
17 
18 #include "JDB/JDB.hh"
19 #include "JDB/JSelector.hh"
21 #include "JDB/JDBToolkit.hh"
23 #include "JDB/JProductRouter.hh"
24 #include "JDB/JAllParams.hh"
25 #include "JDB/JDatalogNumbers.hh"
26 #include "JDB/JDatalog.hh"
27 #include "JDB/JSupport.hh"
28 
29 #include "JTools/JRange.hh"
31 #include "JLang/JLangToolkit.hh"
32 
33 #include "Jeep/JPrint.hh"
34 #include "Jeep/JParser.hh"
35 #include "Jeep/JMessage.hh"
36 
37 namespace {
38 
39  const char FILTER = '!'; //!< negate filter option
40 }
41 
42 
43 /**
44  * \file
45  *
46  * Auxiliary program to convert slow control data from the database to ROOT TTree.
47  * \author mdejong
48  */
49 int main(int argc, char **argv)
50 {
51  using namespace std;
52  using namespace JPP;
53 
54  typedef JRange<int> JRange_t;
55 
57  JServer server;
58  string usr;
59  string pwd;
60  string cookie;
61  string detid;
62  JRange_t runs = JRange_t::DEFAULT_RANGE();
63  long long int numberOfRows;
65  set<string> locate;
66  int prescale;
67  bool graph;
68  JSelector selection;
69  int debug;
70 
71  locate.insert("pmt_id");
72  locate.insert("pmt_threshold");
73  locate.insert("pmt_threshold@");
74  locate.insert("pmt_highvolt");
75  locate.insert("pmt_highvolt@");
76 
77  try {
78 
79  JParser<> zap("Auxiliary program to convert slow control data from the database to ROOT TTree.");
80 
81  zap['s'] = make_field(server) = getServernames();
82  zap['u'] = make_field(usr) = "";
83  zap['!'] = make_field(pwd) = "";
84  zap['C'] = make_field(cookie) = "";
85  zap['o'] = make_field(outputFile);
86  zap['D'] = make_field(detid, "Detector identifier");
87  zap['R'] = make_field(runs, "Run range");
88  zap['n'] = make_field(numberOfRows, "Maximal number of rows") = numeric_limits<long long int>::max();
89  zap['F'] = make_field(filter, "Filter parameter") = JPARSER::initialised();
90  zap['L'] = make_field(locate, "Locate parameter") = JPARSER::initialised();
91  zap['P'] = make_field(prescale, "Prescale sampling of data") = 1;
92  zap['G'] = make_field(graph, "Write TGraph instead of TTree");
93  zap['@'] = make_field(selection, "Possible selections: " << JEEPZ() << getColumns<JDatalogNumbers>()) = JPARSER::initialised();
94  zap['d'] = make_field(debug) = 2;
95 
96  zap(argc, argv);
97  }
98  catch(const exception &error) {
99  FATAL(error.what() << endl);
100  }
101 
102  filter.push_back(MAKE_STRING(FILTER << "CLBControlTimeoutMS"));
103 
104  if (runs.getUpperLimit() < runs.getLowerLimit()) {
105  runs.setUpperLimit(runs.getLowerLimit());
106  }
107 
108  if (prescale < 1) {
109  WARNING("Setting prescale to one." << endl);
110  prescale = 1;
111  }
112 
113 
115 
117  map<int, // string
118  map<int, // floor
119  map<string, // parameter
120  int> > > counts;
121 
122  set<int> strings;
123  set<int> floors;
124 
125  map<string, int> warnings;
126  map<string, int> errors;
127  map<string, double> unit;
128 
129  outputFile.open();
130 
131  if (!outputFile.is_open()) {
132  FATAL("Error opening file: " << outputFile << endl);
133  }
134 
135  try {
136 
137  JDB::reset(usr, pwd, cookie);
138 
139  detid = getDetector<string>(detid);
140 
142 
143  {
144  DEBUG("Reading database table " << getTable<JDetectorIntegration_t>() << endl);
145 
146  ResultSet& rs = getResultSet(getTable<JDetectorIntegration_t>());
147 
148  if (! (rs >> detector)) {
149  THROW(JDatabaseException, "Error reading " << getTable<JDetectorIntegration_t>());
150  }
151  }
152 
153  DEBUG("Number of integrated products (before) " << right << detector.size() << endl);
154 
155  detector.configure(detid);
156 
157  DEBUG("Number of integrated products (after) " << right << detector.size() << endl);
158 
160 
161  {
162  DEBUG("Reading database table " << getTable<JAllParams>() << endl);
163 
164  ResultSet& rs = getResultSet(getTable<JAllParams>());
165 
166  for (JAllParams parameters; rs >> parameters; ) {
167 
168  // "INTEGER_UNIT_SCALE
169  // For real and string parameters, this is meaningless and must be NULL.
170  // For integer parameters, this is 0 if the value is an index to TB_PARAMETER_DISCRETE_REAL;
171  // otherwise this is the positive factor to multiply by the value to obtain the parameter value."
172 
173  if (to_upper(parameters.TYPE) == "INTEGER" && parameters.INTEGER_UNIT_SCALE > 0.0) {
174  unit[to_upper(parameters.NAME)] = parameters.INTEGER_UNIT_SCALE;
175  }
176  }
177 
178  rs.Close();
179  }
180 
181  {
182  DEBUG("Reading database table " << getTable<JDatalogNumbers>() << endl);
183 
184  selection += getSelector<JDatalogNumbers>(detid,
185  runs.getLowerLimit(),
186  runs.getUpperLimit());
187 
188  ResultSet& rs = getResultSet(getTable<JDatalogNumbers>(), selection);
189 
190  long long int counter = 0;
191 
192  for (JDatalogNumbers parameters; rs >> parameters && counter != numberOfRows; ++counter) {
193 
194  STATUS(setw(10) << counter << '\r'); DEBUG(endl);
195 
196  const JUPI_t upi = parameters.SOURCE_NAME;
197  const JLocation_t& location = router.getLocation(upi);
198 
199  if (location != JLocation_t()) {
200 
201  strings.insert(location.string);
202  floors .insert(location.floor);
203 
204  bool status = true;
205 
206  for (vector<string>::const_iterator i = filter.begin(); i != filter.end(); ++i) {
207 
208  if (*i != "") {
209 
210  bool nos = (*i)[0] == FILTER;
211  TRegexp regexp = (nos ? i->substr(1).c_str() : i->c_str());
212 
213  if (TString(parameters.PARAMETER_NAME).Contains(regexp)) {
214  status = !nos;
215  }
216  }
217  }
218 
219  if (status) {
220 
221  if (location.string >= (int) data.size()) {
222  data.resize(location.string + 1);
223  }
224 
225  if (location.floor >= (int) data[location.string].size()) {
226  data[location.string].resize(location.floor + 1);
227  }
228 
229  string buffer = parameters.PARAMETER_NAME;
230 
231  if (locate.count(buffer) != 0) {
232  buffer += MAKE_STRING("[" << FILL(2,'0') << location.position << "]");
233  }
234 
235  JGraph_t& g1 = data[location.string][location.floor][buffer];
236 
237  double factor = 1.0;
238 
239  map<string, double>::const_iterator p = unit.find(to_upper(parameters.PARAMETER_NAME));
240 
241  if (p != unit.end())
242  factor = p->second;
243  else
244  errors[parameters.PARAMETER_NAME] += 1;
245 
246 
247  if ((counts[location.string][location.floor][buffer]++)%prescale == 0) {
248 
249  if (!graph) {
250 
251  outputFile.put(JDatalog(parameters.RUN,
252  location.string,
253  location.floor,
254  location.position,
255  parameters.PARAMETER_NAME,
256  parameters.UNIXTIME,
257  parameters.DATA_VALUE * factor));
258 
259  } else {
260 
261  g1.put(parameters.getTime(), parameters.DATA_VALUE * factor);
262  }
263  }
264  }
265 
266  } else {
267 
268  warnings[parameters.PARAMETER_NAME] += 1;
269  }
270  }
271  STATUS(endl);
272  }
273  }
274  catch(const exception& error) {
275  ERROR(error.what() << endl);
276  }
277 
278  if (debug >= warning_t && !warnings.empty()) {
279 
280  cout << endl << "Parameters without location in detector." << endl;
281 
282  for (map<string, int>::const_iterator i = warnings.begin(); i != warnings.end(); ++i) {
283  cout << left << setw(48) << i->first << ' ' << setw(6) << i->second << endl;
284  }
285  }
286 
287  if (!errors.empty()) {
288 
289  cout << endl << "Parameters without scale." << endl;
290 
291  for (map<string, int>::const_iterator i = errors.begin(); i != errors.end(); ++i) {
292  cout << left << setw(48) << i->first << ' ' << setw(6) << i->second << endl;
293  }
294  }
295 
296  if (graph) {
297 
298  for (size_t string = 0; string != data.size(); ++string) {
299  for (size_t floor = 0; floor != data[string].size(); ++floor) {
300  for (map_type::iterator i = data[string][floor].begin(); i != data[string][floor].end(); ++i) {
301 
302  JGraph g1(i->second, MAKE_CSTRING(FILL(4,'0') << string << '.' << FILL(2,'0') << floor << '.' << setfill(' ') << i->first));
303 
304  const JRange<double> range(i->second.Y.begin(), i->second.Y.end());
305 
306  g1.SetMinimum(range.getLowerLimit());
307  g1.SetMaximum(range.getUpperLimit());
308 
309  outputFile.put(g1);
310  }
311  }
312  }
313 
314  ostringstream os;
315 
316  os << "set_variable NUMBER_OF_STRINGS " << setw(4) << strings.size() << ";" << endl;
317  os << "set_variable NUMBER_OF_FLOORS " << setw(4) << floors. size() << ";" << endl;
318  if (!strings.empty()) {
319  os << "set_variable FIRST_STRING " << setw(4) << *strings. begin() << ";" << endl;
320  os << "set_variable LAST_STRING " << setw(4) << *strings.rbegin() << ";" << endl;
321  }
322  if (!floors.empty()) {
323  os << "set_variable FIRST_FLOOR " << setw(4) << *floors. begin() << ";" << endl;
324  os << "set_variable LAST_FLOOR " << setw(4) << *floors. rbegin() << ";" << endl;
325  }
326  os << "set_array STRINGS ";
327  copy(strings.begin(), strings.end(), ostream_iterator<int>(os, " "));
328  os << endl;
329 
330  outputFile.put(TNamed("TUNA", os.str().c_str()));
331  }
332 
333  outputFile.close();
334 }
string outputFile
ROOT TTree parameter settings.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:712
Recording of objects on file according a format that follows from the file name extension.
General purpose messaging.
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
#define STATUS(A)
Definition: JMessage.hh:63
#define ERROR(A)
Definition: JMessage.hh:66
#define FATAL(A)
Definition: JMessage.hh:67
int debug
debug level
Definition: JSirene.cc:69
#define WARNING(A)
Definition: JMessage.hh:65
Utility class to parse command line options.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:2142
I/O formatting auxiliaries.
#define MAKE_CSTRING(A)
Make C-string.
Definition: JPrint.hh:72
#define MAKE_STRING(A)
Make string.
Definition: JPrint.hh:63
Double_t g1(const Double_t x)
Function.
Definition: JQuantiles.cc:25
Auxiliary class to define a range between two values.
int main(int argc, char **argv)
Definition: JTuna.cc:49
Auxiliary class for specifying selection of database data.
Database exception.
Definition: JException.hh:684
Utility class to parse command line options.
Definition: JParser.hh:1698
Object writing to file.
void setUpperLimit(argument_type y)
Set upper limit.
Definition: JRange.hh:235
T getLowerLimit() const
Get lower limit.
Definition: JRange.hh:202
T getUpperLimit() const
Get upper limit.
Definition: JRange.hh:213
void copy(const Head &from, JHead &to)
Copy header from from to to.
Definition: JHead.cc:162
static const JPBSSequences CLB_SEQUENCES
PBS sequences for central-logic board.
ResultSet & getResultSet(const std::string &query)
Get result set.
Definition: JDB.hh:437
std::vector< JServer > getServernames()
Get list of names of available database servers.
Definition: JDB.hh:107
@ warning_t
warning
Definition: JMessage.hh:31
std::string to_upper(const std::string &value)
Convert all character to upper case.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
bool filter(const JDAQEvent &tev, const JEvt &evt, const Evt *const pE)
Event selection.
void reset(T &value)
Reset value.
std::map< int, range_type > map_type
Definition: JSTDTypes.hh:14
Auxiliary data structure for sequence of same character.
Definition: JManip.hh:330
Type definition of range.
Definition: JHead.hh:43
Detector file.
Definition: JHead.hh:227
Auxiliary data structure for location of product in detector.
Definition: JLocation_t.hh:26
int string
position in detector
Definition: JLocation_t.hh:105
int floor
position in string
Definition: JLocation_t.hh:106
int position
position in floor
Definition: JLocation_t.hh:107
Auxiliary class to map UPI to location in detector.
JLocation_t getLocation(const JUPI_t &upi) const
Get location of product with given UPI.
Wrapper class for server name.
Definition: JDB.hh:53
Universal product identifier (UPI).
Definition: JUPI_t.hh:32
Template definition for getting table specific selector.
Auxiliary data structure for streaming of STL containers.
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:68
Data structure for graph data.
Definition: JGraph.hh:21
Auxiliary data structure to build TGraph.
Definition: JGraph.hh:44