Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
THn.h
Go to the documentation of this file.
1// @(#)root/hist:$Id$
2// Author: Axel Naumann, Nov 2011
3
4/*************************************************************************
5 * Copyright (C) 1995-2012, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12#ifndef ROOT_THN
13#define ROOT_THN
14
15#include "THnBase.h"
16
17#include "TNDArray.h"
18
19#include "TArrayD.h"
20
21#include "TAxis.h"
22
23class TH1;
24class TH1D;
25class TH2D;
26class TH3D;
27class THnSparse;
28class TF1;
29
30class THn: public THnBase {
31
32protected:
33 void AllocCoordBuf() const;
34 void InitStorage(Int_t* nbins, Int_t chunkSize) override;
35
36 THn() = default;
37 THn(const char* name, const char* title, Int_t dim, const Int_t* nbins,
38 const Double_t* xmin, const Double_t* xmax);
39 THn(const char *name, const char *title, const std::vector<TAxis> &axes);
40 THn(const char *name, const char *title, Int_t dim, const Int_t *nbins,
41 const std::vector<std::vector<double>> &xbins);
42
43public:
44 ~THn() override;
45
46 static THn* CreateHn(const char* name, const char* title, const TH1* h1) {
47 return (THn*) CreateHnAny(name, title, h1, kFALSE /*THn*/, -1);
48 }
49 static THn* CreateHn(const char* name, const char* title, const THnBase* hn) {
50 return (THn*) CreateHnAny(name, title, hn, kFALSE /*THn*/, -1);
51 }
52
54 Long64_t GetNbins() const override { return GetArray().GetNbins(); }
55
56 Long64_t GetBin(const Int_t* idx) const override {
57 return GetArray().GetBin(idx);
58 }
59 Long64_t GetBin(const Double_t* x) const override {
60 if (fCoordBuf.empty())
62 for (Int_t d = 0; d < fNdimensions; ++d) {
64 }
65 return GetArray().GetBin(fCoordBuf.data());
66 }
67 Long64_t GetBin(const char* name[]) const override {
68 if (fCoordBuf.empty())
70 for (Int_t d = 0; d < fNdimensions; ++d) {
72 }
73 return GetArray().GetBin(fCoordBuf.data());
74 }
75
76 Long64_t GetBin(const Int_t* idx, Bool_t /*allocate*/ = kTRUE) override {
77 return const_cast<const THn*>(this)->GetBin(idx);
78 }
79 Long64_t GetBin(const Double_t* x, Bool_t /*allocate*/ = kTRUE) override {
80 return const_cast<const THn*>(this)->GetBin(x);
81 }
82 Long64_t GetBin(const char* name[], Bool_t /*allocate*/ = kTRUE) override {
83 return const_cast<const THn*>(this)->GetBin(name);
84 }
85
86 /// Increment the bin content of "bin" by "w", return the bin index.
87 void FillBin(Long64_t bin, Double_t w) override {
88 GetArray().AddAt(bin, w);
89 if (GetCalculateErrors()) {
90 fSumw2.AddAt(bin, w * w);
91 }
93 }
94
95 using THnBase::SetBinContent; // non-virtual void SetBinContent(const Int_t* idx, Double_t v)
96 void SetBinContent(Long64_t bin, Double_t v) override {
97 GetArray().SetAsDouble(bin, v);
98 }
99 void SetBinError2(Long64_t bin, Double_t e2) override {
100 if (!GetCalculateErrors()) Sumw2();
101 fSumw2.At(bin) = e2;
102 }
103 using THnBase::AddBinContent; // non-virtual void AddBinContent(const Int_t* idx, Double_t v = 1.)
104 void AddBinContent(Long64_t bin, Double_t v = 1.) override {
105 GetArray().AddAt(bin, v);
106 }
107 void AddBinError2(Long64_t bin, Double_t e2) override {
108 fSumw2.At(bin) += e2;
109 }
110 using THnBase::GetBinContent; // non-virtual Double_t GetBinContent(const Int_t *idx) const
111 /// Get the content of bin, and set its index if idx is != 0.
112 Double_t GetBinContent(Long64_t bin, Int_t* idx = nullptr) const override {
113 if (idx) {
114 const TNDArray& arr = GetArray();
115 Long64_t prevCellSize = arr.GetNbins();
116 for (Int_t i = 0; i < GetNdimensions(); ++i) {
117 Long64_t cellSize = arr.GetCellSize(i);
118 idx[i] = (bin % prevCellSize) / cellSize;
119 prevCellSize = cellSize;
120 }
121 }
122 return GetArray().AtAsDouble(bin);
123 }
127
128 virtual const TNDArray& GetArray() const = 0;
129 virtual TNDArray& GetArray() = 0;
130
131 void Sumw2() override;
132
133 using THnBase::Projection; // non-virtual TH1D* Projection(Int_t xDim, Option_t* option = "") const
134 // TH2D* Projection(Int_t yDim, Int_t xDim, Option_t* option = "") const
135 // TH3D* Projection(Int_t xDim, Int_t yDim, Int_t zDim, Option_t* option = "") const
136
137 THn* Projection(Int_t ndim, const Int_t* dim,
138 Option_t* option = "") const {
139 return (THn*) ProjectionND(ndim, dim, option);
140 }
141
143 return (THn*) RebinBase(group);
144 }
145 THn* Rebin(const Int_t* group) const {
146 return (THn*) RebinBase(group);
147 }
148
149 void Reset(Option_t* option = "") override;
150
151protected:
152 TNDArrayT<Double_t> fSumw2; // bin error, lazy allocation happens in TNDArrayT
153 mutable std::vector<Int_t> fCoordBuf; //! Temporary buffer
154
155 ClassDefOverride(THn, 1); //Base class for multi-dimensional histogram
156};
157
158
159//______________________________________________________________________________
160/** \class THnT
161 Templated implementation of the abstract base THn.
162 All functionality and the interfaces to be used are in THn!
163
164 THn does not know how to store any bin content itself. Instead, this
165 is delegated to the derived, templated class: the template parameter decides
166 what the format for the bin content is. The actual storage is delegated to
167 TNDArrayT<T>.
168
169 Typedefs exist for template parameters with ROOT's generic types:
170
171 Templated name | Typedef | Bin content type
172 -----------------|---------------|--------------------
173 THnT<Char_t> | THnC | Char_t
174 THnT<Short_t> | THnS | Short_t
175 THnT<Int_t> | THnI | Int_t
176 THnT<Long64_t> | THnL | Long64_t
177 THnT<Float_t> | THnF | Float_t
178 THnT<Double_t> | THnD | Double_t
179
180 We recommend to use THnC wherever possible, and to map its value space
181 of 256 possible values to e.g. float values outside the class. This saves an
182 enormous amount of memory. Only if more than 256 values need to be
183 distinguished should e.g. THnS or even THnF be chosen.
184
185 Implementation detail: the derived, templated class is kept extremely small
186 on purpose. That way the (templated thus inlined) uses of this class will
187 only create a small amount of machine code, in contrast to e.g. STL.
188*/
189
190template <typename T>
191class THnT: public THn {
192public:
193 THnT() {}
194
195 THnT(const char* name, const char* title,
196 Int_t dim, const Int_t* nbins,
197 const Double_t* xmin, const Double_t* xmax):
198 THn(name, title, dim, nbins, xmin, xmax),
199 fArray(dim, nbins, true) {}
200
201 THnT(const char *name, const char *title, const std::vector<TAxis> &axes) : THn(name, title, axes)
202 {
203 const Int_t dim = axes.size();
204 std::vector<Int_t> nbins(dim);
205 for (Int_t i = 0; i < dim; i++)
206 nbins[i] = axes.at(i).GetNbins();
207 fArray = TNDArrayT<T>(dim, nbins.data(), true);
208 }
209
210 THnT(const char *name, const char *title, Int_t dim, const Int_t *nbins,
211 const std::vector<std::vector<double>> &xbins)
212 : THn(name, title, dim, nbins, xbins), fArray(dim, nbins, true)
213 {
214 }
215
216 const TNDArray& GetArray() const override { return fArray; }
217 TNDArray& GetArray() override { return fArray; }
218
219protected:
220 TNDArrayT<T> fArray; ///< Bin content
221 ClassDefOverride(THnT, 1); ///< Multi-dimensional histogram with templated storage
222};
223
231
232#endif // ROOT_THN
#define d(i)
Definition RSha256.hxx:102
bool Bool_t
Boolean (0=false, 1=true) (bool)
Definition RtypesCore.h:77
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
long long Long64_t
Portable signed long integer 8 bytes.
Definition RtypesCore.h:83
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
const char Option_t
Option string (const char)
Definition RtypesCore.h:80
#define ClassDefOverride(name, id)
Definition Rtypes.h:348
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t option
char name[80]
Definition TGX11.cxx:110
float xmin
float xmax
THnT< Int_t > THnI
Definition THn.h:228
THnT< Long64_t > THnL
Definition THn.h:229
THnT< Float_t > THnF
Definition THn.h:224
THnT< Short_t > THnS
Definition THn.h:227
THnT< Double_t > THnD
Definition THn.h:225
THnT< Long64_t > THnL64
Definition THn.h:230
THnT< Char_t > THnC
Definition THn.h:226
Iterator over THnBase bins (internal implementation).
Definition THnBase.h:327
virtual Int_t FindBin(Double_t x)
Find bin number corresponding to abscissa x.
Definition TAxis.cxx:293
virtual Int_t FindFixBin(Double_t x) const
Find bin number corresponding to abscissa x
Definition TAxis.cxx:422
1-Dim function class
Definition TF1.h:182
1-D histogram with a double per channel (see TH1 documentation)
Definition TH1.h:926
TH1 is the base class of all histogram classes in ROOT.
Definition TH1.h:109
2-D histogram with a double per channel (see TH1 documentation)
Definition TH2.h:400
3-D histogram with a double per channel (see TH1 documentation)
Definition TH3.h:424
Multidimensional histogram base.
Definition THnBase.h:45
void AddBinContent(const Int_t *x, Double_t v=1.)
Definition THnBase.h:195
TH1D * Projection(Int_t xDim, Option_t *option="") const
Project all bins into a 1-dimensional histogram, keeping only axis "xDim".
Definition THnBase.h:230
Int_t GetNdimensions() const
Definition THnBase.h:144
static THnBase * CreateHnAny(const char *name, const char *title, const TH1 *h1, Bool_t sparse, Int_t chunkSize=1024 *16)
Create a THn / THnSparse object from a histogram deriving from TH1.
Definition THnBase.cxx:335
THnBase * RebinBase(Int_t group) const
Combine the content of "group" neighboring bins into a new bin and return the resulting THnBase.
Definition THnBase.cxx:1250
THnBase * ProjectionND(Int_t ndim, const Int_t *dim, Option_t *option="") const
Project histogram onto the axes specified in dim.
Definition THnBase.h:269
Bool_t GetCalculateErrors() const
Definition THnBase.h:145
Double_t GetBinContent(const Int_t *idx) const
Definition THnBase.h:201
void SetBinContent(const Int_t *idx, Double_t v)
Definition THnBase.h:211
TAxis * GetAxis(Int_t dim) const
Definition THnBase.h:134
Int_t fNdimensions
Number of dimensions.
Definition THnBase.h:47
void FillBinBase(Double_t w)
Increment the statistics due to filled weight "w",.
Definition THnBase.h:93
Efficient multidimensional histogram.
Definition THnSparse.h:37
Templated implementation of the abstract base THn.
Definition THn.h:191
const TNDArray & GetArray() const override
Definition THn.h:216
TNDArrayT< T > fArray
Bin content.
Definition THn.h:220
THnT()
Definition THn.h:193
THnT(const char *name, const char *title, Int_t dim, const Int_t *nbins, const Double_t *xmin, const Double_t *xmax)
Definition THn.h:195
THnT(const char *name, const char *title, Int_t dim, const Int_t *nbins, const std::vector< std::vector< double > > &xbins)
Definition THn.h:210
THnT(const char *name, const char *title, const std::vector< TAxis > &axes)
Definition THn.h:201
TNDArray & GetArray() override
Definition THn.h:217
Multidimensional histogram.
Definition THn.h:30
void AllocCoordBuf() const
Create the coordinate buffer.
Definition THn.cxx:240
void FillBin(Long64_t bin, Double_t w) override
Increment the bin content of "bin" by "w", return the bin index.
Definition THn.h:87
void InitStorage(Int_t *nbins, Int_t chunkSize) override
Initialize the storage of a histogram created via Init()
Definition THn.cxx:248
Long64_t GetBin(const char *name[], Bool_t=kTRUE) override
Definition THn.h:82
static THn * CreateHn(const char *name, const char *title, const TH1 *h1)
Definition THn.h:46
THn * Rebin(Int_t group) const
Definition THn.h:142
Long64_t GetBin(const Int_t *idx, Bool_t=kTRUE) override
Definition THn.h:76
Long64_t GetBin(const Double_t *x, Bool_t=kTRUE) override
Definition THn.h:79
~THn() override
Destruct a THn.
Definition THn.cxx:207
void AddBinError2(Long64_t bin, Double_t e2) override
Definition THn.h:107
THn * Rebin(const Int_t *group) const
Definition THn.h:145
static THn * CreateHn(const char *name, const char *title, const THnBase *hn)
Definition THn.h:49
std::vector< Int_t > fCoordBuf
Definition THn.h:153
TNDArrayT< Double_t > fSumw2
Definition THn.h:152
void SetBinError2(Long64_t bin, Double_t e2) override
Definition THn.h:99
Double_t GetBinContent(Long64_t bin, Int_t *idx=nullptr) const override
Get the content of bin, and set its index if idx is != 0.
Definition THn.h:112
Double_t GetBinError2(Long64_t linidx) const override
Definition THn.h:124
ROOT::Internal::THnBaseBinIter * CreateIter(Bool_t respectAxisRange) const override
Create an iterator over all bins. Public interface is THnIter.
Definition THn.cxx:215
void Reset(Option_t *option="") override
Reset the contents of a THn.
Definition THn.cxx:258
Long64_t GetBin(const Int_t *idx) const override
Definition THn.h:56
Long64_t GetBin(const char *name[]) const override
Definition THn.h:67
void Sumw2() override
Enable calculation of errors.
Definition THn.cxx:224
THn()=default
Long64_t GetBin(const Double_t *x) const override
Definition THn.h:59
void AddBinContent(Long64_t bin, Double_t v=1.) override
Definition THn.h:104
Long64_t GetNbins() const override
Definition THn.h:54
void SetBinContent(Long64_t bin, Double_t v) override
Definition THn.h:96
virtual TNDArray & GetArray()=0
virtual const TNDArray & GetArray() const =0
THn * Projection(Int_t ndim, const Int_t *dim, Option_t *option="") const
Definition THn.h:137
N-Dim array class.
Definition TNDArray.h:46
Double_t x[n]
Definition legend1.C:17
TH1F * h1
Definition legend1.C:5