32void assignSpan(std::span<T> &to, std::span<T>
const &from)
37std::map<RooFit::Detail::DataKey, std::span<const double>>
38getSingleDataSpans(
RooAbsData const &
data, std::string_view rangeName, std::string
const &prefix,
39 std::stack<std::vector<double>> &buffers,
bool skipZeroWeights)
41 std::map<RooFit::Detail::DataKey, std::span<const double>> dataSpans;
45 auto insert = [&](
const char *key, std::span<const double> span) {
46 const TNamed *namePtr = nameReg.constPtr((prefix + key).c_str());
47 dataSpans[namePtr] = span;
50 auto retrieve = [&](
const char *key) {
51 const TNamed *namePtr = nameReg.constPtr((prefix + key).c_str());
52 return dataSpans.at(namePtr);
55 std::size_t nEvents =
static_cast<size_t>(
data.numEntries());
57 auto weight =
data.getWeightBatch(0, nEvents,
false);
58 auto weightSumW2 =
data.getWeightBatch(0, nEvents,
true);
60 std::vector<bool> hasZeroWeight;
61 hasZeroWeight.resize(nEvents);
62 std::size_t nNonZeroWeight = 0;
69 auto &buffer = buffers.top();
71 auto &bufferSumW2 = buffers.top();
76 buffer.push_back(1.0);
77 bufferSumW2.push_back(1.0);
78 assignSpan(weight, {buffer.data(), 1});
79 assignSpan(weightSumW2, {bufferSumW2.data(), 1});
80 nNonZeroWeight = nEvents;
82 buffer.reserve(nEvents);
83 bufferSumW2.reserve(nEvents);
84 for (std::size_t i = 0; i < nEvents; ++i) {
85 if (!skipZeroWeights || weight[i] != 0) {
86 buffer.push_back(weight[i]);
87 bufferSumW2.push_back(weightSumW2[i]);
90 hasZeroWeight[i] =
true;
93 assignSpan(weight, {buffer.data(), nNonZeroWeight});
94 assignSpan(weightSumW2, {bufferSumW2.data(), nNonZeroWeight});
96 insert(RooNLLVarNew::weightVarName, weight);
97 insert(RooNLLVarNew::weightVarNameSumW2, weightSumW2);
102 for (
auto const &item :
data.getBatches(0, nEvents)) {
104 std::span<const double> span{item.second};
107 auto &buffer = buffers.top();
108 buffer.reserve(nNonZeroWeight);
110 for (std::size_t i = 0; i < nEvents; ++i) {
111 if (!hasZeroWeight[i]) {
112 buffer.push_back(span[i]);
115 insert(item.first->GetName(), {buffer.data(), buffer.size()});
120 for (
auto const &item :
data.getCategoryBatches(0, nEvents)) {
122 std::span<const RooAbsCategory::value_type> intSpan{item.second};
125 auto &buffer = buffers.top();
126 buffer.reserve(nNonZeroWeight);
128 for (std::size_t i = 0; i < nEvents; ++i) {
129 if (!hasZeroWeight[i]) {
130 buffer.push_back(
static_cast<double>(intSpan[i]));
133 insert(item.first->GetName(), {buffer.data(), buffer.size()});
136 nEvents = nNonZeroWeight;
139 if (!rangeName.empty()) {
141 std::vector<bool> isInRange(nEvents,
false);
142 for (
auto const &range :
ROOT::
Split(rangeName,
",")) {
143 std::vector<bool> isInSubRange(nEvents,
true);
147 observable->inRange({
retrieve(observable->GetName()).data(), nEvents}, range, isInSubRange);
150 for (std::size_t i = 0; i < isInSubRange.size(); ++i) {
151 isInRange[i] = isInRange[i] || isInSubRange[i];
156 nEvents = std::accumulate(isInRange.begin(), isInRange.end(), 0);
159 for (
auto const &item : dataSpans) {
160 auto const &allValues = item.second;
161 if (allValues.size() == 1) {
164 buffers.emplace(nEvents);
165 double *buffer = buffers.top().data();
167 for (std::size_t i = 0; i < isInRange.size(); ++i) {
169 buffer[j] = allValues[i];
173 assignSpan(dataSpans[item.first], {buffer, nEvents});
206std::map<RooFit::Detail::DataKey, std::span<const double>>
207RooFit::BatchModeDataHelpers::getDataSpans(
RooAbsData const &
data, std::string
const &rangeName,
209 bool takeGlobalObservablesFromData, std::stack<std::vector<double>> &buffers)
211 std::vector<std::pair<std::string, RooAbsData const *>> datasets;
212 std::vector<bool> isBinnedL;
213 bool splitRange =
false;
214 std::vector<std::unique_ptr<RooAbsData>> splitDataSets;
217 std::unique_ptr<TList> splits{
data.split(*simPdf,
true)};
224 datasets.emplace_back(std::string(
"_") +
d->GetName() +
"_",
d);
225 isBinnedL.emplace_back(simComponent->
getAttribute(
"BinnedLikelihoodActive"));
227 splitDataSets.emplace_back(
d);
231 datasets.emplace_back(
"", &
data);
232 isBinnedL.emplace_back(
false);
235 std::map<RooFit::Detail::DataKey, std::span<const double>> dataSpans;
237 for (std::size_t iData = 0; iData < datasets.size(); ++iData) {
238 auto const &toAdd = datasets[iData];
239 auto spans = getSingleDataSpans(
241 toAdd.first, buffers, skipZeroWeights && !isBinnedL[iData]);
242 for (
auto const &item : spans) {
243 dataSpans.insert(item);
247 if (takeGlobalObservablesFromData &&
data.getGlobalObservables()) {
249 auto &buffer = buffers.top();
250 buffer.reserve(
data.getGlobalObservables()->size());
252 buffer.push_back(arg->getVal());
253 assignSpan(dataSpans[arg], {&buffer.back(), 1});
269std::map<RooFit::Detail::DataKey, std::size_t> RooFit::BatchModeDataHelpers::determineOutputSizes(
272 std::map<RooFit::Detail::DataKey, std::size_t>
output;
278 int inputSize = inputSizeFunc(arg);
281 if (inputSize != -1) {
287 std::size_t
size = 1;
291 if (!arg->isReducerNode()) {
292 for (
RooAbsArg *server : arg->servers()) {
293 if (server->isValueServer(*arg)) {
294 std::size_t inputSize =
output.at(server);
295 if (inputSize != 1) {
ROOT::RRangeCast< T, true, Range_t > dynamic_range_cast(Range_t &&coll)
ROOT::RRangeCast< T, false, Range_t > static_range_cast(Range_t &&coll)
static void retrieve(const gsl_integration_workspace *workspace, double *a, double *b, double *r, double *e)
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
Common abstract base class for objects that represent a value and a "shape" in RooFit.
bool getAttribute(const Text_t *name) const
Check if a named attribute is set. By default, all attributes are unset.
Abstract base class for binned and unbinned datasets.
Abstract interface for all probability density functions.
Abstract base class for objects that represent a real value that may appear on the left hand side of ...
RooArgSet is a container object that can hold multiple RooAbsArg objects.
static RooNameReg & instance()
Return reference to singleton instance.
Variable that can be changed from the outside.
Facilitates simultaneous fitting of multiple PDFs to subsets of a given dataset.
RooAbsPdf * getPdf(RooStringView catName) const
Return the p.d.f associated with the given index category name.
The TNamed class is the base class for all named ROOT classes.
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
std::vector< std::string > Split(std::string_view str, std::string_view delims, bool skipEmpty=false)
Splits a string at each character in delims.
std::string getRangeNameForSimComponent(std::string const &rangeName, bool splitRange, std::string const &catName)
void getSortedComputationGraph(RooAbsArg const &func, RooArgSet &out)