Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:12:09

0001 // @(#)root/hist:$Id$
0002 // Author: Axel Naumann (2011-12-20)
0003 
0004 /*************************************************************************
0005  * Copyright (C) 1995-2012, Rene Brun and Fons Rademakers.               *
0006  * All rights reserved.                                                  *
0007  *                                                                       *
0008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
0009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
0010  *************************************************************************/
0011 
0012 #ifndef ROOT_THnBase
0013 #define ROOT_THnBase
0014 
0015 /*************************************************************************
0016 
0017  THnBase: Common base class for n-dimensional histogramming.
0018  Defines interfaces and algorithms.
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;  ///<  Number of dimensions
0046    TObjArray  fAxes;         ///<  Axes of the histogram
0047    TObjArray  fBrowsables;   ///<! Browser-helpers for each axis
0048    Double_t   fEntries;      ///<  Number of entries, spread over chunks
0049    Double_t   fTsumw;        ///<  Total sum of weights
0050    Double_t   fTsumw2;       ///<  Total sum of weights squared; -1 if no errors are calculated
0051    TArrayD    fTsumwx;       ///<  Total sum of weight*X for each dimension
0052    TArrayD    fTsumwx2;      ///<  Total sum of weight*X*X for each dimension
0053    std::vector<Double_t> fIntegral; ///<! vector with bin weight sums
0054    enum {
0055       kNoInt,
0056       kValidInt,
0057       kInvalidInt
0058    } fIntegralStatus;        ///<! status of integral
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    /// Increment the statistics due to filled weight "w",
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 /*nbins*/) {}
0105    virtual void SetFilledBins(Long64_t /*nbins*/) {};
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    /// Calculate errors (or not if "calc" == kFALSE)
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 /*alloc*/);
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 /*alloc*/);
0157       FillBin(bin, w);
0158       return bin;
0159    }
0160 
0161    /// Fill with the provided variadic arguments.
0162    /// The number of arguments must be equal to the number of histogram dimensions or, for weighted fills, to the
0163    /// number of dimensions + 1; in the latter case, the last function argument is used as weight.
0164    /// A separate `firstval` argument is needed so the compiler does not pick this overload instead of the non-templated
0165    /// Fill overloads
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          // without weight
0172          return Fill(x.data());
0173       } else if (Int_t(x.size()) == (GetNdimensions() + 1)) {
0174          // with weight
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)); } // intentionally non-virtual
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 /*allocate*/ = kTRUE) = 0;
0202    virtual Long64_t GetBin(const Double_t* x, Bool_t /*allocate*/ = kTRUE) = 0;
0203    virtual Long64_t GetBin(const char* name[], Bool_t /*allocate*/ = kTRUE) = 0;
0204 
0205    void SetBinContent(const Int_t* idx, Double_t v) { SetBinContent(GetBin(idx), v); } // intentionally non-virtual
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    /// Project all bins into a 1-dimensional histogram,
0217    /// keeping only axis "xDim".
0218    /// If "option" contains:
0219    ///  - "E" errors will be calculated.
0220    ///  - "A" ranges of the taget axes will be ignored.
0221    ///  - "O" original axis range of the taget axes will be
0222    ///    kept, but only bins inside the selected range
0223    ///    will be filled.
0224    TH1D*    Projection(Int_t xDim, Option_t* option = "") const {
0225       return (TH1D*) ProjectionAny(1, &xDim, false, option);
0226    }
0227 
0228    /// Project all bins into a 2-dimensional histogram,
0229    /// keeping only axes "xDim" and "yDim".
0230    ///
0231    /// WARNING: just like TH3::Project3D("yx") and TTree::Draw("y:x"),
0232    /// Projection(y,x) uses the first argument to define the y-axis and the
0233    /// second for the x-axis!
0234    ///
0235    /// If "option" contains "E" errors will be calculated.
0236    ///                      "A" ranges of the taget axes will be ignored.
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    /// Project all bins into a 3-dimensional histogram,
0243    /// keeping only axes "xDim", "yDim", and "zDim".
0244    /// If "option" contains:
0245    ///  - "E" errors will be calculated.
0246    ///  - "A" ranges of the taget axes will be ignored.
0247    ///  - "O" original axis range of the taget axes will be
0248    ///    kept, but only bins inside the selected range
0249    ///    will be filled.
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 /*wantNDim*/, 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    //void Draw(Option_t* option = "");
0288 
0289    ClassDefOverride(THnBase, 1); // Common base for n-dimensional histogram
0290 
0291    friend class THnIter;
0292 };
0293 
0294 namespace ROOT {
0295 namespace Internal {
0296    // Helper class for browsing THnBase objects
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; // Original histogram
0306       Int_t    fAxis; // Axis to visualize
0307       TH1*     fProj; // Projection result
0308       ClassDefOverride(THnBaseBrowsable, 0); // Browser-helper for THnBase
0309    };
0310 
0311    // Base class for iterating over THnBase bins
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    /// Return the next bin's index.
0337    /// If provided, set coord to that bin's coordinates (bin indexes).
0338    /// I.e. coord must point to Int_t[hist->GetNdimensions()]
0339    /// Returns -1 when all bins have been visited.
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); //Iterator over bins of a THnBase.
0351 };
0352 
0353 #endif //  ROOT_THnBase