CbmRoot
Loading...
Searching...
No Matches
CbmHistManager.h
Go to the documentation of this file.
1/* Copyright (C) 2011-2021 GSI/JINR-LIT, Darmstadt/Dubna
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Semen Lebedev, Andrey Lebedev [committer], Florian Uhlig */
4
12#ifndef CBMHISTMANAGER_H_
13#define CBMHISTMANAGER_H_
14
15#include <Logger.h> // for Logger, LOG
16
17#include <Rtypes.h> // for THashConsistencyHolder, ClassDef
18#include <RtypesCore.h> // for Double_t, Int_t, Bool_t, Option_t
19#include <TCanvas.h> // iwyu: keep for RootCling
20#include <TGraph.h> // for TGraph
21#include <TGraph2D.h> // for TGraph2D
22#include <TH1.h> // for TH1
23#include <TH2.h> // for TH2
24#include <TH3.h> // for TH3
25#include <THnSparse.h> // for THnSparse
26#include <TNamed.h> // for TNamed
27#include <TObject.h> // for TObject
28#include <TProfile.h> // for TProfile
29#include <TProfile2D.h> // for TProfile2D
30
31#include <array> // for array
32#include <cassert> // for assert
33#include <map> // for map, __map_const_iterator, operator!=
34#include <ostream> // for string, operator<<, ostream
35#include <string> // for operator<
36#include <type_traits> // for is_base_of
37#include <utility> // for pair, make_pair
38#include <vector> // for vector
39
40class TFile;
41
48class CbmHistManager : public TObject {
49public:
54
58 virtual ~CbmHistManager();
59
65 void Add(const std::string& name, TNamed* object)
66 {
67
68 std::map<std::string, TNamed*>::iterator it = fMap.find(name);
69 if (it != fMap.end()) {
70 LOG(warn) << "CbmHistManager::Add Object with name:" << name << " was already added. Set new object.";
71 }
72
73 std::pair<std::string, TNamed*> newpair = std::make_pair(name, object);
74 fMap.insert(newpair);
75 }
76
87 template<class T>
88 void Create1(const std::string& name, const std::string& title, Int_t nofBins, Double_t minBin, Double_t maxBin)
89 {
90 T* h = new T(name.c_str(), title.c_str(), nofBins, minBin, maxBin);
91 Add(name, h);
92 }
93
102 template<class T>
103 void Create1(const std::string& name, const std::string& title, const std::vector<Double_t>& binsX)
104 {
105 T* h = new T(name.c_str(), title.c_str(), binsX.size() - 1, binsX.data());
106 Add(name, h);
107 }
108
122 template<class T>
123 void Create2(const std::string& name, const std::string& title, Int_t nofBinsX, Double_t minBinX, Double_t maxBinX,
124 Int_t nofBinsY, Double_t minBinY, Double_t maxBinY)
125 {
126 T* h = new T(name.c_str(), title.c_str(), nofBinsX, minBinX, maxBinX, nofBinsY, minBinY, maxBinY);
127 Add(name, h);
128 }
129
139 template<class T>
140 void Create2(const std::string& name, const std::string& title, const std::vector<Double_t>& binsX,
141 const std::vector<Double_t>& binsY)
142 {
143 T* h = new T(name.c_str(), title.c_str(), binsX.size() - 1, binsX.data(), binsY.size() - 1, binsY.data());
144 Add(name, h);
145 }
146
163 template<class T>
164 void Create3(const std::string& name, const std::string& title, Int_t nofBinsX, Double_t minBinX, Double_t maxBinX,
165 Int_t nofBinsY, Double_t minBinY, Double_t maxBinY, Int_t nofBinsZ, Double_t minBinZ, Double_t maxBinZ)
166 {
167 T* h = new T(name.c_str(), title.c_str(), nofBinsX, minBinX, maxBinX, nofBinsY, minBinY, maxBinY, nofBinsZ, minBinZ,
168 maxBinZ);
169 Add(name, h);
170 }
171
183 template<class T, int nDim>
184 void CreateSparse(const std::string& name, const std::string& title, const std::array<Int_t, nDim>& nBins,
185 const std::array<Double_t, nDim>& minVals, const std::array<Double_t, nDim>& maxVals)
186 {
187 static_assert(std::is_base_of<THnSparse, T>::value, "Class must be derrived from THnSparse");
188 T* h = new T(name.c_str(), title.c_str(), nDim, nBins.data(), minVals.data(), maxVals.data());
189 Add(name, h);
190 }
191
192 TNamed* GetObject(const std::string& name) const
193 {
194 if (fMap.count(name) == 0) { // Temporarily used for debugging
195 LOG(error) << "CbmHistManager::GetObject(name): name=" << name;
196 }
197 assert(fMap.count(name) != 0);
198 return fMap.find(name)->second;
199 }
200
201
207 TH1* H1(const std::string& name) const
208 {
209 if (fMap.count(name) == 0) { // Temporarily used for debugging
210 LOG(error) << "CbmHistManager::H1(name): name=" << name;
211 }
212 assert(fMap.count(name) != 0);
213 return dynamic_cast<TH1*>(fMap.find(name)->second);
214 }
215
221 TH1* H1Clone(const std::string& name) const { return static_cast<TH1*>(H1(name)->Clone()); }
222
228 std::vector<TH1*> H1Vector(const std::vector<std::string>& names) const;
229
230
236 std::vector<TH1*> H1Vector(const std::string& pattern) const;
237
243 TH2* H2(const std::string& name) const
244 {
245 if (fMap.count(name) == 0) { // Temporarily used for debugging
246 LOG(error) << "CbmHistManager::H2(name): name=" << name;
247 }
248 assert(fMap.count(name) != 0);
249 return dynamic_cast<TH2*>(fMap.find(name)->second);
250 }
251
257 TH2* H2Clone(const std::string& name) const { return static_cast<TH2*>(H2(name)->Clone()); }
258
264 std::vector<TH2*> H2Vector(const std::vector<std::string>& names) const;
265
271 std::vector<TH2*> H2Vector(const std::string& pattern) const;
272
278 TH3* H3(const std::string& name) const
279 {
280 if (fMap.count(name) == 0) { // Temporarily used for debugging
281 LOG(error) << "CbmHistManager::H3(name): name=" << name;
282 }
283 assert(fMap.count(name) != 0);
284 return dynamic_cast<TH3*>(fMap.find(name)->second);
285 }
286
292 TH3* H3Clone(const std::string& name) const { return static_cast<TH3*>(H3(name)->Clone()); }
293
299 THnSparse* HnSparse(const std::string& name) const
300 {
301 if (fMap.count(name) == 0) { // Temporarily used for debugging
302 LOG(error) << "CbmHistManager::HnSparse(name): name=" << name;
303 }
304 assert(fMap.count(name) != 0);
305 return dynamic_cast<THnSparse*>(fMap.find(name)->second);
306 }
307
313 TGraph* G1(const std::string& name) const
314 {
315 if (fMap.count(name) == 0) { // Temporarily used for debugging
316 LOG(error) << "CbmHistManager::G1(name): name=" << name;
317 }
318 assert(fMap.count(name) != 0);
319 return dynamic_cast<TGraph*>(fMap.find(name)->second);
320 }
321
327 std::vector<TGraph*> G1Vector(const std::vector<std::string>& names) const;
328
334 std::vector<TGraph*> G1Vector(const std::string& pattern) const;
335
341 TGraph2D* G2(const std::string& name) const
342 {
343 if (fMap.count(name) == 0) { // Temporarily used for debugging
344 LOG(error) << "CbmHistManager::G2(name): name=" << name;
345 }
346 assert(fMap.count(name) != 0);
347 return dynamic_cast<TGraph2D*>(fMap.find(name)->second);
348 }
349
355 std::vector<TGraph2D*> G2Vector(const std::vector<std::string>& names) const;
356
362 std::vector<TGraph2D*> G2Vector(const std::string& pattern) const;
363
369 TProfile* P1(const std::string& name) const
370 {
371 if (fMap.count(name) == 0) { // Temporarily used for debugging
372 LOG(error) << "CbmHistManager::P1(name): name=" << name;
373 }
374 assert(fMap.count(name) != 0);
375 return dynamic_cast<TProfile*>(fMap.find(name)->second);
376 }
377
383 std::vector<TProfile*> P1Vector(const std::vector<std::string>& names) const;
384
390 std::vector<TProfile*> P1Vector(const std::string& pattern) const;
391
397 TProfile2D* P2(const std::string& name) const
398 {
399 if (fMap.count(name) == 0) { // Temporarily used for debugging
400 LOG(error) << "CbmHistManager::P2(name): name=" << name;
401 }
402 assert(fMap.count(name) != 0);
403 return dynamic_cast<TProfile2D*>(fMap.find(name)->second);
404 }
405
411 std::vector<TProfile2D*> P2Vector(const std::vector<std::string>& names) const;
412
418 std::vector<TProfile2D*> P2Vector(const std::string& pattern) const;
419
425 Bool_t Exists(const std::string& name) const { return (fMap.count(name) == 0) ? false : true; }
426
430 void WriteToFile();
431
435 void WriteCanvasToFile();
436
441 void ReadFromFile(TFile* file);
442
446 void AddTNamedObject(TObject* obj);
447
451 void AddTDirectoryObject(TObject* obj);
452
456 void Clear(Option_t* = "");
457
462 void ShrinkEmptyBinsH1(const std::string& histName);
463
468 void ShrinkEmptyBinsH1ByPattern(const std::string& pattern);
469
474 void ShrinkEmptyBinsH2(const std::string& histName);
475
480 void ShrinkEmptyBinsH2ByPattern(const std::string& pattern);
481
487 void Scale(const std::string& histName, Double_t scale);
488
494 void ScaleByPattern(const std::string& pattern, Double_t scale);
495
500 void NormalizeToIntegral(const std::string& histName);
501
506 void NormalizeToIntegralByPattern(const std::string& pattern);
507
513 void Rebin(const std::string& histName, Int_t ngroup);
514
520 void RebinByPattern(const std::string& pattern, Int_t ngroup);
521
526 std::string ToString() const;
527
532 friend std::ostream& operator<<(std::ostream& strm, const CbmHistManager& histManager)
533 {
534 strm << histManager.ToString();
535 return strm;
536 }
537
546 TCanvas* CreateCanvas(const std::string& name, const std::string& title, Int_t width, Int_t height);
547
553 void SaveCanvasToImage(const std::string& outputDir, const std::string& options = "png,eps");
554
555private:
556 template<class T>
557 std::vector<T> ObjectVector(const std::string& pattern) const;
558
559 template<class T>
560 std::vector<T> ObjectVector(const std::vector<std::string>& names) const;
561
562 std::map<std::string, TNamed*> fMap; // Map of histogram (graph) name to its pointer
563 std::vector<TCanvas*> fCanvases; // Pointers to all created canvases
564
565 ClassDef(CbmHistManager, 1)
566};
567
568#endif /* CBMHISTMANAGER_H_ */
Histogram manager.
TGraph2D * G2(const std::string &name) const
Return pointer to TGraph2D.
void ShrinkEmptyBinsH1ByPattern(const std::string &pattern)
Shrink empty bins in H1.
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.
TProfile * P1(const std::string &name) const
Return pointer to TProfile.
std::vector< TGraph2D * > G2Vector(const std::vector< std::string > &names) const
Return vector of pointers to TGraph2D.
std::vector< TH2 * > H2Vector(const std::string &pattern) const
Return vector of pointers to TH2 histogram.
void AddTNamedObject(TObject *obj)
Add TName object to map. Used in ReadFromFile method.
void ShrinkEmptyBinsH2ByPattern(const std::string &pattern)
Shrink empty bins in H2.
std::vector< TProfile2D * > P2Vector(const std::string &pattern) const
Return vector of pointers to TProfile2D.
std::vector< TCanvas * > fCanvases
void SaveCanvasToImage(const std::string &outputDir, const std::string &options="png,eps")
Save all stored canvases to images.
std::vector< TH1 * > H1Vector(const std::vector< std::string > &names) const
Return vector of pointers to TH1 histogram.
void Clear(Option_t *="")
Clear memory. Remove all histograms and canvases.
std::vector< TProfile * > P1Vector(const std::string &pattern) const
Return vector of pointers to TProfile.
std::vector< TH1 * > H1Vector(const std::string &pattern) const
Return vector of pointers to TH1 histogram.
std::vector< TProfile2D * > P2Vector(const std::vector< std::string > &names) const
Return vector of pointers to TProfile2D.
TProfile2D * P2(const std::string &name) const
Return pointer to TProfile2D.
std::map< std::string, TNamed * > fMap
TH2 * H2(const std::string &name) const
Return pointer to TH2 histogram.
void Create1(const std::string &name, const std::string &title, const std::vector< Double_t > &binsX)
Helper function for creation of 1-dimensional histograms and profiles. Template argument is a real ob...
TNamed * GetObject(const std::string &name) const
std::vector< TProfile * > P1Vector(const std::vector< std::string > &names) const
Return vector of pointers to TProfile.
void Rebin(const std::string &histName, Int_t ngroup)
Rebin histogram.
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...
TH3 * H3Clone(const std::string &name) const
Return clone of TH3 histogram.
void ReadFromFile(TFile *file)
Read histograms from file.
void Scale(const std::string &histName, Double_t scale)
Scale histogram.
std::vector< TH2 * > H2Vector(const std::vector< std::string > &names) const
Return vector of pointers to TH2 histogram.
void ScaleByPattern(const std::string &pattern, Double_t scale)
Scale histograms which name matches specified pattern.
void AddTDirectoryObject(TObject *obj)
Add all TName objects to map in directory. Used in ReadFromFile method.
TGraph * G1(const std::string &name) const
Return pointer to TGraph.
void WriteToFile()
Write all objects to current opened file.
void Create2(const std::string &name, const std::string &title, const std::vector< Double_t > &binsX, const std::vector< Double_t > &binsY)
Helper function for creation of 2-dimensional histograms and profiles. Template argument is a real ob...
TH3 * H3(const std::string &name) const
Return pointer to TH3 histogram.
void ShrinkEmptyBinsH2(const std::string &histName)
Shrink empty bins in H2.
void CreateSparse(const std::string &name, const std::string &title, const std::array< Int_t, nDim > &nBins, const std::array< Double_t, nDim > &minVals, const std::array< Double_t, nDim > &maxVals)
Helper function for creation of THnSparse objects.
std::vector< TGraph * > G1Vector(const std::vector< std::string > &names) const
Return vector of pointers to TGraph.
void RebinByPattern(const std::string &pattern, Int_t ngroup)
Rebin histograms which name matches specified pattern.
void NormalizeToIntegral(const std::string &histName)
Normalize histogram to integral.
Bool_t Exists(const std::string &name) const
Check existence of object in manager.
TH2 * H2Clone(const std::string &name) const
Return clone of TH2 histogram.
void NormalizeToIntegralByPattern(const std::string &pattern)
Normalize histograms to integral which name matches specified pattern.
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...
void Add(const std::string &name, TNamed *object)
Add new named object to manager.
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...
void ShrinkEmptyBinsH1(const std::string &histName)
Shrink empty bins in H1.
virtual ~CbmHistManager()
Destructor.
std::vector< TGraph * > G1Vector(const std::string &pattern) const
Return vector of pointers to TGraph.
friend std::ostream & operator<<(std::ostream &strm, const CbmHistManager &histManager)
Operator << for convenient output to std::ostream.
std::string ToString() const
Return string representation of class.
TH1 * H1Clone(const std::string &name) const
Return clone of TH1 histogram.
void WriteCanvasToFile()
Write all canvas to current opened file.
std::vector< T > ObjectVector(const std::string &pattern) const
TH1 * H1(const std::string &name) const
Return pointer to TH1 histogram.
THnSparse * HnSparse(const std::string &name) const
Return pointer to THnSparse histogram.
std::vector< TGraph2D * > G2Vector(const std::string &pattern) const
Return vector of pointers to TGraph2D.
std::vector< T > ObjectVector(const std::vector< std::string > &names) const
Data class with information on a STS local track.