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() { delete [] fCounter; }
38
39 Long64_t Next(Int_t* coord = 0);
40 Int_t GetCoord(Int_t dim) const { return fCounter[dim].i; }
41 private:
42 THnBinIter(const THnBinIter&); // intentionally unimplemented
43 THnBinIter& operator=(const THnBinIter&); // 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 Hist
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
190////////////////////////////////////////////////////////////////////////////////
191/// Destruct a THn
192
194{
195 delete [] fCoordBuf;
196}
197
198
199////////////////////////////////////////////////////////////////////////////////
200/// Create an iterator over all bins. Public interface is THnIter.
201
203{
204 return new THnBinIter(GetNdimensions(), GetListOfAxes(), &GetArray(),
205 respectAxisRange);
206}
207
208////////////////////////////////////////////////////////////////////////////////
209/// Enable calculation of errors
210
212 if (!GetCalculateErrors()) {
213 fTsumw2 = 0.;
214 }
215 // fill sumw2 array with current content
216 TNDArray & content = GetArray();
217 Long64_t nbins = GetNbins();
218 for (Long64_t ibin = 0; ibin < nbins; ++ibin)
219 fSumw2.At(ibin) = content.AtAsDouble(ibin);
220}
221
222
223////////////////////////////////////////////////////////////////////////////////
224/// Create the coordinate buffer. Outlined to hide allocation
225/// from inlined functions.
226
228{
230}
231
232////////////////////////////////////////////////////////////////////////////////
233/// Initialize the storage of a histogram created via Init()
234
235void THn::InitStorage(Int_t* nbins, Int_t /*chunkSize*/)
236{
238 GetArray().Init(fNdimensions, nbins, true /*addOverflow*/);
239 fSumw2.Init(fNdimensions, nbins, true /*addOverflow*/);
240}
241
242////////////////////////////////////////////////////////////////////////////////
243/// Reset the contents of a THn.
244
245void THn::Reset(Option_t* option /*= ""*/)
246{
247 GetArray().Reset(option);
248 fSumw2.Reset(option);
249}
#define d(i)
Definition RSha256.hxx:102
int Int_t
Definition RtypesCore.h:45
bool Bool_t
Definition RtypesCore.h:63
double Double_t
Definition RtypesCore.h:59
long long Long64_t
Definition RtypesCore.h:73
const Bool_t kTRUE
Definition RtypesCore.h:91
const char Option_t
Definition RtypesCore.h:66
#define ClassImp(name)
Definition Rtypes.h:364
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:285
virtual Int_t GetCoord(Int_t dim) const =0
virtual Long64_t Next(Int_t *coord=0)=0
Class to manage histogram axis.
Definition TAxis.h:30
@ kAxisRange
Definition TAxis.h:61
Int_t GetLast() const
Return last bin on the axis i.e.
Definition TAxis.cxx:469
Int_t GetNbins() const
Definition TAxis.h:121
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:123
Int_t GetNdimensions() const
Definition THnBase.h:135
Bool_t GetCalculateErrors() const
Definition THnBase.h:136
Double_t fTsumw2
Definition THnBase.h:50
Int_t fNdimensions
Definition THnBase.h:45
Multidimensional histogram.
Definition THn.h:30
THn()
Definition THn.h:39
void AllocCoordBuf() const
Create the coordinate buffer.
Definition THn.cxx:227
void InitStorage(Int_t *nbins, Int_t chunkSize)
Initialize the storage of a histogram created via Init()
Definition THn.cxx:235
Int_t * fCoordBuf
Definition THn.h:183
virtual ~THn()
Destruct a THn.
Definition THn.cxx:193
TNDArrayT< Double_t > fSumw2
Definition THn.h:182
void Reset(Option_t *option="")
Reset the contents of a THn.
Definition THn.cxx:245
Long64_t GetNbins() const
Definition THn.h:54
void Sumw2()
Enable calculation of errors.
Definition THn.cxx:211
virtual const TNDArray & GetArray() const =0
ROOT::Internal::THnBaseBinIter * CreateIter(Bool_t respectAxisRange) const
Create an iterator over all bins. Public interface is THnIter.
Definition THn.cxx:202
void Reset(Option_t *="")
Definition TNDArray.h:142
void Init(Int_t ndim, const Int_t *nbins, bool addOverflow=false)
Definition TNDArray.h:135
T At(const Int_t *idx) const
Definition TNDArray.h:159
virtual Double_t AtAsDouble(ULong64_t linidx) const =0
Long64_t GetCellSize(Int_t dim) const
Definition TNDArray.h:73
virtual void Init(Int_t ndim, const Int_t *nbins, bool addOverflow=false)
Definition TNDArray.h:56
virtual void Reset(Option_t *option="")=0
An array of TObjects.
Definition TObjArray.h:37
TObject * At(Int_t idx) const
Definition TObjArray.h:166
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
Definition TObject.h:187
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
Definition first.py:1