Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
StandardTestStatDistributionDemo.py
Go to the documentation of this file.
1# \file
2# \ingroup tutorial_roostats
3# \notebook
4# StandardTestStatDistributionDemo.C
5#
6# This simple script plots the sampling distribution of the profile likelihood
7# ratio test statistic based on the input Model File. To do this one needs to
8# specify the value of the parameter of interest that will be used for evaluating
9# the test statistic and the value of the parameters used for generating the toy data.
10# In this case, it uses the upper-limit estimated from the ProfileLikleihoodCalculator,
11# which assumes the asymptotic chi-square distribution for -2 log profile likelihood ratio.
12# Thus, the script is handy for checking to see if the asymptotic approximations are valid.
13# To aid, that comparison, the script overlays a chi-square distribution as well.
14# The most common parameter of interest is a parameter proportional to the signal rate,
15# and often that has a lower-limit of 0, which breaks the standard chi-square distribution.
16# Thus the script allows the parameter to be negative so that the overlay chi-square is
17# the correct asymptotic distribution.
18#
19# \macro_image
20# \macro_output
21# \macro_code
22#
23# \author Kyle Cranmer (C++ version), and P. P. (Python translation)
24
25
26import ROOT
27
28
29# -------------------------------------------------------
30# The actual macro
31
32
34 infile="", workspaceName="combined", modelConfigName="ModelConfig", dataName="obsData"
35):
36
37 # the number of toy MC used to generate the distribution
38 nToyMC = 1000
39 # The parameter below is needed for asymptotic distribution to be chi-square,
40 # but set to false if your model is not numerically stable if mu<0
41 allowNegativeMu = True
42
43 # -------------------------------------------------------
44 # First part is just to access a user-defined file
45 # or create the standard example file if it doesn't exist
46 filename = ""
47 if infile == "":
48 filename = "results/example_combined_GaussExample_model.root"
49 fileExist = not ROOT.gSystem.AccessPathName(filename) # note opposite return code
50 print("does the .root file exists?: ", fileExist)
51 # if file does not exists generate with histfactory
52 if not fileExist:
53 # Normally this would be run on the command line
54 print(f"will run standard hist2workspace example")
55 ROOT.gROOT.ProcessLine(".! prepareHistFactory .")
56 ROOT.gROOT.ProcessLine(".! hist2workspace config/example.xml")
57 print(f"\n\n---------------------")
58 print(f"Done creating example input")
59 print(f"---------------------\n\n")
60
61 else:
62 filename = infile
63
64 # Try to open the file
65 file = ROOT.TFile.Open(filename)
66
67 # if input file was specified but not found, quit
68 if not file:
69 print(f"StandardRooStatsDemoMacro: Input file {filename} is not found")
70 return
71
72 # -------------------------------------------------------
73 # Now get the data and workspace
74
75 # get the workspace out of the file
76 w = file.Get(workspaceName)
77 if not w:
78 print(f"workspace not found")
79 return
80
81 # get the modelConfig out of the file
82 mc = w[modelConfigName]
83
84 # get the modelConfig out of the file
85 data = w[dataName]
86
87 # make sure ingredients are found
88 if not data or not mc:
89 w.Print()
90 print(f"data or ModelConfig was not found")
91 return
92
93 mc.Print()
94 # -------------------------------------------------------
95 # Now find the upper limit based on the asymptotic results
96 firstPOI = mc.GetParametersOfInterest().first()
97 plc = ROOT.RooStats.ProfileLikelihoodCalculator(data, mc)
98 interval = plc.GetInterval()
99 plcUpperLimit = interval.UpperLimit(firstPOI)
100 del interval
101 print(f"\n\n--------------------------------------")
102 print("Will generate sampling distribution at ", firstPOI.GetName(), " = ", plcUpperLimit)
103 nPOI = mc.GetParametersOfInterest().getSize()
104 if nPOI > 1:
105 print(f"not sure what to do with other parameters of interest, but here are their values")
106 mc.GetParametersOfInterest().Print("v")
107
108 # -------------------------------------------------------
109 # create the test stat sampler
110 ts = ROOT.RooStats.ProfileLikelihoodTestStat(mc.GetPdf())
111
112 # to avoid effects from boundary and simplify asymptotic comparison, set min=-max
113 if allowNegativeMu:
114 firstPOI.setMin(-1 * firstPOI.getMax())
115
116 # temporary RooArgSet
117 poi = ROOT.RooArgSet()
118 poi.add(mc.GetParametersOfInterest())
119
120 # create and configure the ToyMCSampler
121 sampler = ROOT.RooStats.ToyMCSampler(ts, nToyMC)
122 sampler.SetPdf(mc.GetPdf())
123 sampler.SetObservables(mc.GetObservables())
124 sampler.SetGlobalObservables(mc.GetGlobalObservables())
125 if not mc.GetPdf().canBeExtended() and (data.numEntries() == 1):
126 print(f"tell it to use 1 event")
127 sampler.SetNEventsPerToy(1)
128
129 firstPOI.setVal(plcUpperLimit) # set POI value for generation
130 sampler.SetParametersForTestStat(mc.GetParametersOfInterest()) # set POI value for evaluation
131
132 firstPOI.setVal(plcUpperLimit)
133 allParameters = ROOT.RooArgSet()
134 allParameters.add(mc.GetParametersOfInterest())
135 allParameters.add(mc.GetNuisanceParameters())
136 allParameters.Print("v")
137
138 sampDist = sampler.GetSamplingDistribution(allParameters)
139 plot = ROOT.RooStats.SamplingDistPlot()
140 plot.AddSamplingDistribution(sampDist)
141 plot.GetTH1F(sampDist).GetYaxis().SetTitle(
142 "f(-log #lambda(#mu={:2f}) | #mu={:2f})".format(plcUpperLimit, plcUpperLimit)
143 )
144 plot.SetAxisTitle("-log #lambda(#mu={:2f})".format(plcUpperLimit))
145
146 c1 = ROOT.TCanvas("c1")
147 c1.SetLogy()
148 plot.Draw()
149 MIN = plot.GetTH1F(sampDist).GetXaxis().GetXmin()
150 MAX = plot.GetTH1F(sampDist).GetXaxis().GetXmax()
151
152 tmp_Form = "2*ROOT::Math::chisquared_pdf(2*x,{:f},0)".format(nPOI)
153 f = ROOT.TF1("f", tmp_Form, MIN, MAX)
154
155 f.Draw("same")
156 c1.Update()
157 c1.Draw()
158 c1.SaveAs("StandardTestStatDistributionDemo.png")
159
160
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t format
void Print(GNN_Data &d, std::string txt="")