BmnRoot
Loading...
Searching...
No Matches
BmnHistManager.cxx
Go to the documentation of this file.
1
8#include "BmnHistManager.h"
9#include "TH1.h"
10#include "TH2.h"
11#include "TNamed.h"
12#include "TGraph.h"
13#include "TGraph2D.h"
14#include "TProfile.h"
15#include "TProfile2D.h"
16#include "TFile.h"
17#include "TDirectory.h"
18#include "TKey.h"
19#include "TClass.h"
20#include <vector>
21#include <map>
22#include <string>
23#include <cassert>
24#include <iostream>
25#include <algorithm>
26#include <limits>
27
28using namespace std;
29
30
32
34
36 map<TString, TNamed*>::iterator it;
37 for (it = fMap.begin(); it != fMap.end(); it++){
38 it->second->Write();
39 }
40}
41
43 map<TString, TNamed*>::iterator it;
44 for (it = fMap.begin(); it != fMap.end(); it++){
45 if (it->second->IsA()->InheritsFrom (TH1::Class()))
46 static_cast<TH1*>(it->second)->Reset();
47 }
48}
49
51 assert(file != NULL);
52 cout << "-I- BmnHistManager::ReadFromFile" << endl;
53 TDirectory* dir = gDirectory;
54 TIter nextkey(dir->GetListOfKeys());
55 TKey *key;
56 //Int_t c = 0;
57 while ((key = (TKey*) nextkey()) != nullptr)
58 {
59 TObject* obj = key->ReadObj();
60 if (obj->IsA()->InheritsFrom (TH1::Class()) || obj->IsA()->InheritsFrom (TGraph::Class()) || obj->IsA()->InheritsFrom (TGraph2D::Class())) {
61 TNamed* h = (TNamed*) obj;
62 TNamed* h1 = (TNamed*)file->Get(h->GetName());
63 Add(TString(h->GetName()), h1);
64 }
65 }
66}
67
69 map<TString, TNamed*>::iterator it;
70 for (it = fMap.begin(); it != fMap.end(); it++) {
71 delete (*it).second;
72 }
73 fMap.clear();
74}
75
76void BmnHistManager::ShrinkEmptyBinsH1(const TString& histName)
77{
78 TH1* hist = H1(histName);
79 Int_t nofBins = hist->GetNbinsX();
80 Int_t minShrinkBin = std::numeric_limits<Int_t>::max();
81 Int_t maxShrinkBin = std::numeric_limits<Int_t>::min();
82 Bool_t isSet = false;
83 for (Int_t iBin = 1; iBin <= nofBins; iBin++) {
84 Double_t content = hist->GetBinContent(iBin);
85 if (content != 0.) {
86 minShrinkBin = std::min(iBin, minShrinkBin);
87 maxShrinkBin = std::max(iBin, maxShrinkBin);
88 isSet = true;
89 }
90 }
91 if (isSet) {
92 hist->GetXaxis()->SetRange(minShrinkBin, maxShrinkBin);
93 hist->GetYaxis()->SetRange(minShrinkBin, maxShrinkBin);
94 }
95}
96
97void BmnHistManager::ShrinkEmptyBinsH2(const TString& histName) {
98 TH1* hist = H2(histName);
99 Int_t nofBinsX = hist->GetNbinsX();
100 Int_t nofBinsY = hist->GetNbinsY();
101 Int_t minShrinkBinX = std::numeric_limits<Int_t>::max();
102 Int_t maxShrinkBinX = std::numeric_limits<Int_t>::min();
103 Int_t minShrinkBinY = std::numeric_limits<Int_t>::max();
104 Int_t maxShrinkBinY = std::numeric_limits<Int_t>::min();
105 Bool_t isSet = false;
106 for (Int_t iBinX = 1; iBinX <= nofBinsX; iBinX++) {
107 for (Int_t iBinY = 1; iBinY <= nofBinsY; iBinY++) {
108 Double_t content = hist->GetBinContent(iBinX, iBinY);
109 if (content != 0.) {
110 minShrinkBinX = std::min(iBinX, minShrinkBinX);
111 maxShrinkBinX = std::max(iBinX, maxShrinkBinX);
112 minShrinkBinY = std::min(iBinY, minShrinkBinY);
113 maxShrinkBinY = std::max(iBinY, maxShrinkBinY);
114 isSet = true;
115 }
116 }
117 }
118 if (isSet) {
119 hist->GetXaxis()->SetRange(minShrinkBinX, maxShrinkBinX);
120 hist->GetYaxis()->SetRange(minShrinkBinY, maxShrinkBinY);
121 }
122}
123
124void BmnHistManager::Scale(const TString& histName, Double_t scale) {
125 H1(histName)->Scale(scale);
126}
127
128void BmnHistManager::NormalizeToIntegral(const TString& histName) {
129 TH1* hist = H1(histName);
130 hist->Scale(1. / hist->Integral());
131}
132
133void BmnHistManager::Rebin(const TString& histName, Int_t ngroup) {
134 TH1* hist = H1(histName);
135 if (ngroup > 1) {
136 hist->Rebin(ngroup);
137 hist->Scale(1. / (Double_t)ngroup);
138 }
139}
140
142{
143 TString str = "BmnHistManager list of histograms:\n";
144 map<TString, TNamed*>::const_iterator it;
145 for (it = fMap.begin(); it != fMap.end(); it++){
146 str += it->first + "\n";
147 }
148 return str;
149}
Histogram manager.
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.
void WriteToFile()
Write all histograms to current opened file.
void Clear()
Clear memory. Remove all histograms.
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.
STL namespace.