Skip to content

Commit

Permalink
[RF] Always use RooDataSet constructor with command arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
guitargeek committed Jan 9, 2024
1 parent a1f54ed commit 387bd0d
Show file tree
Hide file tree
Showing 14 changed files with 93 additions and 239 deletions.
2 changes: 1 addition & 1 deletion roofit/RDataFrameHelpers/test/testActionHelpers.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ TEST(RooAbsDataHelper, SkipEventsOutOfRange) {
RooRealVar x{"x", "x", 0.0, -2.0, 2.0};

// Create a RooDataset from the TTree, and one from the RDataFrame
RooDataSet dataSetTree{"dataSetTree", "dataSetTree", tree, x};
RooDataSet dataSetTree{"dataSetTree", "dataSetTree", x, RooFit::Import(*tree)};
auto dataSetRDF = rdf.Book<double>(RooDataSetHelper("dataSetRDF", "dataSetRDF", RooArgSet(x)), {"x"});

// Check if in the creation of the datasets, the entries outside the
Expand Down
9 changes: 0 additions & 9 deletions roofit/roofitcore/inc/RooDataSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@ class RooDataSet : public RooAbsData, public RooDirItem {
// Constructors, factory methods etc.
RooDataSet() ;

RooDataSet(RooStringView name, RooStringView title, const RooArgSet& vars, const char* wgtVarName)
R__SUGGEST_ALTERNATIVE("Use RooDataSet(name, title, vars, RooFit::WeightVar(wgtVarName)).");

// Universal constructor
RooDataSet(RooStringView name, RooStringView title, const RooArgSet& vars, const RooCmdArg& arg1={}, const RooCmdArg& arg2={},
const RooCmdArg& arg3={}, const RooCmdArg& arg4={},const RooCmdArg& arg5={},
Expand All @@ -81,12 +78,6 @@ class RooDataSet : public RooAbsData, public RooDirItem {
const RooFormulaVar& cutVar, const char* wgtVarName=nullptr) ;


// Constructor importing data from external ROOT Tree
RooDataSet(RooStringView name, RooStringView title, TTree *tree, const RooArgSet& vars,
const char *cuts=nullptr, const char* wgtVarName=nullptr);
RooDataSet(RooStringView name, RooStringView title, TTree *tree, const RooArgSet& vars,
const RooFormulaVar& cutVar, const char* wgtVarName=nullptr) ;

RooDataSet(RooDataSet const & other, const char* newname=nullptr) ;
TObject* Clone(const char* newname = "") const override {
return new RooDataSet(*this, newname && newname[0] != '\0' ? newname : GetName());
Expand Down
80 changes: 1 addition & 79 deletions roofit/roofitcore/src/RooDataSet.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ std::unique_ptr<RooDataSet> makeDataSetFromDataHist(RooDataHist const &hist)
///
/// <table>
/// <tr><th> %RooCmdArg <th> Effect
/// <tr><td> Import(TTree*) <td> Import contents of given TTree. Only branches of the TTree that have names
/// <tr><td> Import(TTree&) <td> Import contents of given TTree. Only branches of the TTree that have names
/// corresponding to those of the RooAbsArgs that define the RooDataSet are
/// imported.
/// <tr><td> ImportFromFile(const char* fileName, const char* treeName) <td> Import tree with given name from file with given name.
Expand Down Expand Up @@ -576,18 +576,6 @@ RooDataSet::RooDataSet(RooStringView name, RooStringView title, const RooArgSet&
}



////////////////////////////////////////////////////////////////////////////////
/// Constructor of an empty data set from a RooArgSet defining the dimensions
/// of the data space.
/// \deprecated Use the more explicit `RooDataSet(name, title, vars, RooFit::WeightVar(wgtVarName))`.

RooDataSet::RooDataSet(RooStringView name, RooStringView title, const RooArgSet& vars, const char* wgtVarName)
: RooDataSet(name,title,vars, RooFit::WeightVar(wgtVarName))
{
}


////////////////////////////////////////////////////////////////////////////////
/// Constructor of a data set from (part of) an existing data
/// set. The dimensions of the data set are defined by the 'vars'
Expand Down Expand Up @@ -664,72 +652,6 @@ RooDataSet::RooDataSet(RooStringView name, RooStringView title, RooDataSet *dset
: RooDataSet{name, title, dset, vars, cutVar.expression(), wgtVarName} {}



////////////////////////////////////////////////////////////////////////////////
/// Constructor of a data set from (part of) an ROOT TTree. The dimensions
/// of the data set are defined by the 'vars' RooArgSet. For each dimension
/// specified, the TTree must have a branch with the same name. For category
/// branches, this branch should contain the numeric index value. Real dimensions
/// can be constructed from either 'double' or 'Float_t' tree branches. In the
/// latter case, an automatic conversion is applied.
///
/// The 'cutVar' formula variable
/// is used to select the subset of data points to be copied.
/// For subsets without selection on the data points, or involving cuts
/// operating exclusively and directly on the data set dimensions, the equivalent
/// constructor with a string based cut expression is recommended.

RooDataSet::RooDataSet(RooStringView name, RooStringView title, TTree *theTree,
const RooArgSet& vars, const RooFormulaVar& cutVar, const char* wgtVarName)
: RooDataSet{name, title, theTree, vars, cutVar.expression(), wgtVarName} {}



////////////////////////////////////////////////////////////////////////////////
/// Constructor of a data set from (part of) a ROOT TTree.
///
/// \param[in] name Name of this dataset.
/// \param[in] title Title for e.g. plotting.
/// \param[in] theTree Tree to be imported.
/// \param[in] vars Defines the columns of the data set. For each dimension
/// specified, the TTree must have a branch with the same name. For category
/// branches, this branch should contain the numeric index value. Real dimensions
/// can be constructed from either 'double' or 'Float_t' tree branches. In the
/// latter case, an automatic conversion is applied.
/// \param[in] cuts Optional RooFormula expression to select the subset of the data points
/// to be imported. The cut expression can refer to any variable in `vars`.
/// \warning The expression only evaluates variables that are also in `vars`.
/// Passing e.g.
/// ```
/// RooDataSet("data", "data", tree, RooArgSet(x), "x>y")
/// ```
/// Will load `x` from the tree, but leave `y` at an undefined value.
/// If other expressions are needed, such as intermediate formula objects, use
/// RooDataSet::RooDataSet(const char*,const char*,TTree*,const RooArgSet&,const RooFormulaVar&,const char*)
/// \param[in] wgtVarName Name of the variable in `vars` that represents an event weight.
RooDataSet::RooDataSet(RooStringView name, RooStringView title, TTree* theTree,
const RooArgSet& vars, const char* cuts, const char* wgtVarName) :
RooAbsData(name,title,vars)
{
// Create tree version of datastore
auto tstore = std::make_unique<RooTreeDataStore>(name,title,_vars,*theTree,cuts,wgtVarName);

// Convert to vector datastore if needed
if (defaultStorageType==Tree) {
_dstore = std::move(tstore);
} else if (defaultStorageType==Vector) {
_dstore = std::make_unique<RooVectorDataStore>(name,title,_vars,wgtVarName);
static_cast<RooVectorDataStore&>(*_dstore).append(*tstore) ;
}

appendToDir(this,true) ;

initialize(wgtVarName) ;
TRACE_CREATE;
}



////////////////////////////////////////////////////////////////////////////////
/// Copy constructor

Expand Down
8 changes: 4 additions & 4 deletions roofit/roofitcore/test/TestStatistics/testRooRealL.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ TEST_P(RooRealL, getVal)
w.factory("Gaussian::g(x[-5,5],mu[0,-3,3],sigma[1,0.01,5.0])");
auto x = w.var("x");
RooAbsPdf *pdf = w.pdf("g");
std::unique_ptr<RooDataSet> data{pdf->generate(RooArgSet(*x), 10000)};
std::unique_ptr<RooDataSet> data{pdf->generate(*x, 10000)};
std::unique_ptr<RooAbsReal> nll{pdf->createNLL(*data)};

auto nominal_result = nll->getVal();
Expand Down Expand Up @@ -208,7 +208,7 @@ TEST_P(RooRealL, setVal)
w.factory("Gaussian::g(x[-5,5],mu[0,-3,3],sigma[1,0.01,5.0])");
auto x = w.var("x");
RooAbsPdf *pdf = w.pdf("g");
std::unique_ptr<RooDataSet> data{pdf->generate(RooArgSet(*x), 10000)};
std::unique_ptr<RooDataSet> data{pdf->generate(*x, 10000)};
std::unique_ptr<RooAbsReal> nll{pdf->createNLL(*data)};

RooFit::TestStatistics::RooRealL nll_new("nll_new", "new style NLL",
Expand Down Expand Up @@ -259,7 +259,7 @@ TEST_P(RealLVsMPFE, getVal)
w.factory("Gaussian::g(x[-5,5],mu[0,-3,3],sigma[1,0.01,5.0])");
auto x = w.var("x");
RooAbsPdf *pdf = w.pdf("g");
std::unique_ptr<RooDataSet> data{pdf->generate(RooArgSet(*x), 10000)};
std::unique_ptr<RooDataSet> data{pdf->generate(*x, 10000)};

std::unique_ptr<RooAbsReal> nll_mpfe{pdf->createNLL(*data)};

Expand Down Expand Up @@ -295,7 +295,7 @@ TEST_P(RealLVsMPFE, minimize)
RooRealVar *mu = w.var("mu");
RooRealVar *sigma = w.var("sigma");

std::unique_ptr<RooDataSet> data{pdf->generate(RooArgSet(*x), 10000)};
std::unique_ptr<RooDataSet> data{pdf->generate(*x, 10000)};
mu->setVal(-2.9);

// If we don't set sigma constant, the fit is not stable as we start with mu
Expand Down
44 changes: 22 additions & 22 deletions roofit/roofitcore/test/stressRooFit_tests.h
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ class TestBasic108 : public RooUnitTest {
// --------------------------------------------

// Sample 2000 events in (dt,mixState,tagFlav) from bmix
std::unique_ptr<RooDataSet> data{bmix.generate(RooArgSet(dt, mixState, tagFlav), 2000)};
std::unique_ptr<RooDataSet> data{bmix.generate({dt, mixState, tagFlav}, 2000)};

// S h o w d t d i s t r i b u t i o n w i t h c u s t o m b i n n i n g
// -------------------------------------------------------------------------------
Expand Down Expand Up @@ -1303,7 +1303,7 @@ class TestBasic301 : public RooUnitTest {
// ---------------------------------------------------------------------------------

// Generate 10000 events in x and y from model
std::unique_ptr<RooDataSet> data{model.generate(RooArgSet(x, y), 10000)};
std::unique_ptr<RooDataSet> data{model.generate({x, y}, 10000)};

// Plot x distribution of data and projection of model on x = Int(dy) model(x,y)
RooPlot *xframe = x.frame();
Expand Down Expand Up @@ -1528,7 +1528,7 @@ class TestBasic304 : public RooUnitTest {
// ---------------------------------------------------------------------------

// Generate 10000 events in x and y from gaussxy
std::unique_ptr<RooDataSet> data{gaussxy.generate(RooArgSet(x, y), 10000)};
std::unique_ptr<RooDataSet> data{gaussxy.generate({x, y}, 10000)};

// Plot x distribution of data and projection of gaussxy on x = Int(dy) gaussxy(x,y)
RooPlot *xframe = x.frame(Title("X projection of gauss(x)*gauss(y)"));
Expand Down Expand Up @@ -1589,7 +1589,7 @@ class TestBasic305 : public RooUnitTest {
// ---------------------------------------------------------------

// Generate 1000 events in x and y from model
std::unique_ptr<RooDataSet> data{model.generate(RooArgSet(x, y), 10000)};
std::unique_ptr<RooDataSet> data{model.generate({x, y}, 10000)};

// Plot x distribution of data and projection of model on x = Int(dy) model(x,y)
RooPlot *xframe = x.frame();
Expand Down Expand Up @@ -1738,7 +1738,7 @@ class TestBasic307 : public RooUnitTest {
// ------------------------------------------------------------------

// Specify external dataset with dterr values to use model_dm as conditional p.d.f.
std::unique_ptr<RooDataSet> data{model.generate(RooArgSet(dt, dterr), 10000)};
std::unique_ptr<RooDataSet> data{model.generate({dt, dterr}, 10000)};

// F i t c o n d i t i o n a l d e c a y _ d m ( d t | d t e r r )
// ---------------------------------------------------------------------
Expand Down Expand Up @@ -1877,7 +1877,7 @@ class TestBasic310 : public RooUnitTest {
RooBMixDecay bmix_gm1("bmix", "decay", dt, mixState, tagFlav, tau, dm, w, dw, gm1, RooBMixDecay::DoubleSided);

// Generate BMixing data with above set of event errors
std::unique_ptr<RooDataSet> data{bmix_gm1.generate(RooArgSet(dt, tagFlav, mixState), 20000)};
std::unique_ptr<RooDataSet> data{bmix_gm1.generate({dt, tagFlav, mixState}, 20000)};

// P l o t f u l l d e c a y d i s t r i b u t i o n
// ----------------------------------------------------------
Expand Down Expand Up @@ -1945,7 +1945,7 @@ class TestBasic311 : public RooUnitTest {
RooRealVar fsig("fsig", "signal fraction", 0.1, 0., 1.);
RooAddPdf model("model", "model", RooArgList(sig, bkg), fsig);

std::unique_ptr<RooDataSet> data{model.generate(RooArgSet(x, y, z), 20000)};
std::unique_ptr<RooDataSet> data{model.generate({x, y, z}, 20000)};

// P r o j e c t p d f a n d d a t a o n x
// -------------------------------------------------
Expand Down Expand Up @@ -2014,7 +2014,7 @@ class TestBasic312 : public RooUnitTest {
RooAddPdf model("model", "model", RooArgList(sig, bkg), f);

// Sample 10000 events in (x,y) from the model
std::unique_ptr<RooDataSet> modelData{model.generate(RooArgSet(x, y), 10000)};
std::unique_ptr<RooDataSet> modelData{model.generate({x, y}, 10000)};

// D e f i n e s i g n a l a n d s i d e b a n d r e g i o n s
// -------------------------------------------------------------------
Expand Down Expand Up @@ -2289,7 +2289,7 @@ class TestBasic316 : public RooUnitTest {
RooRealVar fsig("fsig", "signal fraction", 0.1, 0., 1.);
RooAddPdf model("model", "model", RooArgList(sig, bkg), fsig);

std::unique_ptr<RooDataSet> data{model.generate(RooArgSet(x, y, z), 20000)};
std::unique_ptr<RooDataSet> data{model.generate({x, y, z}, 20000)};

// P r o j e c t p d f a n d d a t a o n x
// -------------------------------------------------
Expand Down Expand Up @@ -2329,7 +2329,7 @@ class TestBasic316 : public RooUnitTest {
// ---------------------------------------------------------------------------------------------

// Generate large number of events for MC integration of pdf projection
std::unique_ptr<RooDataSet> mcprojData{model.generate(RooArgSet(x, y, z), 10000)};
std::unique_ptr<RooDataSet> mcprojData{model.generate({x, y, z}, 10000)};

// Calculate LL ratio for each generated event and select MC events with llratio)0.7
mcprojData->addColumn(llratio_func);
Expand Down Expand Up @@ -2586,7 +2586,7 @@ class TestBasic404 : public RooUnitTest {

// Generate a dummy dataset
RooRealVar x("x", "x", 0, 10);
std::unique_ptr<RooDataSet> data{RooPolynomial("p", "p", x).generate(RooArgSet(x, b0flav, tagCat), 10000)};
std::unique_ptr<RooDataSet> data{RooPolynomial("p", "p", x).generate({x, b0flav, tagCat}, 10000)};

// P r i n t t a b l e s o f c a t e g o r y c o n t e n t s o f d a t a s e t s
// ------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -2745,7 +2745,7 @@ class TestBasic406 : public RooUnitTest {
// Construct a dummy dataset with random values of tagCat and b0flav
RooRealVar x("x", "x", 0, 10);
RooPolynomial p("p", "p", x);
std::unique_ptr<RooDataSet> data{p.generate(RooArgSet(x, b0flav, tagCat), 10000)};
std::unique_ptr<RooDataSet> data{p.generate({x, b0flav, tagCat}, 10000)};

// C r e a t e a c a t - > c a t m a p p i n g c a t e g o r y
// ---------------------------------------------------------------------
Expand Down Expand Up @@ -2843,8 +2843,8 @@ class TestBasic501 : public RooUnitTest {
// ---------------------------------------------------------------

// Generate 1000 events in x and y from model
std::unique_ptr<RooDataSet> data{model.generate(RooArgSet(x), 100)};
std::unique_ptr<RooDataSet> data_ctl{model_ctl.generate(RooArgSet(x), 2000)};
std::unique_ptr<RooDataSet> data{model.generate({x}, 100)};
std::unique_ptr<RooDataSet> data_ctl{model_ctl.generate({x}, 2000)};

// C r e a t e i n d e x c a t e g o r y a n d j o i n s a m p l e s
// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -3640,7 +3640,7 @@ class TestBasic701 : public RooUnitTest {
RooProdPdf model("model", "model", shapePdf, Conditional(effPdf, cut));

// Generate some toy data from model
std::unique_ptr<RooDataSet> data{model.generate(RooArgSet(x, cut), 10000)};
std::unique_ptr<RooDataSet> data{model.generate({x, cut}, 10000)};

// F i t c o n d i t i o n a l e f f i c i e n c y p d f t o d a t a
// --------------------------------------------------------------------------
Expand Down Expand Up @@ -3721,7 +3721,7 @@ class TestBasic702 : public RooUnitTest {
RooProdPdf model("model", "model", shapePdf, Conditional(effPdf, cut));

// Generate some toy data from model
std::unique_ptr<RooDataSet> data{model.generate(RooArgSet(x, y, cut), 10000)};
std::unique_ptr<RooDataSet> data{model.generate({x, y, cut}, 10000)};

// F i t c o n d i t i o n a l e f f i c i e n c y p d f t o d a t a
// --------------------------------------------------------------------------
Expand Down Expand Up @@ -3855,7 +3855,7 @@ class TestBasic704 : public RooUnitTest {
RooRealSumPdf pdf("pdf", "pdf", RooArgList(ampl1, ampl2), RooArgList(f1, f2));

// Generate some toy data from pdf
std::unique_ptr<RooDataSet> data{pdf.generate(RooArgSet(t, cosa), 10000)};
std::unique_ptr<RooDataSet> data{pdf.generate({t, cosa}, 10000)};

// Fit pdf to toy data with only amplitude strength floating
pdf.fitTo(*data);
Expand Down Expand Up @@ -4119,7 +4119,7 @@ class TestBasic707 : public RooUnitTest {
RooRealVar y("y", "y", 0, 20);
RooPolynomial py("py", "py", y, RooArgList(0.01, 0.01, -0.0004));
RooProdPdf pxy("pxy", "pxy", RooArgSet(p, py));
std::unique_ptr<RooDataSet> data2{pxy.generate(RooArgSet(x, y), 1000)};
std::unique_ptr<RooDataSet> data2{pxy.generate({x, y}, 1000)};

// C r e a t e 2 - D k e r n e l e s t i m a t i o n p d f
// ---------------------------------------------------------------
Expand Down Expand Up @@ -4192,7 +4192,7 @@ class TestBasic708 : public RooUnitTest {
// ---------------------------------------------------

// Generate some data
std::unique_ptr<RooDataSet> data{bmix.generate(RooArgSet(dt, mixState, tagFlav), 10000)};
std::unique_ptr<RooDataSet> data{bmix.generate({dt, mixState, tagFlav}, 10000)};

// Plot B0 and B0bar tagged data separately
// For all plots below B0 and B0 tagged data will look somewhat differently
Expand Down Expand Up @@ -4244,7 +4244,7 @@ class TestBasic708 : public RooUnitTest {
// ---------------------------------------------------------------------------

// Generate some data
std::unique_ptr<RooDataSet> data2{bcp.generate(RooArgSet(dt, tagFlav), 10000)};
std::unique_ptr<RooDataSet> data2{bcp.generate({dt, tagFlav}, 10000)};

// Plot B0 and B0bar tagged data separately
RooPlot *frame4 = dt.frame(Title("B decay distribution with CPV(|l|=1,Im(l)=0.7) (B0/B0bar)"));
Expand All @@ -4261,7 +4261,7 @@ class TestBasic708 : public RooUnitTest {
absLambda = 0.7;

// Generate some data
std::unique_ptr<RooDataSet> data3{bcp.generate(RooArgSet(dt, tagFlav), 10000)};
std::unique_ptr<RooDataSet> data3{bcp.generate({dt, tagFlav}, 10000)};

// Plot B0 and B0bar tagged data separately (sin2b = 0.7 plus direct CPV |l|=0.5)
RooPlot *frame5 = dt.frame(Title("B decay distribution with CPV(|l|=0.7,Im(l)=0.7) (B0/B0bar)"));
Expand Down Expand Up @@ -4300,7 +4300,7 @@ class TestBasic708 : public RooUnitTest {
// -------------------------------------------------------------------------------------

// Generate some data
std::unique_ptr<RooDataSet> data4{bcpg.generate(RooArgSet(dt, tagFlav), 10000)};
std::unique_ptr<RooDataSet> data4{bcpg.generate({dt, tagFlav}, 10000)};

// Plot B0 and B0bar tagged data separately
RooPlot *frame6 = dt.frame(Title("B decay distribution with CPV(Im(l)=0.7,Re(l)=0.7,|l|=1,dG/G=0.5) (B0/B0bar)"));
Expand Down
Loading

0 comments on commit 387bd0d

Please sign in to comment.