Jpp - the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Functions
JTestTuna.cc File Reference

Auxiliary program to apply test criteria to file. More...

#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

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  map<string, int> counts;
122 
123  typedef map<string, JParameters_t> map_type;
124 
125  map_type zmap;
126 
127  ifstream in(parametersFile.c_str());
128 
129  if (in) {
130 
131  string key;
132  JParameters_t parameters;
133 
134  for (string buffer; getline(in, buffer); ) {
135 
136  if (!buffer.empty() && buffer[0] != JParameters_t::SKIPLINE) {
137 
138  istringstream is(buffer);
139 
140  if (is >> key >> parameters) {
141  zmap [key] = parameters;
142  counts[key] = 0;
143  }
144  }
145  }
146 
147  in.close();
148 
149  } else {
150  FATAL("Error opening file: " << parametersFile << endl);
151  }
152 
153  if (debug >= debug_t) {
154  for (map_type::const_iterator i = zmap.begin(); i != zmap.end(); ++i) {
155  cout << setw(48) << left << i->first << ' ' << i->second << endl;
156  }
157  }
158 
159 
160  TDirectory* dir = getDirectory(inputFile);
161 
162  if (dir == NULL) {
163  FATAL("File: " << inputFile.getFullFilename() << " not opened." << endl);
164  }
165 
166 
167  TFile* out = (outputFile != "" ? new TFile(outputFile.c_str(), "recreate") : NULL);
168 
169  cout.imbue(locale(cout.getloc(), color_facets[facet]->clone()));
170 
171  int number_of_tests = 0;
172  int number_of_failures = 0;
173 
174  const TRegexp regexp(inputFile.getObjectName());
175 
176  TIter iter(dir->GetListOfKeys());
177 
178  for (TKey* key; (key = (TKey*) iter.Next()) != NULL; ) {
179 
180  const TString tag(key->GetName());
181 
182  DEBUG("Key: " << tag << " match = " << tag.Contains(regexp) << endl);
183 
184  // option match
185 
186  if (tag.Contains(regexp) && strcmp(key->GetClassName(),TGraph::Class_Name()) == 0) {
187 
188  TGraph* g1 = dynamic_cast<TGraph*>(key->ReadObj());
189 
190  DEBUG(key->GetName() << ' ' << (g1 != NULL) << endl);
191 
192  if (g1 != NULL) {
193 
194  map_type::const_iterator p = zmap.find(g1->GetName());
195 
196  if (p != zmap.end()) {
197 
198  counts[p->first] += 1;
199 
200  const JParameters_t& parameters = p->second;
201 
202  const int N = g1->GetN();
203 
204  bool status = (N >= parameters.number_of_entries);
205 
206  int number_of_outliers = 0;
207 
208  for (int i = 0; i != g1->GetN(); ++i) {
209  if (!parameters.range(g1->GetY()[i])) {
210  ++number_of_outliers;
211  }
212  }
213 
214  status = status && (number_of_outliers <= parameters.number_of_outliers);
215 
216  cout << (status ? GREEN : RED) << "Test " << g1->GetName() << " " << (status ? "passed" : "failed") << "." << RESET << endl;
217 
218  if (out != NULL && !status) {
219 
220  const JRange<double> range(g1->GetX(), g1->GetX() + g1->GetN());
221 
222  g1->GetListOfFunctions()->Add(new TF1(MAKE_CSTRING(g1->GetName() << ":upper"), MAKE_CSTRING(parameters.range.getUpperLimit()), range.getLowerLimit(), range.getUpperLimit()));
223  g1->GetListOfFunctions()->Add(new TF1(MAKE_CSTRING(g1->GetName() << ":lower"), MAKE_CSTRING(parameters.range.getLowerLimit()), range.getLowerLimit(), range.getUpperLimit()));
224  out->WriteTObject(g1);
225  }
226 
227  number_of_tests += 1;
228  number_of_failures += (status ? 0 : 1);
229  }
230  }
231  }
232  }
233 
234  for (map<string, int>::const_iterator i = counts.begin(); i != counts.end(); ++i) {
235  if (i->second == 0) {
236 
237  cout << RED << "Test " << i->first << " missing data." << RESET << endl;
238 
239  number_of_tests += 1;
240  number_of_failures += 1;
241  }
242  }
243 
244  cout << (number_of_tests > 0 && number_of_failures == 0 ? GREEN : RED) << "Number of tests/failures " << number_of_tests << "/" << number_of_failures << RESET << endl;
245 
246  if (out != NULL) {
247 
248  out->Write();
249  out->Close();
250 
251  delete out;
252  }
253 }
Utility class to parse command line options.
Definition: JParser.hh:1500
debug
Definition: JMessage.hh:29
*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
string outputFile
is
Definition: JDAQCHSM.chsm:167
static const JColorFacetMap_t color_facets
Color facets.
Definition: JColorFacet.hh:332
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1961
std::istream & getline(std::istream &in, JString &object)
Read string from input stream until end of line.
Definition: JString.hh:478
int debug
debug level
Definition: JSirene.cc:63
#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 $
TDirectory * getDirectory(const JRootObjectID &id)
Get TDirectory pointer.
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
then usage $script[input file[working directory[option]]] nWhere option can be N
Definition: JMuonPostfit.sh:37
const array_type< 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:122
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
Double_t g1(const Double_t x)
Function.
Definition: JQuantiles.cc:25