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 ROOABSDATAHELPER
14#define ROOABSDATAHELPER
15
16#include <RooAbsDataFiller.h>
17
18#include <ROOT/RDataFrame.hxx>
20
21#include <memory>
22
23/// This is a helper for an RDataFrame action, which fills RooFit data classes.
24///
25/// \tparam DataSet_t Either RooDataSet or RooDataHist.
26///
27/// To construct RooDataSet / RooDataHist within RDataFrame
28/// - Construct one of the two action helpers RooDataSetHelper or RooDataHistHelper. Pass constructor arguments
29/// to RooAbsDataHelper::RooAbsDataHelper() as for the original classes.
30/// The arguments are forwarded to the actual data classes without any changes.
31/// - Book the helper as an RDataFrame action. Here, the RDataFrame column types have to be passed as template
32/// parameters.
33/// - Pass the column names to the Book action. These are matched by position to the variables of the dataset.
34/// If there is one more column name than variables in the dataset, the last columns values will be used as weights.
35///
36/// All arguments passed to are forwarded to RooDataSet::RooDataSet() / RooDataHist::RooDataHist().
37///
38/// #### Usage example:
39/// ```
40/// RooRealVar x("x", "x", -5., 5.);
41/// RooRealVar y("y", "y", -50., 50.);
42/// auto myDataSet = rdataframe.Book<double, double>(
43/// RooDataSetHelper{"dataset", // Name (directly forwarded to RooDataSet::RooDataSet())
44/// "Title of dataset", // Title ( ~ " ~ )
45/// RooArgSet(x, y) }, // Variables to create in dataset
46/// {"x", "y", "weight"} // Column names from RDataFrame
47/// // (this example uses an additional column for the weight)
48/// );
49///
50/// ```
51/// \warning Variables in the dataset and columns in RDataFrame are **matched by position, not by name**.
52/// This enables the easy exchanging of columns that should be filled into the dataset.
53template <class DataSet_t>
55 public ROOT::Detail::RDF::RActionImpl<RooAbsDataHelper<DataSet_t>> {
56public:
57 using Result_t = DataSet_t;
58
59 /// Construct a helper to create RooDataSet/RooDataHist.
60 /// \tparam Args_t Parameter pack of arguments.
61 /// \param args Constructor arguments for RooDataSet::RooDataSet() or RooDataHist::RooDataHist().
62 /// All arguments will be forwarded as they are.
63 template <typename... Args_t>
64 RooAbsDataHelper(Args_t &&...args) : _dataset{new DataSet_t(std::forward<Args_t>(args)...)}
65 {
66 }
67
68 /// Return internal dataset/hist.
69 std::shared_ptr<DataSet_t> GetResultPtr() const { return _dataset; }
70
71 /// Method that RDataFrame calls to pass a new event.
72 ///
73 /// \param slot When IMT is used, this is a number in the range [0, nSlots) to fill lock free.
74 /// \param values x, y, z, ... coordinates of the event.
75 template <typename... ColumnTypes>
76 void Exec(unsigned int slot, ColumnTypes... values)
77 {
78 auto &vector = _events[slot];
79 for (auto &&val : {static_cast<double>(values)...}) {
80 vector.push_back(val);
81 }
82
83 ExecImpl(sizeof...(values), vector);
84 }
85
86 RooAbsData &GetAbsData() override { return *_dataset; }
87
88private:
89 std::shared_ptr<DataSet_t> _dataset;
90};
91
92/// Helper for creating a RooDataSet inside RDataFrame. \see RooAbsDataHelper
94/// Helper for creating a RooDataHist inside RDataFrame. \see RooAbsDataHelper
96
97#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.
RooAbsData & 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
std::vector< std::vector< double > > _events
void ExecImpl(std::size_t nValues, std::vector< double > &vector)