BmnRoot
26.10.0-18-g4e3414f9
Software framework of the BM@N experiment
Toggle main menu visibility
Loading...
Searching...
No Matches
BmnHistManager.cxx
Go to the documentation of this file.
1
/**
2
* \file BmnHistManager.cxx
3
* \brief Histogram manager.
4
* \author Semen Lebedev <s.lebedev@gsi.de>
5
* \date 2011
6
*/
7
8
#include "
BmnHistManager.h
"
9
10
#include "
TClass.h
"
11
#include "
TDirectory.h
"
12
#include "
TFile.h
"
13
#include "
TGraph.h
"
14
#include "
TGraph2D.h
"
15
#include "
TH1.h
"
16
#include "
TH2.h
"
17
#include "
TKey.h
"
18
#include "
TNamed.h
"
19
#include "
TProfile.h
"
20
#include "
TProfile2D.h
"
21
22
#include <algorithm>
23
#include <cassert>
24
#include <iostream>
25
#include <limits>
26
#include <map>
27
#include <string>
28
#include <vector>
29
30
using namespace
std
;
31
32
BmnHistManager::BmnHistManager
()
33
:
fMap
()
34
{}
35
36
BmnHistManager::~BmnHistManager
() {}
37
38
void
BmnHistManager::WriteToFile
()
39
{
40
map<TString, TNamed*>::iterator it;
41
for
(it =
fMap
.begin(); it !=
fMap
.end(); it++) {
42
it->second->Write();
43
}
44
}
45
46
void
BmnHistManager::ResetHists
()
47
{
48
map<TString, TNamed*>::iterator it;
49
for
(it =
fMap
.begin(); it !=
fMap
.end(); it++) {
50
if
(it->second->IsA()->InheritsFrom(
TH1::Class
()))
51
static_cast<
TH1
*
>
(it->second)->Reset();
52
}
53
}
54
55
void
BmnHistManager::ReadFromFile
(
TFile
* file)
56
{
57
assert(file != NULL);
58
cout <<
"-I- BmnHistManager::ReadFromFile"
<< endl;
59
TDirectory
* dir =
gDirectory
;
60
TIter
nextkey(dir->
GetListOfKeys
());
61
TKey
* key;
62
// Int_t c = 0;
63
while
((key = (
TKey
*)nextkey()) !=
nullptr
) {
64
TObject
* obj = key->ReadObj();
65
if
(obj->
IsA
()->
InheritsFrom
(
TH1::Class
()) || obj->
IsA
()->
InheritsFrom
(
TGraph::Class
())
66
|| obj->
IsA
()->
InheritsFrom
(
TGraph2D::Class
()))
67
{
68
TNamed
* h = (
TNamed
*)obj;
69
TNamed
* h1 = (
TNamed
*)file->
Get
(h->
GetName
());
70
Add
(
TString
(h->
GetName
()), h1);
71
}
72
}
73
}
74
75
void
BmnHistManager::Clear
()
76
{
77
map<TString, TNamed*>::iterator it;
78
for
(it =
fMap
.begin(); it !=
fMap
.end(); it++) {
79
delete
(*it).second;
80
}
81
fMap
.clear();
82
}
83
84
void
BmnHistManager::ShrinkEmptyBinsH1
(
const
TString
& histName)
85
{
86
TH1
* hist =
H1
(histName);
87
Int_t
nofBins = hist->
GetNbinsX
();
88
Int_t
minShrinkBin = std::numeric_limits<Int_t>::max();
89
Int_t
maxShrinkBin = std::numeric_limits<Int_t>::min();
90
Bool_t
isSet =
false
;
91
for
(
Int_t
iBin = 1; iBin <= nofBins; iBin++) {
92
Double_t
content = hist->
GetBinContent
(iBin);
93
if
(content != 0.) {
94
minShrinkBin = std::min(iBin, minShrinkBin);
95
maxShrinkBin = std::max(iBin, maxShrinkBin);
96
isSet =
true
;
97
}
98
}
99
if
(isSet) {
100
hist->
GetXaxis
()->
SetRange
(minShrinkBin, maxShrinkBin);
101
hist->
GetYaxis
()->
SetRange
(minShrinkBin, maxShrinkBin);
102
}
103
}
104
105
void
BmnHistManager::ShrinkEmptyBinsH2
(
const
TString
& histName)
106
{
107
TH1
* hist =
H2
(histName);
108
Int_t
nofBinsX = hist->
GetNbinsX
();
109
Int_t
nofBinsY = hist->
GetNbinsY
();
110
Int_t
minShrinkBinX = std::numeric_limits<Int_t>::max();
111
Int_t
maxShrinkBinX = std::numeric_limits<Int_t>::min();
112
Int_t
minShrinkBinY = std::numeric_limits<Int_t>::max();
113
Int_t
maxShrinkBinY = std::numeric_limits<Int_t>::min();
114
Bool_t
isSet =
false
;
115
for
(
Int_t
iBinX = 1; iBinX <= nofBinsX; iBinX++) {
116
for
(
Int_t
iBinY = 1; iBinY <= nofBinsY; iBinY++) {
117
Double_t
content = hist->
GetBinContent
(iBinX, iBinY);
118
if
(content != 0.) {
119
minShrinkBinX = std::min(iBinX, minShrinkBinX);
120
maxShrinkBinX = std::max(iBinX, maxShrinkBinX);
121
minShrinkBinY = std::min(iBinY, minShrinkBinY);
122
maxShrinkBinY = std::max(iBinY, maxShrinkBinY);
123
isSet =
true
;
124
}
125
}
126
}
127
if
(isSet) {
128
hist->
GetXaxis
()->
SetRange
(minShrinkBinX, maxShrinkBinX);
129
hist->
GetYaxis
()->
SetRange
(minShrinkBinY, maxShrinkBinY);
130
}
131
}
132
133
void
BmnHistManager::Scale
(
const
TString
& histName,
Double_t
scale)
134
{
135
H1
(histName)->
Scale
(scale);
136
}
137
138
void
BmnHistManager::NormalizeToIntegral
(
const
TString
& histName)
139
{
140
TH1
* hist =
H1
(histName);
141
hist->
Scale
(1. / hist->
Integral
());
142
}
143
144
void
BmnHistManager::Rebin
(
const
TString
& histName,
Int_t
ngroup)
145
{
146
TH1
* hist =
H1
(histName);
147
if
(ngroup > 1) {
148
hist->
Rebin
(ngroup);
149
hist->
Scale
(1. / (
Double_t
)ngroup);
150
}
151
}
152
153
TString
BmnHistManager::ToString
()
const
154
{
155
TString
str =
"BmnHistManager list of histograms:\n"
;
156
map<TString, TNamed*>::const_iterator it;
157
for
(it =
fMap
.begin(); it !=
fMap
.end(); it++) {
158
str += it->first +
"\n"
;
159
}
160
return
str;
161
}
BmnHistManager.h
Histogram manager.
Bool_t
bool Bool_t
Int_t
int Int_t
Double_t
double Double_t
TClass.h
TDirectory.h
gDirectory
#define gDirectory
TFile.h
TGraph2D.h
TGraph.h
TH1.h
TH2.h
TKey.h
TNamed.h
TProfile2D.h
TProfile.h
BmnHistManager::~BmnHistManager
virtual ~BmnHistManager()
Destructor.
Definition
BmnHistManager.cxx:36
BmnHistManager::Add
void Add(const TString &name, TNamed *object)
Add new named object to manager.
Definition
BmnHistManager.h:59
BmnHistManager::ResetHists
void ResetHists()
Reset all histograms.
Definition
BmnHistManager.cxx:46
BmnHistManager::ShrinkEmptyBinsH2
void ShrinkEmptyBinsH2(const TString &histName)
Shrink empty bins in H2.
Definition
BmnHistManager.cxx:105
BmnHistManager::BmnHistManager
BmnHistManager()
Constructor.
Definition
BmnHistManager.cxx:32
BmnHistManager::Rebin
void Rebin(const TString &histName, Int_t ngroup)
Rebin histogram.
Definition
BmnHistManager.cxx:144
BmnHistManager::Scale
void Scale(const TString &histName, Double_t scale)
Scale histogram.
Definition
BmnHistManager.cxx:133
BmnHistManager::H2
TH2 * H2(const TString &name) const
Return pointer to TH2 histogram.
Definition
BmnHistManager.h:128
BmnHistManager::fMap
map< TString, TNamed * > fMap
Definition
BmnHistManager.h:215
BmnHistManager::WriteToFile
void WriteToFile()
Write all histograms to current opened file.
Definition
BmnHistManager.cxx:38
BmnHistManager::Clear
void Clear()
Clear memory.
Definition
BmnHistManager.cxx:75
BmnHistManager::ToString
TString ToString() const
Return string representation of class.
Definition
BmnHistManager.cxx:153
BmnHistManager::NormalizeToIntegral
void NormalizeToIntegral(const TString &histName)
Normalize histogram to integral.
Definition
BmnHistManager.cxx:138
BmnHistManager::H1
TH1 * H1(const TString &name) const
Return pointer to TH1 histogram.
Definition
BmnHistManager.h:114
BmnHistManager::ReadFromFile
void ReadFromFile(TFile *file)
Read histograms from file.
Definition
BmnHistManager.cxx:55
BmnHistManager::ShrinkEmptyBinsH1
void ShrinkEmptyBinsH1(const TString &histName)
Shrink empty bins in H1.
Definition
BmnHistManager.cxx:84
TAxis::SetRange
virtual void SetRange(Int_t first=0, Int_t last=0)
TClass::InheritsFrom
Bool_t InheritsFrom(const char *cl) const override
TDirectoryFile::Get
T * Get(const char *namecycle)
TDirectory
TDirectory::GetListOfKeys
virtual TList * GetListOfKeys() const
TFile
TGraph2D::Class
static TClass * Class()
TGraph::Class
static TClass * Class()
TH1
TH1::Class
static TClass * Class()
TH1::GetNbinsY
virtual Int_t GetNbinsY() const
TH1::GetXaxis
TAxis * GetXaxis()
TH1::GetNbinsX
virtual Int_t GetNbinsX() const
TH1::GetYaxis
TAxis * GetYaxis()
TH1::Rebin
virtual TH1 * Rebin(Int_t ngroup=2, const char *newname="", const Double_t *xbins=nullptr)
TH1::Integral
virtual Double_t Integral(Int_t binx1, Int_t binx2, Option_t *option="") const
TH1::GetBinContent
virtual Double_t GetBinContent(Int_t bin) const
TH1::Scale
virtual void Scale(Double_t c1=1, Option_t *option="")
TIter
TKey
TNamed
TNamed::GetName
const char * GetName() const override
TObject::IsA
virtual TClass * IsA() const
TObject::TObject
TObject()
TString
std
Definition
json.hpp:4446
QA
report
BmnHistManager.cxx
Generated on
for BmnRoot by
1.17.0