NTupleXML.cxx
Go to the documentation of this file.
1 
12 // for truncation warning
13 #ifdef _MSC_VER
14 #include "msdevstudio/MSconfig.h"
15 #endif
16 
17 #include "NTupleXML.h"
18 
19 #include "XmlController.h"
20 #include "XmlDocument.h"
21 #include "XmlElement.h"
22 #include "XmlTextNode.h"
23 
26 #include "datasrcs/NTuple.h"
27 #include "pattern/string_convert.h"
28 
29 #include <iterator>
30 
31 
32 #include <sstream>
33 using std::istringstream;
34 
35 using std::istream_iterator;
36 using std::list;
37 using std::string;
38 using std::vector;
39 
40 using namespace hippodraw;
41 
43  : BaseXML ( "NTuple", controller ),
44  m_name ( "name" ),
45  m_col_name ( "column" ),
46  m_data ( "NTupleData" ),
47  m_label ( "label" )
48 {
49 }
50 
52 {
54 
55  setAttributes ( tag, ntuple );
56 
57  createChildren ( tag, ntuple);
58 
59  return tag;
60 }
61 
62 
63 void
65 setAttributes ( XmlElement * tag, const DataSource & ntuple )
66 {
67  const void * addr = reinterpret_cast < const void * > ( & ntuple );
68  int id = m_controller -> getId ( addr );
69  setId ( *tag, id );
70 
71  const string & full_name = ntuple.getName ();
72  tag -> setAttribute ( m_name, full_name );
73 }
74 
75 namespace dp = hippodraw::DataPoint2DTuple;
76 
77 void
79 createChildren ( XmlElement * tag, const DataSource & ntuple )
80 {
81  unsigned int columns = ntuple.columns ();
82  if ( columns == dp::SIZE ) createChildren2D ( tag, ntuple );
83  else createChildren2D ( tag, ntuple );
84 }
85 
86 void
88 createChildren2D ( XmlElement * tag, const DataSource & ntuple )
89 {
90  static unsigned int cols []
91  = { dp::X, dp::Y, dp::XERR, dp::YERR };
92 
93  for ( unsigned int i = 0; i < dp::SIZE; i++ ) {
94  XmlElement * element
96  element -> setAttribute ( m_col_name, i );
97  const string & label = ntuple.getLabelAt ( cols[i] );
98  element -> setAttribute ( m_label, label );
99 
100  const vector < double > & data = ntuple.getColumn ( cols[i] );
101  unsigned int size = ntuple.rows ();
102 
103  string text;
104  for ( unsigned int j = 0; j < size; j++ ) {
105  text += String::convert ( data[j] );
106  text += " ";
107  }
108 
109  XmlTextNode * node
110  = XmlController::m_xml_doc -> createTextNode ( text );
111  element -> appendChild ( *node );
112  delete node;
113 
114  tag -> appendChild ( *element );
115  delete element;
116  }
117 }
118 
121 void
124 {
125  XmlTextNode * node
126  = XmlController::m_xml_doc -> createTextNode ( "XData" );
127  tag -> appendChild ( *node );
128  delete node;
129 }
130 
133 string NTupleXML::getName ( const XmlElement & tag ) const
134 {
135  string name;
136  bool ok = tag.attribute ( m_name, name );
137 #ifndef STRING_CLEAR_DEFECT // change to ifdef when M4 macro is implemented
138  if ( ! ok ) name.erase ( name.begin(), name.end() );
139 #else
140  if ( ! ok ) name.clear();
141 #endif
142 
143  return name;
144 }
145 
147 {
148  list < XmlElement * > nodelist;
149  tag.fillNodeList ( m_data, nodelist );
150  unsigned int size = nodelist.size ();
151 
152  // tempoary holding area as no guarantee data comes back in order
153  vector < vector < double > > columns ( size );
154  vector < string > labels ( size );
155 
156  list < XmlElement * >::const_iterator first = nodelist.begin ();
157  while ( first != nodelist.end() ) {
158  XmlElement * elem = *first++;
159  int column;
160  bool ok = elem -> attribute ( m_col_name, column );
161 
162  string label;
163  ok = elem -> attribute ( m_label, label );
164  labels[column] = label;
165 
166  const string & text = elem -> getText ();
167  istringstream stream ( text );
168 
169  copy ( istream_iterator<double> ( stream ),
170  istream_iterator<double> (), back_inserter ( columns[column] ) );
171  }
172 
173  NTuple * ntuple = new NTuple ();
174  for ( unsigned int i = 0; i < size; i++ ) {
175  ntuple -> addColumn ( labels[i], columns[i] );
176  }
177 
178  return ntuple;
179 }
NTupleXML(const NTupleXML &)
A private copy constructor in order to avoid copying.
const DataSource * getObject(const XmlElement &tag)
Returns the DataSource corresponding to XmlElement.
Definition: NTupleXML.cxx:146
XmlElement * createElement()
Creates a new element node.
Definition: BaseXML.cxx:43
unsigned int i
std::string m_col_name
the attribute name for the column index.
Definition: NTupleXML.h:47
virtual bool attribute(const std::string &name, bool &value) const =0
Sets value to the attribute name&#39;s value.
XmlDocument class interface.
void * data(numeric::array arr)
Definition: num_util.cpp:389
XmlController class interface.
column
The column indices for 2 dimension data point tuple.
A pure virtual base class of XML element wrapper.
Definition: XmlElement.h:30
XmlTextNode class interface.
The namespace for conversion to string.
virtual const std::vector< double > & getColumn(const std::string &name) const
Returns the data in the column with label name.
Definition: DataSource.cxx:243
void setId(XmlElement &tag, int id)
Sets the unique identification of the object.
Definition: BaseXML.cxx:64
hippodraw::NTuple class interface.
void createChildren(XmlElement *tag, const DataSource &ntuple)
Creates the Dom text node children.
Definition: NTupleXML.cxx:79
bool ok
Definition: CanvasView.cxx:163
error on X or half bin width
virtual void fillNodeList(const std::string &tagName, std::list< XmlElement * > &nodeList) const =0
Fills the nodeList with elements with tag name tagName.
intp size(numeric::array arr)
Definition: num_util.cpp:296
std::string m_name
The tag name for the name attribute.
Definition: NTupleXML.h:43
string convert(int i)
Converts an integer to a string.
std::string getName(const XmlElement &tag) const
Returns the name of the NTuple object.
Definition: NTupleXML.cxx:133
A pure virtual base class of XML DOM Text node wrapper.
Definition: XmlTextNode.h:28
XmlController * m_controller
The singleton XML controller object.
Definition: BaseXML.h:60
XmlElement class interface.
A DataSource class implemented with std::vector&lt;double&gt; to store the column data. ...
Definition: NTuple.h:33
std::string m_data
The tag name of the column data.
Definition: NTupleXML.h:51
void setAttributes(XmlElement *tag, const DataSource &ntuple)
Sets the attributes.
Definition: NTupleXML.cxx:65
static XmlDocument * m_xml_doc
The current document being generated or read.
virtual unsigned int rows() const =0
Returns the number of rows.
void createChildren2D(XmlElement *tag, const DataSource &ntuple)
Creates the Dom text node children for 1D histograms.
Definition: NTupleXML.cxx:88
NTupleXML class interface.
unsigned int columns() const
Returns the number of columns or data arrays available from this DataSource.
Definition: DataSource.h:458
A base class of XML element controllers.
Definition: BaseXML.h:35
hippodraw::DataPointTuple namespace interface
hippodraw::NTupleController class interface
void createChildren3D(XmlElement *tag, const DataSource &ntuple)
Creates the Dom text node children for 2D histograms.
Definition: NTupleXML.cxx:123
A base class that is the interface between GUI and the XML serialization and deserialization.
Definition: XmlController.h:53
virtual const std::string & getLabelAt(unsigned int index) const
Returns the label for the column at index index.
Definition: DataSource.cxx:179
const std::string & getName() const
Returns the name of the data source.
Definition: DataSource.cxx:136
std::string m_label
The attribute name of the column label.
Definition: NTupleXML.h:55
Base class for DataSource.
Definition: DataSource.h:55

Generated for HippoDraw Class Library by doxygen