RootBranch.cxx
Go to the documentation of this file.
1 
12 #ifdef _MSC_VER
13 #include "w32pragma.h"
14 #endif
15 #include "RootBranch.h"
16 
17 #include "axes/Range.h"
18 
19 #include <cassert>
20 
21 using std::string;
22 using std::vector;
23 
24 using namespace hippodraw;
25 
30 RootBranch ( TBranch * branch )
31  : m_branch ( branch ),
32  m_vector_double_data ( 0 ),
33  m_vector_float_data ( 0 ),
34  m_vector_int_data ( 0 ),
35  m_vector_uint_data ( 0 ),
36  m_vector_ulong64_data ( 0 ),
37  m_releventIndex ( -1 ),
38  m_branch_set ( false ),
39  m_useable ( true )
40 {
41  TObjArray * leaves = branch -> GetListOfLeaves ();
42  m_number_leaves = leaves -> GetEntries ();
43 
44  if ( m_number_leaves == 1 )
45  {
46  TObject * object = leaves -> At ( 0 );
47  m_leaf = dynamic_cast < TLeaf * > ( object );
48  assert ( m_leaf != 0 );
49  const string type = m_leaf -> GetTypeName ();
50 
51  if ( type == "Double_t" ) m_leaf_type = RootData::Double;
52  else if ( type == "Float_t" ) m_leaf_type = RootData::Float;
53  else if ( type == "Int_t" ) m_leaf_type = RootData::Int;
54  else if ( type == "Short_t" ) m_leaf_type = RootData::Short;
55  else if ( type == "UShort_t" ) m_leaf_type = RootData::UShort;
56  else if ( type == "UInt_t" ) m_leaf_type = RootData::UInt;
57  else if ( type == "Long64_t" ) m_leaf_type = RootData::Long64;
58  else if ( type == "ULong64_t" ) m_leaf_type = RootData::ULong64;
59  else {
60  m_useable = false;
61  }
62  m_number_elements = m_leaf -> GetLen();
63  }
64  try {
65  initShape ( m_leaf -> GetTitle() );
66  }
67  catch ( ... ) {
68  m_useable = false;
69  }
70 }
71 
74  : m_branch ( 0 ),
75  m_releventIndex ( -1 ),
76  m_branch_set ( false )
77 {
78 }
79 
82 {
83 }
84 
85 unsigned int
87 size () const
88 {
89  assert ( false ); // branch doesn't know its size yet
90 
91  return 0;
92 }
93 
94 
95 bool
97 empty () const
98 {
99  return false;
100 }
101 
102 bool
104 isFilled ( ) const
105 {
106  return true;
107 }
108 
109 bool
112 {
113  return ! ( m_number_elements == 1 );
114 }
115 
116 
117 int
120 {
121  return m_number_elements;
122 }
123 
124 TBranch *
127 {
128  return m_branch;
129 }
130 
131 void
133 initShape ( const char* title )
134 {
135  m_shape.clear ();
136 
137  string s( title );
138 
139  //Creating the list of dropped delimiters.
140  boost::char_separator< char > sep( "][" );
141 
142  // A tokenizer with above dropped delimiters.
143  typedef boost::tokenizer< boost::char_separator< char > > tokenizer;
144  tokenizer tok( s, sep );
145 
146  // Start extracting the dimension sizes.
147  for( tokenizer::iterator tok_iter = tok.begin();
148  tok_iter != tok.end();
149  ++tok_iter )
150  if( tok_iter != tok.begin() ) {
151  unsigned int value = boost::lexical_cast< unsigned int >( *tok_iter );
152  m_shape.push_back( value );
153  }
154 }
155 
160 void
163 {
164  if( m_number_elements == 1 )
165  {
166  switch ( m_leaf_type )
167  {
168  case RootData::Double:
169  m_branch -> SetAddress ( & m_double_data );
171  break;
172  case RootData::Float:
173  m_branch -> SetAddress ( & m_float_data );
175  break;
176  case RootData::Int:
177  m_branch -> SetAddress ( & m_int_data );
179  break;
180  case RootData::UInt:
181  m_branch -> SetAddress ( & m_uint_data );
183  break;
184  case RootData::ULong64:
185  m_branch -> SetAddress ( & m_ulong64_data );
187  break;
188  default:
189  assert ( false );
190  break;
191  }
192  }
193 
194  else if( m_number_elements > 1 )
195  {
196  switch ( m_leaf_type )
197  {
198  case RootData::Double:
199  m_vector_double_data = new Double_t [ m_number_elements ];
200  m_branch -> SetAddress ( m_vector_double_data );
201  break;
202  case RootData::Float:
203  m_vector_float_data = new Float_t [ m_number_elements ];
204  m_branch -> SetAddress ( m_vector_float_data );
205  break;
206  case RootData::Int:
207  m_vector_int_data = new Int_t [ m_number_elements ];
208  m_branch -> SetAddress ( m_vector_int_data );
209  break;
210  case RootData::UInt:
211  m_vector_uint_data = new UInt_t [ m_number_elements ];
212  m_branch -> SetAddress ( m_vector_uint_data );
213  break;
214  case RootData::Short:
215  m_vector_short_data = new Short_t [ m_number_elements ];
216  m_branch -> SetAddress ( m_vector_short_data );
217  break;
218  case RootData::UShort:
219  m_vector_ushort_data = new UShort_t [ m_number_elements ];
220  m_branch -> SetAddress ( m_vector_ushort_data );
221  break;
222  case RootData::Long64:
223  m_vector_long64_data = new Long64_t [ m_number_elements ];
224  m_branch -> SetAddress ( m_vector_long64_data );
225  break;
226  case RootData::ULong64:
227  m_vector_ulong64_data = new ULong64_t [ m_number_elements ];
228  m_branch -> SetAddress ( m_vector_ulong64_data );
229  break;
230  default:
231  assert ( false );
232  break;
233  }
234 
235  }
236 
237  m_branch_set = true;
238 }
239 
240 unsigned int
242 getRank () const
243 {
244  unsigned int size = m_shape.size();
245 
246  return size;
247 }
248 
249 const vector < int > &
252 {
253  return m_shape;
254 }
255 
256 void
258 setReleventIndex( const std::vector< unsigned int > & index )
259 {
260  // This function makes sense only for multi dimenstional data
261  assert( getRank () > 0 );
262 
263  // Index should completely specify the entry of the data in the matrix
264  // So it should have as many enteries as the dimensions of the data
265  assert( getRank () == index.size() );
266 
267  // Clear old relevent entry and put in this new ones.
268  m_releventIndex = index[0];
269  for ( unsigned int d = 1; d < getRank (); d++ ) {
270  m_releventIndex = m_releventIndex * m_shape [ d ] + index[ d ];
271  }
272 }
273 
274 double
276 valueAt ( unsigned int row ) const
277 {
278  if ( m_branch_set == false ) setBranchAddress ();
279 
280  Int_t entry = row;
281  //Int_t bytes =
282  m_branch -> GetEntry ( entry, 1 );
283 
284  double value = -1;
285  if( m_number_elements == 1 )
286  {
287  switch ( m_leaf_type )
288  {
289  case RootData::Double:
290  value = m_double_data;
291  break;
292  case RootData::Float:
293  value = m_float_data;
294  break;
295  case RootData::Int:
296  value = m_int_data;
297  break;
298  case RootData::UInt:
299  value = m_uint_data;
300  break;
301  case RootData::Short:
302  value = m_short_data;
303  break;
304  case RootData::UShort:
305  value = m_ushort_data;
306  break;
307  case RootData::Long64:
308  value = m_long64_data;
309  break;
310  case RootData::ULong64:
311  value = m_ulong64_data;
312  break;
313  default:
314  assert ( false );
315  break;
316  }
317  }
318  else
319  {
321  {
322  // Temporary patch so other columns can be wriiten
323 // std::string what ( "RootBranch: `" );
324 // what += m_leaf -> GetTitle ();
325 // what += "' indices not set properly.";
326 // throw DataSourceException ( what );
327  return 0.0;
328  }
329 
330  // The basic conversion formulae
331  switch ( m_leaf_type )
332  {
333  case RootData::Double:
335  break;
336  case RootData::Float:
338  break;
339  case RootData::Int:
341  break;
342  case RootData::UInt:
344  break;
345  case RootData::Short:
347  break;
348  case RootData::UShort:
350  break;
351  case RootData::Long64:
353  break;
354  case RootData::ULong64:
356  break;
357  default:
358  assert ( false );
359  }
360  }
361 
362  return value;
363 }
364 
365 double *
367 doubleArrayAt ( unsigned int row )
368 {
369  if ( m_branch_set == false ) setBranchAddress ();
370  Int_t entry = row;
371  // Int_t bytes =
372  m_branch -> GetEntry ( entry, 1 );
373 
374  if ( m_leaf_type != RootData::Double ) { // need to convert
375  if ( m_vector_double_data == 0 ) { // memory not yet allocated
376  m_vector_double_data = new Double_t [ m_number_elements ];
377  }
378  switch ( m_leaf_type ) {
379  case RootData::Float :
380  std::copy ( m_vector_float_data,
383  break;
384  case RootData::Int :
385  std::copy ( m_vector_int_data,
388  break;
389  case RootData::UInt :
390  std::copy ( m_vector_uint_data,
393  break;
394  case RootData::Short :
395  std::copy ( m_vector_short_data,
398  break;
399  case RootData::UShort :
400  std::copy ( m_vector_ushort_data,
403  break;
404  case RootData::Long64 :
405  std::copy ( m_vector_long64_data,
408  break;
409  case RootData::ULong64 :
410  std::copy ( m_vector_ulong64_data,
413  break;
414  default:
415  break;
416  }
417  }
418 
419  return m_vector_double_data;
420 }
421 
422 float *
424 floatArrayAt ( unsigned int row )
425 {
426  if ( m_branch_set == false ) setBranchAddress ();
427  Int_t entry = row;
428  // Int_t bytes =
429  m_branch -> GetEntry ( entry, 1 );
430 
431  return m_vector_float_data;
432 }
433 
434 int *
436 intArrayAt ( unsigned int row )
437 {
438  if ( m_branch_set == false ) setBranchAddress ();
439  Int_t entry = row;
440  // Int_t bytes =
441  m_branch -> GetEntry ( entry, 1 );
442 
443  return m_vector_int_data;
444 }
445 
446 unsigned int *
448 uintArrayAt ( unsigned int row )
449 {
450  if ( m_branch_set == false ) setBranchAddress ();
451  Int_t entry = row;
452  // Int_t_bytes =
453  m_branch -> GetEntry ( entry, 1 );
454 
455  return m_vector_uint_data;
456 }
457 
460 getType () const
461 {
462  return m_leaf_type;
463 }
464 
465 bool
467 isUseable () const
468 {
469  return m_useable;
470 }
~RootBranch()
The destructor.
Definition: RootBranch.cxx:81
Type
The type of data on the branch.
Definition: RootDataType.h:25
RootBranch()
The default constructor.
Definition: RootBranch.cxx:73
bool isMultiDimensional() const
Returns true if data sitting in the rows of this branch is and array.
Definition: RootBranch.cxx:111
UShort_t m_ushort_data
The address of the following variable is given to ROOT TTree as the branch address for this branch if...
Definition: RootBranch.h:100
unsigned int type.
Definition: RootDataType.h:29
int m_number_elements
The number of elements in the Leaf array or 0 if not an array.
Definition: RootBranch.h:65
double * doubleArrayAt(unsigned int row)
Returns pointer to a double array in given row.
Definition: RootBranch.cxx:367
ULong64_t * m_vector_ulong64_data
The address of the following variable is given to ROOT TTree as the branch address for this branch if...
Definition: RootBranch.h:171
unsigned 16 bit integer.
Definition: RootDataType.h:33
Long64_t m_long64_data
The address of the following variable is given to ROOT TTree as the branch address for this branch if...
Definition: RootBranch.h:106
unsigned int * uintArrayAt(unsigned int row)
Returns pointer to a unsigned int array type in given row.
Definition: RootBranch.cxx:448
Double_t m_double_data
The address of the following variable is given to ROOT TTree as the branch address for this branch if...
Definition: RootBranch.h:71
void setReleventIndex(const std::vector< unsigned int > &index)
In case we are dealing with multidimensional data in rows of this branch we would like to deal with o...
Definition: RootBranch.cxx:258
int * intArrayAt(unsigned int row)
Returns pointer to a int array type in given row.
Definition: RootBranch.cxx:436
signed 64 bit long.
Definition: RootDataType.h:30
Float_t * m_vector_float_data
The address of the following variable is given to ROOT TTree as the branch address for this branch if...
Definition: RootBranch.h:128
hippodraw::RootData::Type m_leaf_type
The ROOT type name for this branch if single TLeaf is on this branch.
Definition: RootBranch.h:56
hippodraw::RootData::Type getType() const
Returns the type of the data in this branch.
Definition: RootBranch.cxx:460
void setBranchAddress() const
Sets the address where the ROOT TBranch will put its data.
Definition: RootBranch.cxx:162
UShort_t * m_vector_ushort_data
The address of the following variable is given to ROOT TTree as the branch address for this branch if...
Definition: RootBranch.h:157
unsigned int getRank() const
Gives the dimensionality of the data stored in each row of this branch.
Definition: RootBranch.cxx:242
Long64_t * m_vector_long64_data
The address of the following variable is given to ROOT TTree as the branch address for this branch if...
Definition: RootBranch.h:164
bool m_useable
Set to false if the branch is not usable.
Definition: RootBranch.h:194
Short_t * m_vector_short_data
The address of the following variable is given to ROOT TTree as the branch address for this branch if...
Definition: RootBranch.h:150
int m_number_leaves
The number of TLeaf objects on this TBranch.
Definition: RootBranch.h:60
unsigned int size() const
Returns the size of the slice for the next to last dimension.
Definition: RootBranch.cxx:87
std::vector< int > m_shape
In case the data quantity we are dealing with is a vector/matrix store its dimensions of each axis in...
Definition: RootBranch.h:176
PyArray_TYPES type(numeric::array arr)
Definition: num_util.cpp:249
int m_releventIndex
In case the data quantity is vector/matrix we would like just to take one element out of it...
Definition: RootBranch.h:182
bool empty() const
Returns true, if RootBranch is empty, i.e.
Definition: RootBranch.cxx:97
Short_t m_short_data
The address of the following variable is given to ROOT TTree as the branch address for this branch if...
Definition: RootBranch.h:94
void initShape(const char *title)
From the root title, which is of format &quot;name[x][y][z]&quot;, gets the sizes of various dimension...
Definition: RootBranch.cxx:133
signed 16 bit integer.
Definition: RootDataType.h:32
int numberOfElements() const
Number of elements in this branch.
Definition: RootBranch.cxx:119
TLeaf * m_leaf
The ROOT TLeaf which described the data in this branch.
Definition: RootBranch.h:51
The ROOT TLeaf class.
const std::vector< int > & getShape()
Vector of the number of entries in the multidimensional data.
Definition: RootBranch.cxx:251
hippodraw::RootBranch class interface.
UInt_t * m_vector_uint_data
The address of the following variable is given to ROOT TTree as the branch address for this branch if...
Definition: RootBranch.h:142
unsigned 64 bit long.
Definition: RootDataType.h:31
ULong64_t m_ulong64_data
The address of the following variable is given to ROOT TTree as the branch address for this branch if...
Definition: RootBranch.h:112
double valueAt(unsigned int row) const
Returns the value of the leaf at index row.
Definition: RootBranch.cxx:276
hippodraw::Range class interface
UInt_t m_uint_data
The address of the following variable is given to ROOT TTree as the branch address for this branch if...
Definition: RootBranch.h:89
return index
Definition: PickTable.cxx:182
bool isUseable() const
Returns true if the branch is usable.
Definition: RootBranch.cxx:467
TBranch * getTBranch()
Returns the TBranch which was used to initialize this branch.
Definition: RootBranch.cxx:126
Int_t m_int_data
The address of the following variable is given to ROOT TTree as the branch address for this branch if...
Definition: RootBranch.h:83
bool m_branch_set
Set true when branch address has been set.
Definition: RootBranch.h:186
float * floatArrayAt(unsigned int row)
Returns pointer to a float array type in given row.
Definition: RootBranch.cxx:424
Int_t * m_vector_int_data
The address of the following variable is given to ROOT TTree as the branch address for this branch if...
Definition: RootBranch.h:135
bool isFilled() const
Definition: RootBranch.cxx:104
TBranch * m_branch
The ROOT TBranch from which the data will be obtained.
Definition: RootBranch.h:47
Double_t * m_vector_double_data
The address of the following variable is given to ROOT TTree as the branch address for this branch if...
Definition: RootBranch.h:121
Float_t m_float_data
The address of the following variable is given to ROOT TTree as the branch address for this branch if...
Definition: RootBranch.h:77
The ROOT TBranch class.

Generated for HippoDraw Class Library by doxygen