Jpp  18.2.0-rc.1
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 ROOT object identifiers in a ROOT TDirectory and stores it in a vector.
26  * Note: Subdirectories are searched recursively.
27  *
28  * \param dir The ROOT directory
29  */
31 
32  using namespace std;
33  using namespace JPP;
34 
35  vector<JRootObjectID> buffer1;
36 
37  TIter iter(dir->GetListOfKeys());
38 
39  for (TKey* key; (key = (TKey*) iter.Next()) != NULL; ) {
40 
41  if (key->IsFolder()){
42 
43  TDirectory *subdir = dir->GetDirectory(key->GetName());
44 
45  vector<JRootObjectID> buffer2 = readDir(subdir);
46 
47  buffer1.insert(buffer1.end(), buffer2.begin(), buffer2.end());
48 
49  } else {
50 
51  const string fullName = MAKE_STRING(dir->GetPath() <<
53  key->GetName());
54 
55  JRootObjectID objectID(fullName);
56 
57  buffer1.push_back(objectID);
58  }
59  }
60 
61  return buffer1;
62  }
63 }
64 
65 
66 /**
67  * \file
68  *
69  * Program to compare histograms in root files that have same directory structure,\n
70  * and where the histograms have the same names.
71  * The input histograms and the test to be applied for each histogram are specified\n
72  * in an ASCII formatted steering file which is passed by the command line.\n\n
73  *
74  * Each row of the steering file should have multiple columns.\n\n
75  *
76  * Column 1 is the name of the histogram to be compared (including the full path inside the root file)\n
77  * Column 2 is an integer value that indicates the test to be performed. See JTestDictionary()\n
78  * Columns 3..n are reserved for the different parameters of the test.\n\n
79  *
80  * \author rgruiz, bjung
81  */
82 int main(int argc, char** argv) {
83 
84  using namespace JPP;
85  using namespace std;
86 
87  string steeringFile;
88 
89  string file1;
90  string file2;
91 
92  string output;
93  string ascii;
94 
95  vector<string> keys;
96 
97  bool onlyFailures;
98 
99  int debug;
100 
101  const array_type<string>& listOfKeys = get_keys(JTestSummary().getProperties());
102 
103  try {
104 
105  const string& keysExplainer = MAKE_STRING("Terminal output:" << endl << listOfKeys);
106 
107  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");
108 
109  zap['s'] = make_field(steeringFile , "ASCII steering file with list of histograms and tests");
110  zap['a'] = make_field(file1 , "input file 1");
111  zap['b'] = make_field(file2 , "input file 2");
112  zap['o'] = make_field(output , "output file root") = "zebramantis.root";
113  zap['t'] = make_field(ascii , "output file txt" ) = "zebramantis.txt";
114  zap['k'] = make_field(keys , keysExplainer ) = JPARSER::initialised();
115  zap['w'] = make_field(onlyFailures , "write only failed tests" );
116  zap['d'] = make_field(debug) = 2;
117 
118  zap(argc,argv);
119  }
120  catch(const exception &error) {
121  ERROR(error.what() << endl);
122  }
123 
124  if (keys.empty()) {
125  keys = listOfKeys;
126  }
127 
128  TFile* f1 = TFile::Open(file1.c_str());
129  TFile* f2 = TFile::Open(file2.c_str());
130 
131  TFile out(output.c_str(),"recreate");
132 
133  ofstream results;
134  results.open (ascii);
135  results << "# " << listOfKeys << endl;
136 
137  const vector<JRootObjectID> objectIDs = readDir(f1);
138 
139  std::ifstream infile(steeringFile);
140 
141  JTestDictionary d;
142 
143  size_t npassed = 0;
144  size_t nfailed = 0;
145 
146  for (string line; getline(infile, line); ) {
147 
148  istringstream iss(line);
149 
150  TString name;
151  int testID;
152 
153  if (!(iss >> name >> testID)) {
154  continue;
155  }
156 
157  DEBUG("Input: " << name << ' ' << testID << endl);
158 
159  const TRegexp regexp(name);
160 
161  for (vector<JRootObjectID>::const_iterator objectID = objectIDs.cbegin() ; objectID != objectIDs.cend() ; ++objectID) {
162 
163  const TString& dirName = objectID->getDirectory();
164  const TString& fullName = objectID->getFullObjectName();
165 
166  DEBUG("Key: " << fullName << " match = " << fullName.Contains(regexp) << endl);
167 
168  if ((fullName.Index(regexp) != -1)) {
169 
170  TObject* obj1 = (TObject*)f1->Get(fullName);
171  TObject* obj2 = (TObject*)f2->Get(fullName);
172 
173  if (!obj1 || !obj2) {
174  DEBUG("Could not retrieve " << fullName << endl);
175  continue;
176  }
177 
178  d[testID]->read(iss);
179  d[testID]->test(obj1,obj2);
180 
181  if (dirName.Length() > 0 && !out.GetDirectory(dirName)) {
182 
183  if (dirName[0] == JRootObjectID::PATHNAME_SEPARATOR) { // Remove leading forward slash
184  out.mkdir(TString(dirName(1, dirName.Length() - 1)));
185  } else {
186  out.mkdir(dirName);
187  }
188  }
189 
190  out.cd(dirName);
191 
192  for (vector<JTestResult>::iterator r = d[testID]->results.begin() ; r != d[testID]->results.end() ; ++r) {
193 
194  if (onlyFailures && r->passed) {
195  continue;
196  }
197 
198  print(cout, *r, keys.cbegin(), keys.cend(), ' ', false);
199  print(results, *r, listOfKeys.cbegin(), listOfKeys.cend(), ' ', true);
200 
201  r->obj->Write();
202  }
203 
204  const size_t Npass = count_if(d[testID]->results.cbegin(), d[testID]->results.cend(),
206 
207  npassed += Npass;
208  nfailed += (d[testID]->results.size() - Npass);
209 
210  d[testID]->clear();
211  }
212  }
213  }
214 
215  infile.close();
216 
217  results << WHITE << "# PASSED: " << npassed << " " << " FAILED: " << nfailed << " FAILURE FRACTION: " << float (nfailed)/(nfailed+npassed) << endl;
218 
219  putObject(&out, JMeta(argc, argv));
220  JMeta::copy(file1.c_str(), out);
221  JMeta::copy(file2.c_str(), out);
222 
223  results.close();
224  out .Close();
225 
226  return 0;
227 }
Utility class to parse command line options.
Definition: JParser.hh:1514
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:83
data_type r[M+1]
Definition: JPolint.hh:779
#define MAKE_STRING(A)
Make string.
Definition: JPrint.hh:127
const JPolynome f1(1.0, 2.0, 3.0)
Function.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1989
static const char PATHNAME_SEPARATOR
path name separator
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
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.
static const char PATHNAME_SEPARATOR
path name separator
Definition: JeepToolkit.hh:39
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
int debug
debug level
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
void readDir(TDirectory *dir, std::vector< TString > &v)