Skip to content

Commit 9aea348

Browse files
committed
extend HelperFunctions
1 parent aeb0367 commit 9aea348

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

infra/HelperFunctions.hpp

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,36 +122,43 @@ inline T* GetObjectWithNullptrCheck(TFile* fileIn, const std::string& objectName
122122
}
123123

124124
inline void CheckHistogramsForXaxisIdentity(const TH1* h1, const TH1* h2) {
125-
if(h1->GetNbinsX() != h2->GetNbinsX()) {
125+
if (h1->GetNbinsX() != h2->GetNbinsX()) {
126126
throw std::runtime_error("HelperFunctions::CheckHistogramsForXaxisIdentity(): nBinsX do not match for " + static_cast<std::string>(h1->GetName()) + " and " + h2->GetName());
127127
}
128128
const int nBins = h1->GetNbinsX();
129-
for(int iBin=1; iBin<=nBins; iBin++) {
130-
if(std::abs(h1->GetBinCenter(iBin) - h2->GetBinCenter(iBin)) > 1e-6) {
129+
for (int iBin = 1; iBin <= nBins; iBin++) {
130+
if (std::abs(h1->GetBinCenter(iBin) - h2->GetBinCenter(iBin)) > 1e-6) {
131131
throw std::runtime_error("HelperFunctions::CheckHistogramsForXaxisIdentity(): bins do not coincide for " + static_cast<std::string>(h1->GetName()) + " and " + h2->GetName());
132132
}
133133
}
134134
}
135135

136+
inline void Sumw2IfNotYet(TH1* histo, bool value = true) {
137+
const bool isSumw2Already = histo->GetSumw2N() > 0;
138+
if (isSumw2Already != value) histo->Sumw2(value);
139+
}
140+
136141
inline TH1* MergeHistograms(const std::vector<TH1*>& histos) {
137-
for(const auto& h : histos) {
142+
for (const auto& h : histos) {
138143
CheckHistogramsForXaxisIdentity(h, histos.at(0));
139144
}
140145

146+
const bool isSumw2 = histos.at(0)->GetSumw2N() > 0;
147+
141148
TH1* hResult = dynamic_cast<TH1*>(histos.at(0)->Clone("hMerged"));
142-
hResult->Sumw2();
149+
Sumw2IfNotYet(hResult);
143150
hResult->SetDirectory(nullptr);
144-
for(size_t iH=1, nHs=histos.size(); iH<nHs; ++iH) {
151+
for (size_t iH = 1, nHs = histos.size(); iH < nHs; ++iH) {
145152
hResult->Add(histos.at(iH));
146153
}
147-
hResult->Sumw2(false);
154+
Sumw2IfNotYet(hResult, isSumw2);
148155

149156
return hResult;
150157
}
151158

152159
inline TH1* MergeHistograms(TFile* fileIn, const std::vector<std::string>& histoNames) {
153160
std::vector<TH1*> histos;
154-
for(const auto& hN : histoNames) {
161+
for (const auto& hN : histoNames) {
155162
histos.emplace_back(GetObjectWithNullptrCheck<TH1>(fileIn, hN));
156163
}
157164
TH1* hResult = MergeHistograms(histos);
@@ -174,5 +181,13 @@ inline void CD(T* fileOrDirectory, const std::string& name) {
174181
destination->cd();
175182
}
176183

184+
inline double InterpolateTH1SuppressWarning(const TH1* h, double value) {
185+
double result;
186+
if (value <= h->GetBinLowEdge(1) || value >= h->GetBinLowEdge(h->GetNbinsX() + 1)) result = 0.;
187+
else
188+
result = h->Interpolate(value);
189+
return result;
190+
}
191+
177192
}// namespace HelperFunctions
178193
#endif// ANALYSISTREE_INFRA_HELPER_FUNCTIONS_HPP

0 commit comments

Comments
 (0)