Skip to content

Commit 840f3dd

Browse files
committed
[RF] Implement RooFit::DataError() pythonization in C++
This reduces divergence between the Python and C++ interface. Tested by the tutorials where this string-to-enum pythonization is used.
1 parent 3dbc5f7 commit 840f3dd

5 files changed

Lines changed: 34 additions & 25 deletions

File tree

bindings/pyroot/pythonizations/python/ROOT/_pythonization/_roofit/_rooglobalfunc.py

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -194,44 +194,21 @@ def Link(*args, **kwargs):
194194

195195
@cpp_signature("RooFit::DataError(Int_t) ;")
196196
def DataError(etype):
197-
r"""Instead of passing an enum value to this function, you can pass a
198-
string with the name of that enum value, for example:
199-
200-
~~~ {.py}
201-
data.plotOn(frame, DataError="SumW2")
202-
# instead of DataError=ROOT.RooAbsData.SumW2
203-
~~~
204-
205-
If you want to use the `"None"` enum value to disable error plotting, you
197+
r"""If you want to use the `"None"` enum value to disable error plotting, you
206198
can also pass `None` directly instead of passing a string:
207199
208200
~~~ {.py}
209201
data.plotOn(frame, DataError=None)
210202
# instead of DataError="None"
211203
~~~
212204
"""
213-
# Redefinition of `DataError` to also accept `str` or `NoneType` to get the
214-
# corresponding enum values from RooAbsData.DataError.
215205
from cppyy.gbl import RooFit
216206

217207
# One of the possible enum values is "None", and we want the user to be
218208
# able to pass None also as a NoneType for convenience.
219209
if etype is None:
220210
etype = "None"
221211

222-
if isinstance(etype, str):
223-
try:
224-
import ROOT
225-
226-
etype = getattr(ROOT.RooAbsData.ErrorType, etype)
227-
except AttributeError as error:
228-
raise ValueError(
229-
"Unsupported error type type passed to DataError()."
230-
+ ' Supported decay types are : "Poisson", "SumW2", "Auto", "Expected", and None.'
231-
)
232-
except Exception as exception:
233-
raise exception
234-
235212
return RooFit._DataError(etype)
236213

237214

roofit/roofitcore/inc/RooAbsData.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ class RooAbsData : public TNamed, public RooPrintable {
106106
virtual double weightSquared() const = 0 ; // DERIVED
107107

108108
enum ErrorType { Poisson, SumW2, None, Auto, Expected } ;
109+
static ErrorType errorTypeFromString(std::string const &name);
110+
109111
/// Return the symmetric error on the current weight.
110112
/// See also weightError(double&,double&,ErrorType) const for asymmetric errors.
111113
// \param[in] etype Type of error to compute. May throw if not supported.

roofit/roofitcore/inc/RooGlobalFunc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ RooCmdArg EventRange(Int_t nStart, Int_t nStop) ;
224224
// RooChi2Var::ctor / RooNLLVar arguments
225225
RooCmdArg Extended(bool flag=true) ;
226226
RooCmdArg DataError(Int_t) ;
227+
RooCmdArg DataError(std::string const&) ;
227228
RooCmdArg NumCPU(Int_t nCPU, Int_t interleave=0) ;
228229
RooCmdArg Parallelize(int nWorkers) ;
229230
RooCmdArg ModularL(bool flag=false) ;

roofit/roofitcore/src/RooAbsData.cxx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ observable snapshots are stored in the dataset.
119119

120120
#include <iostream>
121121
#include <memory>
122+
#include <sstream>
123+
#include <stdexcept>
124+
#include <unordered_map>
122125

123126

124127
ClassImp(RooAbsData);
@@ -2640,3 +2643,24 @@ TH2F *RooAbsData::createHistogram(const RooAbsRealLValue &var1, const RooAbsReal
26402643

26412644
return histogram;
26422645
}
2646+
2647+
////////////////////////////////////////////////////////////////////////////////
2648+
/// Convert a string to the value of the RooAbsData::ErrorType enum with the
2649+
/// same name.
2650+
RooAbsData::ErrorType RooAbsData::errorTypeFromString(std::string const &name)
2651+
{
2652+
using Map = std::unordered_map<std::string, RooAbsData::ErrorType>;
2653+
static Map enumMap{{"Poisson", RooAbsData::Poisson},
2654+
{"SumW2", RooAbsData::SumW2},
2655+
{"None", RooAbsData::None},
2656+
{"Auto", RooAbsData::Auto},
2657+
{"Expected", RooAbsData::Expected}};
2658+
auto found = enumMap.find(name);
2659+
if (found == enumMap.end()) {
2660+
std::stringstream msg;
2661+
msg << "Unsupported error type type passed to DataError(). "
2662+
"Supported decay types are : \"Poisson\", \"SumW2\", \"Auto\", \"Expected\", and None.";
2663+
throw std::invalid_argument(msg.str());
2664+
}
2665+
return found->second;
2666+
}

roofit/roofitcore/src/RooGlobalFunc.cxx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include <RooGlobalFunc.h>
2020

21+
#include <RooAbsData.h>
2122
#include <RooAbsPdf.h>
2223
#include <RooCategory.h>
2324
#include <RooDataHist.h>
@@ -469,7 +470,11 @@ RooCmdArg Extended(bool flag)
469470
}
470471
RooCmdArg DataError(Int_t etype)
471472
{
472-
return RooCmdArg("DataError", (Int_t)etype, 0, 0, 0, nullptr, nullptr, nullptr, nullptr);
473+
return RooCmdArg("DataError", etype);
474+
}
475+
RooCmdArg DataError(std::string const &etype)
476+
{
477+
return DataError(RooAbsData::errorTypeFromString(etype));
473478
}
474479
RooCmdArg NumCPU(Int_t nCPU, Int_t interleave)
475480
{

0 commit comments

Comments
 (0)