FairShip
Loading...
Searching...
No Matches
test_event_histogram.cxx File Reference
#include <TFile.h>
#include <TFileMerger.h>
#include <TH1D.h>
#include <TRandom3.h>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <vector>
#include "EventHistogram.h"
Include dependency graph for test_event_histogram.cxx:

Go to the source code of this file.

Functions

int main ()

Function Documentation

◆ main()

int main ( )

Definition at line 80 of file test_event_histogram.cxx.

80 {
81 const int nbins = 10;
82 const Sample a = MakeSample(1, 4000);
83 const Sample b = MakeSample(2, 3000);
84
85 // Expected per-event sums, by hand.
86 TH1D binning("binning", "", nbins, 0., 10.);
87 std::vector<double> sumT(nbins + 2, 0.), sumT2(nbins + 2, 0.),
88 sumW2(nbins + 2, 0.);
89 for (const auto& ev : a) {
90 std::vector<double> t(nbins + 2, 0.);
91 for (const auto& [x, w] : ev) {
92 const int bin = binning.GetXaxis()->FindFixBin(x);
93 t[bin] += w;
94 sumW2[bin] += w * w;
95 }
96 for (int i = 0; i < nbins + 2; ++i) {
97 sumT[i] += t[i];
98 sumT2[i] += t[i] * t[i];
99 }
100 }
101
102 EventHistogram ha("x", "test;x", nbins, 0., 10., kTRUE, 200);
103 FillSample(ha, a, 1);
104 Check(ha.GetNEvents() == 4000, "event count", ha.GetNEvents(), 4000);
105
106 std::unique_ptr<TH1D> res(ha.MakeResult());
107 std::unique_ptr<TH1D> naive(ha.MakeNaive());
108 const double n = 4000.;
109 double maxRatio = 0.;
110 for (int i = 1; i <= nbins; ++i) {
111 const double mean = sumT[i] / n;
112 const double err = std::sqrt((sumT2[i] / n - mean * mean) / (n - 1));
113 CheckClose(res->GetBinContent(i), mean, 1e-12, "content");
114 CheckClose(res->GetBinError(i), err, 1e-9, "per-event error");
115 CheckClose(naive->GetBinError(i), std::sqrt(sumW2[i]) / n, 1e-9,
116 "naive error");
117 maxRatio = std::max(maxRatio, res->GetBinError(i) / naive->GetBinError(i));
118 }
119 // 20 clones per event, mostly in one or two bins: the naive error is far
120 // too small.
121 Check(maxRatio > 2.5, "per-event / naive error ratio", maxRatio, 2.5);
122
123 // Covariance: diagonal equals the squared errors, neighbours are positive.
124 const TMatrixDSym cov = ha.GetCovariance();
125 for (int i = 1; i <= nbins; ++i) {
126 CheckClose(cov(i - 1, i - 1), std::pow(res->GetBinError(i), 2), 1e-9,
127 "covariance diagonal");
128 }
129 Check(cov(4, 5) > 0., "neighbour covariance positive", cov(4, 5), 0.);
130
131 // Bootstrap reproduces the per-event errors within its own precision.
132 const std::vector<double> boot = ha.GetBootstrapErrors();
133 for (int i = 1; i <= nbins; ++i) {
134 CheckClose(boot[i - 1], res->GetBinError(i), 0.3, "bootstrap error");
135 }
136
137 // Replica weights are Poisson(1) and depend only on key and replica.
138 double s1 = 0., s2 = 0.;
139 const int nk = 200000;
140 for (int k = 0; k < nk; ++k) {
141 const double r = EventHistogram::ReplicaWeight(k, 3);
142 s1 += r;
143 s2 += r * r;
144 }
145 CheckClose(s1 / nk, 1., 0.01, "replica weight mean");
146 CheckClose(s2 / nk - std::pow(s1 / nk, 2), 1., 0.02, "replica weight var");
147 Require(EventHistogram::ReplicaWeight(42, 7) ==
149 "replica weight deterministic");
150
151 // Add() and a file merge (as hadd does) give the same as one accumulator.
152 EventHistogram hb("x", "test;x", nbins, 0., 10., kTRUE, 200);
153 FillSample(hb, b, 2);
154 EventHistogram hab("x", "test;x", nbins, 0., 10., kTRUE, 200);
155 FillSample(hab, a, 1);
156 FillSample(hab, b, 2);
157 {
158 TFile fa("test_event_histogram_a.root", "RECREATE");
159 ha.Write(&fa);
160 TFile fb("test_event_histogram_b.root", "RECREATE");
161 hb.Write(&fb);
162 }
163 ha.Add(hb);
164 {
165 TFileMerger merger(kFALSE);
166 merger.OutputFile("test_event_histogram_ab.root", "RECREATE");
167 merger.AddFile("test_event_histogram_a.root");
168 merger.AddFile("test_event_histogram_b.root");
169 Require(merger.Merge(), "file merge");
170 }
171 TFile fab("test_event_histogram_ab.root");
172 std::unique_ptr<EventHistogram> merged = EventHistogram::Read(&fab, "x");
173 Require(merged != nullptr, "read merged");
174 if (merged) {
175 Check(merged->GetNEvents() == 7000, "merged event count",
176 merged->GetNEvents(), 7000);
177 std::unique_ptr<TH1D> rAdd(ha.MakeResult("rAdd"));
178 std::unique_ptr<TH1D> rAll(hab.MakeResult("rAll"));
179 std::unique_ptr<TH1D> rMerged(merged->MakeResult("rMerged"));
180 const std::vector<double> bAll = hab.GetBootstrapErrors();
181 const std::vector<double> bMerged = merged->GetBootstrapErrors();
182 for (int i = 1; i <= nbins; ++i) {
183 CheckClose(rAdd->GetBinError(i), rAll->GetBinError(i), 1e-9,
184 "Add() error");
185 CheckClose(rMerged->GetBinContent(i), rAll->GetBinContent(i), 1e-9,
186 "merged content");
187 CheckClose(rMerged->GetBinError(i), rAll->GetBinError(i), 1e-9,
188 "merged error");
189 CheckClose(bMerged[i - 1], bAll[i - 1], 1e-9, "merged bootstrap");
190 }
191 }
192 fab.Close();
193
194 // Incompatible accumulators are refused and leave the target unchanged.
195 const Long64_t nBefore = ha.GetNEvents();
196 const std::vector<double> otherEdges = {0., 1., 2., 3., 4., 5.,
197 6., 7., 8., 9., 12.};
198 EventHistogram hEdges("x", "test;x", nbins, otherEdges.data(), kTRUE, 200);
199 EventHistogram hReplicas("x", "test;x", nbins, 0., 10., kTRUE, 100);
200 EventHistogram hNoCov("x", "test;x", nbins, 0., 10., kFALSE, 200);
201 for (EventHistogram* other : {&hEdges, &hReplicas, &hNoCov}) {
202 FillSample(*other, b, 2);
203 bool refused = false;
204 try {
205 ha.Add(*other);
206 } catch (const std::invalid_argument&) {
207 refused = true;
208 }
209 Require(refused, "incompatible merge throws");
210 }
211 Check(ha.GetNEvents() == nBefore, "refused merge", ha.GetNEvents(), nBefore);
212
213 // An open event is an error.
214 hNoCov.Fill(1.);
215 bool open = false;
216 try {
217 std::unique_ptr<TH1D> r(hNoCov.MakeResult());
218 } catch (const std::logic_error&) {
219 open = true;
220 }
221 Require(open, "open event throws");
222 hNoCov.EndEvent();
223
224 // Files with different settings merged by hadd are refused on reading.
225 {
226 TFile fc("test_event_histogram_c.root", "RECREATE");
227 hNoCov.Write(&fc);
228 }
229 {
230 TFileMerger merger(kFALSE);
231 merger.OutputFile("test_event_histogram_ac.root", "RECREATE");
232 merger.AddFile("test_event_histogram_a.root");
233 merger.AddFile("test_event_histogram_c.root");
234 Require(merger.Merge(), "mixed file merge");
235 }
236 TFile fac("test_event_histogram_ac.root");
237 Require(EventHistogram::Read(&fac, "x") == nullptr, "mixed merge refused");
238 fac.Close();
239
240 for (const char* f : {"a", "b", "ab", "c", "ac"}) {
241 std::remove((std::string("test_event_histogram_") + f + ".root").c_str());
242 }
243
244 if (failures) {
245 std::cerr << failures << " check(s) failed" << std::endl;
246 return 1;
247 }
248 return 0;
249}
static std::unique_ptr< EventHistogram > Read(TDirectory *dir, const char *name)
static Int_t ReplicaWeight(ULong64_t key, Int_t replica)
int i
Definition ShipAna.py:108