CbmRoot
Loading...
Searching...
No Matches
core/base/CbmHistManager.cxx
Go to the documentation of this file.
1/* Copyright (C) 2011-2020 GSI/JINR-LIT, Darmstadt/Dubna
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Semen Lebedev, Andrey Lebedev [committer], Florian Uhlig */
4
11#include "CbmHistManager.h"
12
13#include "CbmUtils.h" // for SaveCanvasA...
14
15#include <TAxis.h> // for TAxis
16#include <TCanvas.h> // for TCanvas
17#include <TClass.h> // for TClass
18#include <TCollection.h> // for TIter
19#include <TDirectory.h> // for TDirectory
20#include <TDirectoryFile.h> // for TDirectoryFile
21#include <TGenericClassInfo.h> // for TGenericCla...
22#include <TGraph.h> // for TGraph
23#include <TGraph2D.h> // for TGraph2D
24#include <TH1.h> // for TH1
25#include <TH2.h> // for TH2
26#include <TKey.h> // for TKey
27#include <TList.h> // for TList
28#include <TNamed.h> // for TNamed
29#include <TObject.h> // for TObject
30#include <TProfile.h> // for TProfile
31#include <TProfile2D.h> // for TProfile2D
32
33#include <boost/regex.hpp> // for basic_regex, regex, regex_match
34
35#include <algorithm> // for max, min, fill
36#include <cassert> // for assert
37#include <exception> // for exception
38#include <iostream> // for string
39#include <limits> // for numeric_limits
40#include <map> // for map, operat...
41#include <string> // for basic_string
42#include <vector> // for vector
43
44using std::exception;
45using std::map;
46using std::sort;
47using std::string;
48using std::vector;
49
50CbmHistManager::CbmHistManager() : fMap(), fCanvases() {}
51
53
54template<class T>
55vector<T> CbmHistManager::ObjectVector(const string& pattern) const
56{
57 vector<T> objects;
58
59 try {
60 const boost::regex e(pattern);
61 map<string, TNamed*>::const_iterator it;
62 for (it = fMap.begin(); it != fMap.end(); it++) {
63 if (boost::regex_match(it->first, e)) {
64 T ObjectPointer = dynamic_cast<T>(it->second);
65 if (ObjectPointer != nullptr) objects.push_back(ObjectPointer);
66 }
67 }
68 }
69 catch (exception& ex) {
70 LOG(info) << "Exception in CbmHistManager::ObjectVector: " << ex.what();
71 }
72
73 sort(objects.begin(), objects.end(), [](const TNamed* object1, const TNamed* object2) {
74 return string(object1->GetName()) > string(object2->GetName());
75 });
76 return objects;
77}
78
79vector<TH1*> CbmHistManager::H1Vector(const string& pattern) const { return ObjectVector<TH1*>(pattern); }
80
81vector<TH2*> CbmHistManager::H2Vector(const string& pattern) const { return ObjectVector<TH2*>(pattern); }
82
83vector<TGraph*> CbmHistManager::G1Vector(const string& pattern) const { return ObjectVector<TGraph*>(pattern); }
84
85vector<TGraph2D*> CbmHistManager::G2Vector(const string& pattern) const { return ObjectVector<TGraph2D*>(pattern); }
86
87vector<TProfile*> CbmHistManager::P1Vector(const string& pattern) const { return ObjectVector<TProfile*>(pattern); }
88
89vector<TProfile2D*> CbmHistManager::P2Vector(const string& pattern) const { return ObjectVector<TProfile2D*>(pattern); }
90
91
92template<class T>
93vector<T> CbmHistManager::ObjectVector(const vector<string>& names) const
94{
95 vector<T> objects;
96 for (const string& name : names) {
97 objects.push_back(dynamic_cast<T>(fMap.find(name)->second));
98 }
99 return objects;
100}
101vector<TH1*> CbmHistManager::H1Vector(const vector<string>& names) const { return ObjectVector<TH1*>(names); }
102
103vector<TH2*> CbmHistManager::H2Vector(const vector<string>& names) const { return ObjectVector<TH2*>(names); }
104
105vector<TGraph*> CbmHistManager::G1Vector(const vector<string>& names) const { return ObjectVector<TGraph*>(names); }
106
107vector<TGraph2D*> CbmHistManager::G2Vector(const vector<string>& names) const { return ObjectVector<TGraph2D*>(names); }
108
109vector<TProfile*> CbmHistManager::P1Vector(const vector<string>& names) const { return ObjectVector<TProfile*>(names); }
110
111vector<TProfile2D*> CbmHistManager::P2Vector(const vector<string>& names) const
112{
113 return ObjectVector<TProfile2D*>(names);
114}
115
117{
118 map<string, TNamed*>::iterator it;
119 for (it = fMap.begin(); it != fMap.end(); it++) {
120 it->second->Write();
121 }
122}
123
125{
126 for (auto& canvas : fCanvases) {
127 canvas->Write();
128 }
129}
130
132{
133 assert(file != nullptr);
134 LOG(info) << "CbmHistManager::ReadFromFile";
135 TDirectory* dir = gDirectory;
136 TIter nextkey(dir->GetListOfKeys());
137 TKey* key;
138 while ((key = (TKey*) nextkey())) {
139 TObject* obj = key->ReadObj();
140 AddTNamedObject(obj);
142 }
143}
144
146{
147 if (obj->IsA()->InheritsFrom(TH1::Class()) || obj->IsA()->InheritsFrom(TGraph::Class())
148 || obj->IsA()->InheritsFrom(TGraph2D::Class()) || obj->IsA()->InheritsFrom(THnBase::Class())) {
149 TNamed* h = (TNamed*) obj;
150 //TNamed* h1 = (TNamed*)file->Get(h->GetName());
151 Add(string(h->GetName()), h);
152 }
153}
154
156{
157 if (obj->IsA()->InheritsFrom(TDirectoryFile::Class())) {
158 TDirectoryFile* fileDir = (TDirectoryFile*) obj;
159 TIter nextkey(fileDir->GetListOfKeys());
160 TKey* key2;
161 while ((key2 = (TKey*) nextkey())) {
162 TObject* obj2 = key2->ReadObj();
163 AddTNamedObject(obj2);
165 }
166 }
167}
168
170{
171 for (auto& pair : fMap) {
172 delete pair.second;
173 }
174 fMap.clear();
175
176 for (auto& canvas : fCanvases) {
177 delete canvas;
178 }
179 fCanvases.clear();
180}
181
182void CbmHistManager::ShrinkEmptyBinsH1(const string& histName)
183{
184 TH1* hist = H1(histName);
185 Int_t nofBins = hist->GetNbinsX();
186 Int_t minShrinkBin = std::numeric_limits<Int_t>::max();
187 Int_t maxShrinkBin = std::numeric_limits<Int_t>::min();
188 Bool_t isSet = false;
189 for (Int_t iBin = 1; iBin <= nofBins; iBin++) {
190 Double_t content = hist->GetBinContent(iBin);
191 if (content != 0.) {
192 minShrinkBin = std::min(iBin, minShrinkBin);
193 maxShrinkBin = std::max(iBin, maxShrinkBin);
194 isSet = true;
195 }
196 }
197 if (isSet) {
198 hist->GetXaxis()->SetRange(minShrinkBin, maxShrinkBin);
199 // hist->GetYaxis()->SetRange(minShrinkBin, maxShrinkBin);
200 }
201}
202
204{
205 vector<TH1*> effHistos = H1Vector(pattern);
206 Int_t nofEffHistos = effHistos.size();
207 for (Int_t iHist = 0; iHist < nofEffHistos; iHist++) {
208 ShrinkEmptyBinsH1(effHistos[iHist]->GetName());
209 }
210}
211
212void CbmHistManager::ShrinkEmptyBinsH2(const string& histName)
213{
214 TH1* hist = H2(histName);
215 Int_t nofBinsX = hist->GetNbinsX();
216 Int_t nofBinsY = hist->GetNbinsY();
217 Int_t minShrinkBinX = std::numeric_limits<Int_t>::max();
218 Int_t maxShrinkBinX = std::numeric_limits<Int_t>::min();
219 Int_t minShrinkBinY = std::numeric_limits<Int_t>::max();
220 Int_t maxShrinkBinY = std::numeric_limits<Int_t>::min();
221 Bool_t isSet = false;
222 for (Int_t iBinX = 1; iBinX <= nofBinsX; iBinX++) {
223 for (Int_t iBinY = 1; iBinY <= nofBinsY; iBinY++) {
224 Double_t content = hist->GetBinContent(iBinX, iBinY);
225 if (content != 0.) {
226 minShrinkBinX = std::min(iBinX, minShrinkBinX);
227 maxShrinkBinX = std::max(iBinX, maxShrinkBinX);
228 minShrinkBinY = std::min(iBinY, minShrinkBinY);
229 maxShrinkBinY = std::max(iBinY, maxShrinkBinY);
230 isSet = true;
231 }
232 }
233 }
234 if (isSet) {
235 hist->GetXaxis()->SetRange(minShrinkBinX, maxShrinkBinX);
236 hist->GetYaxis()->SetRange(minShrinkBinY, maxShrinkBinY);
237 }
238}
239
241{
242 vector<TH1*> effHistos = H1Vector(pattern);
243 Int_t nofEffHistos = effHistos.size();
244 for (Int_t iHist = 0; iHist < nofEffHistos; iHist++) {
245 ShrinkEmptyBinsH2(effHistos[iHist]->GetName());
246 }
247}
248
249void CbmHistManager::Scale(const string& histName, Double_t scale) { H1(histName)->Scale(scale); }
250
251void CbmHistManager::ScaleByPattern(const string& pattern, Double_t scale)
252{
253 vector<TH1*> effHistos = H1Vector(pattern);
254 Int_t nofEffHistos = effHistos.size();
255 for (Int_t iHist = 0; iHist < nofEffHistos; iHist++) {
256 Scale(effHistos[iHist]->GetName(), scale);
257 }
258}
259
260void CbmHistManager::NormalizeToIntegral(const string& histName)
261{
262 TH1* hist = H1(histName);
263 hist->Scale(1. / hist->Integral());
264}
265
267{
268 vector<TH1*> effHistos = H1Vector(pattern);
269 Int_t nofEffHistos = effHistos.size();
270 for (Int_t iHist = 0; iHist < nofEffHistos; iHist++) {
271 NormalizeToIntegral(effHistos[iHist]->GetName());
272 }
273}
274
275void CbmHistManager::Rebin(const string& histName, Int_t ngroup)
276{
277 TH1* hist = H1(histName);
278 if (ngroup > 1) {
279 hist->Rebin(ngroup);
280 hist->Scale(1. / (Double_t) ngroup);
281 }
282}
283
284void CbmHistManager::RebinByPattern(const string& pattern, Int_t ngroup)
285{
286 vector<TH1*> effHistos = H1Vector(pattern);
287 Int_t nofEffHistos = effHistos.size();
288 for (Int_t iHist = 0; iHist < nofEffHistos; iHist++) {
289 Rebin(effHistos[iHist]->GetName(), ngroup);
290 }
291}
292
294{
295 string str = "CbmHistManager list of histograms:\n";
296 map<string, TNamed*>::const_iterator it;
297 for (it = fMap.begin(); it != fMap.end(); it++) {
298 str += it->first + "\n";
299 }
300 return str;
301}
302
303TCanvas* CbmHistManager::CreateCanvas(const std::string& name, const std::string& title, Int_t width, Int_t height)
304{
305 TCanvas* c = new TCanvas(name.c_str(), title.c_str(), width, height);
306 fCanvases.push_back(c);
307 return c;
308}
309
310void CbmHistManager::SaveCanvasToImage(const std::string& outputDir, const std::string& options)
311{
312 for (unsigned int i = 0; i < fCanvases.size(); i++) {
313 Cbm::SaveCanvasAsImage(fCanvases[i], outputDir, options);
314 }
315}
316
ClassImp(CbmConverterManager)
Histogram manager.
Histogram manager.
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.
std::vector< TGraph2D * > G2Vector(const std::vector< std::string > &names) const
Return vector of pointers to TGraph2D.
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< 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< TProfile2D * > P2Vector(const std::vector< std::string > &names) const
Return vector of pointers to TProfile2D.
std::map< std::string, TNamed * > fMap
TH2 * H2(const std::string &name) const
Return pointer to TH2 histogram.
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 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.
void WriteToFile()
Write all objects to current opened file.
void ShrinkEmptyBinsH2(const std::string &histName)
Shrink empty bins in H2.
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.
void NormalizeToIntegralByPattern(const std::string &pattern)
Normalize histograms to integral which name matches specified pattern.
void Add(const std::string &name, TNamed *object)
Add new named object to manager.
void ShrinkEmptyBinsH1(const std::string &histName)
Shrink empty bins in H1.
virtual ~CbmHistManager()
Destructor.
std::string ToString() const
Return string representation of class.
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.
Data class with information on a STS local track.
void SaveCanvasAsImage(TCanvas *c, const string &dir, const string &option)
Definition CbmUtils.cxx:36