File indexing completed on 2025-01-18 10:12:17
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #ifndef ROOT_TNDArray
0013 #define ROOT_TNDArray
0014
0015 #include "TObject.h"
0016 #include "TError.h"
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046 class TNDArray: public TObject {
0047 public:
0048 TNDArray() : fSizes() {}
0049
0050 TNDArray(Int_t ndim, const Int_t *nbins, bool addOverflow = false) : fSizes()
0051 {
0052 TNDArray::Init(ndim, nbins, addOverflow);
0053 }
0054
0055 virtual void Init(Int_t ndim, const Int_t* nbins, bool addOverflow = false) {
0056
0057
0058 fSizes.resize(ndim + 1);
0059 Int_t overBins = addOverflow ? 2 : 0;
0060 fSizes[ndim] = 1;
0061 for (Int_t i = 0; i < ndim; ++i) {
0062 fSizes[ndim - i - 1] = fSizes[ndim - i] * (nbins[ndim - i - 1] + overBins);
0063 }
0064 }
0065
0066 virtual void Reset(Option_t* option = "") = 0;
0067
0068 Int_t GetNdimensions() const { return fSizes.size() - 1; }
0069 Long64_t GetNbins() const { return fSizes[0]; }
0070 Long64_t GetCellSize(Int_t dim) const { return fSizes[dim + 1]; }
0071
0072 Long64_t GetBin(const Int_t* idx) const {
0073
0074 Long64_t bin = idx[fSizes.size() - 2];
0075 for (unsigned int d = 0; d < fSizes.size() - 2; ++d) {
0076 bin += fSizes[d + 1] * idx[d];
0077 }
0078 return bin;
0079 }
0080
0081 virtual Double_t AtAsDouble(ULong64_t linidx) const = 0;
0082 virtual void SetAsDouble(ULong64_t linidx, Double_t value) = 0;
0083 virtual void AddAt(ULong64_t linidx, Double_t value) = 0;
0084
0085 protected:
0086 std::vector<Long64_t> fSizes;
0087 ClassDefOverride(TNDArray, 2);
0088 };
0089
0090 template <typename T>
0091 class TNDArrayRef {
0092 public:
0093 TNDArrayRef(const T* data, const Long64_t* sizes):
0094 fData(data), fSizes(sizes) {}
0095
0096 TNDArrayRef<T> operator[] (Int_t idx) const {
0097 if (!fData) return TNDArrayRef<T>(0, 0);
0098 R__ASSERT(idx < fSizes[-1] / fSizes[0] && "index out of range!");
0099 return TNDArrayRef<T>(fData + idx * fSizes[0], (fSizes[0] == 1) ? nullptr : (fSizes + 1));
0100 }
0101 operator T() const {
0102 if (!fData) return T();
0103 R__ASSERT(fSizes == nullptr && "Element operator can only be used on non-array element. Missing an operator[] level?");
0104 return *fData;
0105 }
0106
0107 private:
0108 const T* fData;
0109 const Long64_t* fSizes;
0110 ClassDefNV(TNDArrayRef, 0);
0111 };
0112
0113 template <typename T>
0114 class TNDArrayT: public TNDArray {
0115 public:
0116 TNDArrayT() : fData() {}
0117
0118 TNDArrayT(Int_t ndim, const Int_t *nbins, bool addOverflow = false) : TNDArray(ndim, nbins, addOverflow), fData() {}
0119
0120 void Init(Int_t ndim, const Int_t* nbins, bool addOverflow = false) override {
0121 fData.clear();
0122 TNDArray::Init(ndim, nbins, addOverflow);
0123 }
0124
0125 void Reset(Option_t* = "") override {
0126
0127 fData.assign(fSizes[0], T());
0128 }
0129
0130 TNDArrayRef<T> operator[](Int_t idx) const {
0131 if (!fData) return TNDArrayRef<T>(0, 0);
0132 R__ASSERT(idx < fSizes[0] / fSizes[1] && "index out of range!");
0133 return TNDArrayRef<T>(fData.data() + idx * fSizes[1], fSizes.data() + 2);
0134 }
0135
0136 T At(const Int_t* idx) const {
0137 return At(GetBin(idx));
0138 }
0139 T& At(const Int_t* idx) {
0140 return At(GetBin(idx));
0141 }
0142 T At(ULong64_t linidx) const {
0143 if (fData.empty())
0144 return T();
0145 return fData[linidx];
0146 }
0147 T& At(ULong64_t linidx) {
0148 if (fData.empty())
0149 fData.resize(fSizes[0], T());
0150 return fData[linidx];
0151 }
0152
0153 Double_t AtAsDouble(ULong64_t linidx) const override {
0154 if (fData.empty())
0155 return 0.;
0156 return fData[linidx];
0157 }
0158 void SetAsDouble(ULong64_t linidx, Double_t value) override {
0159 if (fData.empty())
0160 fData.resize(fSizes[0], T());
0161 fData[linidx] = (T) value;
0162 }
0163 void AddAt(ULong64_t linidx, Double_t value) override {
0164 if (fData.empty())
0165 fData.resize(fSizes[0], T());
0166 fData[linidx] += (T) value;
0167 }
0168
0169 protected:
0170 std::vector<T> fData;
0171 ClassDefOverride(TNDArrayT, 2);
0172 };
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182 template<> void TNDArrayT<double>::Streamer(TBuffer &R__b);
0183 template<> TClass *TNDArrayT<double>::Class();
0184
0185
0186 #endif