RootBranch.cxx
Go to the documentation of this file.
1 
12 #include "RootBranch.h"
13 
14 #include "axes/Range.h"
15 
16 #include <cassert>
17 
18 using std::string;
19 using std::vector;
20 
21 using namespace hippodraw;
22 
27 RootBranch ( TBranch * branch )
28  : m_branch ( branch ),
29  m_vector_double_data ( 0 ),
30  m_vector_float_data ( 0 ),
31  m_vector_int_data ( 0 ),
32  m_vector_uint_data ( 0 ),
33  m_vector_ulong64_data ( 0 ),
34  m_releventIndex ( -1 ),
35  m_branch_set ( false ),
36  m_useable ( true )
37 {
38  TObjArray * leaves = branch -> GetListOfLeaves ();
39  m_number_leaves = leaves -> GetEntries ();
40 
41  if ( m_number_leaves == 1 )
42  {
43  TObject * object = leaves -> At ( 0 );
44  m_leaf = dynamic_cast < TLeaf * > ( object );
45  assert ( m_leaf != 0 );
46  const string type = m_leaf -> GetTypeName ();
47 
48  if ( type == "Double_t" ) m_leaf_type = RootData::Double;
49  else if ( type == "Float_t" ) m_leaf_type = RootData::Float;
50  else if ( type == "Int_t" ) m_leaf_type = RootData::Int;
51  else if ( type == "Short_t" ) m_leaf_type = RootData::Short;
52  else if ( type == "UShort_t" ) m_leaf_type = RootData::UShort;
53  else if ( type == "UInt_t" ) m_leaf_type = RootData::UInt;
54  else if ( type == "Long64_t" ) m_leaf_type = RootData::Long64;
55  else if ( type == "ULong64_t" ) m_leaf_type = RootData::ULong64;
56  else {
57  m_useable = false;
58  }
59  m_number_elements = m_leaf -> GetLen();
60  }
61  try {
62  initShape ( m_leaf -> GetTitle() );
63  }
64  catch ( ... ) {
65  m_useable = false;
66  }
67 }
68 
71  : m_branch ( 0 ),
72  m_releventIndex ( -1 ),
73  m_branch_set ( false )
74 {
75 }
76 
79 {
80 }
81 
82 unsigned int
84 size () const
85 {
86  assert ( false ); // branch doesn't know its size yet
87 
88  return 0;
89 }
90 
91 
92 bool
94 empty () const
95 {
96  return false;
97 }
98 
99 bool
101 isFilled ( ) const
102 {
103  return true;
104 }
105 
106 bool
109 {
110  return ! ( m_number_elements == 1 );
111 }
112 
113 
114 int
117 {
118  return m_number_elements;
119 }
120 
121 TBranch *
124 {
125  return m_branch;
126 }
127 
128 void
130 initShape ( const char* title )
131 {
132  m_shape.clear ();
133 
134  string s( title );
135 
136  //Creating the list of dropped delimiters.
137  boost::char_separator< char > sep( "][" );
138 
139  // A tokenizer with above dropped delimiters.
140  typedef boost::tokenizer< boost::char_separator< char > > tokenizer;
141  tokenizer tok( s, sep );
142 
143  // Start extracting the dimension sizes.
144  for( tokenizer::iterator tok_iter = tok.begin();
145  tok_iter != tok.end();
146  ++tok_iter )
147  if( tok_iter != tok.begin() ) {
148  unsigned int value = boost::lexical_cast< unsigned int >( *tok_iter );
149  m_shape.push_back( value );
150  }
151 }
152 
157 void
160 {
161  if( m_number_elements == 1 )
162  {
163  switch ( m_leaf_type )
164  {
165  case RootData::Double:
166  m_branch -> SetAddress ( & m_double_data );
168  break;
169  case RootData::Float:
170  m_branch -> SetAddress ( & m_float_data );
172  break;
173  case RootData::Int:
174  m_branch -> SetAddress ( & m_int_data );
176  break;
177  case RootData::UInt:
178  m_branch -> SetAddress ( & m_uint_data );
180  break;
181  case RootData::ULong64:
182  m_branch -> SetAddress ( & m_ulong64_data );
184  break;
185  default:
186  assert ( false );
187  break;
188  }
189  }
190 
191  else if( m_number_elements > 1 )
192  {
193  switch ( m_leaf_type )
194  {
195  case RootData::Double:
196  m_vector_double_data = new Double_t [ m_number_elements ];
197  m_branch -> SetAddress ( m_vector_double_data );
198  break;
199  case RootData::Float:
200  m_vector_float_data = new Float_t [ m_number_elements ];
201  m_branch -> SetAddress ( m_vector_float_data );
202  break;
203  case RootData::Int:
204  m_vector_int_data = new Int_t [ m_number_elements ];
205  m_branch -> SetAddress ( m_vector_int_data );
206  break;
207  case RootData::UInt:
208  m_vector_uint_data = new UInt_t [ m_number_elements ];
209  m_branch -> SetAddress ( m_vector_uint_data );
210  break;
211  case RootData::Short:
212  m_vector_short_data = new Short_t [ m_number_elements ];
213  m_branch -> SetAddress ( m_vector_short_data );
214  break;
215  case RootData::UShort:
216  m_vector_ushort_data = new UShort_t [ m_number_elements ];
217  m_branch -> SetAddress ( m_vector_ushort_data );
218  break;
219  case RootData::Long64:
220  m_vector_long64_data = new Long64_t [ m_number_elements ];
221  m_branch -> SetAddress ( m_vector_long64_data );
222  break;
223  case RootData::ULong64:
224  m_vector_ulong64_data = new ULong64_t [ m_number_elements ];
225  m_branch -> SetAddress ( m_vector_ulong64_data );
226  break;
227  default:
228  assert ( false );
229  break;
230  }
231 
232  }
233 
234  m_branch_set = true;
235 }
236 
237 unsigned int
239 getRank () const
240 {
241  unsigned int size = m_shape.size();
242 
243  return size;
244 }
245 
246 const vector < int > &
249 {
250  return m_shape;
251 }
252 
253 void
255 setReleventIndex( const std::vector< unsigned int > & index )
256 {
257  // This function makes sense only for multi dimenstional data
258  assert( getRank () > 0 );
259 
260  // Index should completely specify the entry of the data in the matrix
261  // So it should have as many enteries as the dimensions of the data
262  assert( getRank () == index.size() );
263 
264  // Clear old relevent entry and put in this new ones.
265  m_releventIndex = index[0];
266  for ( unsigned int d = 1; d < getRank (); d++ ) {
267  m_releventIndex = m_releventIndex * m_shape [ d ] + index[ d ];
268  }
269 }
270 
271 double
273 valueAt ( unsigned int row ) const
274 {
275  if ( m_branch_set == false ) setBranchAddress ();
276 
277  Int_t entry = row;
278  //Int_t bytes =
279  m_branch -> GetEntry ( entry, 1 );
280 
281  double value = -1;
282  if( m_number_elements == 1 )
283  {
284  switch ( m_leaf_type )
285  {
286  case RootData::Double:
287  value = m_double_data;
288  break;
289  case RootData::Float:
290  value = m_float_data;
291  break;
292  case RootData::Int:
293  value = m_int_data;
294  break;
295  case RootData::UInt:
296  value = m_uint_data;
297  break;
298  case RootData::Short:
299  value = m_short_data;
300  break;
301  case RootData::UShort:
302  value = m_ushort_data;
303  break;
304  case RootData::Long64:
305  value = m_long64_data;
306  break;
307  case RootData::ULong64:
308  value = m_ulong64_data;
309  break;
310  default:
311  assert ( false );
312  break;
313  }
314  }
315  else
316  {
318  {
319  // Temporary patch so other columns can be wriiten
320 // std::string what ( "RootBranch: `" );
321 // what += m_leaf -> GetTitle ();
322 // what += "' indices not set properly.";
323 // throw DataSourceException ( what );
324  return 0.0;
325  }
326 
327  // The basic conversion formulae
328  switch ( m_leaf_type )
329  {
330  case RootData::Double:
332  break;
333  case RootData::Float:
335  break;
336  case RootData::Int:
338  break;
339  case RootData::UInt:
341  break;
342  case RootData::Short:
344  break;
345  case RootData::UShort:
347  break;
348  case RootData::Long64:
350  break;
351  case RootData::ULong64:
353  break;
354  default:
355  assert ( false );
356  }
357  }
358 
359  return value;
360 }
361 
362 double *
364 doubleArrayAt ( unsigned int row )
365 {
366  if ( m_branch_set == false ) setBranchAddress ();
367  Int_t entry = row;
368  // Int_t bytes =
369  m_branch -> GetEntry ( entry, 1 );
370 
371  if ( m_leaf_type != RootData::Double ) { // need to convert
372  if ( m_vector_double_data == 0 ) { // memory not yet allocated
373  m_vector_double_data = new Double_t [ m_number_elements ];
374  }
375  switch ( m_leaf_type ) {
376  case RootData::Float :
377  std::copy ( m_vector_float_data,
380  break;
381  case RootData::Int :
382  std::copy ( m_vector_int_data,
385  break;
386  case RootData::UInt :
387  std::copy ( m_vector_uint_data,
390  break;
391  case RootData::Short :
392  std::copy ( m_vector_short_data,
395  break;
396  case RootData::UShort :
397  std::copy ( m_vector_ushort_data,
400  break;
401  case RootData::Long64 :
402  std::copy ( m_vector_long64_data,
405  break;
406  case RootData::ULong64 :
407  std::copy ( m_vector_ulong64_data,
410  break;
411  default:
412  break;
413  }
414  }
415 
416  return m_vector_double_data;
417 }
418 
419 float *
421 floatArrayAt ( unsigned int row )
422 {
423  if ( m_branch_set == false ) setBranchAddress ();
424  Int_t entry = row;
425  // Int_t bytes =
426  m_branch -> GetEntry ( entry, 1 );
427 
428  return m_vector_float_data;
429 }
430 
431 int *
433 intArrayAt ( unsigned int row )
434 {
435  if ( m_branch_set == false ) setBranchAddress ();
436  Int_t entry = row;
437  // Int_t bytes =
438  m_branch -> GetEntry ( entry, 1 );
439 
440  return m_vector_int_data;
441 }
442 
443 unsigned int *
445 uintArrayAt ( unsigned int row )
446 {
447  if ( m_branch_set == false ) setBranchAddress ();
448  Int_t entry = row;
449  // Int_t_bytes =
450  m_branch -> GetEntry ( entry, 1 );
451 
452  return m_vector_uint_data;
453 }
454 
457 getType () const
458 {
459  return m_leaf_type;
460 }
461 
462 bool
464 isUseable () const
465 {
466  return m_useable;
467 }

Generated for HippoDraw Class Library by doxygen