Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-19 09:24:27

0001 /*
0002  * Project: RooFit
0003  *
0004  * Copyright (c) 2023, CERN
0005  *
0006  * Redistribution and use in source and binary forms,
0007  * with or without modification, are permitted according to the terms
0008  * listed in LICENSE (http://roofit.sourceforge.net/license.txt)
0009  */
0010 
0011 #ifndef ROOMOMENTMORPHFUNCND
0012 #define ROOMOMENTMORPHFUNCND
0013 
0014 #include "RooAbsPdf.h"
0015 #include "RooRealProxy.h"
0016 #include "RooCategoryProxy.h"
0017 #include "RooAbsReal.h"
0018 #include "RooAbsCategory.h"
0019 #include "RooSetProxy.h"
0020 #include "RooListProxy.h"
0021 #include "RooArgList.h"
0022 #include "RooBinning.h"
0023 #include "RooFit/Detail/NormalizationHelpers.h"
0024 
0025 #include "TMatrixD.h"
0026 #include "TMap.h"
0027 
0028 #include <vector>
0029 #include <map>
0030 
0031 class RooChangeTracker;
0032 class RooRealSumFunc;
0033 
0034 namespace RooFit {
0035 namespace Detail {
0036 class RooMomentMorphFraction;
0037 }
0038 } // namespace RooFit
0039 
0040 class RooMomentMorphFuncND : public RooAbsReal {
0041 
0042 public:
0043    using Base_t = RooAbsReal;
0044 
0045    class Grid2 {
0046    public:
0047       Grid2(){};
0048       Grid2(const Grid2 &other);
0049       Grid2(const RooAbsBinning &binning_x) { _grid.push_back(binning_x.clone()); };
0050       Grid2(const RooAbsBinning &binning_x, const RooAbsBinning &binning_y)
0051       {
0052          _grid.push_back(binning_x.clone());
0053          _grid.push_back(binning_y.clone());
0054       };
0055       Grid2(const RooAbsBinning &binning_x, const RooAbsBinning &binning_y, const RooAbsBinning &binning_z)
0056       {
0057          _grid.push_back(binning_x.clone());
0058          _grid.push_back(binning_y.clone());
0059          _grid.push_back(binning_z.clone());
0060       };
0061       Grid2(std::vector<RooAbsBinning *> const &binnings)
0062       {
0063          for (unsigned int i = 0; i < binnings.size(); i++) {
0064             _grid.push_back(binnings[i]->clone());
0065          }
0066       };
0067 
0068       virtual ~Grid2();
0069 
0070       void addPdf(const RooAbsReal &func, int bin_x);
0071       void addPdf(const RooAbsReal &func, int bin_x, int bin_y);
0072       void addPdf(const RooAbsReal &func, int bin_x, int bin_y, int bin_z);
0073       void addPdf(const RooAbsReal &func, std::vector<int> bins);
0074       void addBinning(const RooAbsBinning &binning) { _grid.push_back(binning.clone()); };
0075 
0076       mutable std::vector<RooAbsBinning *> _grid;
0077       mutable RooArgList _pdfList;
0078       mutable std::map<std::vector<int>, int> _pdfMap;
0079 
0080       mutable std::vector<std::vector<double>> _nref;
0081       mutable std::vector<int> _nnuis;
0082 
0083       ClassDef(RooMomentMorphFuncND::Grid2, 1);
0084    };
0085 
0086    using Grid = Grid2;
0087 
0088 protected:
0089    class CacheElem : public RooAbsCacheElement {
0090    public:
0091       CacheElem(std::unique_ptr<RooAbsReal> && sumFunc, std::unique_ptr<RooChangeTracker> && tracker, const RooArgList &flist);
0092       ~CacheElem() override;
0093       RooArgList containedArgs(Action) override;
0094       std::unique_ptr<RooAbsReal> _sum;
0095       std::unique_ptr<RooChangeTracker> _tracker;
0096       RooArgList _frac;
0097 
0098       RooRealVar *frac(int i);
0099       const RooRealVar *frac(int i) const;
0100       void calculateFractions(const RooMomentMorphFuncND &self, bool verbose = true) const;
0101    };
0102 
0103 public:
0104    enum Setting { Linear, SineLinear, NonLinear, NonLinearPosFractions, NonLinearLinFractions };
0105 
0106    RooMomentMorphFuncND();
0107    RooMomentMorphFuncND(const char *name, const char *title, RooAbsReal &_m, const RooArgList &varList,
0108                         const RooArgList &pdfList, const RooArgList &mrefList, Setting setting);
0109    RooMomentMorphFuncND(const char *name, const char *title, const RooArgList &parList, const RooArgList &obsList,
0110                         const Grid2 &referenceGrid, Setting setting);
0111    RooMomentMorphFuncND(const RooMomentMorphFuncND &other, const char *name = nullptr);
0112    RooMomentMorphFuncND(const char *name, const char *title, RooAbsReal &_m, const RooArgList &varList,
0113                         const RooArgList &pdfList, const TVectorD &mrefpoints, Setting setting);
0114    ~RooMomentMorphFuncND() override;
0115    TObject *clone(const char *newname) const override { return new RooMomentMorphFuncND(*this, newname); }
0116 
0117    void setMode(const Setting &setting) { _setting = setting; }
0118    /// Setting flag makes this RooMomentMorphFuncND instance behave like the
0119    /// former RooMomentMorphND class, with the the only difference being the
0120    /// base class. If you want to create a pdf object that behaves exactly like
0121    /// the old RooMomentMorphND, you can do it as follows:
0122    ///
0123    /// ```C++
0124    /// RooMomentMorphFuncND func{<c'tor args you previously passed to RooMomentMorphFunc>};
0125    ///
0126    /// func.setPdfMode(); // change behavior to be exactly like the former RooMomentMorphND
0127    ///
0128    /// // Pass the selfNormalized=true` flag to the wrapper because the
0129    /// RooMomentMorphFuncND already normalizes itself in pdf mode.
0130    /// RooWrapperPdf pdf{"pdf_name", "pdf_name", func, /*selfNormalized=*/true};
0131    /// ```
0132    void setPdfMode(bool flag=true) { _isPdfMode = flag; }
0133    bool setBinIntegrator(RooArgSet &allVars);
0134    void useHorizontalMorphing(bool val) { _useHorizMorph = val; }
0135 
0136    double evaluate() const override;
0137    double getValV(const RooArgSet *set = nullptr) const override;
0138 
0139    std::unique_ptr<RooAbsArg>
0140    compileForNormSet(RooArgSet const &normSet, RooFit::Detail::CompileContext &ctx) const override;
0141 
0142 protected:
0143    void initialize();
0144 
0145    RooAbsReal *sumFunc(const RooArgSet *nset);
0146    CacheElem *getCache(const RooArgSet *nset) const;
0147 
0148    void findShape(const std::vector<double> &x) const;
0149 
0150    friend class CacheElem;
0151    friend class Grid2;
0152    friend class RooFit::Detail::RooMomentMorphFraction;
0153 
0154    mutable RooObjCacheManager _cacheMgr;     ///<! Transient cache manager
0155    mutable RooArgSet *_curNormSet = nullptr; ///<! Transient cache manager
0156 
0157    RooListProxy _parList;
0158    RooSetProxy _obsList;
0159    mutable Grid2 _referenceGrid;
0160    RooListProxy _pdfList;
0161 
0162    mutable std::unique_ptr<TMatrixD> _M;
0163    mutable std::unique_ptr<TMatrixD> _MSqr;
0164    mutable std::vector<std::vector<double>> _squareVec;
0165    mutable std::vector<int> _squareIdx;
0166 
0167    Setting _setting;
0168    bool _useHorizMorph;
0169    bool _isPdfMode = false;
0170 
0171    inline int sij(const int &i, const int &j) const { return (i * _obsList.size() + j); }
0172 
0173    ClassDefOverride(RooMomentMorphFuncND, 4);
0174 };
0175 
0176 namespace RooFit {
0177 namespace Detail {
0178 
0179 /// Helper compute-graph node that exposes one of the morph mixing fractions to
0180 /// the RooFit::Evaluator. It re-runs RooMomentMorphFuncND::CacheElem::calculateFractions
0181 /// only when the morph parameters change, then returns the cached fraction value
0182 /// for this index.
0183 class RooMomentMorphFraction : public RooAbsReal {
0184 public:
0185    RooMomentMorphFraction() {}
0186    RooMomentMorphFraction(const char *name, const char *title, RooMomentMorphFuncND const &parent, int index);
0187    RooMomentMorphFraction(RooMomentMorphFraction const &other, const char *name = nullptr);
0188    TObject *clone(const char *newname) const override { return new RooMomentMorphFraction(*this, newname); }
0189 
0190 protected:
0191    double evaluate() const override;
0192 
0193 private:
0194    RooListProxy _parList;
0195    const RooMomentMorphFuncND *_parent = nullptr; ///<! morph that owns the cache (not owned)
0196    int _index = 0;
0197 
0198    ClassDefOverride(RooFit::Detail::RooMomentMorphFraction, 0);
0199 };
0200 
0201 } // namespace Detail
0202 } // namespace RooFit
0203 
0204 #endif