Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-15 09:11:30

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       ProfileLikelihoodTestStat(RooAbsPdf &pdf)
0036          : fPdf(&pdf),
0037            fStrategy(::ROOT::Math::MinimizerOptions::DefaultStrategy()),
0038            fTolerance(std::max(1., ::ROOT::Math::MinimizerOptions::DefaultTolerance())),
0039            fPrintLevel(::ROOT::Math::MinimizerOptions::DefaultPrintLevel()),
0040            fLOffset(RooStats::NLLOffsetMode())
0041       {
0042          // avoid default tolerance to be too small (1. is default in RooMinimizer)
0043       }
0044 
0045      ~ProfileLikelihoodTestStat() override {
0046        if(fCachedBestFitParams) delete fCachedBestFitParams;
0047      }
0048 
0049      void SetOneSided(bool flag=true) {fLimitType = (flag ? oneSided : twoSided);}
0050      void SetOneSidedDiscovery(bool flag=true) {fLimitType = (flag ? oneSidedDiscovery : twoSided);}
0051      void SetSigned(bool flag=true) {fSigned = flag;}  // +/- t_mu instead of t_mu>0 with one-sided settings
0052 
0053      bool IsTwoSided() const { return fLimitType == twoSided; }
0054      bool IsOneSidedDiscovery() const { return fLimitType == oneSidedDiscovery; }
0055 
0056      static void SetAlwaysReuseNLL(bool flag);
0057 
0058      void SetReuseNLL(bool flag) { fReuseNll = flag ; }
0059      void SetLOffset(bool flag=true) { fLOffset = flag ? "initial" : "none"; }
0060      void SetLOffset(std::string const &mode) { fLOffset = mode; }
0061 
0062      void SetMinimizer(const char* minimizer){ fMinimizer=minimizer;}
0063      void SetStrategy(Int_t strategy){fStrategy=strategy;}
0064      void SetTolerance(double tol){fTolerance=tol;}
0065      void SetPrintLevel(Int_t printlevel){fPrintLevel=printlevel;}
0066 
0067      /// Main interface to evaluate the test statistic on a dataset
0068      double Evaluate(RooAbsData& data, RooArgSet& paramsOfInterest) override {
0069         return EvaluateProfileLikelihood(0, data, paramsOfInterest);
0070      }
0071 
0072      /// evaluate  the profile likelihood ratio (type = 0) or the minimum of likelihood (type=1) or the conditional LL (type = 2)
0073      virtual double EvaluateProfileLikelihood(int type, RooAbsData &data, RooArgSet & paramsOfInterest);
0074 
0075      virtual void EnableDetailedOutput( bool e=true, bool withErrorsAndPulls=false ) {
0076         fDetailedOutputEnabled = e;
0077         fDetailedOutputWithErrorsAndPulls = withErrorsAndPulls;
0078         fDetailedOutput = nullptr;
0079      }
0080      /// Returns detailed output. The value returned by this function is updated after each call to Evaluate().
0081      /// The returned RooArgSet contains the following:
0082      ///
0083      ///  - the minimum nll, fitstatus and convergence quality for each fit
0084      ///  - for each fit and for each non-constant parameter, the value, error and pull of the parameter are stored
0085      ///
0086      const RooArgSet* GetDetailedOutput(void) const override {
0087       return fDetailedOutput.get();
0088      }
0089 
0090      /// set the conditional observables which will be used when creating the NLL
0091      /// so the pdf's will not be normalized on the conditional observables when computing the NLL
0092      void SetConditionalObservables(const RooArgSet& set) override {fConditionalObs.removeAll(); fConditionalObs.add(set);}
0093 
0094      /// set the global observables which will be used when creating the NLL
0095      /// so the constraint pdf's will be normalized correctly on the global observables when computing the NLL
0096      void SetGlobalObservables(const RooArgSet& set) override {fGlobalObs.removeAll(); fGlobalObs.add(set);}
0097 
0098      virtual void SetVarName(const char* name) { fVarName = name; }
0099      const TString GetVarName() const override {return fVarName;}
0100 
0101      virtual RooAbsPdf * GetPdf() const { return fPdf; }
0102 
0103   private:
0104 
0105      std::unique_ptr<RooFitResult> GetMinNLL();
0106 
0107       RooAbsPdf* fPdf = nullptr;
0108       std::unique_ptr<RooAbsReal> fNll; //!
0109       const RooArgSet* fCachedBestFitParams = nullptr;
0110       RooAbsData* fLastData = nullptr;
0111       LimitType fLimitType = twoSided;
0112       bool fSigned = false;
0113 
0114       /// this will store a snapshot of the unconditional nuisance
0115       /// parameter fit.
0116       bool fDetailedOutputEnabled = false;
0117       bool fDetailedOutputWithErrorsAndPulls = false;
0118       std::unique_ptr<RooArgSet> fDetailedOutput; ///<!
0119       RooArgSet fConditionalObs;  ///< conditional observables
0120       RooArgSet fGlobalObs;       ///< global observables
0121 
0122       TString fVarName = "Profile Likelihood Ratio";
0123 
0124       static bool fgAlwaysReuseNll ;
0125       bool fReuseNll = false;
0126       TString fMinimizer;
0127       Int_t fStrategy;
0128       double fTolerance;
0129       Int_t fPrintLevel;
0130       std::string fLOffset;
0131 
0132       ClassDefOverride(ProfileLikelihoodTestStat,0)   // implements the profile likelihood ratio as a test statistic to be used with several tools
0133    };
0134 }
0135 
0136 
0137 #endif