Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooHelpers.h
Go to the documentation of this file.
1/*
2 * Project: RooFit
3 * Author:
4 * Stephan Hageboeck, CERN 2019
5 *
6 * Copyright (c) 2023, CERN
7 *
8 * Redistribution and use in source and binary forms,
9 * with or without modification, are permitted according to the terms
10 * listed in LICENSE (http://roofit.sourceforge.net/license.txt)
11 */
12
13#ifndef RooFit_RooHelpers_h
14#define RooFit_RooHelpers_h
15
16#include <RooMsgService.h>
17#include <RooAbsArg.h>
18#include <RooAbsReal.h>
19
20#include <ROOT/RSpan.hxx>
21
22#include <sstream>
23#include <list>
24#include <vector>
25#include <string>
26#include <utility>
27
28class RooAbsPdf;
29class RooAbsData;
31
32namespace RooHelpers {
33
34/// Switches the message service to a different level while the instance is alive.
35/// Can also temporarily activate / deactivate message topics.
36/// Use as
37/// ~~~{.cpp}
38/// RooHelpers::LocalChangeMsgLevel changeMsgLvl(RooFit::WARNING);
39/// [ statements that normally generate a lot of output ]
40/// ~~~
42public:
43 /// Change message level (and topics) while this object is alive, reset when it goes out of scope.
44 /// \param[in] lvl The desired message level. Defaults to verbose.
45 /// \param[in] extraTopics Extra topics to be switched on. These will only switched on in the last stream to prevent
46 /// all streams are printing. \param[in] removeTopics Message topics to be switched off \param[in]
47 /// overrideExternalLevel Override the user message level.
49 unsigned int removeTopics = 0u, bool overrideExternalLevel = true);
50
52
53private:
55 std::vector<RooMsgService::StreamConfig> fOldConf;
56 int fExtraStream{-1};
57};
58
59/// Wrap an object into a TObject. Sometimes needed to avoid reinterpret_cast or enable RTTI.
60template <typename T>
61struct WrapIntoTObject : public TObject {
62 WrapIntoTObject(T &obj) : _payload(&obj) {}
64};
65
66/// Hijacks all messages with given level and topic (and optionally object name) while alive.
67/// Use this like an ostringstream afterwards. The messages can e.g. be retrieved using `str()`.
68/// Useful for unit tests / debugging.
70public:
71 HijackMessageStream(RooFit::MsgLevel level, RooFit::MsgTopic topics, const char *objectName = nullptr);
72 template <typename T>
73 const HijackMessageStream &operator<<(const T &v) const
74 {
75 _str << v;
76 return *this;
77 }
78 std::string str() { return _str.str(); }
79 std::ostringstream &stream() { return _str; };
81
82private:
83 std::ostringstream _str;
85 std::vector<RooMsgService::StreamConfig> _oldConf;
87};
88
89/// Check if the parameters have a range, and warn if the range extends below / above the set limits.
90void checkRangeOfParameters(const RooAbsReal *callingClass, std::initializer_list<const RooAbsReal *> pars,
91 double min = -std::numeric_limits<double>::max(),
92 double max = std::numeric_limits<double>::max(), bool limitsInAllowedRange = false,
93 std::string const &extraMessage = "");
94
95/// set all RooRealVars to constants. return true if at least one changed status
96bool setAllConstant(const RooAbsCollection &coll, bool constant = true);
97
98/// Check that `function` is constant (flat) inside each bin defined by the
99/// sorted `boundaries` when scanning the observable `obs`. Several interior
100/// points are sampled per bin and compared to the bin's first sample; if any
101/// of them deviates by more than `relTol` (relative to the value scale), the
102/// function is not flat and false is returned. The value of `obs` is restored
103/// on return.
104bool isFunctionFlatInBins(const RooAbsReal &function, RooAbsRealLValue &obs, std::span<const double> boundaries,
105 double relTol = 1e-9);
106
107/// Return a newly allocated list with the subset of `boundaries` that lies
108/// strictly inside [`xlo`, `xhi`], with `xlo` and `xhi` added as the first and
109/// last entries. This is the form expected by RooFit's binBoundaries()
110/// interface, so the bin integrator covers exactly the integration range.
111/// The caller takes ownership of the returned list.
112std::list<double> *binBoundariesInRange(std::span<const double> boundaries, double xlo, double xhi);
113
114} // namespace RooHelpers
115
116#endif
#define e(i)
Definition RSha256.hxx:103
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
Hijacks all messages with given level and topic (and optionally object name) while alive.
Definition RooHelpers.h:69
std::ostringstream & stream()
Definition RooHelpers.h:79
RooFit::MsgLevel _oldKillBelow
Definition RooHelpers.h:84
HijackMessageStream(RooFit::MsgLevel level, RooFit::MsgTopic topics, const char *objectName=nullptr)
Hijack all messages with given level and topics while this object is alive.
std::vector< RooMsgService::StreamConfig > _oldConf
Definition RooHelpers.h:85
~HijackMessageStream()
Deregister the hijacked stream and restore the stream state of all previous streams.
const HijackMessageStream & operator<<(const T &v) const
Definition RooHelpers.h:73
Switches the message service to a different level while the instance is alive.
Definition RooHelpers.h:41
RooFit::MsgLevel fOldKillBelow
Definition RooHelpers.h:54
LocalChangeMsgLevel(RooFit::MsgLevel lvl=RooFit::DEBUG, unsigned int extraTopics=0u, unsigned int removeTopics=0u, bool overrideExternalLevel=true)
Change message level (and topics) while this object is alive, reset when it goes out of scope.
std::vector< RooMsgService::StreamConfig > fOldConf
Definition RooHelpers.h:55
Mother of all ROOT objects.
Definition TObject.h:42
MsgLevel
Verbosity level for RooMsgService::StreamConfig in RooMsgService.
MsgTopic
Topics for a RooMsgService::StreamConfig in RooMsgService.
void checkRangeOfParameters(const RooAbsReal *callingClass, std::initializer_list< const RooAbsReal * > pars, double min=-std::numeric_limits< double >::max(), double max=std::numeric_limits< double >::max(), bool limitsInAllowedRange=false, std::string const &extraMessage="")
Check if the parameters have a range, and warn if the range extends below / above the set limits.
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::list< double > * binBoundariesInRange(std::span< const double > boundaries, double xlo, double xhi)
Return a newly allocated list with the subset of boundaries that lies strictly inside [xlo,...
bool setAllConstant(const RooAbsCollection &coll, bool constant=true)
set all RooRealVars to constants. return true if at least one changed status
Wrap an object into a TObject. Sometimes needed to avoid reinterpret_cast or enable RTTI.
Definition RooHelpers.h:61