Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
THn.cxx
Go to the documentation of this file.
1// @(#)root/hist:$Id$
2// Author: Axel Naumann (2011-12-13)
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#include "THn.h"
13
14namespace {
15 //______________________________________________________________________________
16 //
17 // Helper struct to hold one dimension's bin range for THnBinIter.
18 /////////////////////////////////////////////////////////////////////////////
19
20 struct CounterRange_t {
21 Int_t i;
23 Int_t last;
24 Int_t len;
25 Long64_t cellSize;
26 };
27
28 //______________________________________________________________________________
29 //
30 // THnBinIter iterates over all bins of a THn, recursing over all dimensions.
31 /////////////////////////////////////////////////////////////////////////////
32
33 class THnBinIter: public ROOT::Internal::THnBaseBinIter {
34 public:
35 THnBinIter(Int_t dim, const TObjArray* axes, const TNDArray* arr,
36 Bool_t respectAxisRange);
37 ~THnBinIter() override { delete [] fCounter; }
38
39 Long64_t Next(Int_t* coord = nullptr) override;
40 Int_t GetCoord(Int_t dim) const override { return fCounter[dim].i; }
41 private:
42 THnBinIter(const THnBinIter&) = delete; // intentionally unimplemented
43 THnBinIter& operator=(const THnBinIter&) = delete; // intentionally unimplemented
44
45 public:
46 Int_t fNdimensions;
47 Long64_t fIndex;
48 const TNDArray* fArray;
49 CounterRange_t* fCounter;
50 };
51
52
53 /////////////////////////////////////////////////////////////////////////////
54 /// Construct a THnBinIter.
55
56 THnBinIter::THnBinIter(Int_t dim, const TObjArray* axes,
57 const TNDArray* arr, Bool_t respectAxisRange):
58 ROOT::Internal::THnBaseBinIter(respectAxisRange),
59 fNdimensions(dim), fIndex(-1), fArray(arr) {
60 fCounter = new CounterRange_t[dim]();
61 for (Int_t i = 0; i < dim; ++i) {
62 TAxis *axis = (TAxis*) axes->At(i);
63 fCounter[i].len = axis->GetNbins() + 2;
64 fCounter[i].cellSize = arr->GetCellSize(i);
65 if (!respectAxisRange || !axis->TestBit(TAxis::kAxisRange)) {
66 fCounter[i].first = 0;
67 fCounter[i].last = fCounter[i].len - 1;
68 fCounter[i].i = 0;
69 continue;
70 }
71 fHaveSkippedBin = kTRUE;
72 Int_t min = axis->GetFirst();
73 Int_t max = axis->GetLast();
74 if (min == 0 && max == 0) {
75 // special case where TAxis::SetBit(kAxisRange) and
76 // over- and underflow bins are de-selected.
77 // first and last are == 0 due to axis12->SetRange(1, axis12->GetNbins());
78 min = 1;
79 max = axis->GetNbins();
80 }
81 fCounter[i].first = min;
82 fCounter[i].last = max;
83 fCounter[i].i = min;
84 fIndex += fCounter[i].first * fCounter[i].cellSize;
85 }
86 // First Next() will increment it:
87 --fCounter[dim - 1].i;
88 }
89
90 /////////////////////////////////////////////////////////////////////////////
91 /// Return the current linear bin index (in range), then go to the next bin.
92 /// If all bins have been visited, return -1.
93
94 Long64_t THnBinIter::Next(Int_t* coord /*= 0*/) {
95 if (fNdimensions < 0) return -1; // end
96 ++fCounter[fNdimensions - 1].i;
97 ++fIndex;
98 // Wrap around if needed
99 for (Int_t d = fNdimensions - 1; d > 0 && fCounter[d].i > fCounter[d].last; --d) {
100 // We skip last + 1..size and 0..first - 1, adjust fIndex
101 Int_t skippedCells = fCounter[d].len - (fCounter[d].last + 1);
102 skippedCells += fCounter[d].first;
103 fIndex += skippedCells * fCounter[d].cellSize;
104 fCounter[d].i = fCounter[d].first;
105 ++fCounter[d - 1].i;
106 }
107 if (fCounter[0].i > fCounter[0].last) {
108 fNdimensions = -1;
109 return -1;
110 }
111 if (coord) {
112 for (Int_t d = 0; d < fNdimensions; ++d) {
113 coord[d] = fCounter[d].i;
114 }
115 }
116 return fIndex;
117 }
118} // unnamed namespce
119
120
121
122/** \class THn
123 \ingroup Histograms
124Multidimensional histogram.
125
126Use a THn if you really, really have to store more than three dimensions,
127and if a large fraction of all bins are filled.
128Better alternatives are
129 - THnSparse if a fraction of all bins are filled
130 - TTree
131
132The major problem of THn is the memory use caused by n-dimensional
133histogramming: a THnD with 8 dimensions and 100 bins per dimension needs
134more than 2.5GB of RAM!
135
136To construct a THn object you must use one of its templated, derived
137classes:
138
139 THnD (typedef for THnT<Double_t>): bin content held by a Double_t,
140 THnF (typedef for THnT<Float_t>): bin content held by a Float_t,
141 THnL (typedef for THnT<Long_t>): bin content held by a Long_t,
142 THnI (typedef for THnT<Int_t>): bin content held by an Int_t,
143 THnS (typedef for THnT<Short_t>): bin content held by a Short_t,
144 THnC (typedef for THnT<Char_t>): bin content held by a Char_t,
145
146They take name and title, the number of dimensions, and for each dimension
147the number of bins, the minimal, and the maximal value on the dimension's
148axis. A TH2F h("h","h",10, 0., 10., 20, -5., 5.) would correspond to
149
150 Int_t bins[2] = {10, 20};
151 Double_t xmin[2] = {0., -5.};
152 Double_t xmax[2] = {10., 5.};
153 THnF hn("hn", "hn", 2, bins, xmin, xmax);
154
155## Filling
156A THn is filled just like a regular histogram, using
157THn::Fill(x, weight), where x is a n-dimensional Double_t value.
158To take errors into account, Sumw2() must be called before filling the
159histogram.
160Storage is allocated when the first bin content is stored.
161
162## Projections
163The dimensionality of a THn can be reduced by projecting it to
1641, 2, 3, or n dimensions, which can be represented by a TH1, TH2, TH3, or
165a THn. See the Projection() members. To only project parts of the
166histogram, call
167
168 hn->GetAxis(12)->SetRange(from_bin, to_bin);
169
170## Conversion from other histogram classes
171The static factory function THn::CreateHn() can be used to create a THn
172from a TH1, TH2, TH3, THnSparse and (for copying) even from a THn. The
173created THn will have compatble storage type, i.e. calling CreateHn() on
174a TH2F will create a THnF.
175*/
176
178
179////////////////////////////////////////////////////////////////////////////////
180/// Construct a THn.
181
182THn::THn(const char* name, const char* title,
183 Int_t dim, const Int_t* nbins,
184 const Double_t* xmin, const Double_t* xmax):
185 THnBase(name, title, dim, nbins, xmin, xmax),
186 fSumw2(dim, nbins, kTRUE /*overflow*/),
187 fCoordBuf() {
188}
189
190THn::THn(const char *name, const char *title, Int_t dim, const Int_t *nbins,
191 const std::vector<std::vector<double>> &xbins)
192 : THnBase(name, title, dim, nbins, xbins), fSumw2(dim, nbins, kTRUE /*overflow*/), fCoordBuf()
193{
194}
195
196////////////////////////////////////////////////////////////////////////////////
197/// Destruct a THn
198
200{
201}
202
203
204////////////////////////////////////////////////////////////////////////////////
205/// Create an iterator over all bins. Public interface is THnIter.
206
208{
209 return new THnBinIter(GetNdimensions(), GetListOfAxes(), &GetArray(),
210 respectAxisRange);
211}
212
213////////////////////////////////////////////////////////////////////////////////
214/// Enable calculation of errors
215
217 if (!GetCalculateErrors()) {
218 fTsumw2 = 0.;
219 }
220 // fill sumw2 array with current content
221 TNDArray & content = GetArray();
222 Long64_t nbins = GetNbins();
223 for (Long64_t ibin = 0; ibin < nbins; ++ibin)
224 fSumw2.At(ibin) = content.AtAsDouble(ibin);
225}
226
227
228////////////////////////////////////////////////////////////////////////////////
229/// Create the coordinate buffer. Outlined to hide allocation
230/// from inlined functions.
231
233{
234 fCoordBuf.assign(fNdimensions, 0);
235}
236
237////////////////////////////////////////////////////////////////////////////////
238/// Initialize the storage of a histogram created via Init()
239
240void THn::InitStorage(Int_t* nbins, Int_t /*chunkSize*/)
241{
242 fCoordBuf.assign(fNdimensions, 0);
243 GetArray().Init(fNdimensions, nbins, true /*addOverflow*/);
244 fSumw2.Init(fNdimensions, nbins, true /*addOverflow*/);
245}
246
247////////////////////////////////////////////////////////////////////////////////
248/// Reset the contents of a THn.
249
251{
253 fSumw2.Reset(option);
254}
#define d(i)
Definition RSha256.hxx:102
bool Bool_t
Definition RtypesCore.h:63
int Int_t
Definition RtypesCore.h:45
long long Long64_t
Definition RtypesCore.h:80
constexpr Bool_t kTRUE
Definition RtypesCore.h:100
const char Option_t
Definition RtypesCore.h:66
#define ClassImp(name)
Definition Rtypes.h:377
Option_t Option_t option
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t UChar_t len
char name[80]
Definition TGX11.cxx:110
float xmin
float xmax
Binding & operator=(OUT(*fun)(void))
Iterator over THnBase bins (internal implementation).
Definition THnBase.h:312
virtual Int_t GetCoord(Int_t dim) const =0
virtual Long64_t Next(Int_t *coord=nullptr)=0
Class to manage histogram axis.
Definition TAxis.h:31
@ kAxisRange
Definition TAxis.h:65
Int_t GetLast() const
Return last bin on the axis i.e.
Definition TAxis.cxx:469
Int_t GetNbins() const
Definition TAxis.h:125
Int_t GetFirst() const
Return first bin on the axis i.e.
Definition TAxis.cxx:458
Multidimensional histogram base.
Definition THnBase.h:43
TObjArray * GetListOfAxes()
Definition THnBase.h:128
Int_t GetNdimensions() const
Definition THnBase.h:140
Bool_t GetCalculateErrors() const
Definition THnBase.h:141
Double_t fTsumw2
Total sum of weights squared; -1 if no errors are calculated.
Definition THnBase.h:50
Int_t fNdimensions
Number of dimensions.
Definition THnBase.h:45
Multidimensional histogram.
Definition THn.h:30
void AllocCoordBuf() const
Create the coordinate buffer.
Definition THn.cxx:232
void InitStorage(Int_t *nbins, Int_t chunkSize) override
Initialize the storage of a histogram created via Init()
Definition THn.cxx:240
~THn() override
Destruct a THn.
Definition THn.cxx:199
std::vector< Int_t > fCoordBuf
Definition THn.h:181
TNDArrayT< Double_t > fSumw2
Definition THn.h:180
ROOT::Internal::THnBaseBinIter * CreateIter(Bool_t respectAxisRange) const override
Create an iterator over all bins. Public interface is THnIter.
Definition THn.cxx:207
void Reset(Option_t *option="") override
Reset the contents of a THn.
Definition THn.cxx:250
void Sumw2() override
Enable calculation of errors.
Definition THn.cxx:216
THn()=default
Long64_t GetNbins() const override
Definition THn.h:54
virtual const TNDArray & GetArray() const =0
N-Dim array class.
Definition TNDArray.h:46
virtual Double_t AtAsDouble(ULong64_t linidx) const =0
Long64_t GetCellSize(Int_t dim) const
Definition TNDArray.h:70
virtual void Init(Int_t ndim, const Int_t *nbins, bool addOverflow=false)
Definition TNDArray.h:55
virtual void Reset(Option_t *option="")=0
An array of TObjects.
Definition TObjArray.h:31
TObject * At(Int_t idx) const override
Definition TObjArray.h:164
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
Definition TObject.h:199
This file contains a specialised ROOT message handler to test for diagnostic in unit tests.
__device__ AFloat max(AFloat x, AFloat y)
Definition Kernels.cuh:207
Definition first.py:1