Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-11 08:44:51

0001 // @(#)root/roostats:$Id$
0002 // Author: Kyle Cranmer, Lorenzo Moneta, Gregory Schott, Wouter Verkerke
0003 // Additional Contributions: Giovanni Petrucciani
0004 /*************************************************************************
0005  * Copyright (C) 1995-2008, Rene Brun and Fons Rademakers.               *
0006  * All rights reserved.                                                  *
0007  *                                                                       *
0008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
0009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
0010  *************************************************************************/
0011 
0012 #ifndef ROOSTATS_ProfileLikelihoodTestStat
0013 #define ROOSTATS_ProfileLikelihoodTestStat
0014 
0015 
0016 #include "Rtypes.h"
0017 
0018 #include "RooStats/TestStatistic.h"
0019 
0020 
0021 #include "RooRealVar.h"
0022 
0023 #include "Math/MinimizerOptions.h"
0024 
0025 #include "RooStats/RooStatsUtils.h"
0026 
0027 
0028 namespace RooStats {
0029 
0030   class ProfileLikelihoodTestStat : public TestStatistic{
0031 
0032      enum LimitType {twoSided, oneSided, oneSidedDiscovery};
0033 
0034    public:
0035       /// Proof constructor. Do not use.
0036       ProfileLikelihoodTestStat()
0037          : fStrategy(::ROOT::Math::MinimizerOptions::DefaultStrategy()),
0038            fTolerance(std::max(1., ::ROOT::Math::MinimizerOptions::DefaultTolerance())),
0039            fPrintLevel(::ROOT::Math::MinimizerOptions::DefaultPrintLevel()),
0040            fLOffset(RooStats::IsNLLOffset())
0041       {
0042       }
0043 
0044       ProfileLikelihoodTestStat(RooAbsPdf &pdf)
0045          : fPdf(&pdf),
0046            fStrategy(::ROOT::Math::MinimizerOptions::DefaultStrategy()),
0047            fTolerance(std::max(1., ::ROOT::Math::MinimizerOptions::DefaultTolerance())),
0048            fPrintLevel(::ROOT::Math::MinimizerOptions::DefaultPrintLevel()),
0049            fLOffset(RooStats::IsNLLOffset())
0050       {
0051          // avoid default tolerance to be too small (1. is default in RooMinimizer)
0052       }
0053 
0054      ~ProfileLikelihoodTestStat() override {
0055        if(fCachedBestFitParams) delete fCachedBestFitParams;
0056      }
0057 
0058      void SetOneSided(bool flag=true) {fLimitType = (flag ? oneSided : twoSided);}
0059      void SetOneSidedDiscovery(bool flag=true) {fLimitType = (flag ? oneSidedDiscovery : twoSided);}
0060      void SetSigned(bool flag=true) {fSigned = flag;}  // +/- t_mu instead of t_mu>0 with one-sided settings
0061 
0062      bool IsTwoSided() const { return fLimitType == twoSided; }
0063      bool IsOneSidedDiscovery() const { return fLimitType == oneSidedDiscovery; }
0064 
0065      static void SetAlwaysReuseNLL(bool flag);
0066 
0067      void SetReuseNLL(bool flag) { fReuseNll = flag ; }
0068      void SetLOffset(bool flag=true) { fLOffset = flag ; }
0069 
0070      void SetMinimizer(const char* minimizer){ fMinimizer=minimizer;}
0071      void SetStrategy(Int_t strategy){fStrategy=strategy;}
0072      void SetTolerance(double tol){fTolerance=tol;}
0073      void SetPrintLevel(Int_t printlevel){fPrintLevel=printlevel;}
0074 
0075      /// Main interface to evaluate the test statistic on a dataset
0076      double Evaluate(RooAbsData& data, RooArgSet& paramsOfInterest) override {
0077         return EvaluateProfileLikelihood(0, data, paramsOfInterest);
0078      }
0079 
0080      /// evaluate  the profile likelihood ratio (type = 0) or the minimum of likelihood (type=1) or the conditional LL (type = 2)
0081      virtual double EvaluateProfileLikelihood(int type, RooAbsData &data, RooArgSet & paramsOfInterest);
0082 
0083      virtual void EnableDetailedOutput( bool e=true, bool withErrorsAndPulls=false ) {
0084         fDetailedOutputEnabled = e;
0085         fDetailedOutputWithErrorsAndPulls = withErrorsAndPulls;
0086         fDetailedOutput = nullptr;
0087      }
0088      /// Returns detailed output. The value returned by this function is updated after each call to Evaluate().
0089      /// The returned RooArgSet contains the following:
0090      ///
0091      ///  - the minimum nll, fitstatus and convergence quality for each fit
0092      ///  - for each fit and for each non-constant parameter, the value, error and pull of the parameter are stored
0093      ///
0094      const RooArgSet* GetDetailedOutput(void) const override {
0095       return fDetailedOutput.get();
0096      }
0097 
0098      /// set the conditional observables which will be used when creating the NLL
0099      /// so the pdf's will not be normalized on the conditional observables when computing the NLL
0100      void SetConditionalObservables(const RooArgSet& set) override {fConditionalObs.removeAll(); fConditionalObs.add(set);}
0101 
0102      /// set the global observables which will be used when creating the NLL
0103      /// so the constraint pdf's will be normalized correctly on the global observables when computing the NLL
0104      void SetGlobalObservables(const RooArgSet& set) override {fGlobalObs.removeAll(); fGlobalObs.add(set);}
0105 
0106      virtual void SetVarName(const char* name) { fVarName = name; }
0107      const TString GetVarName() const override {return fVarName;}
0108 
0109      virtual RooAbsPdf * GetPdf() const { return fPdf; }
0110 
0111   private:
0112 
0113      std::unique_ptr<RooFitResult> GetMinNLL();
0114 
0115       RooAbsPdf* fPdf = nullptr;
0116       std::unique_ptr<RooAbsReal> fNll; //!
0117       const RooArgSet* fCachedBestFitParams = nullptr;
0118       RooAbsData* fLastData = nullptr;
0119       LimitType fLimitType = twoSided;
0120       bool fSigned = false;
0121 
0122       /// this will store a snapshot of the unconditional nuisance
0123       /// parameter fit.
0124       bool fDetailedOutputEnabled = false;
0125       bool fDetailedOutputWithErrorsAndPulls = false;
0126       std::unique_ptr<RooArgSet> fDetailedOutput; ///<!
0127       RooArgSet fConditionalObs;  ///< conditional observables
0128       RooArgSet fGlobalObs;       ///< global observables
0129 
0130       TString fVarName = "Profile Likelihood Ratio";
0131 
0132       static bool fgAlwaysReuseNll ;
0133       bool fReuseNll = false;
0134       TString fMinimizer;
0135       Int_t fStrategy;
0136       double fTolerance;
0137       Int_t fPrintLevel;
0138       bool fLOffset ;
0139 
0140    protected:
0141 
0142       ClassDefOverride(ProfileLikelihoodTestStat,10)   // implements the profile likelihood ratio as a test statistic to be used with several tools
0143    };
0144 }
0145 
0146 
0147 #endif