BmnRoot 26.10.0-18-g4e3414f9
Software framework of the BM@N experiment
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
30using namespace std;
31
35
37
39{
40 map<TString, TNamed*>::iterator it;
41 for (it = fMap.begin(); it != fMap.end(); it++) {
42 it->second->Write();
43 }
44}
45
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
56{
57 assert(file != NULL);
58 cout << "-I- BmnHistManager::ReadFromFile" << endl;
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
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
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
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
133void BmnHistManager::Scale(const TString& histName, Double_t scale)
134{
135 H1(histName)->Scale(scale);
136}
137
139{
140 TH1* hist = H1(histName);
141 hist->Scale(1. / hist->Integral());
142}
143
144void 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
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}
Histogram manager.
bool Bool_t
int Int_t
double Double_t
#define gDirectory
virtual ~BmnHistManager()
Destructor.
void Add(const TString &name, TNamed *object)
Add new named object to manager.
void ResetHists()
Reset all histograms.
void ShrinkEmptyBinsH2(const TString &histName)
Shrink empty bins in H2.
BmnHistManager()
Constructor.
void Rebin(const TString &histName, Int_t ngroup)
Rebin histogram.
void Scale(const TString &histName, Double_t scale)
Scale histogram.
TH2 * H2(const TString &name) const
Return pointer to TH2 histogram.
map< TString, TNamed * > fMap
void WriteToFile()
Write all histograms to current opened file.
void Clear()
Clear memory.
TString ToString() const
Return string representation of class.
void NormalizeToIntegral(const TString &histName)
Normalize histogram to integral.
TH1 * H1(const TString &name) const
Return pointer to TH1 histogram.
void ReadFromFile(TFile *file)
Read histograms from file.
void ShrinkEmptyBinsH1(const TString &histName)
Shrink empty bins in H1.
virtual void SetRange(Int_t first=0, Int_t last=0)
Bool_t InheritsFrom(const char *cl) const override
T * Get(const char *namecycle)
virtual TList * GetListOfKeys() const
static TClass * Class()
static TClass * Class()
static TClass * Class()
virtual Int_t GetNbinsY() const
TAxis * GetXaxis()
virtual Int_t GetNbinsX() const
TAxis * GetYaxis()
virtual TH1 * Rebin(Int_t ngroup=2, const char *newname="", const Double_t *xbins=nullptr)
virtual Double_t Integral(Int_t binx1, Int_t binx2, Option_t *option="") const
virtual Double_t GetBinContent(Int_t bin) const
virtual void Scale(Double_t c1=1, Option_t *option="")
const char * GetName() const override
virtual TClass * IsA() const
Definition json.hpp:4446