Jpp
 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 #include "JSupport/JMeta.hh"
15 
16 using namespace JPP;
17 using namespace std;
18 
19 
20 /*
21  * Gets list of keys in a ROOT TDirectory and stores it on a vector.
22  *
23  * \param dir The ROOT directory
24  * \param v Vector to store the list of keys.
25  */
26 inline void readDir(TDirectory* dir,vector<TString> &v){
27 
28  TIter iter(dir->GetListOfKeys());
29 
30  for (TKey* key; (key = (TKey*) iter.Next()) != NULL; ) {
31 
32  if (key->IsFolder()){
33 
34  dir->cd(key->GetName());
35  TDirectory *subdir = gDirectory;
36  readDir(subdir,v);
37  dir->cd();
38  }else{
39 
40  const TString fullPath(dir->GetPath());
41  TObjArray* t = fullPath.Tokenize(":");
42  v.push_back(TString(((TObjString *)(t->At(1)))->String() + "/" + (TString)key->GetName()));
43  }
44  }
45 }
46 
47 /**
48  * \file
49  *
50  * Program to compare histograms in root files that have same directory structure,\n
51  * and where the histograms have the same names.
52  * The input histograms and the test to be applied for each histogram are specified\n
53  * in an ASCII formatted steering file which is passed by the command line.\n\n
54  *
55  * Each row of the steering file should have multiple columns.\n\n
56  *
57  * Column 1 is the name of the histogram to be compared (including the full path inside the root file)\n
58  * Column 2 is an integer value that indicates the test to be performed. See JTestDictionary()\n
59  * Columns 3..n are reserved for the different parameters of the test.\n\n
60  *
61  * \author rgruiz
62  */
63 int main(int argc, char** argv) {
64 
65  string steeringFile;
66  string file1;
67  string file2;
68  string output;
69  string ascii;
70  bool onlyFailures;
71 
72 
73  try {
74  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");
75  zap['s'] = make_field(steeringFile , "ASCII steering file with list of histograms and tests");
76  zap['a'] = make_field(file1 , "input file 1");
77  zap['b'] = make_field(file2 , "input file 2");
78  zap['o'] = make_field(output , "output file root") = "zebramantis.root";
79  zap['t'] = make_field(ascii , "output file txt" ) = "zebramantis.txt";
80  zap['w'] = make_field(onlyFailures , "write only failed tests" );
81  zap(argc,argv);
82  }
83  catch(const exception &error) {
84  ERROR(error.what() << endl);
85  }
86 
87  TFile* f1 = TFile::Open(file1.c_str());
88  TFile* f2 = TFile::Open(file2.c_str());
89 
90  TFile out(output.c_str(),"recreate");
91  out.cd();
92 
93  ofstream results;
94  results.open (ascii);
95 
96  vector<TString> keys;
97  readDir(f1,keys);
98 
99  std::ifstream infile(steeringFile);
100 
102 
103  TString name;
104  int testID;
105  string rest;
106 
107  int npassed = 0;
108  int nfailed = 0;
109 
110  for (vector<TString>::const_iterator key = keys.begin() ; key != keys.end() ; ++key) {
111 
112  string line;
113  while (getline(infile, line)) {
114  while (line.length()==0)
115  getline(infile, line);
116 
117  istringstream iss(line);
118  iss >> name >> testID;
119 
120  const TRegexp regexp(name);
121 
122  if (((*key).Index(regexp) != -1) && (f2->Get(*key))) {
123 
124  TObject* obj1 = (TObject*)f1->Get(*key);
125  TObject* obj2 = (TObject*)f2->Get(*key);
126 
127  d[testID]->read(iss);
128  d[testID]->test(obj1,obj2);
129 
130  for (vector<JTestResult>::const_iterator r = d[testID]->results.begin() ; r != d[testID]->results.end() ; ++r) {
131  (r->passed ? npassed++ : nfailed++);
132  }
133 
134  string Key = MAKE_STRING(*key);
135  string path = MAKE_STRING(Key.substr (Key.find ('/') + 1 , Key.rfind ('/')));
136 
137  d[testID]->write(cout);
138  d[testID]->write(results, ";", onlyFailures);
139  d[testID]->save (&out, path, onlyFailures);
140  d[testID]->clear();
141  }
142  }
143  infile.clear();
144  infile.seekg(0, ios::beg);
145  }
146 
147  results << WHITE << "PASSED: " << npassed << " " << " FAILED: " << nfailed << " FAILURE FRACTION: " << float (nfailed)/(nfailed+npassed) << endl;
148 
149  putObject(&out, JMeta(argc, argv));
150  JMeta::copy(file1.c_str(), out);
151  JMeta::copy(file2.c_str(), out);
152 
153  results.close();
154  out .Close();
155  return 0;
156 }
Auxiliary class for ROOT I/O of application specific meta data.
Definition: JMeta.hh:70
Utility class to parse command line options.
Definition: JParser.hh:1500
static void copy(const char *const file_name, TFile &out)
Copy meta data.
Definition: JMeta.hh:395
Definition: JRoot.hh:19
data_type r[M+1]
Definition: JPolint.hh:742
#define MAKE_STRING(A)
Make string.
Definition: JPrint.hh:142
do cat driver txt<< EOFevent ev_configure{RC_EVT%< ev_configure.txt > RC_DWRT path
#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
then echo n User name
Definition: JCookie.sh:45
then JMuonMCEvt f $INPUT_FILE o $INTERMEDIATE_FILE d
Definition: JMuonPath.sh:45
Utility class to parse command line options.
void readDir(TDirectory *dir, vector< TString > &v)
Definition: JZebraMantis.cc:26
data_type v[N+1][M+1]
Definition: JPolint.hh:740
bool putObject(TDirectory *dir, const T &object)
Write object to ROOT directory.
int main(int argc, char *argv[])
Definition: Main.cpp:15