Skip to content

Commit d536fdf

Browse files
authored
[RF] Update xRooFit
Update xRooFit again, to accommodate more features/improvements made in past week while working on the Higgs discovery workspaces.
1 parent 8fecb0b commit d536fdf

10 files changed

Lines changed: 728 additions & 354 deletions

File tree

roofit/xroofit/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ ROOT_STANDARD_LIBRARY_PACKAGE(RooFitXRooFit
1515
src/xRooNLLVar.cxx
1616
src/xRooNode.cxx
1717
src/xRooNode_interactive.cxx
18+
src/PythonInterface.cxx
1819
DICTIONARY_OPTIONS
1920
"-writeEmptyRootPCM"
2021
DEPENDENCIES

roofit/xroofit/inc/RooFit/xRooFit/xRooNLLVar.h

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class xRooNLLVar : public std::shared_ptr<RooAbsReal> {
6363
xValueWithError(const std::pair<double, double> &in = {0, 0}) : std::pair<double, double>(in) {}
6464
double value() const { return std::pair<double, double>::first; }
6565
double error() const { return std::pair<double, double>::second; }
66+
std::string __repr__() const { return Form("%g +/- %g", value(), error()); }
6667
};
6768

6869
void Print(Option_t *opt = "");
@@ -405,8 +406,8 @@ class xRooNLLVar : public std::shared_ptr<RooAbsReal> {
405406
double alt_value = std::numeric_limits<double>::quiet_NaN(),
406407
const xRooFit::Asymptotics::PLLType &pllType = xRooFit::Asymptotics::Unknown);
407408
xRooHypoSpace hypoSpace(const char *parName, xRooFit::TestStatistic::Type tsType, int nPoints = 0,
408-
double low = -std::numeric_limits<double>::infinity(),
409-
double high = std::numeric_limits<double>::infinity(),
409+
double low = std::numeric_limits<double>::quiet_NaN(),
410+
double high = std::numeric_limits<double>::quiet_NaN(),
410411
double alt_value = std::numeric_limits<double>::quiet_NaN())
411412
{
412413
return hypoSpace(parName, nPoints, low, high, alt_value, xRooFit::Asymptotics::Unknown, tsType);
@@ -511,11 +512,18 @@ class xRooNLLVar : public std::shared_ptr<RooAbsReal> {
511512
bool kReuseNLL = true;
512513
};
513514

515+
END_XROOFIT_NAMESPACE
516+
517+
#ifndef XROOFIT_NAMESPACE_NAME
518+
#ifdef XROOFIT_NAMESPACE
519+
#define XROOFIT_NAMESPACE_NAME XROOFIT_NAMESPACE
520+
#else
521+
#define XROOFIT_NAMESPACE_NAME
522+
#endif
523+
#endif
514524
namespace cling {
515-
std::string printValue(const xRooNLLVar::xValueWithError *val);
516-
std::string printValue(const std::map<std::string, xRooNLLVar::xValueWithError> *m);
525+
std::string printValue(const XROOFIT_NAMESPACE_NAME::xRooNLLVar::xValueWithError *val);
526+
std::string printValue(const std::map<std::string, XROOFIT_NAMESPACE_NAME::xRooNLLVar::xValueWithError> *m);
517527
} // namespace cling
518528

519-
END_XROOFIT_NAMESPACE
520-
521529
#endif // include guard

roofit/xroofit/inc/RooFit/xRooFit/xRooNode.h

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,23 @@ class xRooNode : public TNamed, public std::vector<std::shared_ptr<xRooNode>> {
197197
return aa == bb;
198198
};
199199
};
200-
auto begin() const -> xRooNodeIterator { return xRooNodeIterator(std::vector<std::shared_ptr<xRooNode>>::begin()); }
200+
auto begin() const -> xRooNodeIterator
201+
{
202+
// this update is to resolve need for calling browse() before iterating, e.g.
203+
// for(auto a : xRooNode(b).browse()) ...
204+
// can now become the more natural:
205+
// for(auto a : xRooNode(b)) ...
206+
// but would still need e.g. xRooNode(s).browse().size() rather than xRooNode(s).size() which would give 0 for
207+
// unpopulated case
208+
static bool browseLock =
209+
false; // need for blocking recursive calls to browse (since browse method uses the iterators)
210+
if (!browseLock && get() && empty()) {
211+
browseLock = true;
212+
const_cast<xRooNode &>(*this).browse();
213+
browseLock = false;
214+
}
215+
return xRooNodeIterator(std::vector<std::shared_ptr<xRooNode>>::begin());
216+
}
201217
auto end() const -> xRooNodeIterator { return xRooNodeIterator(std::vector<std::shared_ptr<xRooNode>>::end()); }
202218

203219
// needed in pyROOT to avoid it creating iterators that follow the 'get' to death
@@ -312,6 +328,9 @@ class xRooNode : public TNamed, public std::vector<std::shared_ptr<xRooNode>> {
312328
xRooNode datasets()
313329
const; // datasets corresponding to this pdf (parent nodes that do observable selections automatically applied)
314330

331+
xRooNode parents() const; // the clients of this node
332+
xRooNode args() const; // all the nodes underneath this node
333+
315334
xRooNode Replace(const xRooNode &node); // use to replace a node in the tree at the location of this node
316335
xRooNode Remove(const xRooNode &child);
317336
xRooNode
@@ -326,6 +345,7 @@ class xRooNode : public TNamed, public std::vector<std::shared_ptr<xRooNode>> {
326345

327346
xRooNode reduced(const std::string &range = "", bool invert = false)
328347
const; // return a node representing reduced version of this node, will use the SetRange to reduce if blank
348+
xRooNode reduced(const std::function<bool(const xRooNode &)> selector) const;
329349

330350
// following versions are for the menu in the GUI
331351
/** @private */
@@ -537,10 +557,17 @@ class xRooNode : public TNamed, public std::vector<std::shared_ptr<xRooNode>> {
537557
ClassDefOverride(xRooNode, 0)
538558
};
539559

560+
END_XROOFIT_NAMESPACE
561+
562+
#ifndef XROOFIT_NAMESPACE_NAME
563+
#ifdef XROOFIT_NAMESPACE
564+
#define XROOFIT_NAMESPACE_NAME XROOFIT_NAMESPACE
565+
#else
566+
#define XROOFIT_NAMESPACE_NAME
567+
#endif
568+
#endif
540569
namespace cling {
541-
std::string printValue(const xRooNode *val);
570+
std::string printValue(const XROOFIT_NAMESPACE_NAME::xRooNode *val);
542571
}
543572

544-
END_XROOFIT_NAMESPACE
545-
546573
#endif // include guard
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include "./PythonInterface.h"
2+
3+
#include <TSystem.h>
4+
5+
namespace xPython {
6+
7+
// https://docs.python.org/3.11/c-api/init.html#c.Py_IsInitialized
8+
bool isPythonInitialized() {
9+
using fn_t = int (*)();
10+
static auto f = reinterpret_cast<fn_t>(gSystem->DynFindSymbol("*", "Py_IsInitialized"));
11+
return f && f();
12+
}
13+
14+
// https://docs.python.org/3/c-api/sys.html#c.PySys_WriteStdout
15+
void writeStdoutLine(const char *msg) {
16+
using fn_t = void (*)(const char *, ...);
17+
static auto f = reinterpret_cast<fn_t>(gSystem->DynFindSymbol("*", "PySys_WriteStdout"));
18+
if (f)
19+
f("%s\n", msg);
20+
}
21+
22+
// https://docs.python.org/3/c-api/sys.html#c.PySys_WriteStderr
23+
void writeStderrLine(const char *msg) {
24+
using fn_t = void (*)(const char *, ...);
25+
static auto f = reinterpret_cast<fn_t>(gSystem->DynFindSymbol("*", "PySys_WriteStderr"));
26+
if (f)
27+
f("%s\n", msg);
28+
}
29+
30+
} // namespace xPython
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef xRooFit_PythonInterface_h
2+
#define xRooFit_PythonInterface_h
3+
4+
// Helper functions to use the Python C API without introducint a link-time
5+
// dependency on libpython. These are intended to be used only when xRooFit is
6+
// used from Python, where libpython is loaded by default and the required
7+
// symbols can be found with gSystem.
8+
9+
namespace xPython {
10+
11+
bool isPythonInitialized();
12+
13+
// Don't call these other functions if Python is not initialized!
14+
void writeStdoutLine(const char *msg);
15+
void writeStderrLine(const char *msg);
16+
} // namespace xPython
17+
18+
#endif

0 commit comments

Comments
 (0)