18CountHelper::CountHelper(
const std::shared_ptr<ULong64_t> &resultCount,
const unsigned int nSlots)
19 : fResultCount(resultCount), fCounts(nSlots, 0)
23void CountHelper::Exec(
unsigned int slot)
28void CountHelper::Finalize()
31 for (
auto &
c : fCounts) {
36ULong64_t &CountHelper::PartialUpdate(
unsigned int slot)
41void FillHelper::UpdateMinMax(
unsigned int slot,
double v)
43 auto &thisMin = fMin[slot * CacheLineStep<BufEl_t>()];
44 auto &thisMax = fMax[slot * CacheLineStep<BufEl_t>()];
45 thisMin = std::min(thisMin,
v);
46 thisMax = std::max(thisMax,
v);
49FillHelper::FillHelper(
const std::shared_ptr<Hist_t> &
h,
const unsigned int nSlots)
50 : fResultHist(
h), fNSlots(nSlots), fBufSize(fgTotalBufSize / nSlots), fPartialHists(fNSlots),
51 fMin(nSlots *
CacheLineStep<BufEl_t>(), std::numeric_limits<BufEl_t>::max()),
52 fMax(nSlots *
CacheLineStep<BufEl_t>(), std::numeric_limits<BufEl_t>::lowest())
54 fBuffers.reserve(fNSlots);
55 fWBuffers.reserve(fNSlots);
56 for (
unsigned int i = 0; i < fNSlots; ++i) {
59 fBuffers.emplace_back(
v);
60 fWBuffers.emplace_back(
v);
64void FillHelper::Exec(
unsigned int slot,
double v)
66 UpdateMinMax(slot,
v);
67 fBuffers[slot].emplace_back(
v);
70void FillHelper::Exec(
unsigned int slot,
double v,
double w)
72 UpdateMinMax(slot,
v);
73 fBuffers[slot].emplace_back(
v);
74 fWBuffers[slot].emplace_back(w);
77Hist_t &FillHelper::PartialUpdate(
unsigned int slot)
79 auto &partialHist = fPartialHists[slot];
82 partialHist = std::make_unique<Hist_t>(*fResultHist);
83 auto weights = fWBuffers[slot].empty() ? nullptr : fWBuffers[slot].data();
84 partialHist->FillN(fBuffers[slot].
size(), fBuffers[slot].data(), weights);
88void FillHelper::Finalize()
90 for (
unsigned int i = 0; i < fNSlots; ++i) {
91 if (!fWBuffers[i].empty() && fBuffers[i].
size() != fWBuffers[i].
size()) {
92 throw std::runtime_error(
"Cannot fill weighted histogram with values in containers of different sizes.");
96 BufEl_t globalMin = *std::min_element(fMin.begin(), fMin.end());
97 BufEl_t globalMax = *std::max_element(fMax.begin(), fMax.end());
99 if (fResultHist->CanExtendAllAxes() && globalMin != std::numeric_limits<BufEl_t>::max() &&
100 globalMax != std::numeric_limits<BufEl_t>::lowest()) {
101 fResultHist->SetBins(fResultHist->GetNbinsX(), globalMin, globalMax);
104 for (
unsigned int i = 0; i < fNSlots; ++i) {
105 auto weights = fWBuffers[i].empty() ? nullptr : fWBuffers[i].data();
106 fResultHist->FillN(fBuffers[i].
size(), fBuffers[i].data(), weights);
110template void FillHelper::Exec(
unsigned int,
const std::vector<float> &);
111template void FillHelper::Exec(
unsigned int,
const std::vector<double> &);
112template void FillHelper::Exec(
unsigned int,
const std::vector<char> &);
113template void FillHelper::Exec(
unsigned int,
const std::vector<int> &);
114template void FillHelper::Exec(
unsigned int,
const std::vector<unsigned int> &);
115template void FillHelper::Exec(
unsigned int,
const std::vector<float> &,
const std::vector<float> &);
116template void FillHelper::Exec(
unsigned int,
const std::vector<double> &,
const std::vector<double> &);
117template void FillHelper::Exec(
unsigned int,
const std::vector<char> &,
const std::vector<char> &);
118template void FillHelper::Exec(
unsigned int,
const std::vector<int> &,
const std::vector<int> &);
119template void FillHelper::Exec(
unsigned int,
const std::vector<unsigned int> &,
const std::vector<unsigned int> &);
134MeanHelper::MeanHelper(
const std::shared_ptr<double> &meanVPtr,
const unsigned int nSlots)
135 : fResultMean(meanVPtr), fCounts(nSlots, 0), fSums(nSlots, 0), fPartialMeans(nSlots)
139void MeanHelper::Exec(
unsigned int slot,
double v)
145void MeanHelper::Finalize()
147 double sumOfSums = 0;
148 for (
auto &s : fSums)
151 for (
auto &
c : fCounts)
153 *fResultMean = sumOfSums / (sumOfCounts > 0 ? sumOfCounts : 1);
156double &MeanHelper::PartialUpdate(
unsigned int slot)
158 fPartialMeans[slot] = fSums[slot] / fCounts[slot];
159 return fPartialMeans[slot];
162template void MeanHelper::Exec(
unsigned int,
const std::vector<float> &);
163template void MeanHelper::Exec(
unsigned int,
const std::vector<double> &);
164template void MeanHelper::Exec(
unsigned int,
const std::vector<char> &);
165template void MeanHelper::Exec(
unsigned int,
const std::vector<int> &);
166template void MeanHelper::Exec(
unsigned int,
const std::vector<unsigned int> &);
168StdDevHelper::StdDevHelper(
const std::shared_ptr<double> &meanVPtr,
const unsigned int nSlots)
169 : fNSlots(nSlots), fResultStdDev(meanVPtr), fCounts(nSlots, 0), fMeans(nSlots, 0), fDistancesfromMean(nSlots, 0)
173void StdDevHelper::Exec(
unsigned int slot,
double v)
176 auto count = ++fCounts[slot];
177 auto delta =
v - fMeans[slot];
178 auto mean = fMeans[slot] + delta / count;
179 auto delta2 =
v - mean;
180 auto distance = fDistancesfromMean[slot] + delta * delta2;
182 fCounts[slot] = count;
184 fDistancesfromMean[slot] = distance;
187void StdDevHelper::Finalize()
190 double totalElements = 0;
191 for (
auto c : fCounts) {
194 if (totalElements == 0 || totalElements == 1) {
200 double overallMean = 0;
201 for (
unsigned int i = 0; i < fNSlots; ++i) {
202 overallMean += fCounts[i] * fMeans[i];
204 overallMean = overallMean / totalElements;
207 for (
unsigned int i = 0; i < fNSlots; ++i) {
208 if (fCounts[i] == 0) {
211 auto setVariance = fDistancesfromMean[i] / (fCounts[i]);
212 variance += (fCounts[i]) * (setVariance + std::pow((fMeans[i] - overallMean), 2));
215 variance = variance / (totalElements - 1);
216 *fResultStdDev = std::sqrt(variance);
219template void StdDevHelper::Exec(
unsigned int,
const std::vector<float> &);
220template void StdDevHelper::Exec(
unsigned int,
const std::vector<double> &);
221template void StdDevHelper::Exec(
unsigned int,
const std::vector<char> &);
222template void StdDevHelper::Exec(
unsigned int,
const std::vector<int> &);
223template void StdDevHelper::Exec(
unsigned int,
const std::vector<unsigned int> &);
227template class TakeHelper<bool, bool, std::vector<bool>>;
228template class TakeHelper<unsigned int, unsigned int, std::vector<unsigned int>>;
229template class TakeHelper<unsigned long, unsigned long, std::vector<unsigned long>>;
230template class TakeHelper<unsigned long long, unsigned long long, std::vector<unsigned long long>>;
231template class TakeHelper<int, int, std::vector<int>>;
232template class TakeHelper<long, long, std::vector<long>>;
233template class TakeHelper<long long, long long, std::vector<long long>>;
234template class TakeHelper<float, float, std::vector<float>>;
235template class TakeHelper<double, double, std::vector<double>>;
242 if (fileMode !=
"update")
246 std::unique_ptr<TFile> outFile{
TFile::Open(fileName.c_str(),
"update")};
247 if (!outFile || outFile->IsZombie())
248 throw std::invalid_argument(
"Snapshot: cannot open file \"" + fileName +
"\" in update mode");
250 TObject *outTree = outFile->Get(treeName.c_str());
251 if (outTree ==
nullptr)
257 static_cast<TTree *
>(outTree)->Delete(
"all");
259 outFile->
Delete(treeName.c_str());
262 const std::string msg =
"Snapshot: tree \"" + treeName +
"\" already present in file \"" + fileName +
263 "\". If you want to delete the original tree and write another, please set "
264 "RSnapshotOptions::fOverwriteIfExists to true.";
265 throw std::invalid_argument(msg);
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
unsigned long long ULong64_t
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.
virtual void Delete(Option_t *option="")
Delete this tree from memory or/and disk.
void ValidateSnapshotOutput(const RSnapshotOptions &opts, const std::string &treeName, const std::string &fileName)
constexpr std::size_t CacheLineStep()
Stepping through CacheLineStep<T> values in a vector<T> brings you to a new cache line.
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
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.