Jpp  master_rocky
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  number_of_bytes_zipped(0),
41  compression_factor(0.)
42  {}
43 
44 
45  /**
46  * Constructor.
47  *
48  * \param tree TTree
49  */
50  tree_info(TTree* tree) :
51  tree_info()
52  {
53  if (tree != NULL) {
54 
55  name = tree->GetName();
56  number_of_entries = tree->GetEntries();
57  number_of_bytes = tree->GetTotBytes();
58  number_of_bytes_zipped = tree->GetZipBytes();
59 
60  if (number_of_bytes != 0){
61  compression_factor = double(number_of_bytes) / number_of_bytes_zipped;
62  }
63 
64  TBranch* branch = dynamic_cast<TBranch*>(tree->GetListOfBranches()->At(0)); // KM3NeT policy
65 
66  if (branch != NULL) {
67  type = branch->GetClassName();
68  }
69  }
70  }
71 
72 
73  /**
74  * Check equality.
75  *
76  * \param object tree type
77  * \return true if this tree type equals given tree type; else false
78  */
79  bool equals(const tree_info& object) const
80  {
81  return ((this->name == "" || object.name == "" || this->name == object.name) &&
82  (this->type == "" || object.type == "" || equals(this->type, object.type)));
83  }
84 
85 
86  /**
87  * Check validity.
88  *
89  * \return true is valid; else false
90  */
91  bool is_valid() const
92  {
93  return (name != "" && type != "");
94  }
95 
96 
97  /**
98  * Write TTree information to output stream.
99  *
100  * \param out output stream
101  * \param object TTree information
102  * \return output stream
103  */
104  friend inline std::ostream& operator<<(std::ostream& out, const tree_info& object)
105  {
106  using namespace std;
107 
108  return out << setw(24) << left << object.name << ' '
109  << setw(32) << left << object.type << ' '
110  << setw(10) << right << object.number_of_entries << ' '
111  << setw( 6) << right << (object.number_of_bytes >> 20) << " [MB] "
112  << setw( 6) << right << (object.number_of_bytes_zipped >> 20) << " [MB] "
113  << FIXED(5,2) << (object.compression_factor) << ' ';
114  }
115 
116  std::string name;
117  std::string type;
118  Long64_t number_of_entries;
119  Long64_t number_of_bytes;
120  Long64_t number_of_bytes_zipped;
121  double compression_factor;
122 
123  private:
124  /**
125  * Check equality of data types.
126  *
127  * \param first first data type
128  * \param second second data type
129  * \return true if first data type equals second; else false
130  */
131  static bool equals(const std::string& first, const std::string& second)
132  {
133  using namespace std;
134  using namespace JPP;
135 
136  if (first == "") { return true; }
137  if (second == "") { return true; }
138 
139  if (getNamespace(first) == "" ||
140  getNamespace(second) == "")
141  return getClassname(first) == getClassname(second);
142  else
143  return first == second;
144  }
145  };
146 }
147 
148 
149 
150 /**
151  * \file
152  *
153 
154  * Auxiliary program to print ROOT TTree information.
155  * \author mdejong
156  */
157 int main(int argc, char **argv)
158 {
159  using namespace std;
160  using namespace JPP;
161 
162  vector<string> inputFile;
163  tree_info ta; // selection
164  tree_info tb; // actual
165  string key; // format
166  int debug;
167 
168  JProperties format;
169 
170  format.insert(gmake_property(tb.name));
171  format.insert(gmake_property(tb.type));
172  format.insert(gmake_property(tb.number_of_entries));
173  format.insert(gmake_property(tb.number_of_bytes));
174  format.insert(gmake_property(tb.number_of_bytes_zipped));
175  format.insert(gmake_property(tb.compression_factor));
176 
177  try {
178 
179  JProperties properties;
180 
181  properties.insert(gmake_property(ta.name));
182  properties.insert(gmake_property(ta.type));
183 
184  JParser<> zap("Auxiliary program to print ROOT TTree information.");
185 
186  zap['f'] = make_field(inputFile);
187  zap['@'] = make_field(properties) = JPARSER::initialised();
188  zap['k'] = make_field(key) = "", get_keys(format);
189  zap['d'] = make_field(debug) = 1;
190 
191  zap(argc, argv);
192  }
193  catch(const exception &error) {
194  FATAL(error.what() << endl);
195  }
196 
197  gErrorIgnoreLevel = kFatal;
198 
199  inputFile = getFilenames(inputFile);
200 
201  int number_of_errors = 0;
202 
203  for (vector<string>::const_iterator file_name = inputFile.begin(); file_name != inputFile.end(); ++file_name) {
204 
205  TFile* file = TFile::Open(file_name->c_str());
206 
207  if (file == NULL) {
208 
209  ++number_of_errors;
210 
211  cerr << *file_name << " not opened." << endl;
212 
213  continue;
214  }
215 
216  if (key == "") {
217  cout << *file_name << endl;
218  }
219 
220  TIter iter(file->GetListOfKeys(), kIterBackward);
221 
222  for (TKey* _key; (_key = (TKey*) iter.Next()) != NULL; ) {
223 
224  TKey* p = dynamic_cast<TKey*>(file->GetListOfKeys()->Before(_key));
225 
226  if (p == NULL || strcmp(_key->GetName(), p->GetName()) != 0) { // select last key
227 
228  tb = tree_info(dynamic_cast<TTree*>(_key->ReadObj()));
229 
230  if (tb.is_valid() && ta == tb) {
231 
232  if (key == "")
233  cout << tb << endl;
234  else
235  format.write(cout, key) << ' ';
236  }
237  }
238  }
239 
240  file->Close();
241 
242  delete file;
243  }
244 
245  return (number_of_errors == 0 ? 0 : 1);
246 }
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:2142
int main(int argc, char **argv)
Definition: JPrintTree.cc:157
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:1698
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
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:448
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:68