Jpp
Functions
JTestTuna.cc File Reference
#include <string>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <map>
#include "TROOT.h"
#include "TFile.h"
#include "TObject.h"
#include "TKey.h"
#include "TString.h"
#include "TRegexp.h"
#include "TGraph.h"
#include "TF1.h"
#include "JGizmo/JRootObjectID.hh"
#include "JGizmo/JGizmoToolkit.hh"
#include "JTools/JRange.hh"
#include "JLang/JLangToolkit.hh"
#include "JLang/JColorFacet.hh"
#include "JLang/JVectorize.hh"
#include "Jeep/JParser.hh"
#include "Jeep/JMessage.hh"
#include "Jeep/JPrint.hh"
#include "Jeep/JColor.hh"

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Detailed Description

Auxiliary program to apply test criteria to file. The option -f corresponds to <file name>:<object name>.

Definition in file JTestTuna.cc.

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 93 of file JTestTuna.cc.

94 {
95  using namespace std;
96  using namespace JPP;
97 
98  JRootObjectID inputFile;
99  string parametersFile;
100  string outputFile;
101  string facet;
102  int debug;
103 
104  try {
105 
106  JParser<> zap("Auxiliary program to apply test criteria to file.");
107 
108  zap['f'] = make_field(inputFile, "<input file>:<object name>");
109  zap['P'] = make_field(parametersFile, "ASCII formatted input file with test criteria");
110  zap['o'] = make_field(outputFile, "Optional output file with copy of failing input") = "";
111  zap['F'] = make_field(facet, "Color facet") = get_keys(color_facets);
112  zap['d'] = make_field(debug) = 1;
113 
114  zap(argc, argv);
115  }
116  catch(const exception &error) {
117  FATAL(error.what() << endl);
118  }
119 
120 
121  typedef map<string, JParameters_t> map_type;
122 
123  map_type zmap;
124 
125  ifstream in(parametersFile.c_str());
126 
127  if (in) {
128 
129  string key;
130  JParameters_t parameters;
131 
132  for (string buffer; getline(in, buffer); ) {
133 
134  if (!buffer.empty() && buffer[0] != JParameters_t::SKIPLINE) {
135 
136  istringstream is(buffer);
137 
138  if (is >> key >> parameters) {
139  zmap[key] = parameters;
140  }
141  }
142  }
143 
144  in.close();
145 
146  } else {
147  FATAL("Error opening file: " << parametersFile << endl);
148  }
149 
150  if (debug >= debug_t) {
151  for (map_type::const_iterator i = zmap.begin(); i != zmap.end(); ++i) {
152  cout << setw(48) << left << i->first << ' ' << i->second << endl;
153  }
154  }
155 
156 
157  TDirectory* dir = getDirectory(inputFile);
158 
159  if (dir == NULL) {
160  FATAL("File: " << inputFile.getFullFilename() << " not opened." << endl);
161  }
162 
163 
164  TFile* out = (outputFile != "" ? new TFile(outputFile.c_str(), "recreate") : NULL);
165 
166  cout.imbue(locale(cout.getloc(), color_facets[facet]->clone()));
167 
168 
169  int number_of_tests = 0;
170  int number_of_failures = 0;
171 
172  const TRegexp regexp(inputFile.getObjectName());
173 
174  TIter iter(dir->GetListOfKeys());
175 
176  for (TKey* key; (key = (TKey*) iter.Next()) != NULL; ) {
177 
178  const TString tag(key->GetName());
179 
180  DEBUG("Key: " << tag << " match = " << tag.Contains(regexp) << endl);
181 
182  // option match
183 
184  if (tag.Contains(regexp)) {
185 
186  TGraph* g1 = dynamic_cast<TGraph*>(key->ReadObj());
187 
188  if (g1 != NULL) {
189 
190  map_type::const_iterator p = zmap.find(g1->GetName());
191 
192  if (p != zmap.end()) {
193 
194  const JParameters_t& parameters = p->second;
195 
196  const int N = g1->GetN();
197 
198  bool status = (N >= parameters.number_of_entries);
199 
200  int number_of_outliers = 0;
201 
202  for (int i = 0; i != g1->GetN(); ++i) {
203  if (!parameters.range(g1->GetY()[i])) {
204  ++number_of_outliers;
205  }
206  }
207 
208  status = status && (number_of_outliers <= parameters.number_of_outliers);
209 
210  cout << (status ? GREEN : RED) << "Test " << g1->GetName() << " " << (status ? "passed" : "failed") << "." << RESET << endl;
211 
212  if (out != NULL && !status) {
213 
214  const JRange<double> range(g1->GetX(), g1->GetX() + g1->GetN());
215 
216  g1->GetListOfFunctions()->Add(new TF1(MAKE_CSTRING(g1->GetName() << ":upper"), MAKE_CSTRING(parameters.range.getUpperLimit()), range.getLowerLimit(), range.getUpperLimit()));
217  g1->GetListOfFunctions()->Add(new TF1(MAKE_CSTRING(g1->GetName() << ":lower"), MAKE_CSTRING(parameters.range.getLowerLimit()), range.getLowerLimit(), range.getUpperLimit()));
218  out->WriteTObject(g1);
219  }
220 
221  number_of_tests += 1;
222  number_of_failures += (status ? 0 : 1);
223  }
224  }
225  }
226  }
227 
228  cout << (number_of_tests > 0 && number_of_failures == 0 ? GREEN : RED) << "Number of tests/failures " << number_of_tests << "/" << number_of_failures << RESET << endl;
229 
230  if (out != NULL) {
231 
232  out->Write();
233  out->Close();
234 
235  delete out;
236  }
237 }
g1
Double_t g1(const Double_t x)
Function.
Definition: JQuantiles.cc:25
JPARSER::JParser
Utility class to parse command line options.
Definition: JParser.hh:1493
JLANG::color_facets
static const JColorFacetMap_t color_facets
Color facets.
Definition: JColorFacet.hh:332
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
JLANG::RED
red
Definition: JColorFacet.hh:30
JGIZMO::getDirectory
TDirectory * getDirectory(const JRootObjectID &id)
Get TDirectory pointer.
Definition: JGizmoToolkit.hh:121
std::map
Definition: JSTDTypes.hh:16
make_field
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1954
JLANG::GREEN
green
Definition: JColorFacet.hh:31
DEBUG
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
std
Definition: jaanetDictionary.h:36
JLANG::get_keys
const std::vector< JKey_t > & get_keys(const std::map< JKey_t, JValue_t, JComparator_t, JAllocator_t > &data)
Method to create array of keys of map.
Definition: JVectorize.hh:141
FATAL
#define FATAL(A)
Definition: JMessage.hh:67
outputFile
string outputFile
Definition: JDAQTimesliceSelector.cc:37
JEEP::debug_t
debug
Definition: JMessage.hh:29
JLANG::getline
std::istream & getline(std::istream &in, JString &object)
Read string from input stream until end of line.
Definition: JString.hh:468
JLANG::RESET
reset
Definition: JColorFacet.hh:37