Jpp
Functions
JDataQuality.cc File Reference
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <set>
#include <map>
#include "TROOT.h"
#include "TFile.h"
#include "TH1D.h"
#include "TF1.h"
#include "TString.h"
#include "TNtuple.h"
#include "TRegexp.h"
#include "JDB/JDB.hh"
#include "JDB/JSelector.hh"
#include "JDB/JSelectorSupportkit.hh"
#include "JDB/JRunQuality.hh"
#include "JDB/JRunsetups.hh"
#include "JDB/JDBToolkit.hh"
#include "JLang/JLangToolkit.hh"
#include "JTools/JRange.hh"
#include "JROOT/JRootPrinter.hh"
#include "JROOT/JRootToolkit.hh"
#include "JGizmo/JManager.hh"
#include "Jeep/JParser.hh"
#include "Jeep/JMessage.hh"

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Detailed Description

Example program to plot quality data from data base.

Author
mdejong

Definition in file JDataQuality.cc.

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 90 of file JDataQuality.cc.

91 {
92  using namespace std;
93  using namespace JPP;
94 
95  typedef JRange<int> JRange_t;
96  typedef vector<JSelection> JSelection_t;
97 
98  string usr;
99  string pwd;
100  string cookie;
101  string inputFile;
102  string outputFile;
103  string detid;
104  JRange_t runs;
105  vector<string> source;
106  JSelection_t selection;
107  JSelection_t veto;
108  TRegexp regexp(".");
109  int debug;
110 
111  try {
112 
113  JParser<> zap("Example program to plot quality data from data base.");
114 
115  zap['u'] = make_field(usr) = "";
116  zap['!'] = make_field(pwd) = "";
117  zap['C'] = make_field(cookie) = "";
118  zap['f'] = make_field(inputFile, "Optional input file instead of database.") = "";
119  zap['o'] = make_field(outputFile, "Output file containing histograms and n-tuple.") = "quality.root";
120  zap['D'] = make_field(detid) = "";
121  zap['R'] = make_field(runs, "Run range") = JRange_t(1, JRange_t::getMaximum());
122  zap['S'] = make_field(source, "GIT versions");
123  zap['Q'] = make_field(selection, "User defined selections");
124  zap['V'] = make_field(veto, "User defined vetos.") = JPARSER::initialised();
125  zap['r'] = make_field(regexp, "TRegexp for selection of run setup names.") = JPARSER::initialised();
126  zap['d'] = make_field(debug, "Debug level") = 1;
127 
128  zap(argc, argv);
129  }
130  catch(const exception &error) {
131  FATAL(error.what() << endl);
132  }
133 
134 
135  double W = 0.0;
136 
137  for (JSelection_t::const_iterator i = selection.begin(); i != selection.end(); ++i) {
138  W += i->weight;
139  }
140 
141  if (W <= 0.0) {
142  FATAL("Invalid total weight: " << W << endl);
143  }
144 
145  JRunsetups setup;
146 
147  set<JRunQuality> buffer;
148 
149  if (inputFile == "") {
150 
151  ASSERT(detid != "");
152 
153  int ID = -1; // integer representation of detectir identifier
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  if (is_integer(detid)) {
165  detid = getDetector(to_value<int>(detid));
166  }
167 
168  ResultSet& rs = getResultSet(getTable<JRuns>(), getSelector<JRuns>(ID));
169 
170  for (JRuns parameters; rs >> parameters; ) {
171  if (TString(parameters.RUNSETUPNAME.c_str()).Contains(regexp)) {
172  setup.put(parameters);
173  }
174  }
175 
176  rs.Close();
177 
178  for (vector<string>::const_iterator i = source.begin(); i != source.end(); ++i) {
179 
180  typedef map<string, string> data_type;
181  typedef map<int, data_type> map_type;
182 
183  map_type zmap;
184 
185  JSelector selector = getSelector<JRunSummaryNumbers>(detid, runs.getLowerLimit(), runs.getUpperLimit());
186 
187  selector.add(&JRunSummaryNumbers::SOURCE_NAME, *i);
188 
189  try {
190 
191  ResultSet& rs = getResultSet(getTable<JRunSummaryNumbers>(), selector);
192 
193  for (JRunSummaryNumbers parameters; rs >> parameters; ) {
194  if (setup.has(parameters.RUN)) {
195  zmap[parameters.RUN].insert(make_pair(parameters.PARAMETER_NAME, parameters.DATA_VALUE));
196  }
197  }
198  }
199  catch(const exception& error) {}
200 
201  for (map_type::const_iterator run = zmap.begin(); run != zmap.end(); ++run) {
202 
203  JRunQuality quality;
204 
205  quality.GIT = *i;
206  quality.detector = ID;
207  quality.run = run->first;
208 
209  for (data_type::const_iterator p = run->second.begin(); p != run->second.end(); ++p) {
210  quality.put(p->first, p->second);
211  }
212 
213  buffer.insert(quality);
214  }
215  }
216  }
217  catch(const exception& error) {
218  FATAL(error.what() << endl);
219  }
220 
221  } else {
222 
223  ifstream in(inputFile.c_str());
224 
225  for (JRunQuality quality; in >> quality; ) {
226  buffer.insert(quality);
227  }
228 
229  in.close();
230  }
231 
232  if (buffer.empty()) {
233  FATAL("No data." << endl);
234  }
235 
236 
237  runs = JRange_t(buffer.begin()->run, buffer.rbegin()->run);
238 
239  TH1D h0("h0", NULL, 1000, 0.0, 1.01);
240  TH1D h1("h1", NULL, selection.size(), -0.5, selection.size() + 0.5);
241 
242  h0.SetMinimum(0.0);
243  h0.SetMaximum(1.1 * (runs.getUpperLimit() - runs.getLowerLimit()));
244 
245  h1.SetMinimum(0.0);
246  h1.SetMaximum(1.1 * (runs.getUpperLimit() - runs.getLowerLimit()));
247 
248  JManager<TString, TH1D> zmap(new TH1D("%", NULL,
249  runs.getLength() + 1,
250  runs.getLowerLimit() - 0.5,
251  runs.getUpperLimit() + 0.5));
252 
253  // show range
254 
255  for (JSelection_t::const_iterator i = selection.begin(); i != selection.end();++i) {
256 
257  const string buffer = i->formula;
258 
259  zmap[buffer]->GetListOfFunctions()->Add(new TF1(MAKE_CSTRING(buffer << ":upper"), MAKE_CSTRING(i->range.getUpperLimit()), runs.getLowerLimit(), runs.getUpperLimit()));
260  zmap[buffer]->GetListOfFunctions()->Add(new TF1(MAKE_CSTRING(buffer << ":lower"), MAKE_CSTRING(i->range.getLowerLimit()), runs.getLowerLimit(), runs.getUpperLimit()));
261  }
262 
263  for (JSelection_t::const_iterator i = veto.begin(); i != veto.end();++i) {
264 
265  const string buffer = MAKE_STRING("VETO[" << i->formula << "]");
266 
267  zmap[buffer]->GetListOfFunctions()->Add(new TF1(MAKE_CSTRING(buffer << ":upper"), MAKE_CSTRING(i->range.getUpperLimit()), runs.getLowerLimit(), runs.getUpperLimit()));
268  zmap[buffer]->GetListOfFunctions()->Add(new TF1(MAKE_CSTRING(buffer << ":lower"), MAKE_CSTRING(i->range.getLowerLimit()), runs.getLowerLimit(), runs.getUpperLimit()));
269  }
270 
271  ostringstream os;
272 
273  os << "run";
274 
275  for (size_t i = 0; i != selection.size(); ++i) {
276  os << ":" << (char) ('a' + i);
277  }
278 
279  os << ":Q:V:R";
280 
281  TNtuple n1("n1", "quality", os.str().c_str());
282 
283 
284  h0.SetDirectory(NULL);
285  h1.SetDirectory(NULL);
286  n1.SetDirectory(NULL);
287 
288 
289  // process data
290 
291  for (set<JRunQuality>::const_iterator quality = buffer.begin(); quality != buffer.end(); ++quality) {
292 
293  vector<Float_t> tuple;
294 
295  tuple.push_back((Float_t) quality->run);
296 
297  double w = 0.0;
298 
299  for (size_t i = 0; i != selection.size(); ++i) {
300 
301  const JSelection& ps = selection[i];
302  const double y = getResult(ps.formula, *quality);
303 
304  tuple.push_back((Float_t) y);
305 
306  zmap[ps.formula]->Fill((double) quality->run, y);
307 
308  if (ps.range(y)) {
309  w += ps.weight;
310  }
311 
312  h1.AddBinContent(i + 1, ps.range(y) ? 1.0 : 0.0);
313  }
314 
315  const double Q = w/W;
316 
317  tuple.push_back((Float_t) Q);
318 
319  int V = 0;
320 
321  for (size_t i = 0; i != veto.size(); ++i) {
322 
323  const JSelection& ps = veto[i];
324  const double y = getResult(ps.formula, *quality);
325 
326  zmap[MAKE_STRING("VETO[" << ps.formula << "]")]->Fill((double) quality->run, ps.range(y) ? 0.0 : 1.0);
327 
328  if (!ps.range(y)) {
329  ++V;
330  }
331  }
332 
333  tuple.push_back((Float_t) V);
334  tuple.push_back((Float_t) setup.get(quality->run));
335 
336  h0.Fill(Q);
337  n1.Fill(tuple.data());
338 
339  cout << setw(8) << quality->run << ' ' << FIXED(5,3) << Q << ' ' << setw(2) << V << endl;
340  }
341 
342  double w = 0.0;
343 
344  for (Int_t i = 0; i <= h0.GetXaxis()->GetNbins(); ++i) {
345  h0.SetBinContent(i, (w += h0.GetBinContent(i)));
346  }
347 
348  // output
349 
350  TFile out(outputFile.c_str(), "recreate");
351 
352  out << h0 << h1 << n1 << zmap;
353 
354  out.Write();
355  out.Close();
356 }
JManager
Auxiliary class to manage set of histograms.
Definition: JHistogramToolkit.hh:160
JTOOLS::w
data_type w[N+1][M+1]
Definition: JPolint.hh:708
FIXED
Auxiliary data structure for floating point format specification.
Definition: JPrint.hh:481
ASSERT
#define ASSERT(A,...)
Assert macro.
Definition: JMessage.hh:90
JLANG::is_integer
bool is_integer(const std::string &buffer)
Check if string is an integer.
Definition: JLangToolkit.hh:58
JPARSER::initialised
Empty structure for specification of parser element that is initialised (i.e.
Definition: JParser.hh:63
JGIZMO::getResult
Double_t getResult(const TString &text, TObject *object=NULL)
Get result of given textual formula.
Definition: JGizmoToolkit.hh:212
std::vector
Definition: JSTDTypes.hh:12
JPARSER::JParser
Utility class to parse command line options.
Definition: JParser.hh:1493
MAKE_STRING
#define MAKE_STRING(A)
Make string.
Definition: JPrint.hh:699
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
MAKE_CSTRING
#define MAKE_CSTRING(A)
Make C-string.
Definition: JPrint.hh:708
debug
int debug
debug level
Definition: JSirene.cc:59
std::map
Definition: JSTDTypes.hh:16
ID
Definition: raw_data_converter.cpp:43
JDETECTOR::reset
void reset(JCLBInput &data, size_t size)
Reset CLB buffers.
Definition: JCLBSimulator.hh:41
make_field
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1954
std
Definition: jaanetDictionary.h:36
FATAL
#define FATAL(A)
Definition: JMessage.hh:67
outputFile
string outputFile
Definition: JDAQTimesliceSelector.cc:37