Jpp  debug
the software that should make you happy
JPrintTree.cc
Go to the documentation of this file.
1 
2 #include <string>
3 #include <iostream>
4 #include <iomanip>
5 
6 #include "TError.h"
7 #include "TROOT.h"
8 #include "TFile.h"
9 #include "TKey.h"
10 #include "TTree.h"
11 
12 #include "JSystem/JGlob.hh"
13 
14 #include "JLang/JEquals.hh"
15 #include "JLang/JVectorize.hh"
16 
17 #include "Jeep/JeepToolkit.hh"
18 #include "Jeep/JProperties.hh"
19 #include "Jeep/JParser.hh"
20 #include "Jeep/JMessage.hh"
21 
22 namespace {
23 
24  using JLANG::JEquals;
25 
26  /**
27  * Auxiliary class for TTree information.
28  */
29  struct tree_info :
30  public JEquals<tree_info>
31  {
32  /**
33  * Default constructor.
34  */
35  tree_info() :
36  name(),
37  type(),
38  number_of_entries(0),
39  number_of_bytes(0)
40  {}
41 
42 
43  /**
44  * Constructor.
45  *
46  * \param tree TTree
47  */
48  tree_info(TTree* tree) :
49  tree_info()
50  {
51  if (tree != NULL) {
52 
53  name = tree->GetName();
54  number_of_entries = tree->GetEntries();
55  number_of_bytes = tree->GetTotBytes();
56 
57  TBranch* branch = dynamic_cast<TBranch*>(tree->GetListOfBranches()->At(0)); // KM3NeT policy
58 
59  if (branch != NULL) {
60  type = branch->GetClassName();
61  }
62  }
63  }
64 
65 
66  /**
67  * Check equality.
68  *
69  * \param object tree type
70  * \return true if this tree type equals given tree type; else false
71  */
72  bool equals(const tree_info& object) const
73  {
74  return ((this->name == "" || object.name == "" || this->name == object.name) &&
75  (this->type == "" || object.type == "" || equals(this->type, object.type)));
76  }
77 
78 
79  /**
80  * Check validity.
81  *
82  * \return true is valid; else false
83  */
84  bool is_valid() const
85  {
86  return (name != "" && type != "");
87  }
88 
89 
90  /**
91  * Write TTree information to output stream.
92  *
93  * \param out output stream
94  * \param object TTree information
95  * \return output stream
96  */
97  friend inline std::ostream& operator<<(std::ostream& out, const tree_info& object)
98  {
99  using namespace std;
100 
101  return out << setw(24) << left << object.name << ' '
102  << setw(32) << left << object.type << ' '
103  << setw(10) << right << object.number_of_entries << ' '
104  << setw( 6) << right << (object.number_of_bytes >> 20) << " [MB]";
105  }
106 
107  std::string name;
108  std::string type;
109  Long64_t number_of_entries;
110  Long64_t number_of_bytes;
111 
112  private:
113  /**
114  * Check equality of data types.
115  *
116  * \param first first data type
117  * \param second second data type
118  * \return true if first data type equals second; else false
119  */
120  static bool equals(const std::string& first, const std::string& second)
121  {
122  using namespace std;
123  using namespace JPP;
124 
125  if (first == "") { return true; }
126  if (second == "") { return true; }
127 
128  if (getNamespace(first) == "" ||
129  getNamespace(second) == "")
130  return getClassname(first) == getClassname(second);
131  else
132  return first == second;
133  }
134  };
135 }
136 
137 
138 
139 /**
140  * \file
141  *
142 
143  * Auxiliary program to print ROOT TTree information.
144  * \author mdejong
145  */
146 int main(int argc, char **argv)
147 {
148  using namespace std;
149  using namespace JPP;
150 
151  vector<string> inputFile;
152  tree_info ta; // selection
153  tree_info tb; // actual
154  string key; // format
155  int debug;
156 
157  JProperties format;
158 
159  format.insert(gmake_property(tb.name));
160  format.insert(gmake_property(tb.type));
161  format.insert(gmake_property(tb.number_of_entries));
162  format.insert(gmake_property(tb.number_of_bytes));
163 
164  try {
165 
166  JProperties properties;
167 
168  properties.insert(gmake_property(ta.name));
169  properties.insert(gmake_property(ta.type));
170 
171  JParser<> zap("Auxiliary program to print ROOT TTree information.");
172 
173  zap['f'] = make_field(inputFile);
174  zap['@'] = make_field(properties) = JPARSER::initialised();
175  zap['k'] = make_field(key) = "", get_keys(format);
176  zap['d'] = make_field(debug) = 1;
177 
178  zap(argc, argv);
179  }
180  catch(const exception &error) {
181  FATAL(error.what() << endl);
182  }
183 
184  gErrorIgnoreLevel = kFatal;
185 
186  inputFile = getFilenames(inputFile);
187 
188  int number_of_errors = 0;
189 
190  for (vector<string>::const_iterator file_name = inputFile.begin(); file_name != inputFile.end(); ++file_name) {
191 
192  TFile* file = TFile::Open(file_name->c_str());
193 
194  if (file == NULL) {
195 
196  ++number_of_errors;
197 
198  cerr << *file_name << " not opened." << endl;
199 
200  continue;
201  }
202 
203  if (key == "") {
204  cout << *file_name << endl;
205  }
206 
207  TIter iter(file->GetListOfKeys(), kIterBackward);
208 
209  for (TKey* _key; (_key = (TKey*) iter.Next()) != NULL; ) {
210 
211  TKey* p = dynamic_cast<TKey*>(file->GetListOfKeys()->Before(_key));
212 
213  if (p == NULL || strcmp(_key->GetName(), p->GetName()) != 0) { // select last key
214 
215  tb = tree_info(dynamic_cast<TTree*>(_key->ReadObj()));
216 
217  if (tb.is_valid() && ta == tb) {
218 
219  if (key == "")
220  cout << tb << endl;
221  else
222  format.write(cout, key) << ' ';
223  }
224  }
225  }
226 
227  file->Close();
228 
229  delete file;
230  }
231 
232  return (number_of_errors == 0 ? 0 : 1);
233 }
File list.
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:67
int debug
debug level
Definition: JSirene.cc:69
Utility class to parse command line options.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:2158
int main(int argc, char **argv)
Definition: JPrintTree.cc:146
Utility class to parse parameter values.
#define gmake_property(A)
macros to convert (template) parameter to JPropertiesElement object
Auxiliary methods to convert data members or return values of member methods of a set of objects to a...
Auxiliary methods for handling file names, type names and environment.
Utility class to parse parameter values.
Definition: JProperties.hh:501
std::ostream & write(std::ostream &out) const
Write the current parameter values.
Definition: JProperties.hh:847
Utility class to parse command line options.
Definition: JParser.hh:1714
std::ostream & operator<<(std::ostream &stream, const CLBCommonHeader &header)
std::string getClassname(const std::string &type_name)
Get type name, i.e. part after JEEP::TYPENAME_SEPARATOR.
Definition: JeepToolkit.hh:289
std::string getNamespace(const std::string &type_name)
Get name space, i.e. part before JEEP::TYPENAME_SEPARATOR.
Definition: JeepToolkit.hh:270
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
bool equals(const JFirst_t &first, const JSecond_t &second, const double precision=std::numeric_limits< double >::min())
Check equality.
Definition: JMathToolkit.hh:87
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
bool is_valid(const json &js)
Check validity of JSon data.
static JGlob getFilenames
Function object to get list of files for given pattern.
Definition: JGlob.hh:123
Definition: JSTDTypes.hh:14
Template definition of auxiliary base class for comparison of data structures.
Definition: JEquals.hh:84
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:84