Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/root/THnSparse.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // @(#)root/hist:$Id$
0002 // Author: Axel Naumann (2007-09-11)
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_THnSparse
0013 #define ROOT_THnSparse
0014 
0015 /*************************************************************************
0016 
0017  THnSparse: histogramming multi-dimensional, sparse distributions in
0018  a memory-efficient way.
0019 
0020 *************************************************************************/
0021 
0022 
0023 #include "THnBase.h"
0024 #include "TExMap.h"
0025 #include "THnSparse_Internal.h"
0026 
0027 // needed only for template instantiations of THnSparseT:
0028 #include "TArrayF.h"
0029 #include "TArrayL.h"
0030 #include "TArrayL64.h"
0031 #include "TArrayI.h"
0032 #include "TArrayS.h"
0033 #include "TArrayC.h"
0034 
0035 class THnSparseCompactBinCoord;
0036 
0037 class THnSparse: public THnBase {
0038  private:
0039    Int_t      fChunkSize;                   ///<  Number of entries for each chunk
0040    Long64_t   fFilledBins;                  ///<  Number of filled bins
0041    TObjArray  fBinContent;                  ///<  Array of THnSparseArrayChunk
0042    TExMap     fBins;                        ///<! Filled bins
0043    TExMap     fBinsContinued;               ///<! Filled bins for non-unique hashes, containing pairs of (bin index 0, bin index 1)
0044    THnSparseCompactBinCoord *fCompactCoord; ///<! Compact coordinate
0045 
0046    THnSparse& operator=(const THnSparse&) = delete;
0047 
0048  protected:
0049 
0050    THnSparseCompactBinCoord* GetCompactCoord() const;
0051    THnSparseArrayChunk* GetChunk(Int_t idx) const {
0052       return (THnSparseArrayChunk*) fBinContent[idx]; }
0053 
0054    THnSparseArrayChunk* AddChunk();
0055    void Reserve(Long64_t nbins) override;
0056    void FillExMap();
0057    virtual TArray* GenerateArray() const = 0;
0058    Long64_t GetBinIndexForCurrentBin(Bool_t allocate);
0059 
0060    /// Increment the bin content of "bin" by "w",
0061    /// return the bin index.
0062    void FillBin(Long64_t bin, Double_t w) override {
0063       THnSparseArrayChunk* chunk = GetChunk(bin / fChunkSize);
0064       chunk->AddBinContent(bin % fChunkSize, w);
0065       FillBinBase(w);
0066    }
0067    void InitStorage(Int_t* nbins, Int_t chunkSize) override;
0068 
0069  public:
0070 
0071    THnSparse();
0072    THnSparse(const char* name, const char* title, Int_t dim,
0073              const Int_t* nbins, const Double_t* xmin = nullptr, const Double_t* xmax = nullptr,
0074              Int_t chunksize = 1024 * 16);
0075    THnSparse(const char* name, const char* title,
0076              const std::vector<TAxis>& axes,
0077              Int_t chunksize = 1024 * 16);
0078 
0079    THnSparse(const char *name, const char *title, Int_t dim, const Int_t *nbins,
0080              const std::vector<std::vector<double>> &xbins, Int_t chunksize = 1024 * 16);
0081 
0082    THnSparse(const THnSparse &other);
0083 
0084    ~THnSparse() override;
0085 
0086    static THnSparse* CreateSparse(const char* name, const char* title,
0087                                   const TH1* h1, Int_t chunkSize = 1024 * 16) {
0088       return (THnSparse*) CreateHnAny(name, title, h1, kTRUE /*sparse*/,
0089                                        chunkSize);
0090    }
0091    static THnSparse* CreateSparse(const char* name, const char* title,
0092                                   const THnBase* hn, Int_t chunkSize = 1024 * 16) {
0093       return (THnSparse*) CreateHnAny(name, title, hn, kTRUE /*sparse*/,
0094                                        chunkSize);
0095    }
0096 
0097    Int_t GetChunkSize() const { return fChunkSize; }
0098    Int_t GetNChunks() const { return fBinContent.GetEntriesFast(); }
0099 
0100    ROOT::Internal::THnBaseBinIter* CreateIter(Bool_t respectAxisRange) const override;
0101 
0102    Long64_t GetNbins() const override { return fFilledBins; }
0103    void SetFilledBins(Long64_t nbins) override { fFilledBins = nbins; }
0104 
0105    Long64_t GetBin(const Int_t* idx) const override { return const_cast<THnSparse*>(this)->GetBin(idx, kFALSE); }
0106    Long64_t GetBin(const Double_t* x) const override { return const_cast<THnSparse*>(this)->GetBin(x, kFALSE); }
0107    Long64_t GetBin(const char* name[]) const override { return const_cast<THnSparse*>(this)->GetBin(name, kFALSE); }
0108    Long64_t GetBin(const Int_t* idx, Bool_t allocate = kTRUE) override;
0109    Long64_t GetBin(const Double_t* x, Bool_t allocate = kTRUE) override;
0110    Long64_t GetBin(const char* name[], Bool_t allocate = kTRUE) override;
0111 
0112    /// Forwards to THnBase::SetBinContent().
0113    /// Non-virtual, CINT-compatible replacement of a using declaration.
0114    void SetBinContent(const Int_t* idx, Double_t v) {
0115       THnBase::SetBinContent(idx, v);
0116    }
0117    void SetBinContent(Long64_t bin, Double_t v) override;
0118    void SetBinError2(Long64_t bin, Double_t e2) override;
0119 
0120    /// Forwards to THnBase::AddBinContent().
0121    /// Non-virtual, CINT-compatible replacement of a using declaration.
0122    void AddBinContent(const Int_t* idx, Double_t v = 1.) {
0123       THnBase::AddBinContent(idx, v);
0124    }
0125    void AddBinContent(Long64_t bin, Double_t v = 1.) override;
0126    void AddBinError2(Long64_t bin, Double_t e2) override;
0127 
0128    /// Forwards to THnBase::GetBinContent() overload.
0129    /// Non-virtual, CINT-compatible replacement of a using declaration.
0130    Double_t GetBinContent(const Int_t *idx) const {
0131 
0132       return THnBase::GetBinContent(idx);
0133    }
0134    Double_t GetBinContent(Long64_t bin, Int_t* idx = nullptr) const override;
0135    Double_t GetBinError2(Long64_t linidx) const override;
0136 
0137    Double_t GetSparseFractionBins() const;
0138    Double_t GetSparseFractionMem() const;
0139 
0140    /// Forwards to THnBase::Projection().
0141    /// Non-virtual, as a CINT-compatible replacement of a using
0142    /// declaration.
0143    TH1D*      Projection(Int_t xDim, Option_t* option = "") const{
0144       return THnBase::Projection(xDim, option);
0145    }
0146 
0147    /// Forwards to THnBase::Projection().
0148    /// Non-virtual, as a CINT-compatible replacement of a using
0149    /// declaration.
0150    TH2D*      Projection(Int_t yDim, Int_t xDim,
0151                          Option_t* option = "") const {
0152       return THnBase::Projection(yDim, xDim, option);
0153    }
0154 
0155    /// Forwards to THnBase::Projection().
0156    /// Non-virtual, as a CINT-compatible replacement of a using
0157    /// declaration.
0158    TH3D*      Projection(Int_t xDim, Int_t yDim, Int_t zDim,
0159                          Option_t* option = "") const {
0160       return THnBase::Projection(xDim, yDim, zDim, option);
0161    }
0162 
0163    THnSparse* Projection(Int_t ndim, const Int_t* dim,
0164                          Option_t* option = "") const {
0165       return (THnSparse*) ProjectionND(ndim, dim, option);
0166    }
0167 
0168    THnSparse* Rebin(Int_t group) const {
0169       return (THnSparse*) RebinBase(group);
0170    }
0171    THnSparse* Rebin(const Int_t* group) const {
0172       return (THnSparse*) RebinBase(group);
0173    }
0174 
0175    void Reset(Option_t* option = "") override;
0176    void Sumw2() override;
0177 
0178    ClassDefOverride(THnSparse, 3); // Interfaces of sparse n-dimensional histogram
0179 };
0180 
0181 
0182 
0183 //______________________________________________________________________________
0184 /** \class THnSparseT
0185  Templated implementation of the abstract base THnSparse.
0186  All functionality and the interfaces to be used are in THnSparse!
0187 
0188  THnSparse does not know how to store any bin content itself. Instead, this
0189  is delegated to the derived, templated class: the template parameter decides
0190  what the format for the bin content is. In fact it even defines the array
0191  itself; possible implementations probably derive from TArray.
0192 
0193  Typedefs exist for template parameters with ROOT's generic types:
0194 
0195  Templated name        |    Typedef    |    Bin content type
0196  ----------------------|---------------|--------------------
0197  THnSparseT<TArrayC>   |  THnSparseC   |  Char_t
0198  THnSparseT<TArrayS>   |  THnSparseS   |  Short_t
0199  THnSparseT<TArrayI>   |  THnSparseI   |  Int_t
0200  THnSparseT<TArrayL64> |  THnSparseL   |  Long64_t
0201  THnSparseT<TArrayF>   |  THnSparseF   |  Float_t
0202  THnSparseT<TArrayD>   |  THnSparseD   |  Double_t
0203 
0204  We recommend to use THnSparseC wherever possible, and to map its value space
0205  of 256 possible values to e.g. float values outside the class. This saves an
0206  enormous amount of memory. Only if more than 256 values need to be
0207  distinguished should e.g. THnSparseS or even THnSparseF be chosen.
0208 
0209  Implementation detail: the derived, templated class is kept extremely small
0210  on purpose. That way the (templated thus inlined) uses of this class will
0211  only create a small amount of machine code, in contrast to e.g. STL.
0212 */
0213 
0214 
0215 template <class CONT>
0216 class THnSparseT: public THnSparse {
0217  public:
0218    using THnSparse::THnSparse;
0219 
0220    TArray* GenerateArray() const override { return new CONT(GetChunkSize()); }
0221  private:
0222    ClassDefOverride(THnSparseT, 1); // Sparse n-dimensional histogram with templated content
0223 };
0224 
0225 typedef THnSparseT<TArrayD> THnSparseD;
0226 typedef THnSparseT<TArrayF> THnSparseF;
0227 typedef THnSparseT<TArrayL64> THnSparseL;
0228 typedef THnSparseT<TArrayI> THnSparseI;
0229 typedef THnSparseT<TArrayS> THnSparseS;
0230 typedef THnSparseT<TArrayC> THnSparseC;
0231 
0232 
0233 #endif //  ROOT_THnSparse