HiNTupleXML.cxx

Go to the documentation of this file.
00001 
00011 // for have root cfitsio
00012 #ifdef HAVE_CONFIG_H
00013 #include "config.h"
00014 #endif
00015 
00016 // for truncation warning
00017 #ifdef _MSC_VER
00018 #include "msdevstudio/MSconfig.h"
00019 #endif
00020 
00021 #include "HiNTupleXML.h"
00022 
00023 #include "XmlController.h"
00024 #include "XmlElement.h"
00025 
00026 #include "datasrcs/NTupleController.h"
00027 #include "datasrcs/NTuple.h"
00028 
00029 #ifdef HAVE_CFITSIO
00030 #include "fits/FitsController.h"
00031 #endif
00032 
00033 #ifdef HAVE_ROOT
00034 #include "root/RootController.h"
00035 #endif
00036 
00037 #include <fstream>
00038 #include <stdexcept>
00039 
00040 using std::ifstream;
00041 using std::runtime_error;
00042 using std::string;
00043 
00044 using namespace hippodraw;
00045 
00046 HiNTupleXML::HiNTupleXML ( XmlController * controller )
00047   : BaseXML ( "HiNTuple", controller ),
00048     m_name ( "name" )
00049 {
00050 }
00051 
00052 XmlElement * HiNTupleXML::createElement ( const DataSource & ntuple )
00053 {
00054   XmlElement * tag = BaseXML::createElement ();
00055 
00056   const void * addr = reinterpret_cast < const void * > ( & ntuple );
00057   int id = m_controller -> getId ( addr );
00058   setId ( *tag, id );
00059 
00060   const string & full_name = ntuple.getName ();
00061   tag -> setAttribute ( m_name, full_name );
00062 
00063   return tag;
00064 }
00065 
00068 string HiNTupleXML::getName ( const XmlElement & tag ) const
00069 {
00070   string name;
00071   bool ok = tag.attribute ( m_name, name );
00072 #ifndef STRING_CLEAR_DEFECT // change to ifdef when M4 macro is implemented
00073   if ( ! ok ) name.erase ( name.begin(), name.end() );
00074 #else
00075   if ( ! ok ) name.clear();
00076 #endif
00077 
00078   return name;
00079 }
00080 
00081 
00082 
00083 const DataSource *
00084 HiNTupleXML::
00085 getObject ( const std::string fullname ) const
00086 {
00087 
00088   const DataSource * ntuple = 0;
00089 
00090   string filename;
00091   string name;
00092   string::size_type pos = fullname.find_last_of ( ":" );
00093   if ( pos != string::npos  && pos != 1 ) { // for Windows "C:"
00094         filename = fullname.substr ( 0, pos );
00095         name = fullname.substr ( pos + 2 );
00096   }
00097   else {
00098     filename = fullname;
00099   }
00100 
00101   string suffix;
00102   pos = filename.find_last_of ( "." );
00103   if ( pos != string::npos ) {
00104     suffix = filename.substr ( pos + 1 );
00105   }
00106 
00107   if ( suffix.empty () == false ) {
00108     if ( suffix == "fits" || suffix == "gz" ) {
00109 #ifdef HAVE_CFITSIO
00110       FitsController * controller = FitsController::instance ();
00111       ntuple = controller -> createNTuple ( filename, name );
00112 #else
00113       string what ("Can not open file\n" );
00114       what += filename + "\n";
00115       what += "because application was not built with FITS support";
00116       throw runtime_error ( what );
00117 #endif
00118     }
00119     else if ( suffix == "root" ) {
00120 #ifdef HAVE_ROOT
00121       RootController * controller = RootController::instance ();
00122       ntuple = controller -> createNTuple ( filename, name );
00123 #else
00124       string what ( "Can not open file\n" );
00125       what += filename + "\n";
00126       what += "because application was not bult with ROOT support";
00127       throw runtime_error ( what );
00128 #endif
00129     }
00130     else {
00131       NTupleController * controller = NTupleController::instance ();
00132       ntuple = controller->createNTuple ( filename );
00133     }
00134   }
00135 
00136   return ntuple;
00137 }
00138   
00139 
00140 const DataSource * 
00141 HiNTupleXML::
00142 getObject ( const XmlElement & tag ) const
00143 {
00144   const string fullname = getName ( tag );
00145   if ( fullname.empty() ) return 0;
00146 
00147   return getObject ( fullname );
00148 }

Generated for HippoDraw Class Library by doxygen