File indexing completed on 2025-01-18 10:12:09
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #ifndef ROOT_THnSparse
0013 #define ROOT_THnSparse
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023 #include "THnBase.h"
0024 #include "TExMap.h"
0025 #include "THnSparse_Internal.h"
0026
0027
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;
0040 Long64_t fFilledBins;
0041 TObjArray fBinContent;
0042 TExMap fBins;
0043 TExMap fBinsContinued;
0044 THnSparseCompactBinCoord *fCompactCoord;
0045
0046 THnSparse(const THnSparse&) = delete;
0047 THnSparse& operator=(const THnSparse&) = delete;
0048
0049 protected:
0050
0051 THnSparse();
0052 THnSparse(const char* name, const char* title, Int_t dim,
0053 const Int_t* nbins, const Double_t* xmin, const Double_t* xmax,
0054 Int_t chunksize);
0055 THnSparseCompactBinCoord* GetCompactCoord() const;
0056 THnSparseArrayChunk* GetChunk(Int_t idx) const {
0057 return (THnSparseArrayChunk*) fBinContent[idx]; }
0058
0059 THnSparseArrayChunk* AddChunk();
0060 void Reserve(Long64_t nbins) override;
0061 void FillExMap();
0062 virtual TArray* GenerateArray() const = 0;
0063 Long64_t GetBinIndexForCurrentBin(Bool_t allocate);
0064
0065
0066
0067 void FillBin(Long64_t bin, Double_t w) override {
0068 THnSparseArrayChunk* chunk = GetChunk(bin / fChunkSize);
0069 chunk->AddBinContent(bin % fChunkSize, w);
0070 FillBinBase(w);
0071 }
0072 void InitStorage(Int_t* nbins, Int_t chunkSize) override;
0073
0074 public:
0075 ~THnSparse() override;
0076
0077 static THnSparse* CreateSparse(const char* name, const char* title,
0078 const TH1* h1, Int_t chunkSize = 1024 * 16) {
0079 return (THnSparse*) CreateHnAny(name, title, h1, kTRUE ,
0080 chunkSize);
0081 }
0082 static THnSparse* CreateSparse(const char* name, const char* title,
0083 const THnBase* hn, Int_t chunkSize = 1024 * 16) {
0084 return (THnSparse*) CreateHnAny(name, title, hn, kTRUE ,
0085 chunkSize);
0086 }
0087
0088 Int_t GetChunkSize() const { return fChunkSize; }
0089 Int_t GetNChunks() const { return fBinContent.GetEntriesFast(); }
0090
0091 ROOT::Internal::THnBaseBinIter* CreateIter(Bool_t respectAxisRange) const override;
0092
0093 Long64_t GetNbins() const override { return fFilledBins; }
0094 void SetFilledBins(Long64_t nbins) override { fFilledBins = nbins; }
0095
0096 Long64_t GetBin(const Int_t* idx) const override { return const_cast<THnSparse*>(this)->GetBin(idx, kFALSE); }
0097 Long64_t GetBin(const Double_t* x) const override { return const_cast<THnSparse*>(this)->GetBin(x, kFALSE); }
0098 Long64_t GetBin(const char* name[]) const override { return const_cast<THnSparse*>(this)->GetBin(name, kFALSE); }
0099 Long64_t GetBin(const Int_t* idx, Bool_t allocate = kTRUE) override;
0100 Long64_t GetBin(const Double_t* x, Bool_t allocate = kTRUE) override;
0101 Long64_t GetBin(const char* name[], Bool_t allocate = kTRUE) override;
0102
0103
0104
0105 void SetBinContent(const Int_t* idx, Double_t v) {
0106 THnBase::SetBinContent(idx, v);
0107 }
0108 void SetBinContent(Long64_t bin, Double_t v) override;
0109 void SetBinError2(Long64_t bin, Double_t e2) override;
0110
0111
0112
0113 void AddBinContent(const Int_t* idx, Double_t v = 1.) {
0114 THnBase::AddBinContent(idx, v);
0115 }
0116 void AddBinContent(Long64_t bin, Double_t v = 1.) override;
0117 void AddBinError2(Long64_t bin, Double_t e2) override;
0118
0119
0120
0121 Double_t GetBinContent(const Int_t *idx) const {
0122
0123 return THnBase::GetBinContent(idx);
0124 }
0125 Double_t GetBinContent(Long64_t bin, Int_t* idx = nullptr) const override;
0126 Double_t GetBinError2(Long64_t linidx) const override;
0127
0128 Double_t GetSparseFractionBins() const;
0129 Double_t GetSparseFractionMem() const;
0130
0131
0132
0133
0134 TH1D* Projection(Int_t xDim, Option_t* option = "") const{
0135 return THnBase::Projection(xDim, option);
0136 }
0137
0138
0139
0140
0141 TH2D* Projection(Int_t yDim, Int_t xDim,
0142 Option_t* option = "") const {
0143 return THnBase::Projection(yDim, xDim, option);
0144 }
0145
0146
0147
0148
0149 TH3D* Projection(Int_t xDim, Int_t yDim, Int_t zDim,
0150 Option_t* option = "") const {
0151 return THnBase::Projection(xDim, yDim, zDim, option);
0152 }
0153
0154 THnSparse* Projection(Int_t ndim, const Int_t* dim,
0155 Option_t* option = "") const {
0156 return (THnSparse*) ProjectionND(ndim, dim, option);
0157 }
0158
0159 THnSparse* Rebin(Int_t group) const {
0160 return (THnSparse*) RebinBase(group);
0161 }
0162 THnSparse* Rebin(const Int_t* group) const {
0163 return (THnSparse*) RebinBase(group);
0164 }
0165
0166 void Reset(Option_t* option = "") override;
0167 void Sumw2() override;
0168
0169 ClassDefOverride(THnSparse, 3);
0170 };
0171
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201
0202
0203
0204
0205
0206 template <class CONT>
0207 class THnSparseT: public THnSparse {
0208 public:
0209 THnSparseT() {}
0210 THnSparseT(const char* name, const char* title, Int_t dim,
0211 const Int_t* nbins, const Double_t* xmin = nullptr,
0212 const Double_t* xmax = nullptr, Int_t chunksize = 1024 * 16):
0213 THnSparse(name, title, dim, nbins, xmin, xmax, chunksize) {}
0214
0215 TArray* GenerateArray() const override { return new CONT(GetChunkSize()); }
0216 private:
0217 ClassDefOverride(THnSparseT, 1);
0218 };
0219
0220 typedef THnSparseT<TArrayD> THnSparseD;
0221 typedef THnSparseT<TArrayF> THnSparseF;
0222 typedef THnSparseT<TArrayL64> THnSparseL;
0223 typedef THnSparseT<TArrayI> THnSparseI;
0224 typedef THnSparseT<TArrayS> THnSparseS;
0225 typedef THnSparseT<TArrayC> THnSparseC;
0226
0227
0228 #endif