Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooAbsDataHelper.h
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#ifndef RooFit_RooFitCore_RooAbsDataHelper_h
14#define RooFit_RooFitCore_RooAbsDataHelper_h
15
16#include <RooArgSet.h>
17#include <RooDataHist.h>
18#include <RooDataSet.h>
19#include <RooRealVar.h>
20
21#include <vector>
22#include <mutex>
23#include <cstddef>
24#include <string>
25
26class TTreeReader;
27
28namespace RooFit {
29namespace Detail {
30
32public:
34
35 /// Move constructor. It transfers ownership of the internal RooAbsData object.
36 RooAbsDataFiller(RooAbsDataFiller &&other) : _events{std::move(other._events)}, _eventSize{other._eventSize} {}
38
39 /// Copy is discouraged.
40 /// Use `rdataframe.Book<...>(std::move(absDataHelper), ...)` instead.
43
44 ~RooAbsDataFiller() = default;
45
46 /// RDataFrame interface method.
47 void Initialize();
48 /// RDataFrame interface method. No tasks.
49 void InitTask(TTreeReader *, unsigned int) {}
50 /// RDataFrame interface method.
51 std::string GetActionName() { return "RooDataSetHelper"; }
52
53 void ExecImpl(std::size_t nValues, std::vector<double> &vector);
54 void Finalize();
55
56 virtual RooAbsData &GetAbsData() = 0;
57
58 std::vector<double> &events(std::size_t slot) { return _events[slot]; }
59
60protected:
61 void FillAbsData(const std::vector<double> &events, unsigned int eventSize);
62
63 std::mutex _mutexDataset;
64 std::size_t _numInvalid = 0;
65
66 std::vector<std::vector<double>> _events; // One vector of values per data-processing slot
67 std::size_t _eventSize; // Number of variables in dataset
68 std::size_t _nValues; // Number of variables in dataframe
69
70 bool _isWeighted = false;
71 bool _isDataHist = false;
72};
73
74} // namespace Detail
75} // namespace RooFit
76
77// Forward declarations from RDF
78namespace ROOT {
79namespace Detail {
80namespace RDF {
81template <typename Helper>
82class RActionImpl;
83}
84} // namespace Detail
85} // namespace ROOT
86
87/// This is a helper for an RDataFrame action, which fills RooFit data classes.
88///
89/// \tparam DataSet_t Either RooDataSet or RooDataHist.
90///
91/// To construct RooDataSet / RooDataHist within RDataFrame
92/// - Construct one of the two action helpers RooDataSetHelper or RooDataHistHelper. Pass constructor arguments
93/// to RooAbsDataHelper::RooAbsDataHelper() as for the original classes.
94/// The arguments are forwarded to the actual data classes without any changes.
95/// - Book the helper as an RDataFrame action. Here, the RDataFrame column types have to be passed as template
96/// parameters.
97/// - Pass the column names to the Book action. These are matched by position to the variables of the dataset.
98/// If there is one more column name than variables in the dataset, the last columns values will be used as weights.
99///
100/// All arguments passed to are forwarded to RooDataSet::RooDataSet() / RooDataHist::RooDataHist().
101///
102/// #### Usage example:
103/// ```
104/// RooRealVar x("x", "x", -5., 5.);
105/// RooRealVar y("y", "y", -50., 50.);
106/// auto myDataSet = rdataframe.Book<double, double>(
107/// RooDataSetHelper{"dataset", // Name (directly forwarded to RooDataSet::RooDataSet())
108/// "Title of dataset", // Title ( ~ " ~ )
109/// RooArgSet(x, y) }, // Variables to create in dataset
110/// {"x", "y", "weight"} // Column names from RDataFrame
111/// // (this example uses an additional column for the weight)
112/// );
113///
114/// ```
115/// \warning Variables in the dataset and columns in RDataFrame are **matched by position, not by name**.
116/// This enables the easy exchanging of columns that should be filled into the dataset.
117template <class DataSet_t>
119 public ROOT::Detail::RDF::RActionImpl<RooAbsDataHelper<DataSet_t>> {
120public:
121 using Result_t = DataSet_t;
122
123 /// Construct a helper to create RooDataSet/RooDataHist.
124 /// \tparam Args_t Parameter pack of arguments.
125 /// \param args Constructor arguments for RooDataSet::RooDataSet() or RooDataHist::RooDataHist().
126 /// All arguments will be forwarded as they are.
127 template <typename... Args_t>
128 RooAbsDataHelper(Args_t &&...args) : _dataset{new DataSet_t(std::forward<Args_t>(args)...)}
129 {
130 }
131
132 /// Return internal dataset/hist.
133 std::shared_ptr<DataSet_t> GetResultPtr() const { return _dataset; }
134
135 /// Method that RDataFrame calls to pass a new event.
136 ///
137 /// \param slot When IMT is used, this is a number in the range [0, nSlots) to fill lock free.
138 /// \param values x, y, z, ... coordinates of the event.
139 template <typename... ColumnTypes>
140 void Exec(unsigned int slot, ColumnTypes... values)
141 {
142 std::vector<double> &vector = _events[slot];
143 for (auto &&val : {static_cast<double>(values)...}) {
144 vector.push_back(val);
145 }
146
147 ExecImpl(sizeof...(values), vector);
148 }
149
150 DataSet_t &GetAbsData() override { return *_dataset; }
151
152private:
153 std::shared_ptr<DataSet_t> _dataset;
154};
155
156/// Helper for creating a RooDataSet inside RDataFrame. \see RooAbsDataHelper
158/// Helper for creating a RooDataHist inside RDataFrame. \see RooAbsDataHelper
160
161#endif
Base class for action helpers, see RInterface::Book() for more information.
This is a helper for an RDataFrame action, which fills RooFit data classes.
void Exec(unsigned int slot, ColumnTypes... values)
Method that RDataFrame calls to pass a new event.
DataSet_t & GetAbsData() override
RooAbsDataHelper(Args_t &&...args)
Construct a helper to create RooDataSet/RooDataHist.
std::shared_ptr< DataSet_t > GetResultPtr() const
Return internal dataset/hist.
std::shared_ptr< DataSet_t > _dataset
Abstract base class for binned and unbinned datasets.
Definition RooAbsData.h:57
RooAbsDataFiller(RooAbsDataFiller &&other)
Move constructor. It transfers ownership of the internal RooAbsData object.
RooAbsDataFiller & operator=(RooAbsDataFiller &&)=delete
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.
RooAbsDataFiller & operator=(const RooAbsDataFiller &)=delete
void InitTask(TTreeReader *, unsigned int)
RDataFrame interface method. No tasks.
std::string GetActionName()
RDataFrame interface method.
void ExecImpl(std::size_t nValues, std::vector< double > &vector)
RooAbsDataFiller(const RooAbsDataFiller &)=delete
Copy is discouraged.
A simple, robust and fast interface to read values from ROOT columnar datasets such as TTree,...
Definition TTreeReader.h:46
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition CodegenImpl.h:64