- Timestamp:
- 10/27/19 18:16:09 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/FACT++/src/root2csv.cc
r19809 r19810 42 42 //("const.*", var<string>(), "Insert a constant number into the given column (--const.mycolumn=5). A special case is `/.../.../`") 43 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 44 ; 46 45 … … 125 124 "automatically allowing to access the columns in a formula with the new name.\n" 126 125 "\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 126 "Sometimes it might also be convenient to skip a leaf, i.e. not writing the " 135 127 "coresponding column in the output file. This can be done with " … … 213 205 { 214 206 cerr << "File '" << path << "' is not writable." << endl; 215 return 2;207 return 7; 216 208 } 217 209 … … 219 211 { 220 212 cerr << "File '" << path << "' already exists." << endl; 221 return 3;213 return 8; 222 214 } 223 215 else … … 230 222 } 231 223 232 void GetLeaves(set<string> &list, const TTreeFormula &f) 224 class Formula : public TTreeFormula 233 225 { 234 int i=0; 235 while (1) 236 { 237 const auto l = f.GetLeaf(i++); 238 if (!l) 226 public: 227 void CollectLeaves(set<string> &list, Formula *formula) 228 { 229 if (!formula) 239 230 return; 240 231 241 list.emplace(l->GetName()); 242 } 243 } 232 TObject *o = 0; 233 234 TIter NextN(&formula->fLeafNames); 235 while ((o=NextN())) 236 list.emplace(o->GetName()); 237 238 TIter NextF(&formula->fAliases); 239 while ((o=NextF())) 240 CollectLeaves(list, static_cast<Formula*>(o)); 241 } 242 243 void CollectLeaves(set<string> &list) 244 { 245 CollectLeaves(list, this); 246 } 247 248 Formula(const char* name, const char* formula, TTree* tree) 249 : TTreeFormula(name, formula, tree) 250 { 251 } 252 }; 244 253 245 254 int main(int argc, const char* argv[]) … … 266 275 const bool dryrun = conf.Get<bool>("dry-run"); 267 276 const bool skip = conf.Get<bool>("skip"); 268 const bool noindirect = conf.Get<bool>("no-indirection");269 277 270 278 const uint16_t verbose = conf.Get<uint16_t>("verbose"); … … 418 426 continue; 419 427 420 string name = o->GetName();428 string name = L->GetName(); 421 429 if (verbose>2) 422 430 cout << '\n' << L->GetTitle() << " {" << L->GetTypeName() << "}"; … … 446 454 447 455 // if renaming has changed name, define alias 448 if (name!= o->GetName())449 { 450 if (!c.SetAlias(name.c_str(), o->GetName()))456 if (name!=L->GetName()) 457 { 458 if (!c.SetAlias(name.c_str(), L->GetName())) 451 459 { 452 460 cerr << "\nERROR - Alias could not be established!\n"; 453 cerr << " " << name << " = " << o->GetName() << endl;461 cerr << " " << name << " = " << L->GetName() << endl; 454 462 return 1; 455 463 } 456 464 if (verbose==1 || verbose==2) 457 cout << "\nAuto-alias: " << name << " = " << o->GetName();465 cout << "\nAuto-alias: " << name << " = " << L->GetName(); 458 466 if (verbose>2) 459 467 cout << " <alias:" << name << ">"; 460 461 if (!noindirect)462 leaflist.emplace(o->GetTitle());463 468 } 464 469 … … 467 472 for (auto b=_ignore.cbegin(); b!=_ignore.cend(); b++) 468 473 { 469 if (boost::regex_match( o->GetName(), boost::regex(*b)))474 if (boost::regex_match(L->GetName(), boost::regex(*b))) 470 475 { 471 476 found = true; 472 if (verbose>2)473 cout << " (-ignored-)";474 477 break; 475 478 } … … 482 485 { 483 486 found = true; 484 if (verbose>2)485 cout << " (-ignored-)";486 487 break; 487 488 } 488 489 } 489 490 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); 491 if (found) 492 { 493 if (verbose>2) 494 cout << " (-ignored-)"; 495 continue; 496 } 497 498 vec.emplace_back(L->GetName(), name, it->type, L->GetLenStatic()); 499 c.SetBranchAddress(L->GetName(), vec.back().ptr); 498 500 } 499 501 … … 534 536 cout << "\nSelector: " << conf.Get<string>("selector") << endl; 535 537 536 TTreeFormula selector("Selector", conf.Get<string>("selector").c_str(), &c);538 Formula selector("Selector", conf.Get<string>("selector").c_str(), &c); 537 539 if (selector.GetNdim()==0) 538 540 { … … 541 543 } 542 544 selector.SetQuickLoad(kTRUE); 545 selector.CollectLeaves(leaflist); 543 546 manager->Add(&selector); 544 GetLeaves(leaflist, selector);545 547 546 548 // -------------------- Configure additional columns ---------------------- … … 559 561 cout << "Adding column: " << name << " = " << val << endl; 560 562 561 TTreeFormula *form = new TTreeFormula(name.c_str(), val.c_str(), &c);563 Formula *form = new Formula(name.c_str(), val.c_str(), &c); 562 564 if (form->GetNdim()==0) 563 565 { … … 566 568 } 567 569 form->SetQuickLoad(kTRUE); 570 form->CollectLeaves(leaflist); 568 571 formulas.emplace_back(form); 569 572 manager->Add(form); 570 GetLeaves(leaflist, *form);571 573 } 572 574 manager->Sync(); 575 576 // ------- Set a branch address for all leaves referenced by formulas ------- 577 578 for (auto ileaf=leaflist.cbegin(); ileaf!=leaflist.cend(); ileaf++) 579 { 580 // Get Leaf 581 TLeaf *L = c.GetLeaf(ileaf->c_str()); 582 if (!L) 583 continue; 584 585 // Adress already set 586 if (L->GetBranch()->GetAddress()) 587 continue; 588 589 if (verbose>2) 590 cout << '\n' << L->GetName() << " {" << L->GetTypeName() << "}"; 591 592 const string tn = L->GetTypeName(); 593 594 // Check if this is a basic type, otherwise skip 595 const auto it = FileEntry::LUT.root(tn); 596 if (it==FileEntry::LUT.cend()) 597 { 598 cerr << "Failed to enable branch '" << L->GetName() << "' (type unknown)" << endl; 599 return 5; 600 } 601 602 // Check if this is an array, otherwise skip 603 if (L->GetLenStatic()!=L->GetLen()) 604 { 605 cerr << "Failed to enable branch '" << L->GetName() << "' (strange array size)" << endl; 606 return 6; 607 } 608 609 vec.emplace_back(L->GetName(), L->GetName(), it->type, L->GetLenStatic()); 610 c.SetBranchAddress(L->GetName(), vec.back().ptr); 611 } 573 612 574 613 if (verbose>0) … … 583 622 584 623 585 // S eting up branch status (must be after all SetBranchAddress)624 // Simply switch on all bracnhs for which an address was set 586 625 c.SetBranchStatus("*", 0); 587 626 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 627 Next.Reset(); 599 628 while ((o=Next())) … … 616 645 if (verbose>2) 617 646 cout << endl; 618 */619 647 620 648 // -------------------------------------------------------------------------
Note:
See TracChangeset
for help on using the changeset viewer.