Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/root/RooStats/RooStatsUtils.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // @(#)root/roostats:$Id$
0002 // Author: Kyle Cranmer   28/07/2008
0003 
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_RooStatsUtils
0013 #define ROOSTATS_RooStatsUtils
0014 
0015 #include "Math/DistFuncMathCore.h"
0016 
0017 #include "RooArgSet.h"
0018 #include "RooRealVar.h"
0019 #include "RooAbsCollection.h"
0020 #include "RooStats/ModelConfig.h"
0021 #include "RooProdPdf.h"
0022 #include "RooDataSet.h"
0023 #include "RooAbsCategoryLValue.h"
0024 
0025 class TTree;
0026 
0027 
0028 /** \namespace RooStats
0029     \ingroup Roostats
0030 
0031 Namespace for the RooStats classes
0032 
0033 All the classes of the %RooStats package are in the RooStats namespace.
0034 In addition the namespace contain a set of utility functions.
0035 
0036 */
0037 
0038 namespace RooStats {
0039    struct RooStatsConfig {
0040       std::string useLikelihoodOffset = "none"; ///< Offset the likelihood by passing RooFit::Offset to fitTo().
0041       bool useEvalErrorWall{true};     ///< Use the error wall RooFit::EvalErrorWall to drive the fitter away from disallowed parameter values.
0042    };
0043 
0044    /// Retrieve the config object which can be used to set flags for things like offsetting the likelihood
0045    /// or using the error wall for the minimiser.
0046    RooStatsConfig& GetGlobalRooStatsConfig();
0047 
0048    /// returns one-sided significance corresponding to a p-value
0049    inline double PValueToSignificance(double pvalue){
0050       return ::ROOT::Math::normal_quantile_c(pvalue,1);
0051    }
0052 
0053    /// returns p-value corresponding to a 1-sided significance
0054    inline double SignificanceToPValue(double Z){
0055       return ::ROOT::Math::normal_cdf_c(Z);
0056    }
0057 
0058    /// Compute the Asimov Median significance for a Poisson process
0059    /// with s = expected number of signal events, b = expected number of background events
0060    /// and optionally sigma_b = expected uncertainty of background events
0061    double AsimovSignificance(double s, double b, double sigma_b = 0.0 );
0062 
0063    inline void SetParameters(const RooArgSet* desiredVals, RooArgSet* paramsToChange){
0064       paramsToChange->assign(*desiredVals) ;
0065    }
0066 
0067    inline void RemoveConstantParameters(RooArgSet* set){
0068       set->removeConstantParameters();
0069    }
0070 
0071    inline void RemoveConstantParameters(RooArgList& set){
0072       set.removeConstantParameters();
0073    }
0074 
0075    /// utility function to set all variable constant in a collection
0076    /// (from G. Petrucciani)
0077    inline bool SetAllConstant(const RooAbsCollection &coll, bool constant = true) {
0078       bool changed = false;
0079       for (auto *v : dynamic_range_cast<RooRealVar *>(coll)) {
0080          if (v && (v->isConstant() != constant)) {
0081             changed = true;
0082             v->setConstant(constant);
0083          }
0084       }
0085       return changed;
0086    }
0087 
0088 
0089    /// assuming all values in set are RooRealVars, randomize their values
0090    inline void RandomizeCollection(RooAbsCollection& set,
0091                                    bool randomizeConstants = true)
0092    {
0093 
0094       // repeat loop to avoid calling isConstant for nothing
0095       if (randomizeConstants) {
0096          for (auto *var : static_range_cast<RooRealVar *>(set))
0097             var->randomize();
0098       }
0099       else {
0100          // exclude constants variables
0101          for (auto *var : static_range_cast<RooRealVar *>(set)) {
0102             if (!var->isConstant() )
0103                var->randomize();
0104          }
0105       }
0106 
0107    }
0108 
0109    void FactorizePdf(const RooArgSet &observables, RooAbsPdf &pdf, RooArgList &obsTerms, RooArgList &constraints);
0110 
0111    void FactorizePdf(RooStats::ModelConfig &model, RooAbsPdf &pdf, RooArgList &obsTerms, RooArgList &constraints);
0112 
0113    /// extract constraint terms from pdf
0114    RooAbsPdf * MakeNuisancePdf(RooAbsPdf &pdf, const RooArgSet &observables, const char *name);
0115    RooAbsPdf * MakeNuisancePdf(const RooStats::ModelConfig &model, const char *name);
0116    /// remove constraints from pdf and return the unconstrained pdf
0117    RooAbsPdf * MakeUnconstrainedPdf(RooAbsPdf &pdf, const RooArgSet &observables, const char *name = nullptr);
0118    RooAbsPdf * MakeUnconstrainedPdf(const RooStats::ModelConfig &model, const char *name = nullptr);
0119 
0120    /// Create a TTree with the given name and description. All RooRealVars in the RooDataSet are represented as branches that contain values of type double.
0121    TTree* GetAsTTree(TString name, TString desc, const RooDataSet& data);
0122 
0123    /// useful function to print in one line the content of a set with their values
0124    void PrintListContent(const RooArgList & l, std::ostream & os = std::cout);
0125 
0126    /// Use an offset in NLL calculations.
0127    /// \deprecated Use SetNLLOffsetMode().
0128    inline void UseNLLOffset(bool on) {
0129       GetGlobalRooStatsConfig().useLikelihoodOffset = on ? "initial" : "none";
0130    }
0131 
0132    /// Function returning if the flag to check if the flag to use  NLLOffset is
0133    /// set.
0134    /// \deprecated Use NLLOffsetMode().
0135    inline bool IsNLLOffset() {
0136       return GetGlobalRooStatsConfig().useLikelihoodOffset == "initial";
0137    }
0138 
0139    /// Function to set a global flag in RooStats to use NLL offset when
0140    /// performing nll computations.
0141    /// \note Not all RooStats tools implement this capabilities.
0142    inline void SetNLLOffsetMode(std::string const& mode) {
0143       GetGlobalRooStatsConfig().useLikelihoodOffset = mode;
0144    }
0145 
0146    /// Test what offsetting mode RooStats should use by default.
0147    inline std::string const &NLLOffsetMode() {
0148       return GetGlobalRooStatsConfig().useLikelihoodOffset;
0149    }
0150 
0151 
0152    /// function that clones a workspace, copying all needed components and discarding all others
0153    RooWorkspace* MakeReducedWorkspace(RooWorkspace *oldWS, const char *newName, bool copySnapshots,
0154                                     const char *mcname, const char *newmcname, bool copyData=true);
0155 
0156 }
0157 
0158 
0159 #endif