CbmRoot
CbmHistManager.h
Go to the documentation of this file.
1 
8 #ifndef CBMHISTMANAGER_H_
9 #define CBMHISTMANAGER_H_
10 
11 #include <Rtypes.h> // for THashConsistencyHolder, ClassDef
12 #include <RtypesCore.h> // for Double_t, Int_t, Bool_t, Option_t
13 #include <TCanvas.h> // iwyu: keep for RootCling
14 #include <TGraph.h> // for TGraph
15 #include <TGraph2D.h> // for TGraph2D
16 #include <TH1.h> // for TH1
17 #include <TH2.h> // for TH2
18 #include <TH3.h> // for TH3
19 #include <TNamed.h> // for TNamed
20 #include <TObject.h> // for TObject
21 #include <TProfile.h> // for TProfile
22 #include <TProfile2D.h> // for TProfile2D
23 
24 #include <FairLogger.h> // for Logger, LOG
25 
26 #include <cassert> // for assert
27 #include <map> // for map, __map_const_iterator, operator!=
28 #include <ostream> // for string, operator<<, ostream
29 #include <string> // for operator<
30 #include <utility> // for pair, make_pair
31 #include <vector> // for vector
32 
33 class TFile;
34 
41 class CbmHistManager : public TObject {
42 public:
47 
51  virtual ~CbmHistManager();
52 
58  void Add(const std::string& name, TNamed* object) {
59 
60  std::map<std::string, TNamed*>::iterator it = fMap.find(name);
61  if (it != fMap.end()) {
62  LOG(warn) << "CbmHistManager::Add Object with name:" << name
63  << " was already added. Set new object.";
64  }
65 
66  std::pair<std::string, TNamed*> newpair = std::make_pair(name, object);
67  fMap.insert(newpair);
68  }
69 
80  template<class T>
81  void Create1(const std::string& name,
82  const std::string& title,
83  Int_t nofBins,
84  Double_t minBin,
85  Double_t maxBin) {
86  T* h = new T(name.c_str(), title.c_str(), nofBins, minBin, maxBin);
87  Add(name, h);
88  }
89 
103  template<class T>
104  void Create2(const std::string& name,
105  const std::string& title,
106  Int_t nofBinsX,
107  Double_t minBinX,
108  Double_t maxBinX,
109  Int_t nofBinsY,
110  Double_t minBinY,
111  Double_t maxBinY) {
112  T* h = new T(name.c_str(),
113  title.c_str(),
114  nofBinsX,
115  minBinX,
116  maxBinX,
117  nofBinsY,
118  minBinY,
119  maxBinY);
120  Add(name, h);
121  }
122 
139  template<class T>
140  void Create3(const std::string& name,
141  const std::string& title,
142  Int_t nofBinsX,
143  Double_t minBinX,
144  Double_t maxBinX,
145  Int_t nofBinsY,
146  Double_t minBinY,
147  Double_t maxBinY,
148  Int_t nofBinsZ,
149  Double_t minBinZ,
150  Double_t maxBinZ) {
151  T* h = new T(name.c_str(),
152  title.c_str(),
153  nofBinsX,
154  minBinX,
155  maxBinX,
156  nofBinsY,
157  minBinY,
158  maxBinY,
159  nofBinsZ,
160  minBinZ,
161  maxBinZ);
162  Add(name, h);
163  }
164 
170  TH1* H1(const std::string& name) const {
171  if (fMap.count(name) == 0) { // Temporarily used for debugging
172  LOG(error) << "CbmHistManager::H1(name): name=" << name;
173  }
174  assert(fMap.count(name) != 0);
175  return dynamic_cast<TH1*>(fMap.find(name)->second);
176  }
177 
183  std::vector<TH1*> H1Vector(const std::string& pattern) const;
184 
190  TH2* H2(const std::string& name) const {
191  if (fMap.count(name) == 0) { // Temporarily used for debugging
192  LOG(error) << "CbmHistManager::H2(name): name=" << name;
193  }
194  assert(fMap.count(name) != 0);
195  return dynamic_cast<TH2*>(fMap.find(name)->second);
196  }
197 
203  std::vector<TH2*> H2Vector(const std::string& pattern) const;
204 
210  TH3* H3(const std::string& name) const {
211  if (fMap.count(name) == 0) { // Temporarily used for debugging
212  LOG(error) << "CbmHistManager::H3(name): name=" << name;
213  }
214  assert(fMap.count(name) != 0);
215  return dynamic_cast<TH3*>(fMap.find(name)->second);
216  }
217 
223  TGraph* G1(const std::string& name) const {
224  if (fMap.count(name) == 0) { // Temporarily used for debugging
225  LOG(error) << "CbmHistManager::G1(name): name=" << name;
226  }
227  assert(fMap.count(name) != 0);
228  return dynamic_cast<TGraph*>(fMap.find(name)->second);
229  }
230 
236  std::vector<TGraph*> G1Vector(const std::string& pattern) const;
237 
243  TGraph2D* G2(const std::string& name) const {
244  if (fMap.count(name) == 0) { // Temporarily used for debugging
245  LOG(error) << "CbmHistManager::G2(name): name=" << name;
246  }
247  assert(fMap.count(name) != 0);
248  return dynamic_cast<TGraph2D*>(fMap.find(name)->second);
249  }
250 
256  std::vector<TGraph2D*> G2Vector(const std::string& pattern) const;
257 
263  TProfile* P1(const std::string& name) const {
264  if (fMap.count(name) == 0) { // Temporarily used for debugging
265  LOG(error) << "CbmHistManager::P1(name): name=" << name;
266  }
267  assert(fMap.count(name) != 0);
268  return dynamic_cast<TProfile*>(fMap.find(name)->second);
269  }
270 
276  std::vector<TProfile*> P1Vector(const std::string& pattern) const;
277 
283  TProfile2D* P2(const std::string& name) const {
284  if (fMap.count(name) == 0) { // Temporarily used for debugging
285  LOG(error) << "CbmHistManager::P2(name): name=" << name;
286  }
287  assert(fMap.count(name) != 0);
288  return dynamic_cast<TProfile2D*>(fMap.find(name)->second);
289  }
290 
296  std::vector<TProfile2D*> P2Vector(const std::string& pattern) const;
297 
303  Bool_t Exists(const std::string& name) const {
304  return (fMap.count(name) == 0) ? false : true;
305  }
306 
310  void WriteToFile();
311 
316  void ReadFromFile(TFile* file);
317 
321  void AddTNamedObject(TObject* obj);
322 
326  void AddTDirectoryObject(TObject* obj);
327 
331  void Clear(Option_t*);
332 
337  void ShrinkEmptyBinsH1(const std::string& histName);
338 
343  void ShrinkEmptyBinsH1ByPattern(const std::string& pattern);
344 
349  void ShrinkEmptyBinsH2(const std::string& histName);
350 
355  void ShrinkEmptyBinsH2ByPattern(const std::string& pattern);
356 
362  void Scale(const std::string& histName, Double_t scale);
363 
369  void ScaleByPattern(const std::string& pattern, Double_t scale);
370 
375  void NormalizeToIntegral(const std::string& histName);
376 
381  void NormalizeToIntegralByPattern(const std::string& pattern);
382 
388  void Rebin(const std::string& histName, Int_t ngroup);
389 
395  void RebinByPattern(const std::string& pattern, Int_t ngroup);
396 
401  std::string ToString() const;
402 
407  friend std::ostream& operator<<(std::ostream& strm,
408  const CbmHistManager& histManager) {
409  strm << histManager.ToString();
410  return strm;
411  }
412 
421  TCanvas* CreateCanvas(const std::string& name,
422  const std::string& title,
423  Int_t width,
424  Int_t height);
425 
431  void SaveCanvasToImage(const std::string& outputDir,
432  const std::string& options = "png,eps");
433 
434 private:
435  template<class T>
436  std::vector<T> ObjectVector(const std::string& pattern) const;
437 
438  std::map<std::string, TNamed*>
439  fMap; // Map of histogram (graph) name to its pointer
440  std::vector<TCanvas*> fCanvases; // Pointers to all created canvases
441 
442  ClassDef(CbmHistManager, 1)
443 };
444 
445 #endif /* CBMHISTMANAGER_H_ */
CbmHistManager::Clear
void Clear(Option_t *)
Clear memory. Remove all histograms.
Definition: core/base/CbmHistManager.cxx:146
CbmHistManager::P1
TProfile * P1(const std::string &name) const
Return pointer to TProfile.
Definition: CbmHistManager.h:263
CbmHistManager::Exists
Bool_t Exists(const std::string &name) const
Check existence of histogram in manager.
Definition: CbmHistManager.h:303
CbmHistManager::ToString
std::string ToString() const
Return string representation of class.
Definition: core/base/CbmHistManager.cxx:258
CbmHistManager::WriteToFile
void WriteToFile()
Write all histograms to current opened file.
Definition: core/base/CbmHistManager.cxx:103
CbmHistManager::AddTNamedObject
void AddTNamedObject(TObject *obj)
Add TName object to map. Used in ReadFromFile method.
Definition: core/base/CbmHistManager.cxx:123
CbmHistManager::ShrinkEmptyBinsH2ByPattern
void ShrinkEmptyBinsH2ByPattern(const std::string &pattern)
Shrink empty bins in H2.
Definition: core/base/CbmHistManager.cxx:209
CbmHistManager::ScaleByPattern
void ScaleByPattern(const std::string &pattern, Double_t scale)
Scale histograms which name matches specified pattern.
Definition: core/base/CbmHistManager.cxx:221
CbmHistManager::Create3
void Create3(const std::string &name, const std::string &title, Int_t nofBinsX, Double_t minBinX, Double_t maxBinX, Int_t nofBinsY, Double_t minBinY, Double_t maxBinY, Int_t nofBinsZ, Double_t minBinZ, Double_t maxBinZ)
Helper function for creation of 3-dimensional histograms and profiles. Template argument is a real ob...
Definition: CbmHistManager.h:140
CbmHistManager::fMap
std::map< std::string, TNamed * > fMap
Definition: CbmHistManager.h:439
CbmHistManager::ReadFromFile
void ReadFromFile(TFile *file)
Read histograms from file.
Definition: core/base/CbmHistManager.cxx:110
CbmHistManager::P2Vector
std::vector< TProfile2D * > P2Vector(const std::string &pattern) const
Return vector of pointers to TProfile2D.
Definition: core/base/CbmHistManager.cxx:99
CbmHistManager::Create2
void Create2(const std::string &name, const std::string &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. Template argument is a real ob...
Definition: CbmHistManager.h:104
CbmHistManager::ShrinkEmptyBinsH1ByPattern
void ShrinkEmptyBinsH1ByPattern(const std::string &pattern)
Shrink empty bins in H1.
Definition: core/base/CbmHistManager.cxx:174
CbmHistManager::NormalizeToIntegral
void NormalizeToIntegral(const std::string &histName)
Normalize histogram to integral.
Definition: core/base/CbmHistManager.cxx:229
CbmHistManager::H2
TH2 * H2(const std::string &name) const
Return pointer to TH2 histogram.
Definition: CbmHistManager.h:190
CbmHistManager::RebinByPattern
void RebinByPattern(const std::string &pattern, Int_t ngroup)
Rebin histograms which name matches specified pattern.
Definition: core/base/CbmHistManager.cxx:250
CbmHistManager::operator<<
friend std::ostream & operator<<(std::ostream &strm, const CbmHistManager &histManager)
Operator << for convenient output to std::ostream.
Definition: CbmHistManager.h:407
CbmHistManager::G1
TGraph * G1(const std::string &name) const
Return pointer to TGraph.
Definition: CbmHistManager.h:223
CbmHistManager::H3
TH3 * H3(const std::string &name) const
Return pointer to TH3 histogram.
Definition: CbmHistManager.h:210
CbmHistManager::NormalizeToIntegralByPattern
void NormalizeToIntegralByPattern(const std::string &pattern)
Normalize histograms to integral which name matches specified pattern.
Definition: core/base/CbmHistManager.cxx:234
CbmHistManager::G2Vector
std::vector< TGraph2D * > G2Vector(const std::string &pattern) const
Return vector of pointers to TGraph2D.
Definition: core/base/CbmHistManager.cxx:91
CbmHistManager
Histogram manager.
Definition: CbmHistManager.h:41
h
Data class with information on a STS local track.
CbmHistManager::ObjectVector
std::vector< T > ObjectVector(const std::string &pattern) const
CbmHistManager::AddTDirectoryObject
void AddTDirectoryObject(TObject *obj)
Add all TName objects to map in directory. Used in ReadFromFile method.
Definition: core/base/CbmHistManager.cxx:133
CbmHistManager::G2
TGraph2D * G2(const std::string &name) const
Return pointer to TGraph2D.
Definition: CbmHistManager.h:243
CbmHistManager::Create1
void Create1(const std::string &name, const std::string &title, Int_t nofBins, Double_t minBin, Double_t maxBin)
Helper function for creation of 1-dimensional histograms and profiles. Template argument is a real ob...
Definition: CbmHistManager.h:81
CbmHistManager::H1
TH1 * H1(const std::string &name) const
Return pointer to TH1 histogram.
Definition: CbmHistManager.h:170
CbmHistManager::P1Vector
std::vector< TProfile * > P1Vector(const std::string &pattern) const
Return vector of pointers to TProfile.
Definition: core/base/CbmHistManager.cxx:95
CbmHistManager::fCanvases
std::vector< TCanvas * > fCanvases
Definition: CbmHistManager.h:440
CbmHistManager::CbmHistManager
CbmHistManager()
Constructor.
Definition: core/base/CbmHistManager.cxx:54
CbmHistManager::CreateCanvas
TCanvas * CreateCanvas(const std::string &name, const std::string &title, Int_t width, Int_t height)
Create and draw TCanvas and store pointer to it.
Definition: core/base/CbmHistManager.cxx:267
CbmHistManager::SaveCanvasToImage
void SaveCanvasToImage(const std::string &outputDir, const std::string &options="png,eps")
Save all stored canvases to images.
Definition: core/base/CbmHistManager.cxx:276
CbmHistManager::ShrinkEmptyBinsH2
void ShrinkEmptyBinsH2(const std::string &histName)
Shrink empty bins in H2.
Definition: core/base/CbmHistManager.cxx:182
CbmHistManager::Rebin
void Rebin(const std::string &histName, Int_t ngroup)
Rebin histogram.
Definition: core/base/CbmHistManager.cxx:242
CbmHistManager::H1Vector
std::vector< TH1 * > H1Vector(const std::string &pattern) const
Return vector of pointers to TH1 histogram.
Definition: core/base/CbmHistManager.cxx:79
CbmHistManager::~CbmHistManager
virtual ~CbmHistManager()
Destructor.
Definition: core/base/CbmHistManager.cxx:56
CbmHistManager::P2
TProfile2D * P2(const std::string &name) const
Return pointer to TH2 histogram.
Definition: CbmHistManager.h:283
CbmHistManager::G1Vector
std::vector< TGraph * > G1Vector(const std::string &pattern) const
Return vector of pointers to TGraph.
Definition: core/base/CbmHistManager.cxx:87
CbmHistManager::H2Vector
std::vector< TH2 * > H2Vector(const std::string &pattern) const
Return vector of pointers to TH2 histogram.
Definition: core/base/CbmHistManager.cxx:83
CbmHistManager::ShrinkEmptyBinsH1
void ShrinkEmptyBinsH1(const std::string &histName)
Shrink empty bins in H1.
Definition: core/base/CbmHistManager.cxx:154
CbmHistManager::Scale
void Scale(const std::string &histName, Double_t scale)
Scale histogram.
Definition: core/base/CbmHistManager.cxx:217
CbmHistManager::Add
void Add(const std::string &name, TNamed *object)
Add new named object to manager.
Definition: CbmHistManager.h:58