#include #include #include #include #include #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" namespace { using JTOOLS::JRange; /** * Auxilliary data structure with test criteria. */ struct JParameters_t { static const char SKIPLINE = '#'; //!< skip line character /** * Default contrustor. */ JParameters_t() : number_of_entries (0), number_of_outliers(0), range(JRange::DEFAULT_RANGE()) {} /** * Read parameters from input stream. * * \param input input stream * \param object parameters * \return input stream */ friend inline std::istream& operator>>(std::istream& in, JParameters_t& object) { return in >> object.number_of_entries >> object.number_of_outliers >> object.range; } /** * Write parameters to output stream. * * \param output output stream * \param object parameters * \return output stream */ friend inline std::ostream& operator<<(std::ostream& out, const JParameters_t& object) { using namespace std; using namespace JPP; return out << setw(5) << object.number_of_entries << ' ' << setw(3) << object.number_of_outliers << ' ' << FIXED(15,3) << object.range.getLowerLimit() << ' ' << FIXED(15,3) << object.range.getUpperLimit(); } int number_of_entries; int number_of_outliers; JRange range; }; } /** * \file * * Auxiliary program to apply test criteria to file. * The option -f corresponds to \:\. */ int main(int argc, char **argv) { using namespace std; using namespace JPP; JRootObjectID inputFile; string parametersFile; string outputFile; string facet; int debug; try { JParser<> zap("Auxiliary program to apply test criteria to file."); zap['f'] = make_field(inputFile, ":"); zap['P'] = make_field(parametersFile, "ASCII formatted input file with test criteria"); zap['o'] = make_field(outputFile, "Optional output file with copy of failing input") = ""; zap['F'] = make_field(facet, "Color facet") = get_keys(color_facets); zap['d'] = make_field(debug) = 1; zap(argc, argv); } catch(const exception &error) { FATAL(error.what() << endl); } map counts; typedef map map_type; map_type zmap; ifstream in(parametersFile.c_str()); if (in) { string key; JParameters_t parameters; for (string buffer; getline(in, buffer); ) { if (!buffer.empty() && buffer[0] != JParameters_t::SKIPLINE) { istringstream is(buffer); if (is >> key >> parameters) { zmap [key] = parameters; counts[key] = 0; } } } in.close(); } else { FATAL("Error opening file: " << parametersFile << endl); } if (debug >= debug_t) { for (map_type::const_iterator i = zmap.begin(); i != zmap.end(); ++i) { cout << setw(48) << left << i->first << ' ' << i->second << endl; } } TDirectory* dir = getDirectory(inputFile); if (dir == NULL) { FATAL("File: " << inputFile.getFullFilename() << " not opened." << endl); } TFile* out = (outputFile != "" ? new TFile(outputFile.c_str(), "recreate") : NULL); cout.imbue(locale(cout.getloc(), color_facets[facet]->clone())); int number_of_tests = 0; int number_of_failures = 0; const TRegexp regexp(inputFile.getObjectName()); TIter iter(dir->GetListOfKeys()); for (TKey* key; (key = (TKey*) iter.Next()) != NULL; ) { const TString tag(key->GetName()); DEBUG("Key: " << tag << " match = " << tag.Contains(regexp) << endl); // option match if (tag.Contains(regexp) && isTObject(key) && strcmp(key->GetClassName(),TGraph::Class_Name()) == 0) { TGraph* g1 = dynamic_cast(key->ReadObj()); DEBUG(key->GetName() << ' ' << (g1 != NULL) << endl); if (g1 != NULL) { map_type::const_iterator p = zmap.find(g1->GetName()); if (p != zmap.end()) { counts[p->first] += 1; const JParameters_t& parameters = p->second; const int number_of_entries = g1->GetN(); int number_of_outliers = 0; for (int i = 0; i != g1->GetN(); ++i) { if (!parameters.range(g1->GetY()[i])) { ++number_of_outliers; } } const bool status = (number_of_entries >= parameters.number_of_entries && number_of_outliers <= parameters.number_of_outliers); cout << (status ? GREEN : RED) << "Test " << g1->GetName() << ' ' << (status ? "passed" : "failed") << ' ' << (number_of_entries >= parameters.number_of_entries ? "" : "too few entries") << ' ' << (number_of_outliers <= parameters.number_of_outliers ? "" : "too many outliers") << '.' << RESET << endl; if (out != NULL && !status) { const JRange range(g1->GetX(), g1->GetX() + g1->GetN()); g1->GetListOfFunctions()->Add(new TF1(MAKE_CSTRING(g1->GetName() << ":upper"), MAKE_CSTRING(parameters.range.getUpperLimit()), range.getLowerLimit(), range.getUpperLimit())); g1->GetListOfFunctions()->Add(new TF1(MAKE_CSTRING(g1->GetName() << ":lower"), MAKE_CSTRING(parameters.range.getLowerLimit()), range.getLowerLimit(), range.getUpperLimit())); out->WriteTObject(g1); } number_of_tests += 1; number_of_failures += (status ? 0 : 1); } } } } for (map::const_iterator i = counts.begin(); i != counts.end(); ++i) { if (i->second == 0) { cout << RED << "Test " << i->first << " missing data." << RESET << endl; number_of_tests += 1; number_of_failures += 1; } } cout << (number_of_tests > 0 && number_of_failures == 0 ? GREEN : RED) << "Number of tests/failures " << number_of_tests << "/" << number_of_failures << RESET << endl; if (out != NULL) { out->Write(); out->Close(); delete out; } }