Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:33:29

0001 #ifndef PhysicsTools_Utilities_RooFitFunction_h
0002 #define PhysicsTools_Utilities_RooFitFunction_h
0003 #include "PhysicsTools/Utilities/interface/Parameter.h"
0004 #include "RooAbsReal.h"
0005 #include "RooRealProxy.h"
0006 #include "RooAbsReal.h"
0007 #include <iostream>
0008 #include <vector>
0009 
0010 namespace root {
0011   struct no_args;
0012 
0013   template <typename X, typename Expr>
0014   class RooFitFunction : public RooAbsReal {
0015   public:
0016     RooFitFunction(const RooFitFunction<X, Expr>& other, const char* name = nullptr)
0017         : RooAbsReal(other, name), e_(other.e_), x_(X::name(), this, other.x_) {
0018       std::cout << ">>> making new RooFitFunction" << std::endl;
0019       std::vector<std::pair<std::shared_ptr<double>, RooRealProxy> >::const_iterator i = other.pars_.begin(),
0020                                                                                      end = other.pars_.end();
0021       for (; i != end; ++i) {
0022         std::cout << ">>> adding par to RooFitFunction" << std::endl;
0023         pars_.push_back(std::make_pair(i->first, RooRealProxy(i->second.GetName(), this, i->second)));
0024       }
0025     }
0026     RooFitFunction(const char* name, const char* title, const Expr& e, RooAbsReal& x)
0027         : RooAbsReal(name, title), e_(e), x_(X::name(), X::name(), this, x) {}
0028     RooFitFunction(
0029         const char* name, const char* title, const Expr& e, RooAbsReal& x, RooAbsReal& rA, funct::Parameter& a)
0030         : RooAbsReal(name, title), e_(e), x_(X::name(), X::name(), this, x) {
0031       pars_.push_back(std::make_pair(a.ptr(), RooRealProxy(a.name().c_str(), a.name().c_str(), this, rA)));
0032     }
0033     RooFitFunction(const char* name,
0034                    const char* title,
0035                    const Expr& e,
0036                    RooAbsReal& x,
0037                    RooAbsReal& rA,
0038                    funct::Parameter& a,
0039                    RooAbsReal& rB,
0040                    funct::Parameter& b)
0041         : RooAbsReal(name, title), e_(e), x_(X::name(), X::name(), this, x) {
0042       pars_.push_back(std::make_pair(a.ptr(), RooRealProxy(a.name().c_str(), a.name().c_str(), this, rA)));
0043       pars_.push_back(std::make_pair(b.ptr(), RooRealProxy(b.name().c_str(), b.name().c_str(), this, rB)));
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         : RooAbsReal(name, title), e_(e), x_(X::name(), X::name(), this, x) {
0056       pars_.push_back(std::make_pair(a.ptr(), RooRealProxy(a.name().c_str(), a.name().c_str(), this, rA)));
0057       pars_.push_back(std::make_pair(b.ptr(), RooRealProxy(b.name().c_str(), b.name().c_str(), this, rB)));
0058       pars_.push_back(std::make_pair(c.ptr(), RooRealProxy(c.name().c_str(), c.name().c_str(), this, rC)));
0059     }
0060     ~RooFitFunction() override {}
0061     void add(RooAbsReal& rA, funct::Parameter& a) {
0062       pars_.push_back(std::make_pair(a.ptr(), RooRealProxy(a.name().c_str(), a.name().c_str(), this, rA)));
0063     }
0064     TObject* clone(const char* newName) const override { return new RooFitFunction<X, Expr>(*this, newName); }
0065 
0066   private:
0067     Expr e_;
0068     RooRealProxy x_;
0069     std::vector<std::pair<std::shared_ptr<double>, RooRealProxy> > pars_;
0070     Double_t evaluate() const override {
0071       X::set(x_);
0072       std::vector<std::pair<std::shared_ptr<double>, RooRealProxy> >::const_iterator i = pars_.begin(),
0073                                                                                      end = pars_.end();
0074       for (; i != end; ++i)
0075         *(i->first) = i->second;
0076       return e_();
0077     }
0078   };
0079 
0080 }  // namespace root
0081 
0082 #endif