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