Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-18 09:32:52

0001 // Authors: Stephan Hageboeck, CERN; Andrea Sciandra, SCIPP-UCSC/Atlas; Nov 2020
0002 
0003 /*****************************************************************************
0004  * Project: RooFit                                                           *
0005  * Package: RooFitCore                                                       *
0006  * Authors:                                                                  *
0007  *   WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu       *
0008  *   DK, David Kirkby,    UC Irvine,         dkirkby@uci.edu                 *
0009  *                                                                           *
0010  * Copyright (c) 2000-2018, Regents of the University of California          *
0011  *                          and Stanford University. All rights reserved.    *
0012  *                                                                           *
0013  * Redistribution and use in source and binary forms,                        *
0014  * with or without modification, are permitted according to the terms        *
0015  * listed in LICENSE (http://roofit.sourceforge.net/license.txt)             *
0016  *****************************************************************************/
0017 #ifndef ROO_BIN_SAMPLING__PDF
0018 #define ROO_BIN_SAMPLING__PDF
0019 
0020 #include "RooAbsReal.h"
0021 #include "RooTemplateProxy.h"
0022 #include "RooAbsPdf.h"
0023 
0024 #include "Math/Integrator.h"
0025 
0026 #include <memory>
0027 
0028 class RooBinSamplingPdf : public RooAbsPdf {
0029 public:
0030 
0031   RooBinSamplingPdf() { };
0032   RooBinSamplingPdf(const char *name, const char *title, RooAbsRealLValue& observable, RooAbsPdf& inputPdf,
0033       double epsilon = 1.E-4);
0034 
0035   RooBinSamplingPdf(const RooBinSamplingPdf& other, const char* name = nullptr);
0036 
0037   TObject* clone(const char* newname=nullptr) const override {
0038     return new RooBinSamplingPdf(*this, newname);
0039   }
0040 
0041   // Analytical Integration handling
0042   bool forceAnalyticalInt(const RooAbsArg& dep) const override {
0043     return _pdf->forceAnalyticalInt(dep);
0044   }
0045   /// Forwards to the PDF's implementation.
0046   Int_t getAnalyticalIntegralWN(RooArgSet& allVars, RooArgSet& analVars, const RooArgSet* normSet,
0047       const char* rangeName=nullptr) const override {
0048     return _pdf->getAnalyticalIntegralWN(allVars, analVars, normSet, rangeName);
0049   }
0050   /// Forwards to the PDF's implementation.
0051   Int_t getAnalyticalIntegral(RooArgSet& allVars, RooArgSet& numVars,
0052       const char* rangeName=nullptr) const override {
0053     return _pdf->getAnalyticalIntegral(allVars, numVars, rangeName);
0054   }
0055   /// Forwards to the PDF's implementation.
0056   double analyticalIntegralWN(Int_t code, const RooArgSet* normSet, const char* rangeName) const override {
0057     return _pdf->analyticalIntegralWN(code, normSet, rangeName);
0058   }
0059   /// Forwards to the PDF's implementation.
0060   double analyticalIntegral(Int_t code, const char* rangeName=nullptr) const override {
0061     return _pdf->analyticalIntegral(code, rangeName);
0062   }
0063   /// Forwards to the PDF's implementation.
0064   std::unique_ptr<RooAbsReal> createExpectedEventsFunc(const RooArgSet *nset) const override {
0065     return _pdf->createExpectedEventsFunc(nset);
0066   }
0067 
0068   /// Forwards to the PDF's implementation.
0069   bool selfNormalized() const override { return true; }
0070 
0071   /// Forwards to the PDF's implementation.
0072   RooFit::OwningPtr<RooAbsReal> createIntegral(const RooArgSet& iset,
0073                              const RooArgSet* nset=nullptr,
0074                              const RooNumIntConfig* cfg=nullptr,
0075                              const char* rangeName=nullptr) const override {
0076     return _pdf->createIntegral(iset, nset, cfg, rangeName);
0077   }
0078 
0079   ExtendMode extendMode() const override { return _pdf->extendMode(); }
0080   double expectedEvents(const RooArgSet* nset) const override { return _pdf->expectedEvents(nset); }
0081 
0082   /// Forwards to the PDF's implementation.
0083   Int_t getGenerator(const RooArgSet& directVars, RooArgSet& generateVars, bool staticInitOK = true) const override {
0084     return _pdf->getGenerator(directVars, generateVars, staticInitOK);
0085   }
0086   /// Forwards to the PDF's implementation.
0087   void initGenerator(Int_t code) override { _pdf->initGenerator(code); }
0088   /// Forwards to the PDF's implementation.
0089   void generateEvent(Int_t code) override { _pdf->generateEvent(code); }
0090   /// Forwards to the PDF's implementation.
0091   bool isDirectGenSafe(const RooAbsArg& arg) const override { return _pdf->isDirectGenSafe(arg); }
0092 
0093 
0094   // Hints for optimized brute-force sampling
0095   Int_t getMaxVal(const RooArgSet& vars) const override { return _pdf->getMaxVal(vars); }
0096   double maxVal(Int_t code) const override { return _pdf->maxVal(code); }
0097   Int_t minTrialSamples(const RooArgSet& arGenObs) const override { return _pdf->minTrialSamples(arGenObs); }
0098 
0099   // Plotting and binning hints
0100   /// Returns true, since this PDF is meant to be binned.
0101   bool isBinnedDistribution(const RooArgSet& /*obs*/) const override { return true; }
0102   std::list<double>* binBoundaries(RooAbsRealLValue& obs, double xlo, double xhi) const override;
0103   std::list<double>* plotSamplingHint(RooAbsRealLValue& obs, double xlo, double xhi) const override;
0104 
0105   std::unique_ptr<ROOT::Math::IntegratorOneDim>& integrator() const;
0106 
0107   static std::unique_ptr<RooAbsPdf> create(RooAbsPdf& pdf, RooAbsData const &data, double precision);
0108 
0109   double epsilon() const { return _relEpsilon; }
0110   const RooAbsPdf& pdf() const { return _pdf.arg(); }
0111   const RooAbsReal& observable() const { return _observable.arg(); }
0112 
0113   void doEval(RooFit::EvalContext &) const override;
0114 
0115 protected:
0116   double evaluate() const override;
0117   std::span<const double> binBoundaries() const;
0118 
0119 private:
0120   template<typename Func>
0121   friend class ROOT::Math::WrappedFunction;
0122   // Call operator for our internal integrator.
0123   double operator()(double x) const;
0124   double integrate(const RooArgSet* normSet, double low, double high) const;
0125 
0126 
0127   RooTemplateProxy<RooAbsPdf> _pdf;
0128   RooTemplateProxy<RooAbsRealLValue> _observable;
0129   double _relEpsilon{1.E-4}; ///< Default integrator precision.
0130 
0131   mutable std::unique_ptr<ROOT::Math::IntegratorOneDim> _integrator{nullptr}; ///<! Integrator used to sample bins.
0132   mutable std::vector<double> _binBoundaries; ///<! Workspace to store data for bin sampling
0133 
0134   ClassDefOverride(RooBinSamplingPdf,1)
0135 };
0136 
0137 #endif