File indexing completed on 2025-12-25 10:03:16
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #include "Config.h"
0014
0015 #ifdef XROOFIT_USE_PRAGMA_ONCE
0016 #pragma once
0017 #endif
0018 #if !defined(XROOFIT_XROONODE_H) || defined(XROOFIT_USE_PRAGMA_ONCE)
0019 #ifndef XROOFIT_USE_PRAGMA_ONCE
0020 #define XROOFIT_XROONODE_H
0021 #endif
0022
0023 #include "TNamed.h"
0024 #include <vector>
0025 #include <functional>
0026
0027 class RooWorkspace;
0028 class RooAbsReal;
0029 class TH1;
0030 class RooAbsLValue;
0031 class RooArgList;
0032 class RooAbsBinning;
0033 class RooFitResult;
0034 class TGraph;
0035 class TAxis;
0036 class TGListTreeItem;
0037 class TGListTree;
0038 class TVirtualPad;
0039 class TStyle;
0040
0041 #include "xRooFit.h"
0042 #include "RooLinkedList.h"
0043 #include "RooCmdArg.h"
0044 #include "TQObject.h"
0045 #include "TMatrixDSym.h"
0046
0047 BEGIN_XROOFIT_NAMESPACE
0048
0049 class xRooNode;
0050 class xRooNLLVar;
0051
0052 class xRooNode : public TNamed, public std::vector<std::shared_ptr<xRooNode>> {
0053
0054 public:
0055
0056
0057
0058 static std::map<std::string, std::tuple<std::function<double(double, double, double)>, bool>> auxFunctions;
0059
0060 static void SetAuxFunction(const char *title, const std::function<double(double, double, double)> &func,
0061 bool symmetrize = false);
0062
0063
0064
0065 static inline bool isNull(const std::shared_ptr<xRooNode> &x) { return x == nullptr; }
0066
0067
0068
0069
0070
0071 xRooNode(const char *type, const char *name, const char *title = "");
0072
0073
0074 template <typename T>
0075 xRooNode(const char *name, const char *title) : TNamed(name, title), fComp(std::make_shared<T>())
0076 {
0077 if (auto x = get<TNamed>(); x) {
0078 x->SetNameTitle(name, title);
0079 }
0080 }
0081 xRooNode(const char *name = "", const std::shared_ptr<TObject> &comp = nullptr,
0082 const std::shared_ptr<xRooNode> &parent = nullptr);
0083
0084
0085 xRooNode(const char *name, const std::shared_ptr<TObject> &comp, const xRooNode &parent)
0086 : xRooNode(name, comp, std::make_shared<xRooNode>(parent))
0087 {
0088 }
0089
0090
0091 xRooNode(const char *name, const TObject &comp, const std::shared_ptr<xRooNode> &parent)
0092 : xRooNode(name, std::shared_ptr<TObject>(const_cast<TObject *>(&comp), [](TObject *) {}), parent)
0093 {
0094 }
0095
0096
0097 xRooNode(const char *name, const TObject &comp, const xRooNode &parent)
0098 : xRooNode(name, std::shared_ptr<TObject>(const_cast<TObject *>(&comp), [](TObject *) {}), parent)
0099 {
0100 }
0101
0102 xRooNode(const TObject &comp, const std::shared_ptr<xRooNode> &parent = nullptr);
0103
0104 xRooNode(const TObject &comp, const xRooNode &parent) : xRooNode(comp, std::make_shared<xRooNode>(parent)) {}
0105
0106 xRooNode(const std::shared_ptr<TObject> &comp, const std::shared_ptr<xRooNode> &parent = nullptr);
0107 template <typename T>
0108
0109 xRooNode(const std::shared_ptr<T> &comp, const std::shared_ptr<xRooNode> &parent = nullptr)
0110 : xRooNode(std::dynamic_pointer_cast<TObject>(comp), parent)
0111 {
0112 }
0113
0114 template <typename T>
0115 xRooNode(const std::shared_ptr<T> &comp, const xRooNode &parent)
0116 : xRooNode(std::dynamic_pointer_cast<TObject>(comp), std::make_shared<xRooNode>(parent))
0117 {
0118 }
0119
0120 template <typename T>
0121 xRooNode(const std::shared_ptr<const T> &comp, const std::shared_ptr<xRooNode> &parent = nullptr)
0122 : xRooNode(std::dynamic_pointer_cast<TObject>(std::const_pointer_cast<T>(comp)), parent)
0123 {
0124 }
0125
0126 template <typename T>
0127 xRooNode(const std::shared_ptr<const T> &comp, const xRooNode &parent)
0128 : xRooNode(std::dynamic_pointer_cast<TObject>(std::const_pointer_cast<T>(comp)),
0129 std::make_shared<xRooNode>(parent))
0130 {
0131 }
0132
0133 xRooNode(double value);
0134
0135 ~xRooNode() override;
0136
0137 void SetName(const char *name) override;
0138 void SetTitle(const char *title) override;
0139
0140
0141 const char *GetNodeType() const;
0142
0143
0144 explicit operator bool() const { return strlen(GetName()) || get(); }
0145
0146
0147 const std::shared_ptr<xRooNode> &at(size_t idx, bool browseResult = true) const
0148 {
0149 IsFolder();
0150 auto &out = std::vector<std::shared_ptr<xRooNode>>::at(idx);
0151 if (browseResult && out)
0152 out->browse();
0153 return out;
0154 }
0155 std::shared_ptr<xRooNode> at(const std::string &name, bool browseResult = true) const;
0156
0157 RooArgList argList() const;
0158
0159 std::shared_ptr<xRooNode>
0160 find(const std::string &name, bool browseResult = true) const;
0161 bool contains(const std::string &name) const;
0162
0163
0164 std::shared_ptr<xRooNode> operator[](size_t idx) { return at(idx); }
0165 std::shared_ptr<xRooNode> operator[](const std::string &name);
0166
0167
0168 class xRooNodeIterator : public std::vector<std::shared_ptr<xRooNode>>::const_iterator {
0169 public:
0170 xRooNodeIterator(std::vector<std::shared_ptr<xRooNode>>::const_iterator itr)
0171 : std::vector<std::shared_ptr<xRooNode>>::const_iterator(itr)
0172 {
0173 }
0174 std::iterator_traits<const std::shared_ptr<xRooNode> *>::reference operator*() const
0175 {
0176 const std::shared_ptr<xRooNode> &out = std::vector<std::shared_ptr<xRooNode>>::const_iterator::operator*();
0177 if (out->get() && out->empty()) {
0178 out->browse();
0179 }
0180 return std::vector<std::shared_ptr<xRooNode>>::const_iterator::operator*();
0181 }
0182 bool operator!=(xRooNodeIterator const &b) const
0183 {
0184 const std::vector<std::shared_ptr<xRooNode>>::const_iterator &aa = (*this);
0185 const std::vector<std::shared_ptr<xRooNode>>::const_iterator &bb = b;
0186 return aa != bb;
0187 };
0188 xRooNodeIterator const &operator++()
0189 {
0190 std::vector<std::shared_ptr<xRooNode>>::const_iterator::operator++();
0191 return *this;
0192 }
0193 bool operator==(xRooNodeIterator const &b) const
0194 {
0195 const std::vector<std::shared_ptr<xRooNode>>::const_iterator &aa = (*this);
0196 const std::vector<std::shared_ptr<xRooNode>>::const_iterator &bb = b;
0197 return aa == bb;
0198 };
0199 };
0200 auto begin() const -> xRooNodeIterator { return xRooNodeIterator(std::vector<std::shared_ptr<xRooNode>>::begin()); }
0201 auto end() const -> xRooNodeIterator { return xRooNodeIterator(std::vector<std::shared_ptr<xRooNode>>::end()); }
0202
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212
0213 void Browse(TBrowser *b = nullptr) override;
0214
0215 bool IsFolder() const override;
0216
0217 const char *GetIconName() const override;
0218 void Inspect() const override;
0219
0220
0221 xRooNode &browse();
0222
0223
0224 std::string GetPath() const;
0225 void Print(Option_t *opt = "") const override;
0226
0227
0228
0229 xRooNode &operator=(const TObject &o);
0230
0231 TObject *get() const { return fComp.get(); }
0232 template <typename T>
0233 T *get() const
0234 {
0235 return dynamic_cast<T *>(get());
0236 }
0237
0238 TObject *xget() const { return xget<TObject>(); }
0239 template <typename T>
0240 T *xget() const
0241 {
0242 for (auto &c : fBrowsables) {
0243 if (strcmp(c->GetName(), ".memory") == 0) {
0244 return c->get<T>();
0245 }
0246 }
0247 return nullptr;
0248 }
0249
0250 TObject *operator->() const { return get(); }
0251
0252 RooWorkspace *ws() const;
0253
0254
0255 std::shared_ptr<TObject>
0256 acquire(const std::shared_ptr<TObject> &arg, bool checkFactory = false, bool mustBeNew = false);
0257
0258
0259 template <typename T, typename... Args>
0260 std::shared_ptr<T> acquire(Args &&...args)
0261 {
0262 return std::dynamic_pointer_cast<T>(acquire(std::make_shared<T>(std::forward<Args>(args)...)));
0263 }
0264
0265 template <typename T, typename T2, typename... Args>
0266
0267
0268 std::shared_ptr<T> acquire2(Args &&...args)
0269 {
0270 return std::dynamic_pointer_cast<T>(acquire(std::make_shared<T2>(std::forward<Args>(args)...)));
0271 }
0272
0273 template <typename T, typename... Args>
0274 std::shared_ptr<T> acquireNew(Args &&...args)
0275 {
0276 return std::dynamic_pointer_cast<T>(acquire(std::make_shared<T>(std::forward<Args>(args)...), false, true));
0277 }
0278
0279 std::shared_ptr<TObject> getObject(const std::string &name, const std::string &type = "") const;
0280
0281 template <typename T>
0282 std::shared_ptr<T> getObject(const std::string &name) const
0283 {
0284 return std::dynamic_pointer_cast<T>(getObject(name, T::Class_Name()));
0285 }
0286
0287
0288 xRooNode shallowCopy(const std::string &name, std::shared_ptr<xRooNode> parent = nullptr);
0289
0290 std::shared_ptr<TObject> convertForAcquisition(xRooNode &acquirer, const char *opt = "") const;
0291
0292 xRooNode vars() const;
0293 xRooNode obs() const;
0294 xRooNode robs() const;
0295 xRooNode globs() const;
0296 xRooNode pars() const;
0297 xRooNode floats() const;
0298 xRooNode consts() const;
0299
0300 xRooNode poi() const;
0301 xRooNode np() const;
0302 xRooNode pp() const;
0303
0304 xRooNode components() const;
0305 xRooNode factors() const;
0306 xRooNode variations() const;
0307 xRooNode coefs(bool recurse = false) const;
0308 xRooNode coords(bool setVals = true) const;
0309 xRooNode bins() const;
0310
0311 xRooNode constraints() const;
0312 xRooNode datasets()
0313 const;
0314
0315 xRooNode Replace(const xRooNode &node);
0316 xRooNode Remove(const xRooNode &child);
0317 xRooNode
0318 Add(const xRooNode &child,
0319 Option_t *opt =
0320 "");
0321 xRooNode Multiply(const xRooNode &child, Option_t *opt = "");
0322 xRooNode Vary(const xRooNode &child);
0323 xRooNode Constrain(const xRooNode &child);
0324
0325 xRooNode Combine(const xRooNode &rhs, bool silent = false);
0326
0327 xRooNode reduced(const std::string &range = "", bool invert = false)
0328 const;
0329
0330
0331
0332 void _Add_(const char *name, const char *opt);
0333
0334 xRooNode _Multiply_(const char *what) { return Multiply(what); }
0335
0336 void _Vary_(const char *what);
0337
0338 xRooNode _Constrain_(const char *what) { return Constrain(what); }
0339
0340 void _ShowVars_(bool set = true);
0341
0342 bool _IsShowVars_() const;
0343
0344 void _SetAttribute_(const char *name, const char *value = nullptr);
0345
0346 void SetHidden(bool set = true);
0347 bool IsHidden() const;
0348
0349 bool SetContents(const TObject &obj)
0350 {
0351 operator=(obj);
0352 return true;
0353 }
0354 bool SetData(const TObject &obj, const xRooNode &data = "obsData");
0355
0356 bool SetContent(double value);
0357 bool SetContent(double value, const char *par, double parVal = 1);
0358 bool SetContents(const TObject &obj, const char *par, double parVal)
0359 {
0360 variations()[TString::Format("%s=%g", par, parVal).Data()]->operator=(obj);
0361 return true;
0362 }
0363 bool SetBinError(int bin, double value);
0364 bool SetBinContent(int bin, double value, const char *par = nullptr, double parVal = 1);
0365 bool SetBinData(int bin, double value, const xRooNode &data = "obsData");
0366
0367
0368 void _SetContent_(double value);
0369
0370 void _SetBinContent_(int bin, double value, const char *par = "", double parVal = 1);
0371
0372 bool SetXaxis(const RooAbsBinning &binning);
0373 bool SetXaxis(TAxis *ax);
0374 bool SetXaxis(const char *name, const char *title, int nbins, double low, double high);
0375 bool SetXaxis(const char *name, const char *title, int nbins, const double *bins);
0376 bool SetXaxis(const char *title, int nbins, double low, double high)
0377 {
0378 return SetXaxis("xaxis", title, nbins, low, high);
0379 }
0380 bool SetXaxis(const char *title, int nbins, const double *bins) { return SetXaxis("xaxis", title, nbins, bins); }
0381 bool SetXaxis(int nbins, double low, double high) { return SetXaxis("xaxis", "", nbins, low, high); }
0382 bool SetXaxis(int nbins, const double *bins) { return SetXaxis("xaxis", "", nbins, bins); }
0383
0384 std::shared_ptr<TStyle>
0385 style(TObject *initObject = nullptr, bool autoCreate = true) const;
0386 xRooNode styles(TObject *initObject = nullptr, bool autoCreate = true) const;
0387
0388 TAxis *GetXaxis() const;
0389
0390 double GetBinData(int bin, const xRooNode &data = "obsData");
0391 double GetBinContent(int bin) const { return GetBinContents(bin, bin).at(0); }
0392 std::vector<double> GetBinContents(int binStart = 1, int binEnd = 0) const;
0393 double
0394 GetBinError(int bin, const xRooNode &fr = "", int nToys = 0, bool errorsHi = false, bool errorsLo = false) const;
0395 std::vector<double> GetBinErrors(int binStart = 1, int binEnd = 0, const xRooNode &fr = "", int nToys = 0,
0396 bool errorsHi = false, bool errorsLo = false) const;
0397 std::pair<double, double> IntegralAndError(const xRooNode &fr = "", const char *rangeName = nullptr, int nToys = 0,
0398 bool errorsHi = false, bool errorsLo = false) const;
0399
0400 std::vector<double> GetBinErrorsHi(int binStart = 1, int binEnd = 0, const xRooNode &fr = "", int nToys = 0) const
0401 {
0402 return GetBinErrors(binStart, binEnd, fr, nToys, true, false);
0403 }
0404 std::vector<double> GetBinErrorsLo(int binStart = 1, int binEnd = 0, const xRooNode &fr = "", int nToys = 0) const
0405 {
0406 return GetBinErrors(binStart, binEnd, fr, nToys, false, true);
0407 }
0408 double GetBinErrorHi(int bin, const xRooNode &fr = "", int nToys = 0) const
0409 {
0410 return GetBinError(bin, fr, nToys, true, false);
0411 }
0412 double GetBinErrorLo(int bin, const xRooNode &fr = "", int nToys = 0) const
0413 {
0414 return GetBinError(bin, fr, nToys, false, true);
0415 }
0416
0417
0418 double GetContent() const { return GetBinContent(fBinNumber); }
0419 double GetError(const xRooNode &fr = "", int nToys = 0, bool errorsHi = false, bool errorsLo = false) const
0420 {
0421 return (fBinNumber == -1) ? IntegralAndError(fr, "", nToys, errorsHi, errorsLo).second
0422 : GetBinError(fBinNumber, fr, nToys, errorsHi, errorsLo);
0423 }
0424 double GetErrorHi(const xRooNode &fr = "", int nToys = 0) const { return GetError(fr, nToys, true, false); }
0425 double GetErrorLo(const xRooNode &fr = "", int nToys = 0) const { return GetError(fr, nToys, false, true); }
0426 double GetData(const xRooNode &data = "obsData") { return GetBinData(fBinNumber, data); }
0427
0428
0429 std::vector<double> contents() const;
0430 TMatrixDSym covariances(const xRooNode &fr = "") const;
0431 xRooNLLVar nll(const xRooNode &_data, std::initializer_list<RooCmdArg> nllOpts) const;
0432 xRooNLLVar nll(const xRooNode &_data, const RooLinkedList &nllOpts) const;
0433 xRooNLLVar nll(const xRooNode &_data = "") const;
0434
0435 xRooNLLVar
0436 nll(const char *_data,
0437 std::initializer_list<RooCmdArg> nllOpts = {}) const;
0438
0439
0440 xRooNode fitResult(const char *opt = "") const;
0441 void SetFitResult(const RooFitResult *fr = nullptr);
0442 void SetFitResult(const std::shared_ptr<const RooFitResult> &fr) { SetFitResult(fr.get()); }
0443 void SetFitResult(const xRooNode &fr);
0444
0445 xRooNode
0446 generate(const xRooNode &fr = "", bool expected = false,
0447 int seed = 0);
0448
0449
0450 void _fit_(const char *constParValues = "", const char *options = "GoF");
0451
0452 void _generate_(const char *name = "", bool expected = false);
0453
0454 void _scan_(const char *what = "plr", double nToys = 0, const char *xvar = "", int nPointsX = 0, double lowX = 0,
0455 double highX = 0 ,
0456 const char *constParValues = "", const char *options = "");
0457
0458
0459
0460
0461
0462
0463
0464
0465
0466
0467
0468
0469
0470
0471
0472
0473 void Checked(TObject *obj, bool val);
0474 void SetChecked(bool val = true) { Checked(this, val); }
0475
0476
0477 xRooNode histo(const xRooNode &vars = "x", const xRooNode &fr = "", bool content = true, bool errors = true,
0478 bool stack = true, bool errorsHi = false, bool errorsLo = false, int nErrorToys = 0) const;
0479
0480 xRooNode filter(const xRooNode &range) const;
0481
0482 TGraph *BuildGraph(RooAbsLValue *v = nullptr, bool includeZeros = false, TVirtualPad *fromPad = nullptr) const;
0483 TH1 *BuildHistogram(RooAbsLValue *v = nullptr, bool empty = false, bool errors = false, int binStart = 1,
0484 int binEnd = 0, const xRooNode &fr = "", bool errorsHi = false, bool errorsLo = false,
0485 int nErrorToys = 0, TH1 *templateHist = nullptr, bool nostack = true,
0486 bool setInterp = false) const;
0487 xRooNode mainChild() const;
0488 void Draw(Option_t *opt = "") override;
0489
0490 void SaveAs(const char *filename = "", Option_t *option = "") const override;
0491
0492
0493 TGListTreeItem *GetTreeItem(TBrowser *b) const;
0494
0495 TGListTree *GetListTree(TBrowser *b) const;
0496
0497 static void Interactive_PLLPlot();
0498 static void Interactive_Pull();
0499 class InteractiveObject : public TQObject {
0500 public:
0501 void Interactive_PLLPlot(TVirtualPad *pad, TObject *obj, Int_t x, Int_t y);
0502 ClassDefOverride(InteractiveObject, 0)
0503 };
0504 static InteractiveObject *gIntObj;
0505
0506 mutable std::shared_ptr<TObject> fComp;
0507 int fTimes = 1;
0508 int fBinNumber = -1;
0509 std::shared_ptr<xRooNode> fParent;
0510 std::string fFolder;
0511
0512 void SetRange(const char *range, double low = std::numeric_limits<double>::quiet_NaN(),
0513 double high = std::numeric_limits<double>::quiet_NaN());
0514 const char *GetRange() const;
0515 mutable std::string fRange;
0516
0517
0518 mutable std::shared_ptr<TAxis>
0519 fXAxis;
0520
0521 mutable bool fInterrupted = false;
0522
0523 bool fAcquirer = false;
0524 std::shared_ptr<xRooNode> fProvider;
0525
0526 std::shared_ptr<xRooNode> parentPdf() const;
0527
0528
0529 void sterilize() const;
0530
0531 std::vector<std::shared_ptr<xRooNode>> fBrowsables;
0532 std::function<xRooNode(xRooNode *)> fBrowseOperation;
0533
0534
0535 std::shared_ptr<xRooNode> getBrowsable(const char *name) const;
0536
0537 ClassDefOverride(xRooNode, 0)
0538 };
0539
0540 namespace cling {
0541 std::string printValue(const xRooNode *val);
0542 }
0543
0544 END_XROOFIT_NAMESPACE
0545
0546 #endif