Changeset 19794 for trunk/FACT++/src


Ignore:
Timestamp:
10/25/19 23:46:32 (5 years ago)
Author:
tbretz
Message:
Improvements and fixes.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/FACT++/src/root2csv.cc

    r19792 r19794  
    5151        ("force,f",        po_switch(),               "Force overwrite if output file already exists.")
    5252        ("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")
    5453        ("tree,t",         var<string>("Events"),     "Name of the root tree to convert")
    5554        ("ignore",         vars<string>(),            "Ignore the given leaf, if the given regular expression matches")
    5655        ("alias.*",        var<string>(),             "Define an alias")
    5756        ("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)")
    5858        ("add.*",          var<string>(),             "Define an additional column")
    5959        ("selector",       var<string>("1"),          "Define a selector for the columns (colums where this evaluates to a value <=0 are discarded)")
     
    6161        ("first",          var<int64_t>(int64_t(0)),  "First event to start with (default: 0), mainly for test purpose")
    6262        ("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 `/.../.../`")
    6464        ("dry-run",        po_switch(),               "Do not create or manipulate any output file")
    6565        ;
     
    329329
    330330// --------------------------- Write Header --------------------------------
    331 void WriteHeader(ostream &out, const vector<Container> &vec, const vector<TTreeFormula*> &form, bool skip)
    332 {
    333     out << "#";
     331void 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;
    334339
    335340    if (!skip)
     
    340345            for (size_t i=0; i<N; i++)
    341346            {
    342                 out << " " << v->column;
     347                string name = v->column;
    343348                if (N!=1)
    344                     out << "["+to_string(i)+"]";
     349                    name += "["+to_string(i)+"]";
     350                join.emplace_back(name);
    345351            }
    346352        }
     
    348354
    349355    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";
    353359}
    354360
     
    422428    const int64_t  first         = conf.Get<int64_t>("first");
    423429    const int64_t  max           = conf.Get<int64_t>("max");
     430    const uint16_t header        = conf.Get<uint16_t>("header");
    424431
    425432    const bool print_ls          = conf.Get<bool>("print-ls");
     
    482489    for (const auto &file : files)
    483490    {
    484         const auto add = c.Add(file.c_str());
     491        const auto add = c.Add(file.c_str(), 0);
    485492        if (verbose>0)
    486493            cout << file << ": " << add << " file(s) added." << endl;
     
    515522    }
    516523
    517     const auto entries = c.GetEntries();
     524    const auto entries = c.GetEntriesFast();
    518525
    519526    if (verbose>0)
     
    587594    */
    588595
    589     if (autoalias.size())
    590     {
     596    // ------------------------- Setup all branches in tree -------------------
     597
    591598        TIter Next(leaves);
    592599        TObject *o = 0;
     
    594601        {
    595602            TLeaf *L = c.GetLeaf(o->GetName());
    596             if (L->GetLenStatic()!=L->GetLen())
    597                 continue;
    598603
    599604            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;
    617605
    618606            for (auto m=autoalias.cbegin(); m!=autoalias.cend(); m++)
    619607                name = boost::regex_replace(name, boost::regex(m->first), m->second);
    620608
    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)
    622618                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());
    712619
    713620            if (verbose>2)
     
    721628            }
    722629
    723             string name = o->GetName();
    724 
    725630            bool found = false;
    726631            for (auto b=_ignore.cbegin(); b!=_ignore.cend(); b++)
    727632            {
    728                 if (boost::regex_match(name, boost::regex(*b)))
     633                if (boost::regex_match(o->GetName(), boost::regex(*b)))
    729634                {
    730635                    found = true;
     
    734639                }
    735640            }
     641
    736642            if (found)
    737643                continue;
     
    756662            c.SetBranchAddress(o->GetTitle(), vec.back().ptr);
    757663        }
    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;
    762743
    763744    // --------------------- Setup all branches in formulas -------------------
     
    850831        outfiles.emplace_back(path.Data(), append ? ios::app : ios::trunc);
    851832        if (rc==-1 || (force && rc==0 && !append))
    852             WriteHeader(outfiles.back(), vec, formulas, skip);
     833            WriteHeader(outfiles.back(), vec, formulas, skip, header);
    853834    }
    854835    else
     
    865846            outfiles.emplace_back(path.Data(), append ? ios::app : ios::trunc);
    866847            if (rc==-1 || (force && rc==0 && !append))
    867                 WriteHeader(outfiles.back(), vec, formulas, skip);
     848                WriteHeader(outfiles.back(), vec, formulas, skip, header);
    868849        }
    869850    }
     
    902883        }
    903884
     885        vector<string> join;
     886
    904887        if (!skip)
    905888        {
     
    908891                const size_t N = v->num;
    909892                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));
    916894            }
    917895        }
    918896
    919897        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";
    928901
    929902        count ++;
Note: See TracChangeset for help on using the changeset viewer.