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. Only the ones that
38 * are RooRealVars are configured: the gammas can also be
39 * functions, e.g. in models imported from HS3.
40 * @param relSigmas The relative sigma values to be used for configuring the
41 * limits and errors.
42 * @param minSigma The minimum relative sigma threshold. If a relative sigma is
43 * below this threshold, the gamma parameter is set to be
44 * constant.
45 */
46void configureConstrainedGammas(RooArgList const &gammas, std::span<const double> relSigmas, double minSigma)
47{
48 assert(gammas.size() == relSigmas.size());
49
50 for (std::size_t i = 0; i < gammas.size(); ++i) {
51 auto *gammaVar = dynamic_cast<RooRealVar *>(gammas.at(i));
52 if (!gammaVar) {
53 continue;
54 }
55 auto &gamma = *gammaVar;
56 double sigmaRel = relSigmas[i];
57
58 // If the sigma is zero, the parameter might as well be constant
59 if (sigmaRel <= 0) {
60 gamma.setConstant(true);
61 continue;
62 }
63
64 // Set reasonable ranges
65 gamma.setMax(1. + 5. * sigmaRel);
66 gamma.setMin(0.);
67 // Set initial error too
68 gamma.setError(sigmaRel);
69
70 // Give reasonable starting point for pre-fit errors by setting it to the
71 // absolute sigma Mostly useful for pre-fit plotting.
72 // Note: in commit 2129c4d920 "[HF] Reduce verbosity of HistFactory."
73 // from 2020, there was a check added to do this only for Gaussian
74 // constrained parameters and for Poisson constrained parameters if they
75 // are stat errors without any justification. In the ROOT 6.30
76 // development cycle, this check got removed again to cause less surprise
77 // to the user.
78 gamma.setError(sigmaRel);
79
80 // If the sigma value is less than a supplied threshold, set the variable to
81 // constant
82 if (sigmaRel < minSigma) {
83 oocxcoutW(nullptr, HistFactory)
84 << "Warning: relative sigma " << sigmaRel << " for \"" << gamma.GetName() << "\" falls below threshold of "
85 << minSigma << ". Setting: " << gamma.GetName() << " to constant" << std::endl;
86 gamma.setConstant(true);
87 }
88 }
89}
90
91// Take a RooArgList of RooAbsReals and create N constraint terms (one for
92// each gamma) whose relative uncertainty is the value of the ith RooAbsReal
94 std::span<const double> relSigmas, double minSigma,
96{
98
99 // Check that there are N elements in the RooArgList
100 if (relSigmas.size() != paramSet.size()) {
101 std::cout << "Error: In createGammaConstraints, encountered bad number of relative sigmas" << std::endl;
102 std::cout << "Given vector with " << relSigmas.size() << " bins,"
103 << " but require exactly " << paramSet.size() << std::endl;
104 throw hf_exc();
105 }
106
107 configureConstrainedGammas(paramSet, relSigmas, minSigma);
108
109 for (std::size_t i = 0; i < paramSet.size(); ++i) {
110
111 // The gammas can also be functions, which are constrained the same way.
112 auto &gamma = static_cast<RooAbsReal &>(paramSet[i]);
113
114 oocxcoutI(nullptr, HistFactory)
115 << "Creating constraint for: " << gamma.GetName() << ". Type of constraint: " << type << std::endl;
116
117 const double sigmaRel = relSigmas[i];
118
119 // If the sigma is <= 0,
120 // do cont create the term
121 if (sigmaRel <= 0) {
122 oocxcoutI(nullptr, HistFactory)
123 << "Not creating constraint term for " << gamma.GetName() << " because sigma = " << sigmaRel
124 << " (sigma<=0)"
125 << " (bin number = " << i << ")" << std::endl;
126 continue;
127 }
128
129 // Make Constraint Term
130 std::string constrName = std::string(gamma.GetName()) + "_constraint";
131 std::string nomName = std::string("nom_") + gamma.GetName();
132
133 if (type == Constraint::Gaussian) {
134
135 // Type 1 : RooGaussian
136
137 // Make sigma
138 std::string sigmaName = std::string(gamma.GetName()) + "_sigma";
139 auto constrSigma = std::make_unique<RooConstVar>(sigmaName.c_str(), sigmaName.c_str(), sigmaRel);
140
141 // Make "observed" value
142 auto constrNom = std::make_unique<RooRealVar>(nomName.c_str(), nomName.c_str(), 1.0, 0, 10);
143 constrNom->setConstant(true);
144
145 // Make the constraint:
146 auto term = std::make_unique<RooGaussian>(constrName.c_str(), constrName.c_str(), *constrNom, gamma, *constrSigma);
147
148 out.globalObservables.push_back(constrNom.get());
149
150 term->addOwnedComponents(std::move(constrSigma));
151 term->addOwnedComponents(std::move(constrNom));
152
153 out.constraints.emplace_back(std::move(term));
154 } else if (type == Constraint::Poisson) {
155
156 // this is correct Poisson equivalent to a Gaussian with mean 1 and stdev sigma
157 const double tau = 1. / (sigmaRel * sigmaRel);
158
159 // Make nominal "observed" value
160 auto constrNom = std::make_unique<RooRealVar>(nomName.c_str(), nomName.c_str(), tau);
161 constrNom->setMin(0);
162 constrNom->setConstant(true);
163
164 // Make the scaling term
165 std::string scalingName = std::string(gamma.GetName()) + "_tau";
166 auto poissonScaling = std::make_unique<RooConstVar>(scalingName.c_str(), scalingName.c_str(), tau);
167
168 // Make mean for scaled Poisson
169 std::string poisMeanName = std::string(gamma.GetName()) + "_poisMean";
170 auto constrMean = std::make_unique<RooProduct>(poisMeanName.c_str(), poisMeanName.c_str(), gamma, *poissonScaling);
171
172 // Type 2 : RooPoisson
173 auto term = std::make_unique<RooPoisson>(constrName.c_str(), constrName.c_str(), *constrNom, *constrMean);
174 term->setNoRounding(true);
175
176 out.globalObservables.push_back(constrNom.get());
177
178 term->addOwnedComponents(std::move(poissonScaling));
179 term->addOwnedComponents(std::move(constrMean));
180 term->addOwnedComponents(std::move(constrNom));
181
182 out.constraints.emplace_back(std::move(term));
183 } else {
184
185 std::cout << "Error: Did not recognize Stat Error constraint term type: " << type
186 << " for : " << gamma.GetName() << std::endl;
187 throw hf_exc();
188 }
189 } // end loop over parameters
190
191 return out;
192}
193
194} // namespace Detail
195} // namespace HistFactory
196} // 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
Abstract base class for objects that represent a real value and implements functionality common to al...
Definition RooAbsReal.h:63
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
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 CodegenImpl.h:67
std::vector< std::unique_ptr< RooAbsPdf > > constraints