Jpp  16.0.0-rc.1
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Functions
JAcousticsMonitorTest.cc File Reference
#include <string>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <map>
#include <locale>
#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"
#include "TH2D.h"

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 Program to test: More...
 

Function Documentation

int main ( int  argc,
char **  argv 
)

Program to test:

  • If there is acoustic data.
  • If the rate recorded by each receiver is within the expected range.
  • If emitters and receivers are working and compares it to what is expected.

Reports failure if: no acoustic data, too large number of outliers, emitter expected to work is not working, hydrophone expected to work is not working.

Definition at line 139 of file JAcousticsMonitorTest.cc.

140 {
141  using namespace std;
142  using namespace JPP;
143 
144  string inputFile;
145  string parametersFile;
146  string facet;
147  string outputFile;
148  int debug;
149  int number_of_failures = 0;
150  int run;
151 
152  try {
153 
154  JParser<> zap("Auxiliary program to apply test criteria to 2D histograms monitoring acoustic rate per emitter.");
155 
156  zap['f'] = make_field(inputFile, "output root file from JAcousticsMonitor_short");
157  zap['P'] = make_field(parametersFile, "ASCII formatted input file with test criteria (acoustic_monitor_00000XXX.txt)");
158  zap['F'] = make_field(facet, "Color facet") = get_keys(color_facets);
159  zap['d'] = make_field(debug) = 1;
160  zap['w'] = make_field(outputFile, "output summary file");
161  zap['r'] = make_field(run, "run number");
162  zap(argc, argv);
163  }
164  catch(const exception &error) {
165  FATAL(error.what() << endl);
166  }
167 
168  ofstream out(outputFile.c_str());
169  out.imbue(locale(out.getloc(), color_facets[facet]->clone()));
170  out << "ACOUSTIC MONITORING \nRun: " << run << endl;
171  out << "\n(Note: red highlights are the reason for the warning)" << endl;
172  // read parameters file
173 
174  typedef map<string, JParameters_t> map_type;
175 
176  map_type zmap;
177 
178  ifstream in(parametersFile.c_str());
179 
180  if (in) {
181 
182  string key;
183  JParameters_t parameters;
184 
185  for (string buffer; getline(in, buffer); ) {
186 
187  if (!buffer.empty() && buffer[0] != JParameters_t::SKIPLINE) {
188 
189  istringstream is(buffer);
190 
191  if (is >> key >> parameters) { zmap[key] = parameters; }
192  }
193  }
194 
195  in.close();
196 
197  } else {
198  FATAL("Error opening file: " << parametersFile << endl);
199  }
200 
201  // read input file
202 
203  TFile* f = TFile::Open(inputFile.c_str());
204 
205  vector<JRootObjectID> objectIDs;
206 
207  readDir(f,objectIDs);
208 
209  // check if the monitor.root file is empty
210  if(objectIDs.empty()) {
211  ++number_of_failures;
212  out << RED << "No acoustic data." << endl;
213  out << RESET;
214  }
215 
216  // loop over expected emitters
217 
218  for (map_type::const_iterator i = zmap.begin(); i != zmap.end(); ++i) {
219 
220  if (i->first[0] == '0') { continue; } // skip receivers
221 
222  out << "\nEmitter: " << i->first << endl;
223 
224  const TRegexp regexp(i->first);
225 
226 
227  // check if data from emitter is in input file
228 
229  for (vector<JRootObjectID>::const_iterator objectID = objectIDs.cbegin() ; objectID != objectIDs.cend() ; ++objectID) {
230 
231  const TString& objectName = objectID->getFullObjectName();
232 
233  // data from emitter is in input file
234  if (objectName.Index(regexp) != -1) {
235 
236  // check if expected status emitter changed
237  if (!i->second.working) {
238  out << (i->second.working != 0 ? RED : GREEN);
239  out << "Emitter " << i->first << " started working." << endl;
240  out << RESET;
241  }
242 
243  // test acoustic rate
244  TObject* p = (TObject*)f->Get(objectName);
245  TH2* h2 = NULL;
246 
247  int number_of_bins = 0;
248  int number_of_outliers = 0;
249 
250  double min_rate = i->second.expected_rate * i->second.range.getLowerLimit();
251  double max_rate = i->second.expected_rate * i->second.range.getUpperLimit();
252  int outliers = i->second.number_of_outliers;
253 
254  if (h2 == NULL && dynamic_cast<TH2*>(p) != NULL) { h2 = dynamic_cast<TH2*>(p); };
255 
256  if (h2 != NULL) {
257 
258  for (Int_t ix = 1; ix <= h2->GetXaxis()->GetNbins(); ++ix) {
259  for (Int_t iy = 1; iy <= h2->GetYaxis()->GetNbins(); ++iy) {
260 
261  const Double_t z = h2->GetBinContent(ix, iy);
262  string du = to_string(h2->GetXaxis()->GetBinLabel(ix));
263  string floor = to_string(h2->GetYaxis()->GetBinLabel(iy));
264 
265  du.insert(du.begin(), 4 - du.length(), '0');
266  floor.insert(floor.begin(), 3 - floor.length(), '0');
267  const string id = du + '.' + floor;
268 
269  int working = 1;
270  if (zmap.find(id)!=zmap.end()) { working = zmap.find(id)->second.working; }
271 
272  ++number_of_bins;
273 
274  // acoustic rate out of range
275  if (z < min_rate || z > max_rate) {
276 
277  ++number_of_outliers;
278  if (working) { // receiver expected to work
279 
280  if (floor == "000" && z == 0) { // if hydrophone doesn't work
281  out << "DU " << du << ", floor " << floor << " : Acoustic rate out of range -> " << z << " Hz." << RED << "Hydrophone stopped working." << endl;
282  out << RESET;
283  ++number_of_failures;
284  } else {
285  out << "DU " << du << ", floor " << floor << " : Acoustic rate out of range -> " << z << " Hz." << endl;
286  }
287  }
288  } else if (!working) { // receiver expected to not work
289 
290  out << "DU " << du << ", floor " << floor << " : Working again -> " << z << " Hz." << endl;
291  }
292  }
293  }
294  } else { FATAL("Object at " << objectName << " is not TH2." << endl); }
295 
296  out << (number_of_outliers > outliers ? RED : GREEN) << "Number of outliers = " << number_of_outliers << "/" << number_of_bins << endl;
297  out << RESET;
298 
299  if (number_of_outliers > outliers) {
300 
301  ++number_of_failures;
302  out << (number_of_outliers > outliers ? RED : GREEN) << "Test failed." << endl;
303  out << RESET;
304  } else {
305 
306  out << (outliers > number_of_outliers ? GREEN : RED) << "Test passed." << endl;
307  out << RESET;
308  }
309 
310  break;
311 
312  // data from emitter is not in input file
313  } else if (objectID + 1 == objectIDs.cend()) {
314 
315  // check if expected status emitter changed
316  if (i->second.working) {
317  ++number_of_failures;
318  out << (i->second.working != 0 ? RED : GREEN) << "Emitter " << i->first << " stopped working." << endl;
319  out << RESET;
320  } else {
321  out << "Emitter " << i->first << " is expected to not work." << endl;
322  }
323  }
324  }
325  }
326  // if number_of_failures > 0 program output needs to be reported
327  cout << number_of_failures << endl;
328  return 0;
329 }
Utility class to parse command line options.
Definition: JParser.hh:1500
o $QUALITY_ROOT d $DEBUG!JPlot1D f
Definition: JDataQuality.sh:66
Definition: JRoot.hh:19
*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
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
std::string to_string(const T &value)
Convert value to string.
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:42
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:139
void readDir(TDirectory *dir, std::vector< TString > &v)