Jpp
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 "JDB/JDB.hh"
17 #include "JDB/JSelector.hh"
19 #include "JDB/JDBToolkit.hh"
21 #include "JDB/JProductRouter.hh"
22 #include "JDB/JAllParams.hh"
23 #include "JDB/JDatalogNumbers.hh"
24 #include "JDB/JDatalog.hh"
25 #include "JDB/JSupport.hh"
26 
27 #include "JTools/JRange.hh"
29 #include "JLang/JLangToolkit.hh"
30 
31 #include "Jeep/JPrint.hh"
32 #include "Jeep/JParser.hh"
33 #include "Jeep/JMessage.hh"
34 
35 namespace {
36 
37  const char FILTER = '!'; //!< negate filter option
38 }
39 
40 
41 /**
42  * \file
43  *
44  * Auxiliary program to convert slow control data from the database to ROOT TTree.
45  * \author mdejong
46  */
47 int main(int argc, char **argv)
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>() << endl);
156 
157  ResultSet& rs = getResultSet(getTable<JDetectorIntegration>());
158 
159  if (! (rs >> detector)) {
160  THROW(JDatabaseException, "Error reading " << getTable<JDetectorIntegration>());
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  try {
179  unit[to_upper(parameters.NAME)] = parameters.UNIT_EASY_SCALE;
180  }
181  catch(const exception &error) {
182  FATAL(error.what() << endl);
183  }
184  }
185 
186  rs.Close();
187  }
188 
189  {
190  selection += getSelector<JDatalogNumbers>(detid,
191  runs.getLowerLimit(),
192  runs.getUpperLimit());
193 
194  ResultSet& rs = getResultSet(getTable<JDatalogNumbers>(), selection);
195 
196  long long int counter = 0;
197 
198  for (JDatalogNumbers parameters; rs >> parameters && counter != numberOfRows; ++counter) {
199 
200  STATUS(setw(10) << counter << '\r'); DEBUG(endl);
201 
202  const JUPI upi = parameters.SOURCE_NAME;
203  const JLocation_t& location = router.getLocation(upi);
204 
205  if (location != JLocation_t()) {
206 
207  strings.insert(location.string);
208  floors .insert(location.floor);
209 
210  bool status = true;
211 
212  for (vector<string>::const_iterator i = filter.begin(); i != filter.end(); ++i) {
213 
214  if (*i != "") {
215 
216  bool nos = (*i)[0] == FILTER;
217  TRegexp regexp = (nos ? i->substr(1).c_str() : i->c_str());
218 
219  if (TString(parameters.PARAMETER_NAME).Contains(regexp)) {
220  status = !nos;
221  }
222  }
223  }
224 
225  if (status) {
226 
227  if (location.string >= (int) data.size()) {
228  data.resize(location.string + 1);
229  }
230 
231  if (location.floor >= (int) data[location.string].size()) {
232  data[location.string].resize(location.floor + 1);
233  }
234 
235  string buffer = parameters.PARAMETER_NAME;
236 
237  if (locate.count(buffer) != 0) {
238  buffer += MAKE_STRING("[" << FILL(2,'0') << location.position << "]");
239  }
240 
241  JGraph_t& g1 = data[location.string][location.floor][buffer];
242 
243  double factor = 1.0;
244 
245  map<string, double>::const_iterator p = unit.find(to_upper(parameters.PARAMETER_NAME));
246 
247  if (p != unit.end())
248  factor = p->second;
249  else
250  errors[parameters.PARAMETER_NAME] += 1;
251 
252 
253  if ((g1.counter++)%prescale == 0) {
254 
255  if (!graph) {
256 
257  outputFile.put(JDatalog(parameters.RUN,
258  location.string,
259  location.floor,
260  location.position,
261  parameters.PARAMETER_NAME,
262  parameters.UNIXTIME,
263  parameters.DATA_VALUE * factor));
264 
265  } else {
266 
267  g1.put(parameters.getTime(), parameters.DATA_VALUE * factor);
268  }
269  }
270  }
271 
272  } else {
273 
274  warnings[parameters.PARAMETER_NAME] += 1;
275  }
276  }
277  STATUS(endl);
278  }
279  }
280  catch(const exception& error) {
281  FATAL(error.what() << endl);
282  }
283 
284  if (debug >= warning_t && !warnings.empty()) {
285 
286  cout << endl << "Parameters without location in detector." << endl;
287 
288  for (map<string, int>::const_iterator i = warnings.begin(); i != warnings.end(); ++i) {
289  cout << left << setw(48) << i->first << ' ' << setw(6) << i->second << endl;
290  }
291  }
292 
293  if (!errors.empty()) {
294 
295  cout << endl << "Parameters without scale." << endl;
296 
297  for (map<string, int>::const_iterator i = errors.begin(); i != errors.end(); ++i) {
298  cout << left << setw(48) << i->first << ' ' << setw(6) << i->second << endl;
299  }
300  }
301 
302  if (graph) {
303 
304  for (size_t string = 0; string != data.size(); ++string) {
305  for (size_t floor = 0; floor != data[string].size(); ++floor) {
306  for (map_type::iterator i = data[string][floor].begin(); i != data[string][floor].end(); ++i) {
307 
308  TGraph g1(i->second.X.size(), i->second.X.data(), i->second.Y.data());
309 
310  g1.SetName(MAKE_CSTRING(FILL(3,'0') << string << '.' << FILL(2,'0') << floor << '.' << setfill(' ') << i->first));
311 
312  const JRange<double> range(i->second.Y.begin(), i->second.Y.end());
313 
314  g1.SetMinimum(range.getLowerLimit());
315  g1.SetMaximum(range.getUpperLimit());
316 
317  outputFile.put(g1);
318  }
319  }
320  }
321 
322  ostringstream os;
323 
324  os << "set_variable NUMBER_OF_STRINGS " << setw(4) << strings.size() << ";" << endl;
325  os << "set_variable NUMBER_OF_FLOORS " << setw(4) << floors. size() << ";" << endl;
326  if (!strings.empty()) {
327  os << "set_variable FIRST_STRING " << setw(4) << *strings. begin() << ";" << endl;
328  os << "set_variable LAST_STRING " << setw(4) << *strings.rbegin() << ";" << endl;
329  }
330  if (!floors.empty()) {
331  os << "set_variable FIRST_FLOOR " << setw(4) << *floors. begin() << ";" << endl;
332  os << "set_variable LAST_FLOOR " << setw(4) << *floors. rbegin() << ";" << endl;
333  }
334  os << "set_array STRINGS ";
335  copy(strings.begin(), strings.end(), ostream_iterator<int>(os, " "));
336  os << endl;
337 
338  outputFile.put(TNamed("TUNA", os.str().c_str()));
339  }
340 
341  outputFile.close();
342 }
JProductRouter.hh
JLANG::JDatabaseException
Database exception.
Definition: JException.hh:648
JDATABASE::JDatalogNumbers
Definition: JDatalogNumbers.hh:17
JFileRecorder.hh
JMessage.hh
JPrint.hh
JLANG::is_integer
bool is_integer(const std::string &buffer)
Check if string is an integer.
Definition: JLangToolkit.hh:58
JSelector.hh
g1
Double_t g1(const Double_t x)
Function.
Definition: JQuantiles.cc:25
JLANG::to_upper
std::string to_upper(const std::string &value)
Convert all character to upper case.
Definition: JLangToolkit.hh:229
JPARSER::initialised
Empty structure for specification of parser element that is initialised (i.e.
Definition: JParser.hh:63
std::vector
Definition: JSTDTypes.hh:12
JTOOLS::JRange< int >
JDatalog.hh
JDBToolkit.hh
JAllParams.hh
JEEPZ
Auxiliary data structure for streaming of STL containers.
Definition: JPrint.hh:558
JDATABASE::JLocation_t::floor
int floor
position in string
Definition: JProductRouter.hh:94
JPARSER::JParser
Utility class to parse command line options.
Definition: JParser.hh:1493
MAKE_STRING
#define MAKE_STRING(A)
Make string.
Definition: JPrint.hh:699
JAANET::copy
void copy(const Head &from, JHead &to)
Copy header from from to to.
Definition: JHead.cc:152
std::set
Definition: JSTDTypes.hh:13
JDATABASE::getDetector
static const JDetectorsHelper & getDetector
Function object for mapping serial number and object identifier of detectors.
Definition: JDBToolkit.hh:131
JDATABASE::getResultSet
ResultSet & getResultSet(const std::string &query)
Get result set.
Definition: JDB.hh:403
JEEP::warning_t
warning
Definition: JMessage.hh:31
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
JDATABASE::JProductRouter
Auxiliary class to map UPI to location in detector.
Definition: JProductRouter.hh:102
MAKE_CSTRING
#define MAKE_CSTRING(A)
Make C-string.
Definition: JPrint.hh:708
JDB.hh
JRange.hh
JDATABASE::JLocation_t::position
int position
position in floor
Definition: JProductRouter.hh:95
WARNING
#define WARNING(A)
Definition: JMessage.hh:65
debug
int debug
debug level
Definition: JSirene.cc:59
FILL
Auxiliary data structure for sequence of same character.
Definition: JPrint.hh:361
JSupport.hh
THROW
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:670
JDATABASE::getSelector
Template definition for getting table specific selector.
Definition: JSelectorSupportkit.hh:31
JDATABASE::JLocation_t
Auxiliary data structure for location of product in detector.
Definition: JProductRouter.hh:26
std::map
Definition: JSTDTypes.hh:16
STATUS
#define STATUS(A)
Definition: JMessage.hh:63
JDetectorIntegration.hh
JDETECTOR::reset
void reset(JCLBInput &data, size_t size)
Reset CLB buffers.
Definition: JCLBSimulator.hh:41
JParser.hh
JDATABASE::JAllParams
Definition: JAllParams.hh:18
JDATABASE::JDatalog
Definition: JDatalog.hh:17
make_field
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1954
JDATABASE::JDetectorIntegration
Detector integration.
Definition: JDetectorIntegration.hh:294
JAANET::detector
Detector file.
Definition: JHead.hh:130
DEBUG
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
std
Definition: jaanetDictionary.h:36
JTOOLS::if
if((p==this->begin() &&this->getDistance(x,(p++) ->getX()) > distance_type::precision)||(p==this->end() &&this->getDistance((--p) ->getX(), x) > distance_type::precision))
Template base class for polynomial interpolations with polynomial result.
Definition: JPolint.hh:619
JDATABASE::JSelector
Auxiliary class for specifying selection of database data.
Definition: JDB/JSelector.hh:67
main
int main(int argc, char **argv)
Definition: JTuna.cc:47
FATAL
#define FATAL(A)
Definition: JMessage.hh:67
outputFile
string outputFile
Definition: JDAQTimesliceSelector.cc:37
JSUPPORT::JFileRecorder
Object writing to file.
Definition: JFileRecorder.hh:41
JDATABASE::JUPI
Universal product identifier (UPI).
Definition: JUPI.hh:29
JDatalogNumbers.hh
JDATABASE::JLocation_t::string
int string
position in detector
Definition: JProductRouter.hh:93
JSelectorSupportkit.hh
JLangToolkit.hh