Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
RooStatsUtils.h
Go to the documentation of this file.
1 // @(#)root/roostats:$Id$
2 // Author: Kyle Cranmer 28/07/2008
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2008, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #ifndef ROOSTATS_RooStatsUtils
13 #define ROOSTATS_RooStatsUtils
14 
15 #include "TMath.h"
16 
17 #include "TTree.h"
18 
19 #include "Math/DistFuncMathCore.h"
20 
21 #include "RooArgSet.h"
22 #include "RooRealVar.h"
23 #include "RooAbsCollection.h"
24 #include "RooStats/ModelConfig.h"
25 #include "RooProdPdf.h"
26 #include "RooDataSet.h"
27 
28 
29 /** \namespace RooStats
30  \ingroup Roostats
31 
32 Namespace for the RooStats classes
33 
34 All the classes of the %RooStats package are in the RooStats namespace.
35 In addition the namespace contain a set of utility functions.
36 
37 */
38 
39 namespace RooStats {
40  struct RooStatsConfig {
41  bool useLikelihoodOffset{false}; /// Offset the likelihood by passing RooFit::Offset to fitTo().
42  bool useEvalErrorWall{true}; /// Use the error wall RooFit::EvalErrorWall to drive the fitter away from disallowed parameter values.
43  };
44 
45  /// Retrieve the config object which can be used to set flags for things like offsetting the likelihood
46  /// or using the error wall for the minimiser.
47  RooStatsConfig& GetGlobalRooStatsConfig();
48 
49  /// returns one-sided significance corresponding to a p-value
50  inline Double_t PValueToSignificance(Double_t pvalue){
51  return ::ROOT::Math::normal_quantile_c(pvalue,1);
52  }
53 
54  /// returns p-value corresponding to a 1-sided significance
55  inline Double_t SignificanceToPValue(Double_t Z){
56  return ::ROOT::Math::normal_cdf_c(Z);
57  }
58 
59  /// Compute the Asimov Median significance for a Poisson process
60  /// with s = expected number of signal events, b = expected numner of background events
61  /// and optionally sigma_b = expected uncertainty of backgorund events
62  Double_t AsimovSignificance(Double_t s, Double_t b, Double_t sigma_b = 0.0 );
63 
64  inline void SetParameters(const RooArgSet* desiredVals, RooArgSet* paramsToChange){
65  *paramsToChange=*desiredVals ;
66  }
67 
68  inline void RemoveConstantParameters(RooArgSet* set){
69  RooArgSet constSet;
70  RooLinkedListIter it = set->iterator();
71  RooRealVar *myarg;
72  while ((myarg = (RooRealVar *)it.Next())) {
73  if(myarg->isConstant()) constSet.add(*myarg);
74  }
75  set->remove(constSet);
76  }
77 
78  inline void RemoveConstantParameters(RooArgList& set){
79  RooArgSet constSet;
80  RooLinkedListIter it = set.iterator();
81  RooRealVar *myarg;
82  while ((myarg = (RooRealVar *)it.Next())) {
83  if(myarg->isConstant()) constSet.add(*myarg);
84  }
85  set.remove(constSet);
86  }
87 
88  inline bool SetAllConstant(const RooAbsCollection &coll, bool constant = true) {
89  // utility function to set all variable constant in a collection
90  // (from G. Petrucciani)
91  bool changed = false;
92  RooLinkedListIter iter = coll.iterator();
93  for (RooAbsArg *a = (RooAbsArg *) iter.Next(); a != 0; a = (RooAbsArg *) iter.Next()) {
94  RooRealVar *v = dynamic_cast<RooRealVar *>(a);
95  if (v && (v->isConstant() != constant)) {
96  changed = true;
97  v->setConstant(constant);
98  }
99  }
100  return changed;
101  }
102 
103 
104  // assuming all values in set are RooRealVars, randomize their values
105  inline void RandomizeCollection(RooAbsCollection& set,
106  Bool_t randomizeConstants = kTRUE)
107  {
108  RooLinkedListIter it = set.iterator();
109  RooRealVar* var;
110 
111  // repeat loop to avoid calling isConstant for nothing
112  if (randomizeConstants) {
113  while ((var = (RooRealVar*)it.Next()) != NULL)
114  var->randomize();
115  }
116  else {
117  // exclude constants variables
118  while ((var = (RooRealVar*)it.Next()) != NULL)
119  if (!var->isConstant() )
120  var->randomize();
121  }
122 
123 
124  }
125 
126  void FactorizePdf(const RooArgSet &observables, RooAbsPdf &pdf, RooArgList &obsTerms, RooArgList &constraints);
127 
128  void FactorizePdf(RooStats::ModelConfig &model, RooAbsPdf &pdf, RooArgList &obsTerms, RooArgList &constraints);
129 
130  // extract constraint terms from pdf
131  RooAbsPdf * MakeNuisancePdf(RooAbsPdf &pdf, const RooArgSet &observables, const char *name);
132  RooAbsPdf * MakeNuisancePdf(const RooStats::ModelConfig &model, const char *name);
133  // remove constraints from pdf and return the unconstrained pdf
134  RooAbsPdf * MakeUnconstrainedPdf(RooAbsPdf &pdf, const RooArgSet &observables, const char *name = NULL);
135  RooAbsPdf * MakeUnconstrainedPdf(const RooStats::ModelConfig &model, const char *name = NULL);
136 
137  // Create a TTree with the given name and description. All RooRealVars in the RooDataSet are represented as branches that contain values of type Double_t.
138  TTree* GetAsTTree(TString name, TString desc, const RooDataSet& data);
139 
140  // useful function to print in one line the content of a set with their values
141  void PrintListContent(const RooArgList & l, std::ostream & os = std::cout);
142 
143  // function to set a global flag in RooStats to use NLL offset when performing nll computations
144  // Note that not all ROoStats tools implement this capabilities
145  void UseNLLOffset(bool on);
146 
147  // function returning if the flag to check if the flag to use NLLOffset is set
148  bool IsNLLOffset();
149 
150 
151 
152 }
153 
154 
155 #endif