Jpp test-rotations-new
the software that should make you happy
Loading...
Searching...
No Matches
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
22namespace {
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 TObjArray* ts = tree->GetListOfBranches();
65
66 if (ts != NULL) {
67
68 if (ts->GetEntries() == 1) {
69
70 TBranch* branch = dynamic_cast<TBranch*>(tree->GetListOfBranches()->At(0)); // KM3NeT policy
71
72 if (branch != NULL) {
73 type = branch->GetClassName();
74 }
75
76 } else {
77
78 for (TIter next(ts); TObject* ps = next(); ) {
79 type += (type == "" ? "" : ",") + std::string(ps->GetName()); // flat file policy
80 }
81
82 type = "{" + type + "}";
83 }
84 }
85 }
86 }
87
88
89 /**
90 * Check equality.
91 *
92 * \param object tree type
93 * \return true if this tree type equals given tree type; else false
94 */
95 bool equals(const tree_info& object) const
96 {
97 return ((this->name == "" || object.name == "" || this->name == object.name) &&
98 (this->type == "" || object.type == "" || equals(this->type, object.type)));
99 }
100
101
102 /**
103 * Check validity.
104 *
105 * \return true is valid; else false
106 */
107 bool is_valid() const
108 {
109 return (name != "" && type != "");
110 }
111
112
113 /**
114 * Write TTree information to output stream.
115 *
116 * \param out output stream
117 * \param object TTree information
118 * \return output stream
119 */
120 friend inline std::ostream& operator<<(std::ostream& out, const tree_info& object)
121 {
122 using namespace std;
123
124 return out << setw(24) << left << object.name << ' '
125 << setw(32) << left << object.type << ' '
126 << setw(10) << right << object.number_of_entries << ' '
127 << setw( 6) << right << (object.number_of_bytes >> 20) << " [MB] "
128 << setw( 6) << right << (object.number_of_bytes_zipped >> 20) << " [MB] "
129 << FIXED(5,2) << (object.compression_factor) << ' ';
130 }
131
132 std::string name;
133 std::string type;
134 Long64_t number_of_entries;
135 Long64_t number_of_bytes;
136 Long64_t number_of_bytes_zipped;
137 double compression_factor;
138
139 private:
140 /**
141 * Check equality of data types.
142 *
143 * \param first first data type
144 * \param second second data type
145 * \return true if first data type equals second; else false
146 */
147 static bool equals(const std::string& first, const std::string& second)
148 {
149 using namespace std;
150 using namespace JPP;
151
152 if (first == "") { return true; }
153 if (second == "") { return true; }
154
155 if (getNamespace(first) == "" ||
156 getNamespace(second) == "")
157 return getClassname(first) == getClassname(second);
158 else
159 return first == second;
160 }
161 };
162}
163
164
165
166/**
167 * \file
168 *
169
170 * Auxiliary program to print ROOT TTree information.
171 * \author mdejong
172 */
173int main(int argc, char **argv)
174{
175 using namespace std;
176 using namespace JPP;
177
178 vector<string> inputFile;
179 tree_info ta; // selection
180 tree_info tb; // actual
181 string key; // format
182 int debug;
183
184 JProperties format;
185
186 format.insert(gmake_property(tb.name));
187 format.insert(gmake_property(tb.type));
188 format.insert(gmake_property(tb.number_of_entries));
189 format.insert(gmake_property(tb.number_of_bytes));
190 format.insert(gmake_property(tb.number_of_bytes_zipped));
191 format.insert(gmake_property(tb.compression_factor));
192
193 try {
194
195 JProperties properties;
196
197 properties.insert(gmake_property(ta.name));
198 properties.insert(gmake_property(ta.type));
199
200 JParser<> zap("Auxiliary program to print ROOT TTree information.");
201
202 zap['f'] = make_field(inputFile);
203 zap['@'] = make_field(properties) = JPARSER::initialised();
204 zap['k'] = make_field(key) = "", get_keys(format);
205 zap['d'] = make_field(debug) = 1;
206
207 zap(argc, argv);
208 }
209 catch(const exception &error) {
210 FATAL(error.what() << endl);
211 }
212
213 gErrorIgnoreLevel = kFatal;
214
215 inputFile = getFilenames(inputFile);
216
217 int number_of_errors = 0;
218
219 for (vector<string>::const_iterator file_name = inputFile.begin(); file_name != inputFile.end(); ++file_name) {
220
221 TFile* file = TFile::Open(file_name->c_str());
222
223 if (file == NULL) {
224
225 ++number_of_errors;
226
227 cerr << *file_name << " not opened." << endl;
228
229 continue;
230 }
231
232 if (key == "") {
233 cout << *file_name << endl;
234 }
235
236 TIter iter(file->GetListOfKeys(), kIterBackward);
237
238 for (TKey* _key; (_key = (TKey*) iter.Next()) != NULL; ) {
239
240 TKey* p = dynamic_cast<TKey*>(file->GetListOfKeys()->Before(_key));
241
242 DEBUG("Key: " << _key->GetName() << ' ' << (p == NULL || strcmp(_key->GetName(), p->GetName()) != 0) << endl);
243
244 if (p == NULL || strcmp(_key->GetName(), p->GetName()) != 0) { // select last key
245
246 tb = tree_info(dynamic_cast<TTree*>(_key->ReadObj()));
247
248 DEBUG(tb << endl);
249
250 if (tb.is_valid() && ta == tb) {
251
252 if (key == "")
253 cout << tb << endl;
254 else
255 format.write(cout, key) << ' ';
256 }
257 }
258 }
259
260 file->Close();
261
262 delete file;
263 }
264
265 return (number_of_errors == 0 ? 0 : 1);
266}
File list.
General purpose messaging.
#define DEBUG(A)
Message macros.
Definition JMessage.hh:62
#define FATAL(A)
Definition JMessage.hh:67
int debug
debug level
Definition JSirene.cc:72
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)
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.
std::ostream & write(std::ostream &out) const
Write the current parameter values.
Utility class to parse command line options.
Definition JParser.hh:1698
std::ostream & operator<<(std::ostream &stream, const CLBCommonHeader &header)
bool equals(const JFirst_t &first, const JSecond_t &second, const double precision=std::numeric_limits< double >::min())
Check equality.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
bool is_valid(const json &js)
Check validity of JSon data.
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