Index: /trunk/FACT++/src/root2csv.cc
===================================================================
--- /trunk/FACT++/src/root2csv.cc	(revision 19808)
+++ /trunk/FACT++/src/root2csv.cc	(revision 19809)
@@ -42,4 +42,5 @@
         //("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.")
         ;
 
@@ -124,4 +125,11 @@
         "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 "
@@ -222,5 +230,5 @@
 }
 
-void GetLeaves(vector<string> &list, const TTreeFormula &f)
+void GetLeaves(set<string> &list, const TTreeFormula &f)
 {
     int i=0;
@@ -230,5 +238,6 @@
         if (!l)
             return;
-        list.emplace_back(l->GetName());
+
+        list.emplace(l->GetName());
     }
 }
@@ -257,4 +266,5 @@
     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");
@@ -396,4 +406,6 @@
     */
 
+    set<string> leaflist;
+
     // ------------------------- Setup all branches in tree -------------------
 
@@ -402,25 +414,24 @@
         while ((o=Next()))
         {
-            TLeaf *L = c.GetLeaf(o->GetName());
+            TLeaf *L = dynamic_cast<TLeaf*>(o);//c.GetLeaf(o->GetName());
+            if (!L)
+                continue;
 
             string name = o->GetName();
-
-            for (auto m=autoalias.cbegin(); m!=autoalias.cend(); m++)
-                name = boost::regex_replace(name, boost::regex(m->first), m->second);
-
-            if (name!=o->GetName())
-            {
-                if (verbose>0)
-                    cout << "Auto-alias: " << name << " = " << o->GetName() << endl;
-                if (!c.SetAlias(name.c_str(), o->GetName()))
-                    cout << "WARNING - Alias could not be established!" << endl;
-            }
-
-            if (skip)
-                continue;
-
             if (verbose>2)
                 cout << '\n' << L->GetTitle() << " {" << 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())
+            {
+                if (verbose>2)
+                    cout << " (-n/a-)";
+                continue;
+            }
+
+            // Check if this is an array, otherwise skip
             if (L->GetLenStatic()!=L->GetLen())
             {
@@ -430,5 +441,28 @@
             }
 
-            bool found = false;
+            // Check for renaming via auto-alias
+            for (auto m=autoalias.cbegin(); m!=autoalias.cend(); m++)
+                name = boost::regex_replace(name, boost::regex(m->first), m->second);
+
+            // if renaming has changed name, define alias
+            if (name!=o->GetName())
+            {
+                if (!c.SetAlias(name.c_str(), o->GetName()))
+                {
+                    cerr << "\nERROR - Alias could not be established!\n";
+                    cerr << "   " << name << " = " << o->GetName() << endl;
+                    return 1;
+                }
+                if (verbose==1 || verbose==2)
+                    cout << "\nAuto-alias: " << name << " = " << o->GetName();
+                if (verbose>2)
+                    cout << " <alias:" << name << ">";
+
+                if (!noindirect)
+                    leaflist.emplace(o->GetTitle());
+            }
+
+            // Check whether the un-aliased column is in the ignore list
+            bool found = skip;
             for (auto b=_ignore.cbegin(); b!=_ignore.cend(); b++)
             {
@@ -441,4 +475,6 @@
                 }
             }
+
+            // Check whether the aliased column is in the ignore list
             for (auto b=_ignore.cbegin(); b!=_ignore.cend(); b++)
             {
@@ -452,23 +488,10 @@
             }
 
-            if (found)
-                continue;
-
-            const string tn = L->GetTypeName();
-
-            const auto it = FileEntry::LUT.root(tn);
-            if (it==FileEntry::LUT.cend())
-            {
-                if (verbose>2)
-                    cout << " (-n/a-)";
-                continue;
-            }
-
-            if (verbose==2)
-                cout << '\n' << L->GetTitle() << " {" << L->GetTypeName() << "}";
-
-            if (verbose>1)
-                cout << " (" << name << ")";
-
+            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);
@@ -477,9 +500,9 @@
         if (verbose>0)
         {
+            cout << '\n';
             if (skip)
                 cout << "Default columns skipped: ";
             cout << vec.size() << " default leaf/leaves setup for reading." << endl;
         }
-
 
     // ------------------- Configure manual aliases ----------------------------
@@ -498,5 +521,6 @@
         if (!c.SetAlias(name.c_str(), val.c_str()))
         {
-            cerr << "Alias could not be established!" << endl;
+            cerr << "\nERROR - Alias could not be established!\n";
+            cerr << "   " << name << " = " << val << endl;
             return 2;
         }
@@ -504,7 +528,4 @@
 
     // -------------------------- Configure Selector --------------------------
-
-    vector<string> leaflist;
-    c.SetBranchStatus("*", 1);
 
     TTreeFormulaManager *manager = new TTreeFormulaManager;
@@ -554,63 +575,46 @@
         cout << '\n' << formulas.size() << " additional columns setup for writing." << endl;
 
-    // --------------------- Setup all branches in formulas -------------------
-
-    for (auto l=leaflist.cbegin(); l!=leaflist.cend(); l++)
-    {
-        // Branch address already set
-        if (c.GetBranch(l->c_str())->GetAddress())
-            continue;
-
-        TLeaf *L = c.GetLeaf(l->c_str());
-
-        if (verbose>2)
-            cout << '\n' << L->GetTitle() << " {" << L->GetTypeName() << "}";
-
-        if (L->GetLenStatic()!=L->GetLen())
-        {
-            if (verbose>2)
-                cout << " (-skipped-)";
-            continue;
-        }
-
-        const string tn = L->GetTypeName();
-
-        const auto it = FileEntry::LUT.root(tn);
-        if (it==FileEntry::LUT.cend())
-        {
-            if (verbose>2)
-                cout << " (-n/a-)";
-            continue;
-        }
-
-        if (verbose==2)
-            cout << '\n' << L->GetTitle() << " {" << L->GetTypeName() << "}";
-
-        if (verbose>1)
-            cout << " (" << *l << ")";
-
-        vec.emplace_back(l->c_str(), l->c_str(), it->type, L->GetLenStatic());
-        c.SetBranchAddress(l->c_str(), vec.back().ptr);
-    }
-    if (verbose>1)
-        cout << '\n';
-
     // ------------------------- Enable branch reading ------------------------
 
     UInt_t datatype = 0;
     const bool has_datatype = c.SetBranchAddress("DataType.fVal", &datatype) >= 0;
+    if (has_datatype && verbose>0)
+        cout << "\nRows with DataType.fVal!=1 will be skipped." << endl;
+
 
     // Seting up branch status (must be after all SetBranchAddress)
     c.SetBranchStatus("*", 0);
-    for (auto v=vec.cbegin(); v!=vec.cend(); v++)
-        if (v->type!=FileEntry::kConst)
-            c.SetBranchStatus(v->branch.c_str(), 1);
-
-    if (has_datatype)
-    {
-        c.SetBranchStatus("DataType.fVal", 1);
-        if (verbose>0)
-            cout << "Rows with DataType.fVal!=1 will be skipped." << endl;
-    }
+
+    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()))
+    {
+        const TLeaf *L = dynamic_cast<TLeaf*>(o);//c.GetLeaf(o->GetName());
+        if (!L)
+            continue;
+
+        const TBranch *B = L->GetBranch();
+        if (!B)
+            continue;
+
+        if (!B->GetAddress())
+            continue;
+
+        c.SetBranchStatus(B->GetName(), 1);
+        if (verbose>2)
+            cout << "\nEnable Branch: " << B->GetName();
+    }
+    if (verbose>2)
+        cout << endl;
+    */
 
     // -------------------------------------------------------------------------
