Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooHelpers.cxx
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#include <RooHelpers.h>
14
15#include <RooAbsCategory.h>
16#include <RooAbsData.h>
17#include <RooAbsPdf.h>
18#include <RooAbsRealLValue.h>
19#include <RooArgList.h>
20#include <RooCategory.h>
21#include <RooDataHist.h>
22#include <RooDataSet.h>
23#include <RooMultiPdf.h>
24#include <RooProdPdf.h>
25#include <RooRealSumPdf.h>
26#include <RooRealVar.h>
27#include <RooSimultaneous.h>
28
29#include <ROOT/StringUtils.hxx>
30#include <TClass.h>
31
32#include <unordered_map>
33
34namespace RooHelpers {
35
38{
40 fOldKillBelow = msg.globalKillBelow();
42 msg.setGlobalKillBelow(lvl);
43
44 for (int i = 0; i < msg.numStreams(); ++i) {
45 fOldConf.push_back(msg.getStream(i));
47 msg.getStream(i).minLevel = lvl;
48 msg.getStream(i).removeTopic(static_cast<RooFit::MsgTopic>(removeTopics));
49 msg.setStreamStatus(i, true);
50 }
51
52 if (extraTopics != 0) {
53 fExtraStream = msg.addStream(lvl);
54 msg.getStream(fExtraStream).addTopic(static_cast<RooFit::MsgTopic>(extraTopics));
55 }
56}
57
59{
61 msg.setGlobalKillBelow(fOldKillBelow);
62 for (int i = 0; i < msg.numStreams(); ++i) {
63 if (i < static_cast<int>(fOldConf.size()))
64 msg.getStream(i) = fOldConf[i];
65 }
66
67 if (fExtraStream > 0)
68 msg.deleteStream(fExtraStream);
69}
70
71/// Hijack all messages with given level and topics while this object is alive.
72/// \param[in] level Minimum level to hijack. Higher levels also get captured.
73/// \param[in] topics Topics to hijack. Use `|` to combine different topics, and cast to `RooFit::MsgTopic` if
74/// necessary. \param[in] objectName Only hijack messages from an object with the given name. Defaults to any object.
76{
78 _oldKillBelow = msg.globalKillBelow();
79 if (_oldKillBelow > level)
80 msg.setGlobalKillBelow(level);
81
82 std::vector<RooMsgService::StreamConfig> tmpStreams;
83 for (int i = 0; i < msg.numStreams(); ++i) {
84 _oldConf.push_back(msg.getStream(i));
85 if (msg.getStream(i).match(level, topics, static_cast<RooAbsArg *>(nullptr))) {
86 tmpStreams.push_back(msg.getStream(i));
87 msg.setStreamStatus(i, false);
88 }
89 }
90
92 objectName ? RooFit::ObjectName(objectName) : RooCmdArg());
93
95 msg.addStream(st.minLevel, RooFit::Topic(st.topic), RooFit::OutputStream(*st.os),
96 RooFit::ObjectName(st.objectName.c_str()), RooFit::ClassName(st.className.c_str()),
97 RooFit::BaseClassName(st.baseClassName.c_str()), RooFit::TagName(st.tagName.c_str()));
98 }
99}
100
101/// Deregister the hijacked stream and restore the stream state of all previous streams.
103{
105 msg.setGlobalKillBelow(_oldKillBelow);
106 for (unsigned int i = 0; i < _oldConf.size(); ++i) {
107 msg.getStream(i) = _oldConf[i];
108 }
109
110 while (_thisStream < msg.numStreams()) {
111 msg.deleteStream(_thisStream);
112 }
113}
114
115/// \param[in] callingClass Class that's calling. Needed to include name and type name of the class in error message.
116/// \param[in] pars List of all parameters to be checked.
117/// \param[in] min Minimum of allowed range. `min` itself counts as disallowed.
118/// \param[in] max Maximum of allowed range. `max` itself counts as disallowed.
119/// \param[in] limitsInAllowedRange If true, the limits passed as parameters are part of the allowed range.
120/// \param[in] extraMessage Message that should be appended to the warning.
121void checkRangeOfParameters(const RooAbsReal *callingClass, std::initializer_list<const RooAbsReal *> pars, double min,
122 double max, bool limitsInAllowedRange, std::string const &extraMessage)
123{
124 const char openBr = limitsInAllowedRange ? '[' : '(';
125 const char closeBr = limitsInAllowedRange ? ']' : ')';
126
127 for (auto parameter : pars) {
128 auto par = dynamic_cast<const RooAbsRealLValue *>(parameter);
129 if (par && ((par->getMin() < min || par->getMax() > max) ||
130 (!limitsInAllowedRange && (par->getMin() == min || par->getMax() == max)))) {
131 std::stringstream rangeMsg;
132 rangeMsg << openBr;
133 if (min > -std::numeric_limits<double>::max()) {
134 rangeMsg << min << ", ";
135 } else {
136 rangeMsg << "-inf, ";
137 }
138
139 if (max < std::numeric_limits<double>::max()) {
140 rangeMsg << max << closeBr;
141 } else {
142 rangeMsg << "inf" << closeBr;
143 }
144
145 oocoutW(callingClass, InputArguments)
146 << "The parameter '" << par->GetName() << "' with range [" << par->getMin("") << ", " << par->getMax()
147 << "] of the " << callingClass->ClassName() << " '" << callingClass->GetName()
148 << "' exceeds the safe range of " << rangeMsg.str() << ". Advise to limit its range."
149 << (!extraMessage.empty() ? "\n" : "") << extraMessage << std::endl;
150 }
151 }
152}
153
155{
156 bool changed = false;
157 for (RooAbsArg *a : coll) {
158 RooRealVar *v = dynamic_cast<RooRealVar *>(a);
159 RooCategory *cv = dynamic_cast<RooCategory *>(a);
160 if (v && (v->isConstant() != constant)) {
161 changed = true;
162 v->setConstant(constant);
163 } else if (cv && (cv->isConstant() != constant)) {
164 changed = true;
165 cv->setConstant(constant);
166 }
167 }
168 return changed;
169}
170
171} // namespace RooHelpers
#define a(i)
Definition RSha256.hxx:99
#define oocoutW(o, a)
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Common abstract base class for objects that represent a value and a "shape" in RooFit.
Definition RooAbsArg.h:76
Abstract container object that can hold multiple RooAbsArg objects.
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
Object to represent discrete states.
Definition RooCategory.h:28
Named container for two doubles, two integers two object points and three string pointers that can be...
Definition RooCmdArg.h:26
RooFit::MsgLevel _oldKillBelow
Definition RooHelpers.h:80
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:81
~HijackMessageStream()
Deregister the hijacked stream and restore the stream state of all previous streams.
RooFit::MsgLevel fOldKillBelow
Definition RooHelpers.h:50
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:51
static RooMsgService & instance()
Return reference to singleton instance.
Variable that can be changed from the outside.
Definition RooRealVar.h:37
RooCmdArg ClassName(const char *name)
RooCmdArg OutputStream(std::ostream &os)
RooCmdArg Topic(Int_t topic)
RooCmdArg TagName(const char *name)
RooCmdArg BaseClassName(const char *name)
RooCmdArg ObjectName(const char *name)
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 setAllConstant(const RooAbsCollection &coll, bool constant=true)
set all RooRealVars to constants. return true if at least one changed status