Jpp - the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Functions
JDataMonitor.cc File Reference

Auxiliary program to plot data from data base. More...

#include <string>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include "TROOT.h"
#include "TFile.h"
#include "TH1D.h"
#include "TTimeStamp.h"
#include "JDB/JDB.hh"
#include "JDB/JSelector.hh"
#include "JDB/JSelectorSupportkit.hh"
#include "JDB/JRunQuality.hh"
#include "JDB/JRuns.hh"
#include "JDB/JDBToolkit.hh"
#include "JDB/JDBSupportkit.hh"
#include "JDB/JGITTags.hh"
#include "JLang/JVectorize.hh"
#include "JLang/JLangToolkit.hh"
#include "JTools/JRange.hh"
#include "JROOT/JRootPrinter.hh"
#include "JROOT/JRootToolkit.hh"
#include "JROOT/JMathSupportkit.hh"
#include "JROOT/JManager.hh"
#include "JGizmo/JGizmoToolkit.hh"
#include "Jeep/JParser.hh"
#include "Jeep/JMessage.hh"

Go to the source code of this file.

Functions

std::istream & operator>> (std::istream &in, TTimeStamp &object)
 Read time stamp from input stream. More...
 
std::ostream & operator>> (std::ostream &out, const TTimeStamp &object)
 Write time stamp to output stream. More...
 
int main (int argc, char **argv)
 

Detailed Description

Auxiliary program to plot data from data base.

Author
mdejong

Definition in file JDataMonitor.cc.

Function Documentation

std::istream& operator>> ( std::istream &  in,
TTimeStamp &  object 
)
inline

Read time stamp from input stream.

Format is YYYY-MM-DD HH:MM:SS"

Parameters
ininput stream
objecttime stamp
Returns
input stream

Definition at line 46 of file JDataMonitor.cc.

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 }
TString replace(const TString &target, const TRegexp &regexp, const T &replacement)
Replace regular expression in input by given replacement.
Definition: JPrintResult.cc:63
is
Definition: JDAQCHSM.chsm:167
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:38
std::ostream& operator>> ( std::ostream &  out,
const TTimeStamp &  object 
)
inline

Write time stamp to output stream.

Parameters
outoutput stream
objecttime stamp
Returns
output stream

Definition at line 97 of file JDataMonitor.cc.

98 {
99  return out << object.AsString();
100 }
int main ( int  argc,
char **  argv 
)

Definition at line 109 of file JDataMonitor.cc.

110 {
111  using namespace std;
112  using namespace JPP;
113 
114  typedef JRange<int> JRange_t;
115 
116  string usr;
117  string pwd;
118  string cookie;
119  string outputFile;
120  string detid;
121  JRange_t runs;
122  vector<string> source;
123  JRange<TTimeStamp> UTC;
124  double Tmin_s;
125  int debug;
126 
127  try {
128 
129  JParser<> zap("Example program to plot quality data from data base.");
130 
131  zap['u'] = make_field(usr) = "";
132  zap['!'] = make_field(pwd) = "";
133  zap['C'] = make_field(cookie) = "";
134  zap['o'] = make_field(outputFile) = "monitor.root";
135  zap['D'] = make_field(detid, "detector identifier");
136  zap['R'] = make_field(runs, "run range") = JRange_t(1, JRange_t::getMaximum());
137  zap['S'] = make_field(source, "GIT versions") = getGITTags(TRegexp("v.*\\..*\\..*"), JGITTags_t::key_type("2019-04-12"));
138  zap['U'] = make_field(UTC, "UTC time range" ) = JRange<TTimeStamp>();
139  zap['T'] = make_field(Tmin_s, "minimal run duration [s]") = 60;
140  zap['d'] = make_field(debug) = 1;
141 
142  zap(argc, argv);
143  }
144  catch(const exception &error) {
145  FATAL(error.what() << endl);
146  }
147 
148 
149  int ID = -1; // integer representation of detector identifier
150 
151  vector<JRuns> data;
153  vector<JRunQuality> buffer;
154 
155  try {
156 
157  JDB::reset(usr, pwd, cookie);
158 
159  if (is_integer(detid))
160  ID = to_value<int>(detid);
161  else
162  ID = getDetector(detid);
163 
164  // runs
165 
166  try {
167 
168  ResultSet& rs = getResultSet(getTable<JRuns>(), getSelector<JRuns>(ID));
169 
170  for (JRuns parameters; rs >> parameters; ) {
171 
172  parameters.DETID = ID;
173 
174  if (UTC(parameters.getRunStartTime())) {
175  data.push_back(parameters);
176  }
177  }
178 
179  rs.Close();
180  }
181  catch(const exception& error) {}
182 
183  // data quality
184 
185  if (is_integer(detid)) {
186  detid = getDetector(to_value<int>(detid));
187  }
188 
189  for (vector<string>::const_iterator i = source.begin(); i != source.end(); ++i) {
190 
191  typedef map<string, string> data_type;
192  typedef map<int, data_type> map_type;
193 
194  map_type zmap;
195 
196  JSelector selector = getSelector<JRunSummaryNumbers>(detid, runs.getLowerLimit(), runs.getUpperLimit());
197 
198  selector.add(&JRunSummaryNumbers::SOURCE_NAME, *i);
199 
200  try {
201 
202  ResultSet& rs = getResultSet(getTable<JRunSummaryNumbers>(), selector);
203 
204  for (JRunSummaryNumbers parameters; rs >> parameters; ) {
205  zmap[parameters.RUN].insert(make_pair(parameters.PARAMETER_NAME, parameters.DATA_VALUE));
206  }
207 
208  rs.Close();
209  }
210  catch(const exception& error) {}
211 
212  for (map_type::const_iterator run = zmap.begin(); run != zmap.end(); ++run) {
213 
214  JRunQuality quality;
215 
216  quality.GIT = *i;
217  quality.detector = ID;
218  quality.run = run->first;
219 
220  for (data_type::const_iterator p = run->second.begin(); p != run->second.end(); ++p) {
221  quality.put(p->first, p->second);
222  }
223 
224  qaqc.insert(JRun_t(ID, quality.run));
225 
226  if (UTC(TTimeStamp(quality.UTCMin_s, 0)) &&
227  UTC(TTimeStamp(quality.UTCMax_s, 0))) {
228 
229  vector<JRunQuality>::iterator p = lower_bound(buffer.begin(), buffer.end(), quality);
230 
231  if (p == buffer.end() || p->run != quality.run) {
232  buffer.insert(p, quality);
233  }
234  }
235  }
236  }
237  }
238  catch(const exception& error) {
239  FATAL(error.what() << endl);
240  }
241 
242  if (buffer.empty()) {
243  FATAL("No valid QA/QC data for detector " << detid << endl);
244  }
245 
246 
247  // bins
248 
250 
251  for (vector<JRunQuality>::const_iterator quality = buffer.begin(); quality != buffer.end(); ++quality) {
252  X.push_back(quality->UTCMin_s);
253  X.push_back(quality->UTCMax_s);
254  }
255 
256  sort(X.begin(), X.end());
257 
258  struct Xmin {
259  Xmin(double xmin) :
260  xmin(xmin)
261  {}
262 
263  bool operator()(const double x1, const double x2)
264  {
265  return x2 - x1 <= xmin;
266  }
267 
268  double xmin;
269  };
270 
271  X.push_back(data.rbegin()->getRunStartTime()); // start of last run
272 
273  X.erase(unique(X.begin(), X.end(), Xmin(Tmin_s)), X.end());
274 
275  TH1D h0("livetime_s", NULL, X.size() - 1, X.data());
276  TH1D h1("QAQC", NULL, X.size() - 1, X.data());
277 
278  for (vector<JRuns>::const_iterator i = data.begin(); i != data.end(); ++i) {
279 
280  const JRun_t run(i->DETID,i->RUN);
281 
282  if (debug >= debug_t) {
283 
284  cout << "Run "
285  << setw(8) << i->RUN << ' '
286  << TTimeStamp((time_t) i->UNIXSTARTTIME/1000).AsString("c") << ' ';
287 
288  vector<JRunQuality>::const_iterator quality = lower_bound(buffer.begin(), buffer.end(), run);
289 
290  if (quality != buffer.end() && quality->run == i->RUN) {
291  cout << "[" << TTimeStamp((time_t) quality->UTCMin_s).AsString("c") << "," << TTimeStamp((time_t) quality->UTCMax_s).AsString("c") << "]";
292  } else {
293  cout << (qaqc.count(run) == 0 ? "missing" : "invalid") << " QA/QC data";
294  }
295 
296  cout << endl;
297  }
298 
299  double W = 1.0;
300 
301  if (! binary_search(buffer.begin(), buffer.end(), run)) {
302  W = (qaqc.count(run) == 0 ? -1.0 : 0.0);
303  }
304 
305  h1.Fill(i->getRunStartTime() + Tmin_s, W);
306  }
307 
308  JManager<string, TH1D> H1(new TH1D("%", NULL, X.size() - 1, X.data()));
309 
310  for (vector<JRunQuality>::const_iterator quality = buffer.begin(); quality != buffer.end(); ++quality) {
311 
312  const double x = 0.5 * (quality->UTCMin_s + quality->UTCMax_s);
313 
314  h0.Fill(x, 100.0 * quality->livetime_s / (quality->UTCMax_s - quality->UTCMin_s));
315 
316  H1["JDAQEvent"] -> Fill(x, quality->JDAQEvent);
317  H1["JTrigger3DShower"] -> Fill(x, quality->JTrigger3DShower);
318  H1["JTrigger3DMuon"] -> Fill(x, quality->JTrigger3DMuon);
319  H1["JTriggerMXShower"] -> Fill(x, quality->JTriggerMXShower);
320  }
321 
322 
323  Double_t W[2] = { 0.0 };
324 
325  W[0] = *X.rbegin() - *X.begin();
326 
327  for (vector<JRunQuality>::const_iterator quality = buffer.begin(); quality != buffer.end(); ++quality) {
328  W[1] += quality->livetime_s;
329  }
330 
331  NOTICE("Average data taking efficiency " << FIXED(5,1) << 100.0*W[1]/W[0] << " %." << endl);
332 
333 
334  for (TH1* p : { &h0, &h1 }) {
335  p->GetXaxis()->SetTimeDisplay(1);
336  p->GetXaxis()->SetTimeFormat(TIMESTAMP);
337  p->Sumw2(false);
338  }
339 
340  for (JManager<string, TH1D>::iterator p = H1.begin(); p != H1.end(); ++p) {
341 
342  Double_t W = 0.0;
343 
344  for (Int_t i = 1; i <= p->second->GetXaxis()->GetNbins(); ++i) {
345  p->second->SetBinContent(i, (W += p->second->GetBinContent(i)));
346  }
347 
348  p->second->GetXaxis()->SetTimeDisplay(1);
349  p->second->GetXaxis()->SetTimeFormat(TIMESTAMP);
350  p->second->Sumw2(false);
351  }
352 
353  TFile out(outputFile.c_str(), "recreate");
354 
355  out << h0 << h1 << H1;
356 
357  out.Write();
358  out.Close();
359 }
Utility class to parse command line options.
Definition: JParser.hh:1500
debug
Definition: JMessage.hh:29
std::vector< std::string > getGITTags(const TRegexp regexp, const JGITTags_t::key_type date)
Get selection of GIT tags.
Definition: JDB/JGITTags.hh:34
do rm f tmp H1
*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:69
then let ID
Definition: JAcoustics.sh:30
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:446
string outputFile
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
#define FATAL(A)
Definition: JMessage.hh:67
void reset(T &value)
Reset value.
ResultSet & getResultSet(const std::string &query)
Get result set.
Definition: JDB.hh:269
static const char *const TIMESTAMP
Time stamp of earliest UTC time.
int qaqc
QA/QC file descriptor.