Changeset 18836 for trunk/FACT++


Ignore:
Timestamp:
04/18/17 17:05:49 (8 years ago)
Author:
tbretz
Message:
Implemented that a factfits file can be read as if with zfits (already roughly calibrated). Implemented the possibility to dump the fits table to a root file.
File:
1 edited

Legend:

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

    r18602 r18836  
    2020
    2121#ifdef HAVE_ROOT
     22#include "TFile.h"
     23#include "TTree.h"
    2224#include "TFormula.h"
    2325#endif
     
    8587
    8688    ///Display the selected columns values VS time
    87     void Dump(ostream &, const vector<string> &, const vector<MyColumn> &, const string &, size_t, size_t, const string &);
     89    int  Dump(ostream &, const vector<string> &, const vector<MyColumn> &, const string &, size_t, size_t, const string &, Configuration &);
    8890    void DumpRoot(ostream &, const vector<string> &, const string &, size_t, size_t, const string &);
    8991    void DumpMinMax(ostream &, const vector<MyColumn> &, size_t, size_t, bool);
     
    409411//
    410412#ifdef HAVE_ROOT
    411 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)
     413int 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)
    412414#else
    413 void FitsDumper::Dump(ostream &fout, const vector<string> &format, const vector<MyColumn> &cols, const string &, size_t first, size_t limit, const string &filename)
     415int FitsDumper::Dump(ostream &fout, const vector<string> &format, const vector<MyColumn> &cols, const string &, size_t first, size_t limit, const string &filename, Configuration &)
    414416#endif
    415417{
     
    438440    fout << "#\n";
    439441
     442#ifdef HAVE_ROOT
     443    const bool rootfile = filename.rfind(".root")==filename.size()-5;
     444
     445    TFile *tfile = 0;
     446    if (rootfile)
     447    {
     448        tfile = new TFile(filename.c_str(), conf.Get<bool>("update")?"UPDATE":(conf.Get<bool>("overwrite")?"RECREATE":"CREATE"), fFilename.c_str(), conf.Get<bool>("force"));
     449        if (tfile->IsZombie())
     450        {
     451            cerr << "Could not open root file '" << rootfile << "'" << endl;
     452            return -2;
     453        }
     454    }
     455
     456    // FIXME: Add header values
     457    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;
     458#endif
     459
    440460    size_t num = 0;
    441461    for (auto it=cols.begin(); it!=cols.end(); it++)
     
    460480
    461481    // -----------------------------------------------------------------
     482
     483#ifdef HAVE_ROOT
     484    if (ttree)
     485    {
     486        fout << "## --------------------------------------------------------------------------\n";
     487
     488        fout << "#\n";
     489        fout << "# Tree: " << fKeyMap.find("EXTNAME")->second.value << "\n";
     490        fout << "#\n";
     491
     492        for (auto it=cols.begin(); it!=cols.end(); it++)
     493        {
     494            const string branch = it->name+"["+to_string(it->last-it->first+1)+"]";
     495
     496            fout << "# Branch: " << branch << "\n";
     497
     498            switch (it->col.type)
     499            {
     500            case 'B':
     501                ttree->Branch(branch.c_str(), reinterpret_cast<unsigned char*>(it->ptr)+it->first);
     502                break;
     503            case 'L':
     504                ttree->Branch(branch.c_str(), reinterpret_cast<bool*>(it->ptr)+it->first);
     505                break;
     506            case 'I':
     507                ttree->Branch(branch.c_str(), reinterpret_cast<int16_t*>(it->ptr)+it->first);
     508                break;
     509            case 'J':
     510                ttree->Branch(branch.c_str(), reinterpret_cast<int32_t*>(it->ptr)+it->first);
     511                break;
     512            case 'K':
     513                ttree->Branch(branch.c_str(), reinterpret_cast<int64_t*>(it->ptr)+it->first);
     514                break;
     515            case 'E':
     516                ttree->Branch(branch.c_str(), reinterpret_cast<float*>(it->ptr)+it->first);
     517                break;
     518            case 'D':
     519                ttree->Branch(branch.c_str(), reinterpret_cast<double*>(it->ptr)+it->first);
     520                break;
     521            default:
     522                ;
     523            }
     524        }
     525
     526        fout << "#\n";
     527        fout << "## --------------------------------------------------------------------------\n";
     528}
     529#endif
     530
     531
     532    // -----------------------------------------------------------------
     533
     534
    462535
    463536#ifdef HAVE_ROOT
     
    535608        if (!filter.empty() && select.EvalPar(0, data.data())<0.5)
    536609            continue;
    537 #endif
     610
     611        if (ttree)
     612        {
     613            ttree->Fill();
     614            continue;
     615        }
     616#endif
     617
    538618        fout << sout.str() << endl;
    539619    }
     620
     621#ifdef HAVE_ROOT
     622    if (tfile)
     623    {
     624        fout << "#\n";
     625        fout << "# " << ttree->GetEntries() << " rows filled into tree.\n";
     626        fout << "#\n";
     627        fout << "## --------------------------------------------------------------------------\n";
     628        fout << endl;
     629
     630        ttree->Write();
     631        delete tfile;
     632    }
     633#endif
     634
     635    return 0;
    540636}
    541637
     
    729825#endif
    730826}
     827
    731828
    732829void FitsDumper::DumpMinMax(ostream &fout, const vector<MyColumn> &cols, size_t first, size_t limit, bool fNoZeroPlease)
     
    9581055int FitsDumper::Exec(Configuration& conf)
    9591056{
     1057    if (conf.Get<bool>("autocal"))
     1058        resetCalibration();
     1059
    9601060    if (conf.Get<bool>("list"))
    9611061        List();
     
    10031103
    10041104    ofstream sout;
    1005     if (filename!="-")
     1105    if (filename!="-" && filename.rfind(".root")!=filename.length()-5)
    10061106    {
    10071107        sout.open(filename);
     
    10611161    }
    10621162
    1063     Dump(fout, format, cols, filter, first, limit, filename);
    1064 
    1065     return 0;
     1163    return Dump(fout, format, cols, filter, first, limit, filename, conf);
    10661164}
    10671165
     
    11231221        "\n"
    11241222        "  fitsdump col1 -%h\n"
     1223        "\n"
     1224        "To convert a fits table to a root file, use --outfile, for example\n\n"
     1225        "  fitsdump --outfile outfile.root [--compression N] [--overwrite|--update]\n\n"
     1226        "Note  that this option can not be combined  with  the  root mode (--root). "
     1227        "According to the FITS standard, all columns (except bytes) are written as "
     1228        "signed values.\n"
    11251229        "\n";
    11261230    cout << endl;
     
    11541258        ("limit",       var<size_t>(size_t(0)), "Limit for the maximum number of rows to read (0=unlimited)")
    11551259        ("tablename,t", var<string>(""),        "Name of the table to open. If not specified, first binary table is opened")
     1260        ("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.")
    11561261#ifdef HAVE_ROOT
    11571262        ("root,r",      po_switch(),            "Enable root mode")
    11581263        ("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)")
     1264        ("overwrite",   po_switch(),            "Force overwriting an existing root file ('RECREATE')")
     1265        ("update",      po_switch(),            "Update an existing root file with the new tree ('UPDATE')")
     1266        ("compression", var<uint16_t>(1),       "zlib compression level for the root file")
    11591267#endif
    11601268        ;
Note: See TracChangeset for help on using the changeset viewer.