Jpp - the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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  string usr;
58  string pwd;
59  string cookie;
60  string detid;
61  JRange_t runs = JRange_t::DEFAULT_RANGE;
62  long long int numberOfRows;
63  vector<string> filter;
64  set<string> locate;
65  int prescale;
66  bool graph;
67  JSelector selection;
68  int debug;
69 
70  locate.insert("pmt_id");
71  locate.insert("pmt_threshold");
72  locate.insert("pmt_threshold@");
73  locate.insert("pmt_highvolt");
74  locate.insert("pmt_highvolt@");
75 
76  try {
77 
78  JParser<> zap("Auxiliary program to convert slow control data from the database to ROOT TTree.");
79 
80  zap['u'] = make_field(usr) = "";
81  zap['!'] = make_field(pwd) = "";
82  zap['C'] = make_field(cookie) = "";
83  zap['o'] = make_field(outputFile);
84  zap['D'] = make_field(detid, "Detector identifier");
85  zap['R'] = make_field(runs, "Run range");
86  zap['n'] = make_field(numberOfRows, "Maximal number of rows") = numeric_limits<long long int>::max();
87  zap['F'] = make_field(filter, "Filter parameter") = JPARSER::initialised();
88  zap['L'] = make_field(locate, "Locate parameter") = JPARSER::initialised();
89  zap['P'] = make_field(prescale, "Prescale sampling of data") = 1;
90  zap['G'] = make_field(graph, "Write TGraph instead of TTree");
91  zap['@'] = make_field(selection, "Possible selections: " << JEEPZ() << getColumns<JDatalogNumbers>()) = JPARSER::initialised();
92  zap['d'] = make_field(debug) = 2;
93 
94  zap(argc, argv);
95  }
96  catch(const exception &error) {
97  FATAL(error.what() << endl);
98  }
99 
100  filter.push_back(MAKE_STRING(FILTER << "CLBControlTimeoutMS"));
101 
102  if (runs.getUpperLimit() < runs.getLowerLimit()) {
103  runs.setUpperLimit(runs.getLowerLimit());
104  }
105 
106  if (prescale < 1) {
107  WARNING("Setting prescale to one." << endl);
108  prescale = 1;
109  }
110 
111 
112  typedef map<string, JGraph_t> map_type;
113 
115  map<int, // string
116  map<int, // floor
117  map<string, // parameter
118  int> > > counts;
119 
120  set<int> strings;
121  set<int> floors;
122 
123  map<string, int> warnings;
124  map<string, int> errors;
125  map<string, double> unit;
126 
127  outputFile.open();
128 
129  if (!outputFile.is_open()) {
130  FATAL("Error opening file: " << outputFile << endl);
131  }
132 
133  try {
134 
135  JDB::reset(usr, pwd, cookie);
136 
137  if (is_integer(detid)) {
138  detid = getDetector(to_value<int>(detid));
139  }
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 
159  const JProductRouter router(detector, PBS::CLB_SEQUENCES);
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 
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 }
Object writing to file.
Utility class to parse command line options.
Definition: JParser.hh:1500
#define WARNING(A)
Definition: JMessage.hh:65
int main(int argc, char *argv[])
Definition: Main.cc:15
void put(const Double_t x, const Double_t y)
Put data.
Definition: JGraph.hh:28
Data structure for graph data.
Definition: JGraph.hh:21
Database exception.
Definition: JException.hh:648
#define STATUS(A)
Definition: JMessage.hh:63
Recording of objects on file according a format that follows from the file name extension.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:670
*fatal Wrong number of arguments esac JCookie sh typeset Z DETECTOR typeset Z SOURCE_RUN typeset Z TARGET_RUN set_variable PARAMETERS_FILE $WORKDIR parameters
Definition: diff-Tuna.sh:38
#define MAKE_CSTRING(A)
Make C-string.
Definition: JPrint.hh:151
warning
Definition: JMessage.hh:31
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:66
Universal product identifier (UPI).
Definition: JUPI_t.hh:29
Auxiliary class for specifying selection of database data.
Auxiliary data structure to build TGraph.
Definition: JGraph.hh:42
string outputFile
#define MAKE_STRING(A)
Make string.
Definition: JPrint.hh:142
JDetectorsHelper getDetector
Function object for mapping serial number to object identifier of detector and vice versa...
Definition: JDBToolkit.cc:5
int floor
position in string
I/O formatting auxiliaries.
Auxiliary data structure for location of product in detector.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1961
#define ERROR(A)
Definition: JMessage.hh:66
bool is_integer(const std::string &buffer)
Check if string is an integer.
Definition: JLangToolkit.hh:58
std::string to_upper(const std::string &value)
Convert all character to upper case.
ROOT TTree parameter settings.
int debug
debug level
Definition: JSirene.cc:63
Auxiliary data structure for streaming of STL containers.
Definition: JPrint.hh:65
static const JPBSSequences CLB_SEQUENCES
PBS sequences for central-logic board.
Auxiliary class to map UPI to location in detector.
General purpose messaging.
Auxiliary data structure for sequence of same character.
Definition: JManip.hh:328
z range($ZMAX-$ZMIN)< $MINIMAL_DZ." fi fi mv $WORKDIR/fit.root $MODULE_ROOT typeset -Z 4 STRING typeset -Z 2 FLOOR JPlot1D -f $
Definition: module-Z:fit.sh:84
#define FATAL(A)
Definition: JMessage.hh:67
void reset(T &value)
Reset value.
int position
position in floor
Auxiliary class to define a range between two values.
Utility class to parse command line options.
ResultSet & getResultSet(const std::string &query)
Get result set.
Definition: JDB.hh:269
void copy(const Head &from, JHead &to)
Copy header from from to to.
Definition: JHead.cc:139
do set_variable DETECTOR_TXT $WORKDIR detector
int string
position in detector
void configure(const std::string &detid)
Configure detector integration for given detector identifier.
Template definition for getting table specific selector.
do if[[!-f $ACOUSTICS_WORKDIR/${KEY}.txt]]
Definition: JAcoustics.sh:38
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
Double_t g1(const Double_t x)
Function.
Definition: JQuantiles.cc:25