CbmRoot
core/base/CbmHistManager.cxx
Go to the documentation of this file.
1 
7 #include "CbmHistManager.h"
8 
9 #include "CbmUtils.h" // for SaveCanvasA...
10 
11 #include <TAxis.h> // for TAxis
12 #include <TCanvas.h> // for TCanvas
13 #include <TClass.h> // for TClass
14 #include <TCollection.h> // for TIter
15 #include <TDirectory.h> // for TDirectory
16 #include <TDirectoryFile.h> // for TDirectoryFile
17 #include <TGenericClassInfo.h> // for TGenericCla...
18 #include <TGraph.h> // for TGraph
19 #include <TGraph2D.h> // for TGraph2D
20 #include <TH1.h> // for TH1
21 #include <TH2.h> // for TH2
22 #include <TKey.h> // for TKey
23 #include <TList.h> // for TList
24 #include <TNamed.h> // for TNamed
25 #include <TObject.h> // for TObject
26 #include <TProfile.h> // for TProfile
27 #include <TProfile2D.h> // for TProfile2D
28 
29 #include <boost/regex.hpp> // for basic_regex, regex, regex_match
30 
31 #include <algorithm> // for max, min, fill
32 #include <cassert> // for assert
33 #include <exception> // for exception
34 #include <iostream> // for string
35 #include <limits> // for numeric_limits
36 #include <map> // for map, operat...
37 #include <string> // for basic_string
38 #include <vector> // for vector
39 
40 using std::exception;
41 using std::map;
42 using std::sort;
43 using std::string;
44 using std::vector;
45 
47  public std::binary_function<const TNamed*, const TNamed*, Bool_t> {
48 public:
49  Bool_t operator()(const TNamed* object1, const TNamed* object2) const {
50  return string(object1->GetName()) > string(object2->GetName());
51  }
52 };
53 
54 CbmHistManager::CbmHistManager() : fMap(), fCanvases() {}
55 
57 
58 template<class T>
59 vector<T> CbmHistManager::ObjectVector(const string& pattern) const {
60  vector<T> objects;
61 
62  try {
63  const boost::regex e(pattern);
64  map<string, TNamed*>::const_iterator it;
65  for (it = fMap.begin(); it != fMap.end(); it++) {
66  if (boost::regex_match(it->first, e)) {
67  T ObjectPointer = dynamic_cast<T>(it->second);
68  if (ObjectPointer != nullptr) objects.push_back(ObjectPointer);
69  }
70  }
71  } catch (exception& ex) {
72  LOG(info) << "Exception in CbmHistManager::ObjectVector: " << ex.what();
73  }
74 
75  sort(objects.begin(), objects.end(), CompareTNamedMore());
76  return objects;
77 }
78 
79 vector<TH1*> CbmHistManager::H1Vector(const string& pattern) const {
80  return ObjectVector<TH1*>(pattern);
81 }
82 
83 vector<TH2*> CbmHistManager::H2Vector(const string& pattern) const {
84  return ObjectVector<TH2*>(pattern);
85 }
86 
87 vector<TGraph*> CbmHistManager::G1Vector(const string& pattern) const {
88  return ObjectVector<TGraph*>(pattern);
89 }
90 
91 vector<TGraph2D*> CbmHistManager::G2Vector(const string& pattern) const {
92  return ObjectVector<TGraph2D*>(pattern);
93 }
94 
95 vector<TProfile*> CbmHistManager::P1Vector(const string& pattern) const {
96  return ObjectVector<TProfile*>(pattern);
97 }
98 
99 vector<TProfile2D*> CbmHistManager::P2Vector(const string& pattern) const {
100  return ObjectVector<TProfile2D*>(pattern);
101 }
102 
104  map<string, TNamed*>::iterator it;
105  for (it = fMap.begin(); it != fMap.end(); it++) {
106  it->second->Write();
107  }
108 }
109 
110 void CbmHistManager::ReadFromFile(TFile* file) {
111  assert(file != nullptr);
112  LOG(info) << "CbmHistManager::ReadFromFile";
113  TDirectory* dir = gDirectory;
114  TIter nextkey(dir->GetListOfKeys());
115  TKey* key;
116  while ((key = (TKey*) nextkey())) {
117  TObject* obj = key->ReadObj();
118  AddTNamedObject(obj);
119  AddTDirectoryObject(obj);
120  }
121 }
122 
124  if (obj->IsA()->InheritsFrom(TH1::Class())
125  || obj->IsA()->InheritsFrom(TGraph::Class())
126  || obj->IsA()->InheritsFrom(TGraph2D::Class())) {
127  TNamed* h = (TNamed*) obj;
128  //TNamed* h1 = (TNamed*)file->Get(h->GetName());
129  Add(string(h->GetName()), h);
130  }
131 }
132 
134  if (obj->IsA()->InheritsFrom(TDirectoryFile::Class())) {
135  TDirectoryFile* fileDir = (TDirectoryFile*) obj;
136  TIter nextkey(fileDir->GetListOfKeys());
137  TKey* key2;
138  while ((key2 = (TKey*) nextkey())) {
139  TObject* obj2 = key2->ReadObj();
140  AddTNamedObject(obj2);
141  AddTDirectoryObject(obj2);
142  }
143  }
144 }
145 
146 void CbmHistManager::Clear(Option_t*) {
147  map<string, TNamed*>::iterator it;
148  for (it = fMap.begin(); it != fMap.end(); it++) {
149  delete (*it).second;
150  }
151  fMap.clear();
152 }
153 
154 void CbmHistManager::ShrinkEmptyBinsH1(const string& histName) {
155  TH1* hist = H1(histName);
156  Int_t nofBins = hist->GetNbinsX();
157  Int_t minShrinkBin = std::numeric_limits<Int_t>::max();
158  Int_t maxShrinkBin = std::numeric_limits<Int_t>::min();
159  Bool_t isSet = false;
160  for (Int_t iBin = 1; iBin <= nofBins; iBin++) {
161  Double_t content = hist->GetBinContent(iBin);
162  if (content != 0.) {
163  minShrinkBin = std::min(iBin, minShrinkBin);
164  maxShrinkBin = std::max(iBin, maxShrinkBin);
165  isSet = true;
166  }
167  }
168  if (isSet) {
169  hist->GetXaxis()->SetRange(minShrinkBin, maxShrinkBin);
170  // hist->GetYaxis()->SetRange(minShrinkBin, maxShrinkBin);
171  }
172 }
173 
174 void CbmHistManager::ShrinkEmptyBinsH1ByPattern(const string& pattern) {
175  vector<TH1*> effHistos = H1Vector(pattern);
176  Int_t nofEffHistos = effHistos.size();
177  for (Int_t iHist = 0; iHist < nofEffHistos; iHist++) {
178  ShrinkEmptyBinsH1(effHistos[iHist]->GetName());
179  }
180 }
181 
182 void CbmHistManager::ShrinkEmptyBinsH2(const string& histName) {
183  TH1* hist = H2(histName);
184  Int_t nofBinsX = hist->GetNbinsX();
185  Int_t nofBinsY = hist->GetNbinsY();
186  Int_t minShrinkBinX = std::numeric_limits<Int_t>::max();
187  Int_t maxShrinkBinX = std::numeric_limits<Int_t>::min();
188  Int_t minShrinkBinY = std::numeric_limits<Int_t>::max();
189  Int_t maxShrinkBinY = std::numeric_limits<Int_t>::min();
190  Bool_t isSet = false;
191  for (Int_t iBinX = 1; iBinX <= nofBinsX; iBinX++) {
192  for (Int_t iBinY = 1; iBinY <= nofBinsY; iBinY++) {
193  Double_t content = hist->GetBinContent(iBinX, iBinY);
194  if (content != 0.) {
195  minShrinkBinX = std::min(iBinX, minShrinkBinX);
196  maxShrinkBinX = std::max(iBinX, maxShrinkBinX);
197  minShrinkBinY = std::min(iBinY, minShrinkBinY);
198  maxShrinkBinY = std::max(iBinY, maxShrinkBinY);
199  isSet = true;
200  }
201  }
202  }
203  if (isSet) {
204  hist->GetXaxis()->SetRange(minShrinkBinX, maxShrinkBinX);
205  hist->GetYaxis()->SetRange(minShrinkBinY, maxShrinkBinY);
206  }
207 }
208 
209 void CbmHistManager::ShrinkEmptyBinsH2ByPattern(const string& pattern) {
210  vector<TH1*> effHistos = H1Vector(pattern);
211  Int_t nofEffHistos = effHistos.size();
212  for (Int_t iHist = 0; iHist < nofEffHistos; iHist++) {
213  ShrinkEmptyBinsH2(effHistos[iHist]->GetName());
214  }
215 }
216 
217 void CbmHistManager::Scale(const string& histName, Double_t scale) {
218  H1(histName)->Scale(scale);
219 }
220 
221 void CbmHistManager::ScaleByPattern(const string& pattern, Double_t scale) {
222  vector<TH1*> effHistos = H1Vector(pattern);
223  Int_t nofEffHistos = effHistos.size();
224  for (Int_t iHist = 0; iHist < nofEffHistos; iHist++) {
225  Scale(effHistos[iHist]->GetName(), scale);
226  }
227 }
228 
229 void CbmHistManager::NormalizeToIntegral(const string& histName) {
230  TH1* hist = H1(histName);
231  hist->Scale(1. / hist->Integral());
232 }
233 
234 void CbmHistManager::NormalizeToIntegralByPattern(const string& pattern) {
235  vector<TH1*> effHistos = H1Vector(pattern);
236  Int_t nofEffHistos = effHistos.size();
237  for (Int_t iHist = 0; iHist < nofEffHistos; iHist++) {
238  NormalizeToIntegral(effHistos[iHist]->GetName());
239  }
240 }
241 
242 void CbmHistManager::Rebin(const string& histName, Int_t ngroup) {
243  TH1* hist = H1(histName);
244  if (ngroup > 1) {
245  hist->Rebin(ngroup);
246  hist->Scale(1. / (Double_t) ngroup);
247  }
248 }
249 
250 void CbmHistManager::RebinByPattern(const string& pattern, Int_t ngroup) {
251  vector<TH1*> effHistos = H1Vector(pattern);
252  Int_t nofEffHistos = effHistos.size();
253  for (Int_t iHist = 0; iHist < nofEffHistos; iHist++) {
254  Rebin(effHistos[iHist]->GetName(), ngroup);
255  }
256 }
257 
258 string CbmHistManager::ToString() const {
259  string str = "CbmHistManager list of histograms:\n";
260  map<string, TNamed*>::const_iterator it;
261  for (it = fMap.begin(); it != fMap.end(); it++) {
262  str += it->first + "\n";
263  }
264  return str;
265 }
266 
267 TCanvas* CbmHistManager::CreateCanvas(const std::string& name,
268  const std::string& title,
269  Int_t width,
270  Int_t height) {
271  TCanvas* c = new TCanvas(name.c_str(), title.c_str(), width, height);
272  fCanvases.push_back(c);
273  return c;
274 }
275 
276 void CbmHistManager::SaveCanvasToImage(const std::string& outputDir,
277  const std::string& options) {
278  for (unsigned int i = 0; i < fCanvases.size(); i++) {
279  Cbm::SaveCanvasAsImage(fCanvases[i], outputDir, options);
280  }
281 }
282 
CbmHistManager::Clear
void Clear(Option_t *)
Clear memory. Remove all histograms.
Definition: core/base/CbmHistManager.cxx:146
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::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::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
i
int i
Definition: L1/vectors/P4_F32vec4.h:25
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.h
Histogram manager.
min
friend F32vec4 min(const F32vec4 &a, const F32vec4 &b)
Definition: L1/vectors/P4_F32vec4.h:33
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
ClassImp
ClassImp(CbmConverterManager) InitStatus CbmConverterManager
Definition: CbmConverterManager.cxx:12
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
CbmUtils.h
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
CompareTNamedMore
Definition: core/base/CbmHistManager.cxx:47
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::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
Cbm::SaveCanvasAsImage
void SaveCanvasAsImage(TCanvas *c, const std::string &dir, const std::string &option)
Definition: CbmUtils.cxx:20
max
friend F32vec4 max(const F32vec4 &a, const F32vec4 &b)
Definition: L1/vectors/P4_F32vec4.h:36
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
CompareTNamedMore::operator()
Bool_t operator()(const TNamed *object1, const TNamed *object2) const
Definition: core/base/CbmHistManager.cxx:49
CbmHistManager::Add
void Add(const std::string &name, TNamed *object)
Add new named object to manager.
Definition: CbmHistManager.h:58