![]() |
|
|||
File indexing completed on 2025-09-16 09:09:02
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 RooFit_RooFitCore_RooAbsDataHelper_h 0014 #define RooFit_RooFitCore_RooAbsDataHelper_h 0015 0016 #include <RooArgSet.h> 0017 #include <RooDataHist.h> 0018 #include <RooDataSet.h> 0019 #include <RooRealVar.h> 0020 0021 #include <vector> 0022 #include <mutex> 0023 #include <cstddef> 0024 #include <string> 0025 0026 class TTreeReader; 0027 0028 namespace RooFit { 0029 namespace Detail { 0030 0031 class RooAbsDataFiller { 0032 public: 0033 RooAbsDataFiller(); 0034 0035 /// Move constructor. It transfers ownership of the internal RooAbsData object. 0036 RooAbsDataFiller(RooAbsDataFiller &&other) : _events{std::move(other._events)}, _eventSize{other._eventSize} {} 0037 RooAbsDataFiller &operator=(RooAbsDataFiller &&) = delete; 0038 0039 /// Copy is discouraged. 0040 /// Use `rdataframe.Book<...>(std::move(absDataHelper), ...)` instead. 0041 RooAbsDataFiller(const RooAbsDataFiller &) = delete; 0042 RooAbsDataFiller &operator=(const RooAbsDataFiller &) = delete; 0043 0044 ~RooAbsDataFiller() = default; 0045 0046 /// RDataFrame interface method. 0047 void Initialize(); 0048 /// RDataFrame interface method. No tasks. 0049 void InitTask(TTreeReader *, unsigned int) {} 0050 /// RDataFrame interface method. 0051 std::string GetActionName() { return "RooDataSetHelper"; } 0052 0053 void ExecImpl(std::size_t nValues, std::vector<double> &vector); 0054 void Finalize(); 0055 0056 virtual RooAbsData &GetAbsData() = 0; 0057 0058 std::vector<double> &events(std::size_t slot) { return _events[slot]; } 0059 0060 protected: 0061 void FillAbsData(const std::vector<double> &events, unsigned int eventSize); 0062 0063 std::mutex _mutexDataset; 0064 std::size_t _numInvalid = 0; 0065 0066 std::vector<std::vector<double>> _events; // One vector of values per data-processing slot 0067 std::size_t _eventSize; // Number of variables in dataset 0068 std::size_t _nValues; // Number of variables in dataframe 0069 0070 bool _isWeighted = false; 0071 bool _isDataHist = false; 0072 }; 0073 0074 } // namespace Detail 0075 } // namespace RooFit 0076 0077 // Forward declarations from RDF 0078 namespace ROOT { 0079 namespace Detail { 0080 namespace RDF { 0081 template <typename Helper> 0082 class RActionImpl; 0083 } 0084 } // namespace Detail 0085 } // namespace ROOT 0086 0087 /// This is a helper for an RDataFrame action, which fills RooFit data classes. 0088 /// 0089 /// \tparam DataSet_t Either RooDataSet or RooDataHist. 0090 /// 0091 /// To construct RooDataSet / RooDataHist within RDataFrame 0092 /// - Construct one of the two action helpers RooDataSetHelper or RooDataHistHelper. Pass constructor arguments 0093 /// to RooAbsDataHelper::RooAbsDataHelper() as for the original classes. 0094 /// The arguments are forwarded to the actual data classes without any changes. 0095 /// - Book the helper as an RDataFrame action. Here, the RDataFrame column types have to be passed as template 0096 /// parameters. 0097 /// - Pass the column names to the Book action. These are matched by position to the variables of the dataset. 0098 /// If there is one more column name than variables in the dataset, the last columns values will be used as weights. 0099 /// 0100 /// All arguments passed to are forwarded to RooDataSet::RooDataSet() / RooDataHist::RooDataHist(). 0101 /// 0102 /// #### Usage example: 0103 /// ``` 0104 /// RooRealVar x("x", "x", -5., 5.); 0105 /// RooRealVar y("y", "y", -50., 50.); 0106 /// auto myDataSet = rdataframe.Book<double, double>( 0107 /// RooDataSetHelper{"dataset", // Name (directly forwarded to RooDataSet::RooDataSet()) 0108 /// "Title of dataset", // Title ( ~ " ~ ) 0109 /// RooArgSet(x, y) }, // Variables to create in dataset 0110 /// {"x", "y", "weight"} // Column names from RDataFrame 0111 /// // (this example uses an additional column for the weight) 0112 /// ); 0113 /// 0114 /// ``` 0115 /// \warning Variables in the dataset and columns in RDataFrame are **matched by position, not by name**. 0116 /// This enables the easy exchanging of columns that should be filled into the dataset. 0117 template <class DataSet_t> 0118 class RooAbsDataHelper : public RooFit::Detail::RooAbsDataFiller, 0119 public ROOT::Detail::RDF::RActionImpl<RooAbsDataHelper<DataSet_t>> { 0120 public: 0121 using Result_t = DataSet_t; 0122 0123 /// Construct a helper to create RooDataSet/RooDataHist. 0124 /// \tparam Args_t Parameter pack of arguments. 0125 /// \param args Constructor arguments for RooDataSet::RooDataSet() or RooDataHist::RooDataHist(). 0126 /// All arguments will be forwarded as they are. 0127 template <typename... Args_t> 0128 RooAbsDataHelper(Args_t &&...args) : _dataset{new DataSet_t(std::forward<Args_t>(args)...)} 0129 { 0130 } 0131 0132 /// Return internal dataset/hist. 0133 std::shared_ptr<DataSet_t> GetResultPtr() const { return _dataset; } 0134 0135 /// Method that RDataFrame calls to pass a new event. 0136 /// 0137 /// \param slot When IMT is used, this is a number in the range [0, nSlots) to fill lock free. 0138 /// \param values x, y, z, ... coordinates of the event. 0139 template <typename... ColumnTypes> 0140 void Exec(unsigned int slot, ColumnTypes... values) 0141 { 0142 std::vector<double> &vector = _events[slot]; 0143 for (auto &&val : {static_cast<double>(values)...}) { 0144 vector.push_back(val); 0145 } 0146 0147 ExecImpl(sizeof...(values), vector); 0148 } 0149 0150 DataSet_t &GetAbsData() override { return *_dataset; } 0151 0152 private: 0153 std::shared_ptr<DataSet_t> _dataset; 0154 }; 0155 0156 /// Helper for creating a RooDataSet inside RDataFrame. \see RooAbsDataHelper 0157 using RooDataSetHelper = RooAbsDataHelper<RooDataSet>; 0158 /// Helper for creating a RooDataHist inside RDataFrame. \see RooAbsDataHelper 0159 using RooDataHistHelper = RooAbsDataHelper<RooDataHist>; 0160 0161 #endif
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
![]() ![]() |