Jpp  15.0.1-rc.1-highQE
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JZebraMantis.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <fstream>
3 
4 #include "TString.h"
5 #include "TRegexp.h"
6 #include "TObjArray.h"
7 #include "TObjString.h"
8 #include "TFile.h"
9 #include "TKey.h"
10 
11 #include "Jeep/JParser.hh"
12 
13 #include "JSupport/JMeta.hh"
14 
15 #include "JLang/JPredicate.hh"
16 
19 
20 #include "JGizmo/JRootObjectID.hh"
21 
22 namespace {
23 
24  /*
25  * Gets list of keys in a ROOT TDirectory and stores it on a vector.
26  *
27  * \param dir The ROOT directory
28  * \param buffer Vector to store keys
29  */
30  void readDir(TDirectory* dir,
32 
33  TIter iter(dir->GetListOfKeys());
34 
35  for (TKey* key; (key = (TKey*) iter.Next()) != NULL; ) {
36 
37  if (key->IsFolder()){
38 
39  dir->cd(key->GetName());
40 
41  TDirectory *subdir = gDirectory;
42  readDir(subdir, buffer);
43 
44  dir->cd();
45 
46  } else {
47 
48  JGIZMO::JRootObjectID objectID(MAKE_STRING(dir->GetPath() << key->GetName()));
49 
50  buffer.push_back(objectID);
51  }
52  }
53  }
54 }
55 
56 
57 /**
58  * \file
59  *
60  * Program to compare histograms in root files that have same directory structure,\n
61  * and where the histograms have the same names.
62  * The input histograms and the test to be applied for each histogram are specified\n
63  * in an ASCII formatted steering file which is passed by the command line.\n\n
64  *
65  * Each row of the steering file should have multiple columns.\n\n
66  *
67  * Column 1 is the name of the histogram to be compared (including the full path inside the root file)\n
68  * Column 2 is an integer value that indicates the test to be performed. See JTestDictionary()\n
69  * Columns 3..n are reserved for the different parameters of the test.\n\n
70  *
71  * \author rgruiz, bjung
72  */
73 int main(int argc, char** argv) {
74 
75  using namespace JPP;
76  using namespace std;
77 
78  string steeringFile;
79 
80  string file1;
81  string file2;
82 
83  string output;
84  string ascii;
85 
86  vector<string> keys;
87 
88  bool onlyFailures;
89 
90  int debug;
91 
92  const array_type<string>& listOfKeys = get_keys(JTestSummary().getProperties());
93 
94  try {
95 
96  const string& keysExplainer = MAKE_STRING("Terminal output:" << endl << listOfKeys);
97 
98  JParser<> zap("\nProgram to compare histograms in root files that have the same directory structure. See the link below this usage for further details.\n");
99 
100  zap['s'] = make_field(steeringFile , "ASCII steering file with list of histograms and tests");
101  zap['a'] = make_field(file1 , "input file 1");
102  zap['b'] = make_field(file2 , "input file 2");
103  zap['o'] = make_field(output , "output file root") = "zebramantis.root";
104  zap['t'] = make_field(ascii , "output file txt" ) = "zebramantis.txt";
105  zap['k'] = make_field(keys , keysExplainer ) = JPARSER::initialised();
106  zap['w'] = make_field(onlyFailures , "write only failed tests" );
107  zap['d'] = make_field(debug) = 2;
108 
109  zap(argc,argv);
110  }
111  catch(const exception &error) {
112  ERROR(error.what() << endl);
113  }
114 
115  if (keys.empty()) {
116  keys = listOfKeys;
117  }
118 
119  TFile* f1 = TFile::Open(file1.c_str());
120  TFile* f2 = TFile::Open(file2.c_str());
121 
122  TFile out(output.c_str(),"recreate");
123 
124  ofstream results;
125  results.open (ascii);
126  results << "# " << listOfKeys << endl;
127 
128  vector<JRootObjectID> objectIDs;
129  readDir(f1,objectIDs);
130 
131  std::ifstream infile(steeringFile);
132 
133  JTestDictionary d;
134 
135  size_t npassed = 0;
136  size_t nfailed = 0;
137 
138  for (string line; getline(infile, line); ) {
139 
140  istringstream iss(line);
141 
142  TString name;
143  int testID;
144 
145  if (!(iss >> name >> testID)) {
146  continue;
147  }
148 
149  const TRegexp regexp(name);
150 
151  for (vector<JRootObjectID>::const_iterator objectID = objectIDs.cbegin() ; objectID != objectIDs.cend() ; ++objectID) {
152 
153  const TString& objectDir = objectID->getDirectory();
154  const TString& objectName = objectID->getFullObjectName();
155 
156  if ((objectName.Index(regexp) != -1) && (f2->Get(objectName))) {
157 
158  TObject* obj1 = (TObject*)f1->Get(objectName);
159  TObject* obj2 = (TObject*)f2->Get(objectName);
160 
161  d[testID]->read(iss);
162  d[testID]->test(obj1,obj2);
163 
164  if (out.GetDirectory(objectDir)==0) {
165  out.mkdir(objectDir);
166  }
167 
168  out.cd(objectDir);
169 
170  for (vector<JTestResult>::iterator r = d[testID]->results.begin() ; r != d[testID]->results.end() ; ++r) {
171 
172  if (onlyFailures && r->passed) {
173  continue;
174  }
175 
176  print(cout, *r, keys.cbegin(), keys.cend(), ' ', false);
177  print(results, *r, listOfKeys.cbegin(), listOfKeys.cend(), ' ', true);
178 
179  r->obj->Write();
180  }
181 
182  const size_t Npass = count_if(d[testID]->results.cbegin(), d[testID]->results.cend(),
184 
185  npassed += Npass;
186  nfailed += (d[testID]->results.size() - Npass);
187 
188  d[testID]->clear();
189  }
190  }
191  }
192 
193  infile.close();
194 
195  results << WHITE << "# PASSED: " << npassed << " " << " FAILED: " << nfailed << " FAILURE FRACTION: " << float (nfailed)/(nfailed+npassed) << endl;
196 
197  putObject(&out, JMeta(argc, argv));
198  JMeta::copy(file1.c_str(), out);
199  JMeta::copy(file2.c_str(), out);
200 
201  results.close();
202  out .Close();
203 
204  return 0;
205 }
Utility class to parse command line options.
Definition: JParser.hh:1500
JPredicate< JResult_t T::*, JComparison::eq > make_predicate(JResult_t T::*member, const JResult_t value)
Helper method to create predicate for data member.
Definition: JPredicate.hh:128
int main(int argc, char *argv[])
Definition: Main.cc:15
JProperties & getProperties(T &object, const JEquationParameters &parameters=JEquationParameters(), const int debug=1)
Get properties of a given object.
static void copy(const char *const file_name, TFile &out)
Copy meta data.
Definition: JMeta.hh:421
then echo Enter input within $TIMEOUT_S seconds echo n User name
Definition: JCookie.sh:42
Definition: JRoot.hh:19
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:66
Auxiliary class to handle file name, ROOT directory and object name.
data_type r[M+1]
Definition: JPolint.hh:742
#define MAKE_STRING(A)
Make string.
Definition: JPrint.hh:142
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1961
ROOT I/O of application specific meta data.
#define ERROR(A)
Definition: JMessage.hh:66
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
bool putObject(TDirectory &dir, const TObject &object)
Write object to ROOT directory.
print
Definition: JConvertDusj.sh:44
then JMuonMCEvt f $INPUT_FILE o $INTERMEDIATE_FILE d
Definition: JMuonPath.sh:47
Utility class to parse command line options.
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)