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 }

Generated for HippoDraw Class Library by doxygen