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);
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
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 virtual ~THn();
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
53 ROOT::Internal::THnBaseBinIter* CreateIter(Bool_t respectAxisRange) const;
54 Long64_t GetNbins() const { return GetArray().GetNbins(); }
55
56 Long64_t GetBin(const Int_t* idx) const {
57 return GetArray().GetBin(idx);
58 }
59 Long64_t GetBin(const Double_t* x) const {
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 {
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) {
77 return const_cast<const THn*>(this)->GetBin(idx);
78 }
79 Long64_t GetBin(const Double_t* x, Bool_t /*allocate*/ = kTRUE) {
80 return const_cast<const THn*>(this)->GetBin(x);
81 }
82 Long64_t GetBin(const char* name[], Bool_t /*allocate*/ = kTRUE) {
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) {
88 GetArray().AddAt(bin, w);
89 if (GetCalculateErrors()) {
90 fSumw2.AddAt(bin, w * w);
91 }
92 FillBinBase(w);
93 }
94
95 /// Forwards to THnBase::SetBinContent().
96 /// Non-virtual, CINT-compatible replacement of a using declaration.
97 void SetBinContent(const Int_t* idx, Double_t v) {
99 }
101 GetArray().SetAsDouble(bin, v);
102 }
104 if (!GetCalculateErrors()) Sumw2();
105 fSumw2.At(bin) = e2;
106 }
107 /// Forwards to THnBase::SetBinContent().
108 /// Non-virtual, CINT-compatible replacement of a using declaration.
109 void AddBinContent(const Int_t* idx, Double_t v = 1.) {
111 }
113 GetArray().AddAt(bin, v);
114 }
116 fSumw2.At(bin) += e2;
117 }
118 /// Forwards to THnBase::GetBinContent() overload.
119 /// Non-virtual, CINT-compatible replacement of a using declaration.
120 Double_t GetBinContent(const Int_t *idx) const {
121 return THnBase::GetBinContent(idx);
122 }
123 /// Get the content of bin, and set its index if idx is != 0.
124 Double_t GetBinContent(Long64_t bin, Int_t* idx = 0) const {
125 if (idx) {
126 const TNDArray& arr = GetArray();
127 Long64_t prevCellSize = arr.GetNbins();
128 for (Int_t i = 0; i < GetNdimensions(); ++i) {
129 Long64_t cellSize = arr.GetCellSize(i);
130 idx[i] = (bin % prevCellSize) / cellSize;
131 prevCellSize = cellSize;
132 }
133 }
134 return GetArray().AtAsDouble(bin);
135 }
137 return GetCalculateErrors() ? fSumw2.At(linidx) : GetBinContent(linidx);
138 }
139
140 virtual const TNDArray& GetArray() const = 0;
141 virtual TNDArray& GetArray() = 0;
142
143 void Sumw2();
144
145 /// Forwards to THnBase::Projection().
146 /// Non-virtual, as a CINT-compatible replacement of a using declaration.
147 TH1D* Projection(Int_t xDim, Option_t* option = "") const {
148 return THnBase::Projection(xDim, option);
149 }
150
151 /// Forwards to THnBase::Projection().
152 /// Non-virtual, as a CINT-compatible replacement of a using declaration.
154 Option_t* option = "") const {
155 return THnBase::Projection(yDim, xDim, option);
156 }
157
158 /// Forwards to THnBase::Projection().
159 /// Non-virtual, as a CINT-compatible replacement of a using declaration.
160 TH3D* Projection(Int_t xDim, Int_t yDim, Int_t zDim,
161 Option_t* option = "") const {
162 return THnBase::Projection(xDim, yDim, zDim, option);
163 }
164
165 THn* Projection(Int_t ndim, const Int_t* dim,
166 Option_t* option = "") const {
167 return (THn*) ProjectionND(ndim, dim, option);
168 }
169
171 return (THn*) RebinBase(group);
172 }
173 THn* Rebin(const Int_t* group) const {
174 return (THn*) RebinBase(group);
175 }
176
177 void Reset(Option_t* option = "");
178
179protected:
180 TNDArrayT<Double_t> fSumw2; // bin error, lazy allocation happens in TNDArrayT
181 mutable std::vector<Int_t> fCoordBuf; //! Temporary buffer
182
183 ClassDef(THn, 1); //Base class for multi-dimensional histogram
184};
185
186
187//______________________________________________________________________________
188/** \class THnT
189 Templated implementation of the abstract base THn.
190 All functionality and the interfaces to be used are in THn!
191
192 THn does not know how to store any bin content itself. Instead, this
193 is delegated to the derived, templated class: the template parameter decides
194 what the format for the bin content is. The actual storage is delegated to
195 TNDArrayT<T>.
196
197 Typedefs exist for template parameters with ROOT's generic types:
198
199 Templated name | Typedef | Bin content type
200 -----------------|---------------|--------------------
201 THnT<Char_t> | THnC | Char_t
202 THnT<Short_t> | THnS | Short_t
203 THnT<Int_t> | THnI | Int_t
204 THnT<Long_t> | THnL | Long_t
205 THnT<Float_t> | THnF | Float_t
206 THnT<Double_t> | THnD | Double_t
207
208 We recommend to use THnC wherever possible, and to map its value space
209 of 256 possible values to e.g. float values outside the class. This saves an
210 enormous amount of memory. Only if more than 256 values need to be
211 distinguished should e.g. THnS or even THnF be chosen.
212
213 Implementation detail: the derived, templated class is kept extremely small
214 on purpose. That way the (templated thus inlined) uses of this class will
215 only create a small amount of machine code, in contrast to e.g. STL.
216*/
217
218template <typename T>
219class THnT: public THn {
220public:
221 THnT() {}
222
223 THnT(const char* name, const char* title,
224 Int_t dim, const Int_t* nbins,
225 const Double_t* xmin, const Double_t* xmax):
226 THn(name, title, dim, nbins, xmin, xmax),
227 fArray(dim, nbins, true) {}
228
229 THnT(const char *name, const char *title, Int_t dim, const Int_t *nbins,
230 const std::vector<std::vector<double>> &xbins)
231 : THn(name, title, dim, nbins, xbins), fArray(dim, nbins, true)
232 {
233 }
234
235 const TNDArray& GetArray() const { return fArray; }
236 TNDArray& GetArray() { return fArray; }
237
238protected:
239 TNDArrayT<T> fArray; ///< Bin content
240 ClassDef(THnT, 1); ///< Multi-dimensional histogram with templated storage
241};
242
250
251#endif // ROOT_THN
#define d(i)
Definition RSha256.hxx:102
const Bool_t kFALSE
Definition RtypesCore.h:101
bool Bool_t
Definition RtypesCore.h:63
double Double_t
Definition RtypesCore.h:59
long long Long64_t
Definition RtypesCore.h:80
const Bool_t kTRUE
Definition RtypesCore.h:100
const char Option_t
Definition RtypesCore.h:66
#define ClassDef(name, id)
Definition Rtypes.h:325
char name[80]
Definition TGX11.cxx:110
float xmin
float xmax
THnT< Int_t > THnI
Definition THn.h:247
THnT< Long_t > THnL
Definition THn.h:248
THnT< Float_t > THnF
Definition THn.h:243
THnT< Short_t > THnS
Definition THn.h:246
THnT< Double_t > THnD
Definition THn.h:244
THnT< Long64_t > THnL64
Definition THn.h:249
THnT< Char_t > THnC
Definition THn.h:245
Iterator over THnBase bins (internal implementation).
Definition THnBase.h:312
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:419
1-Dim function class
Definition TF1.h:213
1-D histogram with a double per channel (see TH1 documentation)}
Definition TH1.h:618
TH1 is the base class of all histogram classes in ROOT.
Definition TH1.h:58
2-D histogram with a double per channel (see TH1 documentation)}
Definition TH2.h:292
3-D histogram with a double per channel (see TH1 documentation)}
Definition TH3.h:305
Multidimensional histogram base.
Definition THnBase.h:43
void AddBinContent(const Int_t *x, Double_t v=1.)
Definition THnBase.h:191
TH1D * Projection(Int_t xDim, Option_t *option="") const
Project all bins into a 1-dimensional histogram, keeping only axis "xDim".
Definition THnBase.h:224
Int_t GetNdimensions() const
Definition THnBase.h:140
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:296
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:1168
THnBase * ProjectionND(Int_t ndim, const Int_t *dim, Option_t *option="") const
Definition THnBase.h:255
Bool_t GetCalculateErrors() const
Definition THnBase.h:141
Double_t GetBinContent(const Int_t *idx) const
Definition THnBase.h:195
void SetBinContent(const Int_t *idx, Double_t v)
Definition THnBase.h:205
TAxis * GetAxis(Int_t dim) const
Definition THnBase.h:130
Int_t fNdimensions
Number of dimensions.
Definition THnBase.h:45
void FillBinBase(Double_t w)
Increment the statistics due to filled weight "w",.
Definition THnBase.h:89
Efficient multidimensional histogram.
Definition THnSparse.h:36
Templated implementation of the abstract base THn.
Definition THn.h:219
const TNDArray & GetArray() const
Definition THn.h:235
TNDArrayT< T > fArray
Bin content.
Definition THn.h:239
THnT()
Definition THn.h:221
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:223
THnT(const char *name, const char *title, Int_t dim, const Int_t *nbins, const std::vector< std::vector< double > > &xbins)
Definition THn.h:229
TNDArray & GetArray()
Definition THn.h:236
Multidimensional histogram.
Definition THn.h:30
void AllocCoordBuf() const
Create the coordinate buffer.
Definition THn.cxx:232
void FillBin(Long64_t bin, Double_t w)
Increment the bin content of "bin" by "w", return the bin index.
Definition THn.h:87
Double_t GetBinError2(Long64_t linidx) const
Definition THn.h:136
TH1D * Projection(Int_t xDim, Option_t *option="") const
Forwards to THnBase::Projection().
Definition THn.h:147
void AddBinContent(const Int_t *idx, Double_t v=1.)
Forwards to THnBase::SetBinContent().
Definition THn.h:109
void InitStorage(Int_t *nbins, Int_t chunkSize)
Initialize the storage of a histogram created via Init()
Definition THn.cxx:240
static THn * CreateHn(const char *name, const char *title, const TH1 *h1)
Definition THn.h:46
void AddBinContent(Long64_t bin, Double_t v=1.)
Definition THn.h:112
THn * Rebin(Int_t group) const
Definition THn.h:170
Double_t GetBinContent(const Int_t *idx) const
Forwards to THnBase::GetBinContent() overload.
Definition THn.h:120
Long64_t GetBin(const Int_t *idx) const
Definition THn.h:56
Long64_t GetBin(const char *name[], Bool_t=kTRUE)
Definition THn.h:82
void SetBinContent(Long64_t bin, Double_t v)
Definition THn.h:100
Long64_t GetBin(const char *name[]) const
Definition THn.h:67
TH2D * Projection(Int_t yDim, Int_t xDim, Option_t *option="") const
Forwards to THnBase::Projection().
Definition THn.h:153
THn * Rebin(const Int_t *group) const
Definition THn.h:173
virtual ~THn()
Destruct a THn.
Definition THn.cxx:199
static THn * CreateHn(const char *name, const char *title, const THnBase *hn)
Definition THn.h:49
Long64_t GetBin(const Int_t *idx, Bool_t=kTRUE)
Definition THn.h:76
std::vector< Int_t > fCoordBuf
Definition THn.h:181
TNDArrayT< Double_t > fSumw2
Definition THn.h:180
void SetBinContent(const Int_t *idx, Double_t v)
Forwards to THnBase::SetBinContent().
Definition THn.h:97
Double_t GetBinContent(Long64_t bin, Int_t *idx=0) const
Get the content of bin, and set its index if idx is != 0.
Definition THn.h:124
void Reset(Option_t *option="")
Reset the contents of a THn.
Definition THn.cxx:250
Long64_t GetBin(const Double_t *x, Bool_t=kTRUE)
Definition THn.h:79
Long64_t GetNbins() const
Definition THn.h:54
THn()=default
TH3D * Projection(Int_t xDim, Int_t yDim, Int_t zDim, Option_t *option="") const
Forwards to THnBase::Projection().
Definition THn.h:160
void AddBinError2(Long64_t bin, Double_t e2)
Definition THn.h:115
virtual TNDArray & GetArray()=0
void Sumw2()
Enable calculation of errors.
Definition THn.cxx:216
virtual const TNDArray & GetArray() const =0
Long64_t GetBin(const Double_t *x) const
Definition THn.h:59
THn * Projection(Int_t ndim, const Int_t *dim, Option_t *option="") const
Definition THn.h:165
ROOT::Internal::THnBaseBinIter * CreateIter(Bool_t respectAxisRange) const
Create an iterator over all bins. Public interface is THnIter.
Definition THn.cxx:207
void SetBinError2(Long64_t bin, Double_t e2)
Definition THn.h:103
void AddAt(ULong64_t linidx, Double_t value)
Definition TNDArray.h:165
T At(const Int_t *idx) const
Definition TNDArray.h:138
N-Dim array class.
Definition TNDArray.h:46
virtual void AddAt(ULong64_t linidx, Double_t value)=0
virtual Double_t AtAsDouble(ULong64_t linidx) const =0
Long64_t GetNbins() const
Definition TNDArray.h:69
Long64_t GetCellSize(Int_t dim) const
Definition TNDArray.h:70
Long64_t GetBin(const Int_t *idx) const
Definition TNDArray.h:72
virtual void SetAsDouble(ULong64_t linidx, Double_t value)=0
Double_t x[n]
Definition legend1.C:17
TH1F * h1
Definition legend1.C:5