CbmRoot
Loading...
Searching...
No Matches
CbmQaOnlineInterface.h
Go to the documentation of this file.
1/* Copyright (C) 2024 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Sergei Zharko [committer] */
4
9
10// TODO: move to more suitable place (histoserv, for example)
11
12#pragma once
13
14#include "Logger.h"
15#include "TH1D.h"
16#include "TH2D.h"
17#include "TProfile.h"
18#include "TProfile2D.h"
19#include "algo/qa/Histogram.h"
20
21//#include <log.hpp>
22#include <type_traits>
23
24namespace
25{
30} // namespace
31
32namespace cbm::qa
33{
40 template<class RootHistogram>
41 class RootHistogramAccessor : public RootHistogram {
42
43 public:
44 template<class... Args>
45 RootHistogramAccessor(Args... args) : RootHistogram(args...)
46 {
47 }
48
53 template<class SourceQaHistogram>
54 void AddSliceFromQaHistogram(const SourceQaHistogram& histo, double val);
55
58 template<class QaHistogram>
59 void SetFromQaHistogram(const QaHistogram& histo);
60 };
61
65 public:
66 // TODO: generalize
71 static void AddSlice(const H1D& src, double value, TH2D* dst);
72
77 static void AddSlice(const Prof1D& src, double value, TProfile2D* dst);
78
82 static TH1D* ROOTHistogram(const H1D& hist);
83
87 static TH2D* ROOTHistogram(const H2D& hist);
88
92 static TProfile* ROOTHistogram(const Prof1D& prof);
93
97 static TProfile2D* ROOTHistogram(const Prof2D& prof);
98
106 static bool ConvertOutput(const std::string& inFilename, std::string outFilename = "");
107 };
108} // namespace cbm::qa
109
110
111namespace cbm::qa
112{
113 // -------------------------------------------------------------------------------------------------------------------
114 //
115 template<class RootHistogram>
116 template<class QaHistogram>
118 {
119 constexpr bool IsDstH2D = std::is_same_v<RootHistogram, TH2D>;
120 constexpr bool IsDstProf2D = std::is_same_v<RootHistogram, TProfile2D>;
121 constexpr bool IsSrcH1D = std::is_same_v<QaHistogram, H1D>;
122 constexpr bool IsSrcProf1D = std::is_same_v<QaHistogram, Prof1D>;
123
124 static_assert((IsDstH2D && IsSrcH1D) || (IsDstProf2D && IsSrcProf1D),
125 "function not implemented for a given (RootHistogram, QaHistogram) pair");
126
127 auto iBinX = this->GetXaxis()->FindBin(val);
128 for (int iBinY = 0; iBinY <= this->GetNbinsY() + 1; ++iBinY) {
129 int iGlobBin = this->GetBin(iBinX, iBinY);
130 auto binSrc = src.GetBinAccumulator(iBinY);
131 if constexpr (IsSrcH1D) {
132 this->fArray[iGlobBin] += binSrc.value();
133 if (this->fSumw2.fN) {
134 this->fSumw2.fArray[iGlobBin] += binSrc.variance();
135 }
136 }
137 else if constexpr (IsSrcProf1D) {
138 this->fArray[iGlobBin] += binSrc.GetSumWV();
139 this->fBinEntries.fArray[iGlobBin] += binSrc.GetSumW();
140 this->fSumw2.fArray[iGlobBin] += binSrc.GetSumWV2();
141 this->fBinSumw2.fArray[iGlobBin] += binSrc.GetSumW2();
142 this->fTsumwz += binSrc.GetSumWV();
143 this->fTsumwz2 += binSrc.GetSumWV2();
144 }
145 }
146 this->fTsumw += src.GetTotSumW();
147 this->fTsumw2 += src.GetTotSumW2();
148 this->fTsumwx += src.GetTotSumWX();
149 this->fTsumwx2 += src.GetTotSumWX2();
150 this->SetEntries(this->GetEntries() + src.GetEntries());
151 }
152
153 // -------------------------------------------------------------------------------------------------------------------
154 //
155 template<class RootHistogram>
156 template<class QaHistogram>
158 {
159 // Set individual bin properties
160 if constexpr (std::is_same_v<RootHistogram, TProfile> || std::is_same_v<RootHistogram, TProfile2D>) {
161 this->Sumw2();
162 auto SetBin = [&](int iBinRoot, const auto& binQa) {
163 this->fArray[iBinRoot] = binQa.GetSumWV();
164 this->fBinEntries.fArray[iBinRoot] = binQa.GetSumW();
165 this->fSumw2.fArray[iBinRoot] = binQa.GetSumWV2();
166 this->fBinSumw2.fArray[iBinRoot] = binQa.GetSumW2();
167 };
168 if constexpr (std::is_same_v<RootHistogram, TProfile>) {
169 for (int iBinX = 0; iBinX <= this->GetNbinsX() + 1; ++iBinX) {
170 int iBin = this->GetBin(iBinX);
171 auto bin = hist.GetBinAccumulator(iBinX);
172 SetBin(iBin, bin);
173 this->fTsumwy += bin.GetSumWV();
174 this->fTsumwy2 += bin.GetSumWV2();
175 }
176 }
177 else if constexpr (std::is_same_v<RootHistogram, TProfile2D>) {
178 for (int iBinX = 0; iBinX <= this->GetNbinsX() + 1; ++iBinX) {
179 for (int iBinY = 0; iBinY <= this->GetNbinsY() + 1; ++iBinY) {
180 int iBin = this->GetBin(iBinX, iBinY);
181 auto bin = hist.GetBinAccumulator(iBinX, iBinY);
182 SetBin(iBin, bin);
183 this->fTsumwz += bin.GetSumWV();
184 this->fTsumwz2 += bin.GetSumWV2();
185 }
186 }
187 }
188 }
189 else if constexpr (std::is_same_v<RootHistogram, TH1D> || std::is_same_v<RootHistogram, TH2D>) {
190 if (hist.GetTotSumW() != hist.GetTotSumW2()) { // some of weights were not equal to 1.
191 if (!this->fSumw2.fN) {
192 this->Sumw2();
193 }
194 }
195 auto SetBin = [&](int iBinRoot, const auto& binQa) {
196 this->fArray[iBinRoot] = binQa.value();
197 if (this->fSumw2.fN) {
198 this->fSumw2.fArray[iBinRoot] = binQa.variance();
199 }
200 };
201 if constexpr (std::is_same_v<RootHistogram, TH1D>) {
202 for (int iBinX = 0; iBinX <= this->GetNbinsX() + 1; ++iBinX) {
203 SetBin(this->GetBin(iBinX), hist.GetBinAccumulator(iBinX));
204 }
205 }
206 else if constexpr (std::is_same_v<RootHistogram, TH2D>) {
207 for (int iBinX = 0; iBinX <= this->GetNbinsX() + 1; ++iBinX) {
208 for (int iBinY = 0; iBinY <= this->GetNbinsY() + 1; ++iBinY) {
209 SetBin(this->GetBin(iBinX, iBinY), hist.GetBinAccumulator(iBinX, iBinY));
210 }
211 }
212 }
213 }
214
215 // Set total sums
216 if constexpr (std::is_base_of_v<TH2D, RootHistogram>) {
217 this->fTsumwy = hist.GetTotSumWY();
218 this->fTsumwy2 = hist.GetTotSumWY2();
219 this->fTsumwxy = hist.GetTotSumWXY();
220 }
221 this->fTsumw = hist.GetTotSumW();
222 this->fTsumw2 = hist.GetTotSumW2();
223 this->fTsumwx = hist.GetTotSumWX();
224 this->fTsumwx2 = hist.GetTotSumWX2();
225 this->SetEntries(hist.GetEntries());
226 }
227} // namespace cbm::qa
ROOT-free implementation of a histogram.
1D-histogram
Definition Histogram.h:442
2D-histogram
Definition Histogram.h:511
1D-histogram
Definition Histogram.h:442
2D-histogram
Definition Histogram.h:511
A collection of tools for online QA object conversions.
static void AddSlice(const H1D &src, double value, TH2D *dst)
Fills a slice of a histogram of a higher dimension for a given value (....)
static bool ConvertOutput(const std::string &inFilename, std::string outFilename="")
Converts histograms from a .qa.out file from the online binary to the ROOT format and stores them int...
static TH1D * ROOTHistogram(const H1D &hist)
Converts histogram H1D to ROOT histogram TH1D.
void SetFromQaHistogram(const QaHistogram &histo)
Sets fields from qa histogram.
void AddSliceFromQaHistogram(const SourceQaHistogram &histo, double val)
Sets slice from the lower-dimension histogram.