File indexing completed on 2023-03-17 11:16:54
0001 #ifndef PhysicsTools_Utilities_RooFitFunction_h
0002 #define PhysicsTools_Utilities_RooFitFunction_h
0003
0004 #include "PhysicsTools/Utilities/interface/Parameter.h"
0005 #include "RooAbsReal.h"
0006 #include "RooListProxy.h"
0007 #include "RooRealProxy.h"
0008
0009 #include <iostream>
0010 #include <vector>
0011
0012 namespace root {
0013 struct no_args;
0014
0015 template <typename X, typename Expr>
0016 class RooFitFunction : public RooAbsReal {
0017 public:
0018 RooFitFunction(const RooFitFunction<X, Expr>& other, const char* name = nullptr)
0019 : RooAbsReal(other, name),
0020 e_(other.e_),
0021 x_(X::name(), this, other.x_),
0022 parsPtrs_{other.parsPtrs_},
0023 parsArgs_{"!pars", this, other.parsArgs_} {}
0024 RooFitFunction(const char* name, const char* title, const Expr& e, RooAbsReal& x)
0025 : RooAbsReal(name, title),
0026 e_(e),
0027 x_(X::name(), X::name(), this, x),
0028 parsArgs_("!pars", "List of parameters", this) {}
0029 RooFitFunction(
0030 const char* name, const char* title, const Expr& e, RooAbsReal& x, RooAbsReal& rA, funct::Parameter& a)
0031 : RooFitFunction{name, title, e, x} {
0032 add(rA, a);
0033 }
0034 RooFitFunction(const char* name,
0035 const char* title,
0036 const Expr& e,
0037 RooAbsReal& x,
0038 RooAbsReal& rA,
0039 funct::Parameter& a,
0040 RooAbsReal& rB,
0041 funct::Parameter& b)
0042 : RooFitFunction{name, title, e, x, rA, a} {
0043 add(rB, b);
0044 }
0045 RooFitFunction(const char* name,
0046 const char* title,
0047 const Expr& e,
0048 RooAbsReal& x,
0049 RooAbsReal& rA,
0050 funct::Parameter& a,
0051 RooAbsReal& rB,
0052 funct::Parameter& b,
0053 RooAbsReal& rC,
0054 funct::Parameter& c)
0055 : RooFitFunction{name, title, e, x, rA, a, rB, b} {
0056 add(rC, c);
0057 }
0058 void add(RooAbsReal& rA, funct::Parameter& a) {
0059 parsPtrs_.emplace_back(a.ptr());
0060 parsArgs_.add(rA);
0061 }
0062 TObject* clone(const char* newName) const override { return new RooFitFunction<X, Expr>(*this, newName); }
0063
0064 private:
0065 Expr e_;
0066 RooRealProxy x_;
0067 std::vector<std::shared_ptr<double>> parsPtrs_;
0068 RooListProxy parsArgs_;
0069 Double_t evaluate() const override {
0070 X::set(x_);
0071 for (std::size_t i = 0; i < parsPtrs_.size(); ++i) {
0072 *(parsPtrs_[i]) = static_cast<RooAbsReal const&>(parsArgs_[i]).getVal(parsArgs_.nset());
0073 }
0074 return e_();
0075 }
0076 };
0077
0078 }
0079
0080 #endif