12#include "FairLogger.h"
15#include "TParameter.h"
19std::vector<Double_t> AxisEdges(
const TAxis* axis) {
20 std::vector<Double_t> edges;
21 for (Int_t i = 1;
i <= axis->GetNbins(); ++
i) {
22 edges.push_back(axis->GetBinLowEdge(i));
24 edges.push_back(axis->GetBinUpEdge(axis->GetNbins()));
28std::string Suffixed(
const char* name,
const char* suffix) {
29 return std::string(name) +
suffix;
33std::unique_ptr<T> DetachedClone(
const T* h) {
34 std::unique_ptr<T> clone(
static_cast<T*
>(
h->Clone()));
35 clone->SetDirectory(
nullptr);
39ULong64_t SplitMix64(ULong64_t x) {
40 x += 0x9E3779B97F4A7C15ULL;
41 x = (
x ^ (
x >> 30U)) * 0xBF58476D1CE4E5B9ULL;
42 x = (
x ^ (
x >> 27U)) * 0x94D049BB133111EBULL;
43 return x ^ (
x >> 31U);
49 Double_t xlow, Double_t xup, Bool_t covariance,
51 :
fSum(new TH1D(name, title, nbins, xlow, xup)) {
52 Init(covariance, nReplicas);
56 const Double_t* edges, Bool_t covariance,
58 :
fSum(new TH1D(name, title, nbins, edges)) {
59 Init(covariance, nReplicas);
65 fSum->SetDirectory(
nullptr);
67 const std::string name =
fSum->GetName();
68 fNaive.reset(
dynamic_cast<TH1D*
>(
69 fSum->Clone(Suffixed(name.c_str(),
"_naive").c_str())));
70 fNaive->SetDirectory(
nullptr);
71 const Int_t nbins =
fSum->GetNbinsX();
72 const std::vector<Double_t> edges = AxisEdges(
fSum->GetXaxis());
74 fCov = std::make_unique<TH2D>(Suffixed(name.c_str(),
"_cov").c_str(),
75 "sum over events of t_a t_b", nbins,
76 edges.data(), nbins, edges.data());
77 fCov->SetDirectory(
nullptr);
80 fBoot = std::make_unique<TH2D>(Suffixed(name.c_str(),
"_boot").c_str(),
81 "bootstrap replicas;;replica", nbins,
82 edges.data(), nReplicas, 0., nReplicas);
83 fBoot->SetDirectory(
nullptr);
94 const Int_t bin =
fSum->GetXaxis()->FindFixBin(x);
100 fNaive->AddBinContent(bin, w);
101 (*
fNaive->GetSumw2())[bin] += w * w;
105 TArrayD& sumw2 = *
fSum->GetSumw2();
106 const Int_t nbins =
fSum->GetNbinsX();
111 fSum->AddBinContent(b, t);
115 const auto begin =
fTouched.begin();
118 [nbins](Int_t b) { return b >= 1 && b <= nbins; });
120 for (
auto a = begin; a != end; ++a) {
121 for (
auto b = begin; b != end; ++b) {
126 if (
fBoot && begin != end) {
127 for (Int_t r = 0; r <
fBoot->GetNbinsY(); ++r) {
129 if (k == 0)
continue;
130 for (
auto b = begin; b != end; ++b) {
148 const char* problem =
nullptr;
149 if (AxisEdges(other.
fSum->GetXaxis()) != AxisEdges(
fSum->GetXaxis())) {
152 problem =
"covariance";
155 problem =
"number of bootstrap replicas";
158 throw std::invalid_argument(std::string(
"EventHistogram::Add: ") + problem +
159 " of " + other.
fSum->GetName() +
160 " differs from " +
fSum->GetName());
171 throw std::logic_error(std::string(
"EventHistogram::") + where +
" on " +
173 " with an open event; call EndEvent() first");
178 const std::string newName =
179 name ? std::string(name) : Suffixed(
fSum->GetName(), suffix);
180 auto* h =
dynamic_cast<TH1D*
>(
fSum->Clone(newName.c_str()));
181 h->SetDirectory(
nullptr);
191 const TArrayD& sumw2 = *
fSum->GetSumw2();
192 for (Int_t b = 0; b <=
fSum->GetNbinsX() + 1; ++b) {
193 const Double_t mean =
fSum->GetBinContent(b) / n;
195 n > 1 ? (sumw2[b] / n - mean * mean) / (n - 1) : sumw2[b] / (n * n);
196 h->SetBinContent(b, mean);
197 h->SetBinError(b, std::sqrt(std::max(var, 0.)));
208 const TArrayD& sumw2 = *
fNaive->GetSumw2();
209 for (Int_t b = 0; b <=
fSum->GetNbinsX() + 1; ++b) {
210 h->SetBinContent(b,
fNaive->GetBinContent(b) / n);
211 h->SetBinError(b, std::sqrt(sumw2[b]) / n);
219 const TArrayD& sumw2 = *
fSum->GetSumw2();
220 for (Int_t b = 0; b <=
fSum->GetNbinsX() + 1; ++b) {
221 const Double_t s =
fSum->GetBinContent(b);
222 h->SetBinContent(b, sumw2[b] > 0 ? s * s / sumw2[b] : 0.);
229 const Int_t nbins =
fSum->GetNbinsX();
230 TMatrixDSym cov(nbins);
232 throw std::logic_error(std::string(
"EventHistogram ") +
fSum->GetName() +
233 " was created without covariance");
236 if (n < 2)
return cov;
237 for (Int_t a = 1; a <= nbins; ++a) {
238 for (Int_t b = 1; b <= nbins; ++b) {
239 const Double_t ma =
fSum->GetBinContent(a) / n;
240 const Double_t mb =
fSum->GetBinContent(b) / n;
241 cov(a - 1, b - 1) = (
fCov->GetBinContent(a, b) / n - ma * mb) / (n - 1);
249 const Int_t nbins =
fSum->GetNbinsX();
250 std::vector<Double_t> errors(nbins, 0.);
252 throw std::logic_error(std::string(
"EventHistogram ") +
fSum->GetName() +
253 " has fewer than two bootstrap replicas");
256 const Int_t nrep =
fBoot->GetNbinsY();
257 for (Int_t b = 1; b <= nbins; ++b) {
258 Double_t sum = 0., sum2 = 0.;
259 for (Int_t r = 1; r <= nrep; ++r) {
264 const Double_t mean = sum / nrep;
266 std::sqrt(std::max((sum2 - nrep * mean * mean) / (nrep - 1), 0.));
276 {
static_cast<TH1*
>(
fSum.get()),
static_cast<TH1*
>(
fNaive.get()),
277 static_cast<TH1*
>(
fCov.get()),
static_cast<TH1*
>(
fBoot.get())}) {
280 dir->WriteTObject(
fSum.get(),
fSum->GetName(),
"Overwrite");
281 dir->WriteTObject(
fNaive.get(),
fNaive->GetName(),
"Overwrite");
282 if (
fCov) dir->WriteTObject(
fCov.get(),
fCov->GetName(),
"Overwrite");
283 if (
fBoot) dir->WriteTObject(
fBoot.get(),
fBoot->GetName(),
"Overwrite");
284 const std::string key = Suffixed(
fSum->GetName(),
"_nevents");
285 TParameter<Long64_t> nevents(key.c_str(),
fNEvents,
'+');
286 dir->WriteTObject(&nevents, key.c_str(),
"Overwrite");
291 auto* sum = dir->Get<TH1D>(name);
292 auto* naive = dir->Get<TH1D>(Suffixed(name,
"_naive").c_str());
294 dir->Get<TParameter<Long64_t>>(Suffixed(name,
"_nevents").c_str());
295 if (!sum || !naive || !nevents) {
296 LOG(error) <<
"EventHistogram::Read: " << name <<
" not found in "
300 auto* cov = dir->Get<TH2D>(Suffixed(name,
"_cov").c_str());
301 auto* boot = dir->Get<TH2D>(Suffixed(name,
"_boot").c_str());
302 for (
const TH1* obj :
303 {
static_cast<const TH1*
>(sum),
static_cast<const TH1*
>(naive),
304 static_cast<const TH1*
>(cov),
static_cast<const TH1*
>(boot)}) {
305 if (obj && obj->GetEntries() != nevents->GetVal()) {
306 LOG(error) <<
"EventHistogram::Read: " << obj->GetName() <<
" holds "
307 << obj->GetEntries() <<
" events, " << nevents->GetName()
308 <<
" " << nevents->GetVal()
309 <<
"; were files with different settings merged?";
314 h->fSum = DetachedClone(sum);
315 h->fNaive = DetachedClone(naive);
316 if (cov) h->fCov = DetachedClone(cov);
317 if (boot) h->fBoot = DetachedClone(boot);
318 h->fNEvents = nevents->GetVal();
319 h->InitEventBuffers();
324 const ULong64_t x = SplitMix64(
325 key ^ (
static_cast<ULong64_t
>(replica + 1) * 0xD1B54A32D192ED03ULL));
326 const Double_t u =
static_cast<Double_t
>(x >> 11U) * 0x1.0p-53;
328 static const Double_t p0 = std::exp(-1.);
332 while (u > cdf && k < 20) {
std::vector< bool > fIsTouched
per bin: listed in fTouched
TH1D * MakeNeff(const char *name=nullptr) const
void Fill(Double_t x, Double_t w=1.)
Add one branch of the current event.
std::unique_ptr< TH2D > fCov
optional: sum t_a t_b
TH1D * MakePerEvent(const char *name, const char *suffix) const
void Init(Bool_t covariance, Int_t nReplicas)
static std::unique_ptr< EventHistogram > Read(TDirectory *dir, const char *name)
static Int_t ReplicaWeight(ULong64_t key, Int_t replica)
TH1D * MakeResult(const char *name=nullptr) const
Contents per generated event with event-level errors.
std::unique_ptr< TH1D > fSum
contents sum t, sumw2 sum t^2
void AddEmptyEvents(Long64_t n)
Generated events that left no entry still count in the normalisation.
std::unique_ptr< TH2D > fBoot
optional: (bin, replica) sum r t
EventHistogram(const char *name, const char *title, Int_t nbins, Double_t xlow, Double_t xup, Bool_t covariance=kFALSE, Int_t nReplicas=0)
void EndEvent(ULong64_t eventKey=0)
void Add(const EventHistogram &other)
std::vector< Double_t > fCurrent
t_b of the open event, incl. flows
TH1D * MakeNaive(const char *name=nullptr) const
Same contents with sqrt(sum w^2)/N errors, for comparison only.
std::unique_ptr< TH1D > fNaive
contents sum w, sumw2 sum w^2
Bool_t HasOpenEvent() const
void CheckClosed(const char *where) const
TMatrixDSym GetCovariance() const
std::vector< Double_t > GetBootstrapErrors() const
std::vector< Int_t > fTouched
bins filled in the open event
void Write(TDirectory *dir=gDirectory) const