BmnRoot 26.10.0-18-g4e3414f9
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
BmnHistManager.h
Go to the documentation of this file.
1/**
2 * \file BmnHistManager.h
3 * \brief Histogram manager.
4 * \author Semen Lebedev <s.lebedev@gsi.de>
5 * \date 2011
6 */
7
8#ifndef BMNHISTMANAGER_H_
9#define BMNHISTMANAGER_H_
10
11#include "TObject.h"
12#include "TString.h"
13
14#include <cassert>
15#include <functional>
16#include <iostream>
17#include <map>
18#include <utility>
19#include <vector>
20
21class TFile;
22class TNamed;
23class TH1;
24class TH2;
25class TGraph;
26class TGraph2D;
27class TProfile;
28class TProfile2D;
29
30using std::make_pair;
31using std::map;
32using std::string;
33using std::vector;
34
35/**
36 * \class BmnHistManager
37 * \brief Histogram manager.
38 * \author Semen Lebedev <s.lebedev@gsi.de>
39 * \date 2011
40 */
41class BmnHistManager : public TObject
42{
43 public:
44 /**
45 * \brief Constructor.
46 */
48
49 /**
50 * \brief Destructor.
51 */
52 virtual ~BmnHistManager();
53
54 /**
55 * \brief Add new named object to manager.
56 * \param[in] name Name of the object.
57 * \param[in] object Pointer to object.
58 */
59 void Add(const TString& name, TNamed* object)
60 {
61 std::pair<TString, TNamed*> newpair = std::make_pair(name, object);
62 fMap.insert(newpair);
63 }
64
65 /**
66 * \brief Helper function for creation of 1-dimensional histograms and profiles.
67 * Template argument is a real object type that has to be created, for example,
68 * Create1<TH1F>("name", "title", 100, 0, 100);
69 * \param[in] name Object name.
70 * \param[in] title Object title.
71 * \param[in] nofBins Number of bins.
72 * \param[in] minBin Low axis limit.
73 * \param[in] maxBin Upper axis limit.
74 */
75 template<class T>
76 void Create1(const TString& name, const TString& title, Int_t nofBins, Double_t minBin, Double_t maxBin)
77 {
78 T* h = new T(name.Data(), title.Data(), nofBins, minBin, maxBin);
79 Add(name, h);
80 }
81
82 /**
83 * \brief Helper function for creation of 2-dimensional histograms and profiles.
84 * Template argument is a real object type that has to be created, for example,
85 * Create2<TH2F>("name", "title", 100, 0, 100, 200, 0, 200);
86 * \param[in] name Object name.
87 * \param[in] title Object title.
88 * \param[in] nofBinsX Number of bins for X axis.
89 * \param[in] minBinX Low X axis limit.
90 * \param[in] maxBinX Upper X axis limit.
91 * \param[in] nofBinsY Number of bins for Y axis.
92 * \param[in] minBinY Low Y axis limit.
93 * \param[in] maxBinY Upper Y axis limit.
94 */
95 template<class T>
96 void Create2(const TString& name,
97 const TString& title,
98 Int_t nofBinsX,
99 Double_t minBinX,
100 Double_t maxBinX,
101 Int_t nofBinsY,
102 Double_t minBinY,
103 Double_t maxBinY)
104 {
105 T* h = new T(name.Data(), title.Data(), nofBinsX, minBinX, maxBinX, nofBinsY, minBinY, maxBinY);
106 Add(name, h);
107 }
108
109 /**
110 * \brief Return pointer to TH1 histogram.
111 * \param[in] name Name of histogram.
112 * \return pointer to TH1 histogram.
113 */
114 TH1* H1(const TString& name) const
115 {
116 if (fMap.count(name) == 0) { // Temporarily used for debugging
117 std::cout << "Error: BmnHistManager::H1(name): name=" << name << std::endl;
118 }
119 assert(fMap.count(name) != 0);
120 return (TH1*)fMap.find(name)->second;
121 }
122
123 /**
124 * \brief Return pointer to TH2 histogram.
125 * \param[in] name Name of histogram.
126 * \return pointer to TH1 histogram.
127 */
128 TH2* H2(const TString& name) const
129 {
130 if (fMap.count(name) == 0) { // Temporarily used for debugging
131 std::cout << "Error: BmnHistManager::H2(name): name=" << name << std::endl;
132 }
133 assert(fMap.count(name) != 0);
134 return (TH2*)fMap.find(name)->second;
135 }
136
137 /**
138 * \brief Check existence of histogram in manager.
139 * \param[in] name Name of histogram.
140 * \return True if histogram exists in manager.
141 */
142 Bool_t Exists(const TString& name) const { return (fMap.count(name) == 0) ? false : true; }
143
144 /**
145 * \brief Write all histograms to current opened file.
146 */
148
149 /**
150 * \brief Reset all histograms.
151 */
153
154 /**
155 * \brief Read histograms from file.
156 * \param[in] file Pointer to file with histograms.
157 */
158 void ReadFromFile(TFile* file);
159
160 /**
161 * \brief Clear memory. Remove all histograms.
162 */
163 void Clear();
164
165 /**
166 * \brief Shrink empty bins in H1.
167 * \param[in] histName Name of histogram.
168 */
169 void ShrinkEmptyBinsH1(const TString& histName);
170
171 /**
172 * \brief Shrink empty bins in H2.
173 * \param[in] histName Name of histogram.
174 */
175 void ShrinkEmptyBinsH2(const TString& histName);
176
177 /**
178 * \brief Scale histogram.
179 * \param[in] histName Name of histogram.
180 * \param[in] scale Scaling factor.
181 */
182 void Scale(const TString& histName, Double_t scale);
183
184 /**
185 * \brief Normalize histogram to integral.
186 * \param[in] histName Name of histogram.
187 */
188 void NormalizeToIntegral(const TString& histName);
189
190 /**
191 * \brief Rebin histogram.
192 * \param[in] histName Name of histogram.
193 * \param[in] ngroup Rebining factor.
194 */
195 void Rebin(const TString& histName, Int_t ngroup);
196
197 /**
198 * \brief Return string representation of class.
199 * \return string representation of class.
200 */
202
203 /**
204 * \brief Operator << for convenient output to std::ostream.
205 * \return Insertion stream in order to be able to call a succession of insertion operations.
206 */
207 friend std::ostream& operator<<(std::ostream& strm, const BmnHistManager& histManager)
208 {
209 strm << histManager.ToString();
210 return strm;
211 }
212
213 private:
214 // Map of histogram (graph) name to its pointer
215 map<TString, TNamed*> fMap;
216
218};
219
220#endif /* BmnHISTMANAGER_H_ */
bool Bool_t
int Int_t
double Double_t
#define ClassDef(name, id)
Histogram manager.
friend std::ostream & operator<<(std::ostream &strm, const BmnHistManager &histManager)
Operator << for convenient output to std::ostream.
void Add(const TString &name, TNamed *object)
Add new named object to manager.
void ResetHists()
Reset all histograms.
void ShrinkEmptyBinsH2(const TString &histName)
Shrink empty bins in H2.
virtual ~BmnHistManager()
Destructor.
BmnHistManager()
Constructor.
void Rebin(const TString &histName, Int_t ngroup)
Rebin histogram.
void Scale(const TString &histName, Double_t scale)
Scale histogram.
Bool_t Exists(const TString &name) const
Check existence of histogram in manager.
TH2 * H2(const TString &name) const
Return pointer to TH2 histogram.
map< TString, TNamed * > fMap
void WriteToFile()
Write all histograms to current opened file.
void Create2(const TString &name, const TString &title, Int_t nofBinsX, Double_t minBinX, Double_t maxBinX, Int_t nofBinsY, Double_t minBinY, Double_t maxBinY)
Helper function for creation of 2-dimensional histograms and profiles.
void Clear()
Clear memory.
TString ToString() const
Return string representation of class.
void NormalizeToIntegral(const TString &histName)
Normalize histogram to integral.
void Create1(const TString &name, const TString &title, Int_t nofBins, Double_t minBin, Double_t maxBin)
Helper function for creation of 1-dimensional histograms and profiles.
TH1 * H1(const TString &name) const
Return pointer to TH1 histogram.
void ReadFromFile(TFile *file)
Read histograms from file.
void ShrinkEmptyBinsH1(const TString &histName)
Shrink empty bins in H1.
const char * Data() const