1 | #include <boost/regex.hpp>
|
---|
2 | #include <boost/filesystem.hpp>
|
---|
3 | #include <boost/algorithm/string/join.hpp>
|
---|
4 |
|
---|
5 | #include "tools.h"
|
---|
6 | #include "Time.h"
|
---|
7 | #include "Splitting.h"
|
---|
8 |
|
---|
9 | #include <TROOT.h>
|
---|
10 | #include <TSystem.h>
|
---|
11 | #include <TChain.h>
|
---|
12 | #include <TLeaf.h>
|
---|
13 | #include <TError.h>
|
---|
14 | #include <TTreeFormula.h>
|
---|
15 | #include <TTreeFormulaManager.h>
|
---|
16 |
|
---|
17 | #include "FileEntry.h"
|
---|
18 |
|
---|
19 | using namespace std;
|
---|
20 | namespace fs = boost::filesystem;
|
---|
21 |
|
---|
22 | // ------------------------------------------------------------------------
|
---|
23 |
|
---|
24 | void SetupConfiguration(Configuration &conf)
|
---|
25 | {
|
---|
26 | po::options_description control("Root to SQL");
|
---|
27 | control.add_options()
|
---|
28 | ("file", vars<string>()->required(),"The root files to read from")
|
---|
29 | ("out,o", var<string>()->required(), "Output file name")
|
---|
30 | ("force,f", po_switch(), "Force overwrite if output file already exists.")
|
---|
31 | ("append,a", po_switch(), "Append to an existing file (not check for the format is done!)")
|
---|
32 | ("tree,t", var<string>("Events"), "Name of the root tree to convert")
|
---|
33 | ("ignore", vars<string>(), "Ignore the given leaf, if the given regular expression matches")
|
---|
34 | ("alias.*", var<string>(), "Define an alias")
|
---|
35 | ("auto-alias", vars<Configuration::Map>(),"Regular expression to define aliases from the branch names automatically")
|
---|
36 | ("header", var<uint16_t>(uint16_t(0)),"Type of header line (0: preceeding #, 1: without preceeding #, 2: none)")
|
---|
37 | ("add.*", var<string>(), "Define an additional column")
|
---|
38 | ("selector,s", var<string>("1"), "Define a selector for the columns (colums where this evaluates to a value <=0 are discarded)")
|
---|
39 | ("skip", po_switch(), "Discards all default leaves and writes only the columns defined by --add.*")
|
---|
40 | ("first", var<int64_t>(int64_t(0)), "First event to start with (default: 0), mainly for test purpose")
|
---|
41 | ("max", var<int64_t>(int64_t(0)), "Maximum number of events to process (0: all), mainly for test purpose")
|
---|
42 | //("const.*", var<string>(), "Insert a constant number into the given column (--const.mycolumn=5). A special case is `/.../.../`")
|
---|
43 | ("dry-run", po_switch(), "Do not create or manipulate any output file")
|
---|
44 | ("no-indirection", po_switch(), "Column of alias of alias need all auto-aliases to be enables. This is slow. If you know that you do not have any, use this for faster processing.")
|
---|
45 | ;
|
---|
46 |
|
---|
47 | po::options_description debug("Debug options");
|
---|
48 | debug.add_options()
|
---|
49 | ("print-ls", po_switch(), "Calls TFile::ls()")
|
---|
50 | ("print-branches", po_switch(), "Print the branches found in the tree")
|
---|
51 | ("print-leaves", po_switch(), "Print the leaves found in the tree (this is what is processed)")
|
---|
52 | ("verbose,v", var<uint16_t>(1), "Verbosity (0: quiet, 1: default, 2: more, 3, ...)")
|
---|
53 | ;
|
---|
54 |
|
---|
55 | po::positional_options_description p;
|
---|
56 | p.add("file", -1); // All positional options
|
---|
57 |
|
---|
58 | conf.AddOptions(control);
|
---|
59 | conf.AddOptions(Tools::Splitting::options());
|
---|
60 | conf.AddOptions(debug);
|
---|
61 | conf.SetArgumentPositions(p);
|
---|
62 | }
|
---|
63 |
|
---|
64 | void PrintUsage()
|
---|
65 | {
|
---|
66 | cout <<
|
---|
67 | "root2csv - Reads data from a root tree and writes a csv file\n"
|
---|
68 | "\n"
|
---|
69 | "For convenience, this documentation uses the extended version of the options, "
|
---|
70 | "refer to the output below to get the abbreviations.\n"
|
---|
71 | "\n"
|
---|
72 | "Similar functionaliy is also provided by root2sql. In addition to root2sql, "
|
---|
73 | "this tool is more flexible in the slection of columns and adds the possibility "
|
---|
74 | "to use formulas (implemented through TTreeFormula) to calculate values for "
|
---|
75 | "additional columns. Note that root can even write complex data like a TH1F "
|
---|
76 | "into a file. Here, only numeric columns are supported.\n"
|
---|
77 | "\n"
|
---|
78 | "Input files are given as positional arguments or with --file. "
|
---|
79 | "As files are read by adding them through TChain::Add, wildcards are "
|
---|
80 | "supported in file names. Note that on the command lines, file names "
|
---|
81 | "with wildcards have to be escaped in quotation marks if the wildcards "
|
---|
82 | "should be evaluated by the program and not by the shell. The output base "
|
---|
83 | "name of the output file(s) is given with --out.\n"
|
---|
84 | "\n"
|
---|
85 | "The format of the first line on the file is defined with the --header option:\n"
|
---|
86 | " 0: '# Col1 Col2 Col3 ...'\n"
|
---|
87 | " 1: 'Col1 Col2 Col3 ...'\n"
|
---|
88 | " 2: first data row\n"
|
---|
89 | "\n"
|
---|
90 | "As default, existing files are not overwritten. To force overwriting use "
|
---|
91 | "--force. To append data to existing files use --append. Note that no "
|
---|
92 | "check is done if this created valid and reasonable files.\n"
|
---|
93 | "\n"
|
---|
94 | "Each root tree has branches and leaves (the basic data types). These leaves can "
|
---|
95 | "be read independently of the classes which were used to write the root file. "
|
---|
96 | "The default tree to read from is 'Events' but the name can be overwritten "
|
---|
97 | "using --tree. The default table name to fill the data into is identical to "
|
---|
98 | "the tree name. It can be overwritten using --table.\n"
|
---|
99 | "\n"
|
---|
100 | "To get a list of the contents (keys and trees) of a root file, you can use --print-ls. "
|
---|
101 | "The name of each column to which data is filled from a leave is obtained from "
|
---|
102 | "the leaves' names. The leave names can be checked using --print-leaves. "
|
---|
103 | "A --print-branches exists for convenience to print only the high-level branches.\n"
|
---|
104 | "\n"
|
---|
105 | "Assuming a leaf with name MHillas.fWidth and a leaf with MHillas.fLength, "
|
---|
106 | "a new column can be added with name Area by\n"
|
---|
107 | " --add.Area='TMath::TwoPi()*MHillas.fWidth*MHillas.fLength'\n"
|
---|
108 | "\n"
|
---|
109 | "To simplify expression, root allows to define aliases, for example\n"
|
---|
110 | " --alias.Width='MHillas.fWidth'\n"
|
---|
111 | " --alias.Length='MHillas.fLength'\n"
|
---|
112 | "\n"
|
---|
113 | "This can then be used to simplyfy the above expression as\n"
|
---|
114 | " --add.Area='TMath::TwoPi()*Width*Length'\n"
|
---|
115 | "\n"
|
---|
116 | "Sometimes leaf names might be quite unconvenient like MTime.fTime.fMilliSec or "
|
---|
117 | "just MHillas.fWidth. To allow to simplify column names, regular expressions "
|
---|
118 | "(using boost's regex) can be defined to change the names. Note that these regular "
|
---|
119 | "expressions are applied one by one on each leaf's name. A valid expression could "
|
---|
120 | "be:\n"
|
---|
121 | " --auto-alias=MHillas\\.f/\n"
|
---|
122 | "which would remove all occurances of 'MHillas.f'. This option can be used more than "
|
---|
123 | "once. They are applied in sequence. A single match does not stop the sequence. "
|
---|
124 | "In addition to replacing the column names accordingly, a alias is created "
|
---|
125 | "automatically allowing to access the columns in a formula with the new name.\n"
|
---|
126 | "\n"
|
---|
127 | "Note that in the default configuration, all data which is auto-alias'ed is read "
|
---|
128 | "from the file. This can become quite slow if there is a lot. An alternative is "
|
---|
129 | "to switch on no-indirections with --no-indirection. The ensures that only "
|
---|
130 | "references leaves are read, which can signiifcantly accelerate reading. "
|
---|
131 | "The disadvantage is that adding a column which reference an alias which "
|
---|
132 | "itself references an alias will fail.\n"
|
---|
133 | "\n"
|
---|
134 | "Sometimes it might also be convenient to skip a leaf, i.e. not writing the "
|
---|
135 | "coresponding column in the output file. This can be done with "
|
---|
136 | "the --ignore resource. If the given regular expresion yields a match, the "
|
---|
137 | "leaf will be ignored. An automatic alias would still be created and the "
|
---|
138 | "leaf could still be used in a formula. Example\n"
|
---|
139 | " --ignore=ThetaSq\\..*\n"
|
---|
140 | "will skip all leaved which start with 'ThetaSq.'. This directive can be given "
|
---|
141 | "more than once. The so defined ignore list is applied entry-wise, first to the "
|
---|
142 | "raw leaf names, then to the aliased names.\n"
|
---|
143 | "\n"
|
---|
144 | "To select only certain extries from the file, a selector (cut) can be defined "
|
---|
145 | "in the same style as the --add directives, for exmple:\n"
|
---|
146 | " --selector='MHillas.fLength*Width<0'\n"
|
---|
147 | "Note that the selctor is not evaluated to a boolean expression (==0 or !=0) "
|
---|
148 | "but all positive none zero values are considered 'true' (select the entry) "
|
---|
149 | "and all negative values are considered 'fales' (discard the entry).\n"
|
---|
150 | "\n"
|
---|
151 | << Tools::Splitting::usage() <<
|
---|
152 | "\n"
|
---|
153 | "In case of success, 0 is returned, a value>0 otherwise.\n"
|
---|
154 | "\n"
|
---|
155 | "Usage: root2csv input1.root [input2.root ...] -o output.csv [-t tree] [-u] [-f] [-n] [-vN] [-cN]\n"
|
---|
156 | "\n"
|
---|
157 | ;
|
---|
158 | cout << endl;
|
---|
159 | }
|
---|
160 |
|
---|
161 | void ErrorHandlerAll(Int_t level, Bool_t abort, const char *location, const char *msg)
|
---|
162 | {
|
---|
163 | if (string(msg).substr(0,24)=="no dictionary for class ")
|
---|
164 | return;
|
---|
165 | if (string(msg).substr(0,15)=="unknown branch ")
|
---|
166 | return;
|
---|
167 |
|
---|
168 | DefaultErrorHandler(level, abort, location, msg);
|
---|
169 | }
|
---|
170 |
|
---|
171 | // --------------------------- Write Header --------------------------------
|
---|
172 | void WriteHeader(ostream &out, const vector<FileEntry::Container> &vec, const vector<TTreeFormula*> &form, bool skip, uint16_t header)
|
---|
173 | {
|
---|
174 | if (header>1)
|
---|
175 | return;
|
---|
176 | if (header==0)
|
---|
177 | out << "# ";
|
---|
178 |
|
---|
179 | vector<string> join;
|
---|
180 |
|
---|
181 | if (!skip)
|
---|
182 | {
|
---|
183 | for (auto v=vec.cbegin(); v!=vec.cend(); v++)
|
---|
184 | {
|
---|
185 | const size_t N = v->num;
|
---|
186 | for (size_t i=0; i<N; i++)
|
---|
187 | {
|
---|
188 | string name = v->column;
|
---|
189 | if (N!=1)
|
---|
190 | name += "["+to_string(i)+"]";
|
---|
191 | join.emplace_back(name);
|
---|
192 | }
|
---|
193 | }
|
---|
194 | }
|
---|
195 |
|
---|
196 | for (auto v=form.cbegin(); v!=form.cend(); v++)
|
---|
197 | join.emplace_back((*v)->GetName());
|
---|
198 |
|
---|
199 | out << boost::join(join, " ") << "\n";
|
---|
200 | }
|
---|
201 |
|
---|
202 | int CheckFile(TString &path, bool force, int verbose)
|
---|
203 | {
|
---|
204 | gSystem->ExpandPathName(path);
|
---|
205 |
|
---|
206 | FileStat_t stat;
|
---|
207 | const Int_t exist = !gSystem->GetPathInfo(path, stat);
|
---|
208 | const Bool_t _write = !gSystem->AccessPathName(path, kWritePermission) && R_ISREG(stat.fMode);
|
---|
209 |
|
---|
210 | if (exist)
|
---|
211 | {
|
---|
212 | if (!_write)
|
---|
213 | {
|
---|
214 | cerr << "File '" << path << "' is not writable." << endl;
|
---|
215 | return 2;
|
---|
216 | }
|
---|
217 |
|
---|
218 | if (!force)
|
---|
219 | {
|
---|
220 | cerr << "File '" << path << "' already exists." << endl;
|
---|
221 | return 3;
|
---|
222 | }
|
---|
223 | else
|
---|
224 | {
|
---|
225 | if (verbose>0)
|
---|
226 | cerr << "File '" << path << "' will be overwritten." << endl;
|
---|
227 | }
|
---|
228 | }
|
---|
229 | return exist ? 0 : -1;
|
---|
230 | }
|
---|
231 |
|
---|
232 | void GetLeaves(set<string> &list, const TTreeFormula &f)
|
---|
233 | {
|
---|
234 | int i=0;
|
---|
235 | while (1)
|
---|
236 | {
|
---|
237 | const auto l = f.GetLeaf(i++);
|
---|
238 | if (!l)
|
---|
239 | return;
|
---|
240 |
|
---|
241 | list.emplace(l->GetName());
|
---|
242 | }
|
---|
243 | }
|
---|
244 |
|
---|
245 | int main(int argc, const char* argv[])
|
---|
246 | {
|
---|
247 | Time start;
|
---|
248 |
|
---|
249 | gROOT->SetBatch();
|
---|
250 | SetErrorHandler(ErrorHandlerAll);
|
---|
251 |
|
---|
252 | Configuration conf(argv[0]);
|
---|
253 | conf.SetPrintUsage(PrintUsage);
|
---|
254 | SetupConfiguration(conf);
|
---|
255 |
|
---|
256 | if (!conf.DoParse(argc, argv))
|
---|
257 | return 127;
|
---|
258 |
|
---|
259 | // ----------------------------- Evaluate options --------------------------
|
---|
260 | const vector<string> files = conf.Vec<string>("file");
|
---|
261 | const string out = conf.Get<string>("out");
|
---|
262 | const string tree = conf.Get<string>("tree");
|
---|
263 |
|
---|
264 | const bool force = conf.Get<bool>("force");
|
---|
265 | const bool append = conf.Get<bool>("append");
|
---|
266 | const bool dryrun = conf.Get<bool>("dry-run");
|
---|
267 | const bool skip = conf.Get<bool>("skip");
|
---|
268 | const bool noindirect = conf.Get<bool>("no-indirection");
|
---|
269 |
|
---|
270 | const uint16_t verbose = conf.Get<uint16_t>("verbose");
|
---|
271 | const int64_t first = conf.Get<int64_t>("first");
|
---|
272 | const int64_t max = conf.Get<int64_t>("max");
|
---|
273 | const uint16_t header = conf.Get<uint16_t>("header");
|
---|
274 |
|
---|
275 | const bool print_ls = conf.Get<bool>("print-ls");
|
---|
276 | const bool print_branches = conf.Get<bool>("print-branches");
|
---|
277 | const bool print_leaves = conf.Get<bool>("print-leaves");
|
---|
278 |
|
---|
279 | const auto _ignore = conf.Vec<string>("ignore");
|
---|
280 | const auto autoalias = conf.Vec<Configuration::Map>("auto-alias");
|
---|
281 |
|
---|
282 | if (max && first>=max)
|
---|
283 | cerr << "WARNING: Resource `first` (" << first << ") exceeds `max` (" << max << ")" << endl;
|
---|
284 |
|
---|
285 | // -------------------------------------------------------------------------
|
---|
286 |
|
---|
287 | /*const*/ Tools::Splitting split(conf);
|
---|
288 |
|
---|
289 | if (verbose>0)
|
---|
290 | {
|
---|
291 | cout << "\n-------------------------- Evaluating input ------------------------\n";
|
---|
292 | cout << "Start Time: " << Time::sql << Time(Time::local) << endl;
|
---|
293 | }
|
---|
294 |
|
---|
295 | if (verbose>0)
|
---|
296 | cout << "Processing Tree: " << tree << endl;
|
---|
297 |
|
---|
298 | TChain c(tree.c_str());
|
---|
299 |
|
---|
300 | uint64_t cnt = 0;
|
---|
301 | for (const auto &file : files)
|
---|
302 | {
|
---|
303 | const auto add = c.Add(file.c_str(), 0);
|
---|
304 | if (verbose>0)
|
---|
305 | cout << file << ": " << add << " file(s) added." << endl;
|
---|
306 | cnt += add;
|
---|
307 | }
|
---|
308 |
|
---|
309 | if (cnt==0)
|
---|
310 | {
|
---|
311 | cerr << "No files found." << endl;
|
---|
312 | return 1;
|
---|
313 | }
|
---|
314 |
|
---|
315 | if (verbose>0)
|
---|
316 | cout << cnt << " file(s) found." << endl;
|
---|
317 |
|
---|
318 | if (print_ls)
|
---|
319 | {
|
---|
320 | cout << '\n';
|
---|
321 | c.ls();
|
---|
322 | cout << '\n';
|
---|
323 | }
|
---|
324 |
|
---|
325 | c.SetMakeClass(1);
|
---|
326 |
|
---|
327 | TObjArray *branches = c.GetListOfBranches();
|
---|
328 | TObjArray *leaves = c.GetListOfLeaves();
|
---|
329 |
|
---|
330 | if (print_branches)
|
---|
331 | {
|
---|
332 | cout << '\n';
|
---|
333 | branches->Print();
|
---|
334 | }
|
---|
335 |
|
---|
336 | const auto entries = c.GetEntriesFast();
|
---|
337 |
|
---|
338 | if (verbose>0)
|
---|
339 | cout << branches->GetEntries() << " branches found." << endl;
|
---|
340 |
|
---|
341 | if (print_leaves)
|
---|
342 | {
|
---|
343 | cout << '\n';
|
---|
344 | leaves->Print();
|
---|
345 | }
|
---|
346 | if (verbose>0)
|
---|
347 | {
|
---|
348 | cout << leaves->GetEntries() << " leaves found." << endl;
|
---|
349 | cout << entries << " events found." << endl;
|
---|
350 | }
|
---|
351 |
|
---|
352 | // ----------------------------------------------------------------------
|
---|
353 |
|
---|
354 | if (verbose>0)
|
---|
355 | cout << "\n-------------------------- Evaluating output -----------------------" << endl;
|
---|
356 |
|
---|
357 | vector<FileEntry::Container> vec;
|
---|
358 |
|
---|
359 | /*
|
---|
360 | const auto fixed = conf.GetWildcardOptions("const.*");
|
---|
361 |
|
---|
362 | string where;
|
---|
363 | vector<string> vindex;
|
---|
364 | for (auto it=fixed.cbegin(); it!=fixed.cend(); it++)
|
---|
365 | {
|
---|
366 | const string name = it->substr(6);
|
---|
367 | string val = conf.Get<string>(*it);
|
---|
368 |
|
---|
369 | boost::smatch match;
|
---|
370 | if (boost::regex_match(val, match, boost::regex("\\/(.+)(?<!\\\\)\\/(.*)(?<!\\\\)\\/")))
|
---|
371 | {
|
---|
372 | const string reg = match[1];
|
---|
373 | const string fmt = match[2];
|
---|
374 |
|
---|
375 | val = boost::regex_replace(file, boost::regex(reg), fmt.empty()?"$0":fmt,
|
---|
376 | boost::regex_constants::format_default|boost::regex_constants::format_no_copy);
|
---|
377 |
|
---|
378 | if (verbose>0)
|
---|
379 | {
|
---|
380 | cout << "Regular expression detected for constant column `" << *it << "`\n";
|
---|
381 | cout << "Filename converted with /" << reg << "/ to /" << fmt << "/\n";
|
---|
382 | cout << "Filename: " << file << '\n';
|
---|
383 | cout << "Result: " << val << endl;
|
---|
384 | }
|
---|
385 | }
|
---|
386 |
|
---|
387 | if (verbose>2)
|
---|
388 | cout << "\n" << val << " [-const-]";
|
---|
389 | if (verbose>1)
|
---|
390 | cout << " (" << name << ")";
|
---|
391 |
|
---|
392 | string sqltype = "INT UNSIGNED";
|
---|
393 |
|
---|
394 | for (auto m=sqltypes.cbegin(); m!=sqltypes.cend(); m++)
|
---|
395 | if (m->first==name)
|
---|
396 | sqltype = m->second;
|
---|
397 |
|
---|
398 | if (!vec.empty())
|
---|
399 | query += ",\n";
|
---|
400 | query += " `"+name+"` "+sqltype+" NOT NULL COMMENT '--user--'";
|
---|
401 |
|
---|
402 | vec.emplace_back(name, val);
|
---|
403 | where += " AND `"+name+"`="+val;
|
---|
404 | vindex.emplace_back(name);
|
---|
405 | }
|
---|
406 | */
|
---|
407 |
|
---|
408 | set<string> leaflist;
|
---|
409 |
|
---|
410 | // ------------------------- Setup all branches in tree -------------------
|
---|
411 |
|
---|
412 | TIter Next(leaves);
|
---|
413 | TObject *o = 0;
|
---|
414 | while ((o=Next()))
|
---|
415 | {
|
---|
416 | TLeaf *L = dynamic_cast<TLeaf*>(o);//c.GetLeaf(o->GetName());
|
---|
417 | if (!L)
|
---|
418 | continue;
|
---|
419 |
|
---|
420 | string name = o->GetName();
|
---|
421 | if (verbose>2)
|
---|
422 | cout << '\n' << L->GetTitle() << " {" << L->GetTypeName() << "}";
|
---|
423 |
|
---|
424 | const string tn = L->GetTypeName();
|
---|
425 |
|
---|
426 | // Check if this is a basic type, otherwise skip
|
---|
427 | const auto it = FileEntry::LUT.root(tn);
|
---|
428 | if (it==FileEntry::LUT.cend())
|
---|
429 | {
|
---|
430 | if (verbose>2)
|
---|
431 | cout << " (-n/a-)";
|
---|
432 | continue;
|
---|
433 | }
|
---|
434 |
|
---|
435 | // Check if this is an array, otherwise skip
|
---|
436 | if (L->GetLenStatic()!=L->GetLen())
|
---|
437 | {
|
---|
438 | if (verbose>2)
|
---|
439 | cout << " (-skipped-)";
|
---|
440 | continue;
|
---|
441 | }
|
---|
442 |
|
---|
443 | // Check for renaming via auto-alias
|
---|
444 | for (auto m=autoalias.cbegin(); m!=autoalias.cend(); m++)
|
---|
445 | name = boost::regex_replace(name, boost::regex(m->first), m->second);
|
---|
446 |
|
---|
447 | // if renaming has changed name, define alias
|
---|
448 | if (name!=o->GetName())
|
---|
449 | {
|
---|
450 | if (!c.SetAlias(name.c_str(), o->GetName()))
|
---|
451 | {
|
---|
452 | cerr << "\nERROR - Alias could not be established!\n";
|
---|
453 | cerr << " " << name << " = " << o->GetName() << endl;
|
---|
454 | return 1;
|
---|
455 | }
|
---|
456 | if (verbose==1 || verbose==2)
|
---|
457 | cout << "\nAuto-alias: " << name << " = " << o->GetName();
|
---|
458 | if (verbose>2)
|
---|
459 | cout << " <alias:" << name << ">";
|
---|
460 |
|
---|
461 | if (!noindirect)
|
---|
462 | leaflist.emplace(o->GetTitle());
|
---|
463 | }
|
---|
464 |
|
---|
465 | // Check whether the un-aliased column is in the ignore list
|
---|
466 | bool found = skip;
|
---|
467 | for (auto b=_ignore.cbegin(); b!=_ignore.cend(); b++)
|
---|
468 | {
|
---|
469 | if (boost::regex_match(o->GetName(), boost::regex(*b)))
|
---|
470 | {
|
---|
471 | found = true;
|
---|
472 | if (verbose>2)
|
---|
473 | cout << " (-ignored-)";
|
---|
474 | break;
|
---|
475 | }
|
---|
476 | }
|
---|
477 |
|
---|
478 | // Check whether the aliased column is in the ignore list
|
---|
479 | for (auto b=_ignore.cbegin(); b!=_ignore.cend(); b++)
|
---|
480 | {
|
---|
481 | if (boost::regex_match(name.c_str(), boost::regex(*b)))
|
---|
482 | {
|
---|
483 | found = true;
|
---|
484 | if (verbose>2)
|
---|
485 | cout << " (-ignored-)";
|
---|
486 | break;
|
---|
487 | }
|
---|
488 | }
|
---|
489 |
|
---|
490 | if (!found)
|
---|
491 | leaflist.emplace(o->GetTitle());
|
---|
492 |
|
---|
493 | // As it is easier, we setup addresses for all branches
|
---|
494 | // also for the ones not requested. They might later be
|
---|
495 | // requested by a formula.
|
---|
496 | vec.emplace_back(o->GetTitle(), name, it->type, L->GetLenStatic());
|
---|
497 | c.SetBranchAddress(o->GetTitle(), vec.back().ptr);
|
---|
498 | }
|
---|
499 |
|
---|
500 | if (verbose>0)
|
---|
501 | {
|
---|
502 | cout << '\n';
|
---|
503 | if (skip)
|
---|
504 | cout << "Default columns skipped: ";
|
---|
505 | cout << vec.size() << " default leaf/leaves setup for reading." << endl;
|
---|
506 | }
|
---|
507 |
|
---|
508 | // ------------------- Configure manual aliases ----------------------------
|
---|
509 |
|
---|
510 | const auto valiases = conf.GetWildcardOptions("alias.*");
|
---|
511 | if (verbose>0 && valiases.size()>0)
|
---|
512 | cout << '\n';
|
---|
513 | for (auto it=valiases.cbegin(); it!=valiases.cend(); it++)
|
---|
514 | {
|
---|
515 | const string name = it->substr(6);
|
---|
516 | const string val = conf.Get<string>(*it);
|
---|
517 |
|
---|
518 | if (verbose>0)
|
---|
519 | cout << "Alias: " << name << " = " << val << endl;
|
---|
520 |
|
---|
521 | if (!c.SetAlias(name.c_str(), val.c_str()))
|
---|
522 | {
|
---|
523 | cerr << "\nERROR - Alias could not be established!\n";
|
---|
524 | cerr << " " << name << " = " << val << endl;
|
---|
525 | return 2;
|
---|
526 | }
|
---|
527 | }
|
---|
528 |
|
---|
529 | // -------------------------- Configure Selector --------------------------
|
---|
530 |
|
---|
531 | TTreeFormulaManager *manager = new TTreeFormulaManager;
|
---|
532 |
|
---|
533 | if (verbose>0)
|
---|
534 | cout << "\nSelector: " << conf.Get<string>("selector") << endl;
|
---|
535 |
|
---|
536 | TTreeFormula selector("Selector", conf.Get<string>("selector").c_str(), &c);
|
---|
537 | if (selector.GetNdim()==0)
|
---|
538 | {
|
---|
539 | cerr << "Compilation of Selector failed!" << endl;
|
---|
540 | return 3;
|
---|
541 | }
|
---|
542 | selector.SetQuickLoad(kTRUE);
|
---|
543 | manager->Add(&selector);
|
---|
544 | GetLeaves(leaflist, selector);
|
---|
545 |
|
---|
546 | // -------------------- Configure additional columns ----------------------
|
---|
547 |
|
---|
548 | vector<TTreeFormula*> formulas;
|
---|
549 |
|
---|
550 | const auto vform = conf.GetWildcardOptions("add.*");
|
---|
551 | if (verbose>0 && vform.size()>0)
|
---|
552 | cout << '\n';
|
---|
553 | for (auto it=vform.cbegin(); it!=vform.cend(); it++)
|
---|
554 | {
|
---|
555 | const string name = it->substr(4);
|
---|
556 | const string val = conf.Get<string>(*it);
|
---|
557 |
|
---|
558 | if (verbose>0)
|
---|
559 | cout << "Adding column: " << name << " = " << val << endl;
|
---|
560 |
|
---|
561 | TTreeFormula *form = new TTreeFormula(name.c_str(), val.c_str(), &c);
|
---|
562 | if (form->GetNdim()==0)
|
---|
563 | {
|
---|
564 | cerr << "Compilation of Column failed!" << endl;
|
---|
565 | return 4;
|
---|
566 | }
|
---|
567 | form->SetQuickLoad(kTRUE);
|
---|
568 | formulas.emplace_back(form);
|
---|
569 | manager->Add(form);
|
---|
570 | GetLeaves(leaflist, *form);
|
---|
571 | }
|
---|
572 | manager->Sync();
|
---|
573 |
|
---|
574 | if (verbose>0)
|
---|
575 | cout << '\n' << formulas.size() << " additional columns setup for writing." << endl;
|
---|
576 |
|
---|
577 | // ------------------------- Enable branch reading ------------------------
|
---|
578 |
|
---|
579 | UInt_t datatype = 0;
|
---|
580 | const bool has_datatype = c.SetBranchAddress("DataType.fVal", &datatype) >= 0;
|
---|
581 | if (has_datatype && verbose>0)
|
---|
582 | cout << "\nRows with DataType.fVal!=1 will be skipped." << endl;
|
---|
583 |
|
---|
584 |
|
---|
585 | // Seting up branch status (must be after all SetBranchAddress)
|
---|
586 | c.SetBranchStatus("*", 0);
|
---|
587 |
|
---|
588 | for (auto it=leaflist.cbegin(); it!=leaflist.cend(); it++)
|
---|
589 | {
|
---|
590 | c.SetBranchStatus(it->c_str(), 1);
|
---|
591 | if (verbose>2)
|
---|
592 | cout << "\nEnable Branch: " << *it;
|
---|
593 | }
|
---|
594 | if (verbose>2)
|
---|
595 | cout << endl;
|
---|
596 |
|
---|
597 | /*
|
---|
598 | Next.Reset();
|
---|
599 | while ((o=Next()))
|
---|
600 | {
|
---|
601 | const TLeaf *L = dynamic_cast<TLeaf*>(o);//c.GetLeaf(o->GetName());
|
---|
602 | if (!L)
|
---|
603 | continue;
|
---|
604 |
|
---|
605 | const TBranch *B = L->GetBranch();
|
---|
606 | if (!B)
|
---|
607 | continue;
|
---|
608 |
|
---|
609 | if (!B->GetAddress())
|
---|
610 | continue;
|
---|
611 |
|
---|
612 | c.SetBranchStatus(B->GetName(), 1);
|
---|
613 | if (verbose>2)
|
---|
614 | cout << "\nEnable Branch: " << B->GetName();
|
---|
615 | }
|
---|
616 | if (verbose>2)
|
---|
617 | cout << endl;
|
---|
618 | */
|
---|
619 |
|
---|
620 | // -------------------------------------------------------------------------
|
---|
621 |
|
---|
622 | if (verbose>0)
|
---|
623 | {
|
---|
624 | cout << '\n';
|
---|
625 | split.print();
|
---|
626 | }
|
---|
627 |
|
---|
628 | if (dryrun)
|
---|
629 | {
|
---|
630 | cout << "\nDry run: file output skipped!" << endl;
|
---|
631 | return 0;
|
---|
632 | }
|
---|
633 |
|
---|
634 | if (verbose>0)
|
---|
635 | cout << "\n-------------------------- Converting file -------------------------" << endl;
|
---|
636 |
|
---|
637 | vector<ofstream> outfiles;
|
---|
638 |
|
---|
639 | if (split.empty())
|
---|
640 | {
|
---|
641 | TString path(out.c_str());
|
---|
642 | const int rc = CheckFile(path, force, verbose);
|
---|
643 | if (rc>0)
|
---|
644 | return rc;
|
---|
645 |
|
---|
646 | outfiles.emplace_back(path.Data(), append ? ios::app : ios::trunc);
|
---|
647 | if (rc==-1 || (force && rc==0 && !append))
|
---|
648 | WriteHeader(outfiles.back(), vec, formulas, skip, header);
|
---|
649 | }
|
---|
650 | else
|
---|
651 | {
|
---|
652 | for (size_t i=0; i<split.size(); i++)
|
---|
653 | {
|
---|
654 | TString path(out.c_str());
|
---|
655 | path += "-";
|
---|
656 | path += i;
|
---|
657 |
|
---|
658 | const int rc = CheckFile(path, force, verbose);
|
---|
659 | if (rc>0)
|
---|
660 | return rc;
|
---|
661 | outfiles.emplace_back(path.Data(), append ? ios::app : ios::trunc);
|
---|
662 | if (rc==-1 || (force && rc==0 && !append))
|
---|
663 | WriteHeader(outfiles.back(), vec, formulas, skip, header);
|
---|
664 | }
|
---|
665 | }
|
---|
666 |
|
---|
667 | // ---------------------------- Write Body --------------------------------
|
---|
668 | size_t count = 0;
|
---|
669 | vector<size_t> ncount(split.empty()?1:split.size());
|
---|
670 |
|
---|
671 | auto itree = c.GetTreeNumber();
|
---|
672 |
|
---|
673 | const size_t num = max>0 && (max-first)<entries ? (max-first) : entries;
|
---|
674 | for (size_t j=first; j<num; j++)
|
---|
675 | {
|
---|
676 | c.GetEntry(j);
|
---|
677 | if (has_datatype && datatype!=1)
|
---|
678 | continue;
|
---|
679 |
|
---|
680 | if (itree != c.GetTreeNumber())
|
---|
681 | {
|
---|
682 | manager->UpdateFormulaLeaves();
|
---|
683 | itree = c.GetTreeNumber();
|
---|
684 | }
|
---|
685 |
|
---|
686 | if (selector.GetNdim() && selector.EvalInstance(0)<=0)
|
---|
687 | continue;
|
---|
688 |
|
---|
689 | const size_t index = split.index(count++);
|
---|
690 | ncount[index]++;
|
---|
691 |
|
---|
692 | vector<string> join;
|
---|
693 |
|
---|
694 | if (!skip)
|
---|
695 | {
|
---|
696 | for (auto v=vec.cbegin(); v!=vec.cend(); v++)
|
---|
697 | {
|
---|
698 | const size_t N = v->num;
|
---|
699 | for (size_t i=0; i<N; i++)
|
---|
700 | join.emplace_back(v->fmt(i));
|
---|
701 | }
|
---|
702 | }
|
---|
703 |
|
---|
704 | for (auto v=formulas.cbegin(); v!=formulas.cend(); v++)
|
---|
705 | join.emplace_back(to_string((*v)->EvalInstance(0)));
|
---|
706 |
|
---|
707 | outfiles[index] << boost::join(join, " ") << "\n";
|
---|
708 | }
|
---|
709 |
|
---|
710 | if (verbose>0)
|
---|
711 | {
|
---|
712 | cout << "\nTotal: N=" << count << " out of " << num << " row(s) written [N=" << first << ".." << num-1 << "]." << endl;
|
---|
713 | for (int i=0; i<split.size(); i++)
|
---|
714 | cout << "File " << i << ": nrows=" << ncount[i] << '\n';
|
---|
715 | cout << '\n';
|
---|
716 | }
|
---|
717 |
|
---|
718 | if (verbose>0)
|
---|
719 | {
|
---|
720 | cout << "Total execution time: " << Time().UnixTime()-start.UnixTime() << "s.\n";
|
---|
721 | cout << "Success!\n" << endl;
|
---|
722 | }
|
---|
723 | return 0;
|
---|
724 | }
|
---|