Changeset 19794 for trunk/FACT++
- Timestamp:
- 10/25/19 23:46:32 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/FACT++/src/root2csv.cc
r19792 r19794 51 51 ("force,f", po_switch(), "Force overwrite if output file already exists.") 52 52 ("append,a", po_switch(), "Append to an existing file (not check for the format is done!)") 53 ("create", po_switch(), "Create the database if not existing")54 53 ("tree,t", var<string>("Events"), "Name of the root tree to convert") 55 54 ("ignore", vars<string>(), "Ignore the given leaf, if the given regular expression matches") 56 55 ("alias.*", var<string>(), "Define an alias") 57 56 ("auto-alias", vars<Map>(), "Regular expression to define aliases from the branch names automatically") 57 ("header", var<uint16_t>(uint16_t(0)),"Type of header line (0: preceeding #, 1: without preceeding #, 2: none)") 58 58 ("add.*", var<string>(), "Define an additional column") 59 59 ("selector", var<string>("1"), "Define a selector for the columns (colums where this evaluates to a value <=0 are discarded)") … … 61 61 ("first", var<int64_t>(int64_t(0)), "First event to start with (default: 0), mainly for test purpose") 62 62 ("max", var<int64_t>(int64_t(0)), "Maximum number of events to process (0: all), mainly for test purpose") 63 ("const.*", var<string>(), "Insert a constant number into the given column (--const.mycolumn=5). A special case is `/.../.../`")63 //("const.*", var<string>(), "Insert a constant number into the given column (--const.mycolumn=5). A special case is `/.../.../`") 64 64 ("dry-run", po_switch(), "Do not create or manipulate any output file") 65 65 ; … … 329 329 330 330 // --------------------------- Write Header -------------------------------- 331 void WriteHeader(ostream &out, const vector<Container> &vec, const vector<TTreeFormula*> &form, bool skip) 332 { 333 out << "#"; 331 void WriteHeader(ostream &out, const vector<Container> &vec, const vector<TTreeFormula*> &form, bool skip, uint16_t header) 332 { 333 if (header>1) 334 return; 335 if (header==0) 336 out << "# "; 337 338 vector<string> join; 334 339 335 340 if (!skip) … … 340 345 for (size_t i=0; i<N; i++) 341 346 { 342 out << " " <<v->column;347 string name = v->column; 343 348 if (N!=1) 344 out << "["+to_string(i)+"]"; 349 name += "["+to_string(i)+"]"; 350 join.emplace_back(name); 345 351 } 346 352 } … … 348 354 349 355 for (auto v=form.cbegin(); v!=form.cend(); v++) 350 out << " " << (*v)->GetName();351 352 out << "\n";356 join.emplace_back((*v)->GetName()); 357 358 out << boost::join(join, " ") << "\n"; 353 359 } 354 360 … … 422 428 const int64_t first = conf.Get<int64_t>("first"); 423 429 const int64_t max = conf.Get<int64_t>("max"); 430 const uint16_t header = conf.Get<uint16_t>("header"); 424 431 425 432 const bool print_ls = conf.Get<bool>("print-ls"); … … 482 489 for (const auto &file : files) 483 490 { 484 const auto add = c.Add(file.c_str() );491 const auto add = c.Add(file.c_str(), 0); 485 492 if (verbose>0) 486 493 cout << file << ": " << add << " file(s) added." << endl; … … 515 522 } 516 523 517 const auto entries = c.GetEntries ();524 const auto entries = c.GetEntriesFast(); 518 525 519 526 if (verbose>0) … … 587 594 */ 588 595 589 if (autoalias.size())590 { 596 // ------------------------- Setup all branches in tree ------------------- 597 591 598 TIter Next(leaves); 592 599 TObject *o = 0; … … 594 601 { 595 602 TLeaf *L = c.GetLeaf(o->GetName()); 596 if (L->GetLenStatic()!=L->GetLen())597 continue;598 603 599 604 string name = o->GetName(); 600 /*601 bool found = false;602 for (auto b=_ignore.cbegin(); b!=_ignore.cend(); b++)603 {604 if (boost::regex_match(name, boost::regex(*b)))605 {606 found = true;607 break;608 }609 }610 if (found)611 continue;612 */613 const string tn = L->GetTypeName();614 auto it = ConvRoot.find(tn);615 if (it==ConvRoot.end())616 continue;617 605 618 606 for (auto m=autoalias.cbegin(); m!=autoalias.cend(); m++) 619 607 name = boost::regex_replace(name, boost::regex(m->first), m->second); 620 608 621 if (name==o->GetName()) 609 if (name!=o->GetName()) 610 { 611 if (verbose>0) 612 cout << "Auto-alias: " << name << " = " << o->GetName() << endl; 613 if (!c.SetAlias(name.c_str(), o->GetName())) 614 cout << "WARNING - Alias could not be established!" << endl; 615 } 616 617 if (skip) 622 618 continue; 623 624 if (verbose>0)625 cout << "Auto-alias: " << name << " = " << o->GetName() << endl;626 627 if (!c.SetAlias(name.c_str(), o->GetName()))628 cout << "WARNING - Alias could not be established!" << endl;629 }630 }631 632 // ------------------------ Configure Aliases -----------------------------633 634 const auto valiases = conf.GetWildcardOptions("alias.*");635 if (verbose>0 && valiases.size()>0)636 cout << '\n';637 for (auto it=valiases.cbegin(); it!=valiases.cend(); it++)638 {639 const string name = it->substr(6);640 const string val = conf.Get<string>(*it);641 642 if (verbose>0)643 cout << "Alias: " << name << " = " << val << endl;644 645 if (!c.SetAlias(name.c_str(), val.c_str()))646 {647 cerr << "Alias could not be established!" << endl;648 return 2;649 }650 }651 652 // -------------------------- Configure Selector --------------------------653 654 vector<string> leaflist;655 c.SetBranchStatus("*", 1);656 657 TTreeFormulaManager *manager = new TTreeFormulaManager;658 659 if (verbose>0)660 cout << "\nSelector: " << conf.Get<string>("selector") << endl;661 662 TTreeFormula selector("Selector", conf.Get<string>("selector").c_str(), &c);663 if (selector.GetNdim()==0)664 {665 cerr << "Compilation of Selector failed!" << endl;666 return 3;667 }668 selector.SetQuickLoad(kTRUE);669 manager->Add(&selector);670 GetLeaves(leaflist, selector);671 672 // -------------------- Configure additional columns ----------------------673 674 vector<TTreeFormula*> formulas;675 676 const auto vform = conf.GetWildcardOptions("add.*");677 if (verbose>0 && vform.size()>0)678 cout << '\n';679 for (auto it=vform.cbegin(); it!=vform.cend(); it++)680 {681 const string name = it->substr(4);682 const string val = conf.Get<string>(*it);683 684 if (verbose>0)685 cout << "Column: " << name << " = " << val << endl;686 687 TTreeFormula *form = new TTreeFormula(name.c_str(), val.c_str(), &c);688 if (form->GetNdim()==0)689 {690 cerr << "Compilation of Column failed!" << endl;691 return 4;692 }693 form->SetQuickLoad(kTRUE);694 formulas.emplace_back(form);695 manager->Add(form);696 GetLeaves(leaflist, *form);697 }698 manager->Sync();699 700 if (verbose>0)701 cout << '\n' << formulas.size() << " additional columns setup for writing." << endl;702 703 // ------------------------- Setup all branches in tree -------------------704 705 if (!skip)706 {707 TIter Next(leaves);708 TObject *o = 0;709 while ((o=Next()))710 {711 TLeaf *L = c.GetLeaf(o->GetName());712 619 713 620 if (verbose>2) … … 721 628 } 722 629 723 string name = o->GetName();724 725 630 bool found = false; 726 631 for (auto b=_ignore.cbegin(); b!=_ignore.cend(); b++) 727 632 { 728 if (boost::regex_match( name, boost::regex(*b)))633 if (boost::regex_match(o->GetName(), boost::regex(*b))) 729 634 { 730 635 found = true; … … 734 639 } 735 640 } 641 736 642 if (found) 737 643 continue; … … 756 662 c.SetBranchAddress(o->GetTitle(), vec.back().ptr); 757 663 } 758 } 759 760 if (verbose>0) 761 cout << vec.size() << " default leaf/leaves setup for reading." << endl; 664 665 if (verbose>0) 666 { 667 if (skip) 668 cout << "Default columns skipped: "; 669 cout << vec.size() << " default leaf/leaves setup for reading." << endl; 670 } 671 672 673 // ------------------- Configure manual aliases ---------------------------- 674 675 const auto valiases = conf.GetWildcardOptions("alias.*"); 676 if (verbose>0 && valiases.size()>0) 677 cout << '\n'; 678 for (auto it=valiases.cbegin(); it!=valiases.cend(); it++) 679 { 680 const string name = it->substr(6); 681 const string val = conf.Get<string>(*it); 682 683 if (verbose>0) 684 cout << "Alias: " << name << " = " << val << endl; 685 686 if (!c.SetAlias(name.c_str(), val.c_str())) 687 { 688 cerr << "Alias could not be established!" << endl; 689 return 2; 690 } 691 } 692 693 // -------------------------- Configure Selector -------------------------- 694 695 vector<string> leaflist; 696 c.SetBranchStatus("*", 1); 697 698 TTreeFormulaManager *manager = new TTreeFormulaManager; 699 700 if (verbose>0) 701 cout << "\nSelector: " << conf.Get<string>("selector") << endl; 702 703 TTreeFormula selector("Selector", conf.Get<string>("selector").c_str(), &c); 704 if (selector.GetNdim()==0) 705 { 706 cerr << "Compilation of Selector failed!" << endl; 707 return 3; 708 } 709 selector.SetQuickLoad(kTRUE); 710 manager->Add(&selector); 711 GetLeaves(leaflist, selector); 712 713 // -------------------- Configure additional columns ---------------------- 714 715 vector<TTreeFormula*> formulas; 716 717 const auto vform = conf.GetWildcardOptions("add.*"); 718 if (verbose>0 && vform.size()>0) 719 cout << '\n'; 720 for (auto it=vform.cbegin(); it!=vform.cend(); it++) 721 { 722 const string name = it->substr(4); 723 const string val = conf.Get<string>(*it); 724 725 if (verbose>0) 726 cout << "Adding column: " << name << " = " << val << endl; 727 728 TTreeFormula *form = new TTreeFormula(name.c_str(), val.c_str(), &c); 729 if (form->GetNdim()==0) 730 { 731 cerr << "Compilation of Column failed!" << endl; 732 return 4; 733 } 734 form->SetQuickLoad(kTRUE); 735 formulas.emplace_back(form); 736 manager->Add(form); 737 GetLeaves(leaflist, *form); 738 } 739 manager->Sync(); 740 741 if (verbose>0) 742 cout << '\n' << formulas.size() << " additional columns setup for writing." << endl; 762 743 763 744 // --------------------- Setup all branches in formulas ------------------- … … 850 831 outfiles.emplace_back(path.Data(), append ? ios::app : ios::trunc); 851 832 if (rc==-1 || (force && rc==0 && !append)) 852 WriteHeader(outfiles.back(), vec, formulas, skip );833 WriteHeader(outfiles.back(), vec, formulas, skip, header); 853 834 } 854 835 else … … 865 846 outfiles.emplace_back(path.Data(), append ? ios::app : ios::trunc); 866 847 if (rc==-1 || (force && rc==0 && !append)) 867 WriteHeader(outfiles.back(), vec, formulas, skip );848 WriteHeader(outfiles.back(), vec, formulas, skip, header); 868 849 } 869 850 } … … 902 883 } 903 884 885 vector<string> join; 886 904 887 if (!skip) 905 888 { … … 908 891 const size_t N = v->num; 909 892 for (size_t i=0; i<N; i++) 910 { 911 if (v!=vec.cbegin() || i>0) 912 outfiles[index] << " "; 913 914 outfiles[index] << v->fmt(i); 915 } 893 join.emplace_back(v->fmt(i)); 916 894 } 917 895 } 918 896 919 897 for (auto v=formulas.cbegin(); v!=formulas.cend(); v++) 920 { 921 if (v!=formulas.cbegin() || (vec.size()>0 && !skip)) 922 outfiles[index] << " "; 923 924 outfiles[index] << setprecision(8) << (*v)->EvalInstance(0); 925 } 926 927 outfiles[index] << "\n"; 898 join.emplace_back(to_string((*v)->EvalInstance(0))); 899 900 outfiles[index] << boost::join(join, " ") << "\n"; 928 901 929 902 count ++;
Note:
See TracChangeset
for help on using the changeset viewer.