examples/convert2ROOT.cxx

This is a simple utility to convert a Mimotera file into a text file containing the pixel data organized in columns.

The program is taking one input file at a time only. If multiple files are specified the program will be terminate with error.

The user can specify the name of the output file and if he wants to add an additional text line at the beginning of the output file containing the description of the column content.

Return value: 0. No error. 1. No input file specified. 2. More than one input file specified. 3. No output filename specified. 4. Output file already existing. 5. Output file opening problems. 6. Missing input file. 7. Input file not a regular file. 8. Input file read error. 9. Wrong input file size. 10. Unknown I/O file error.

Author:
Antonio Bulgheroni <antonio.bulgheroni@gmail.com>
// Author Antonio Bulgheroni, <mailto:antonio.bulgheroni@gmail.com>
// Copyright 2009 Antonio Bulgheroni

// $Rev:: 9                                                             $: Revision of the last commit
// $Author:: drhilbert                                                  $: Author of the last commit
// $Date:: 2009-08-27 18:29:29 +0200 (Thu, 27 Aug 2009)                 $: Date of the last commit

// This file is part of Mimotera++.
//
// Mimotera++ is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Mimotera++ is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Mimotera++.  If not, see <http://www.gnu.org/licenses/>.


#ifdef USE_ROOT

// personal includes ".hh"
#include "mimotera++/MimoteraAnyOption.hh"
#include "mimotera++/MimoteraLicense.hh"
#include "mimotera++/MimoteraUtils.hh"
#include "mimotera++/MimoteraReader.hh"
#include "mimotera++/MimoteraException.hh"
#include "mimotera++/MimoteraEvent.hh"

// ROOT includes <.h>
#include <TFile.h>
#include <TTree.h>

// system includes <>
#include <iostream>
#include <fstream>
#include <sys/types.h>
#include <sys/stat.h>
#include <memory>
#include <string>
#include <limits>

#define NOERROR           0
#define NOINPUTFILESPEC   1
#define MORETHANONE       2
#define NOOUTPUTFILESPEC  3
#define OUTPUTEXISTS      4
#define OUTPUTFILEOPENPROBLEM 5
#define INPUTNOTFOUND     6
#define INPUTNOTREGULAR   7
#define INPUTFILEREAD     8
#define FILESIZE          9
#define FILEERROR         10

using namespace Mimotera;
using namespace std;

int main(int argc, char ** argv ) {

  // print out the license short version
  cout << to_string( argv[0] ) << "  Copyright (C) 2009 Antonio Bulgheroni " << endl
       << "This program comes with ABSOLUTELY NO WARRANTY." << endl
       << "This is free software, and you are welcome to redistribute it " << endl
       << "under certain conditions. type `" << to_string( argv[0] ) << " --license` for details. " << endl << endl;

  auto_ptr< AnyOption > option( new AnyOption );

  string usageString = "\n"
    "This program allows to convert a Mimotera data (binary) file into an ROOT file\n"
    "containing a TTree with the converted events.\n"
    "\n"
    "Synopsis:\n"
    "\n"
    "     "  + to_string(argv[0])  + " [options] -o | --output OUTPUT_FILENAME INPUT_FILENAME\n"
    "\n"
    "Options:\n"
    "\n"
    "     "  "-h | --help \n"
    "     "  "       Print this message and exit\n\n"
    "     "  "-l | --license\n"
    "     "  "       Print the license and exit\n\n"
    "     "  "-f | --force \n"
    "     "  "       Force the creation of the output file even if this is\n"
    "     "  "       already existing\n\n"
    "     "  "-n | --num=MAX_EVENTS \n"
    "     "  "       Specify the maximum number of events. If not specified, all\n"
    "     "  "       events found will be processed\n"
    "     "  "-o | --output=OUTPUT_FILENAME \n"
    "     "  "       Specify the output filename\n\n";

  option->addUsage( usageString.c_str() );
  option->setFlag( "help", 'h' );
  option->setFlag( "license", 'l');
  option->setFlag( "force", 'f' );
  option->setOption( "output", 'o' );
  option->setOption( "num", 'n' );

  option->processCommandArgs( argc, argv );

  // react to the help flag
  if (option->getFlag('h') || option->getFlag("help")  || ( argc == 1 )) {
    option->printUsage();
    return NOERROR;
  }

  // react to the license flag
  if (option->getFlag('l') || option->getFlag("license") ) {
    cout << licenseString << endl;
    return NOERROR;
  }
  // get all the other flags
  bool force       = option->getFlag('f') || option->getFlag("force");

  size_t maxEvents = numeric_limits< size_t >::max();
  if  ( option->getValue( "num" ) != NULL ) {
    maxEvents = atoi( option->getValue( "num" ) );
  }

  // abort if no input file provided
  if ( option->getArgc() == 0 ) {
    cerr << "Please specify one input file" << endl;
    return NOINPUTFILESPEC;
  }

  // abort if more than one input file provided
  if ( option->getArgc() != 1 ) {
    cerr << argv[0] << " can process only one input file at a time." << endl;
    return MORETHANONE;
  }

  // get the input filename
  string inputFileName = option->getArgv( 0 );

  // check that the output filename is specified. In case it is not
  string outputFileName;
  if ( option->getValue( "output" ) == NULL ) {
    cerr << "Please specify an output file name using -o option" << endl;
    return NOOUTPUTFILESPEC;
  } else {
    outputFileName = option->getValue( "output" ) ;
  }

  // check if the output file already exists and the user specified he
  // doesn't want to force the creation
  struct stat statbuf;
  int status = stat( outputFileName.c_str(), &statbuf);
  if ( ( status == 0 ) && // it means that the file exists!
       ! force ) {
    cerr << "Output file " << outputFileName << " already exists. Specify -f to force overwriting." << endl;
    return OUTPUTEXISTS;
  }

  // don't need to check if the input file is present or not because
  // this is already done by the MimoteraReader. In case we just have
  // to catch the thrown exception and return with the corresponding
  // error value.
  try {

    MimoteraReader   reader( inputFileName.c_str() ) ;
    MimoteraEvent  * event = 0;
    DataArrays_t   * dataArrays = 0;
    MimoteraInfo   * info = 0;
    size_t iEvt = 0;


    // open the output file
    auto_ptr<TFile> outputFile( TFile::Open( outputFileName.c_str(), "RECREATE" ) );
    if ( !outputFile.get() ) {
      cerr << "Unknown problems opening the output file" << endl;
      return OUTPUTFILEOPENPROBLEM;
    }

    // prepare the data TTree
    auto_ptr<TTree> dataTree( new TTree("MimoTree", "MimoTree" ) );
    dataTree->Branch( "Data", "Mimotera::DataArrays_t", &dataArrays );
    dataTree->Branch( "Info", "Mimotera::MimoteraInfo", &info );
    dataTree->Branch( "iEvt", &iEvt, "iEvt/I" );

    while ( reader.readNextEvent() && ( iEvt < maxEvents ) ) {

      if ( iEvt % 20 == 0 ) {
        cout << "Processing event number " << iEvt << endl;
      }

      event       = reader.getEvent();
      dataArrays  = event->getArrays();
      info        = event->info();

      // fill the tree
      dataTree->Fill();

      ++iEvt;
    }

    dataTree.release()->Write();
    outputFile->Close();


  } catch ( FileNotFoundException & e ) {
    cerr << e.what() << endl;
    return INPUTNOTFOUND;
  } catch ( NotRegularFileException & e ) {
    cerr << e.what() << endl;
    return INPUTNOTREGULAR;
  } catch ( FileReadException & e ) {
    cerr << e.what() << endl;
    return INPUTFILEREAD;
  } catch ( WrongFileSizeException & e ) {
    cerr << e.what() << endl;
    return FILESIZE;
  } catch ( FileException & e ) {
    cerr << e.what() << endl;
    return FILEERROR;
  }



  return 0;

}

#endif

Generated on Fri Sep 18 19:15:15 2009 for Mimotera++ by  doxygen 1.5.7.1