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
23class RooSimultaneous;
24
25/// @brief A wrapper class to store a C++ function of type 'double (*)(double*, double*)'.
26/// The parameters can be accessed as params[<relative position of param in paramSet>] in the function body.
27/// The observables can be accessed as obs[i + j], where i represents the observable position and j
28/// represents the data entry.
29class RooFuncWrapper final : public RooAbsReal {
30public:
31 RooFuncWrapper(const char *name, const char *title, RooAbsReal const &obj, RooArgSet const &normSet,
32 const RooAbsData *data, RooSimultaneous const *simPdf, bool createGradient);
33
34 RooFuncWrapper(const RooFuncWrapper &other, const char *name = nullptr);
35
36 TObject *clone(const char *newname) const override { return new RooFuncWrapper(*this, newname); }
37
38 double defaultErrorLevel() const override { return 0.5; }
39
40 bool hasGradient() const override { return _hasGradient; }
41 void gradient(double *out) const override;
42
43 void gradient(const double *x, double *g) const;
44
45 std::size_t getNumParams() const { return _params.size(); }
46
47 void dumpCode();
48
49 void dumpGradient();
50
51 /// No constant term optimization is possible in code-generation mode.
52 void constOptimizeTestStatistic(ConstOpCode /*opcode*/, bool /*doAlsoTrackingOpt*/) override {}
53
54 std::string const &funcName() const { return _funcName; }
55
56protected:
57 double evaluate() const override;
58
59private:
60 std::string buildCode(RooAbsReal const &head);
61
62 void updateGradientVarBuffer() const;
63
64 void loadParamsAndData(RooAbsArg const *head, RooArgSet const &paramSet, const RooAbsData *data,
65 RooSimultaneous const *simPdf);
66
67 void declareAndDiffFunction(std::string const &funcBody, bool createGradient);
68
70
71 using Func = double (*)(double *, double const *);
72 using Grad = void (*)(double *, double const *, double *);
73
74 struct ObsInfo {
75 ObsInfo(std::size_t i, std::size_t n) : idx{i}, size{n} {}
76 std::size_t idx = 0;
77 std::size_t size = 0;
78 };
79
81 std::string _funcName;
84 bool _hasGradient = false;
85 mutable std::vector<double> _gradientVarBuffer;
86 std::vector<double> _observables;
87 std::map<RooFit::Detail::DataKey, ObsInfo> _obsInfos;
88 std::map<RooFit::Detail::DataKey, std::size_t> _nodeOutputSizes;
89};
90
91#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:79
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::vector< double > _gradientVarBuffer
void dumpGradient()
Prints the derivative code body to console.
std::size_t getNumParams() const
TObject * clone(const char *newname) const override
void declareAndDiffFunction(std::string const &funcBody, bool createGradient)
std::map< RooFit::Detail::DataKey, std::size_t > _nodeOutputSizes
std::map< RooFit::Detail::DataKey, ObsInfo > _obsInfos
std::string const & funcName() const
std::string buildCode(RooAbsReal const &head)
void gradient(double *out) const override
double defaultErrorLevel() const override
void constOptimizeTestStatistic(ConstOpCode, bool) override
No constant term optimization is possible in code-generation mode.
double(*)(double *, double const *) Func
void loadParamsAndData(RooAbsArg const *head, RooArgSet const &paramSet, const RooAbsData *data, RooSimultaneous const *simPdf)
std::vector< double > _observables
void(*)(double *, double const *, double *) Grad
std::string _funcName
double evaluate() const override
Evaluate this PDF / function / constant. Needs to be overridden by all derived classes.
RooListProxy _params
bool hasGradient() const override
void updateGradientVarBuffer() const
void dumpCode()
Prints the squashed code body to console.
void buildFuncAndGradFunctors()
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
ObsInfo(std::size_t i, std::size_t n)