00001
00012
00013 #ifdef _MSC_VER
00014 #include "msdevstudio/MSconfig.h"
00015 #endif
00016
00017 #include "NTupleXML.h"
00018
00019 #include "XmlController.h"
00020 #include "XmlDocument.h"
00021 #include "XmlElement.h"
00022 #include "XmlTextNode.h"
00023
00024 #include "datasrcs/DataPointTuple.h"
00025 #include "datasrcs/NTupleController.h"
00026 #include "datasrcs/NTuple.h"
00027
00028 #include <iterator>
00029
00030
00031 #ifdef SSTREAM_DEFECT
00032 #include <strstream>
00033 using std::ostrstream;
00034 #else
00035 #include <sstream>
00036 using std::istringstream;
00037 using std::ostringstream;
00038 #endif
00039
00040 using namespace hippodraw;
00041
00042 using std::istream_iterator;
00043 using std::list;
00044 using std::string;
00045 using std::vector;
00046
00047 NTupleXML::NTupleXML ( XmlController * controller )
00048 : BaseXML ( "NTuple", controller ),
00049 m_name ( "name" ),
00050 m_col_name ( "column" ),
00051 m_data ( "NTupleData" ),
00052 m_label ( "label" )
00053 {
00054 }
00055
00056 XmlElement * NTupleXML::createElement ( const DataSource & ntuple )
00057 {
00058 XmlElement * tag = BaseXML::createElement ();
00059
00060 setAttributes ( tag, ntuple );
00061
00062 createChildren ( tag, ntuple);
00063
00064 return tag;
00065 }
00066
00067
00068 void
00069 NTupleXML::
00070 setAttributes ( XmlElement * tag, const DataSource & ntuple )
00071 {
00072 const void * addr = reinterpret_cast < const void * > ( & ntuple );
00073 int id = m_controller -> getId ( addr );
00074 setId ( *tag, id );
00075
00076 const string & full_name = ntuple.getName ();
00077 tag -> setAttribute ( m_name, full_name );
00078 }
00079
00080 namespace dp = hippodraw::DataPoint2DTuple;
00081
00082 void
00083 NTupleXML::
00084 createChildren ( XmlElement * tag, const DataSource & ntuple )
00085 {
00086 unsigned int columns = ntuple.columns ();
00087 if ( columns == dp::SIZE ) createChildren2D ( tag, ntuple );
00088 else createChildren2D ( tag, ntuple );
00089 }
00090
00091 void
00092 NTupleXML::
00093 createChildren2D ( XmlElement * tag, const DataSource & ntuple )
00094 {
00095 static unsigned int cols []
00096 = { dp::X, dp::Y, dp::XERR, dp::YERR };
00097
00098 for ( unsigned int i = 0; i < dp::SIZE; i++ ) {
00099 XmlElement * element
00100 = XmlController::m_xml_doc -> createElement ( m_data );
00101 element -> setAttribute ( m_col_name, i );
00102 const string & label = ntuple.getLabelAt ( cols[i] );
00103 element -> setAttribute ( m_label, label );
00104
00105 const vector < double > & data = ntuple.getColumn ( cols[i] );
00106 unsigned int size = ntuple.rows ();
00107
00108 #ifdef SSTREAM_DEFECT
00109 ostrstream strm_text;
00110 #else
00111 ostringstream strm_text;
00112 #endif
00113 for ( unsigned int j = 0; j < size; j++ ) {
00114 strm_text << data[j] << " ";
00115 }
00116 strm_text << std::ends;
00117 const string sdata = strm_text.str();
00118
00119 XmlTextNode * node = XmlController::m_xml_doc -> createTextNode ( sdata );
00120 element -> appendChild ( *node );
00121 tag -> appendChild ( *element );
00122 }
00123 }
00124
00125 void
00126 NTupleXML::
00127 createChildren3D ( XmlElement * tag, const DataSource & ntuple )
00128 {
00129 XmlTextNode * node
00130 = XmlController::m_xml_doc -> createTextNode ( "XData" );
00131 tag -> appendChild ( *node );
00132 }
00133
00136 string NTupleXML::getName ( const XmlElement & tag ) const
00137 {
00138 string name;
00139 bool ok = tag.attribute ( m_name, name );
00140 #ifndef STRING_CLEAR_DEFECT // change to ifdef when M4 macro is implemented
00141 if ( ! ok ) name.erase ( name.begin(), name.end() );
00142 #else
00143 if ( ! ok ) name.clear();
00144 #endif
00145
00146 return name;
00147 }
00148
00149 const DataSource * NTupleXML::getObject ( const XmlElement & tag )
00150 {
00151 list < XmlElement * > nodelist;
00152 tag.fillNodeList ( m_data, nodelist );
00153 unsigned int size = nodelist.size ();
00154
00155
00156 vector < vector < double > > columns ( size );
00157 vector < string > labels ( size );
00158
00159 list < XmlElement * >::const_iterator first = nodelist.begin ();
00160 while ( first != nodelist.end() ) {
00161 XmlElement * elem = *first++;
00162 int column;
00163 bool ok = elem -> attribute ( m_col_name, column );
00164
00165 string label;
00166 ok = elem -> attribute ( m_label, label );
00167 labels[column] = label;
00168
00169 const string & text = elem -> getText ();
00170 istringstream stream ( text );
00171
00172 copy ( istream_iterator<double> ( stream ),
00173 istream_iterator<double> (), back_inserter ( columns[column] ) );
00174 }
00175
00176 NTuple * ntuple = new NTuple ();
00177 for ( unsigned int i = 0; i < size; i++ ) {
00178 ntuple -> addColumn ( labels[i], columns[i] );
00179 }
00180
00181 return ntuple;
00182 }