Jpp  master_rocky
the software that should make you happy
Functions
JTestRange1D.cc File Reference

Auxiliary program to test contents of of 1D histograms. More...

#include <string>
#include <iostream>
#include <iomanip>
#include <vector>
#include <map>
#include "TROOT.h"
#include "TFile.h"
#include "TKey.h"
#include "TString.h"
#include "TRegexp.h"
#include "TH1.h"
#include "TProfile.h"
#include "TGraph.h"
#include "JTools/JRange.hh"
#include "JGizmo/JRootObjectID.hh"
#include "JGizmo/JGizmoToolkit.hh"
#include "Jeep/JColor.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

Auxiliary program to test contents of of 1D histograms.

Author
mdejong

Definition in file JTestRange1D.cc.

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 30 of file JTestRange1D.cc.

31 {
32  using namespace std;
33  using namespace JPP;
34 
35  typedef JRange<Double_t> JRange_t;
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) && isTObject(key)) {
92 
93  TObject* p = key->ReadObj();
94 
95  const TH1* h1 = ( dynamic_cast<TProfile*>(p) != NULL ? dynamic_cast<TProfile*>(p) :
96  (dynamic_cast<TH1*>(p) != NULL ? dynamic_cast<TH1*>(p) : NULL) );
97  const TGraph* g1 = ( dynamic_cast<TGraph*>(p) != NULL ? dynamic_cast<TGraph*>(p) : NULL );
98 
99  for (map_type::const_iterator i = zmap.begin(); i != zmap.end(); ++i) {
100 
101  const double value = getResult(i->first, p);
102  const JRange_t& range = i->second;
103 
104  DEBUG("Global test " << i->first << ' ' << (range(value) ? "passed" : "failed") << endl);
105 
106  ASSERT(range(value));
107  }
108 
109 
110  int number_of_events = 0;
111  int number_of_outliers = 0;
112 
113  if (h1 != NULL) {
114 
115  for (Int_t i = 1; i <= h1->GetNbinsX(); ++i) {
116 
117  const Double_t x = h1->GetBinCenter (i);
118  const Double_t y = h1->GetBinContent(i);
119 
120  if (X(x) == !invertX) {
121 
122  ++number_of_events;
123 
124  const bool ok = (Y(y) == !invertY);
125 
126  DEBUG("Test outlier " << h1->GetName() << " bin (" << i << ") (" << h1->GetXaxis()->GetBinLabel(i) << ") " << y << ' ' << (ok ? "passed" : "failed") << endl);
127 
128  if (!ok) {
129  ++number_of_outliers;
130  }
131  }
132  }
133 
134  } else if (g1 != NULL) {
135 
136  for (Int_t i = 0; i != g1->GetN(); ++i) {
137 
138  const Double_t x = g1->GetX()[i];
139  const Double_t y = g1->GetY()[i];
140 
141  if (X(x) == !invertX) {
142 
143  ++number_of_events;
144 
145  const bool ok = (Y(y) == !invertY);
146 
147  DEBUG("Test outlier " << g1->GetName() << " bin (" << i << ") " << y << ' ' << (ok ? "passed" : "failed") << endl);
148 
149  if (!ok) {
150  ++number_of_outliers;
151  }
152  }
153  }
154 
155  } else {
156 
157  FATAL("Object at " << *input << " is not TH1 nor TGraph." << endl);
158  }
159 
160 
161  cout << (number_of_outliers > numberOfOutliers ? RED : GREEN);
162  NOTICE("Number of outliers \"" << p->GetName() << "\" = " << number_of_outliers << "/" << number_of_events << endl);
163  cout << RESET;
164 
165  if (number_of_outliers > numberOfOutliers) {
166  ++number_of_failures;
167  }
168  }
169  }
170  }
171 
172  ASSERT(number_of_failures == 0);
173 
174  return 0;
175 }
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
#define ASSERT(A,...)
Assert macro.
Definition: JMessage.hh:90
#define NOTICE(A)
Definition: JMessage.hh:64
#define FATAL(A)
Definition: JMessage.hh:67
int debug
debug level
Definition: JSirene.cc:69
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:2142
Double_t g1(const Double_t x)
Function.
Definition: JQuantiles.cc:25
Utility class to parse command line options.
Definition: JParser.hh:1698
JValue_t second
Definition: JPair.hh:129
Range of values.
Definition: JRange.hh:42
TDirectory * getDirectory(const JRootObjectID &id)
Get TDirectory pointer.
bool isTObject(const TKey *key)
Check if given key corresponds to a TObject.
Double_t getResult(const TString &text, TObject *object=NULL)
Get result of given textual formula.
@ RED
red
Definition: JColorFacet.hh:30
@ RESET
reset
Definition: JColorFacet.hh:37
@ GREEN
green
Definition: JColorFacet.hh:31
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
std::map< int, range_type > map_type
Definition: JSTDTypes.hh:14
Type definition of range.
Definition: JHead.hh:43
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:68
Definition: JRoot.hh:19