Jpp  15.0.1-rc.1-highqe
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 #include "JDB/JGITTags.hh"
23 
24 #include "JLang/JVectorize.hh"
25 #include "JLang/JLangToolkit.hh"
26 #include "JTools/JRange.hh"
27 #include "JROOT/JRootPrinter.hh"
28 #include "JROOT/JRootToolkit.hh"
29 #include "JROOT/JMathSupportkit.hh"
30 #include "JROOT/JManager.hh"
31 #include "JGizmo/JGizmoToolkit.hh"
32 
33 #include "Jeep/JParser.hh"
34 #include "Jeep/JMessage.hh"
35 
36 
37 /**
38  * Read time stamp from input stream.
39  *
40  * Format is <tt>YYYY-MM-DD HH:MM:SS"</tt>
41  *
42  * \param in input stream
43  * \param object time stamp
44  * \return input stream
45  */
46 inline std::istream& operator>>(std::istream& in, TTimeStamp& object)
47 {
48  using namespace std;
49  using namespace JPP;
50 
51  string buffer;
52 
53  if (in >> buffer) {
54 
55  UInt_t year;
56  UInt_t month;
57  UInt_t day;
58 
59  istringstream is;
60 
61  is.str(replace(buffer, '-', ' '));
62 
63  if (is >> year >> month >> day) {
64 
65  UInt_t hour = 0;
66  UInt_t min = 0;
67  UInt_t sec = 0;
68 
69  if (in >> buffer) {
70 
71  is.clear();
72  is.str(replace(buffer, ':', ' '));
73 
74  if (is >> hour >> min >> sec) {
75 
76  object = TTimeStamp(year, month, day, hour, min, sec);
77 
78  return in;
79  }
80  }
81  }
82  }
83 
84  in.setstate(ios::failbit);
85 
86  return in;
87 }
88 
89 
90 /**
91  * Write time stamp to output stream.
92  *
93  * \param out output stream
94  * \param object time stamp
95  * \return output stream
96  */
97 inline std::ostream& operator>>(std::ostream& out, const TTimeStamp& object)
98 {
99  return out << object.AsString();
100 }
101 
102 
103 /**
104  * \file
105  *
106  * Auxiliary program to plot data from data base.
107  * \author mdejong
108  */
109 int main(int argc, char **argv)
110 {
111  using namespace std;
112  using namespace JPP;
113 
114  typedef JRange<int> JRange_t;
115 
116  JServer server;
117  string usr;
118  string pwd;
119  string cookie;
120  string outputFile;
121  string detid;
122  JRange_t runs;
123  vector<string> source;
124  JRange<TTimeStamp> UTC;
125  double Tmin_s;
126  int debug;
127 
128  try {
129 
130  JParser<> zap("Example program to plot quality data from data base.");
131 
132  zap['s'] = make_field(server) = getServernames();
133  zap['u'] = make_field(usr) = "";
134  zap['!'] = make_field(pwd) = "";
135  zap['C'] = make_field(cookie) = "";
136  zap['o'] = make_field(outputFile) = "monitor.root";
137  zap['D'] = make_field(detid, "detector identifier");
138  zap['R'] = make_field(runs, "run range") = JRange_t(1, JRange_t::getMaximum());
139  zap['S'] = make_field(source, "GIT versions") = getGITTags(TRegexp("v[0-9]*\\.[0-9]*\\.[0-9]*$"), JGITTags_t::key_type("2019-04-12"));
140  zap['U'] = make_field(UTC, "UTC time range" ) = JRange<TTimeStamp>();
141  zap['T'] = make_field(Tmin_s, "minimal run duration [s]") = 60;
142  zap['d'] = make_field(debug) = 1;
143 
144  zap(argc, argv);
145  }
146  catch(const exception &error) {
147  FATAL(error.what() << endl);
148  }
149 
150 
151  int ID = -1; // integer representation of detector identifier
152 
153  vector<JRuns> data;
155  vector<JRunQuality> buffer;
156 
157  try {
158 
159  JDB::reset(usr, pwd, cookie);
160 
161  if (is_integer(detid))
162  ID = to_value<int>(detid);
163  else
164  ID = getDetector(detid);
165 
166  // runs
167 
168  try {
169 
170  NOTICE("Extracting run information from database... " << flush);
171 
172  ResultSet& rs = getResultSet(getTable<JRuns>(), getSelector<JRuns>(ID));
173 
174  for (JRuns parameters; rs >> parameters; ) {
175 
176  parameters.DETID = ID;
177 
178  if (UTC(parameters.getRunStartTime())) {
179  data.push_back(parameters);
180  }
181  }
182 
183  rs.Close();
184 
185  NOTICE("OK" << endl);
186  }
187  catch(const exception& error) {}
188 
189  // data quality
190 
191  if (is_integer(detid)) {
192  detid = getDetector(to_value<int>(detid));
193  }
194 
195  for (vector<string>::const_iterator git = source.begin(); git != source.end(); ++git) {
196 
197  typedef map<string, string> data_type;
198  typedef map<int, data_type> map_type;
199 
200  map_type zmap;
201 
202  JSelector selector = getSelector<JRunSummaryNumbers>(detid, runs.getLowerLimit(), runs.getUpperLimit());
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  qaqc.insert(JRun_t(ID, quality.run));
235 
236  if (UTC(TTimeStamp(quality.UTCMin_s, 0)) &&
237  UTC(TTimeStamp(quality.UTCMax_s, 0))) {
238 
239  vector<JRunQuality>::iterator p = lower_bound(buffer.begin(), buffer.end(), quality);
240 
241  if (p == buffer.end() || p->run != quality.run) {
242  buffer.insert(p, quality);
243  }
244  }
245  }
246  }
247  }
248  catch(const exception& error) {
249  FATAL(error.what() << endl);
250  }
251 
252  if (buffer.empty()) {
253  FATAL("No valid QA/QC data for detector " << detid << endl);
254  }
255 
256 
257  // bins
258 
260 
261  for (vector<JRunQuality>::const_iterator quality = buffer.begin(); quality != buffer.end(); ++quality) {
262  X.push_back(quality->UTCMin_s);
263  X.push_back(quality->UTCMax_s);
264  }
265 
266  sort(X.begin(), X.end());
267 
268  struct Xmin {
269  Xmin(double xmin) :
270  xmin(xmin)
271  {}
272 
273  bool operator()(const double x1, const double x2)
274  {
275  return x2 - x1 <= xmin;
276  }
277 
278  double xmin;
279  };
280 
281  X.push_back(data.rbegin()->getRunStartTime()); // start of last run
282 
283  X.erase(unique(X.begin(), X.end(), Xmin(Tmin_s)), X.end());
284 
285  TH1D h0("livetime_s", NULL, X.size() - 1, X.data());
286  TH1D h1("QAQC", NULL, X.size() - 1, X.data());
287 
288  for (vector<JRuns>::const_iterator i = data.begin(); i != data.end(); ++i) {
289 
290  const JRun_t run(i->DETID,i->RUN);
291 
292  if (debug >= debug_t) {
293 
294  cout << "Run "
295  << setw(8) << i->RUN << ' '
296  << TTimeStamp((time_t) i->UNIXSTARTTIME/1000).AsString("c") << ' ';
297 
298  vector<JRunQuality>::const_iterator quality = lower_bound(buffer.begin(), buffer.end(), run);
299 
300  if (quality != buffer.end() && quality->run == i->RUN) {
301  cout << "[" << TTimeStamp((time_t) quality->UTCMin_s).AsString("c") << "," << TTimeStamp((time_t) quality->UTCMax_s).AsString("c") << "]";
302  } else {
303  cout << (qaqc.count(run) == 0 ? "missing" : "invalid") << " QA/QC data";
304  }
305 
306  cout << endl;
307  }
308 
309  double W = 1.0;
310 
311  if (! binary_search(buffer.begin(), buffer.end(), run)) {
312  W = (qaqc.count(run) == 0 ? -1.0 : 0.0);
313  }
314 
315  h1.Fill(i->getRunStartTime() + Tmin_s, W);
316  }
317 
318  JManager<string, TH1D> H1(new TH1D("H[%]", NULL, X.size() - 1, X.data()));
319  JManager<string, TH1D> R1(new TH1D("R[%]", NULL, X.size() - 1, X.data()));
320 
321  for (vector<JRunQuality>::const_iterator quality = buffer.begin(); quality != buffer.end(); ++quality) {
322 
323  const double x = 0.5 * (quality->UTCMin_s + quality->UTCMax_s);
324 
325  h0.Fill(x, 100.0 * quality->livetime_s / (quality->UTCMax_s - quality->UTCMin_s));
326 
327  H1["JDAQEvent"] -> Fill(x, quality->JDAQEvent);
328  H1["JTrigger3DShower"] -> Fill(x, quality->JTrigger3DShower);
329  H1["JTrigger3DMuon"] -> Fill(x, quality->JTrigger3DMuon);
330  H1["JTriggerMXShower"] -> Fill(x, quality->JTriggerMXShower);
331 
332  if (quality->livetime_s > 0.0) {
333  R1["JDAQEvent"] -> Fill(x, quality->JDAQEvent / quality->livetime_s);
334  R1["JTrigger3DShower"] -> Fill(x, quality->JTrigger3DShower / quality->livetime_s);
335  R1["JTrigger3DMuon"] -> Fill(x, quality->JTrigger3DMuon / quality->livetime_s);
336  R1["JTriggerMXShower"] -> Fill(x, quality->JTriggerMXShower / quality->livetime_s);
337  }
338  }
339 
340 
341  Double_t W[2] = { 0.0 };
342 
343  W[0] = *X.rbegin() - *X.begin();
344 
345  for (vector<JRunQuality>::const_iterator quality = buffer.begin(); quality != buffer.end(); ++quality) {
346  W[1] += quality->livetime_s;
347  }
348 
349  NOTICE("Average data taking efficiency " << FIXED(5,1) << 100.0*W[1]/W[0] << " %." << endl);
350 
351 
352  for (TH1* p : { &h0, &h1 }) {
353  p->GetXaxis()->SetTimeDisplay(1);
354  p->GetXaxis()->SetTimeFormat(TIMESTAMP);
355  p->Sumw2(false);
356  }
357 
358  for (JManager<string, TH1D>::iterator p = H1.begin(); p != H1.end(); ++p) {
359 
360  Double_t W = 0.0;
361 
362  for (Int_t i = 1; i <= p->second->GetXaxis()->GetNbins(); ++i) {
363  p->second->SetBinContent(i, (W += p->second->GetBinContent(i)));
364  }
365 
366  p->second->GetXaxis()->SetTimeDisplay(1);
367  p->second->GetXaxis()->SetTimeFormat(TIMESTAMP);
368  p->second->Sumw2(false);
369  }
370 
371  for (JManager<string, TH1D>::iterator p = R1.begin(); p != R1.end(); ++p) {
372 
373  p->second->GetXaxis()->SetTimeDisplay(1);
374  p->second->GetXaxis()->SetTimeFormat(TIMESTAMP);
375  p->second->Sumw2(false);
376  }
377 
378  TFile out(outputFile.c_str(), "recreate");
379 
380  out << h0 << h1 << H1 << R1;
381 
382  out.Write();
383  out.Close();
384 }
Utility class to parse command line options.
Definition: JParser.hh:1500
debug
Definition: JMessage.hh:29
int main(int argc, char *argv[])
Definition: Main.cc:15
TString replace(const TString &target, const TRegexp &regexp, const T &replacement)
Replace regular expression in input by given replacement.
Definition: JPrintResult.cc:63
do rm f tmp H1
Print objects in ASCII format using ROOT dictionary.
#define R1(x)
*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
then for HISTOGRAM in h0 h1
Definition: JMatrixNZ.sh:71
then let ID
Definition: JAcoustics.sh:31
Dynamic ROOT object management.
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:446
string outputFile
is
Definition: JDAQCHSM.chsm:167
std::vector< std::string > getGITTags(const TRegexp &regexp, const JGITTags_t::key_type &date)
Get selection of GIT tags.
Definition: JDB/JGITTags.hh:34
JDetectorsHelper getDetector
Function object for mapping serial number to object identifier of detector and vice versa...
Definition: JDBToolkit.cc:5
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1961
#define NOTICE(A)
Definition: JMessage.hh:64
then break fi done getCenter read X Y Z let X
bool is_integer(const std::string &buffer)
Check if string is an integer.
Definition: JLangToolkit.hh:58
int debug
debug level
Definition: JSirene.cc:63
Auxiliary methods to convert data members or return values of member methods of a set of objects to a...
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:67
void reset(T &value)
Reset value.
std::istream & operator>>(std::istream &in, JAANET::JHead &header)
Read header from input.
Definition: JHead.hh:1618
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:432
std::vector< JServer > getServernames()
Get list of names of available database servers.
Definition: JDB.hh:98
Selection of GIT tags.
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY source JAcoustics sh $DETECTOR_ID CHECK_EXIT_CODE typeset A TRIPODS get_tripods $WORKDIR tripod txt TRIPODS for EMITTER in
Definition: JCanberra.sh:41
static const char *const TIMESTAMP
Time stamp of earliest UTC time.
int qaqc
QA/QC file descriptor.