Logo ROOT   6.10/09
Reference Guide
TDFActionHelpers.cxx
Go to the documentation of this file.
1 // Author: Enrico Guiraud, Danilo Piparo CERN 12/2016
2 
3 /*************************************************************************
4  * Copyright (C) 1995-2016, Rene Brun and Fons Rademakers. *
5  * All rights reserved. *
6  * *
7  * For the licensing terms see $ROOTSYS/LICENSE. *
8  * For the list of contributors see $ROOTSYS/README/CREDITS. *
9  *************************************************************************/
10 
12 
13 namespace ROOT {
14 namespace Internal {
15 namespace TDF {
16 
17 CountHelper::CountHelper(const std::shared_ptr<unsigned int> &resultCount, unsigned int nSlots)
18  : fResultCount(resultCount), fCounts(nSlots, 0)
19 {
20 }
21 
22 void CountHelper::Exec(unsigned int slot)
23 {
24  fCounts[slot]++;
25 }
26 
27 void CountHelper::Finalize()
28 {
29  *fResultCount = 0;
30  for (auto &c : fCounts) {
31  *fResultCount += c;
32  }
33 }
34 
35 void FillHelper::UpdateMinMax(unsigned int slot, double v)
36 {
37  auto &thisMin = fMin[slot];
38  auto &thisMax = fMax[slot];
39  thisMin = std::min(thisMin, v);
40  thisMax = std::max(thisMax, v);
41 }
42 
43 FillHelper::FillHelper(const std::shared_ptr<Hist_t> &h, unsigned int nSlots)
44  : fResultHist(h), fNSlots(nSlots), fBufSize(fgTotalBufSize / nSlots),
45  fMin(nSlots, std::numeric_limits<BufEl_t>::max()), fMax(nSlots, std::numeric_limits<BufEl_t>::lowest())
46 {
47  fBuffers.reserve(fNSlots);
48  fWBuffers.reserve(fNSlots);
49  for (unsigned int i = 0; i < fNSlots; ++i) {
50  Buf_t v;
51  v.reserve(fBufSize);
52  fBuffers.emplace_back(v);
53  fWBuffers.emplace_back(v);
54  }
55 }
56 
57 void FillHelper::Exec(unsigned int slot, double v)
58 {
59  UpdateMinMax(slot, v);
60  fBuffers[slot].emplace_back(v);
61 }
62 
63 void FillHelper::Exec(unsigned int slot, double v, double w)
64 {
65  UpdateMinMax(slot, v);
66  fBuffers[slot].emplace_back(v);
67  fWBuffers[slot].emplace_back(w);
68 }
69 
70 void FillHelper::Finalize()
71 {
72  for (unsigned int i = 0; i < fNSlots; ++i) {
73  if (!fWBuffers[i].empty() && fBuffers[i].size() != fWBuffers[i].size()) {
74  throw std::runtime_error("Cannot fill weighted histogram with values in containers of different sizes.");
75  }
76  }
77 
78  BufEl_t globalMin = *std::min_element(fMin.begin(), fMin.end());
79  BufEl_t globalMax = *std::max_element(fMax.begin(), fMax.end());
80 
81  if (fResultHist->CanExtendAllAxes() && globalMin != std::numeric_limits<BufEl_t>::max() &&
82  globalMax != std::numeric_limits<BufEl_t>::lowest()) {
83  fResultHist->SetBins(fResultHist->GetNbinsX(), globalMin, globalMax);
84  }
85 
86  for (unsigned int i = 0; i < fNSlots; ++i) {
87  // TODO: Here one really needs to fix FillN!
88  if (fWBuffers[i].empty()) {
89  fWBuffers[i].resize(fBuffers[i].size(), 1.);
90  }
91  fResultHist->FillN(fBuffers[i].size(), fBuffers[i].data(), fWBuffers[i].data());
92  }
93 }
94 
95 template void FillHelper::Exec(unsigned int, const std::vector<float> &);
96 template void FillHelper::Exec(unsigned int, const std::vector<double> &);
97 template void FillHelper::Exec(unsigned int, const std::vector<char> &);
98 template void FillHelper::Exec(unsigned int, const std::vector<int> &);
99 template void FillHelper::Exec(unsigned int, const std::vector<unsigned int> &);
100 template void FillHelper::Exec(unsigned int, const std::vector<float> &, const std::vector<float> &);
101 template void FillHelper::Exec(unsigned int, const std::vector<double> &, const std::vector<double> &);
102 template void FillHelper::Exec(unsigned int, const std::vector<char> &, const std::vector<char> &);
103 template void FillHelper::Exec(unsigned int, const std::vector<int> &, const std::vector<int> &);
104 template void FillHelper::Exec(unsigned int, const std::vector<unsigned int> &, const std::vector<unsigned int> &);
105 
106 MinHelper::MinHelper(const std::shared_ptr<double> &minVPtr, unsigned int nSlots)
107  : fResultMin(minVPtr), fMins(nSlots, std::numeric_limits<double>::max())
108 {
109 }
110 
111 void MinHelper::Exec(unsigned int slot, double v)
112 {
113  fMins[slot] = std::min(v, fMins[slot]);
114 }
115 
116 void MinHelper::Finalize()
117 {
118  *fResultMin = std::numeric_limits<double>::max();
119  for (auto &m : fMins) *fResultMin = std::min(m, *fResultMin);
120 }
121 
122 template void MinHelper::Exec(unsigned int, const std::vector<float> &);
123 template void MinHelper::Exec(unsigned int, const std::vector<double> &);
124 template void MinHelper::Exec(unsigned int, const std::vector<char> &);
125 template void MinHelper::Exec(unsigned int, const std::vector<int> &);
126 template void MinHelper::Exec(unsigned int, const std::vector<unsigned int> &);
127 
128 MaxHelper::MaxHelper(const std::shared_ptr<double> &maxVPtr, unsigned int nSlots)
129  : fResultMax(maxVPtr), fMaxs(nSlots, std::numeric_limits<double>::lowest())
130 {
131 }
132 
133 void MaxHelper::Exec(unsigned int slot, double v)
134 {
135  fMaxs[slot] = std::max(v, fMaxs[slot]);
136 }
137 
138 void MaxHelper::Finalize()
139 {
140  *fResultMax = std::numeric_limits<double>::lowest();
141  for (auto &m : fMaxs) {
142  *fResultMax = std::max(m, *fResultMax);
143  }
144 }
145 
146 template void MaxHelper::Exec(unsigned int, const std::vector<float> &);
147 template void MaxHelper::Exec(unsigned int, const std::vector<double> &);
148 template void MaxHelper::Exec(unsigned int, const std::vector<char> &);
149 template void MaxHelper::Exec(unsigned int, const std::vector<int> &);
150 template void MaxHelper::Exec(unsigned int, const std::vector<unsigned int> &);
151 
152 MeanHelper::MeanHelper(const std::shared_ptr<double> &meanVPtr, unsigned int nSlots)
153  : fResultMean(meanVPtr), fCounts(nSlots, 0), fSums(nSlots, 0)
154 {
155 }
156 
157 void MeanHelper::Exec(unsigned int slot, double v)
158 {
159  fSums[slot] += v;
160  fCounts[slot]++;
161 }
162 
163 void MeanHelper::Finalize()
164 {
165  double sumOfSums = 0;
166  for (auto &s : fSums) sumOfSums += s;
167  Count_t sumOfCounts = 0;
168  for (auto &c : fCounts) sumOfCounts += c;
169  *fResultMean = sumOfSums / (sumOfCounts > 0 ? sumOfCounts : 1);
170 }
171 
172 template void MeanHelper::Exec(unsigned int, const std::vector<float> &);
173 template void MeanHelper::Exec(unsigned int, const std::vector<double> &);
174 template void MeanHelper::Exec(unsigned int, const std::vector<char> &);
175 template void MeanHelper::Exec(unsigned int, const std::vector<int> &);
176 template void MeanHelper::Exec(unsigned int, const std::vector<unsigned int> &);
177 
178 } // end NS TDF
179 } // end NS Internal
180 } // end NS ROOT
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
TH1 * h
Definition: legend2.C:5
STL namespace.
SVector< double, 2 > v
Definition: Dict.h:5
TMarker * m
Definition: textangle.C:8