Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/root/RooAbsDataHelper.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002  * Project: RooFit
0003  * Authors:
0004  *   Stephan Hageboeck, CERN 2021
0005  *
0006  * Copyright (c) 2024, CERN
0007  *
0008  * Redistribution and use in source and binary forms,
0009  * with or without modification, are permitted according to the terms
0010  * listed in LICENSE (http://roofit.sourceforge.net/license.txt)
0011  */
0012 
0013 #ifndef ROOABSDATAHELPER
0014 #define ROOABSDATAHELPER
0015 
0016 #include <RooAbsDataFiller.h>
0017 
0018 #include <ROOT/RDataFrame.hxx>
0019 #include <ROOT/RDF/ActionHelpers.hxx>
0020 
0021 #include <memory>
0022 
0023 /// This is a helper for an RDataFrame action, which fills RooFit data classes.
0024 ///
0025 /// \tparam DataSet_t Either RooDataSet or RooDataHist.
0026 ///
0027 /// To construct RooDataSet / RooDataHist within RDataFrame
0028 /// - Construct one of the two action helpers RooDataSetHelper or RooDataHistHelper. Pass constructor arguments
0029 ///   to RooAbsDataHelper::RooAbsDataHelper() as for the original classes.
0030 ///   The arguments are forwarded to the actual data classes without any changes.
0031 /// - Book the helper as an RDataFrame action. Here, the RDataFrame column types have to be passed as template
0032 /// parameters.
0033 /// - Pass the column names to the Book action. These are matched by position to the variables of the dataset.
0034 ///   If there is one more column name than variables in the dataset, the last columns values will be used as weights.
0035 ///
0036 /// All arguments passed to  are forwarded to RooDataSet::RooDataSet() / RooDataHist::RooDataHist().
0037 ///
0038 /// #### Usage example:
0039 /// ```
0040 ///    RooRealVar x("x", "x", -5.,   5.);
0041 ///    RooRealVar y("y", "y", -50., 50.);
0042 ///    auto myDataSet = rdataframe.Book<double, double>(
0043 ///      RooDataSetHelper{"dataset",          // Name   (directly forwarded to RooDataSet::RooDataSet())
0044 ///                      "Title of dataset",  // Title  (                   ~ " ~                      )
0045 ///                      RooArgSet(x, y) },   // Variables to create in dataset
0046 ///      {"x", "y", "weight"}                 // Column names from RDataFrame
0047 ///                                           // (this example uses an additional column for the weight)
0048 ///    );
0049 ///
0050 /// ```
0051 /// \warning Variables in the dataset and columns in RDataFrame are **matched by position, not by name**.
0052 /// This enables the easy exchanging of columns that should be filled into the dataset.
0053 template <class DataSet_t>
0054 class RooAbsDataHelper : public RooFit::Detail::RooAbsDataFiller,
0055                          public ROOT::Detail::RDF::RActionImpl<RooAbsDataHelper<DataSet_t>> {
0056 public:
0057    using Result_t = DataSet_t;
0058 
0059    /// Construct a helper to create RooDataSet/RooDataHist.
0060    /// \tparam Args_t Parameter pack of arguments.
0061    /// \param args Constructor arguments for RooDataSet::RooDataSet() or RooDataHist::RooDataHist().
0062    /// All arguments will be forwarded as they are.
0063    template <typename... Args_t>
0064    RooAbsDataHelper(Args_t &&...args) : _dataset{new DataSet_t(std::forward<Args_t>(args)...)}
0065    {
0066    }
0067 
0068    /// Return internal dataset/hist.
0069    std::shared_ptr<DataSet_t> GetResultPtr() const { return _dataset; }
0070 
0071    /// Method that RDataFrame calls to pass a new event.
0072    ///
0073    /// \param slot When IMT is used, this is a number in the range [0, nSlots) to fill lock free.
0074    /// \param values x, y, z, ... coordinates of the event.
0075    template <typename... ColumnTypes>
0076    void Exec(unsigned int slot, ColumnTypes... values)
0077    {
0078       auto &vector = _events[slot];
0079       for (auto &&val : {static_cast<double>(values)...}) {
0080          vector.push_back(val);
0081       }
0082 
0083       ExecImpl(sizeof...(values), vector);
0084    }
0085 
0086    RooAbsData &GetAbsData() override { return *_dataset; }
0087 
0088 private:
0089    std::shared_ptr<DataSet_t> _dataset;
0090 };
0091 
0092 /// Helper for creating a RooDataSet inside RDataFrame. \see RooAbsDataHelper
0093 using RooDataSetHelper = RooAbsDataHelper<RooDataSet>;
0094 /// Helper for creating a RooDataHist inside RDataFrame. \see RooAbsDataHelper
0095 using RooDataHistHelper = RooAbsDataHelper<RooDataHist>;
0096 
0097 #endif