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