Logo ROOT   6.12/07
Reference Guide
TArrayBranch.hxx
Go to the documentation of this file.
1 // Author: Enrico Guiraud CERN 10/2017
2 
3 /*************************************************************************
4  * Copyright (C) 1995-2016, Rene Brun and Fons Rademakers. *
5  * All rights reserved. *
6  * *
7  * For the licensing terms see $ROOTSYS/LICENSE. *
8  * For the list of contributors see $ROOTSYS/README/CREDITS. *
9  *************************************************************************/
10 
11 #ifndef ROOT_TDF_TARRAYBRANCH
12 #define ROOT_TDF_TARRAYBRANCH
13 
14 #include "TTreeReaderArray.h"
15 #include <iterator>
16 
17 namespace ROOT {
18 namespace Experimental {
19 namespace TDF {
20 /// When using TDataFrame to read data from a ROOT file, users can specify that the type of a branch is TArrayBranch<T>
21 /// to indicate the branch is a c-style array, an STL array or any other type that can/must be accessed through a
22 /// TTreeReaderArray<T> (as opposed to a TTreeReaderValue<T>).
23 /// Column values of type TArrayBranch perform no copy of the underlying array data and offer a minimal array-like
24 /// interface to access the array elements: either via square brackets, or with C++11 range-based for loops.
25 template <typename T>
26 class TArrayBranch {
27  TTreeReaderArray<T> *fReaderArray = nullptr; ///< Pointer to the TTreeReaderArray that actually owns the data
28 public:
31  using value_type = T;
32 
33  TArrayBranch() {} // jitted types must be default-constructible
34  TArrayBranch(TTreeReaderArray<T> &arr) : fReaderArray(&arr)
35  {
36  // trigger loading of entry into TTreeReaderArray
37  // TODO: could we load more lazily? we need to guarantee that when a TArrayBranch is constructed all data
38  // is loaded into the TTreeReaderArray because otherwise Snapshot never triggers this load.
39  // Should we have Snapshot explicitly trigger the loading instead?
40  // N.B. we would not need to trigger the load explicitly if Snapshot did not read the buffer returned by GetData
41  // directly -- e.g. if we wrote a std::vector for each c-style array in input
42  arr.At(0);
43  }
44 
45  const T &operator[](std::size_t n) const { return fReaderArray->At(n); }
46 
47  // TODO: remove the need of GetData, e.g. by writing out std::vectors instead of c-style arrays
48  T *GetData() { return static_cast<T *>(fReaderArray->GetAddress()); }
49 
50  iterator begin() { return fReaderArray->begin(); }
51  iterator end() { return fReaderArray->end(); }
52  const_iterator begin() const { return fReaderArray->cbegin(); }
53  const_iterator end() const { return fReaderArray->cend(); }
54 
55  std::size_t size() const { return fReaderArray->GetSize(); }
56 };
57 }
58 }
59 }
60 
61 #endif
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
double T(double x)
Definition: ChebyshevPol.h:34
T & At(std::size_t idx)
const T & operator[](std::size_t n) const
TArrayBranch(TTreeReaderArray< T > &arr)
typename TTreeReaderArray< T >::const_iterator const_iterator
When using TDataFrame to read data from a ROOT file, users can specify that the type of a branch is T...
TTreeReaderArray< T > * fReaderArray
Pointer to the TTreeReaderArray that actually owns the data.
void * GetAddress()
Returns the memory address of the object being read.
Extracts array data from a TTree.
Random access iterator to the elements of a TTreeReaderArray.
const_iterator cbegin() const
const Int_t n
Definition: legend1.C:16
const_iterator cend() const
typename TTreeReaderArray< T >::iterator iterator