Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TNDArray.h
Go to the documentation of this file.
1// @(#)root/hist:$Id$
2// Author: Axel Naumann, Nov 2011
3
4/*************************************************************************
5 * Copyright (C) 1995-2012, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12#ifndef ROOT_TNDArray
13#define ROOT_TNDArray
14
15#include "TObject.h"
16#include "TError.h"
17
18/** \class TNDArray
19
20N-Dim array class.
21
22Storage layout:
23Assume 3 dimensions, array sizes 2, 4 and 3 i.e. 24 bins:
24Data is stored as [0,0,0], [0,0,1], [0,0,2], [0,1,0],...
25
26fSizes stores the combined size of each bin in a dimension, i.e. in
27above example it would contain 24, 12, 3, 1.
28
29Storage is allocated lazily, only when data is written to the array.
30*/
31
32
33/** \class TNDArrayRef
34
35gives access to a sub-dimension, e.g. arr[0][1] in above
36three-dimensional example, up to an element with conversion operator
37to double: double value = arr[0][1][2];
38*/
39
40
41// Array layout:
42// nbins[0] = 2, nbins[1] = 4, nbins[2] = 3 => 24 bins
43//
44// fSizes: 24, 12, 3 [, 1
45
46class TNDArray: public TObject {
47public:
49
50 TNDArray(Int_t ndim, const Int_t *nbins, bool addOverflow = false) : fSizes()
51 {
52 TNDArray::Init(ndim, nbins, addOverflow);
53 }
54
55 virtual void Init(Int_t ndim, const Int_t* nbins, bool addOverflow = false) {
56 // Calculate fSize based on ndim dimensions, nbins for each dimension,
57 // possibly adding over- and underflow bin to each dimensions' nbins.
58 fSizes.resize(ndim + 1);
59 Int_t overBins = addOverflow ? 2 : 0;
60 fSizes[ndim] = 1;
61 for (Int_t i = 0; i < ndim; ++i) {
62 fSizes[ndim - i - 1] = fSizes[ndim - i] * (nbins[ndim - i - 1] + overBins);
63 }
64 }
65
66 virtual void Reset(Option_t* option = "") = 0;
67
68 Int_t GetNdimensions() const { return fSizes.size() - 1; }
69 Long64_t GetNbins() const { return fSizes[0]; }
70 Long64_t GetCellSize(Int_t dim) const { return fSizes[dim + 1]; }
71
72 Long64_t GetBin(const Int_t* idx) const {
73 // Get the linear bin number for each dimension's bin index
74 Long64_t bin = idx[fSizes.size() - 2];
75 for (unsigned int d = 0; d < fSizes.size() - 2; ++d) {
76 bin += fSizes[d + 1] * idx[d];
77 }
78 return bin;
79 }
80
81 virtual Double_t AtAsDouble(ULong64_t linidx) const = 0;
82 virtual void SetAsDouble(ULong64_t linidx, Double_t value) = 0;
83 virtual void AddAt(ULong64_t linidx, Double_t value) = 0;
84
85protected:
86 std::vector<Long64_t> fSizes; ///< bin count
87 ClassDefOverride(TNDArray, 2); ///< Base for n-dimensional array
88};
89
90template <typename T>
92public:
93 TNDArrayRef(const T* data, const Long64_t* sizes):
94 fData(data), fSizes(sizes) {}
95
97 if (!fData) return TNDArrayRef<T>(0, 0);
98 R__ASSERT(idx < fSizes[-1] / fSizes[0] && "index out of range!");
99 return TNDArrayRef<T>(fData + idx * fSizes[0], (fSizes[0] == 1) ? nullptr : (fSizes + 1));
100 }
101 operator T() const {
102 if (!fData) return T();
103 R__ASSERT(fSizes == nullptr && "Element operator can only be used on non-array element. Missing an operator[] level?");
104 return *fData;
105 }
106
107private:
108 const T* fData; ///< Pointer into TNDArray's fData
109 const Long64_t* fSizes; ///< Pointer into TNDArray's fSizes
110 ClassDefNV(TNDArrayRef, 0); ///< Subdimension of a TNDArray
111};
112
113template <typename T>
114class TNDArrayT: public TNDArray {
115public:
117
118 TNDArrayT(Int_t ndim, const Int_t *nbins, bool addOverflow = false) : TNDArray(ndim, nbins, addOverflow), fData() {}
119
120 void Init(Int_t ndim, const Int_t* nbins, bool addOverflow = false) override {
121 fData.clear();
122 TNDArray::Init(ndim, nbins, addOverflow);
123 }
124
125 void Reset(Option_t* /*option*/ = "") override {
126 // Reset the content
127 fData.assign(fSizes[0], T());
128 }
129
131 if (!fData) return TNDArrayRef<T>(0, 0);
132 R__ASSERT(idx < fSizes[0] / fSizes[1] && "index out of range!");
133 return TNDArrayRef<T>(fData.data() + idx * fSizes[1], fSizes.data() + 2);
134 }
135
136 T At(const Int_t* idx) const {
137 return At(GetBin(idx));
138 }
139 T& At(const Int_t* idx) {
140 return At(GetBin(idx));
141 }
142 T At(ULong64_t linidx) const {
143 if (fData.empty())
144 return T();
145 return fData[linidx];
146 }
147 T& At(ULong64_t linidx) {
148 if (fData.empty())
149 fData.resize(fSizes[0], T());
150 return fData[linidx];
151 }
152
153 Double_t AtAsDouble(ULong64_t linidx) const override {
154 if (fData.empty())
155 return 0.;
156 return fData[linidx];
157 }
158 void SetAsDouble(ULong64_t linidx, Double_t value) override {
159 if (fData.empty())
160 fData.resize(fSizes[0], T());
161 fData[linidx] = (T) value;
162 }
163 void AddAt(ULong64_t linidx, Double_t value) override {
164 if (fData.empty())
165 fData.resize(fSizes[0], T());
166 fData[linidx] += (T) value;
167 }
168
169protected:
170 std::vector<T> fData; // data
171 ClassDefOverride(TNDArrayT, 2); // N-dimensional array
172};
173
174// FIXME: Remove once we implement https://sft.its.cern.ch/jira/browse/ROOT-6284
175// When building with -fmodules, it instantiates all pending instantiations,
176// instead of delaying them until the end of the translation unit.
177// We 'got away with' probably because the use and the definition of the
178// explicit specialization do not occur in the same TU.
179//
180// In case we are building with -fmodules, we need to forward declare the
181// specialization in order to compile the dictionary G__Hist.cxx.
182template<> void TNDArrayT<double>::Streamer(TBuffer &R__b);
183template<> TClass *TNDArrayT<double>::Class();
184
185
186#endif // ROOT_TNDArray
#define d(i)
Definition RSha256.hxx:102
long long Long64_t
Definition RtypesCore.h:80
unsigned long long ULong64_t
Definition RtypesCore.h:81
const char Option_t
Definition RtypesCore.h:66
#define ClassDefNV(name, id)
Definition Rtypes.h:345
#define ClassDefOverride(name, id)
Definition Rtypes.h:341
#define R__ASSERT(e)
Definition TError.h:118
Option_t Option_t option
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
Buffer base class used for serializing objects.
Definition TBuffer.h:43
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition TClass.h:81
gives access to a sub-dimension, e.g.
Definition TNDArray.h:91
TNDArrayRef< T > operator[](Int_t idx) const
Definition TNDArray.h:96
TNDArrayRef(const T *data, const Long64_t *sizes)
Definition TNDArray.h:93
const Long64_t * fSizes
Pointer into TNDArray's fSizes.
Definition TNDArray.h:109
const T * fData
Pointer into TNDArray's fData.
Definition TNDArray.h:108
Double_t AtAsDouble(ULong64_t linidx) const override
Definition TNDArray.h:153
T & At(const Int_t *idx)
Definition TNDArray.h:139
std::vector< T > fData
Definition TNDArray.h:170
void Reset(Option_t *="") override
Definition TNDArray.h:125
T & At(ULong64_t linidx)
Definition TNDArray.h:147
TNDArrayRef< T > operator[](Int_t idx) const
Definition TNDArray.h:130
void Streamer(TBuffer &) override
Stream an object of class TObject.
void SetAsDouble(ULong64_t linidx, Double_t value) override
Definition TNDArray.h:158
void Init(Int_t ndim, const Int_t *nbins, bool addOverflow=false) override
Definition TNDArray.h:120
TNDArrayT(Int_t ndim, const Int_t *nbins, bool addOverflow=false)
Definition TNDArray.h:118
T At(ULong64_t linidx) const
Definition TNDArray.h:142
void AddAt(ULong64_t linidx, Double_t value) override
Definition TNDArray.h:163
static TClass * Class()
T At(const Int_t *idx) const
Definition TNDArray.h:136
N-Dim array class.
Definition TNDArray.h:46
TNDArray()
Definition TNDArray.h:48
TNDArray(Int_t ndim, const Int_t *nbins, bool addOverflow=false)
Definition TNDArray.h:50
virtual void AddAt(ULong64_t linidx, Double_t value)=0
virtual Double_t AtAsDouble(ULong64_t linidx) const =0
Long64_t GetNbins() const
Definition TNDArray.h:69
std::vector< Long64_t > fSizes
bin count
Definition TNDArray.h:86
Long64_t GetCellSize(Int_t dim) const
Definition TNDArray.h:70
Long64_t GetBin(const Int_t *idx) const
Definition TNDArray.h:72
virtual void Init(Int_t ndim, const Int_t *nbins, bool addOverflow=false)
Definition TNDArray.h:55
virtual void Reset(Option_t *option="")=0
virtual void SetAsDouble(ULong64_t linidx, Double_t value)=0
Int_t GetNdimensions() const
Definition TNDArray.h:68
Mother of all ROOT objects.
Definition TObject.h:41