Skip to content

Commit 5454e38

Browse files
committed
ParamHistFuncs accept custom modifiers
1 parent 356b093 commit 5454e38

File tree

5 files changed

+72
-15
lines changed

5 files changed

+72
-15
lines changed

roofit/hs3/src/JSONFactories_HistFactory.cxx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,15 @@ void collectElements(RooArgSet &elems, RooAbsArg *arg)
823823
}
824824
}
825825

826+
bool allRooRealVar(const RooAbsCollection &list) {
827+
for (auto* var : list) {
828+
if (!dynamic_cast<RooRealVar*>(var)) {
829+
return false;
830+
}
831+
}
832+
return true;
833+
}
834+
826835
struct Sample {
827836
std::string name;
828837
std::vector<double> hist;
@@ -920,7 +929,7 @@ Channel readChannel(RooJSONFactoryWSTool *tool, const std::string &pdfname, cons
920929
addNormFactor(par, sample, ws);
921930
} else if (auto hf = dynamic_cast<const RooHistFunc *>(e)) {
922931
updateObservables(hf->dataHist());
923-
} else if (auto phf = dynamic_cast<ParamHistFunc *>(e)) {
932+
} else if (ParamHistFunc* phf = dynamic_cast<ParamHistFunc *>(e); phf && allRooRealVar(phf->paramList())) {
924933
phfs.push_back(phf);
925934
} else if (auto fip = dynamic_cast<RooStats::HistFactory::FlexibleInterpVar *>(e)) {
926935
// some (modified) histfactory models have several instances of FlexibleInterpVar

roofit/hs3/src/JSONFactories_RooFitCore.cxx

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <RooLegacyExpPoly.h>
3434
#include <RooLognormal.h>
3535
#include <RooMultiVarGaussian.h>
36+
#include <RooStats/HistFactory/ParamHistFunc.h>
3637
#include <RooPoisson.h>
3738
#include <RooPolynomial.h>
3839
#include <RooPolyVar.h>
@@ -532,6 +533,23 @@ class RooMultiVarGaussianFactory : public RooFit::JSONIO::Importer {
532533
}
533534
};
534535

536+
class ParamHistFuncFactory : public RooFit::JSONIO::Importer {
537+
public:
538+
bool importArg(RooJSONFactoryWSTool *tool, const JSONNode &p) const override
539+
{
540+
std::string name(RooJSONFactoryWSTool::name(p));
541+
RooArgList vars = tool->requestArgList<RooRealVar>(p, "variables");
542+
std::vector<int> nbins;
543+
nbins << p["nbins"];
544+
for (size_t i = 0; i < vars.size(); ++i) {
545+
auto *v = dynamic_cast<RooRealVar*>(vars.at(i));
546+
v->setBins(nbins[i]);
547+
}
548+
tool->wsEmplace<ParamHistFunc>(name, vars, tool->requestArgList<RooAbsReal>(p, "parameters"));
549+
return true;
550+
}
551+
};
552+
535553
///////////////////////////////////////////////////////////////////////////////////////////////////////
536554
// specialized exporter implementations
537555
///////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -696,6 +714,7 @@ class RooFormulaArgStreamer : public RooFit::JSONIO::Exporter {
696714
expr.ReplaceAll("TMath::Sin", "sin");
697715
expr.ReplaceAll("TMath::Sqrt", "sqrt");
698716
expr.ReplaceAll("TMath::Power", "pow");
717+
expr.ReplaceAll("TMath::Erf", "erf");
699718
}
700719
};
701720
template <class RooArg_t>
@@ -952,6 +971,26 @@ class RooExtendPdfStreamer : public RooFit::JSONIO::Exporter {
952971
}
953972
};
954973

974+
class ParamHistFuncStreamer : public RooFit::JSONIO::Exporter {
975+
public:
976+
std::string const &key() const override;
977+
bool exportObject(RooJSONFactoryWSTool *, const RooAbsArg *func, JSONNode &elem) const override
978+
{
979+
auto *pdf = static_cast<const ParamHistFunc *>(func);
980+
elem["type"] << key();
981+
RooJSONFactoryWSTool::fillSeq(elem["variables"], pdf->dataVars());
982+
RooJSONFactoryWSTool::fillSeq(elem["parameters"], pdf->paramList());
983+
std::vector<int> nbins;
984+
for (auto *arg : pdf->dataVars()) {
985+
auto *var = dynamic_cast<RooRealVar*>(arg);
986+
nbins.push_back(var->numBins());
987+
}
988+
elem["nbins"] << nbins;
989+
990+
return true;
991+
}
992+
};
993+
955994
#define DEFINE_EXPORTER_KEY(class_name, name) \
956995
std::string const &class_name::key() const \
957996
{ \
@@ -989,6 +1028,7 @@ DEFINE_EXPORTER_KEY(RooRealIntegralStreamer, "integral");
9891028
DEFINE_EXPORTER_KEY(RooDerivativeStreamer, "derivative");
9901029
DEFINE_EXPORTER_KEY(RooFFTConvPdfStreamer, "fft_conv_pdf");
9911030
DEFINE_EXPORTER_KEY(RooExtendPdfStreamer, "extend_pdf");
1031+
DEFINE_EXPORTER_KEY(ParamHistFuncStreamer, "param_hist_func");
9921032

9931033
///////////////////////////////////////////////////////////////////////////////////////////////////////
9941034
// instantiate all importers and exporters
@@ -1021,6 +1061,7 @@ STATIC_EXECUTE([]() {
10211061
registerImporter<RooDerivativeFactory>("derivative", false);
10221062
registerImporter<RooFFTConvPdfFactory>("fft_conv_pdf", false);
10231063
registerImporter<RooExtendPdfFactory>("extend_pdf", false);
1064+
registerImporter<ParamHistFuncFactory>("param_hist_func", false);
10241065

10251066
registerExporter<RooAddPdfStreamer<RooAddPdf>>(RooAddPdf::Class(), false);
10261067
registerExporter<RooAddPdfStreamer<RooAddModel>>(RooAddModel::Class(), false);
@@ -1047,6 +1088,7 @@ STATIC_EXECUTE([]() {
10471088
registerExporter<RooDerivativeStreamer>(RooDerivative::Class(), false);
10481089
registerExporter<RooFFTConvPdfStreamer>(RooFFTConvPdf::Class(), false);
10491090
registerExporter<RooExtendPdfStreamer>(RooExtendPdf::Class(), false);
1091+
registerExporter<ParamHistFuncStreamer>(ParamHistFunc::Class(), false);
10501092
});
10511093

10521094
} // namespace

roofit/hs3/src/RooFitHS3_wsexportkeys.cxx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ auto RooFitHS3_wsexportkeys = R"({
6262
"sigmaR": "sigma_R"
6363
}
6464
},
65+
"RooEffProd": {
66+
"type": "efficiency_product_pdf_dist",
67+
"proxies": {
68+
"pdf": "pdf",
69+
"eff": "eff"
70+
}
71+
},
6572
"RooGamma": {
6673
"type": "gamma_dist",
6774
"proxies": {
@@ -79,13 +86,6 @@ auto RooFitHS3_wsexportkeys = R"({
7986
"sigma": "sigma"
8087
}
8188
},
82-
"ParamHistFunc": {
83-
"type": "step",
84-
"proxies": {
85-
"dataVars": "variables",
86-
"paramSet": "parameters"
87-
}
88-
},
8989
"RooLandau": {
9090
"type": "landau_dist",
9191
"proxies": {

roofit/hs3/src/RooFitHS3_wsfactoryexpressions.cxx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ auto RooFitHS3_wsfactoryexpressions = R"({
4343
"coefficients"
4444
]
4545
},
46+
"efficiency_product_pdf_dist": {
47+
"class": "RooEffProd",
48+
"arguments": [
49+
"pdf",
50+
"eff"
51+
]
52+
},
4653
"gamma_dist": {
4754
"class": "RooGamma",
4855
"arguments": [
@@ -112,13 +119,6 @@ auto RooFitHS3_wsfactoryexpressions = R"({
112119
"observables"
113120
]
114121
},
115-
"step": {
116-
"class": "ParamHistFunc",
117-
"arguments": [
118-
"variables",
119-
"parameters"
120-
]
121-
},
122122
"sum": {
123123
"class": "RooAddition",
124124
"arguments": [

roofit/jsoninterface/inc/RooFit/Detail/JSONInterface.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,12 @@ inline RooFit::Detail::JSONNode &operator<<(RooFit::Detail::JSONNode &n, std::sp
265265
return n;
266266
}
267267

268+
inline RooFit::Detail::JSONNode &operator<<(RooFit::Detail::JSONNode &n, std::span<const int> v)
269+
{
270+
n.fill_seq(v);
271+
return n;
272+
}
273+
268274
template <class Key, class T, class Hash, class KeyEqual, class Allocator>
269275
RooFit::Detail::JSONNode &
270276
operator<<(RooFit::Detail::JSONNode &n, const std::unordered_map<Key, T, Hash, KeyEqual, Allocator> &m)

0 commit comments

Comments
 (0)