Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
hf001_example.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_histfactory
3/// A ROOT script demonstrating an example of writing and fitting a HistFactory model using C++ only.
4///
5/// \macro_image
6/// \macro_code
7/// \macro_output
8///
9/// \author George Lewis
10
13
14#include <TFile.h>
15#include <TROOT.h>
16
17void hf001_example()
18{
19 std::string InputFile = "./data/example.root";
20 // in case the file is not found
21 bool bfile = gSystem->AccessPathName(InputFile.c_str());
22 if (bfile) {
23 std::cout << "Input file is not found - run prepareHistFactory script " << std::endl;
24 gROOT->ProcessLine(".! prepareHistFactory .");
25 bfile = gSystem->AccessPathName(InputFile.c_str());
26 if (bfile) {
27 std::cout << "Still no " << InputFile << ", giving up.\n";
28 exit(1);
29 }
30 }
31
32 // Create the measurement
33 RooStats::HistFactory::Measurement meas("meas", "meas");
34
35 meas.SetOutputFilePrefix("./results/example_UsingC");
36 meas.SetPOI("SigXsecOverSM");
37 meas.AddConstantParam("alpha_syst1");
38 meas.AddConstantParam("Lumi");
39
40 meas.SetLumi(1.0);
41 meas.SetLumiRelErr(0.10);
42 meas.SetBinHigh(2);
43
44 // Create a channel
45
46 RooStats::HistFactory::Channel chan("channel1");
47 chan.SetData("data", InputFile);
48 chan.SetStatErrorConfig(0.05, "Poisson");
49
50 // Now, create some samples
51
52 // Create the signal sample
53 RooStats::HistFactory::Sample signal("signal", "signal", InputFile);
54 signal.AddOverallSys("syst1", 0.95, 1.05);
55 signal.AddNormFactor("SigXsecOverSM", 1, 0, 3);
56 chan.AddSample(signal);
57
58 // Background 1
59 RooStats::HistFactory::Sample background1("background1", "background1", InputFile);
60 background1.ActivateStatError("background1_statUncert", InputFile);
61 background1.AddOverallSys("syst2", 0.95, 1.05);
62 chan.AddSample(background1);
63
64 // Background 1
65 RooStats::HistFactory::Sample background2("background2", "background2", InputFile);
66 background2.ActivateStatError();
67 background2.AddOverallSys("syst3", 0.95, 1.05);
68 chan.AddSample(background2);
69
70 // Done with this channel
71 // Add it to the measurement:
72 meas.AddChannel(chan);
73
74 // Collect the histograms from their files,
75 // print some output,
76 meas.CollectHistograms();
77 meas.PrintTree();
78
79 // One can print XML code to an output directory:
80 // meas.PrintXML( "xmlFromCCode", meas.GetOutputFilePrefix() );
81
82 // Now, do the measurement
83 std::unique_ptr<RooWorkspace> ws{MakeModelAndMeasurementFast(meas)};
84
85 RooStats::ModelConfig *modelConfig = static_cast<RooStats::ModelConfig *>(ws->obj("ModelConfig"));
86
87 // Get probability density function and parameters list from model
88 RooAbsPdf *pdf = modelConfig->GetPdf();
89 RooArgSet globalObservables{*modelConfig->GetGlobalObservables()};
90
91 // Perform the fit using Minos to get the correct asymmetric uncertainties
92 using namespace RooFit;
93 std::unique_ptr<RooFitResult> result{
94 pdf->fitTo(*ws->data("obsData"), Save(), PrintLevel(-1), GlobalObservables(globalObservables), Minos(true))};
95
96 // Getting list of Parameters of Interest and getting first from them
97 RooRealVar *poi = static_cast<RooRealVar *>(modelConfig->GetParametersOfInterest()->first());
98
99 std::unique_ptr<RooAbsReal> nll{pdf->createNLL(*ws->data("obsData"))};
100 std::unique_ptr<RooAbsReal> profile{nll->createProfile(*poi)};
101
102 // frame for future plot
103 RooPlot *frame = poi->frame();
104
105 frame->SetTitle("");
106 frame->GetYaxis()->SetTitle("-log likelihood");
107 frame->GetXaxis()->SetTitle(poi->GetTitle());
108
109 TCanvas *profileLikelihoodCanvas = new TCanvas{"combined", "", 800, 600};
110
111 double xmin = poi->getMin();
112 double xmax = poi->getMax();
113 TLine *line = new TLine(xmin, .5, xmax, .5);
115 TLine *line90 = new TLine(xmin, 2.71 / 2., xmax, 2.71 / 2.);
116 line90->SetLineColor(kGreen);
117 TLine *line95 = new TLine(xmin, 3.84 / 2., xmax, 3.84 / 2.);
118 line95->SetLineColor(kGreen);
119 frame->addObject(line);
120 frame->addObject(line90);
121 frame->addObject(line95);
122
123 nll->plotOn(frame, ShiftToZero(), LineColor(kRed), LineStyle(kDashed));
124 profile->plotOn(frame);
125
126 frame->SetMinimum(0);
127 frame->SetMaximum(2.);
128
129 frame->Draw();
130
131 // Print fit results to console in verbose mode to see asymmetric uncertainties
132 result->Print("v");
133}
@ kRed
Definition Rtypes.h:66
@ kGreen
Definition Rtypes.h:66
@ kDashed
Definition TAttLine.h:48
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 result
float xmin
float xmax
#define gROOT
Definition TROOT.h:406
R__EXTERN TSystem * gSystem
Definition TSystem.h:561
RooAbsArg * first() const
Abstract interface for all probability density functions.
Definition RooAbsPdf.h:40
RooFit::OwningPtr< RooAbsReal > createNLL(RooAbsData &data, CmdArgs_t const &... cmdArgs)
Construct representation of -log(L) of PDF with given dataset.
Definition RooAbsPdf.h:163
RooFit::OwningPtr< RooFitResult > fitTo(RooAbsData &data, CmdArgs_t const &... cmdArgs)
Fit PDF to given dataset.
Definition RooAbsPdf.h:157
virtual double getMax(const char *name=nullptr) const
Get maximum of currently defined range.
virtual double getMin(const char *name=nullptr) const
Get minimum of currently defined range.
RooPlot * frame(const RooCmdArg &arg1, const RooCmdArg &arg2={}, const RooCmdArg &arg3={}, const RooCmdArg &arg4={}, const RooCmdArg &arg5={}, const RooCmdArg &arg6={}, const RooCmdArg &arg7={}, const RooCmdArg &arg8={}) const
Create a new RooPlot on the heap with a drawing frame initialized for this object,...
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:24
Plot frame and a container for graphics objects within that frame.
Definition RooPlot.h:45
void SetTitle(const char *name) override
Set the title of the RooPlot to 'title'.
Definition RooPlot.cxx:1243
void addObject(TObject *obj, Option_t *drawOptions="", bool invisible=false)
Add a generic object to this plot.
Definition RooPlot.cxx:366
TAxis * GetYaxis() const
Definition RooPlot.cxx:1264
void Draw(Option_t *options=nullptr) override
Draw this plot and all of the elements it contains.
Definition RooPlot.cxx:637
TAxis * GetXaxis() const
Definition RooPlot.cxx:1262
SetMaximum(ymax)
SetMinimum(ymin)
Variable that can be changed from the outside.
Definition RooRealVar.h:37
This class encapsulates all information for the statistical interpretation of one experiment.
Definition Channel.h:30
The RooStats::HistFactory::Measurement class can be used to construct a model by combining multiple R...
Definition Measurement.h:33
ModelConfig is a simple class that holds configuration information specifying how a model should be u...
Definition ModelConfig.h:35
const RooArgSet * GetGlobalObservables() const
get RooArgSet for global observables (return nullptr if not existing)
const RooArgSet * GetParametersOfInterest() const
get RooArgSet containing the parameter of interest (return nullptr if not existing)
RooAbsPdf * GetPdf() const
get model PDF (return nullptr if pdf has not been specified or does not exist)
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition TAttLine.h:40
The Canvas class.
Definition TCanvas.h:23
Use the TLine constructor to create a simple line.
Definition TLine.h:22
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition TNamed.cxx:164
const char * GetTitle() const override
Returns title of object.
Definition TNamed.h:48
virtual Bool_t AccessPathName(const char *path, EAccessMode mode=kFileExists)
Returns FALSE if one can access a file using the specified access mode.
Definition TSystem.cxx:1296
TLine * line
RooCmdArg Save(bool flag=true)
RooCmdArg GlobalObservables(Args_t &&... argsOrArgSet)
RooCmdArg Minos(bool flag=true)
RooCmdArg PrintLevel(Int_t code)
RooCmdArg ShiftToZero()
RooCmdArg LineColor(Color_t color)
RooCmdArg LineStyle(Style_t style)
double nll(double pdf, double weight, int binnedL, int doBinOffset)
Definition MathFuncs.h:358
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition JSONIO.h:26
RooFit::OwningPtr< RooWorkspace > MakeModelAndMeasurementFast(RooStats::HistFactory::Measurement &measurement, HistoToWorkspaceFactoryFast::Configuration const &cfg={})