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/// Check that `function` is constant (flat) inside each bin defined by the
131/// sorted `boundaries` when scanning the observable `obs`. Several interior
132/// points are sampled per bin and compared to the bin's first sample; if any
133/// of them deviates by more than `relTol` (relative to the value scale), the
134/// function is not flat and false is returned. The value of `obs` is restored
135/// on return.
136bool isFunctionFlatInBins(const RooAbsReal &function, RooAbsRealLValue &obs, std::span<const double> boundaries,
137 double relTol = 1e-9);
138
139} // namespace RooHelpers
140
141namespace RooFit::Detail {
142
143std::string makeValidVarName(std::string const &in);
144
145void replaceAll(std::string &inOut, std::string_view what, std::string_view with);
146
148
149// Inlined because this is called inside RooAbsPdf::getValV(), and therefore
150// performance critical.
151inline double normalizeWithNaNPacking(RooAbsPdf const &pdf, double rawVal, double normVal)
152{
153
154 if (normVal < 0. || (normVal == 0. && rawVal != 0)) {
155 // Unreasonable normalisations. A zero integral can be tolerated if the function vanishes, though.
156 const std::string msg = "p.d.f normalization integral is zero or negative: " + std::to_string(normVal);
157 pdf.logEvalError(msg.c_str());
158 return RooNaNPacker::packFloatIntoNaN(-normVal + (rawVal < 0. ? -rawVal : 0.));
159 }
160
161 if (rawVal < 0.) {
162 std::stringstream ss;
163 ss << "p.d.f value is less than zero (" << rawVal << "), trying to recover";
164 pdf.logEvalError(ss.str().c_str());
166 }
167
168 if (TMath::IsNaN(rawVal)) {
169 pdf.logEvalError("p.d.f value is Not-a-Number");
170 return rawVal;
171 }
172
173 return (rawVal == 0. && normVal == 0.) ? 0. : rawVal / normVal;
174}
175
176} // namespace RooFit::Detail
177
178double toDouble(const char *s);
179double toDouble(const std::string &s);
180
181#endif
#define e(i)
Definition RSha256.hxx:103
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
Abstract base class for objects that represent a real value that may appear on the left hand side of ...
Abstract base class for objects that represent a real value and implements functionality common to al...
Definition RooAbsReal.h:63
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)
bool isFunctionFlatInBins(const RooAbsReal &function, RooAbsRealLValue &obs, std::span< const double > boundaries, double relTol=1e-9)
Check that function is constant (flat) inside each bin defined by the sorted boundaries when scanning...
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:905
static const char * what
Definition stlLoader.cc:5
static double packFloatIntoNaN(float payload)
Pack float into mantissa of a NaN.
static void output()