-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[RF][HS3] Add importers and exporters for RooRealSumFunc and RooHistPdf #10210
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
3390b30
6bbb1e3
99fe84f
5697b79
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,7 +22,9 @@ | |
| #include <RooFormulaVar.h> | ||
| #include <RooGenericPdf.h> | ||
| #include <RooHistFunc.h> | ||
| #include <RooHistPdf.h> | ||
| #include <RooProdPdf.h> | ||
| #include <RooRealSumFunc.h> | ||
| #include <RooRealSumPdf.h> | ||
| #include <RooRealVar.h> | ||
| #include <RooSimultaneous.h> | ||
|
|
@@ -256,6 +258,34 @@ class RooRealSumPdfFactory : public RooFit::JSONIO::Importer { | |
| } | ||
| }; | ||
|
|
||
| class RooRealSumFuncFactory : public RooFit::JSONIO::Importer { | ||
| public: | ||
| bool importFunction(RooJSONFactoryWSTool *tool, const JSONNode &p) const override | ||
| { | ||
| std::string name(RooJSONFactoryWSTool::name(p)); | ||
| if (!p.has_child("samples")) { | ||
| RooJSONFactoryWSTool::error("no samples given in '" + name + "'"); | ||
| } | ||
| if (!p.has_child("coefficients")) { | ||
| RooJSONFactoryWSTool::error("no coefficients given in '" + name + "'"); | ||
| } | ||
| RooArgList samples; | ||
| for (const auto &sample : p["samples"].children()) { | ||
| RooAbsReal *s = tool->request<RooAbsReal>(sample.val(), name); | ||
| samples.add(*s); | ||
| } | ||
| RooArgList coefficients; | ||
| for (const auto &coef : p["coefficients"].children()) { | ||
| RooAbsReal *c = tool->request<RooAbsReal>(coef.val(), name); | ||
| coefficients.add(*c); | ||
| } | ||
|
|
||
| RooRealSumFunc thefunc(name.c_str(), name.c_str(), samples, coefficients); | ||
| tool->workspace()->import(thefunc, RooFit::RecycleConflictNodes(true), RooFit::Silence(true)); | ||
| return true; | ||
| } | ||
| }; | ||
|
|
||
| /////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
| // specialized exporter implementations | ||
| /////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
|
|
@@ -286,6 +316,31 @@ class RooRealSumPdfStreamer : public RooFit::JSONIO::Exporter { | |
| } | ||
| }; | ||
|
|
||
| class RooRealSumFuncStreamer : public RooFit::JSONIO::Exporter { | ||
| public: | ||
| std::string const &key() const override | ||
| { | ||
| const static std::string keystring = "sumfunc"; | ||
| return keystring; | ||
| } | ||
| bool exportObject(RooJSONFactoryWSTool *, const RooAbsArg *func, JSONNode &elem) const override | ||
| { | ||
| const RooRealSumFunc *pdf = static_cast<const RooRealSumFunc *>(func); | ||
| elem["type"] << key(); | ||
| auto &samples = elem["samples"]; | ||
| samples.set_seq(); | ||
| auto &coefs = elem["coefficients"]; | ||
| coefs.set_seq(); | ||
| for (const auto &s : pdf->funcList()) { | ||
| samples.append_child() << s->GetName(); | ||
| } | ||
| for (const auto &c : pdf->coefList()) { | ||
| coefs.append_child() << c->GetName(); | ||
| } | ||
| return true; | ||
| } | ||
| }; | ||
|
|
||
| class RooSimultaneousStreamer : public RooFit::JSONIO::Exporter { | ||
| public: | ||
| std::string const &key() const override | ||
|
|
@@ -356,6 +411,48 @@ class RooHistFuncFactory : public RooFit::JSONIO::Importer { | |
| } | ||
| }; | ||
|
|
||
| class RooHistPdfStreamer : public RooFit::JSONIO::Exporter { | ||
| public: | ||
| std::string const &key() const override | ||
| { | ||
| static const std::string keystring = "histogramPdf"; | ||
| return keystring; | ||
| } | ||
| bool exportObject(RooJSONFactoryWSTool *, const RooAbsArg *func, JSONNode &elem) const override | ||
| { | ||
| const RooHistPdf *hf = static_cast<const RooHistPdf *>(func); | ||
| const RooDataHist &dh = hf->dataHist(); | ||
| elem["type"] << key(); | ||
| RooArgList vars(*dh.get()); | ||
| std::unique_ptr<TH1> hist{hf->createHistogram(RooJSONFactoryWSTool::concat(&vars).c_str())}; | ||
| auto &data = elem["data"]; | ||
| RooJSONFactoryWSTool::exportHistogram(*hist, data, RooJSONFactoryWSTool::names(&vars)); | ||
| return true; | ||
| } | ||
| }; | ||
|
|
||
| class RooHistPdfFactory : public RooFit::JSONIO::Importer { | ||
| public: | ||
| bool importPdf(RooJSONFactoryWSTool *tool, const JSONNode &p) const override | ||
| { | ||
| std::string name(RooJSONFactoryWSTool::name(p)); | ||
| if (!p.has_child("data")) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I only see a
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The bin boundaries are stored in the observables. |
||
| RooJSONFactoryWSTool::error("function '" + name + "' is of histogram type, but does not define a 'data' key"); | ||
| } | ||
| RooArgSet varlist; | ||
| tool->getObservables(*tool->workspace(), p["data"], name, varlist); | ||
| RooDataHist *dh = dynamic_cast<RooDataHist *>(tool->workspace()->embeddedData(name)); | ||
| if (!dh) { | ||
| auto dhForImport = tool->readBinnedData(*tool->workspace(), p["data"], name, varlist); | ||
| tool->workspace()->import(*dhForImport, RooFit::Silence(true), RooFit::Embedded()); | ||
| dh = static_cast<RooDataHist *>(tool->workspace()->embeddedData(dhForImport->GetName())); | ||
| } | ||
| RooHistPdf hf(name.c_str(), name.c_str(), *(dh->get()), *dh); | ||
| tool->workspace()->import(hf, RooFit::RecycleConflictNodes(true), RooFit::Silence(true)); | ||
| return true; | ||
| } | ||
| }; | ||
|
|
||
| class RooBinSamplingPdfStreamer : public RooFit::JSONIO::Exporter { | ||
| public: | ||
| std::string const &key() const override | ||
|
|
@@ -461,20 +558,24 @@ STATIC_EXECUTE( | |
| registerImporter<RooProdPdfFactory>("pdfprod", false); registerImporter<RooGenericPdfFactory>("genericpdf", false); | ||
| registerImporter<RooFormulaVarFactory>("formulavar", false); | ||
| registerImporter<RooBinSamplingPdfFactory>("binsampling", false); | ||
| registerImporter<RooAddPdfFactory>("pdfsum", false); registerImporter<RooHistFuncFactory>("histogram", false); | ||
| registerImporter<RooAddPdfFactory>("pdfsum", false); | ||
| registerImporter<RooHistFuncFactory>("histogram", false); | ||
| registerImporter<RooHistFuncFactory>("histogramPdf", false); | ||
| registerImporter<RooSimultaneousFactory>("simultaneous", false); | ||
| registerImporter<RooBinWidthFunctionFactory>("binwidth", false); | ||
| registerImporter<RooRealSumPdfFactory>("sumpdf", false); | ||
| registerImporter<RooRealSumFuncFactory>("sumfunc", false); | ||
|
|
||
| registerExporter<RooBinWidthFunctionStreamer>(RooBinWidthFunction::Class(), false); | ||
| registerExporter<RooProdPdfStreamer>(RooProdPdf::Class(), false); | ||
| registerExporter<RooSimultaneousStreamer>(RooSimultaneous::Class(), false); | ||
| registerExporter<RooBinSamplingPdfStreamer>(RooBinSamplingPdf::Class(), false); | ||
| registerExporter<RooHistFuncStreamer>(RooHistFunc::Class(), false); | ||
| registerExporter<RooHistPdfStreamer>(RooHistPdf::Class(), false); | ||
| registerExporter<RooGenericPdfStreamer>(RooGenericPdf::Class(), false); | ||
| registerExporter<RooFormulaVarStreamer>(RooFormulaVar::Class(), false); | ||
| registerExporter<RooRealSumPdfStreamer>(RooRealSumPdf::Class(), false); | ||
|
|
||
| registerExporter<RooRealSumFuncStreamer>(RooRealSumFunc::Class(), false); | ||
| ) | ||
|
|
||
| } // namespace | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,4 @@ | ||
| ROOT_ADD_PYUNITTEST(roofit-hs3-histfactory-json test_hs3_histfactory_json.py | ||
| COPY_TO_BUILDDIR ${CMAKE_CURRENT_SOURCE_DIR}/test_hs3_histfactory_json_input.root | ||
| ) | ||
|
|
||
| ROOT_ADD_GTEST(testRooFitHS3 testRooFitHS3.cxx LIBRARIES RooFitCore RooFit RooFitHS3) | ||
| ROOT_ADD_GTEST(testHS3HistFactory testHS3HistFactory.cxx LIBRARIES RooFit RooFitHS3 HistFactory | ||
| COPY_TO_BUILDDIR ${CMAKE_CURRENT_SOURCE_DIR}/test_hs3_histfactory_json_input.root | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this called
samples? This is specific to HistFactory, but in general the functions can also represent things that are not "samples". Can it just be called "functions" without any compatibility issues?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I opted for "samples" rather than "functions" because the structure of the sumpdf with "functions" and "coefficients" is clearly intended to work that way - if you're just adding random stuff, you could also use RooAddition or RooAddPdf