Jpp
Functions
JDataMonitor.cc File Reference
#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 "JLang/JVectorize.hh"
#include "JLang/JLangToolkit.hh"
#include "JTools/JRange.hh"
#include "JROOT/JRootPrinter.hh"
#include "JROOT/JRootToolkit.hh"
#include "JROOT/JMathSupportkit.hh"
#include "JGizmo/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

◆ operator>>() [1/2]

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 45 of file JDataMonitor.cc.

46 {
47  using namespace std;
48  using namespace JPP;
49 
50  string buffer;
51 
52  if (in >> buffer) {
53 
54  UInt_t year;
55  UInt_t month;
56  UInt_t day;
57 
58  istringstream is;
59 
60  is.str(replace(buffer, '-', ' '));
61 
62  if (is >> year >> month >> day) {
63 
64  UInt_t hour = 0;
65  UInt_t min = 0;
66  UInt_t sec = 0;
67 
68  if (in >> buffer) {
69 
70  is.clear();
71  is.str(replace(buffer, ':', ' '));
72 
73  if (is >> hour >> min >> sec) {
74 
75  object = TTimeStamp(year, month, day, hour, min, sec);
76 
77  return in;
78  }
79  }
80  }
81  }
82 
83  in.setstate(ios::failbit);
84 
85  return in;
86 }

◆ operator>>() [2/2]

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 96 of file JDataMonitor.cc.

97 {
98  return out << object.AsString();
99 }

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 108 of file JDataMonitor.cc.

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