Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooFitImplHelpers.h
Go to the documentation of this file.
1/*
2 * Project: RooFit
3 *
4 * Copyright (c) 2023, CERN
5 *
6 * Redistribution and use in source and binary forms,
7 * with or without modification, are permitted according to the terms
8 * listed in LICENSE (http://roofit.sourceforge.net/license.txt)
9 */
10
11#ifndef RooFit_RooFitImplHelpers_h
12#define RooFit_RooFitImplHelpers_h
13
14#include <RooAbsArg.h>
15#include <RooAbsPdf.h>
16#include <RooAbsReal.h>
17#include <RooMsgService.h>
18
19#include <RooNaNPacker.h>
20
21#include <TMath.h>
22
23#include <sstream>
24#include <string>
25#include <string_view>
26#include <utility>
27#include <vector>
28
29class RooAbsPdf;
30class RooAbsData;
31
32/// Disable all caches for sub-branches in an expression tree.
33/// This is helpful when an expression with cached sub-branches needs to be integrated numerically.
35public:
36 /// Inhibit all dirty-state propagation, and assume every node as dirty.
37 /// \param[in] oldState Restore this state when going out of scope.
39
42
44
45private:
47};
48
49/// Scope guard that temporarily changes the operation mode of one or more
50/// RooAbsArg instances. Each call to change() records the arg's current
51/// operMode before flipping it to the requested mode (non-recursively, i.e.
52/// value clients are not touched). Destruction (or an explicit clear())
53/// restores every recorded mode in LIFO order.
54///
55/// The class is movable but not copyable, so it can be returned from
56/// functions that build up a batch of changes to hand to the caller.
58public:
59 ChangeOperModeRAII() = default;
60
61 /// Convenience ctor: behaves like a scope guard for a single arg.
63
65
70
71 /// Record arg's current operMode and flip it to opMode. If the current
72 /// mode already equals opMode, this is a no-op (nothing to restore).
74 {
75 if (opMode == arg->operMode())
76 return;
77 _entries.emplace_back(arg, arg->operMode());
78 arg->setOperMode(opMode, /*recurseADirty=*/false);
79 }
80
81 /// Restore every recorded change right away, emptying this guard.
82 void clear()
83 {
84 for (auto it = _entries.rbegin(); it != _entries.rend(); ++it) {
85 it->first->setOperMode(it->second, /*recurseADirty=*/false);
86 }
87 _entries.clear();
88 }
89
90 bool empty() const { return _entries.empty(); }
91
92private:
93 std::vector<std::pair<RooAbsArg *, RooAbsArg::OperMode>> _entries;
94};
95
96namespace RooHelpers {
97
98std::pair<double, double> getRangeOrBinningInterval(RooAbsArg const *arg, const char *rangeName);
99
100bool checkIfRangesOverlap(RooArgSet const &observables, std::vector<std::string> const &rangeNames);
101
102std::string getColonSeparatedNameString(RooArgSet const &argSet, char delim = ':');
103RooArgSet selectFromArgSet(RooArgSet const &, std::string const &names);
104
105namespace Detail {
106
109
110} // namespace Detail
111
112/// Clone RooAbsArg object and reattach to original parameters.
113template <class T>
114std::unique_ptr<T> cloneTreeWithSameParameters(T const &arg, RooArgSet const *observables = nullptr)
115{
116 return std::unique_ptr<T>{static_cast<T *>(Detail::cloneTreeWithSameParametersImpl(arg, observables))};
117}
118
119std::string getRangeNameForSimComponent(std::string const &rangeName, bool splitRange, std::string const &catName);
120
123 bool isBinnedL = false;
124};
125
127
129
130} // namespace RooHelpers
131
132namespace RooFit::Detail {
133
134std::string makeValidVarName(std::string const &in);
135
136void replaceAll(std::string &inOut, std::string_view what, std::string_view with);
137
139
140// Inlined because this is called inside RooAbsPdf::getValV(), and therefore
141// performance critical.
142inline double normalizeWithNaNPacking(RooAbsPdf const &pdf, double rawVal, double normVal)
143{
144
145 if (normVal < 0. || (normVal == 0. && rawVal != 0)) {
146 // Unreasonable normalisations. A zero integral can be tolerated if the function vanishes, though.
147 const std::string msg = "p.d.f normalization integral is zero or negative: " + std::to_string(normVal);
148 pdf.logEvalError(msg.c_str());
149 return RooNaNPacker::packFloatIntoNaN(-normVal + (rawVal < 0. ? -rawVal : 0.));
150 }
151
152 if (rawVal < 0.) {
153 std::stringstream ss;
154 ss << "p.d.f value is less than zero (" << rawVal << "), trying to recover";
155 pdf.logEvalError(ss.str().c_str());
157 }
158
159 if (TMath::IsNaN(rawVal)) {
160 pdf.logEvalError("p.d.f value is Not-a-Number");
161 return rawVal;
162 }
163
164 return (rawVal == 0. && normVal == 0.) ? 0. : rawVal / normVal;
165}
166
167} // namespace RooFit::Detail
168
169double toDouble(const char *s);
170double toDouble(const std::string &s);
171
172#endif
double toDouble(const char *s)
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void input
Scope guard that temporarily changes the operation mode of one or more RooAbsArg instances.
ChangeOperModeRAII(RooAbsArg *arg, RooAbsArg::OperMode opMode)
Convenience ctor: behaves like a scope guard for a single arg.
ChangeOperModeRAII(ChangeOperModeRAII &&)=default
ChangeOperModeRAII(ChangeOperModeRAII const &)=delete
void change(RooAbsArg *arg, RooAbsArg::OperMode opMode)
Record arg's current operMode and flip it to opMode.
void clear()
Restore every recorded change right away, emptying this guard.
std::vector< std::pair< RooAbsArg *, RooAbsArg::OperMode > > _entries
ChangeOperModeRAII & operator=(ChangeOperModeRAII const &)=delete
ChangeOperModeRAII()=default
ChangeOperModeRAII & operator=(ChangeOperModeRAII &&)=default
Disable all caches for sub-branches in an expression tree.
DisableCachingRAII(DisableCachingRAII const &other)=delete
DisableCachingRAII & operator=(DisableCachingRAII const &other)=delete
DisableCachingRAII(bool oldState)
Inhibit all dirty-state propagation, and assume every node as dirty.
Common abstract base class for objects that represent a value and a "shape" in RooFit.
Definition RooAbsArg.h:76
void setOperMode(OperMode mode, bool recurseADirty=true)
Set the operation mode of this node.
static void setDirtyInhibit(bool flag)
Control global dirty inhibit mode.
OperMode operMode() const
Query the operation mode of this node.
Definition RooAbsArg.h:419
Abstract container object that can hold multiple RooAbsArg objects.
Abstract base class for binned and unbinned datasets.
Definition RooAbsData.h:56
Abstract interface for all probability density functions.
Definition RooAbsPdf.h:32
void logEvalError(const char *message, const char *serverValueString=nullptr) const
Log evaluation error message.
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:24
std::string makeValidVarName(std::string const &in)
void replaceAll(std::string &inOut, std::string_view what, std::string_view with)
std::string makeSliceCutString(RooArgSet const &sliceDataSet)
double normalizeWithNaNPacking(RooAbsPdf const &pdf, double rawVal, double normVal)
RooAbsArg * cloneTreeWithSameParametersImpl(RooAbsArg const &arg, RooArgSet const *observables)
bool snapshotImpl(RooAbsCollection const &input, RooAbsCollection &output, bool deepCopy, RooArgSet const *observables)
RooArgSet selectFromArgSet(RooArgSet const &, std::string const &names)
bool checkIfRangesOverlap(RooArgSet const &observables, std::vector< std::string > const &rangeNames)
std::unique_ptr< T > cloneTreeWithSameParameters(T const &arg, RooArgSet const *observables=nullptr)
Clone RooAbsArg object and reattach to original parameters.
std::string getRangeNameForSimComponent(std::string const &rangeName, bool splitRange, std::string const &catName)
void getSortedComputationGraph(RooAbsArg const &func, RooArgSet &out)
BinnedLOutput getBinnedL(RooAbsPdf const &pdf)
std::string getColonSeparatedNameString(RooArgSet const &argSet, char delim=':')
std::pair< double, double > getRangeOrBinningInterval(RooAbsArg const *arg, const char *rangeName)
Bool_t IsNaN(Double_t x)
Definition TMath.h:903
static const char * what
Definition stlLoader.cc:5
static double packFloatIntoNaN(float payload)
Pack float into mantissa of a NaN.
static void output()