Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
HistFactoryImpl.cxx
Go to the documentation of this file.
1/*
2 * Project: RooFit
3 * Authors:
4 * Jonas Rembser, CERN 2023
5 *
6 * Copyright (c) 2023, CERN
7 *
8 * Redistribution and use in source and binary forms,
9 * with or without modification, are permitted according to the terms
10 * listed in LICENSE (http://roofit.sourceforge.net/license.txt)
11 */
12
14
16
17#include <RooConstVar.h>
18#include <RooGaussian.h>
19#include <RooMsgService.h>
20#include <RooPoisson.h>
21#include <RooProduct.h>
22#include <RooRealVar.h>
23
24namespace RooStats {
25namespace HistFactory {
26namespace Detail {
27
28/**
29 * \brief Configure constrained gamma parameters for fitting.
30 *
31 * This function configures constrained gamma parameters for fitting. If a
32 * given relative sigma is less than or equal to zero or below a threshold, the
33 * gamma parameter is set to be constant. The function also sets reasonable
34 * ranges for the gamma parameter and provides a reasonable starting point for
35 * pre-fit errors.
36 *
37 * @param gammas The gamma parameters to be configured.
38 * @param sigmaRel The relative sigma values to be used for configuring the
39 * limits and errors.
40 * @param minSigma The minimum relative sigma threshold. If a relative sigma is
41 * below this threshold, the gamma parameter is set to be
42 * constant.
43 */
44void configureConstrainedGammas(RooArgList const &gammas, std::span<const double> relSigmas, double minSigma)
45{
46 assert(gammas.size() == relSigmas.size());
47
48 for (std::size_t i = 0; i < gammas.size(); ++i) {
49 auto &gamma = *static_cast<RooRealVar *>(gammas.at(i));
50 double sigmaRel = relSigmas[i];
51
52 // If the sigma is zero, the parameter might as well be constant
53 if (sigmaRel <= 0) {
54 gamma.setConstant(true);
55 continue;
56 }
57
58 // Set reasonable ranges
59 gamma.setMax(1. + 5. * sigmaRel);
60 gamma.setMin(0.);
61
62 // Give reasonable starting point for pre-fit errors by setting it to the
63 // absolute sigma Mostly useful for pre-fit plotting.
64 // Note: in commit 2129c4d920 "[HF] Reduce verbosity of HistFactory."
65 // from 2020, there was a check added to do this only for Gaussian
66 // constrained parameters and for Poisson constrained parameters if they
67 // are stat errors without any justification. In the ROOT 6.30
68 // development cycle, this check got removed again to cause less surprise
69 // to the user.
70 gamma.setError(sigmaRel);
71
72 // If the sigma value is less than a supplied threshold, set the variable to
73 // constant
74 if (sigmaRel < minSigma) {
75 oocxcoutW(nullptr, HistFactory)
76 << "Warning: relative sigma " << sigmaRel << " for \"" << gamma.GetName() << "\" falls below threshold of "
77 << minSigma << ". Setting: " << gamma.GetName() << " to constant" << std::endl;
78 gamma.setConstant(true);
79 }
80 }
81}
82
83// Take a RooArgList of RooAbsReals and create N constraint terms (one for
84// each gamma) whose relative uncertainty is the value of the ith RooAbsReal
86 std::span<const double> relSigmas, double minSigma,
88{
90
91 // Check that there are N elements in the RooArgList
92 if (relSigmas.size() != paramSet.size()) {
93 std::cout << "Error: In createGammaConstraints, encountered bad number of relative sigmas" << std::endl;
94 std::cout << "Given vector with " << relSigmas.size() << " bins,"
95 << " but require exactly " << paramSet.size() << std::endl;
96 throw hf_exc();
97 }
98
99 configureConstrainedGammas(paramSet, relSigmas, minSigma);
100
101 for (std::size_t i = 0; i < paramSet.size(); ++i) {
102
103 RooRealVar &gamma = static_cast<RooRealVar &>(paramSet[i]);
104
105 oocxcoutI(nullptr, HistFactory)
106 << "Creating constraint for: " << gamma.GetName() << ". Type of constraint: " << type << std::endl;
107
108 const double sigmaRel = relSigmas[i];
109
110 // If the sigma is <= 0,
111 // do cont create the term
112 if (sigmaRel <= 0) {
113 oocxcoutI(nullptr, HistFactory)
114 << "Not creating constraint term for " << gamma.GetName() << " because sigma = " << sigmaRel
115 << " (sigma<=0)"
116 << " (bin number = " << i << ")" << std::endl;
117 continue;
118 }
119
120 // Make Constraint Term
121 std::string constrName = std::string(gamma.GetName()) + "_constraint";
122 std::string nomName = std::string("nom_") + gamma.GetName();
123
124 if (type == Constraint::Gaussian) {
125
126 // Type 1 : RooGaussian
127
128 // Make sigma
129 std::string sigmaName = std::string(gamma.GetName()) + "_sigma";
130 auto constrSigma = std::make_unique<RooConstVar>(sigmaName.c_str(), sigmaName.c_str(), sigmaRel);
131
132 // Make "observed" value
133 auto constrNom = std::make_unique<RooRealVar>(nomName.c_str(), nomName.c_str(), 1.0, 0, 10);
134 constrNom->setConstant(true);
135
136 // Make the constraint:
137 auto term = std::make_unique<RooGaussian>(constrName.c_str(), constrName.c_str(), *constrNom, gamma, *constrSigma);
138
139 out.globalObservables.push_back(constrNom.get());
140
141 term->addOwnedComponents(std::move(constrSigma));
142 term->addOwnedComponents(std::move(constrNom));
143
144 out.constraints.emplace_back(std::move(term));
145 } else if (type == Constraint::Poisson) {
146
147 // this is correct Poisson equivalent to a Gaussian with mean 1 and stdev sigma
148 const double tau = 1. / (sigmaRel * sigmaRel);
149
150 // Make nominal "observed" value
151 auto constrNom = std::make_unique<RooRealVar>(nomName.c_str(), nomName.c_str(), tau);
152 constrNom->setMin(0);
153 constrNom->setConstant(true);
154
155 // Make the scaling term
156 std::string scalingName = std::string(gamma.GetName()) + "_tau";
157 auto poissonScaling = std::make_unique<RooConstVar>(scalingName.c_str(), scalingName.c_str(), tau);
158
159 // Make mean for scaled Poisson
160 std::string poisMeanName = std::string(gamma.GetName()) + "_poisMean";
161 auto constrMean = std::make_unique<RooProduct>(poisMeanName.c_str(), poisMeanName.c_str(), gamma, *poissonScaling);
162
163 // Type 2 : RooPoisson
164 auto term = std::make_unique<RooPoisson>(constrName.c_str(), constrName.c_str(), *constrNom, *constrMean);
165 term->setNoRounding(true);
166
167 out.globalObservables.push_back(constrNom.get());
168
169 term->addOwnedComponents(std::move(poissonScaling));
170 term->addOwnedComponents(std::move(constrMean));
171 term->addOwnedComponents(std::move(constrNom));
172
173 out.constraints.emplace_back(std::move(term));
174 } else {
175
176 std::cout << "Error: Did not recognize Stat Error constraint term type: " << type
177 << " for : " << gamma.GetName() << std::endl;
178 throw hf_exc();
179 }
180 } // end loop over parameters
181
182 return out;
183}
184
185} // namespace Detail
186} // namespace HistFactory
187} // namespace RooStats
#define oocxcoutW(o, a)
#define oocxcoutI(o, a)
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 type
Storage_t::size_type size() const
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition RooArgList.h:22
RooAbsArg * at(Int_t idx) const
Return object at given index, or nullptr if index is out of range.
Definition RooArgList.h:110
RooRealVar represents a variable that can be changed from the outside.
Definition RooRealVar.h:37
void configureConstrainedGammas(RooArgList const &gammas, std::span< const double > relSigmas, double minSigma)
Configure constrained gamma parameters for fitting.
CreateGammaConstraintsOutput createGammaConstraints(RooArgList const &paramList, std::span< const double > relSigmas, double minSigma, Constraint::Type type)
Namespace for the RooStats classes.
Definition Asimov.h:19