Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooAbsDataHelper.cxx
Go to the documentation of this file.
1/*
2 * Project: RooFit
3 * Authors:
4 * Stephan Hageboeck, CERN 2021
5 *
6 * Copyright (c) 2024, CERN
7 *
8 * Redistribution and use in source and binary forms,
9 * with or without modification, are permitted according to the terms
10 * listed in LICENSE (http://roofit.sourceforge.net/license.txt)
11 */
12
13#include <RooAbsDataHelper.h>
14
15#include <RooMsgService.h>
16#include <RooDataSet.h>
17#include <RooDataHist.h>
18
19#include <TROOT.h>
20
21#include <stdexcept>
22
23namespace RooFit {
24namespace Detail {
25
31
33{
35
36 _eventSize = absData.get()->size();
37 _isWeighted = absData.isWeighted();
38 _isDataHist = std::string{absData.ClassName()} != "RooDataSet";
39}
40
41/// Append all `events` to the internal RooDataSet or increment the bins of a RooDataHist at the given locations.
42///
43/// \param events Events to fill into `data`. The layout is assumed to be `(x, y, z, ...) (x, y, z, ...), (...)`.
44/// \note The order of the variables inside `events` must be consistent with the order given in the constructor.
45/// No matching by name is performed.
46/// \param eventSize Size of a single event.
47void RooAbsDataFiller::FillAbsData(const std::vector<double> &events, unsigned int eventSize)
48{
49 if (events.empty())
50 return;
51
53 const RooArgSet &argSet = *absData.get();
54
55 // Relevant for weighted RooDataSet
56 RooRealVar *weightVar = !_isDataHist && _isWeighted ? static_cast<RooDataSet &>(absData).weightVar() : nullptr;
57
58 for (std::size_t i = 0; i < events.size(); i += eventSize) {
59
60 // The RooDataHist has no dedicated RooRealVar for the weight. So we just
61 // use a double.
62 double weightVal = 1.0;
63
64 // Creating a RooDataSet from an RDataFrame should be consistent with the
65 // creation from a TTree. The construction from a TTree discards entries
66 // outside the variable definition range, so we have to do that too (see
67 // also RooTreeDataStore::loadValues).
68
69 bool allOK = true;
70 for (std::size_t j = 0; j < eventSize; ++j) {
71 RooAbsRealLValue *destArg = nullptr;
72 if (j < argSet.size()) {
73 destArg = static_cast<RooAbsRealLValue *>(argSet[j]);
74 } else {
75 destArg = weightVar;
76 }
77 double sourceVal = events[i + j];
78
79 if (destArg && !destArg->inRange(sourceVal, nullptr)) {
81 allOK = false;
82 const auto prefix = std::string(absData.ClassName()) + "Helper::FillAbsData(" + absData.GetName() + ") ";
83 if (_numInvalid < 5) {
84 // Unlike in the TreeVectorStore case, we don't log the event
85 // number here because we don't know it anyway, because of
86 // RDataFrame slots and multithreading.
87 oocoutI(nullptr, DataHandling) << prefix << "Skipping event because " << destArg->GetName()
88 << " cannot accommodate the value " << sourceVal << "\n";
89 } else if (_numInvalid == 5) {
90 oocoutI(nullptr, DataHandling) << prefix << "Skipping ...\n";
91 }
92 break;
93 }
94 if (destArg) {
95 destArg->setVal(sourceVal);
96 } else {
98 }
99 }
100 if (allOK) {
101 absData.add(argSet, weightVar ? weightVar->getVal() : weightVal);
102 }
103 }
104}
105
106/// Empty all buffers into the dataset/hist to finish processing.
108{
110
111 for (auto &vector : _events) {
112 FillAbsData(vector, _nValues);
113 vector.clear();
114 }
115
116 if (_numInvalid > 0) {
117 const auto prefix = std::string(absData.ClassName()) + "Helper::Finalize(" + absData.GetName() + ") ";
118 oocoutW(nullptr, DataHandling) << prefix << "Ignored " << _numInvalid << " out-of-range events\n";
119 }
120}
121
122void RooAbsDataFiller::ExecImpl(std::size_t nValues, std::vector<double> &vector)
123{
124 if (nValues != _eventSize && !(_isWeighted && nValues == _eventSize + 1)) {
125 throw std::invalid_argument(
126 std::string("RooAbsData can hold ") + std::to_string(_eventSize) +
127 " variables per event (plus an optional weight in case of weighted data), but RDataFrame passed " +
128 std::to_string(nValues) + " columns.");
129 }
130
132
133 if (vector.size() > 1024 && _mutexDataset.try_lock()) {
134 const std::lock_guard<std::mutex> guard(_mutexDataset, std::adopt_lock_t());
135 FillAbsData(vector, _nValues);
136 vector.clear();
137 }
138}
139
140} // namespace Detail
141} // namespace RooFit
#define oocoutW(o, a)
#define oocoutI(o, a)
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Storage_t::size_type size() const
Abstract base class for binned and unbinned datasets.
Definition RooAbsData.h:57
Abstract base class for objects that represent a real value that may appear on the left hand side of ...
double getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition RooAbsReal.h:103
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:24
Container class to hold unbinned data.
Definition RooDataSet.h:34
virtual RooAbsData & GetAbsData()=0
void Initialize()
RDataFrame interface method.
std::vector< double > & events(std::size_t slot)
void FillAbsData(const std::vector< double > &events, unsigned int eventSize)
Append all events to the internal RooDataSet or increment the bins of a RooDataHist at the given loca...
std::vector< std::vector< double > > _events
void Finalize()
Empty all buffers into the dataset/hist to finish processing.
void ExecImpl(std::size_t nValues, std::vector< double > &vector)
Variable that can be changed from the outside.
Definition RooRealVar.h:37
Bool_t IsImplicitMTEnabled()
Returns true if the implicit multi-threading in ROOT is enabled.
Definition TROOT.cxx:595
UInt_t GetThreadPoolSize()
Returns the size of ROOT's thread pool.
Definition TROOT.cxx:602
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition CodegenImpl.h:65