Changeset 19810 for trunk/FACT++/src


Ignore:
Timestamp:
10/27/19 18:16:09 (5 years ago)
Author:
tbretz
Message:
Found the solution... it needs a recursive access to the aliases stored in TTreeFormula.
File:
1 edited

Legend:

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

    r19809 r19810  
    4242        //("const.*",        var<string>(),             "Insert a constant number into the given column (--const.mycolumn=5). A special case is `/.../.../`")
    4343        ("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.")
    4544        ;
    4645
     
    125124        "automatically allowing to access the columns in a formula with the new name.\n"
    126125        "\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"
    134126        "Sometimes it might also be convenient to skip a leaf, i.e. not writing the "
    135127        "coresponding column in the output file. This can be done with "
     
    213205        {
    214206            cerr << "File '" << path << "' is not writable." << endl;
    215             return 2;
     207            return 7;
    216208        }
    217209
     
    219211        {
    220212            cerr << "File '" << path << "' already exists." << endl;
    221             return 3;
     213            return 8;
    222214        }
    223215        else
     
    230222}
    231223
    232 void GetLeaves(set<string> &list, const TTreeFormula &f)
     224class Formula : public TTreeFormula
    233225{
    234     int i=0;
    235     while (1)
    236     {
    237         const auto l = f.GetLeaf(i++);
    238         if (!l)
     226public:
     227    void CollectLeaves(set<string> &list, Formula *formula)
     228    {
     229        if (!formula)
    239230            return;
    240231
    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};
    244253
    245254int main(int argc, const char* argv[])
     
    266275    const bool dryrun            = conf.Get<bool>("dry-run");
    267276    const bool skip              = conf.Get<bool>("skip");
    268     const bool noindirect        = conf.Get<bool>("no-indirection");
    269277
    270278    const uint16_t verbose       = conf.Get<uint16_t>("verbose");
     
    418426                continue;
    419427
    420             string name = o->GetName();
     428            string name = L->GetName();
    421429            if (verbose>2)
    422430                cout << '\n' << L->GetTitle() << " {" << L->GetTypeName() << "}";
     
    446454
    447455            // 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()))
    451459                {
    452460                    cerr << "\nERROR - Alias could not be established!\n";
    453                     cerr << "   " << name << " = " << o->GetName() << endl;
     461                    cerr << "   " << name << " = " << L->GetName() << endl;
    454462                    return 1;
    455463                }
    456464                if (verbose==1 || verbose==2)
    457                     cout << "\nAuto-alias: " << name << " = " << o->GetName();
     465                    cout << "\nAuto-alias: " << name << " = " << L->GetName();
    458466                if (verbose>2)
    459467                    cout << " <alias:" << name << ">";
    460 
    461                 if (!noindirect)
    462                     leaflist.emplace(o->GetTitle());
    463468            }
    464469
     
    467472            for (auto b=_ignore.cbegin(); b!=_ignore.cend(); b++)
    468473            {
    469                 if (boost::regex_match(o->GetName(), boost::regex(*b)))
     474                if (boost::regex_match(L->GetName(), boost::regex(*b)))
    470475                {
    471476                    found = true;
    472                     if (verbose>2)
    473                         cout << " (-ignored-)";
    474477                    break;
    475478                }
     
    482485                {
    483486                    found = true;
    484                     if (verbose>2)
    485                         cout << " (-ignored-)";
    486487                    break;
    487488                }
    488489            }
    489490
    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);
    498500        }
    499501
     
    534536        cout << "\nSelector: " <<  conf.Get<string>("selector") << endl;
    535537
    536     TTreeFormula selector("Selector", conf.Get<string>("selector").c_str(), &c);
     538    Formula selector("Selector", conf.Get<string>("selector").c_str(), &c);
    537539    if (selector.GetNdim()==0)
    538540    {
     
    541543    }
    542544    selector.SetQuickLoad(kTRUE);
     545    selector.CollectLeaves(leaflist);
    543546    manager->Add(&selector);
    544     GetLeaves(leaflist, selector);
    545547
    546548    // -------------------- Configure additional columns ----------------------
     
    559561            cout << "Adding column: " << name << " = " << val << endl;
    560562
    561         TTreeFormula *form = new TTreeFormula(name.c_str(), val.c_str(), &c);
     563        Formula *form = new Formula(name.c_str(), val.c_str(), &c);
    562564        if (form->GetNdim()==0)
    563565        {
     
    566568        }
    567569        form->SetQuickLoad(kTRUE);
     570        form->CollectLeaves(leaflist);
    568571        formulas.emplace_back(form);
    569572        manager->Add(form);
    570         GetLeaves(leaflist, *form);
    571573    }
    572574    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    }
    573612
    574613    if (verbose>0)
     
    583622
    584623
    585     // Seting up branch status (must be after all SetBranchAddress)
     624    // Simply switch on all bracnhs for which an address was set
    586625    c.SetBranchStatus("*", 0);
    587626
    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     /*
    598627    Next.Reset();
    599628    while ((o=Next()))
     
    616645    if (verbose>2)
    617646        cout << endl;
    618     */
    619647
    620648    // -------------------------------------------------------------------------
Note: See TracChangeset for help on using the changeset viewer.