Jpp 20.0.0-rc.1
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 "JLang/JEquals.hh"
13#include "JLang/JVectorize.hh"
14
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 * Auxiliary program to print ROOT TTree information.
170 * \author mdejong
171 */
172int main(int argc, char **argv)
173{
174 using namespace std;
175 using namespace JPP;
176
177 JMultipleFileScanner_t inputFile;
178 tree_info ta; // selection
179 tree_info tb; // actual
180 string key; // format
181 int debug;
182
183 JProperties format;
184
185 format.insert(gmake_property(tb.name));
186 format.insert(gmake_property(tb.type));
187 format.insert(gmake_property(tb.number_of_entries));
188 format.insert(gmake_property(tb.number_of_bytes));
189 format.insert(gmake_property(tb.number_of_bytes_zipped));
190 format.insert(gmake_property(tb.compression_factor));
191
192 try {
193
194 JProperties properties;
195
196 properties.insert(gmake_property(ta.name));
197 properties.insert(gmake_property(ta.type));
198
199 JParser<> zap("Auxiliary program to print ROOT TTree information.");
200
201 zap['f'] = make_field(inputFile);
202 zap['@'] = make_field(properties) = JPARSER::initialised();
203 zap['k'] = make_field(key) = "", get_keys(format);
204 zap['d'] = make_field(debug) = 1;
205
206 zap(argc, argv);
207 }
208 catch(const exception &error) {
209 FATAL(error.what() << endl);
210 }
211
212 gErrorIgnoreLevel = kFatal;
213
214 int number_of_errors = 0;
215
216 for (vector<string>::const_iterator file_name = inputFile.begin(); file_name != inputFile.end(); ++file_name) {
217
218 TFile* file = TFile::Open(file_name->c_str());
219
220 if (file == NULL) {
221
222 ++number_of_errors;
223
224 cerr << *file_name << " not opened." << endl;
225
226 continue;
227 }
228
229 if (key == "") {
230 cout << *file_name << endl;
231 }
232
233 TIter iter(file->GetListOfKeys(), kIterBackward);
234
235 for (TKey* _key; (_key = (TKey*) iter.Next()) != NULL; ) {
236
237 TKey* p = dynamic_cast<TKey*>(file->GetListOfKeys()->Before(_key));
238
239 DEBUG("Key: " << _key->GetName() << ' ' << (p == NULL || strcmp(_key->GetName(), p->GetName()) != 0) << endl);
240
241 if (p == NULL || strcmp(_key->GetName(), p->GetName()) != 0) { // select last key
242
243 tb = tree_info(dynamic_cast<TTree*>(_key->ReadObj()));
244
245 DEBUG(tb << endl);
246
247 if (tb.is_valid() && ta == tb) {
248
249 if (key == "")
250 cout << tb << endl;
251 else
252 format.write(cout, key) << ' ';
253 }
254 }
255 }
256
257 file->Close();
258
259 delete file;
260 }
261
262 return (number_of_errors == 0 ? 0 : 1);
263}
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
Scanning of objects from multiple files according a format that follows from the extension of each fi...
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::string getClassname(const std::string &type_name)
Get type name, i.e. part after JEEP::TYPENAME_SEPARATOR.
const char * getNamespace()
Get namespace.
Definition Jpp.hh:37
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.
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.
JWriter & operator<<(JWriter &out, const JDAQChronometer &chronometer)
Write DAQ chronometer to output.
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
Auxiliary base class for list of file names.