Logo ROOT  
Reference Guide
RooHelpers.cxx
Go to the documentation of this file.
1// Author: Stephan Hageboeck, CERN 01/2019
2
3/*****************************************************************************
4 * RooFit
5 * Authors: *
6 * WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu *
7 * DK, David Kirkby, UC Irvine, dkirkby@uci.edu *
8 * *
9 * Copyright (c) 2000-2019, Regents of the University of California *
10 * and Stanford University. All rights reserved. *
11 * *
12 * Redistribution and use in source and binary forms, *
13 * with or without modification, are permitted according to the terms *
14 * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
15 *****************************************************************************/
16
17#include "RooHelpers.h"
18#include "RooAbsRealLValue.h"
19
20#include "TClass.h"
21
22namespace RooHelpers {
23
25 unsigned int extraTopics, unsigned int removeTopics, bool overrideExternalLevel) {
26 auto& msg = RooMsgService::instance();
27 fOldKillBelow = msg.globalKillBelow();
28 if (overrideExternalLevel) msg.setGlobalKillBelow(lvl);
29
30 for (int i = 0; i < msg.numStreams(); ++i) {
31 fOldConf.push_back(msg.getStream(i));
32 if (overrideExternalLevel) msg.getStream(i).minLevel = lvl;
33 msg.getStream(i).removeTopic(static_cast<RooFit::MsgTopic>(removeTopics));
34 msg.setStreamStatus(i, true);
35 }
36
37 if (extraTopics != 0) {
38 fExtraStream = msg.addStream(lvl);
39 msg.getStream(fExtraStream).addTopic(static_cast<RooFit::MsgTopic>(extraTopics));
40 }
41}
42
44 auto& msg = RooMsgService::instance();
45 msg.setGlobalKillBelow(fOldKillBelow);
46 for (int i=0; i < msg.numStreams(); ++i) {
47 if (i < static_cast<int>(fOldConf.size()))
48 msg.getStream(i) = fOldConf[i];
49 }
50
51 if (fExtraStream > 0)
52 msg.deleteStream(fExtraStream);
53}
54
55
56/// Tokenise the string by splitting at the characters in delims.
57/// Consecutive delimiters are collapsed, so that no delimiters will appear in the
58/// tokenised strings, and no emtpy strings are returned.
59/// \param[in] str String to tokenise.
60/// \param[in] delims One or more delimiters used to split the string.
61/// \param[in] returnEmptyToken If the string is empty, return one empty token. Default is to return an empty vector.
62std::vector<std::string> tokenise(const std::string &str, const std::string &delims, bool returnEmptyToken /*= true*/) {
63 if (str.empty())
64 return std::vector<std::string>(returnEmptyToken ? 1 : 0);
65
66 std::vector<std::string> tokens;
67
68 auto beg = str.find_first_not_of(delims, 0);
69 auto end = str.find_first_of(delims, beg);
70 do {
71 tokens.emplace_back(str.substr(beg, end-beg));
72 beg = str.find_first_not_of(delims, end);
73 end = str.find_first_of(delims, beg);
74 } while (beg != std::string::npos);
75
76 return tokens;
77}
78
79
80/// Hijack all messages with given level and topics while this object is alive.
81/// \param[in] level Minimum level to hijack. Higher levels also get captured.
82/// \param[in] topics Topics to hijack. Use `|` to combine different topics, and cast to `RooFit::MsgTopic` if necessary.
83/// \param[in] objectName Only hijack messages from an object with the given name. Defaults to any object.
85 std::ostringstream()
86{
87 auto& msg = RooMsgService::instance();
88 _oldKillBelow = msg.globalKillBelow();
89 msg.setGlobalKillBelow(level);
90 for (int i = 0; i < msg.numStreams(); ++i) {
91 _oldConf.push_back(msg.getStream(i));
92 msg.getStream(i).removeTopic(topics);
93 msg.setStreamStatus(i, true);
94 }
95
96 _thisStream = msg.addStream(level,
97 RooFit::Topic(topics),
99 objectName ? RooFit::ObjectName(objectName) : RooCmdArg());
100}
101
103 auto& msg = RooMsgService::instance();
104 msg.setGlobalKillBelow(_oldKillBelow);
105 for (unsigned int i = 0; i < _oldConf.size(); ++i) {
106 msg.getStream(i) = _oldConf[i];
107 }
108 msg.deleteStream(_thisStream);
109}
110
111
112/// \param[in] callingClass Class that's calling. Needed to include name and type name of the class in error message.
113/// \param[in] pars List of all parameters to be checked.
114/// \param[in] min Minimum of allowed range. `min` itself counts as disallowed.
115/// \param[in] max Maximum of allowed range. `max` itself counts as disallowed.
116/// \param[in] limitsInAllowedRange If true, the limits passed as parameters are part of the allowed range.
117/// \param[in] extraMessage Message that should be appended to the warning.
118void checkRangeOfParameters(const RooAbsReal* callingClass, std::initializer_list<const RooAbsReal*> pars,
119 double min, double max, bool limitsInAllowedRange, std::string extraMessage) {
120 const char openBr = limitsInAllowedRange ? '[' : '(';
121 const char closeBr = limitsInAllowedRange ? ']' : ')';
122
123 for (auto parameter : pars) {
124 auto par = dynamic_cast<const RooAbsRealLValue*>(parameter);
125 if (par && (
126 (par->getMin() < min || par->getMax() > max)
127 || (!limitsInAllowedRange && (par->getMin() == min || par->getMax() == max)) )) {
128 std::stringstream rangeMsg;
129 rangeMsg << openBr;
130 if (min > -std::numeric_limits<double>::max())
131 rangeMsg << min << ", ";
132 else
133 rangeMsg << "-inf, ";
134
135 if (max < std::numeric_limits<double>::max())
136 rangeMsg << max << closeBr;
137 else
138 rangeMsg << "inf" << closeBr;
139
140 oocoutW(callingClass, InputArguments) << "The parameter '" << par->GetName() << "' with range [" << par->getMin("") << ", "
141 << par->getMax() << "] of the " << callingClass->IsA()->GetName() << " '" << callingClass->GetName()
142 << "' exceeds the safe range of " << rangeMsg.str() << ". Advise to limit its range."
143 << (!extraMessage.empty() ? "\n" : "") << extraMessage << std::endl;
144 }
145 }
146}
147
148}
#define oocoutW(o, a)
Definition: RooMsgService.h:47
RooAbsRealLValue is the common abstract base class for objects that represent a real value that may a...
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition: RooAbsReal.h:60
RooCmdArg is a named container for two doubles, two integers two object points and three string point...
Definition: RooCmdArg.h:28
RooFit::MsgLevel _oldKillBelow
Definition: RooHelpers.h:65
HijackMessageStream(RooFit::MsgLevel level, RooFit::MsgTopic topics, const char *objectName=nullptr)
Hijack all messages with given level and topics while this object is alive.
Definition: RooHelpers.cxx:84
std::vector< RooMsgService::StreamConfig > _oldConf
Definition: RooHelpers.h:66
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.
Definition: RooHelpers.cxx:24
std::vector< RooMsgService::StreamConfig > fOldConf
Definition: RooHelpers.h:51
static RooMsgService & instance()
Return reference to singleton instance.
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
RooCmdArg OutputStream(std::ostream &os)
RooCmdArg Topic(Int_t topic)
RooCmdArg ObjectName(const char *name)
MsgLevel
Verbosity level for RooMsgService::StreamConfig in RooMsgService.
Definition: RooGlobalFunc.h:65
MsgTopic
Topics for a RooMsgService::StreamConfig in RooMsgService.
Definition: RooGlobalFunc.h:67
@ InputArguments
Definition: RooGlobalFunc.h:68
std::vector< std::string > tokenise(const std::string &str, const std::string &delims, bool returnEmptyToken=true)
Tokenise the string by splitting at the characters in delims.
Definition: RooHelpers.cxx:62
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 extraMessage="")
Check if the parameters have a range, and warn if the range extends below / above the set limits.
Definition: RooHelpers.cxx:118