Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JDataQuality.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <sstream>
3 #include <fstream>
4 #include <iomanip>
5 #include <set>
6 #include <map>
7 
8 #include "TROOT.h"
9 #include "TFile.h"
10 #include "TH1D.h"
11 #include "TF1.h"
12 #include "TString.h"
13 #include "TNtuple.h"
14 #include "TRegexp.h"
15 
16 #include "JDB/JDB.hh"
17 #include "JDB/JSelector.hh"
19 #include "JDB/JRunQuality.hh"
20 #include "JDB/JRunsetups.hh"
21 #include "JDB/JDBToolkit.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"
29 #include "JROOT/JRootDictionary.hh"
30 #include "JROOT/JManager.hh"
31 
32 #include "Jeep/JParser.hh"
33 #include "Jeep/JMessage.hh"
34 
35 namespace {
36 
37  /**
38  * Auxiliary data structure for run quality evaluation.
39  *
40  * A condition is composed of a ROOT TFormula compatible exoression and a range.
41  */
42  struct JCondition {
43 
44  static const char SEPARATOR = ';';
45 
46  /**
47  * Read selection from input stream.
48  *
49  * \param in input stream
50  * \param object selection
51  * \return input stream
52  */
53  friend inline std::istream& operator>>(std::istream& in, JCondition& object)
54  {
55  std::getline(in, object.formula, SEPARATOR);
56 
57  object.range = JTOOLS::JRange<double>();
58  object.weight = 1.0;
59 
60  if ((in >> object.range) && ! (in >> object.weight)) {
61  in.clear();
62  }
63 
64  return in;
65  }
66 
67 
68  /**
69  * Write selection to output stream.
70  *
71  * \param out output stream
72  * \param object selection
73  * \return output stream
74  */
75  friend inline std::ostream& operator<<(std::ostream& out, const JCondition& object)
76  {
77  out << object.formula << SEPARATOR << object.range << ' ' << object.weight << std::endl;
78 
79  return out;
80  }
81 
82  std::string formula;
84  double weight;
85  };
86 }
87 
88 
89 /**
90  * \file
91  *
92  * Example program to plot quality data from data base.
93  * \author mdejong
94  */
95 int main(int argc, char **argv)
96 {
97  using namespace std;
98  using namespace JPP;
99 
100  typedef JRange<int> JRange_t;
101  typedef vector<JCondition> JCondition_t;
102 
103  string usr;
104  string pwd;
105  string cookie;
106  string inputFile;
107  string outputFile;
108  string detid;
109  JRange_t runs;
110  vector<string> source;
111  JCondition_t condition;
112  JCondition_t veto;
113  TRegexp regexp(".");
114  JSelector selection;
115  int debug;
116 
117  try {
118 
119  JParser<> zap("Example program to plot quality data from data base."\
120  "\nThe condition and veto can be any TFormula compatible expression involving QA/QC parameters (see e.g. JQAQC.sh -h).");
121 
122  zap['u'] = make_field(usr) = "";
123  zap['!'] = make_field(pwd) = "";
124  zap['C'] = make_field(cookie) = "";
125  zap['f'] = make_field(inputFile, "Optional input file instead of database.") = "";
126  zap['o'] = make_field(outputFile, "Output file containing histograms and n-tuple.") = "quality.root";
127  zap['D'] = make_field(detid) = "";
128  zap['R'] = make_field(runs, "Run range") = JRange_t(1, JRange_t::getMaximum());
129  zap['S'] = make_field(source, "GIT versions") = getGITTags(TRegexp("v.*\\..*\\..*"), JGITTags_t::key_type("2019-04-12"));
130  zap['Q'] = make_field(condition, "User defined conditions");
131  zap['V'] = make_field(veto, "User defined vetos.") = JPARSER::initialised();
132  zap['r'] = make_field(regexp, "TRegexp for selection of run setup names.") = JPARSER::initialised();
133  zap['@'] = make_field(selection) = JPARSER::initialised();
134  zap['d'] = make_field(debug, "Debug level") = 1;
135 
136  zap(argc, argv);
137  }
138  catch(const exception &error) {
139  FATAL(error.what() << endl);
140  }
141 
142 
143  double W = 0.0;
144 
145  for (JCondition_t::const_iterator i = condition.begin(); i != condition.end(); ++i) {
146  W += i->weight;
147  }
148 
149  if (W <= 0.0) {
150  FATAL("Invalid total weight: " << W << endl);
151  }
152 
153  JRunsetups setup;
154 
155  set<JRunQuality> buffer;
156 
157  if (inputFile == "") {
158 
159  ASSERT(detid != "");
160 
161  int ID = -1; // integer representation of detectir identifier
162 
163  try {
164 
165  JDB::reset(usr, pwd, cookie);
166 
167  if (is_integer(detid))
168  ID = to_value<int>(detid);
169  else
170  ID = getDetector(detid);
171 
172  if (is_integer(detid)) {
173  detid = getDetector(to_value<int>(detid));
174  }
175 
176  selection += getSelector<JRuns>(ID);
177 
178  ResultSet& rs = getResultSet(getTable<JRuns>(), selection);
179 
180  for (JRuns parameters; rs >> parameters; ) {
181  if (TString(parameters.RUNSETUPNAME.c_str()).Contains(regexp)) {
182  setup.put(parameters);
183  }
184  }
185 
186  rs.Close();
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  if (setup.has(parameters.RUN)) {
205  zmap[parameters.RUN].insert(make_pair(parameters.PARAMETER_NAME, parameters.DATA_VALUE));
206  }
207  }
208  }
209  catch(const exception& error) {}
210 
211  for (map_type::const_iterator run = zmap.begin(); run != zmap.end(); ++run) {
212 
213  JRunQuality quality;
214 
215  quality.GIT = *i;
216  quality.detector = ID;
217  quality.run = run->first;
218 
219  for (data_type::const_iterator p = run->second.begin(); p != run->second.end(); ++p) {
220  quality.put(p->first, p->second);
221  }
222 
223  buffer.insert(quality);
224  }
225  }
226  }
227  catch(const exception& error) {
228  FATAL(error.what() << endl);
229  }
230 
231  } else {
232 
233  ifstream in(inputFile.c_str());
234 
235  for (JRunQuality quality; in >> quality; ) {
236  buffer.insert(quality);
237  }
238 
239  in.close();
240  }
241 
242  if (buffer.empty()) {
243  FATAL("No data." << endl);
244  }
245 
246  if (debug >= debug_t) {
247 
248  JRootWriter writer(cout, JEquationParameters(), JDBDictionary::getInstance());
249 
250  for (set<JRunQuality>::const_iterator i = buffer.begin(); i != buffer.end(); ++i) {
251  writer.put(*i);
252  }
253  }
254 
255 
256  runs = JRange_t(buffer.begin()->run, buffer.rbegin()->run);
257 
258  TH1D h0("h0", NULL, 1000, 0.0, 1.01);
259  TH1D h1("h1", NULL, condition.size(), -0.5, condition.size() + 0.5);
260 
261  h0.SetMinimum(0.0);
262  h0.SetMaximum(1.1 * (runs.getUpperLimit() - runs.getLowerLimit()));
263 
264  h1.SetMinimum(0.0);
265  h1.SetMaximum(1.1 * (runs.getUpperLimit() - runs.getLowerLimit()));
266 
267  JManager<TString, TH1D> zmap(new TH1D("%", NULL,
268  runs.getLength() + 1,
269  runs.getLowerLimit() - 0.5,
270  runs.getUpperLimit() + 0.5));
271 
272  // show range
273 
274  for (JCondition_t::const_iterator i = condition.begin(); i != condition.end();++i) {
275 
276  const string buffer = i->formula;
277 
278  zmap[buffer]->GetListOfFunctions()->Add(new TF1(MAKE_CSTRING(buffer << ":upper"), MAKE_CSTRING(i->range.getUpperLimit()), runs.getLowerLimit(), runs.getUpperLimit()));
279  zmap[buffer]->GetListOfFunctions()->Add(new TF1(MAKE_CSTRING(buffer << ":lower"), MAKE_CSTRING(i->range.getLowerLimit()), runs.getLowerLimit(), runs.getUpperLimit()));
280  }
281 
282  for (JCondition_t::const_iterator i = veto.begin(); i != veto.end();++i) {
283 
284  const string buffer = MAKE_STRING("VETO[" << i->formula << "]");
285 
286  zmap[buffer]->GetListOfFunctions()->Add(new TF1(MAKE_CSTRING(buffer << ":upper"), MAKE_CSTRING(i->range.getUpperLimit()), runs.getLowerLimit(), runs.getUpperLimit()));
287  zmap[buffer]->GetListOfFunctions()->Add(new TF1(MAKE_CSTRING(buffer << ":lower"), MAKE_CSTRING(i->range.getLowerLimit()), runs.getLowerLimit(), runs.getUpperLimit()));
288  }
289 
290  ostringstream os;
291 
292  os << "run";
293 
294  for (size_t i = 0; i != condition.size(); ++i) {
295  os << ":" << (char) ('a' + i);
296  }
297 
298  os << ":Q:V:R";
299 
300  TNtuple n1("n1", "quality", os.str().c_str());
301 
302 
303  resetObject(&h0);
304  resetObject(&h1);
305  resetObject(&n1);
306  resetObject(&zmap);
307 
308  // process data
309 
310  for (set<JRunQuality>::const_iterator quality = buffer.begin(); quality != buffer.end(); ++quality) {
311 
312  vector<Float_t> tuple;
313 
314  tuple.push_back((Float_t) quality->run);
315 
316  double w = 0.0;
317 
318  for (size_t i = 0; i != condition.size(); ++i) {
319 
320  const JCondition& ps = condition[i];
321  const double y = getResult(ps.formula, *quality);
322 
323  DEBUG(ps.formula << ' ' << y << endl);
324 
325  tuple.push_back((Float_t) y);
326 
327  TH1D* p = zmap[ps.formula];
328 
329  p->SetBinContent(p->FindBin((double) quality->run), y);
330  p->SetBinError (p->FindBin((double) quality->run), 0.0);
331 
332  if (ps.range(y)) {
333  w += ps.weight;
334  }
335 
336  h1.AddBinContent(i + 1, ps.range(y) ? 1.0 : 0.0);
337  }
338 
339  const double Q = w/W;
340 
341  tuple.push_back((Float_t) Q);
342 
343  int V = 0;
344 
345  for (size_t i = 0; i != veto.size(); ++i) {
346 
347  const JCondition& ps = veto[i];
348  const double y = getResult(ps.formula, *quality);
349 
350  TH1D* p = zmap[MAKE_STRING("VETO[" << ps.formula << "]")];
351 
352  p->SetBinContent(p->FindBin((double) quality->run), ps.range(y) ? 0.0 : 1.0);
353  p->SetBinError (p->FindBin((double) quality->run), 0.0);
354 
355  if (!ps.range(y)) {
356  ++V;
357  }
358  }
359 
360  tuple.push_back((Float_t) V);
361  tuple.push_back((Float_t) setup.get(quality->run));
362 
363  h0.Fill(Q);
364  n1.Fill(tuple.data());
365 
366  cout << setw(8) << quality->run << ' ' << FIXED(5,3) << Q << ' ' << setw(2) << V << endl;
367  }
368 
369  double w = 0.0;
370 
371  for (Int_t i = 0; i <= h0.GetXaxis()->GetNbins(); ++i) {
372  h0.SetBinContent(i, (w += h0.GetBinContent(i)));
373  }
374 
375  // output
376 
377  TFile out(outputFile.c_str(), "recreate");
378 
379  out << h0 << h1 << n1 << zmap;
380 
381  out.Write();
382  out.Close();
383 }
Utility class to parse command line options.
Definition: JParser.hh:1500
data_type w[N+1][M+1]
Definition: JPolint.hh:741
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
Print objects in ASCII format using ROOT dictionary.
*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
#define MAKE_CSTRING(A)
Make C-string.
Definition: JPrint.hh:151
then for HISTOGRAM in h0 h1
Definition: JMatrixNZ.sh:69
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:66
then let ID
Definition: JAcoustics.sh:30
Dynamic ROOT object management.
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:445
string outputFile
bool resetObject(JManager< JKey_t, JValue_t > *object, const bool reset=false)
Reset JManager object.
Definition: JManager.hh:366
#define MAKE_STRING(A)
Make string.
Definition: JPrint.hh:142
JDetectorsHelper getDetector
Function object for mapping serial number to object identifier of detector and vice versa...
Definition: JDBToolkit.cc:5
#define ASSERT(A,...)
Assert macro.
Definition: JMessage.hh:90
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
Double_t getResult(const TString &text, TObject *object=NULL)
Get result of given textual formula.
std::istream & getline(std::istream &in, JString &object)
Read string from input stream until end of line.
Definition: JString.hh:478
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
General purpose messaging.
z range($ZMAX-$ZMIN)< $MINIMAL_DZ." fi fi mv $WORKDIR/fit.root $MODULE_ROOT typeset -Z 4 STRING typeset -Z 2 FLOOR JPlot1D -f $
Definition: module-Z:fit.sh:84
#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:1549
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:269
std::ostream & operator<<(std::ostream &stream, const CLBCommonHeader &header)
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 typeset A TRIPODS get_tripods $WORKDIR tripod txt TRIPODS for EMITTER in
Definition: JCanberra.sh:36
std::vector< double > weight
Definition: JAlgorithm.hh:428
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
int main(int argc, char *argv[])
Definition: Main.cpp:15