Jpp  debug
the software that should make you happy
JDataMonitor.cc
Go to the documentation of this file.
1 #include <string>
2 #include <iostream>
3 #include <sstream>
4 #include <iomanip>
5 #include <vector>
6 #include <set>
7 #include <map>
8 #include <algorithm>
9 
10 #include "TROOT.h"
11 #include "TFile.h"
12 #include "TH1D.h"
13 #include "TTimeStamp.h"
14 
15 #include "JDB/JDB.hh"
16 #include "JDB/JSelector.hh"
18 #include "JDB/JRunQuality.hh"
19 #include "JDB/JRuns.hh"
20 #include "JDB/JDBToolkit.hh"
21 #include "JDB/JDBSupportkit.hh"
22 
23 #include "JLang/JLangToolkit.hh"
24 #include "JTools/JRange.hh"
25 #include "JROOT/JRootPrinter.hh"
26 #include "JROOT/JRootToolkit.hh"
27 #include "JROOT/JMathSupportkit.hh"
29 #include "JROOT/JManager.hh"
30 #include "JGizmo/JGizmoToolkit.hh"
31 #include "JSupport/JMeta.hh"
33 
34 #include "JDataQuality/JGITTags.hh"
35 
36 #include "Jeep/JeepToolkit.hh"
37 #include "Jeep/JComment.hh"
38 #include "Jeep/JParser.hh"
39 #include "Jeep/JMessage.hh"
40 
41 
42 /**
43  * Read time stamp from input stream.
44  *
45  * Format is <tt>YYYY-MM-DD HH:MM:SS"</tt>
46  *
47  * \param in input stream
48  * \param object time stamp
49  * \return input stream
50  */
51 inline std::istream& operator>>(std::istream& in, TTimeStamp& object)
52 {
53  using namespace std;
54  using namespace JPP;
55 
56  string buffer;
57 
58  if (in >> buffer) {
59 
60  UInt_t year;
61  UInt_t month;
62  UInt_t day;
63 
64  istringstream is;
65 
66  is.str(replace(buffer, '-', ' '));
67 
68  if (is >> year >> month >> day) {
69 
70  UInt_t hour = 0;
71  UInt_t min = 0;
72  UInt_t sec = 0;
73 
74  if (in >> buffer) {
75 
76  is.clear();
77  is.str(replace(buffer, ':', ' '));
78 
79  if (is >> hour >> min >> sec) {
80 
81  object = TTimeStamp(year, month, day, hour, min, sec);
82 
83  return in;
84  }
85  }
86  }
87  }
88 
89  in.setstate(ios::failbit);
90 
91  return in;
92 }
93 
94 
95 /**
96  * Write time stamp to output stream.
97  *
98  * \param out output stream
99  * \param object time stamp
100  * \return output stream
101  */
102 inline std::ostream& operator>>(std::ostream& out, const TTimeStamp& object)
103 {
104  return out << object.AsString();
105 }
106 
107 
108 /**
109  * \file
110  *
111  * Auxiliary program to plot data from data base or input file.
112  * \author mdejong
113  */
114 int main(int argc, char **argv)
115 {
116  using namespace std;
117  using namespace JPP;
118 
120 
121  JServer server;
122  string usr;
123  string pwd;
124  string cookie;
125  string inputFile;
126  string outputFile;
127  string detid;
128  JRange_t UTC;
129  vector<string> source;
130  double Tmin_s;
131  int debug;
132 
133  try {
134 
135  JParser<> zap("Example program to plot quality data from data base or input file.");
136 
137  zap['s'] = make_field(server) = getServernames();
138  zap['u'] = make_field(usr) = "";
139  zap['!'] = make_field(pwd) = "";
140  zap['C'] = make_field(cookie) = "";
141  zap['f'] = make_field(inputFile, "Optional input file instead of database.") = "";
142  zap['o'] = make_field(outputFile, "ROOT file with histograms and n-tuple or ASCII file with QA/QC data.") = "monitor.root";
143  zap['D'] = make_field(detid, "detector identifier");
144  zap['S'] = make_field(source, "GIT versions") = getGITTags(TRegexp("v[0-9]*\\.[0-9]*\\.[0-9]*$"), JGITTags_t::key_type("2019-04-12"));
145  zap['U'] = make_field(UTC, "UTC time range" ) = JRange<TTimeStamp>();
146  zap['T'] = make_field(Tmin_s, "minimal run duration [s]") = 60;
147  zap['d'] = make_field(debug) = 1;
148 
149  zap(argc, argv);
150  }
151  catch(const exception &error) {
152  FATAL(error.what() << endl);
153  }
154 
155 
156  set<JRuns> runs;
157  set<JRunQuality> buffer;
158 
159  try {
160 
161  JDB::reset(usr, pwd, cookie);
162 
163  const int ID = getDetector<int >(detid);
164  detid = getDetector<string>(detid);
165 
166  // runs
167 
168  NOTICE("Extracting run information from database... " << flush);
169 
170  ResultSet& rs = getResultSet(getTable<JRuns>(), getSelector<JRuns>(ID));
171 
172  for (JRuns parameters; rs >> parameters; ) {
173 
174  parameters.DETID = ID;
175 
176  if (UTC(parameters.getRunStartTime())) {
177  runs.insert(parameters);
178  }
179  }
180 
181  rs.Close();
182 
183  NOTICE("OK" << endl);
184 
185  if (runs.empty()) {
186  FATAL("No runs for detector " << detid << endl);
187  }
188 
189  NOTICE("Run range " << runs.begin()->RUN << ' ' << runs.rbegin()->RUN << endl);
190 
191  if (inputFile == "") { // read data from database
192 
193  // run summary data
194 
195  for (vector<string>::const_iterator git = source.begin(); git != source.end(); ++git) {
196 
199 
200  map_type zmap;
201 
202  JSelector selector = getSelector<JRunSummaryNumbers>(detid, runs.begin()->RUN, runs.rbegin()->RUN);
203 
204  selector.add(&JRunSummaryNumbers::SOURCE_NAME, *git);
205 
206  try {
207 
208  NOTICE("Extracting run summmary information with source " << *git << " from database... " << flush);
209 
210  ResultSet& rs = getResultSet(getTable<JRunSummaryNumbers>(), selector);
211 
212  for (JRunSummaryNumbers parameters; rs >> parameters; ) {
213  zmap[parameters.RUN].insert(make_pair(parameters.PARAMETER_NAME, parameters.DATA_VALUE));
214  }
215 
216  rs.Close();
217 
218  NOTICE("OK" << endl);
219  }
220  catch(const exception& error) { NOTICE(endl); }
221 
222  for (map_type::const_iterator run = zmap.begin(); run != zmap.end(); ++run) {
223 
224  JRunQuality quality;
225 
226  quality.GIT = *git;
227  quality.detector = ID;
228  quality.run = run->first;
229 
230  for (data_type::const_iterator p = run->second.begin(); p != run->second.end(); ++p) {
231  quality.put(p->first, p->second);
232  }
233 
234  if (UTC(TTimeStamp(quality.UTCMin_s, 0)) &&
235  UTC(TTimeStamp(quality.UTCMax_s, 0))) {
236  buffer.insert(quality);
237  }
238  }
239  }
240 
241  } else { // read data from file
242 
244 
245  JComment comment;
246 
247  in >> comment;
248 
249  for (JRunQuality quality; in >> quality; ) {
250  if (UTC(TTimeStamp(quality.UTCMin_s, 0)) &&
251  UTC(TTimeStamp(quality.UTCMax_s, 0))) {
252  buffer.insert(quality);
253  }
254  }
255 
256  in.close();
257  }
258  }
259  catch(const exception& error) {
260  FATAL(error.what() << endl);
261  }
262 
263 
265 
266  // bins
267 
268  vector<double> X;
269 
270  X.push_back(runs. begin()->getRunStartTime()); // start of first run
271 
272  for (set<JRunQuality>::const_iterator quality = buffer.begin(); quality != buffer.end(); ++quality) {
273  X.push_back(quality->UTCMin_s);
274  X.push_back(quality->UTCMax_s);
275  }
276 
277  X.push_back(runs.rbegin()->getRunStartTime()); // start of last run
278 
279  sort(X.begin(), X.end());
280 
281  struct Xmin {
282  Xmin(double xmin) :
283  xmin(xmin)
284  {}
285 
286  bool operator()(const double x1, const double x2)
287  {
288  return x2 - x1 <= xmin;
289  }
290 
291  double xmin;
292  };
293 
294  X.erase(unique(X.begin(), X.end(), Xmin(Tmin_s)), X.end());
295 
296  TH1D h0("livetime_s", NULL, X.size() - 1, X.data());
297  TH1D h1("QAQC", NULL, X.size() - 1, X.data());
298 
299  for (set<JRuns>::const_iterator i = runs.begin(); i != runs.end(); ++i) {
300 
301  set<JRunQuality>::const_iterator quality = buffer.find(JRunQuality(i->DETID, i->RUN));
302 
303  if (debug >= debug_t) {
304 
305  cout << "Run "
306  << setw(8) << i->RUN << ' '
307  << TTimeStamp((time_t) i->UNIXSTARTTIME/1000).AsString("c") << ' ';
308 
309  if (quality != buffer.end())
310  cout << "[" << TTimeStamp((time_t) quality->UTCMin_s).AsString("c") << "," << TTimeStamp((time_t) quality->UTCMax_s).AsString("c") << "]";
311  else
312  cout << "missing QA/QC data";
313 
314  cout << endl;
315  }
316 
317  h1.Fill(i->getRunStartTime() + Tmin_s, (quality != buffer.end() ? 1.0 : -1.0));
318  }
319 
320  JManager<string, TH1D> H1(new TH1D("H[%]", NULL, X.size() - 1, X.data()));
321  JManager<string, TH1D> R1(new TH1D("R[%]", NULL, X.size() - 1, X.data()));
322 
323  for (set<JRunQuality>::const_iterator quality = buffer.begin(); quality != buffer.end(); ++quality) {
324 
325  const double x = 0.5 * (quality->UTCMin_s + quality->UTCMax_s);
326 
327  h0.Fill(x, 100.0 * quality->livetime_s / (quality->UTCMax_s - quality->UTCMin_s));
328 
329  H1["JDAQEvent"] -> Fill(x, quality->JDAQEvent);
330  H1["JTrigger3DShower"] -> Fill(x, quality->JTrigger3DShower);
331  H1["JTrigger3DMuon"] -> Fill(x, quality->JTrigger3DMuon);
332  H1["JTriggerMXShower"] -> Fill(x, quality->JTriggerMXShower);
333 
334  if (quality->livetime_s > 0.0) {
335  R1["JDAQEvent"] -> Fill(x, quality->JDAQEvent / quality->livetime_s);
336  R1["JTrigger3DShower"] -> Fill(x, quality->JTrigger3DShower / quality->livetime_s);
337  R1["JTrigger3DMuon"] -> Fill(x, quality->JTrigger3DMuon / quality->livetime_s);
338  R1["JTriggerMXShower"] -> Fill(x, quality->JTriggerMXShower / quality->livetime_s);
339  }
340  }
341 
342 
343  Double_t W[2] = { 0.0 };
344 
345  W[0] = *X.rbegin() - *X.begin();
346 
347  for (set<JRunQuality>::const_iterator quality = buffer.begin(); quality != buffer.end(); ++quality) {
348  W[1] += quality->livetime_s;
349  }
350 
351  NOTICE("Average data taking efficiency " << FIXED(5,1) << 100.0*W[1]/W[0] << " %." << endl);
352 
353 
354  for (TH1* p : { &h0, &h1 }) {
355  p->GetXaxis()->SetTimeDisplay(1);
356  p->GetXaxis()->SetTimeFormat(TIMESTAMP);
357  p->Sumw2(false);
358  }
359 
360  for (JManager<string, TH1D>::iterator p = H1.begin(); p != H1.end(); ++p) {
361 
362  Double_t W = 0.0;
363 
364  for (Int_t i = 1; i <= p->second->GetXaxis()->GetNbins(); ++i) {
365  p->second->SetBinContent(i, (W += p->second->GetBinContent(i)));
366  }
367 
368  p->second->GetXaxis()->SetTimeDisplay(1);
369  p->second->GetXaxis()->SetTimeFormat(TIMESTAMP);
370  p->second->Sumw2(false);
371  }
372 
373  for (JManager<string, TH1D>::iterator p = R1.begin(); p != R1.end(); ++p) {
374 
375  p->second->GetXaxis()->SetTimeDisplay(1);
376  p->second->GetXaxis()->SetTimeFormat(TIMESTAMP);
377  p->second->Sumw2(false);
378  }
379 
380  TFile out(outputFile.c_str(), "recreate");
381 
382  out << h0 << h1 << H1 << R1;
383 
384  out.Write();
385  out.Close();
386  }
387 
388 
390 
391  // store data
392 
394 
395  out.setf(ios::fixed);
396 
397  JComment comment;
398 
399  comment.add(JMeta(argc, argv));
400 
401  out << comment;
402 
403  for (set<JRunQuality>::const_iterator i = buffer.begin(); i != buffer.end(); ++i) {
404  out << *i << endl;
405  }
406 
407  out.close();
408  }
409 }
string outputFile
std::istream & operator>>(std::istream &in, TTimeStamp &object)
Read time stamp from input stream.
Definition: JDataMonitor.cc:51
int main(int argc, char **argv)
Selection of GIT tags.
Specifications of file name extensions.
Dynamic ROOT object management.
General purpose messaging.
#define NOTICE(A)
Definition: JMessage.hh:64
#define FATAL(A)
Definition: JMessage.hh:67
int debug
debug level
Definition: JSirene.cc:69
ROOT I/O of application specific meta data.
Utility class to parse command line options.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:2158
TString replace(const TString &target, const TRegexp &regexp, const T &replacement)
Replace regular expression in input by given replacement.
Definition: JPrintResult.cc:63
Auxiliary class to define a range between two values.
Print objects in ASCII format using ROOT dictionary.
Auxiliary methods for handling file names, type names and environment.
Auxiliary class for specifying selection of database data.
JSelector & add(const JSelector &selection)
Add selection.
Object reading from ASCII file.
Object(s) writing to ASCII file.
virtual void close()
Close file.
virtual void close()
Close file.
Utility class to parse command line options.
Definition: JParser.hh:1714
Range of values.
Definition: JRange.hh:42
#define R1(x)
const double xmin
Definition: JQuadrature.cc:23
ResultSet & getResultSet(const std::string &query)
Get result set.
Definition: JDB.hh:436
std::vector< JServer > getServernames()
Get list of names of available database servers.
Definition: JDB.hh:106
std::vector< std::string > getGITTags(const TRegexp &regexp, const JGITTags_t::key_type &date)
Get selection of GIT tags.
std::string getFilenameExtension(const std::string &file_name)
Get file name extension, i.e. part after last JEEP::FILENAME_SEPARATOR if any.
Definition: JeepToolkit.hh:109
@ debug_t
debug
Definition: JMessage.hh:29
static const char *const TIMESTAMP
Time stamp of earliest UTC time.
T & getInstance(const T &object)
Get static instance from temporary object.
Definition: JObject.hh:75
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
std::vector< event_type > data_type
Definition: JPerth.cc:81
static const char *const ASCII_FILE_FORMAT
file name extension ASCII format
static const char *const ROOT_FILE_FORMAT
file name extension ROOT format
void reset(T &value)
Reset value.
std::map< int, range_type > map_type
Definition: JSTDTypes.hh:14
std::istream & operator>>(std::istream &in, std::vector< int > &object)
Read std::vector<int> from input.
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:448
Type definition of range.
Definition: JHead.hh:43
Auxiliary data structure for data quality.
Definition: JRunQuality.hh:36
double UTCMin_s
minimal UTC time (from "runs" table)
Definition: JRunQuality.hh:145
void put(const std::string &key, const std::string &value)
Put value at given key.
Definition: JRunQuality.cc:25
double UTCMax_s
maximal UTC time (from "runs" table)
Definition: JRunQuality.hh:146
std::string GIT
GIT version used to write QA/QC data.
Definition: JRunQuality.hh:138
int detector
detector identifier
Definition: JRunQuality.hh:142
Wrapper class for server name.
Definition: JDB.hh:52
Template definition for getting table specific selector.
Auxiliary class for comment.
Definition: JComment.hh:43
JComment & add(const std::string &comment)
Add comment.
Definition: JComment.hh:100
Auxiliary class for ROOT I/O of application specific meta data.
Definition: JMeta.hh:72