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