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