Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooFuncWrapper.h
Go to the documentation of this file.
1/*
2 * Project: RooFit
3 * Authors:
4 * Garima Singh, CERN 2022
5 *
6 * Copyright (c) 2022, 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_RooFuncWrapper_h
14#define RooFit_RooFuncWrapper_h
15
16#include <RooAbsReal.h>
17#include <RooListProxy.h>
18
19#include <map>
20#include <memory>
21#include <string>
22#include <sstream>
23
24class RooSimultaneous;
25
26namespace RooFit {
27
28namespace Experimental {
29
30/// @brief A wrapper class to store a C++ function of type 'double (*)(double*, double*)'.
31/// The parameters can be accessed as params[<relative position of param in paramSet>] in the function body.
32/// The observables can be accessed as obs[i + j], where i represents the observable position and j
33/// represents the data entry.
34class RooFuncWrapper final : public RooAbsReal {
35public:
36 RooFuncWrapper(const char *name, const char *title, RooAbsReal &obj, const RooAbsData *data = nullptr,
37 RooSimultaneous const *simPdf = nullptr, bool useEvaluator = false);
38
39 RooFuncWrapper(const RooFuncWrapper &other, const char *name = nullptr);
40
41 TObject *clone(const char *newname) const override { return new RooFuncWrapper(*this, newname); }
42
43 double defaultErrorLevel() const override { return 0.5; }
44
45 bool hasGradient() const override { return _hasGradient; }
46 void gradient(double *out) const override;
47
48 void gradient(const double *x, double *g) const;
49
50 std::size_t getNumParams() const { return _params.size(); }
51
52 /// No constant term optimization is possible in code-generation mode.
53 void constOptimizeTestStatistic(ConstOpCode /*opcode*/, bool /*doAlsoTrackingOpt*/) override {}
54
55 std::string const &funcName() const { return _funcName; }
56
57 void createGradient();
58
59 void disableEvaluator() { _useEvaluator = false; }
60
61 void writeDebugMacro(std::string const &) const;
62
63 std::string declareFunction(std::string const &funcBody);
64
65 std::string buildCode(RooAbsReal const &head);
66
67protected:
68 double evaluate() const override;
69
70private:
71 void updateGradientVarBuffer() const;
72
73 void loadParamsAndData(RooAbsArg const *head, RooArgSet const &paramSet, const RooAbsData *data,
74 RooSimultaneous const *simPdf);
75
77
78 bool declareToInterpreter(std::string const &code);
79
80 using Func = double (*)(double *, double const *, double const *);
81 using Grad = void (*)(double *, double const *, double const *, double *);
82
83 struct ObsInfo {
84 ObsInfo(std::size_t i, std::size_t n) : idx{i}, size{n} {}
85 std::size_t idx = 0;
86 std::size_t size = 0;
87 };
88
89 std::unique_ptr<RooAbsReal> _absReal;
91 std::string _funcName;
94 bool _hasGradient = false;
95 bool _useEvaluator = false;
96 mutable std::vector<double> _gradientVarBuffer;
97 std::vector<double> _observables;
98 std::map<RooFit::Detail::DataKey, ObsInfo> _obsInfos;
99 std::map<RooFit::Detail::DataKey, std::size_t> _nodeOutputSizes;
100 std::vector<double> _xlArr;
101 std::stringstream _allCode;
102};
103
104} // namespace Experimental
105
106} // namespace RooFit
107
108#endif
#define g(i)
Definition RSha256.hxx:105
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
char name[80]
Definition TGX11.cxx:110
Common abstract base class for objects that represent a value and a "shape" in RooFit.
Definition RooAbsArg.h:77
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 and implements functionality common to al...
Definition RooAbsReal.h:59
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:55
A wrapper class to store a C++ function of type 'double (*)(double*, double*)'.
std::map< RooFit::Detail::DataKey, std::size_t > _nodeOutputSizes
std::string const & funcName() const
double(*)(double *, double const *, double const *) Func
double evaluate() const override
Evaluate this PDF / function / constant. Needs to be overridden by all derived classes.
std::unique_ptr< RooAbsReal > _absReal
TObject * clone(const char *newname) const override
double defaultErrorLevel() const override
std::string buildCode(RooAbsReal const &head)
void loadParamsAndData(RooAbsArg const *head, RooArgSet const &paramSet, const RooAbsData *data, RooSimultaneous const *simPdf)
void writeDebugMacro(std::string const &) const
Dumps a macro "filename.C" that can be used to test and debug the generated code and gradient.
void constOptimizeTestStatistic(ConstOpCode, bool) override
No constant term optimization is possible in code-generation mode.
void gradient(double *out) const override
std::map< RooFit::Detail::DataKey, ObsInfo > _obsInfos
std::vector< double > _gradientVarBuffer
void(*)(double *, double const *, double const *, double *) Grad
std::string declareFunction(std::string const &funcBody)
bool declareToInterpreter(std::string const &code)
Declare code to the interpreter and keep track of all declared code in this RooFuncWrapper.
Facilitates simultaneous fitting of multiple PDFs to subsets of a given dataset.
Mother of all ROOT objects.
Definition TObject.h:41
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition JSONIO.h:26