Logo ROOT   6.21/01
Reference Guide
THnChain.h
Go to the documentation of this file.
1 // @(#)root/hist:$Id$
2 // Author: Benjamin Bannier, August 2016
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2016, 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_THnChain
13 #define ROOT_THnChain
14 
15 #include <TObject.h>
16 
17 #include <string>
18 #include <vector>
19 
20 class TAxis;
21 class TH1;
22 class TH2;
23 class TH3;
24 class THnBase;
25 
26 /** \class THnChain
27 A class to chain together multiple histograms.
28 
29 This class allows to chain together any `THnBase`-derived (`THn` or `THnSparse`)
30 histograms from multiple files. Operations on the axes and projections are
31 supported. The intent is to allow convenient merging merging of projections
32 of high-dimensional histograms.
33 
34 \code{.cpp}
35 // `file1.root` and `file2.root` contain a `THnSparse` named `hsparse`.
36 THnChain hs("hsparse");
37 hs.AddFile("file1.root");
38 hs.AddFile("file2.root");
39 
40 // Project out axis 0, integrate over other axes.
41 TH1* h0 = hs.Projection(0);
42 
43 // Project out axis 0, integrate over other axes in their active ranges.
44 hs.GetAxis(1)->SetRangeUser(0, 0.1); // select a subrange
45 TH1* h0 = hs.Projection(0);
46 \endcode
47 */
48 
49 class THnChain : public TObject
50 {
51  public:
52  /// Default constructor.
53  ///
54  /// \param name name of the histogram to work on
55  explicit THnChain(const char* name) : fName(name) {}
56 
57  /// Add a new file to this chain.
58  ///
59  /// \param fileName path of the file to add
60  void AddFile(const char* fileName);
61 
62  /// Get an axis from the histogram.
63  ///
64  /// \param i index of the axis to retrieve
65  ///
66  /// This function requires that a file containing the histogram was
67  /// already added with `AddFile`.
68  ///
69  /// Properties set on the axis returned by `GetAxis` are propagated to all
70  /// histograms in the chain, so it can e.g. be used to set ranges for
71  /// projections.
72  TAxis* GetAxis(Int_t i) const;
73 
74  /// See `THnBase::Projection` for the intended behavior.
75  TH1* Projection(Int_t xDim, Option_t* option = "") const;
76 
77  /// See `THnBase::Projection` for the intended behavior.
78  TH2* Projection(Int_t yDim, Int_t xDim, Option_t* option = "") const;
79 
80  /// See `THnBase::Projection` for the intended behavior.
81  TH3* Projection(Int_t xDim, Int_t yDim, Int_t zDim, Option_t* option = "") const;
82 
83  /// See `THnBase::Projection` for the intended behavior.
84  THnBase* ProjectionND(Int_t ndim, const Int_t* dim, Option_t* option = "") const;
85 
86  private:
87  std::string fName; ///< name of the histogram
88 
89  std::vector<std::string> fFiles; ///< a list of files to extract the histogram from
90  std::vector<TAxis*> fAxes; ///< the list of histogram axes
91 
92  /// Projects all histograms in the chain.
93  ///
94  /// See `THnBase::Projection` for parameters and their semantics.
95  TObject* ProjectionAny(Int_t ndim, const Int_t* dim, Option_t* option = "") const;
96 
97  /// Retrieve a histogram from a file.
98  ///
99  /// \param fileName path of the file to read.
100  THnBase* ReadHistogram(const char* fileName) const;
101 
102  /// Copy the properties of all axes to a histogram.
103  ///
104  /// \param hs histogram whose axes should be updated
105  void SetupAxes(THnBase& hs) const;
106 
107  /// Ensure a histogram has axes similar to the ones we expect.
108  ///
109  /// \param h histogram to verify
110  /// \param axes expected set of axes
111  static bool CheckConsistency(const THnBase& h, const std::vector<TAxis*>& axes);
112 
113  ClassDef(THnChain, 0);
114 };
115 
116 #endif
const char Option_t
Definition: RtypesCore.h:62
static bool CheckConsistency(const THnBase &h, const std::vector< TAxis *> &axes)
Ensure a histogram has axes similar to the ones we expect.
Definition: THnChain.cxx:145
TObject * ProjectionAny(Int_t ndim, const Int_t *dim, Option_t *option="") const
Projects all histograms in the chain.
Definition: THnChain.cxx:55
std::string fName
name of the histogram
Definition: THnChain.h:87
int Int_t
Definition: RtypesCore.h:41
#define ClassDef(name, id)
Definition: Rtypes.h:326
THnChain(const char *name)
Default constructor.
Definition: THnChain.h:55
The 3-D histogram classes derived from the 1-D histogram classes.
Definition: TH3.h:31
TH1 * Projection(Int_t xDim, Option_t *option="") const
See THnBase::Projection for the intended behavior.
Definition: THnChain.cxx:195
A class to chain together multiple histograms.
Definition: THnChain.h:49
TAxis * GetAxis(Int_t i) const
Get an axis from the histogram.
Definition: THnChain.cxx:46
void SetupAxes(THnBase &hs) const
Copy the properties of all axes to a histogram.
Definition: THnChain.cxx:135
Service class for 2-Dim histogram classes.
Definition: TH2.h:30
Class to manage histogram axis.
Definition: TAxis.h:30
std::vector< std::string > fFiles
a list of files to extract the histogram from
Definition: THnChain.h:89
#define h(i)
Definition: RSha256.hxx:106
The TH1 histogram class.
Definition: TH1.h:56
Mother of all ROOT objects.
Definition: TObject.h:37
std::vector< TAxis * > fAxes
the list of histogram axes
Definition: THnChain.h:90
THnBase * ReadHistogram(const char *fileName) const
Retrieve a histogram from a file.
Definition: THnChain.cxx:118
THnBase * ProjectionND(Int_t ndim, const Int_t *dim, Option_t *option="") const
See THnBase::Projection for the intended behavior.
Definition: THnChain.cxx:216
void AddFile(const char *fileName)
Add a new file to this chain.
Definition: THnChain.cxx:24
Multidimensional histogram base.
Definition: THnBase.h:43
char name[80]
Definition: TGX11.cxx:109