File indexing completed on 2025-01-18 10:12:09
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #ifndef ROOT_THN
0013 #define ROOT_THN
0014
0015 #include "THnBase.h"
0016
0017 #include "TNDArray.h"
0018
0019 #include "TArrayD.h"
0020
0021 #include "TAxis.h"
0022
0023 class TH1;
0024 class TH1D;
0025 class TH2D;
0026 class TH3D;
0027 class THnSparse;
0028 class TF1;
0029
0030 class THn: public THnBase {
0031
0032 protected:
0033 void AllocCoordBuf() const;
0034 void InitStorage(Int_t* nbins, Int_t chunkSize) override;
0035
0036 THn() = default;
0037 THn(const char* name, const char* title, Int_t dim, const Int_t* nbins,
0038 const Double_t* xmin, const Double_t* xmax);
0039
0040 THn(const char *name, const char *title, Int_t dim, const Int_t *nbins,
0041 const std::vector<std::vector<double>> &xbins);
0042
0043 public:
0044 ~THn() override;
0045
0046 static THn* CreateHn(const char* name, const char* title, const TH1* h1) {
0047 return (THn*) CreateHnAny(name, title, h1, kFALSE , -1);
0048 }
0049 static THn* CreateHn(const char* name, const char* title, const THnBase* hn) {
0050 return (THn*) CreateHnAny(name, title, hn, kFALSE , -1);
0051 }
0052
0053 ROOT::Internal::THnBaseBinIter* CreateIter(Bool_t respectAxisRange) const override;
0054 Long64_t GetNbins() const override { return GetArray().GetNbins(); }
0055
0056 Long64_t GetBin(const Int_t* idx) const override {
0057 return GetArray().GetBin(idx);
0058 }
0059 Long64_t GetBin(const Double_t* x) const override {
0060 if (fCoordBuf.empty())
0061 AllocCoordBuf();
0062 for (Int_t d = 0; d < fNdimensions; ++d) {
0063 fCoordBuf[d] = GetAxis(d)->FindFixBin(x[d]);
0064 }
0065 return GetArray().GetBin(fCoordBuf.data());
0066 }
0067 Long64_t GetBin(const char* name[]) const override {
0068 if (fCoordBuf.empty())
0069 AllocCoordBuf();
0070 for (Int_t d = 0; d < fNdimensions; ++d) {
0071 fCoordBuf[d] = GetAxis(d)->FindBin(name[d]);
0072 }
0073 return GetArray().GetBin(fCoordBuf.data());
0074 }
0075
0076 Long64_t GetBin(const Int_t* idx, Bool_t = kTRUE) override {
0077 return const_cast<const THn*>(this)->GetBin(idx);
0078 }
0079 Long64_t GetBin(const Double_t* x, Bool_t = kTRUE) override {
0080 return const_cast<const THn*>(this)->GetBin(x);
0081 }
0082 Long64_t GetBin(const char* name[], Bool_t = kTRUE) override {
0083 return const_cast<const THn*>(this)->GetBin(name);
0084 }
0085
0086
0087 void FillBin(Long64_t bin, Double_t w) override {
0088 GetArray().AddAt(bin, w);
0089 if (GetCalculateErrors()) {
0090 fSumw2.AddAt(bin, w * w);
0091 }
0092 FillBinBase(w);
0093 }
0094
0095
0096
0097 void SetBinContent(const Int_t* idx, Double_t v) {
0098 THnBase::SetBinContent(idx, v);
0099 }
0100 void SetBinContent(Long64_t bin, Double_t v) override {
0101 GetArray().SetAsDouble(bin, v);
0102 }
0103 void SetBinError2(Long64_t bin, Double_t e2) override {
0104 if (!GetCalculateErrors()) Sumw2();
0105 fSumw2.At(bin) = e2;
0106 }
0107
0108
0109 void AddBinContent(const Int_t* idx, Double_t v = 1.) {
0110 THnBase::AddBinContent(idx, v);
0111 }
0112 void AddBinContent(Long64_t bin, Double_t v = 1.) override {
0113 GetArray().AddAt(bin, v);
0114 }
0115 void AddBinError2(Long64_t bin, Double_t e2) override {
0116 fSumw2.At(bin) += e2;
0117 }
0118
0119
0120 Double_t GetBinContent(const Int_t *idx) const {
0121 return THnBase::GetBinContent(idx);
0122 }
0123
0124 Double_t GetBinContent(Long64_t bin, Int_t* idx = nullptr) const override {
0125 if (idx) {
0126 const TNDArray& arr = GetArray();
0127 Long64_t prevCellSize = arr.GetNbins();
0128 for (Int_t i = 0; i < GetNdimensions(); ++i) {
0129 Long64_t cellSize = arr.GetCellSize(i);
0130 idx[i] = (bin % prevCellSize) / cellSize;
0131 prevCellSize = cellSize;
0132 }
0133 }
0134 return GetArray().AtAsDouble(bin);
0135 }
0136 Double_t GetBinError2(Long64_t linidx) const override {
0137 return GetCalculateErrors() ? fSumw2.At(linidx) : GetBinContent(linidx);
0138 }
0139
0140 virtual const TNDArray& GetArray() const = 0;
0141 virtual TNDArray& GetArray() = 0;
0142
0143 void Sumw2() override;
0144
0145
0146
0147 TH1D* Projection(Int_t xDim, Option_t* option = "") const {
0148 return THnBase::Projection(xDim, option);
0149 }
0150
0151
0152
0153 TH2D* Projection(Int_t yDim, Int_t xDim,
0154 Option_t* option = "") const {
0155 return THnBase::Projection(yDim, xDim, option);
0156 }
0157
0158
0159
0160 TH3D* Projection(Int_t xDim, Int_t yDim, Int_t zDim,
0161 Option_t* option = "") const {
0162 return THnBase::Projection(xDim, yDim, zDim, option);
0163 }
0164
0165 THn* Projection(Int_t ndim, const Int_t* dim,
0166 Option_t* option = "") const {
0167 return (THn*) ProjectionND(ndim, dim, option);
0168 }
0169
0170 THn* Rebin(Int_t group) const {
0171 return (THn*) RebinBase(group);
0172 }
0173 THn* Rebin(const Int_t* group) const {
0174 return (THn*) RebinBase(group);
0175 }
0176
0177 void Reset(Option_t* option = "") override;
0178
0179 protected:
0180 TNDArrayT<Double_t> fSumw2;
0181 mutable std::vector<Int_t> fCoordBuf;
0182
0183 ClassDefOverride(THn, 1);
0184 };
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201
0202
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212
0213
0214
0215
0216
0217
0218 template <typename T>
0219 class THnT: public THn {
0220 public:
0221 THnT() {}
0222
0223 THnT(const char* name, const char* title,
0224 Int_t dim, const Int_t* nbins,
0225 const Double_t* xmin, const Double_t* xmax):
0226 THn(name, title, dim, nbins, xmin, xmax),
0227 fArray(dim, nbins, true) {}
0228
0229 THnT(const char *name, const char *title, Int_t dim, const Int_t *nbins,
0230 const std::vector<std::vector<double>> &xbins)
0231 : THn(name, title, dim, nbins, xbins), fArray(dim, nbins, true)
0232 {
0233 }
0234
0235 const TNDArray& GetArray() const override { return fArray; }
0236 TNDArray& GetArray() override { return fArray; }
0237
0238 protected:
0239 TNDArrayT<T> fArray;
0240 ClassDefOverride(THnT, 1);
0241 };
0242
0243 typedef THnT<Float_t> THnF;
0244 typedef THnT<Double_t> THnD;
0245 typedef THnT<Char_t> THnC;
0246 typedef THnT<Short_t> THnS;
0247 typedef THnT<Int_t> THnI;
0248 typedef THnT<Long64_t> THnL;
0249 typedef THnT<Long64_t> THnL64;
0250
0251 #endif