Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Functions
JTuna.cc File Reference

Auxiliary program to convert slow control data from the database to ROOT TTree. More...

#include <string>
#include <iostream>
#include <iomanip>
#include <limits>
#include <vector>
#include <set>
#include <map>
#include "TROOT.h"
#include "TFile.h"
#include "TGraph.h"
#include "TNamed.h"
#include "TString.h"
#include "TRegexp.h"
#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"

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Detailed Description

Auxiliary program to convert slow control data from the database to ROOT TTree.

Author
mdejong

Definition in file JTuna.cc.

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 47 of file JTuna.cc.

48 {
49  using namespace std;
50  using namespace JPP;
51 
52  typedef JRange<int> JRange_t;
53 
55  string usr;
56  string pwd;
57  string cookie;
58  string detid;
59  JRange_t runs = JRange_t::DEFAULT_RANGE;
60  long long int numberOfRows;
61  vector<string> filter;
62  set<string> locate;
63  int prescale;
64  bool graph;
65  JSelector selection;
66  int debug;
67 
68  locate.insert("pmt_id");
69  locate.insert("pmt_threshold");
70  locate.insert("pmt_threshold@");
71  locate.insert("pmt_highvolt");
72  locate.insert("pmt_highvolt@");
73 
74  try {
75 
76  JParser<> zap("Auxiliary program to convert slow control data from the database to ROOT TTree.");
77 
78  zap['u'] = make_field(usr) = "";
79  zap['!'] = make_field(pwd) = "";
80  zap['C'] = make_field(cookie) = "";
81  zap['o'] = make_field(outputFile);
82  zap['D'] = make_field(detid, "Detector identifier");
83  zap['R'] = make_field(runs, "Run range");
84  zap['n'] = make_field(numberOfRows, "Maximal number of rows") = numeric_limits<long long int>::max();
85  zap['F'] = make_field(filter, "Filter parameter") = JPARSER::initialised();
86  zap['L'] = make_field(locate, "Locate parameter") = JPARSER::initialised();
87  zap['P'] = make_field(prescale, "Prescale sampling of data") = 1;
88  zap['G'] = make_field(graph, "Write TGraph instead of TTree");
89  zap['@'] = make_field(selection, "Possible selections: " << JEEPZ() << getColumns<JDatalogNumbers>()) = JPARSER::initialised();
90  zap['d'] = make_field(debug) = 2;
91 
92  zap(argc, argv);
93  }
94  catch(const exception &error) {
95  FATAL(error.what() << endl);
96  }
97 
98  filter.push_back(MAKE_STRING(FILTER << "CLBControlTimeoutMS"));
99 
100  if (runs.getUpperLimit() < runs.getLowerLimit()) {
101  runs.setUpperLimit(runs.getLowerLimit());
102  }
103 
104  if (prescale < 1) {
105  WARNING("Setting prescale to one." << endl);
106  prescale = 1;
107  }
108 
109  struct JGraph_t {
110 
111  JGraph_t() :
112  counter(0)
113  {}
114 
115  void put(const Double_t x, const Double_t y)
116  {
117  X.push_back(x);
118  Y.push_back(y);
119  }
120 
121  int counter;
122 
125  };
126 
127  typedef map<string, JGraph_t> map_type;
128 
130 
131  set<int> strings;
132  set<int> floors;
133 
134  map<string, int> warnings;
135  map<string, int> errors;
136  map<string, double> unit;
137 
138  outputFile.open();
139 
140  if (!outputFile.is_open()) {
141  FATAL("Error opening file: " << outputFile << endl);
142  }
143 
144  try {
145 
146  JDB::reset(usr, pwd, cookie);
147 
148  if (is_integer(detid)) {
149  detid = getDetector(to_value<int>(detid));
150  }
151 
153 
154  {
155  DEBUG("Reading database table " << getTable<JDetectorIntegration_t>() << endl);
156 
157  ResultSet& rs = getResultSet(getTable<JDetectorIntegration_t>());
158 
159  if (! (rs >> detector)) {
160  THROW(JDatabaseException, "Error reading " << getTable<JDetectorIntegration_t>());
161  }
162  }
163 
164  DEBUG("Number of integrated products (before) " << right << detector.size() << endl);
165 
166  detector.configure(detid);
167 
168  DEBUG("Number of integrated products (after) " << right << detector.size() << endl);
169 
170  const JProductRouter router(detector);
171 
172  {
173  DEBUG("Reading database table " << getTable<JAllParams>() << endl);
174 
175  ResultSet& rs = getResultSet(getTable<JAllParams>());
176 
177  for (JAllParams parameters; rs >> parameters; ) {
178 
179  // "INTEGER_UNIT_SCALE
180  // For real and string parameters, this is meaningless and must be NULL.
181  // For integer parameters, this is 0 if the value is an index to TB_PARAMETER_DISCRETE_REAL;
182  // otherwise this is the positive factor to multiply by the value to obtain the parameter value."
183 
184  if (to_upper(parameters.TYPE) == "INTEGER" && parameters.INTEGER_UNIT_SCALE > 0.0) {
185  unit[to_upper(parameters.NAME)] = parameters.INTEGER_UNIT_SCALE;
186  }
187  }
188 
189  rs.Close();
190  }
191 
192  {
193  DEBUG("Reading database table " << getTable<JDatalogNumbers>() << endl);
194 
195  selection += getSelector<JDatalogNumbers>(detid,
196  runs.getLowerLimit(),
197  runs.getUpperLimit());
198 
199  ResultSet& rs = getResultSet(getTable<JDatalogNumbers>(), selection);
200 
201  long long int counter = 0;
202 
203  for (JDatalogNumbers parameters; rs >> parameters && counter != numberOfRows; ++counter) {
204 
205  STATUS(setw(10) << counter << '\r'); DEBUG(endl);
206 
207  const JUPI upi = parameters.SOURCE_NAME;
208  const JLocation_t& location = router.getLocation(upi);
209 
210  if (location != JLocation_t()) {
211 
212  strings.insert(location.string);
213  floors .insert(location.floor);
214 
215  bool status = true;
216 
217  for (vector<string>::const_iterator i = filter.begin(); i != filter.end(); ++i) {
218 
219  if (*i != "") {
220 
221  bool nos = (*i)[0] == FILTER;
222  TRegexp regexp = (nos ? i->substr(1).c_str() : i->c_str());
223 
224  if (TString(parameters.PARAMETER_NAME).Contains(regexp)) {
225  status = !nos;
226  }
227  }
228  }
229 
230  if (status) {
231 
232  if (location.string >= (int) data.size()) {
233  data.resize(location.string + 1);
234  }
235 
236  if (location.floor >= (int) data[location.string].size()) {
237  data[location.string].resize(location.floor + 1);
238  }
239 
240  string buffer = parameters.PARAMETER_NAME;
241 
242  if (locate.count(buffer) != 0) {
243  buffer += MAKE_STRING("[" << FILL(2,'0') << location.position << "]");
244  }
245 
246  JGraph_t& g1 = data[location.string][location.floor][buffer];
247 
248  double factor = 1.0;
249 
250  map<string, double>::const_iterator p = unit.find(to_upper(parameters.PARAMETER_NAME));
251 
252  if (p != unit.end())
253  factor = p->second;
254  else
255  errors[parameters.PARAMETER_NAME] += 1;
256 
257 
258  if ((g1.counter++)%prescale == 0) {
259 
260  if (!graph) {
261 
262  outputFile.put(JDatalog(parameters.RUN,
263  location.string,
264  location.floor,
265  location.position,
266  parameters.PARAMETER_NAME,
267  parameters.UNIXTIME,
268  parameters.DATA_VALUE * factor));
269 
270  } else {
271 
272  g1.put(parameters.getTime(), parameters.DATA_VALUE * factor);
273  }
274  }
275  }
276 
277  } else {
278 
279  warnings[parameters.PARAMETER_NAME] += 1;
280  }
281  }
282  STATUS(endl);
283  }
284  }
285  catch(const exception& error) {
286  ERROR(error.what() << endl);
287  }
288 
289  if (debug >= warning_t && !warnings.empty()) {
290 
291  cout << endl << "Parameters without location in detector." << endl;
292 
293  for (map<string, int>::const_iterator i = warnings.begin(); i != warnings.end(); ++i) {
294  cout << left << setw(48) << i->first << ' ' << setw(6) << i->second << endl;
295  }
296  }
297 
298  if (!errors.empty()) {
299 
300  cout << endl << "Parameters without scale." << endl;
301 
302  for (map<string, int>::const_iterator i = errors.begin(); i != errors.end(); ++i) {
303  cout << left << setw(48) << i->first << ' ' << setw(6) << i->second << endl;
304  }
305  }
306 
307  if (graph) {
308 
309  for (size_t string = 0; string != data.size(); ++string) {
310  for (size_t floor = 0; floor != data[string].size(); ++floor) {
311  for (map_type::iterator i = data[string][floor].begin(); i != data[string][floor].end(); ++i) {
312 
313  TGraph g1(i->second.X.size(), i->second.X.data(), i->second.Y.data());
314 
315  g1.SetName(MAKE_CSTRING(FILL(3,'0') << string << '.' << FILL(2,'0') << floor << '.' << setfill(' ') << i->first));
316 
317  const JRange<double> range(i->second.Y.begin(), i->second.Y.end());
318 
319  g1.SetMinimum(range.getLowerLimit());
320  g1.SetMaximum(range.getUpperLimit());
321 
322  outputFile.put(g1);
323  }
324  }
325  }
326 
327  ostringstream os;
328 
329  os << "set_variable NUMBER_OF_STRINGS " << setw(4) << strings.size() << ";" << endl;
330  os << "set_variable NUMBER_OF_FLOORS " << setw(4) << floors. size() << ";" << endl;
331  if (!strings.empty()) {
332  os << "set_variable FIRST_STRING " << setw(4) << *strings. begin() << ";" << endl;
333  os << "set_variable LAST_STRING " << setw(4) << *strings.rbegin() << ";" << endl;
334  }
335  if (!floors.empty()) {
336  os << "set_variable FIRST_FLOOR " << setw(4) << *floors. begin() << ";" << endl;
337  os << "set_variable LAST_FLOOR " << setw(4) << *floors. rbegin() << ";" << endl;
338  }
339  os << "set_array STRINGS ";
340  copy(strings.begin(), strings.end(), ostream_iterator<int>(os, " "));
341  os << endl;
342 
343  outputFile.put(TNamed("TUNA", os.str().c_str()));
344  }
345 
346  outputFile.close();
347 }
Object writing to file.
Utility class to parse command line options.
Definition: JParser.hh:1493
#define WARNING(A)
Definition: JMessage.hh:65
size_t size() const
size of integration data
Database exception.
Definition: JException.hh:648
#define STATUS(A)
Definition: JMessage.hh:63
#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:708
warning
Definition: JMessage.hh:31
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:63
Auxiliary class for specifying selection of database data.
string outputFile
*fatal Wrong number of arguments esac if[!-d ${OUTPUT_DIR}]
#define MAKE_STRING(A)
Make string.
Definition: JPrint.hh:699
int floor
position in string
Auxiliary class to map UPI to location in detector.
Auxiliary data structure for location of product in detector.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1954
#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.
int debug
debug level
Definition: JSirene.cc:61
Auxiliary data structure for streaming of STL containers.
Definition: JPrint.hh:558
Auxiliary data structure for sequence of same character.
Definition: JPrint.hh:361
#define FATAL(A)
Definition: JMessage.hh:67
JRange< Double_t > JRange_t
Definition: JFitToT.hh:34
static const JDetectorsHelper & getDetector
Function object for mapping serial number and object identifier of detectors.
Definition: JDBToolkit.hh:131
void reset(T &value)
Reset value.
Universal product identifier (UPI).
Definition: JUPI.hh:30
int position
position in floor
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:153
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.
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
Double_t g1(const Double_t x)
Function.
Definition: JQuantiles.cc:25