Index: trunk/FACT++/src/fitsdump.cc
===================================================================
--- trunk/FACT++/src/fitsdump.cc	(revision 18835)
+++ trunk/FACT++/src/fitsdump.cc	(revision 18836)
@@ -20,4 +20,6 @@
 
 #ifdef HAVE_ROOT
+#include "TFile.h"
+#include "TTree.h"
 #include "TFormula.h"
 #endif
@@ -85,5 +87,5 @@
 
     ///Display the selected columns values VS time
-    void Dump(ostream &, const vector<string> &, const vector<MyColumn> &, const string &, size_t, size_t, const string &);
+    int  Dump(ostream &, const vector<string> &, const vector<MyColumn> &, const string &, size_t, size_t, const string &, Configuration &);
     void DumpRoot(ostream &, const vector<string> &, const string &, size_t, size_t, const string &);
     void DumpMinMax(ostream &, const vector<MyColumn> &, size_t, size_t, bool);
@@ -409,7 +411,7 @@
 //
 #ifdef HAVE_ROOT
-void FitsDumper::Dump(ostream &fout, const vector<string> &format, const vector<MyColumn> &cols, const string &filter, size_t first, size_t limit, const string &filename)
+int FitsDumper::Dump(ostream &fout, const vector<string> &format, const vector<MyColumn> &cols, const string &filter, size_t first, size_t limit, const string &filename, Configuration &conf)
 #else
-void FitsDumper::Dump(ostream &fout, const vector<string> &format, const vector<MyColumn> &cols, const string &, size_t first, size_t limit, const string &filename)
+int FitsDumper::Dump(ostream &fout, const vector<string> &format, const vector<MyColumn> &cols, const string &, size_t first, size_t limit, const string &filename, Configuration &)
 #endif
 {
@@ -438,4 +440,22 @@
     fout << "#\n";
 
+#ifdef HAVE_ROOT
+    const bool rootfile = filename.rfind(".root")==filename.size()-5;
+
+    TFile *tfile = 0;
+    if (rootfile)
+    {
+        tfile = new TFile(filename.c_str(), conf.Get<bool>("update")?"UPDATE":(conf.Get<bool>("overwrite")?"RECREATE":"CREATE"), fFilename.c_str(), conf.Get<bool>("force"));
+        if (tfile->IsZombie())
+        {
+            cerr << "Could not open root file '" << rootfile << "'" << endl;
+            return -2;
+        }
+    }
+
+    // FIXME: Add header values
+    TTree *ttree = tfile ? new TTree(fKeyMap.find("EXTNAME")->second.value.c_str(), Tools::Form("From N=%d [cnt=%d] {filter=%s}", first, limit, filter.c_str()).c_str()) : 0;
+#endif
+
     size_t num = 0;
     for (auto it=cols.begin(); it!=cols.end(); it++)
@@ -460,4 +480,57 @@
 
     // -----------------------------------------------------------------
+
+#ifdef HAVE_ROOT
+    if (ttree)
+    {
+        fout << "## --------------------------------------------------------------------------\n";
+
+        fout << "#\n";
+        fout << "# Tree: " << fKeyMap.find("EXTNAME")->second.value << "\n";
+        fout << "#\n";
+
+        for (auto it=cols.begin(); it!=cols.end(); it++)
+        {
+            const string branch = it->name+"["+to_string(it->last-it->first+1)+"]";
+
+            fout << "# Branch: " << branch << "\n";
+
+            switch (it->col.type)
+            {
+            case 'B':
+                ttree->Branch(branch.c_str(), reinterpret_cast<unsigned char*>(it->ptr)+it->first);
+                break;
+            case 'L':
+                ttree->Branch(branch.c_str(), reinterpret_cast<bool*>(it->ptr)+it->first);
+                break;
+            case 'I':
+                ttree->Branch(branch.c_str(), reinterpret_cast<int16_t*>(it->ptr)+it->first);
+                break;
+            case 'J':
+                ttree->Branch(branch.c_str(), reinterpret_cast<int32_t*>(it->ptr)+it->first);
+                break;
+            case 'K':
+                ttree->Branch(branch.c_str(), reinterpret_cast<int64_t*>(it->ptr)+it->first);
+                break;
+            case 'E':
+                ttree->Branch(branch.c_str(), reinterpret_cast<float*>(it->ptr)+it->first);
+                break;
+            case 'D':
+                ttree->Branch(branch.c_str(), reinterpret_cast<double*>(it->ptr)+it->first);
+                break;
+            default:
+                ;
+            }
+        }
+
+        fout << "#\n";
+        fout << "## --------------------------------------------------------------------------\n";
+}
+#endif
+
+
+    // -----------------------------------------------------------------
+
+
 
 #ifdef HAVE_ROOT
@@ -535,7 +608,30 @@
         if (!filter.empty() && select.EvalPar(0, data.data())<0.5)
             continue;
-#endif
+
+        if (ttree)
+        {
+            ttree->Fill();
+            continue;
+        }
+#endif
+
         fout << sout.str() << endl;
     }
+
+#ifdef HAVE_ROOT
+    if (tfile)
+    {
+        fout << "#\n";
+        fout << "# " << ttree->GetEntries() << " rows filled into tree.\n";
+        fout << "#\n";
+        fout << "## --------------------------------------------------------------------------\n";
+        fout << endl;
+
+        ttree->Write();
+        delete tfile;
+    }
+#endif
+
+    return 0;
 }
 
@@ -729,4 +825,5 @@
 #endif
 }
+
 
 void FitsDumper::DumpMinMax(ostream &fout, const vector<MyColumn> &cols, size_t first, size_t limit, bool fNoZeroPlease)
@@ -958,4 +1055,7 @@
 int FitsDumper::Exec(Configuration& conf)
 {
+    if (conf.Get<bool>("autocal"))
+        resetCalibration();
+
     if (conf.Get<bool>("list"))
         List();
@@ -1003,5 +1103,5 @@
 
     ofstream sout;
-    if (filename!="-")
+    if (filename!="-" && filename.rfind(".root")!=filename.length()-5)
     {
         sout.open(filename);
@@ -1061,7 +1161,5 @@
     }
 
-    Dump(fout, format, cols, filter, first, limit, filename);
-
-    return 0;
+    return Dump(fout, format, cols, filter, first, limit, filename, conf);
 }
 
@@ -1123,4 +1221,10 @@
         "\n"
         "  fitsdump col1 -%h\n"
+        "\n"
+        "To convert a fits table to a root file, use --outfile, for example\n\n"
+        "  fitsdump --outfile outfile.root [--compression N] [--overwrite|--update]\n\n"
+        "Note  that this option can not be combined  with  the  root mode (--root). "
+        "According to the FITS standard, all columns (except bytes) are written as "
+        "signed values.\n"
         "\n";
     cout << endl;
@@ -1154,7 +1258,11 @@
         ("limit",       var<size_t>(size_t(0)), "Limit for the maximum number of rows to read (0=unlimited)")
         ("tablename,t", var<string>(""),        "Name of the table to open. If not specified, first binary table is opened")
+        ("autocal",     po_switch(),            "This option can be used to skip the application of the noise calibration when reading back a compressed fits file. This is identical in using zfits instead of factfits.")
 #ifdef HAVE_ROOT
         ("root,r",      po_switch(),            "Enable root mode")
         ("filter,f",    var<string>(""),        "Filter to restrict the selection of events (e.g. '[0]>10 && [0]<20';  does not work with stat and minmax yet)")
+        ("overwrite",   po_switch(),            "Force overwriting an existing root file ('RECREATE')")
+        ("update",      po_switch(),            "Update an existing root file with the new tree ('UPDATE')")
+        ("compression", var<uint16_t>(1),       "zlib compression level for the root file")
 #endif
         ;
