Jpp - the software that should make you happy
 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 detector 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  rs.Close();
210  }
211  catch(const exception& error) {}
212 
213  for (map_type::const_iterator run = zmap.begin(); run != zmap.end(); ++run) {
214 
215  JRunQuality quality;
216 
217  quality.GIT = *i;
218  quality.detector = ID;
219  quality.run = run->first;
220 
221  for (data_type::const_iterator p = run->second.begin(); p != run->second.end(); ++p) {
222  quality.put(p->first, p->second);
223  }
224 
225  buffer.insert(quality);
226  }
227  }
228  }
229  catch(const exception& error) {
230  FATAL(error.what() << endl);
231  }
232 
233  } else {
234 
235  ifstream in(inputFile.c_str());
236 
237  for (JRunQuality quality; in >> quality; ) {
238  buffer.insert(quality);
239  }
240 
241  in.close();
242  }
243 
244  if (buffer.empty()) {
245  FATAL("No data." << endl);
246  }
247 
248  if (debug >= debug_t) {
249 
250  JRootWriter writer(cout, JEquationParameters(), JDBDictionary::getInstance());
251 
252  for (set<JRunQuality>::const_iterator i = buffer.begin(); i != buffer.end(); ++i) {
253  writer.put(*i);
254  }
255  }
256 
257 
258  runs = JRange_t(buffer.begin()->run, buffer.rbegin()->run);
259 
260  TH1D h0("h0", NULL, 1000, 0.0, 1.01);
261  TH1D h1("h1", NULL, condition.size(), -0.5, condition.size() + 0.5);
262 
263  h0.SetMinimum(0.0);
264  h0.SetMaximum(1.1 * (runs.getUpperLimit() - runs.getLowerLimit()));
265 
266  h1.SetMinimum(0.0);
267  h1.SetMaximum(1.1 * (runs.getUpperLimit() - runs.getLowerLimit()));
268 
269  JManager<TString, TH1D> zmap(new TH1D("%", NULL,
270  runs.getLength() + 1,
271  runs.getLowerLimit() - 0.5,
272  runs.getUpperLimit() + 0.5));
273 
274  // show range
275 
276  for (JCondition_t::const_iterator i = condition.begin(); i != condition.end();++i) {
277 
278  const string buffer = i->formula;
279 
280  zmap[buffer]->GetListOfFunctions()->Add(new TF1(MAKE_CSTRING(buffer << ":upper"), MAKE_CSTRING(i->range.getUpperLimit()), runs.getLowerLimit(), runs.getUpperLimit()));
281  zmap[buffer]->GetListOfFunctions()->Add(new TF1(MAKE_CSTRING(buffer << ":lower"), MAKE_CSTRING(i->range.getLowerLimit()), runs.getLowerLimit(), runs.getUpperLimit()));
282  }
283 
284  for (JCondition_t::const_iterator i = veto.begin(); i != veto.end();++i) {
285 
286  const string buffer = MAKE_STRING("VETO[" << i->formula << "]");
287 
288  zmap[buffer]->GetListOfFunctions()->Add(new TF1(MAKE_CSTRING(buffer << ":upper"), MAKE_CSTRING(i->range.getUpperLimit()), runs.getLowerLimit(), runs.getUpperLimit()));
289  zmap[buffer]->GetListOfFunctions()->Add(new TF1(MAKE_CSTRING(buffer << ":lower"), MAKE_CSTRING(i->range.getLowerLimit()), runs.getLowerLimit(), runs.getUpperLimit()));
290  }
291 
292  ostringstream os;
293 
294  os << "run";
295 
296  for (size_t i = 0; i != condition.size(); ++i) {
297  os << ":" << (char) ('a' + i);
298  }
299 
300  os << ":Q:V:R";
301 
302  TNtuple n1("n1", "quality", os.str().c_str());
303 
304 
305  resetObject(&h0);
306  resetObject(&h1);
307  resetObject(&n1);
308  resetObject(&zmap);
309 
310  // process data
311 
312  for (set<JRunQuality>::const_iterator quality = buffer.begin(); quality != buffer.end(); ++quality) {
313 
314  vector<Float_t> tuple;
315 
316  tuple.push_back((Float_t) quality->run);
317 
318  double w = 0.0;
319 
320  for (size_t i = 0; i != condition.size(); ++i) {
321 
322  const JCondition& ps = condition[i];
323  const double y = getResult(ps.formula, *quality);
324 
325  DEBUG(ps.formula << ' ' << y << endl);
326 
327  tuple.push_back((Float_t) y);
328 
329  TH1D* p = zmap[ps.formula];
330 
331  p->SetBinContent(p->FindBin((double) quality->run), y);
332  p->SetBinError (p->FindBin((double) quality->run), 0.0);
333 
334  if (ps.range(y)) {
335  w += ps.weight;
336  }
337 
338  h1.AddBinContent(i + 1, ps.range(y) ? 1.0 : 0.0);
339  }
340 
341  const double Q = w/W;
342 
343  tuple.push_back((Float_t) Q);
344 
345  int V = 0;
346 
347  for (size_t i = 0; i != veto.size(); ++i) {
348 
349  const JCondition& ps = veto[i];
350  const double y = getResult(ps.formula, *quality);
351 
352  TH1D* p = zmap[MAKE_STRING("VETO[" << ps.formula << "]")];
353 
354  p->SetBinContent(p->FindBin((double) quality->run), ps.range(y) ? 0.0 : 1.0);
355  p->SetBinError (p->FindBin((double) quality->run), 0.0);
356 
357  if (!ps.range(y)) {
358  ++V;
359  }
360  }
361 
362  tuple.push_back((Float_t) V);
363  tuple.push_back((Float_t) setup.get(quality->run));
364 
365  h0.Fill(Q);
366  n1.Fill(tuple.data());
367 
368  cout << setw(8) << quality->run << ' ' << FIXED(5,3) << Q << ' ' << setw(2) << V << endl;
369  }
370 
371  double w = 0.0;
372 
373  for (Int_t i = 0; i <= h0.GetXaxis()->GetNbins(); ++i) {
374  h0.SetBinContent(i, (w += h0.GetBinContent(i)));
375  }
376 
377  // output
378 
379  TFile out(outputFile.c_str(), "recreate");
380 
381  out << h0 << h1 << n1 << zmap;
382 
383  out.Write();
384  out.Close();
385 }
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
int main(int argc, char *argv[])
Definition: Main.cc:15
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:446
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.
#define FATAL(A)
Definition: JMessage.hh:67
z range($ZMAX-$ZMIN)< $MINIMAL_DZ." fi fi typeset -Z 4 STRING typeset -Z 2 FLOOR JPlot1D -f $
void reset(T &value)
Reset value.
std::istream & operator>>(std::istream &in, JAANET::JHead &header)
Read header from input.
Definition: JHead.hh:1618
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 CHECK_EXIT_CODE typeset A TRIPODS get_tripods $WORKDIR tripod txt TRIPODS for EMITTER in
Definition: JCanberra.sh:38
std::vector< double > weight
Definition: JAlgorithm.hh:428
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62