Jpp  15.0.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JTestRange1D.cc
Go to the documentation of this file.
1 #include <string>
2 #include <iostream>
3 #include <iomanip>
4 #include <vector>
5 #include <map>
6 
7 #include "TROOT.h"
8 #include "TFile.h"
9 #include "TKey.h"
10 #include "TString.h"
11 #include "TRegexp.h"
12 #include "TH1.h"
13 #include "TProfile.h"
14 #include "TGraph.h"
15 
16 #include "JTools/JRange.hh"
17 #include "JGizmo/JRootObjectID.hh"
18 #include "JGizmo/JGizmoToolkit.hh"
19 
20 #include "Jeep/JColor.hh"
21 #include "Jeep/JParser.hh"
22 #include "Jeep/JMessage.hh"
23 
24 
25 /**
26  * \file
27  * Auxiliary program for y-range test of 1D histograms.
28  * \author mdejong
29  */
30 int main(int argc, char **argv)
31 {
32  using namespace std;
33  using namespace JPP;
34 
35  typedef JRange<Double_t> JRange_t;
36  typedef map<TString, JRange_t> map_type;
37 
38  vector<JRootObjectID> inputFile;
39  JRange_t X;
40  JRange_t Y;
41  bool invertX;
42  bool invertY;
43  int numberOfOutliers;
44  map_type zmap;
45  int debug;
46 
47  try {
48 
49  JParser<> zap("Auxiliary program to test contents of 1D histograms.");
50 
51  zap['f'] = make_field(inputFile, "measurement histogram, e.g: <file name>:<object name>");
52  zap['x'] = make_field(X, "accepted x-range values") = JRange_t();
53  zap['y'] = make_field(Y, "accepted y-range values") = JRange_t();
54  zap['X'] = make_field(invertX);
55  zap['Y'] = make_field(invertY);
56  zap['N'] = make_field(numberOfOutliers) = 0;
57  zap['H'] = make_field(zmap, "global tests") = JPARSER::initialised();
58  zap['d'] = make_field(debug) = 1;
59 
60  zap(argc, argv);
61  }
62  catch(const exception &error) {
63  FATAL(error.what() << endl);
64  }
65 
66 
67  int number_of_failures = 0;
68 
69  for (vector<JRootObjectID>::const_iterator input = inputFile.begin(); input != inputFile.end(); ++input) {
70 
71  DEBUG("Input: " << *input << endl);
72 
73  TDirectory* dir = getDirectory(*input);
74 
75  if (dir == NULL) {
76  FATAL("File: " << input->getFullFilename() << " not opened." << endl);
77  }
78 
79  const TRegexp regexp(input->getObjectName());
80 
81  TIter iter(dir->GetListOfKeys());
82 
83  for (TKey* key; (key = (TKey*) iter.Next()) != NULL; ) {
84 
85  const TString tag(key->GetName());
86 
87  DEBUG("Key: " << tag << " match = " << tag.Contains(regexp) << endl);
88 
89  // option match
90 
91  if (tag.Contains(regexp)) {
92 
93  TObject* p = key->ReadObj();
94 
95 
96  TH1* h1 = NULL;
97  TGraph* g1 = NULL;
98 
99  if (h1 == NULL && dynamic_cast<TProfile*>(p) != NULL) { h1 = dynamic_cast<TProfile*>(p)->ProjectionX(); }
100 
101  if (h1 == NULL && dynamic_cast<TH1*>(p) != NULL) { h1 = dynamic_cast<TH1*>(p); }
102 
103  if (g1 == NULL && dynamic_cast<TGraph*>(p) != NULL) { g1 = dynamic_cast<TGraph*>(p); }
104 
105 
106  for (map_type::const_iterator i = zmap.begin(); i != zmap.end(); ++i) {
107 
108  const double value = getResult(i->first, p);
109  const JRange_t& range = i->second;
110 
111  DEBUG("Global test " << i->first << ' ' << (range(value) ? "passed" : "failed") << endl);
112 
113  ASSERT(range(value));
114  }
115 
116 
117  int number_of_events = 0;
118  int number_of_outliers = 0;
119 
120  if (h1 != NULL) {
121 
122  for (Int_t i = 1; i <= h1->GetNbinsX(); ++i) {
123 
124  const Double_t x = h1->GetBinCenter (i);
125  const Double_t y = h1->GetBinContent(i);
126 
127  if (X(x) == !invertX) {
128 
129  ++number_of_events;
130 
131  const bool ok = (Y(y) == !invertY);
132 
133  DEBUG("Test outlier " << h1->GetName() << " bin (" << i << ") " << y << ' ' << (ok ? "passed" : "failed") << endl);
134 
135  if (!ok) {
136  ++number_of_outliers;
137  }
138  }
139  }
140 
141  } else if (g1 != NULL) {
142 
143  for (Int_t i = 0; i != g1->GetN(); ++i) {
144 
145  const Double_t x = g1->GetX()[i];
146  const Double_t y = g1->GetY()[i];
147 
148  if (X(x) == !invertX) {
149 
150  ++number_of_events;
151 
152  const bool ok = (Y(y) == !invertY);
153 
154  DEBUG("Test outlier " << g1->GetName() << " bin (" << i << ") " << y << ' ' << (ok ? "passed" : "failed") << endl);
155 
156  if (!ok) {
157  ++number_of_outliers;
158  }
159  }
160  }
161 
162  } else {
163 
164  FATAL("Object at " << *input << " is not TH1 nor TGraph." << endl);
165  }
166 
167 
168  cout << (number_of_outliers > numberOfOutliers ? RED : GREEN);
169  NOTICE("Number of outliers \"" << p->GetName() << "\" = " << number_of_outliers << "/" << number_of_events << endl);
170  cout << RESET;
171 
172  if (number_of_outliers > numberOfOutliers) {
173  ++number_of_failures;
174  }
175  }
176  }
177  }
178 
179  ASSERT(number_of_failures == 0);
180 
181  return 0;
182 }
Utility class to parse command line options.
Definition: JParser.hh:1500
int main(int argc, char *argv[])
Definition: Main.cc:15
Definition: JRoot.hh:19
then for HISTOGRAM in h0 h1
Definition: JMatrixNZ.sh:71
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:66
then fatal Wrong number of arguments fi set_variable STRING $argv[1] set_variable DETECTORXY_TXT $WORKDIR $DETECTORXY_TXT tail read X Y CHI2 RMS printf optimum n $X $Y $CHI2 $RMS awk v Y
#define ASSERT(A,...)
Assert macro.
Definition: JMessage.hh:90
I/O coloring auxiliaries.
#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.
#define NOTICE(A)
Definition: JMessage.hh:64
then break fi done getCenter read X Y Z let X
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 $
Auxiliary class to define a range between two values.
Utility class to parse command line options.
then usage $script< input_file >< detector_file > fi set_variable OUTPUT_DIR set_variable SELECTOR JDAQTimesliceL1 set_variable DEBUG case set_variable DEBUG
TDirectory * getDirectory(const JRootObjectID &id)
Get TDirectory pointer.
Double_t g1(const Double_t x)
Function.
Definition: JQuantiles.cc:25