Skip to content

Commit 4157d78

Browse files
authored
[RF] Update xroofit
1 parent ef1b534 commit 4157d78

File tree

7 files changed

+1064
-539
lines changed

7 files changed

+1064
-539
lines changed

roofit/xroofit/inc/RooFit/xRooFit/xRooNLLVar.h

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ class xRooNLLVar : public std::shared_ptr<RooAbsReal> {
8383

8484
void reinitialize();
8585

86-
void AddOption(const RooCmdArg &opt);
86+
void SetOption(const RooCmdArg &opt);
87+
[[deprecated("Use SetOption()")]] void AddOption(const RooCmdArg &opt) { SetOption(opt); }
8788

8889
std::pair<std::shared_ptr<RooAbsData>, std::shared_ptr<const RooAbsCollection>>
8990
getData() const; // returns pointer to data and snapshot of globs
@@ -240,11 +241,32 @@ class xRooNLLVar : public std::shared_ptr<RooAbsReal> {
240241
double fNullVal();
241242
double fAltVal();
242243

244+
void setNullVal(double val);
245+
void setAltVal(double val);
246+
void setObsTS(double val, double err)
247+
{
248+
obs_ts = val;
249+
obs_ts_err = err;
250+
fPllType = xRooFit::Asymptotics::Unknown;
251+
}
252+
void addNullToy(double value, double weight = 1., int seed = 0)
253+
{
254+
fPllType = xRooFit::Asymptotics::Unknown;
255+
nullToys.emplace_back(std::make_tuple(seed, value, weight));
256+
}
257+
void addAltToy(double value, double weight = 1., int seed = 0)
258+
{
259+
fPllType = xRooFit::Asymptotics::Unknown;
260+
altToys.emplace_back(std::make_tuple(seed, value, weight));
261+
}
262+
243263
std::shared_ptr<const RooAbsCollection> coords; // pars of the nll that will be held const alongside POI
244264

245265
std::shared_ptr<const RooFitResult> fUfit, fNull_cfit, fAlt_cfit, fLbound_cfit;
246266
std::shared_ptr<const RooFitResult> fGenFit; // if the data was generated, this is the fit is was generated from
247267
bool isExpected = false; // if genFit, flag says is asimov or not
268+
double obs_ts = std::numeric_limits<double>::quiet_NaN(); // only specified for unknown pll types
269+
double obs_ts_err = std::numeric_limits<double>::quiet_NaN();
248270

249271
std::shared_ptr<xRooHypoPoint>
250272
fAsimov; // same as this point but pllType is twosided and data is expected post alt-fit
@@ -289,7 +311,8 @@ class xRooNLLVar : public std::shared_ptr<RooAbsReal> {
289311

290312
bool AddModel(const xRooNode &pdf, const char *validity = "");
291313

292-
void LoadFits(const char *apath);
314+
// the directory where fits are cached from scans
315+
TDirectory *fitCache() const { return fFitDb.get(); }
293316

294317
// A points over given parameter, number of points between low and high
295318
int AddPoints(const char *parName, size_t nPoints, double low, double high);
@@ -368,22 +391,25 @@ class xRooNLLVar : public std::shared_ptr<RooAbsReal> {
368391

369392
std::set<std::pair<std::shared_ptr<RooArgList>, std::shared_ptr<xRooNode>>> fPdfs;
370393

371-
std::shared_ptr<TFile> fFitDb;
394+
std::shared_ptr<TDirectory> fFitDb;
372395
};
373396

374397
xRooHypoSpace hypoSpace(const char *parName, int nPoints, double low, double high,
375398
double alt_value = std::numeric_limits<double>::quiet_NaN(),
376-
const xRooFit::Asymptotics::PLLType &pllType = xRooFit::Asymptotics::Unknown);
399+
const xRooFit::Asymptotics::PLLType &pllType = xRooFit::Asymptotics::Unknown,
400+
int tsType = 0);
377401
xRooHypoSpace hypoSpace(const char *parName = "",
378402
const xRooFit::Asymptotics::PLLType &pllType = xRooFit::Asymptotics::Unknown,
379403
double alt_value = std::numeric_limits<double>::quiet_NaN());
380404
xRooHypoSpace hypoSpace(int nPoints, double low, double high,
381405
double alt_value = std::numeric_limits<double>::quiet_NaN(),
382406
const xRooFit::Asymptotics::PLLType &pllType = xRooFit::Asymptotics::Unknown);
383-
xRooHypoSpace hypoSpace(const char *parName, xRooFit::TestStatistic::Type tsType, int nPoints = 0)
407+
xRooHypoSpace hypoSpace(const char *parName, xRooFit::TestStatistic::Type tsType, int nPoints = 0,
408+
double low = -std::numeric_limits<double>::infinity(),
409+
double high = std::numeric_limits<double>::infinity(),
410+
double alt_value = std::numeric_limits<double>::quiet_NaN())
384411
{
385-
return hypoSpace(parName, int(tsType), nPoints, -std::numeric_limits<double>::infinity(),
386-
std::numeric_limits<double>::infinity());
412+
return hypoSpace(parName, nPoints, low, high, alt_value, xRooFit::Asymptotics::Unknown, tsType);
387413
}
388414

389415
std::shared_ptr<RooArgSet> pars(bool stripGlobalObs = true) const;

roofit/xroofit/inc/RooFit/xRooFit/xRooNode.h

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ class xRooNode : public TNamed, public std::vector<std::shared_ptr<xRooNode>> {
322322
xRooNode Vary(const xRooNode &child);
323323
xRooNode Constrain(const xRooNode &child);
324324

325-
xRooNode Combine(const xRooNode &rhs); // combine rhs with this node
325+
xRooNode Combine(const xRooNode &rhs, bool silent = false); // combine rhs with this node
326326

327327
xRooNode reduced(const std::string &range = "", bool invert = false)
328328
const; // return a node representing reduced version of this node, will use the SetRange to reduce if blank
@@ -340,6 +340,8 @@ class xRooNode : public TNamed, public std::vector<std::shared_ptr<xRooNode>> {
340340
void _ShowVars_(bool set = true); // *TOGGLE* *GETTER=_IsShowVars_
341341
/** @private */
342342
bool _IsShowVars_() const;
343+
/** @private */
344+
void _SetAttribute_(const char *name, const char *value = nullptr); // *MENU*
343345

344346
void SetHidden(bool set = true); // *TOGGLE* *GETTER=IsHidden
345347
bool IsHidden() const;
@@ -392,7 +394,8 @@ class xRooNode : public TNamed, public std::vector<std::shared_ptr<xRooNode>> {
392394
GetBinError(int bin, const xRooNode &fr = "", int nToys = 0, bool errorsHi = false, bool errorsLo = false) const;
393395
std::vector<double> GetBinErrors(int binStart = 1, int binEnd = 0, const xRooNode &fr = "", int nToys = 0,
394396
bool errorsHi = false, bool errorsLo = false) const;
395-
std::pair<double, double> IntegralAndError(const xRooNode &fr = "", const char *rangeName = nullptr) const;
397+
std::pair<double, double> IntegralAndError(const xRooNode &fr = "", const char *rangeName = nullptr, int nToys = 0,
398+
bool errorsHi = false, bool errorsLo = false) const;
396399

397400
std::vector<double> GetBinErrorsHi(int binStart = 1, int binEnd = 0, const xRooNode &fr = "", int nToys = 0) const
398401
{
@@ -413,10 +416,13 @@ class xRooNode : public TNamed, public std::vector<std::shared_ptr<xRooNode>> {
413416

414417
// methods to access default content and error
415418
double GetContent() const { return GetBinContent(fBinNumber); }
416-
double GetError(const xRooNode &fr = "") const
419+
double GetError(const xRooNode &fr = "", int nToys = 0, bool errorsHi = false, bool errorsLo = false) const
417420
{
418-
return (fBinNumber == -1) ? IntegralAndError(fr).second : GetBinError(fBinNumber, fr);
421+
return (fBinNumber == -1) ? IntegralAndError(fr, "", nToys, errorsHi, errorsLo).second
422+
: GetBinError(fBinNumber, fr, nToys, errorsHi, errorsLo);
419423
}
424+
double GetErrorHi(const xRooNode &fr = "", int nToys = 0) const { return GetError(fr, nToys, true, false); }
425+
double GetErrorLo(const xRooNode &fr = "", int nToys = 0) const { return GetError(fr, nToys, false, true); }
420426
double GetData(const xRooNode &data = "obsData") { return GetBinData(fBinNumber, data); }
421427

422428
// methods to access content and covariances of the CHILDREN of a node
@@ -441,13 +447,13 @@ class xRooNode : public TNamed, public std::vector<std::shared_ptr<xRooNode>> {
441447
int seed = 0); // generate a dataset from a pdf node using given fr - if none given will use current fit
442448

443449
/** @private */
444-
void _fit_(const char *constParValues = "", const char* options = ""); // *MENU*
450+
void _fit_(const char *constParValues = "", const char *options = "GoF"); // *MENU*
445451
/** @private */
446452
void _generate_(const char *name = "", bool expected = false); // *MENU*
447453
/** @private */
448454
void _scan_(const char *what = "plr", double nToys = 0, const char *xvar = "", int nPointsX = 0, double lowX = 0,
449455
double highX = 0 /*, const char* yvar="", int nBinsY=0, double lowY=0, double highY=0*/,
450-
const char *constParValues = "", const char* options = ""); // *MENU*
456+
const char *constParValues = "", const char *options = ""); // *MENU*
451457
// xRooNode fitTo(const char* datasetName) const;
452458
// xRooNode fitTo(const xRooNode& _data) const;
453459
// xRooNode generate(bool expected=false) const;

roofit/xroofit/src/Asymptotics.cxx

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ double xRooFit::Asymptotics::k(const IncompatFunc &compatRegions, double pValue,
4949
double operator()(double x) const
5050
{
5151
double val = PValue(cFunc, x, poiVal, alt_val, sigma_mu, low, high);
52-
if (val < 0)
52+
if (val < -1e-10)
5353
kInvalid = true;
5454
return val - target;
5555
}
@@ -154,26 +154,22 @@ double xRooFit::Asymptotics::PValue(const IncompatFunc &compatRegions, double k,
154154
// go through the 4 'regions' ... only two of which will apply
155155
if (k <= k_high) {
156156
out2 += ROOT::Math::gaussian_cdf_c(sqrt(k) + Lambda_y);
157-
out += 1.0 -
158-
Phi_m(poiVal, poi_primeVal, Lambda_y + sqrt(k), sigma, compatRegions);
157+
out += 1.0 - Phi_m(poiVal, poi_primeVal, Lambda_y + sqrt(k), sigma, compatRegions);
159158
} else {
160159
double Lambda_high = (poiVal - upBound) * (poiVal + upBound - 2. * poi_primeVal) / (sigma * sigma);
161160
double sigma_high = 2. * (upBound - poiVal) / sigma;
162161
out2 += ROOT::Math::gaussian_cdf_c((k - Lambda_high) / sigma_high);
163-
out += 1.0 -
164-
Phi_m(poiVal, poi_primeVal, (k - Lambda_high) / sigma_high, sigma, compatRegions);
162+
out += 1.0 - Phi_m(poiVal, poi_primeVal, (k - Lambda_high) / sigma_high, sigma, compatRegions);
165163
}
166164

167165
if (k <= k_low) {
168166
out2 += ROOT::Math::gaussian_cdf_c(sqrt(k) - Lambda_y);
169-
out += 1.0 +
170-
Phi_m(poiVal, poi_primeVal, Lambda_y - sqrt(k), sigma, compatRegions);
167+
out += 1.0 + Phi_m(poiVal, poi_primeVal, Lambda_y - sqrt(k), sigma, compatRegions);
171168
} else {
172169
double Lambda_low = (poiVal - lowBound) * (poiVal + lowBound - 2. * poi_primeVal) / (sigma * sigma);
173170
double sigma_low = 2. * (poiVal - lowBound) / sigma;
174171
out2 += ROOT::Math::gaussian_cdf_c((k - Lambda_low) / sigma_low);
175-
out += 1.0 +
176-
Phi_m(poiVal, poi_primeVal, (Lambda_low - k) / sigma_low, sigma, compatRegions);
172+
out += 1.0 + Phi_m(poiVal, poi_primeVal, (Lambda_low - k) / sigma_low, sigma, compatRegions);
177173
/*out += ROOT::Math::gaussian_cdf((k-Lambda_low)/sigma_low) +
178174
2*Phi_m(poiVal,poi_primeVal,(Lambda_low - k_low)==0 ? 0 : ((Lambda_low -
179175
k_low)/sigma_low),sigma,compatRegions)

roofit/xroofit/src/xRooFit.cxx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,10 +1283,13 @@ std::shared_ptr<const RooFitResult> xRooFit::minimize(RooAbsReal &nll,
12831283
}
12841284
}
12851285

1286-
if(miniStrat < _minimizer.fitter()->Config().MinimizerOptions().Strategy() && hesse && out->edm() > _minimizer.fitter()->Config().MinimizerOptions().Tolerance()*1e-3 && out->status() != 3) {
1286+
if (miniStrat < _minimizer.fitter()->Config().MinimizerOptions().Strategy() && hesse &&
1287+
out->edm() > _minimizer.fitter()->Config().MinimizerOptions().Tolerance() * 1e-3 && out->status() != 3) {
12871288
// hesse may have updated edm by using a better strategy than used in the minimization
12881289
// so print a warning about this
1289-
std::cerr << "Warning: post-Hesse edm greater than allowed by tolerance. Consider increasing minimization strategy" << std::endl;
1290+
std::cerr << "Warning: post-Hesse edm " << out->edm() << " greater than allowed by tolerance "
1291+
<< _minimizer.fitter()->Config().MinimizerOptions().Tolerance() * 1e-3
1292+
<< ", consider increasing minimization strategy" << std::endl;
12901293
// Dec24: As this is a new warning, will not update status code for now, so edm will be large
12911294
// but in the future we should probably update the code to 3 so that users don't miss this warning.
12921295
// out->setStatus(3); // edm above max
@@ -1400,7 +1403,7 @@ std::shared_ptr<const RooFitResult> xRooFit::minimize(RooAbsReal &nll,
14001403
}
14011404
}
14021405

1403-
if(out && out->status() == 0 && minos) {
1406+
if (out && out->status() == 0 && minos) {
14041407
// call minos if requested on any parameters
14051408
for (auto label : {"xminos", "xMinos"}) {
14061409
std::unique_ptr<RooAbsCollection> pars(floatPars->selectByAttrib(label, true));

0 commit comments

Comments
 (0)