Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 09:15:16

0001 /*****************************************************************************
0002  * Project: RooFit                                                           *
0003  * Package: RooFitCore                                                       *
0004  *    File: $Id: RooProdPdf.h,v 1.44 2007/07/16 21:04:28 wouter Exp $
0005  * Authors:                                                                  *
0006  *   WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu       *
0007  *   DK, David Kirkby,    UC Irvine,         dkirkby@uci.edu                 *
0008  *                                                                           *
0009  * Copyright (c) 2000-2005, Regents of the University of California          *
0010  *                          and Stanford University. All rights reserved.    *
0011  *                                                                           *
0012  * Redistribution and use in source and binary forms,                        *
0013  * with or without modification, are permitted according to the terms        *
0014  * listed in LICENSE (http://roofit.sourceforge.net/license.txt)             *
0015  *****************************************************************************/
0016 #ifndef ROO_PROD_PDF
0017 #define ROO_PROD_PDF
0018 
0019 #include "RooAbsPdf.h"
0020 #include "RooListProxy.h"
0021 #include "RooLinkedList.h"
0022 #include "RooAICRegistry.h"
0023 #include "RooObjCacheManager.h"
0024 #include "RooCmdArg.h"
0025 
0026 #include <vector>
0027 #include <list>
0028 #include <string>
0029 
0030 namespace RooFit {
0031 namespace Detail {
0032 class RooFixedProdPdf;
0033 }
0034 }
0035 
0036 class RooProdPdf : public RooAbsPdf {
0037 public:
0038 
0039   RooProdPdf() ;
0040   RooProdPdf(const char *name, const char *title,
0041        RooAbsPdf& pdf1, RooAbsPdf& pdf2, double cutOff=0.0) ;
0042   RooProdPdf(const char* name, const char* title, const RooArgList& pdfList, double cutOff=0.0) ;
0043   RooProdPdf(const char* name, const char* title, const RooArgSet& fullPdfSet, const RooLinkedList& cmdArgList) ;
0044 
0045   RooProdPdf(const char* name, const char* title, const RooArgSet& fullPdfSet,
0046            const RooCmdArg& arg1            , const RooCmdArg& arg2={},
0047              const RooCmdArg& arg3={}, const RooCmdArg& arg4={},
0048              const RooCmdArg& arg5={}, const RooCmdArg& arg6={},
0049              const RooCmdArg& arg7={}, const RooCmdArg& arg8={}) ;
0050 
0051   RooProdPdf(const char* name, const char* title,
0052              const RooCmdArg& arg1,             const RooCmdArg& arg2={},
0053              const RooCmdArg& arg3={}, const RooCmdArg& arg4={},
0054              const RooCmdArg& arg5={}, const RooCmdArg& arg6={},
0055              const RooCmdArg& arg7={}, const RooCmdArg& arg8={}) ;
0056 
0057   RooProdPdf(const RooProdPdf& other, const char* name=nullptr) ;
0058   TObject* clone(const char* newname=nullptr) const override { return new RooProdPdf(*this,newname) ; }
0059   ~RooProdPdf() override ;
0060 
0061   bool forceAnalyticalInt(const RooAbsArg& dep) const override ;
0062   Int_t getAnalyticalIntegralWN(RooArgSet& allVars, RooArgSet& numVars, const RooArgSet* normSet, const char* rangeName=nullptr) const override ;
0063   double analyticalIntegralWN(Int_t code, const RooArgSet* normSet, const char* rangeName=nullptr) const override ;
0064   bool selfNormalized() const override { return _selfNorm ; }
0065 
0066   ExtendMode extendMode() const override ;
0067   double expectedEvents(const RooArgSet* nset) const override ;
0068   std::unique_ptr<RooAbsReal> createExpectedEventsFunc(const RooArgSet* nset) const override;
0069 
0070   const RooArgList& pdfList() const { return _pdfList ; }
0071 
0072   void addPdfs(RooAbsCollection const& pdfs);
0073   void removePdfs(RooAbsCollection const& pdfs);
0074 
0075   Int_t getGenerator(const RooArgSet& directVars, RooArgSet &generateVars, bool staticInitOK=true) const override;
0076   void initGenerator(Int_t code) override ;
0077   void generateEvent(Int_t code) override;
0078   bool isDirectGenSafe(const RooAbsArg& arg) const override ;
0079 
0080   // Constraint management
0081   RooArgSet* getConstraints(const RooArgSet& observables, RooArgSet const& constrainedParams, RooArgSet &pdfParams) const override ;
0082 
0083   std::list<double>* plotSamplingHint(RooAbsRealLValue& obs, double xlo, double xhi) const override ;
0084   std::list<double>* binBoundaries(RooAbsRealLValue& /*obs*/, double /*xlo*/, double /*xhi*/) const override ;
0085   bool isBinnedDistribution(const RooArgSet& obs) const override  ;
0086 
0087   void printMetaArgs(std::ostream& os) const override ;
0088 
0089   void selectNormalizationRange(const char* rangeName=nullptr, bool force=false) override ;
0090   void fixRefRange(const char* rangeName) ;
0091 
0092   void setSelfNormalized(bool flag) { _selfNorm = flag ; }
0093   void setDefNormSet(const RooArgSet& nset) { _defNormSet.removeAll() ; _defNormSet.addClone(nset) ; }
0094 
0095 
0096   bool redirectServersHook(const RooAbsCollection& /*newServerList*/, bool /*mustReplaceAll*/, bool /*nameChange*/, bool /*isRecursive*/) override ;
0097 
0098   RooArgSet* getConnectedParameters(const RooArgSet& observables) const ;
0099 
0100   RooArgSet* findPdfNSet(RooAbsPdf const& pdf) const ;
0101 
0102   std::unique_ptr<RooAbsArg> compileForNormSet(RooArgSet const &normSet, RooFit::Detail::CompileContext & ctx) const override;
0103 
0104   // The cache object. Internal, do not use.
0105   class CacheElem final : public RooAbsCacheElement {
0106   public:
0107     CacheElem() : _isRearranged(false) { }
0108     // Payload
0109     RooArgList _partList ;
0110     RooArgList _numList ;
0111     RooArgList _denList ;
0112     RooArgList _ownedList ;
0113     std::vector<std::unique_ptr<RooArgSet>> _normList;
0114     bool _isRearranged ;
0115     std::unique_ptr<RooAbsReal> _rearrangedNum{};
0116     std::unique_ptr<RooAbsReal> _rearrangedDen{};
0117     // Cache management functions
0118     RooArgList containedArgs(Action) override ;
0119     void printCompactTreeHook(std::ostream&, const char *, Int_t, Int_t) override ;
0120     void writeToStream(std::ostream& os) const ;
0121   } ;
0122 
0123 private:
0124 
0125   std::unique_ptr<RooArgSet> fillNormSetForServer(RooArgSet const& normSet, RooAbsArg const& server) const;
0126 
0127   double evaluate() const override ;
0128 
0129   std::unique_ptr<RooAbsReal> makeCondPdfRatioCorr(RooAbsReal& term, const RooArgSet& termNset, const RooArgSet& termImpSet, const char* normRange, const char* refRange) const ;
0130 
0131   void getParametersHook(const RooArgSet* /*nset*/, RooArgSet* /*list*/, bool stripDisconnected) const override ;
0132 
0133   void initializeFromCmdArgList(const RooArgSet& fullPdfSet, const RooLinkedList& l) ;
0134 
0135   struct Factorized {
0136      ~Factorized();
0137 
0138      RooArgSet *termNormDeps(int i) const { return static_cast<RooArgSet*>(norms.At(i)); }
0139      RooArgSet *termIntDeps(int i) const { return static_cast<RooArgSet*>(ints.At(i)); }
0140      RooArgSet *termImpDeps(int i) const { return static_cast<RooArgSet*>(imps.At(i)); }
0141      RooArgSet *termCrossDeps(int i) const { return static_cast<RooArgSet*>(cross.At(i)); }
0142 
0143      RooLinkedList terms;
0144      RooLinkedList norms;
0145      RooLinkedList imps;
0146      RooLinkedList ints;
0147      RooLinkedList cross;
0148   };
0149 
0150   void factorizeProduct(const RooArgSet& normSet, const RooArgSet& intSet, Factorized &factorized) const;
0151   std::string makeRGPPName(const char* pfx, const RooArgSet& term, const RooArgSet& iset, const RooArgSet& nset, const char* isetRangeName) const ;
0152   void groupProductTerms(std::list<std::vector<RooArgSet*>>& groupedTerms, RooArgSet& outerIntDeps, Factorized const &factorized) const;
0153 
0154 
0155 
0156   Int_t getPartIntList(const RooArgSet* nset, const RooArgSet* iset, const char* isetRangeName=nullptr) const ;
0157 
0158   struct ProcessProductTermOutput {
0159     bool isOwned = false;
0160     RooAbsReal* x0 = nullptr;
0161     std::unique_ptr<RooAbsReal> x1;
0162     std::unique_ptr<RooAbsReal> x2;
0163   };
0164 
0165   ProcessProductTermOutput processProductTerm(const RooArgSet* nset, const RooArgSet* iset, const char* isetRangeName,
0166                      const RooArgSet* term,const RooArgSet& termNSet, const RooArgSet& termISet,
0167                      bool forceWrap=false) const ;
0168 
0169 
0170   CacheMode canNodeBeCached() const override { return RooAbsArg::NotAdvised ; } ;
0171   void setCacheAndTrackHints(RooArgSet&) override ;
0172 
0173   std::unique_ptr<CacheElem> createCacheElem(const RooArgSet* nset, const RooArgSet* iset, const char* isetRangeName=nullptr) const;
0174 
0175   mutable RooObjCacheManager _cacheMgr ; //! The cache manager
0176 
0177   CacheElem* getCacheElem(RooArgSet const* nset) const ;
0178   void rearrangeProduct(CacheElem&) const;
0179   std::unique_ptr<RooAbsReal> specializeIntegral(RooAbsReal& orig, const char* targetRangeName) const ;
0180   std::unique_ptr<RooAbsReal> specializeRatio(RooFormulaVar& input, const char* targetRangeName) const ;
0181   double calculate(const RooProdPdf::CacheElem& cache, bool verbose=false) const ;
0182   void doEvalImpl(RooAbsArg const* caller, const RooProdPdf::CacheElem &cache, RooFit::EvalContext &) const;
0183 
0184 
0185   friend class RooProdGenContext ;
0186   friend class RooFit::Detail::RooFixedProdPdf ;
0187   RooAbsGenContext* genContext(const RooArgSet &vars, const RooDataSet *prototype=nullptr,
0188                                   const RooArgSet *auxProto=nullptr, bool verbose= false) const override ;
0189 
0190 
0191   mutable RooAICRegistry _genCode ; ///<! Registry of composite direct generator codes
0192 
0193   double _cutOff = 0.0;       ///<  Cutoff parameter for running product
0194   RooListProxy _pdfList ;  ///<  List of PDF components
0195   std::vector<std::unique_ptr<RooArgSet>> _pdfNSetList ; ///< List of PDF component normalization sets
0196   Int_t _extendedIndex = -1; ///<  Index of extended PDF (if any)
0197 
0198   void useDefaultGen(bool flag=true) { _useDefaultGen = flag ; }
0199   bool _useDefaultGen = false; ///< Use default or distributed event generator
0200 
0201   mutable TNamed* _refRangeName = nullptr; ///< Reference range name for interpretation of conditional products
0202 
0203   bool _selfNorm = true; ///< Is self-normalized
0204   RooArgSet _defNormSet ; ///< Default normalization set
0205 
0206 private:
0207 
0208 
0209 
0210   ClassDefOverride(RooProdPdf,6) // PDF representing a product of PDFs
0211 };
0212 
0213 namespace RooFit {
0214 namespace Detail {
0215 
0216 /// A RooProdPdf with a fixed normalization set can be replaced by this class.
0217 /// Its purpose is to provide the right client-server interface for the
0218 /// evaluation of RooProdPdf cache elements that were created for a given
0219 /// normalization set.
0220 class RooFixedProdPdf : public RooAbsPdf {
0221 public:
0222    RooFixedProdPdf(std::unique_ptr<RooProdPdf> &&prodPdf, RooArgSet const &normSet);
0223    RooFixedProdPdf(const RooFixedProdPdf &other, const char *name = nullptr);
0224 
0225    inline TObject *clone(const char *newname) const override { return new RooFixedProdPdf(*this, newname); }
0226 
0227    inline bool selfNormalized() const override { return true; }
0228 
0229    inline bool canComputeBatchWithCuda() const override { return true; }
0230 
0231    inline void doEval(RooFit::EvalContext &ctx) const override { _prodPdf->doEvalImpl(this, *_cache, ctx); }
0232 
0233    inline ExtendMode extendMode() const override { return _prodPdf->extendMode(); }
0234    inline double expectedEvents(const RooArgSet * /*nset*/) const override
0235    {
0236       return _prodPdf->expectedEvents(&_normSet);
0237    }
0238    inline std::unique_ptr<RooAbsReal> createExpectedEventsFunc(const RooArgSet * /*nset*/) const override
0239    {
0240       return _prodPdf->createExpectedEventsFunc(&_normSet);
0241    }
0242 
0243    // Analytical Integration handling
0244    inline bool forceAnalyticalInt(const RooAbsArg &dep) const override { return _prodPdf->forceAnalyticalInt(dep); }
0245    inline Int_t getAnalyticalIntegralWN(RooArgSet &allVars, RooArgSet &analVars, const RooArgSet *normSet,
0246                                         const char *rangeName = nullptr) const override
0247    {
0248       return _prodPdf->getAnalyticalIntegralWN(allVars, analVars, normSet, rangeName);
0249    }
0250    inline Int_t
0251    getAnalyticalIntegral(RooArgSet &allVars, RooArgSet &numVars, const char *rangeName = nullptr) const override
0252    {
0253       return _prodPdf->getAnalyticalIntegral(allVars, numVars, rangeName);
0254    }
0255    inline double analyticalIntegralWN(Int_t code, const RooArgSet *normSet, const char *rangeName) const override
0256    {
0257       return _prodPdf->analyticalIntegralWN(code, normSet, rangeName);
0258    }
0259    inline double analyticalIntegral(Int_t code, const char *rangeName = nullptr) const override
0260    {
0261       return _prodPdf->analyticalIntegral(code, rangeName);
0262    }
0263 
0264    RooProdPdf::CacheElem const &cache() const { return *_cache; }
0265 
0266 private:
0267    void initialize();
0268 
0269    inline double evaluate() const override { return _prodPdf->calculate(*_cache); }
0270 
0271    RooArgSet _normSet;
0272    std::unique_ptr<RooProdPdf::CacheElem> _cache;
0273    RooSetProxy _servers;
0274    std::unique_ptr<RooProdPdf> _prodPdf;
0275 
0276    ClassDefOverride(RooFit::Detail::RooFixedProdPdf, 0);
0277 };
0278 
0279 } // namespace Detail
0280 } // namespace RooFit
0281 
0282 #endif