File indexing completed on 2025-09-18 09:33:53
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #ifndef ROOT_THnBase
0013 #define ROOT_THnBase
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023 #include "TNamed.h"
0024 #include "TMath.h"
0025 #include "TFitResultPtr.h"
0026 #include "TObjArray.h"
0027 #include "TArrayD.h"
0028 #include <vector>
0029
0030
0031 class TAxis;
0032 class TH1;
0033 class TH1D;
0034 class TH2D;
0035 class TH3D;
0036 class TF1;
0037 class THnIter;
0038
0039 namespace ROOT {
0040 namespace Internal {
0041 class THnBaseBinIter;
0042 }
0043 }
0044
0045 class THnBase: public TNamed {
0046 protected:
0047 Int_t fNdimensions;
0048 TObjArray fAxes;
0049 TObjArray fBrowsables;
0050 Double_t fEntries;
0051 Double_t fTsumw;
0052 Double_t fTsumw2;
0053 TArrayD fTsumwx;
0054 TArrayD fTsumwx2;
0055 std::vector<Double_t> fIntegral;
0056 enum {
0057 kNoInt,
0058 kValidInt,
0059 kInvalidInt
0060 } fIntegralStatus;
0061
0062 protected:
0063 THnBase() : fNdimensions(0), fEntries(0), fTsumw(0), fTsumw2(-1.), fIntegral(), fIntegralStatus(kNoInt) {}
0064
0065 THnBase(const char *name, const char *title, Int_t dim, const Int_t *nbins, const Double_t *xmin,
0066 const Double_t *xmax);
0067
0068 THnBase(const char* name, const char* title, const std::vector<TAxis>& axes);
0069
0070 THnBase(const char *name, const char *title, Int_t dim, const Int_t *nbins,
0071 const std::vector<std::vector<double>> &xbins);
0072
0073 THnBase(const THnBase &other);
0074
0075 THnBase &operator=(const THnBase &other);
0076
0077 THnBase(THnBase &&other);
0078
0079 THnBase &operator=(THnBase &&other);
0080
0081 void UpdateXStat(const Double_t *x, Double_t w = 1.)
0082 {
0083 if (GetCalculateErrors()) {
0084 for (Int_t d = 0; d < fNdimensions; ++d) {
0085 const Double_t xd = x[d];
0086 fTsumwx[d] += w * xd;
0087 fTsumwx2[d] += w * xd * xd;
0088 }
0089 }
0090 }
0091
0092
0093 void FillBinBase(Double_t w) {
0094 fEntries += 1;
0095 if (GetCalculateErrors()) {
0096 fTsumw += w;
0097 fTsumw2 += w*w;
0098 }
0099 fIntegralStatus = kInvalidInt;
0100 }
0101
0102 virtual void InitStorage(Int_t* nbins, Int_t chunkSize) = 0;
0103 void Init(const char* name, const char* title,
0104 const TObjArray* axes, Bool_t keepTargetAxis,
0105 Int_t chunkSize = 1024 * 16);
0106 THnBase* CloneEmpty(const char* name, const char* title,
0107 const TObjArray* axes, Bool_t keepTargetAxis) const;
0108 virtual void Reserve(Long64_t ) {}
0109 virtual void SetFilledBins(Long64_t ) {};
0110
0111 Bool_t CheckConsistency(const THnBase *h, const char *tag) const;
0112 TH1* CreateHist(const char* name, const char* title,
0113 const TObjArray* axes, Bool_t keepTargetAxis) const;
0114 TObject* ProjectionAny(Int_t ndim, const Int_t* dim,
0115 Bool_t wantNDim, Option_t* option = "") const;
0116 Bool_t PrintBin(Long64_t idx, Int_t* coord, Option_t* options) const;
0117 void AddInternal(const THnBase* h, Double_t c, Bool_t rebinned);
0118 THnBase* RebinBase(Int_t group) const;
0119 THnBase* RebinBase(const Int_t* group) const;
0120 void ResetBase(Option_t *option= "");
0121
0122 static THnBase* CreateHnAny(const char* name, const char* title,
0123 const TH1* h1, Bool_t sparse,
0124 Int_t chunkSize = 1024 * 16);
0125 static THnBase* CreateHnAny(const char* name, const char* title,
0126 const THnBase* hn, Bool_t sparse,
0127 Int_t chunkSize = 1024 * 16);
0128
0129 public:
0130 ~THnBase() override;
0131
0132 TObjArray* GetListOfAxes() { return &fAxes; }
0133 const TObjArray* GetListOfAxes() const { return &fAxes; }
0134 TAxis* GetAxis(Int_t dim) const { return (TAxis*)fAxes[dim]; }
0135
0136 TFitResultPtr Fit(TF1 *f1 ,Option_t *option = "", Option_t *goption = "");
0137 TList* GetListOfFunctions() { return nullptr; }
0138
0139 virtual ROOT::Internal::THnBaseBinIter* CreateIter(Bool_t respectAxisRange) const = 0;
0140
0141 virtual Long64_t GetNbins() const = 0;
0142 Double_t GetEntries() const { return fEntries; }
0143 Double_t GetWeightSum() const { return fTsumw; }
0144 Int_t GetNdimensions() const { return fNdimensions; }
0145 Bool_t GetCalculateErrors() const { return fTsumw2 >= 0.; }
0146
0147
0148 void CalculateErrors(Bool_t calc = kTRUE) {
0149 if (calc) Sumw2();
0150 else fTsumw2 = -1.;
0151 }
0152
0153 Long64_t Fill(const Double_t *x, Double_t w = 1.) {
0154 UpdateXStat(x, w);
0155 Long64_t bin = GetBin(x, kTRUE );
0156 FillBin(bin, w);
0157 return bin;
0158 }
0159 Long64_t Fill(const char* name[], Double_t w = 1.) {
0160 Long64_t bin = GetBin(name, kTRUE );
0161 FillBin(bin, w);
0162 return bin;
0163 }
0164
0165
0166
0167
0168
0169
0170 template <typename... MoreTypes>
0171 Long64_t Fill(Double_t firstval, MoreTypes... morevals)
0172 {
0173 const std::array<double, 1 + sizeof...(morevals)> x{firstval, static_cast<double>(morevals)...};
0174 if (Int_t(x.size()) == GetNdimensions()) {
0175
0176 return Fill(x.data());
0177 } else if (Int_t(x.size()) == (GetNdimensions() + 1)) {
0178
0179 return Fill(x.data(), x.back());
0180 } else {
0181 Error("Fill", "Wrong number of arguments for number of histogram axes.");
0182 }
0183
0184 return -1;
0185 }
0186
0187 virtual void FillBin(Long64_t bin, Double_t w) = 0;
0188
0189 void SetBinEdges(Int_t idim, const Double_t* bins);
0190 Bool_t IsInRange(Int_t *coord) const;
0191 Double_t GetBinError(const Int_t *idx) const { return GetBinError(GetBin(idx)); }
0192 Double_t GetBinError(Long64_t linidx) const { return TMath::Sqrt(GetBinError2(linidx)); }
0193 void SetBinError(const Int_t* idx, Double_t e) { SetBinError(GetBin(idx), e); }
0194 void SetBinError(Long64_t bin, Double_t e) { SetBinError2(bin, e*e); }
0195 void AddBinContent(const Int_t* x, Double_t v = 1.) { AddBinContent(GetBin(x), v); }
0196 void SetEntries(Double_t entries) { fEntries = entries; }
0197 void SetTitle(const char *title) override;
0198
0199 std::vector<Double_t> GetBinCenter(const std::vector<Int_t> &idx) const;
0200
0201 Double_t GetBinContent(const Int_t *idx) const { return GetBinContent(GetBin(idx)); }
0202 virtual Double_t GetBinContent(Long64_t bin, Int_t* idx = nullptr) const = 0;
0203 virtual Double_t GetBinError2(Long64_t linidx) const = 0;
0204 virtual Long64_t GetBin(const Int_t* idx) const = 0;
0205 virtual Long64_t GetBin(const Double_t* x) const = 0;
0206 virtual Long64_t GetBin(const char* name[]) const = 0;
0207 virtual Long64_t GetBin(const Int_t* idx, Bool_t = kTRUE) = 0;
0208 virtual Long64_t GetBin(const Double_t* x, Bool_t = kTRUE) = 0;
0209 virtual Long64_t GetBin(const char* name[], Bool_t = kTRUE) = 0;
0210
0211 void SetBinContent(const Int_t* idx, Double_t v) { SetBinContent(GetBin(idx), v); }
0212 virtual void SetBinContent(Long64_t bin, Double_t v) = 0;
0213 virtual void SetBinError2(Long64_t bin, Double_t e2) = 0;
0214 virtual void AddBinError2(Long64_t bin, Double_t e2) = 0;
0215 virtual void AddBinContent(Long64_t bin, Double_t v = 1.) = 0;
0216
0217 Double_t GetSumw() const { return fTsumw; }
0218 Double_t GetSumw2() const { return fTsumw2; }
0219 Double_t GetSumwx(Int_t dim) const { return fTsumwx[dim]; }
0220 Double_t GetSumwx2(Int_t dim) const { return fTsumwx2[dim]; }
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230 TH1D* Projection(Int_t xDim, Option_t* option = "") const {
0231 return (TH1D*) ProjectionAny(1, &xDim, false, option);
0232 }
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243 TH2D* Projection(Int_t yDim, Int_t xDim, Option_t* option = "") const {
0244 const Int_t dim[2] = {xDim, yDim};
0245 return (TH2D*) ProjectionAny(2, dim, false, option);
0246 }
0247
0248
0249
0250
0251
0252
0253
0254
0255
0256 TH3D* Projection(Int_t xDim, Int_t yDim, Int_t zDim, Option_t* option = "") const {
0257 const Int_t dim[3] = {xDim, yDim, zDim};
0258 return (TH3D*) ProjectionAny(3, dim, false, option);
0259 }
0260
0261 THnBase* ProjectionND(Int_t ndim, const Int_t* dim,
0262 Option_t* option = "") const {
0263 return (THnBase*)ProjectionAny(ndim, dim, kTRUE , option);
0264 }
0265
0266 Long64_t Merge(TCollection* list);
0267
0268 void Scale(Double_t c);
0269 void Add(const THnBase* h, Double_t c=1.);
0270 void Add(const TH1* hist, Double_t c=1.);
0271 void Multiply(const THnBase* h);
0272 void Multiply(TF1* f, Double_t c = 1.);
0273 void Divide(const THnBase* h);
0274 void Divide(const THnBase* h1, const THnBase* h2, Double_t c1 = 1., Double_t c2 = 1., Option_t* option="");
0275 void RebinnedAdd(const THnBase* h, Double_t c=1.);
0276
0277 virtual void Reset(Option_t* option = "") = 0;
0278 virtual void Sumw2() = 0;
0279
0280 Double_t ComputeIntegral();
0281 void GetRandom(Double_t *rand, Bool_t subBinRandom = kTRUE);
0282 Double_t Integral(Bool_t respectAxisRange) const;
0283
0284 void Print(Option_t* option = "") const override;
0285 void PrintEntries(Long64_t from = 0, Long64_t howmany = -1, Option_t* options = nullptr) const;
0286 void PrintBin(Int_t* coord, Option_t* options) const {
0287 PrintBin(-1, coord, options);
0288 }
0289 void PrintBin(Long64_t idx, Option_t* options) const;
0290
0291 void Browse(TBrowser *b) override;
0292 Bool_t IsFolder() const override { return kTRUE; }
0293
0294
0295
0296 ClassDefOverride(THnBase, 1);
0297
0298 friend class THnIter;
0299 };
0300
0301 namespace ROOT {
0302 namespace Internal {
0303
0304 class THnBaseBrowsable: public TNamed {
0305 public:
0306 THnBaseBrowsable(THnBase* hist, Int_t axis);
0307 ~THnBaseBrowsable() override;
0308 void Browse(TBrowser *b) override;
0309 Bool_t IsFolder() const override { return kFALSE; }
0310
0311 private:
0312 THnBase* fHist;
0313 Int_t fAxis;
0314 TH1* fProj;
0315 ClassDefOverride(THnBaseBrowsable, 0);
0316 };
0317
0318
0319 class THnBaseBinIter {
0320 public:
0321 THnBaseBinIter(Bool_t respectAxisRange):
0322 fRespectAxisRange(respectAxisRange), fHaveSkippedBin(kFALSE) {}
0323 virtual ~THnBaseBinIter();
0324 Bool_t HaveSkippedBin() const { return fHaveSkippedBin; }
0325 Bool_t RespectsAxisRange() const { return fRespectAxisRange; }
0326
0327 virtual Int_t GetCoord(Int_t dim) const = 0;
0328 virtual Long64_t Next(Int_t* coord = nullptr) = 0;
0329
0330 protected:
0331 Bool_t fRespectAxisRange;
0332 Bool_t fHaveSkippedBin;
0333 };
0334 }
0335 }
0336
0337 class THnIter: public TObject {
0338 public:
0339 THnIter(const THnBase* hist, Bool_t respectAxisRange = kFALSE):
0340 fIter(hist->CreateIter(respectAxisRange)) {}
0341 ~THnIter() override;
0342
0343
0344
0345
0346
0347 Long64_t Next(Int_t* coord = nullptr) {
0348 return fIter->Next(coord);
0349 }
0350
0351 Int_t GetCoord(Int_t dim) const { return fIter->GetCoord(dim); }
0352 Bool_t HaveSkippedBin() const { return fIter->HaveSkippedBin(); }
0353 Bool_t RespectsAxisRange() const { return fIter->RespectsAxisRange(); }
0354
0355 private:
0356 ROOT::Internal::THnBaseBinIter* fIter;
0357 ClassDefOverride(THnIter, 0);
0358 };
0359
0360 #endif