Skip to content

Commit de79f13

Browse files
committed
[RF][HF] Support setting ShapeFactor value and range
Closes #20697.
1 parent fd2caca commit de79f13

5 files changed

Lines changed: 72 additions & 33 deletions

File tree

roofit/histfactory/inc/RooStats/HistFactory/Detail/HistFactoryImpl.h

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,24 @@
1313
#ifndef HistFactoryImplHelpers_h
1414
#define HistFactoryImplHelpers_h
1515

16-
#include <RooStats/HistFactory/Measurement.h>
17-
1816
#include <RooGlobalFunc.h>
1917
#include <RooWorkspace.h>
2018

2119
#include <ROOT/RSpan.hxx>
2220

23-
namespace RooStats {
24-
namespace HistFactory {
21+
namespace RooStats::HistFactory {
22+
23+
namespace Constraint {
24+
25+
enum Type {
26+
Gaussian,
27+
Poisson
28+
};
29+
std::string Name(Type type);
30+
Type GetType(const std::string &Name);
31+
32+
} // namespace Constraint
33+
2534
namespace Detail {
2635

2736
namespace MagicConstants {
@@ -49,15 +58,13 @@ void configureConstrainedGammas(RooArgList const &gammas, std::span<const double
4958

5059
struct CreateGammaConstraintsOutput {
5160
std::vector<std::unique_ptr<RooAbsPdf>> constraints;
52-
std::vector<RooRealVar*> globalObservables;
61+
std::vector<RooRealVar *> globalObservables;
5362
};
5463

55-
CreateGammaConstraintsOutput createGammaConstraints(RooArgList const &paramList,
56-
std::span<const double> relSigmas, double minSigma,
57-
Constraint::Type type);
64+
CreateGammaConstraintsOutput createGammaConstraints(RooArgList const &paramList, std::span<const double> relSigmas,
65+
double minSigma, Constraint::Type type);
5866

5967
} // namespace Detail
60-
} // namespace HistFactory
61-
} // namespace RooStats
68+
} // namespace RooStats::HistFactory
6269

6370
#endif

roofit/histfactory/inc/RooStats/HistFactory/Measurement.h

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
#include <TNamed.h>
1515

16+
#include <RooStats/HistFactory/Detail/HistFactoryImpl.h>
17+
1618
#include <fstream>
1719
#include <iostream>
1820
#include <map>
@@ -27,17 +29,6 @@ class RooWorkspace;
2729

2830
namespace RooStats::HistFactory {
2931

30-
namespace Constraint {
31-
32-
enum Type {
33-
Gaussian,
34-
Poisson
35-
};
36-
std::string Name(Type type);
37-
Type GetType(const std::string &Name);
38-
39-
} // namespace Constraint
40-
4132
/** \class OverallSys
4233
* \ingroup HistFactory
4334
* Configuration for a constrained overall systematic to scale sample normalisations.
@@ -257,12 +248,24 @@ class ShapeFactor : public HistogramUncertaintyBase {
257248
}
258249
const std::string &GetHistoPath() const { return fHistoPathHigh; }
259250

251+
double GetVal() const { return fValue; }
252+
253+
double GetMin() const { return fMinVal; }
254+
double GetMax() const { return fMaxVal; }
255+
256+
void SetVal(double value) { fValue = value; }
257+
258+
void SetMin(double minVal) { fMinVal = minVal; }
259+
void SetMax(double maxVal) { fMaxVal = maxVal; }
260+
260261
protected:
261262
bool fConstant = false;
262-
263-
// A histogram representing
264-
// the initial shape
265263
bool fHasInitialShape = false;
264+
double fValue = 1.0;
265+
// GHL: Again, we are putting hard ranges on the gammas by default.
266+
// We should change this to range from 0 to /inf.
267+
double fMinVal = Detail::MagicConstants::defaultGammaMin;
268+
double fMaxVal = Detail::MagicConstants::defaultShapeFactorGammaMax;
266269
};
267270

268271
/** \class StatError
@@ -484,6 +487,7 @@ class Sample {
484487
void AddHistoFactor(const HistoFactor &Factor);
485488

486489
void AddShapeFactor(std::string Name);
490+
void AddShapeFactor(std::string Name, double initialVal, double minVal, double maxVal);
487491
void AddShapeFactor(const ShapeFactor &Factor);
488492

489493
void AddShapeSys(std::string Name, Constraint::Type ConstraintType, std::string HistoName, std::string HistoFile,

roofit/histfactory/src/ConfigParser.cxx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,6 +1253,15 @@ HistFactory::ShapeFactor ConfigParser::MakeShapeFactor( TXMLNode* node ) {
12531253
else if( attrName == TString( "Name" ) ) {
12541254
shapeFactor.SetName( attrVal );
12551255
}
1256+
else if( attrName == TString( "Val" ) ) {
1257+
shapeFactor.SetVal( toDouble(attrVal) );
1258+
}
1259+
else if( attrName == TString( "Min" ) ) {
1260+
shapeFactor.SetMin( toDouble(attrVal) );
1261+
}
1262+
else if( attrName == TString( "Max" ) ) {
1263+
shapeFactor.SetMax( toDouble(attrVal) );
1264+
}
12561265
else if( attrName == TString( "Const" ) ) {
12571266
shapeFactor.SetConstant( CheckTrueFalse(attrVal, "ShapeFactor" ) );
12581267
}

roofit/histfactory/src/HistoToWorkspaceFactoryFast.cxx

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,13 +1032,18 @@ RooArgList HistoToWorkspaceFactoryFast::createObservables(const TH1 *hist, RooWo
10321032
}
10331033

10341034
// Create the Parameters
1035-
std::string funcParams = "gamma_" + shapeFactor.GetName();
1036-
1037-
// GHL: Again, we are putting hard ranges on the gamma's
1038-
// We should change this to range from 0 to /inf
10391035
RooArgList shapeFactorParams = ParamHistFunc::createParamSet(proto,
1040-
funcParams,
1041-
theObservables, defaultGammaMin, defaultShapeFactorGammaMax);
1036+
"gamma_" + shapeFactor.GetName(),
1037+
theObservables);
1038+
for (auto *comp : shapeFactorParams) {
1039+
// If the gamma is subject to a preprocess function, it is a RooAbsReal and
1040+
// we don't need to set the initial value.
1041+
if(auto var = dynamic_cast<RooRealVar*>(comp)) {
1042+
var->setVal(shapeFactor.GetVal());
1043+
var->setMin(shapeFactor.GetMin());
1044+
var->setMax(shapeFactor.GetMax());
1045+
}
1046+
}
10421047

10431048
// Create the Function
10441049
ParamHistFunc shapeFactorFunc( funcName.c_str(), funcName.c_str(),

roofit/histfactory/src/Measurement.cxx

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -994,9 +994,18 @@ void Sample::AddHistoFactor(const HistoFactor &Factor)
994994
void Sample::AddShapeFactor(std::string SysName)
995995
{
996996

997-
ShapeFactor factor;
998-
factor.SetName(SysName);
999-
fShapeFactorList.push_back(factor);
997+
fShapeFactorList.emplace_back();
998+
fShapeFactorList.back().SetName(SysName);
999+
}
1000+
1001+
void Sample::AddShapeFactor(std::string SysName, double value, double minVal, double maxVal)
1002+
{
1003+
1004+
fShapeFactorList.emplace_back();
1005+
fShapeFactorList.back().SetName(SysName);
1006+
fShapeFactorList.back().SetVal(value);
1007+
fShapeFactorList.back().SetMin(minVal);
1008+
fShapeFactorList.back().SetMax(maxVal);
10001009
}
10011010

10021011
void Sample::AddShapeFactor(const ShapeFactor &Factor)
@@ -1904,6 +1913,8 @@ void ShapeFactor::Print(std::ostream &stream) const
19041913
<< " Shape Hist Name: " << fHistoNameHigh << " Shape Hist Path Name: " << fHistoPathHigh
19051914
<< " Shape Hist FileName: " << fInputFileHigh << std::endl;
19061915
}
1916+
// Print value and range in RooRealVar style
1917+
stream << "\t \t Value: " << GetVal() << " L(" << GetMin() << " - " << GetMax() << ")\n";
19071918

19081919
if (fConstant) {
19091920
stream << "\t \t ( Constant ): " << std::endl;
@@ -1937,6 +1948,9 @@ void ShapeFactor::PrintXML(std::ostream &xml) const
19371948
<< " HistoName=\"" << GetHistoName() << "\" "
19381949
<< " HistoPath=\"" << GetHistoPath() << "\" ";
19391950
}
1951+
xml << " Value=\"" << GetVal() << "\" "
1952+
<< " Min=\"" << GetMin() << "\" "
1953+
<< " Max=\"" << GetMax() << "\" ";
19401954
xml << " /> " << std::endl;
19411955
}
19421956

0 commit comments

Comments
 (0)