Jpp  15.0.5
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  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;
64  vector<string> filter;
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 
114  typedef map<string, JGraph_t> map_type;
115 
117  map<int, // string
118  map<int, // floor
119  map<string, // parameter
120  int> > > counts;
121 
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  if (is_integer(detid)) {
140  detid = getDetector(to_value<int>(detid));
141  }
142 
144 
145  {
146  DEBUG("Reading database table " << getTable<JDetectorIntegration_t>() << endl);
147 
148  ResultSet& rs = getResultSet(getTable<JDetectorIntegration_t>());
149 
150  if (! (rs >> detector)) {
151  THROW(JDatabaseException, "Error reading " << getTable<JDetectorIntegration_t>());
152  }
153  }
154 
155  DEBUG("Number of integrated products (before) " << right << detector.size() << endl);
156 
157  detector.configure(detid);
158 
159  DEBUG("Number of integrated products (after) " << right << detector.size() << endl);
160 
161  const JProductRouter router(detector, PBS::CLB_SEQUENCES);
162 
163  {
164  DEBUG("Reading database table " << getTable<JAllParams>() << endl);
165 
166  ResultSet& rs = getResultSet(getTable<JAllParams>());
167 
168  for (JAllParams parameters; rs >> parameters; ) {
169 
170  // "INTEGER_UNIT_SCALE
171  // For real and string parameters, this is meaningless and must be NULL.
172  // For integer parameters, this is 0 if the value is an index to TB_PARAMETER_DISCRETE_REAL;
173  // otherwise this is the positive factor to multiply by the value to obtain the parameter value."
174 
175  if (to_upper(parameters.TYPE) == "INTEGER" && parameters.INTEGER_UNIT_SCALE > 0.0) {
176  unit[to_upper(parameters.NAME)] = parameters.INTEGER_UNIT_SCALE;
177  }
178  }
179 
180  rs.Close();
181  }
182 
183  {
184  DEBUG("Reading database table " << getTable<JDatalogNumbers>() << endl);
185 
186  selection += getSelector<JDatalogNumbers>(detid,
187  runs.getLowerLimit(),
188  runs.getUpperLimit());
189 
190  ResultSet& rs = getResultSet(getTable<JDatalogNumbers>(), selection);
191 
192  long long int counter = 0;
193 
194  for (JDatalogNumbers parameters; rs >> parameters && counter != numberOfRows; ++counter) {
195 
196  STATUS(setw(10) << counter << '\r'); DEBUG(endl);
197 
198  const JUPI_t upi = parameters.SOURCE_NAME;
199  const JLocation_t& location = router.getLocation(upi);
200 
201  if (location != JLocation_t()) {
202 
203  strings.insert(location.string);
204  floors .insert(location.floor);
205 
206  bool status = true;
207 
208  for (vector<string>::const_iterator i = filter.begin(); i != filter.end(); ++i) {
209 
210  if (*i != "") {
211 
212  bool nos = (*i)[0] == FILTER;
213  TRegexp regexp = (nos ? i->substr(1).c_str() : i->c_str());
214 
215  if (TString(parameters.PARAMETER_NAME).Contains(regexp)) {
216  status = !nos;
217  }
218  }
219  }
220 
221  if (status) {
222 
223  if (location.string >= (int) data.size()) {
224  data.resize(location.string + 1);
225  }
226 
227  if (location.floor >= (int) data[location.string].size()) {
228  data[location.string].resize(location.floor + 1);
229  }
230 
231  string buffer = parameters.PARAMETER_NAME;
232 
233  if (locate.count(buffer) != 0) {
234  buffer += MAKE_STRING("[" << FILL(2,'0') << location.position << "]");
235  }
236 
237  JGraph_t& g1 = data[location.string][location.floor][buffer];
238 
239  double factor = 1.0;
240 
241  map<string, double>::const_iterator p = unit.find(to_upper(parameters.PARAMETER_NAME));
242 
243  if (p != unit.end())
244  factor = p->second;
245  else
246  errors[parameters.PARAMETER_NAME] += 1;
247 
248 
249  if ((counts[location.string][location.floor][buffer]++)%prescale == 0) {
250 
251  if (!graph) {
252 
254  location.string,
255  location.floor,
256  location.position,
257  parameters.PARAMETER_NAME,
258  parameters.UNIXTIME,
259  parameters.DATA_VALUE * factor));
260 
261  } else {
262 
263  g1.put(parameters.getTime(), parameters.DATA_VALUE * factor);
264  }
265  }
266  }
267 
268  } else {
269 
270  warnings[parameters.PARAMETER_NAME] += 1;
271  }
272  }
273  STATUS(endl);
274  }
275  }
276  catch(const exception& error) {
277  ERROR(error.what() << endl);
278  }
279 
280  if (debug >= warning_t && !warnings.empty()) {
281 
282  cout << endl << "Parameters without location in detector." << endl;
283 
284  for (map<string, int>::const_iterator i = warnings.begin(); i != warnings.end(); ++i) {
285  cout << left << setw(48) << i->first << ' ' << setw(6) << i->second << endl;
286  }
287  }
288 
289  if (!errors.empty()) {
290 
291  cout << endl << "Parameters without scale." << endl;
292 
293  for (map<string, int>::const_iterator i = errors.begin(); i != errors.end(); ++i) {
294  cout << left << setw(48) << i->first << ' ' << setw(6) << i->second << endl;
295  }
296  }
297 
298  if (graph) {
299 
300  for (size_t string = 0; string != data.size(); ++string) {
301  for (size_t floor = 0; floor != data[string].size(); ++floor) {
302  for (map_type::iterator i = data[string][floor].begin(); i != data[string][floor].end(); ++i) {
303 
304  JGraph g1(i->second, MAKE_CSTRING(FILL(4,'0') << string << '.' << FILL(2,'0') << floor << '.' << setfill(' ') << i->first));
305 
306  const JRange<double> range(i->second.Y.begin(), i->second.Y.end());
307 
308  g1.SetMinimum(range.getLowerLimit());
309  g1.SetMaximum(range.getUpperLimit());
310 
311  outputFile.put(g1);
312  }
313  }
314  }
315 
316  ostringstream os;
317 
318  os << "set_variable NUMBER_OF_STRINGS " << setw(4) << strings.size() << ";" << endl;
319  os << "set_variable NUMBER_OF_FLOORS " << setw(4) << floors. size() << ";" << endl;
320  if (!strings.empty()) {
321  os << "set_variable FIRST_STRING " << setw(4) << *strings. begin() << ";" << endl;
322  os << "set_variable LAST_STRING " << setw(4) << *strings.rbegin() << ";" << endl;
323  }
324  if (!floors.empty()) {
325  os << "set_variable FIRST_FLOOR " << setw(4) << *floors. begin() << ";" << endl;
326  os << "set_variable LAST_FLOOR " << setw(4) << *floors. rbegin() << ";" << endl;
327  }
328  os << "set_array STRINGS ";
329  copy(strings.begin(), strings.end(), ostream_iterator<int>(os, " "));
330  os << endl;
331 
332  outputFile.put(TNamed("TUNA", os.str().c_str()));
333  }
334 
335  outputFile.close();
336 }
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
Definition: JLocation_t.hh:106
I/O formatting auxiliaries.
Auxiliary data structure for location of product in detector.
Definition: JLocation_t.hh:24
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1961
then usage $script< detector specific acoustics-fit script >< option > nAuxiliary script to make scan of pre stretching of detector strings(see JEditDetector)." "\nPossible options
#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
#define FATAL(A)
Definition: JMessage.hh:67
z range($ZMAX-$ZMIN)< $MINIMAL_DZ." fi fi typeset -Z 4 STRING typeset -Z 2 FLOOR JPlot1D -f $
void reset(T &value)
Reset value.
int position
position in floor
Definition: JLocation_t.hh:107
Auxiliary class to define a range between two values.
Utility class to parse command line options.
then usage $script< input_file >< detector_file > fi set_variable OUTPUT_DIR set_variable SELECTOR JDAQTimesliceL1 set_variable DEBUG case set_variable DEBUG
ResultSet & getResultSet(const std::string &query)
Get result set.
Definition: JDB.hh:432
std::vector< JServer > getServernames()
Get list of names of available database servers.
Definition: JDB.hh:98
Wrapper class for server name.
Definition: JDB.hh:42
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
Definition: JLocation_t.hh:105
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:39
Double_t g1(const Double_t x)
Function.
Definition: JQuantiles.cc:25