17CountHelper::CountHelper(
const std::shared_ptr<ULong64_t> &resultCount,
const unsigned int nSlots)
18 : fResultCount(resultCount), fCounts(nSlots, 0)
27void CountHelper::Finalize()
30 for (
auto &
c : fCounts) {
35ULong64_t &CountHelper::PartialUpdate(
unsigned int slot)
40void BufferedFillHelper::UpdateMinMax(
unsigned int slot,
double v)
42 auto &thisMin = fMin[slot];
43 auto &thisMax = fMax[slot];
44 thisMin = std::min(thisMin,
v);
45 thisMax = std::max(thisMax,
v);
48BufferedFillHelper::BufferedFillHelper(
const std::shared_ptr<Hist_t> &
h,
const unsigned int nSlots)
49 : fResultHist(
h), fNSlots(nSlots), fBufSize(fgTotalBufSize / nSlots), fPartialHists(fNSlots),
50 fMin(nSlots, std::numeric_limits<BufEl_t>::max()), fMax(nSlots, std::numeric_limits<BufEl_t>::lowest())
52 fBuffers.reserve(fNSlots);
53 fWBuffers.reserve(fNSlots);
54 for (
unsigned int i = 0; i < fNSlots; ++i) {
57 fBuffers.emplace_back(
v);
58 fWBuffers.emplace_back(
v);
64 UpdateMinMax(slot,
v);
65 fBuffers[slot].emplace_back(
v);
70 UpdateMinMax(slot,
v);
71 fBuffers[slot].emplace_back(
v);
72 fWBuffers[slot].emplace_back(
w);
75Hist_t &BufferedFillHelper::PartialUpdate(
unsigned int slot)
77 auto &partialHist = fPartialHists[slot];
80 partialHist = std::make_unique<Hist_t>(*fResultHist);
81 auto weights = fWBuffers[slot].empty() ? nullptr : fWBuffers[slot].data();
82 partialHist->FillN(fBuffers[slot].
size(), fBuffers[slot].
data(), weights);
86void BufferedFillHelper::Finalize()
88 for (
unsigned int i = 0; i < fNSlots; ++i) {
89 if (!fWBuffers[i].empty() && fBuffers[i].
size() != fWBuffers[i].
size()) {
90 throw std::runtime_error(
"Cannot fill weighted histogram with values in containers of different sizes.");
94 BufEl_t globalMin = *std::min_element(fMin.begin(), fMin.end());
95 BufEl_t globalMax = *std::max_element(fMax.begin(), fMax.end());
97 if (fResultHist->CanExtendAllAxes() && globalMin != std::numeric_limits<BufEl_t>::max() &&
98 globalMax != std::numeric_limits<BufEl_t>::lowest()) {
99 fResultHist->SetBins(fResultHist->GetNbinsX(), globalMin, globalMax);
102 for (
unsigned int i = 0; i < fNSlots; ++i) {
103 auto weights = fWBuffers[i].empty() ? nullptr : fWBuffers[i].data();
104 fResultHist->FillN(fBuffers[i].
size(), fBuffers[i].
data(), weights);
133MeanHelper::MeanHelper(
const std::shared_ptr<double> &meanVPtr,
const unsigned int nSlots)
134 : fResultMean(meanVPtr), fCounts(nSlots, 0), fSums(nSlots, 0), fPartialMeans(nSlots)
144void MeanHelper::Finalize()
146 double sumOfSums = 0;
147 for (
auto &
s : fSums)
150 for (
auto &
c : fCounts)
152 *fResultMean = sumOfSums / (sumOfCounts > 0 ? sumOfCounts : 1);
155double &MeanHelper::PartialUpdate(
unsigned int slot)
157 fPartialMeans[slot] = fSums[slot] / fCounts[slot];
158 return fPartialMeans[slot];
165template void MeanHelper::Exec(
unsigned int,
const std::vector<unsigned int> &);
167StdDevHelper::StdDevHelper(
const std::shared_ptr<double> &meanVPtr,
const unsigned int nSlots)
168 : fNSlots(nSlots), fResultStdDev(meanVPtr), fCounts(nSlots, 0), fMeans(nSlots, 0), fDistancesfromMean(nSlots, 0)
175 auto count = ++fCounts[slot];
176 auto delta =
v - fMeans[slot];
177 auto mean = fMeans[slot] + delta / count;
178 auto delta2 =
v - mean;
179 auto distance = fDistancesfromMean[slot] + delta * delta2;
181 fCounts[slot] = count;
183 fDistancesfromMean[slot] = distance;
186void StdDevHelper::Finalize()
189 double totalElements = 0;
190 for (
auto c : fCounts) {
193 if (totalElements == 0 || totalElements == 1) {
199 double overallMean = 0;
200 for (
unsigned int i = 0; i < fNSlots; ++i) {
201 overallMean += fCounts[i] * fMeans[i];
203 overallMean = overallMean / totalElements;
206 for (
unsigned int i = 0; i < fNSlots; ++i) {
207 if (fCounts[i] == 0) {
210 auto setVariance = fDistancesfromMean[i] / (fCounts[i]);
211 variance += (fCounts[i]) * (setVariance +
std::pow((fMeans[i] - overallMean), 2));
214 variance = variance / (totalElements - 1);
226template class TakeHelper<bool, bool, std::vector<bool>>;
227template class TakeHelper<unsigned int, unsigned int, std::vector<unsigned int>>;
228template class TakeHelper<unsigned long, unsigned long, std::vector<unsigned long>>;
229template class TakeHelper<unsigned long long, unsigned long long, std::vector<unsigned long long>>;
230template class TakeHelper<int, int, std::vector<int>>;
231template class TakeHelper<long, long, std::vector<long>>;
232template class TakeHelper<long long, long long, std::vector<long long>>;
233template class TakeHelper<float, float, std::vector<float>>;
234template class TakeHelper<double, double, std::vector<double>>;
241 if (fileMode !=
"update")
245 std::unique_ptr<TFile> outFile{
TFile::Open(fileName.c_str(),
"update")};
246 if (!outFile || outFile->IsZombie())
247 throw std::invalid_argument(
"Snapshot: cannot open file \"" + fileName +
"\" in update mode");
249 TObject *outTree = outFile->Get(treeName.c_str());
250 if (outTree ==
nullptr)
256 static_cast<TTree *
>(outTree)->Delete(
"all");
258 outFile->
Delete(treeName.c_str());
261 const std::string msg =
"Snapshot: tree \"" + treeName +
"\" already present in file \"" + fileName +
262 "\". If you want to delete the original tree and write another, please set "
263 "RSnapshotOptions::fOverwriteIfExists to true.";
264 throw std::invalid_argument(msg);
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
unsigned long long ULong64_t
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
Mother of all ROOT objects.
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
void ToLower()
Change string to lower-case.
A TTree represents a columnar dataset.
void Delete(Option_t *option="") override
Delete this tree from memory or/and disk.
RVec< PromoteTypes< T0, T1 > > pow(const T0 &x, const RVec< T1 > &v)
CPYCPPYY_EXTERN bool Exec(const std::string &cmd)
void ValidateSnapshotOutput(const RSnapshotOptions &opts, const std::string &treeName, const std::string &fileName)
VecExpr< UnaryOp< Sqrt< T >, VecExpr< A, T, D >, T >, T, D > sqrt(const VecExpr< A, T, D > &rhs)
This file contains a specialised ROOT message handler to test for diagnostic in unit tests.
static constexpr double s
A collection of options to steer the creation of the dataset on file.
std::string fMode
Mode of creation of output file.
bool fOverwriteIfExists
If fMode is "UPDATE", overwrite object in output file if it already exists.