Index: /trunk/FACT++/src/root2csv.cc
===================================================================
--- /trunk/FACT++/src/root2csv.cc	(revision 19809)
+++ /trunk/FACT++/src/root2csv.cc	(revision 19810)
@@ -42,5 +42,4 @@
         //("const.*",        var<string>(),             "Insert a constant number into the given column (--const.mycolumn=5). A special case is `/.../.../`")
         ("dry-run",        po_switch(),               "Do not create or manipulate any output file")
-        ("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.")
         ;
 
@@ -125,11 +124,4 @@
         "automatically allowing to access the columns in a formula with the new name.\n"
         "\n"
-        "Note that in the default configuration, all data which is auto-alias'ed is read "
-        "from the file. This can become quite slow if there is a lot. An alternative is "
-        "to switch on no-indirections with --no-indirection. The ensures that only "
-        "references leaves are read, which can signiifcantly accelerate reading. "
-        "The disadvantage is that adding a column which reference an alias which "
-        "itself references an alias will fail.\n"
-        "\n"
         "Sometimes it might also be convenient to skip a leaf, i.e. not writing the "
         "coresponding column in the output file. This can be done with "
@@ -213,5 +205,5 @@
         {
             cerr << "File '" << path << "' is not writable." << endl;
-            return 2;
+            return 7;
         }
 
@@ -219,5 +211,5 @@
         {
             cerr << "File '" << path << "' already exists." << endl;
-            return 3;
+            return 8;
         }
         else
@@ -230,16 +222,33 @@
 }
 
-void GetLeaves(set<string> &list, const TTreeFormula &f)
+class Formula : public TTreeFormula
 {
-    int i=0;
-    while (1)
-    {
-        const auto l = f.GetLeaf(i++);
-        if (!l)
+public:
+    void CollectLeaves(set<string> &list, Formula *formula)
+    {
+        if (!formula)
             return;
 
-        list.emplace(l->GetName());
-    }
-}
+        TObject *o = 0;
+
+        TIter NextN(&formula->fLeafNames);
+        while ((o=NextN()))
+            list.emplace(o->GetName());
+
+        TIter NextF(&formula->fAliases);
+        while ((o=NextF()))
+            CollectLeaves(list, static_cast<Formula*>(o));
+    }
+
+    void CollectLeaves(set<string> &list)
+    {
+        CollectLeaves(list, this);
+    }
+
+    Formula(const char* name, const char* formula, TTree* tree)
+        : TTreeFormula(name, formula, tree)
+    {
+    }
+};
 
 int main(int argc, const char* argv[])
@@ -266,5 +275,4 @@
     const bool dryrun            = conf.Get<bool>("dry-run");
     const bool skip              = conf.Get<bool>("skip");
-    const bool noindirect        = conf.Get<bool>("no-indirection");
 
     const uint16_t verbose       = conf.Get<uint16_t>("verbose");
@@ -418,5 +426,5 @@
                 continue;
 
-            string name = o->GetName();
+            string name = L->GetName();
             if (verbose>2)
                 cout << '\n' << L->GetTitle() << " {" << L->GetTypeName() << "}";
@@ -446,19 +454,16 @@
 
             // if renaming has changed name, define alias
-            if (name!=o->GetName())
-            {
-                if (!c.SetAlias(name.c_str(), o->GetName()))
+            if (name!=L->GetName())
+            {
+                if (!c.SetAlias(name.c_str(), L->GetName()))
                 {
                     cerr << "\nERROR - Alias could not be established!\n";
-                    cerr << "   " << name << " = " << o->GetName() << endl;
+                    cerr << "   " << name << " = " << L->GetName() << endl;
                     return 1;
                 }
                 if (verbose==1 || verbose==2)
-                    cout << "\nAuto-alias: " << name << " = " << o->GetName();
+                    cout << "\nAuto-alias: " << name << " = " << L->GetName();
                 if (verbose>2)
                     cout << " <alias:" << name << ">";
-
-                if (!noindirect)
-                    leaflist.emplace(o->GetTitle());
             }
 
@@ -467,9 +472,7 @@
             for (auto b=_ignore.cbegin(); b!=_ignore.cend(); b++)
             {
-                if (boost::regex_match(o->GetName(), boost::regex(*b)))
+                if (boost::regex_match(L->GetName(), boost::regex(*b)))
                 {
                     found = true;
-                    if (verbose>2)
-                        cout << " (-ignored-)";
                     break;
                 }
@@ -482,18 +485,17 @@
                 {
                     found = true;
-                    if (verbose>2)
-                        cout << " (-ignored-)";
                     break;
                 }
             }
 
-            if (!found)
-                leaflist.emplace(o->GetTitle());
-
-            // As it is easier, we setup addresses for all branches
-            // also for the ones not requested. They might later be
-            // requested by a formula.
-            vec.emplace_back(o->GetTitle(), name, it->type, L->GetLenStatic());
-            c.SetBranchAddress(o->GetTitle(), vec.back().ptr);
+            if (found)
+            {
+                if (verbose>2)
+                    cout << " (-ignored-)";
+                continue;
+            }
+
+            vec.emplace_back(L->GetName(), name, it->type, L->GetLenStatic());
+            c.SetBranchAddress(L->GetName(), vec.back().ptr);
         }
 
@@ -534,5 +536,5 @@
         cout << "\nSelector: " <<  conf.Get<string>("selector") << endl;
 
-    TTreeFormula selector("Selector", conf.Get<string>("selector").c_str(), &c);
+    Formula selector("Selector", conf.Get<string>("selector").c_str(), &c);
     if (selector.GetNdim()==0)
     {
@@ -541,6 +543,6 @@
     }
     selector.SetQuickLoad(kTRUE);
+    selector.CollectLeaves(leaflist);
     manager->Add(&selector);
-    GetLeaves(leaflist, selector);
 
     // -------------------- Configure additional columns ----------------------
@@ -559,5 +561,5 @@
             cout << "Adding column: " << name << " = " << val << endl;
 
-        TTreeFormula *form = new TTreeFormula(name.c_str(), val.c_str(), &c);
+        Formula *form = new Formula(name.c_str(), val.c_str(), &c);
         if (form->GetNdim()==0)
         {
@@ -566,9 +568,46 @@
         }
         form->SetQuickLoad(kTRUE);
+        form->CollectLeaves(leaflist);
         formulas.emplace_back(form);
         manager->Add(form);
-        GetLeaves(leaflist, *form);
     }
     manager->Sync();
+
+    // ------- Set a branch address for all leaves referenced by formulas -------
+
+    for (auto ileaf=leaflist.cbegin(); ileaf!=leaflist.cend(); ileaf++)
+    {
+        // Get Leaf
+        TLeaf *L = c.GetLeaf(ileaf->c_str());
+        if (!L)
+            continue;
+
+        // Adress already set
+        if (L->GetBranch()->GetAddress())
+            continue;
+
+        if (verbose>2)
+            cout << '\n' << L->GetName() << " {" << L->GetTypeName() << "}";
+
+        const string tn = L->GetTypeName();
+
+        // Check if this is a basic type, otherwise skip
+        const auto it = FileEntry::LUT.root(tn);
+        if (it==FileEntry::LUT.cend())
+        {
+            cerr << "Failed to enable branch '" << L->GetName() << "' (type unknown)" << endl;
+            return 5;
+        }
+
+        // Check if this is an array, otherwise skip
+        if (L->GetLenStatic()!=L->GetLen())
+        {
+            cerr << "Failed to enable branch '" << L->GetName() << "' (strange array size)" << endl;
+            return 6;
+        }
+
+        vec.emplace_back(L->GetName(), L->GetName(), it->type, L->GetLenStatic());
+        c.SetBranchAddress(L->GetName(), vec.back().ptr);
+    }
 
     if (verbose>0)
@@ -583,17 +622,7 @@
 
 
-    // Seting up branch status (must be after all SetBranchAddress)
+    // Simply switch on all bracnhs for which an address was set
     c.SetBranchStatus("*", 0);
 
-    for (auto it=leaflist.cbegin(); it!=leaflist.cend(); it++)
-    {
-        c.SetBranchStatus(it->c_str(), 1);
-        if (verbose>2)
-            cout << "\nEnable Branch: " << *it;
-    }
-    if (verbose>2)
-        cout << endl;
-
-    /*
     Next.Reset();
     while ((o=Next()))
@@ -616,5 +645,4 @@
     if (verbose>2)
         cout << endl;
-    */
 
     // -------------------------------------------------------------------------
