Jpp 19.3.0-rc.1
the software that should make you happy
Loading...
Searching...
No Matches
JDataMonitor.cc File Reference

Auxiliary program to plot data from data base or input file. 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 "JLang/JLangToolkit.hh"
#include "JTools/JRange.hh"
#include "JROOT/JRootToolkit.hh"
#include "JROOT/JMathSupportkit.hh"
#include "JROOT/JASCIIFileStreamer.hh"
#include "JROOT/JManager.hh"
#include "JGizmo/JGizmoToolkit.hh"
#include "JSupport/JMeta.hh"
#include "JSupport/JFilenameSupportkit.hh"
#include "JDataQuality/JGITTags.hh"
#include "Jeep/JeepToolkit.hh"
#include "Jeep/JComment.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.
 
std::ostream & operator>> (std::ostream &out, const TTimeStamp &object)
 Write time stamp to output stream.
 
int main (int argc, char **argv)
 

Detailed Description

Auxiliary program to plot data from data base or input file.

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

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}
TString replace(const TString &target, const TRegexp &regexp, const T &replacement)
Replace regular expression in input by given replacement.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).

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

102{
103 return out << object.AsString();
104}

◆ main()

int main ( int argc,
char ** argv )

Definition at line 113 of file JDataMonitor.cc.

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 or input file.");
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
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
263 if (getFilenameExtension(outputFile) == ROOT_FILE_FORMAT) {
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
388 if (getFilenameExtension(outputFile) == ASCII_FILE_FORMAT) {
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}
string outputFile
#define NOTICE(A)
Definition JMessage.hh:64
#define FATAL(A)
Definition JMessage.hh:67
int debug
debug level
Definition JSirene.cc:72
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition JParser.hh:2142
Object reading from ASCII file.
Object(s) writing to ASCII file.
Utility class to parse command line options.
Definition JParser.hh:1698
Range of values.
Definition JRange.hh:42
#define R1(x)
const double xmin
ResultSet & getResultSet(const std::string &query)
Get result set.
Definition JDB.hh:438
std::vector< JServer > getServernames()
Get list of names of available database servers.
Definition JDB.hh:108
std::vector< std::string > getGITTags(const TRegexp &regexp, const JGITTags_t::key_type &date)
Get selection of GIT tags.
Auxiliary data structure for floating point format specification.
Definition JManip.hh:448
Data structure for measured coincidence rates of all pairs of PMTs in optical module.
Definition JFitK40.hh:103
Auxiliary data structure for data quality.
double UTCMin_s
minimal UTC time (from "runs" table)
void put(const std::string &key, const std::string &value)
Put value at given key.
double UTCMax_s
maximal UTC time (from "runs" table)
std::string GIT
GIT version used to write QA/QC data.
int detector
detector identifier
Wrapper class for server name.
Definition JDB.hh:54
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