Logo ROOT   6.10/09
Reference Guide
THist.hxx
Go to the documentation of this file.
1 /// \file ROOT/THist.h
2 /// \ingroup Hist ROOT7
3 /// \author Axel Naumann <axel@cern.ch>
4 /// \date 2015-03-23
5 /// \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback is welcome!
6 
7 /*************************************************************************
8  * Copyright (C) 1995-2015, Rene Brun and Fons Rademakers. *
9  * All rights reserved. *
10  * *
11  * For the licensing terms see $ROOTSYS/LICENSE. *
12  * For the list of contributors see $ROOTSYS/README/CREDITS. *
13  *************************************************************************/
14 
15 #ifndef ROOT7_THist
16 #define ROOT7_THist
17 
18 #include "ROOT/RArrayView.hxx"
19 #include "ROOT/TAxis.hxx"
20 #include "ROOT/TDrawable.hxx"
21 #include "ROOT/THistBinIter.hxx"
22 #include "ROOT/THistDrawable.hxx"
23 #include "ROOT/THistImpl.hxx"
24 #include "ROOT/THistData.hxx"
25 #include <initializer_list>
26 
27 namespace ROOT {
28 namespace Experimental {
29 
30 // fwd declare for fwd declare for friend declaration in THist...
31 template<int DIMENSIONS, class PRECISION,
32  template <int D_, class P_, template <class P__> class S_> class... STAT>
33 class THist;
34 
35 // fwd declare for friend declaration in THist.
36 template<int DIMENSIONS, class PRECISION,
37  template <int D_, class P_, template <class P__> class S_> class... STAT>
38 class THist<DIMENSIONS, PRECISION, STAT...>
39  HistFromImpl(std::unique_ptr<typename THist<DIMENSIONS, PRECISION, STAT...>::ImplBase_t> pHistImpl);
40 
41 /**
42  \class THist
43  Histogram class for histograms with `DIMENSIONS` dimensions, where each
44  bin count is stored by a value of type `PRECISION`. STAT stores statistical
45  data of the entries filled into the histogram (bin content, uncertainties etc).
46 
47  A histogram counts occurrences of values or n-dimensional combinations thereof.
48  Contrary to for instance a `TTree`, a histogram combines adjacent values. The
49  resolution of this combination is defined by the axis binning, see e.g.
50  http://www.wikiwand.com/en/Histogram
51  */
52 
53 template<int DIMENSIONS, class PRECISION,
54  template <int D_, class P_, template <class P__> class S_> class... STAT>
55 class THist {
56 public:
57  /// The type of the `Detail::THistImplBase` of this histogram.
58  using ImplBase_t
61  /// The coordinates type: a `DIMENSIONS`-dimensional `std::array` of `double`.
63  /// The type of weights
65  /// Pointer type to `HistImpl_t::Fill`, for faster access.
67  /// Range.
69 
71 
72  /// Number of dimensions of the coordinates
73  static constexpr int GetNDim() noexcept { return DIMENSIONS; }
74 
75  THist() = default;
76  THist(THist&&) = default;
77 
78  /// Create a histogram from an `array` of axes (`TAxisConfig`s). Example code:
79  ///
80  /// Construct a 1-dimensional histogram that can be filled with `floats`s.
81  /// The axis has 10 bins between 0. and 1. The two outermost sets of curly
82  /// braces are to reach the initialization of the `std::array` elements; the
83  /// inner one is for the initialization of a `TAxisCoordinate`.
84  ///
85  /// THist<1,float> h1f({{ {10, 0., 1.} }});
86  ///
87  /// Construct a 2-dimensional histogram, with the first axis as before, and
88  /// the second axis having non-uniform ("irregular") binning, where all bin-
89  /// edges are specified. As this is itself an array it must be enclosed by
90  /// double curlies.
91  ///
92  /// THist<2,int> h2i({{ {10, 0., 1.}, {{-1., 0., 1., 10., 100.}} }});
93  explicit THist(std::array<TAxisConfig, DIMENSIONS> axes);
94 
95  /// Constructor overload taking the histogram title
96  THist(std::string_view histTitle, std::array<TAxisConfig, DIMENSIONS> axes);
97 
98  /// Constructor overload that's only available for a 1-dimensional histogram.
99  template <int ENABLEIF_NDIM = DIMENSIONS,
101  explicit THist(const TAxisConfig &xaxis):
102  THist(std::array<TAxisConfig, 1>{{xaxis}})
103  { }
104 
105  /// Constructor overload that's only available for a 1-dimensional histogram,
106  /// also passing the histogram title.
107  template <int ENABLEIF_NDIM = DIMENSIONS,
109  THist(std::string_view histTitle, const TAxisConfig &xaxis):
110  THist(histTitle, std::array<TAxisConfig, 1>{{xaxis}})
111  { }
112 
113  /// Constructor overload that's only available for a 2-dimensional histogram.
114  template<int ENABLEIF_NDIM = DIMENSIONS,
116  THist(const TAxisConfig &xaxis, const TAxisConfig &yaxis):
117  THist(std::array<TAxisConfig, 2>{{xaxis, yaxis}})
118  { }
119 
120  /// Constructor overload that's only available for a 2-dimensional histogram,
121  /// also passing the histogram title.
122  template<int ENABLEIF_NDIM = DIMENSIONS,
124  THist(std::string_view histTitle, const TAxisConfig &xaxis, const TAxisConfig &yaxis):
125  THist(histTitle, std::array<TAxisConfig, 2>{{xaxis, yaxis}})
126  { }
127 
128  /// Constructor overload that's only available for a 3-dimensional histogram.
129  template<int ENABLEIF_NDIM = DIMENSIONS,
131  THist(const TAxisConfig &xaxis, const TAxisConfig &yaxis, const TAxisConfig &zaxis):
132  THist(std::array<TAxisConfig, 3>{{xaxis, yaxis, zaxis}})
133  { }
134 
135  /// Constructor overload that's only available for a 3-dimensional histogram,
136  /// also passing the histogram title.
137  template<int ENABLEIF_NDIM = DIMENSIONS,
139  THist(std::string_view histTitle,
140  const TAxisConfig &xaxis, const TAxisConfig &yaxis, const TAxisConfig &zaxis):
141  THist(histTitle, std::array<TAxisConfig, 3>{{xaxis, yaxis, zaxis}})
142  { }
143 
144 
145  /// Access the ImplBase_t this THist points to.
146  ImplBase_t *GetImpl() const noexcept { return fImpl.get(); }
147 
148  /// "Steal" the ImplBase_t this THist points to.
149  std::unique_ptr<ImplBase_t>&& TakeImpl() noexcept { return std::move(fImpl); }
150 
151  /// Add `weight` to the bin containing coordinate `x`.
152  void Fill(const CoordArray_t &x, Weight_t weight = (Weight_t) 1) noexcept { (fImpl.get()->*fFillFunc)(x, weight); }
153 
154  /// For each coordinate in `xN`, add `weightN[i]` to the bin at coordinate
155  /// `xN[i]`. The sizes of `xN` and `weightN` must be the same. This is more
156  /// efficient than many separate calls to `Fill()`.
157  void FillN(const std::array_view <CoordArray_t> xN,
158  const std::array_view <Weight_t> weightN) noexcept { fImpl->FillN(xN, weightN); }
159 
160  /// Convenience overload: `FillN()` with weight 1.
161  void FillN(const std::array_view <CoordArray_t> xN) noexcept { fImpl->FillN(xN); }
162 
163  /// Get the number of entries this histogram was filled with.
164  int64_t GetEntries() const noexcept { return fImpl->GetStat().GetEntries(); }
165 
166  /// Get the content of the bin at `x`.
167  Weight_t GetBinContent(const CoordArray_t &x) const { return fImpl->GetBinContent(x); }
168 
169  /// Get the uncertainty on the content of the bin at `x`.
170  double GetBinUncertainty(const CoordArray_t &x) const { return fImpl->GetBinUncertainty(x); }
171 
172  const_iterator begin() const { return const_iterator(*fImpl); }
173 
174  const_iterator end() const { return const_iterator(*fImpl, fImpl->GetNBins()); }
175 
176  /// Swap *this and other.
177  ///
178  /// Very efficient; swaps the `fImpl` pointers.
180  std::swap(fImpl, other.fImpl);
181  std::swap(fFillFunc, other.fFillFunc);
182  }
183 
184 private:
185  std::unique_ptr<ImplBase_t> fImpl; ///< The actual histogram implementation
186  FillFunc_t fFillFunc = nullptr; ///<! Pinter to THistImpl::Fill() member function
187 
188  friend THist HistFromImpl<>(std::unique_ptr<ImplBase_t>);
189 };
190 
191 /// THist with no STAT parameter uses THistStatContent by default.
192 template<int DIMENSIONS, class PRECISION>
193 class THist<DIMENSIONS, PRECISION>:
194  public THist<DIMENSIONS, PRECISION, THistStatContent>
195 {
197 };
198 
199 
200 /// Swap two histograms.
201 ///
202 /// Very efficient; swaps the `fImpl` pointers.
203 template<int DIMENSIONS, class PRECISION,
204  template <int D_, class P_, template <class P__> class S_> class... STAT>
207 {
208  a.swap(b);
209 };
210 
211 
212 namespace Internal {
213 /**
214  Generate THist::fImpl from THist constructor arguments.
215  */
216 template<int NDIM, int IDIM, class DATA, class... PROCESSEDAXISCONFIG>
217 struct THistImplGen {
218  /// Select the template argument for the next axis type, and "recurse" into
219  /// THistImplGen for the next axis.
220  template<TAxisConfig::EKind KIND>
221  std::unique_ptr<Detail::THistImplBase<DATA>>
222  MakeNextAxis(std::string_view title, const std::array<TAxisConfig, NDIM> &axes,
223  PROCESSEDAXISCONFIG... processedAxisArgs)
224  {
225  using NextAxis_t = typename AxisConfigToType<KIND>::Axis_t;
226  NextAxis_t nextAxis = AxisConfigToType<KIND>()(axes[IDIM]);
227  using HistImpl_t = THistImplGen<NDIM, IDIM + 1, DATA, PROCESSEDAXISCONFIG..., NextAxis_t>;
228  return HistImpl_t()(title, axes, processedAxisArgs..., nextAxis);
229  }
230 
231  /// Make a THistImpl-derived object reflecting the TAxisConfig array.
232  ///
233  /// Delegate to the appropriate MakeNextAxis instantiation, depending on the
234  /// axis type selected in the TAxisConfig.
235  /// \param axes - `TAxisConfig` objects describing the axis of the resulting
236  /// THistImpl.
237  /// \param statConfig - the statConfig parameter to be passed to the THistImpl
238  /// \param processedAxisArgs - the TAxisBase-derived axis objects describing the
239  /// axes of the resulting THistImpl. There are `IDIM` of those; in the end
240  /// (`IDIM` == `GetNDim()`), all `axes` have been converted to
241  /// `processedAxisArgs` and the THistImpl constructor can be invoked, passing
242  /// the `processedAxisArgs`.
243  std::unique_ptr<Detail::THistImplBase<DATA>>
244  operator()(std::string_view title, const std::array <TAxisConfig, NDIM> &axes,
245  PROCESSEDAXISCONFIG... processedAxisArgs)
246  {
247  switch (axes[IDIM].GetKind()) {
249  return MakeNextAxis<TAxisConfig::kEquidistant>(title, axes, processedAxisArgs...);
250  case TAxisConfig::kGrow:
251  return MakeNextAxis<TAxisConfig::kGrow>(title, axes, processedAxisArgs...);
253  return MakeNextAxis<TAxisConfig::kIrregular>(title, axes, processedAxisArgs...);
254  default:
255  R__ERROR_HERE("HIST") << "Unhandled axis kind";
256  }
257  return nullptr;
258  }
259 };
260 
261 /// Generate THist::fImpl from constructor arguments; recursion end.
262 template<int NDIM, class DATA, class... PROCESSEDAXISCONFIG>
263 /// Create the histogram, now that all axis types and initializer objects are
264 /// determined.
265 struct THistImplGen<NDIM, NDIM, DATA, PROCESSEDAXISCONFIG...> {
267  std::unique_ptr<HistImplBase_t>
268  operator()(std::string_view title, const std::array<TAxisConfig, DATA::GetNDim()> &, PROCESSEDAXISCONFIG... axisArgs)
269  {
270  using HistImplt_t = Detail::THistImpl<DATA, PROCESSEDAXISCONFIG...>;
271  return std::make_unique<HistImplt_t>(title, axisArgs...);
272  }
273 };
274 } // namespace Internal
275 
276 
277 template<int DIMENSIONS, class PRECISION,
278  template <int D_, class P_, template <class P__> class S_> class... STAT>
279 THist<DIMENSIONS, PRECISION, STAT...>::THist(std::string_view title, std::array<TAxisConfig, DIMENSIONS> axes):
280  fImpl{std::move(Internal::THistImplGen<THist::GetNDim(), 0,
282 {
283  fFillFunc = fImpl->GetFillFunc();
284 }
285 
286 
287 template<int DIMENSIONS, class PRECISION,
288  template <int D_, class P_, template <class P__> class S_> class... STAT>
289 THist<DIMENSIONS, PRECISION, STAT...>::THist(std::array<TAxisConfig, DIMENSIONS> axes):
290  THist("", axes) {}
291 
292 
293 /// Adopt an external, stand-alone THistImpl. The THist will take ownership.
294 template<int DIMENSIONS, class PRECISION,
295  template <int D_, class P_, template <class P__> class S_> class... STAT>
296 THist<DIMENSIONS, PRECISION, STAT...>
298 {
299  THist<DIMENSIONS, PRECISION, STAT...> ret;
300  ret.fFillFunc = pHistImpl->GetFillFunc();
301  std::swap(ret.fImpl, pHistImpl);
302  return ret;
303 };
304 
305 
306 /// \name THist Typedefs
307 ///\{ Convenience typedefs (ROOT6-compatible type names)
308 
309 // Keep them as typedefs, to make sure old-style documentation tools can understand them.
315 
321 
327 ///\}
328 
329 
330 /// Add two histograms. This is the generic, inefficient version for now; it
331 /// assumes no matching axes.
332 template<int DIMENSIONS,
333  class PRECISION_TO, class PRECISION_FROM,
334  template <int D_, class P_, template <class P__> class S_> class... STAT_TO,
335  template <int D_, class P_, template <class P__> class S_> class... STAT_FROM>
338 {
339  auto toImpl = to.GetImpl();
340  auto fillFuncTo = toImpl->GetFillFunc();
341  using HistFrom_t = THist<DIMENSIONS, PRECISION_FROM, STAT_FROM...>;
342  using FromCoord_t = typename HistFrom_t::CoordArray_t;
343  using FromWeight_t = typename HistFrom_t::Weight_t;
344  auto add = [fillFuncTo, toImpl](const FromCoord_t& x, FromWeight_t c)
345  {
346  (toImpl->*fillFuncTo)(x, c);
347  // TODO: something nice with the uncertainty - depending on whether `to` cares
348  };
349  from.GetImpl()->ApplyXC(add);
350 }
351 
352 
353 /// Interface to graphics taking a unique_ptr<THist>.
354 template<int DIMENSIONS, class PRECISION,
355  template <int D_, class P_, template <class P__> class S_> class... STAT>
356 std::unique_ptr <Internal::TDrawable>
359 {
360  return std::make_unique<Internal::THistDrawable<DIMENSIONS>>(hist, opts);
361 }
362 
363 /// Interface to graphics taking a shared_ptr<THist>.
364 template<int DIMENSIONS, class PRECISION,
365  template <int D_, class P_, template <class P__> class S_> class... STAT>
366 std::unique_ptr <Internal::TDrawable>
369 {
370  return std::make_unique<Internal::THistDrawable<DIMENSIONS>>(std::move(hist), opts);
371 }
372 
373 } // namespace Experimental
374 } // namespace ROOT
375 
376 #endif
std::vector< PRECISION > THistDataDefaultStorage
std::vector has more template arguments; for the default storage we don&#39;t care about them...
Definition: THistData.hxx:374
std::array< double, DIMENSIONS > CoordArray_t
Definition: THistUtils.hxx:25
static constexpr int GetNDim() noexcept
Number of dimensions of the coordinates.
Definition: THist.hxx:73
std::unique_ptr< ImplBase_t > fImpl
The actual histogram implementation.
Definition: THist.hxx:185
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
Interface class for THistImpl.
Definition: THistImpl.hxx:141
typename ImplBase_t::FillFunc_t FillFunc_t
Pointer type to HistImpl_t::Fill, for faster access.
Definition: THist.hxx:66
Generate THist::fImpl from THist constructor arguments.
Definition: THist.hxx:217
Iterates over the bins of a THist or THistImpl.
std::unique_ptr< ImplBase_t > && TakeImpl() noexcept
"Steal" the ImplBase_t this THist points to.
Definition: THist.hxx:149
TArc * a
Definition: textangle.C:12
const_iterator begin() const
Definition: THist.hxx:172
void swap(THist< DIMENSIONS, PRECISION, STAT... > &other) noexcept
Swap *this and other.
Definition: THist.hxx:179
const_iterator end() const
Definition: THist.hxx:174
STL namespace.
THist(const TAxisConfig &xaxis)
Constructor overload that&#39;s only available for a 1-dimensional histogram.
Definition: THist.hxx:101
Detail::THistBinIter< ImplBase_t > const_iterator
Definition: THist.hxx:70
Weight_t GetBinContent(const CoordArray_t &x) const
Get the content of the bin at x.
Definition: THist.hxx:167
int64_t GetEntries() const noexcept
Get the number of entries this histogram was filled with.
Definition: THist.hxx:164
Double_t x[n]
Definition: legend1.C:17
#define PRECISION
Definition: MnPrint.cxx:26
THist(std::string_view histTitle, const TAxisConfig &xaxis, const TAxisConfig &yaxis, const TAxisConfig &zaxis)
Constructor overload that&#39;s only available for a 3-dimensional histogram, also passing the histogram ...
Definition: THist.hxx:139
represents a TAxisEquidistant
Definition: TAxis.hxx:314
THist(std::string_view histTitle, const TAxisConfig &xaxis, const TAxisConfig &yaxis)
Constructor overload that&#39;s only available for a 2-dimensional histogram, also passing the histogram ...
Definition: THist.hxx:124
represents a TAxisGrow
Definition: TAxis.hxx:315
std::unique_ptr< HistImplBase_t > operator()(std::string_view title, const std::array< TAxisConfig, DATA::GetNDim()> &, PROCESSEDAXISCONFIG... axisArgs)
Definition: THist.hxx:268
double GetBinUncertainty(const CoordArray_t &x) const
Get the uncertainty on the content of the bin at x.
Definition: THist.hxx:170
THist(const TAxisConfig &xaxis, const TAxisConfig &yaxis)
Constructor overload that&#39;s only available for a 2-dimensional histogram.
Definition: THist.hxx:116
std::unique_ptr< Detail::THistImplBase< DATA > > MakeNextAxis(std::string_view title, const std::array< TAxisConfig, NDIM > &axes, PROCESSEDAXISCONFIG... processedAxisArgs)
Select the template argument for the next axis type, and "recurse" into THistImplGen for the next axi...
Definition: THist.hxx:222
std::unique_ptr< Internal::TDrawable > GetDrawable(std::unique_ptr< THist< DIMENSIONS, PRECISION, STAT... >> &&hist, THistDrawOptions< DIMENSIONS > opts={})
Interface to graphics taking a shared_ptr<THist>.
Definition: THist.hxx:367
Objects used to configure the different axis types.
Definition: TAxis.hxx:311
THist(const TAxisConfig &xaxis, const TAxisConfig &yaxis, const TAxisConfig &zaxis)
Constructor overload that&#39;s only available for a 3-dimensional histogram.
Definition: THist.hxx:131
void FillN(const std::array_view< CoordArray_t > xN) noexcept
Convenience overload: FillN() with weight 1.
Definition: THist.hxx:161
A THistImplBase&#39;s data, provides accessors to all its statistics.
Definition: THistData.hxx:425
THist(std::string_view histTitle, const TAxisConfig &xaxis)
Constructor overload that&#39;s only available for a 1-dimensional histogram, also passing the histogram ...
Definition: THist.hxx:109
THist< DIMENSIONS, PRECISION, STAT... > HistFromImpl(std::unique_ptr< typename THist< DIMENSIONS, PRECISION, STAT... >::ImplBase_t > pHistImpl)
Adopt an external, stand-alone THistImpl. The THist will take ownership.
Definition: THist.hxx:297
represents a TAxisIrregular
Definition: TAxis.hxx:316
void FillN(const std::array_view< CoordArray_t > xN, const std::array_view< Weight_t > weightN) noexcept
For each coordinate in xN, add weightN[i] to the bin at coordinate xN[i].
Definition: THist.hxx:157
Hist::CoordArray_t< DATA::GetNDim()> CoordArray_t
Type of the coordinate: a DIMENSIONS-dimensional array of doubles.
Definition: THistImpl.hxx:146
Histogram class for histograms with DIMENSIONS dimensions, where each bin count is stored by a value ...
Definition: THist.hxx:33
void swap(THist< DIMENSIONS, PRECISION, STAT... > &a, THist< DIMENSIONS, PRECISION, STAT... > &b) noexcept
Swap two histograms.
Definition: THist.hxx:205
void Add(THist< DIMENSIONS, PRECISION_TO, STAT_TO... > &to, const THist< DIMENSIONS, PRECISION_FROM, STAT_FROM... > &from)
Add two histograms.
Definition: THist.hxx:336
void(THistImplBase::*)(const CoordArray_t &x, Weight_t w) FillFunc_t
Type of the Fill(x, w) function.
Definition: THistImpl.hxx:151
int type
Definition: TGX11.cxx:120
ImplBase_t * GetImpl() const noexcept
Access the ImplBase_t this THist points to.
Definition: THist.hxx:146
Hist::AxisIterRange_t< DIMENSIONS > AxisIterRange_t
Range type.
Definition: THistImpl.hxx:73
FillFunc_t fFillFunc
! Pinter to THistImpl::Fill() member function
Definition: THist.hxx:186
Converts a TAxisConfig of whatever kind to the corresponding TAxisBase-derived object.
Definition: TAxis.hxx:821
typename ImplBase_t::CoordArray_t CoordArray_t
The coordinates type: a DIMENSIONS-dimensional std::array of double.
Definition: THist.hxx:62
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t Double_t b
Definition: TRolke.cxx:630
class THist< DIMENSIONS, PRECISION, STAT... > HistFromImpl(std::unique_ptr< typename THist< DIMENSIONS, PRECISION, STAT... >::ImplBase_t > pHistImpl)
Adopt an external, stand-alone THistImpl. The THist will take ownership.
Definition: THist.hxx:297
void Fill(const CoordArray_t &x, Weight_t weight=(Weight_t) 1) noexcept
Add weight to the bin containing coordinate x.
Definition: THist.hxx:152
typename ImplBase_t::AxisIterRange_t AxisRange_t
Range.
Definition: THist.hxx:68
#define R__ERROR_HERE(GROUP)
Definition: TLogger.hxx:122
std::unique_ptr< Detail::THistImplBase< DATA > > operator()(std::string_view title, const std::array< TAxisConfig, NDIM > &axes, PROCESSEDAXISCONFIG... processedAxisArgs)
Make a THistImpl-derived object reflecting the TAxisConfig array.
Definition: THist.hxx:244