15#include "TDirectory.h"
24#include "TProfile2D.h"
29#include <boost/regex.hpp>
46class CompareTNamedMore :
public std::binary_function<const TNamed*, const TNamed*, Bool_t> {
48 Bool_t
operator()(
const TNamed* object1,
const TNamed* object2)
const
50 return string(object1->GetName()) > string(object2->GetName());
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);
73 catch (exception& ex) {
74 LOG(info) <<
"Exception in CbmHistManager::ObjectVector: " << ex.what();
95 map<string, TNamed*>::iterator it;
96 for (it =
fMap.begin(); it !=
fMap.end(); it++) {
101void CbmHistManager::WriteToFileNew(
const string& str)
103 std::string histoName =
"";
104 map<string, TNamed*>::iterator it;
107 TFile* oldFile = gFile;
108 TDirectory* oldDir = gDirectory;
110 TFile*
fHist =
new TFile(str.c_str(),
"RECREATE");
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))) {
120 std::cout <<
"Histogram: " << it->first << std::endl;
125 TDirectory* cdHistosQa =
fHist->mkdir(
"HistosQa");
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) {
146 assert(file != NULL);
147 LOG(info) <<
"CbmHistManager::ReadFromFile";
148 TDirectory* dir = gDirectory;
149 TIter nextkey(dir->GetListOfKeys());
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);
166 map<string, TNamed*>::iterator it;
167 for (it =
fMap.begin(); it !=
fMap.end(); it++) {
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);
183 minShrinkBin = std::min(iBin, minShrinkBin);
184 maxShrinkBin = std::max(iBin, maxShrinkBin);
189 hist->GetXaxis()->SetRange(minShrinkBin, maxShrinkBin);
196 vector<TH1*> effHistos =
H1Vector(pattern);
197 Int_t nofEffHistos = effHistos.size();
198 for (Int_t iHist = 0; iHist < nofEffHistos; iHist++) {
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);
217 minShrinkBinX = std::min(iBinX, minShrinkBinX);
218 maxShrinkBinX = std::max(iBinX, maxShrinkBinX);
219 minShrinkBinY = std::min(iBinY, minShrinkBinY);
220 maxShrinkBinY = std::max(iBinY, maxShrinkBinY);
226 hist->GetXaxis()->SetRange(minShrinkBinX, maxShrinkBinX);
227 hist->GetYaxis()->SetRange(minShrinkBinY, maxShrinkBinY);
233 vector<TH1*> effHistos =
H1Vector(pattern);
234 Int_t nofEffHistos = effHistos.size();
235 for (Int_t iHist = 0; iHist < nofEffHistos; iHist++) {
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);
253 TH1* hist =
H1(histName);
254 hist->Scale(1. / hist->Integral());
259 vector<TH1*> effHistos =
H1Vector(pattern);
260 Int_t nofEffHistos = effHistos.size();
261 for (Int_t iHist = 0; iHist < nofEffHistos; iHist++) {
268 TH1* hist =
H1(histName);
271 hist->Scale(1. / (Double_t) ngroup);
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);
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";
296 TCanvas* c =
new TCanvas(name.c_str(), title.c_str(), width, height);
303 for (
unsigned int i = 0; i <
fCanvases.size(); i++) {
ClassImp(CbmConverterManager)
void ShrinkEmptyBinsH1ByPattern(const std::string &pattern)
Shrink empty bins in H1.
CbmHistManager()
Constructor.
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)