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 
22 namespace 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.
62 std::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 {
86  auto& msg = RooMsgService::instance();
87  _oldKillBelow = msg.globalKillBelow();
88  msg.setGlobalKillBelow(level);
89  for (int i = 0; i < msg.numStreams(); ++i) {
90  _oldConf.push_back(msg.getStream(i));
91  msg.getStream(i).removeTopic(topics);
92  msg.setStreamStatus(i, true);
93  }
94 
95  _thisStream = msg.addStream(level,
96  RooFit::Topic(topics),
98  objectName ? RooFit::ObjectName(objectName) : RooCmdArg());
99 }
100 
102  auto& msg = RooMsgService::instance();
103  msg.setGlobalKillBelow(_oldKillBelow);
104  for (unsigned int i = 0; i < _oldConf.size(); ++i) {
105  msg.getStream(i) = _oldConf[i];
106  }
107  msg.deleteStream(_thisStream);
108 }
109 
110 
111 /// \param[in] callingClass Class that's calling. Needed to include name and type name of the class in error message.
112 /// \param[in] pars List of all parameters to be checked.
113 /// \param[in] min Minimum of allowed range. `min` itself counts as disallowed.
114 /// \param[in] max Maximum of allowed range. `max` itself counts as disallowed.
115 /// \param[in] limitsInAllowedRange If true, the limits passed as parameters are part of the allowed range.
116 /// \param[in] extraMessage Message that should be appended to the warning.
117 void checkRangeOfParameters(const RooAbsReal* callingClass, std::initializer_list<const RooAbsReal*> pars,
118  double min, double max, bool limitsInAllowedRange, std::string extraMessage) {
119  const char openBr = limitsInAllowedRange ? '[' : '(';
120  const char closeBr = limitsInAllowedRange ? ']' : ')';
121 
122  for (auto parameter : pars) {
123  auto par = dynamic_cast<const RooAbsRealLValue*>(parameter);
124  if (par && (
125  (par->getMin() < min || par->getMax() > max)
126  || (!limitsInAllowedRange && (par->getMin() == min || par->getMax() == max)) )) {
127  std::stringstream rangeMsg;
128  rangeMsg << openBr;
129  if (min > -std::numeric_limits<double>::max())
130  rangeMsg << min << ", ";
131  else
132  rangeMsg << "-inf, ";
133 
134  if (max < std::numeric_limits<double>::max())
135  rangeMsg << max << closeBr;
136  else
137  rangeMsg << "inf" << closeBr;
138 
139  oocoutW(callingClass, InputArguments) << "The parameter '" << par->GetName() << "' with range [" << par->getMin("") << ", "
140  << par->getMax() << "] of the " << callingClass->IsA()->GetName() << " '" << callingClass->GetName()
141  << "' exceeds the safe range of " << rangeMsg.str() << ". Advise to limit its range."
142  << (!extraMessage.empty() ? "\n" : "") << extraMessage << std::endl;
143  }
144  }
145 }
146 
147 
148 /// Get the lower and upper bound of parameter range if arg can be casted to RooAbsRealLValue.
149 /// If no range with rangeName is defined for the argument, this will check if a binning of the
150 /// same name exists and return the interval covered by the binning.
151 /// Returns `{-infinity, infinity}` if agument can't be casted to RooAbsRealLValue* or if no
152 /// range or binning with the requested name exists.
153 /// \param[in] arg RooAbsArg for which to get the range.
154 /// \param[in] rangeName The name of the range.
155 std::pair<double, double> getRangeOrBinningInterval(RooAbsArg const* arg, const char* rangeName) {
156  auto rlv = dynamic_cast<RooAbsRealLValue const*>(arg);
157  if (rlv) {
158  const RooAbsBinning* binning = rlv->getBinningPtr(rangeName);
159  if (rangeName && rlv->hasRange(rangeName)) {
160  return {rlv->getMin(rangeName), rlv->getMax(rangeName)};
161  } else if (binning) {
162  if (!binning->isParameterized()) {
163  return {binning->lowBound(), binning->highBound()};
164  } else {
165  return {binning->lowBoundFunc()->getVal(), binning->highBoundFunc()->getVal()};
166  }
167  }
168  }
169  return {-std::numeric_limits<double>::infinity(), +std::numeric_limits<double>::infinity()};
170 }
171 
172 
173 }
RooCmdArg
RooCmdArg is a named container for two doubles, two integers two object points and three string point...
Definition: RooCmdArg.h:27
RooHelpers.h
RooAbsBinning::highBoundFunc
virtual RooAbsReal * highBoundFunc() const
Return pointer to RooAbsReal parameterized upper bound, if any.
Definition: RooAbsBinning.h:88
RooFit::InputArguments
@ InputArguments
Definition: RooGlobalFunc.h:68
RooAbsBinning::lowBoundFunc
virtual RooAbsReal * lowBoundFunc() const
Return pointer to RooAbsReal parameterized lower bound, if any.
Definition: RooAbsBinning.h:84
RooHelpers::HijackMessageStream::~HijackMessageStream
~HijackMessageStream()
Definition: RooHelpers.cxx:101
RooAbsBinning::highBound
virtual Double_t highBound() const =0
RooHelpers::checkRangeOfParameters
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:117
RooAbsReal::getVal
Double_t getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition: RooAbsReal.h:91
RooHelpers::HijackMessageStream::_str
std::ostringstream _str
Definition: RooHelpers.h:84
RooHelpers::HijackMessageStream::_oldKillBelow
RooFit::MsgLevel _oldKillBelow
Definition: RooHelpers.h:85
TClass.h
RooAbsReal
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition: RooAbsReal.h:61
RooFit::MsgTopic
MsgTopic
Topics for a RooMsgService::StreamConfig in RooMsgService.
Definition: RooGlobalFunc.h:67
RooFit::MsgLevel
MsgLevel
Verbosity level for RooMsgService::StreamConfig in RooMsgService.
Definition: RooGlobalFunc.h:65
RooHelpers::HijackMessageStream::_thisStream
Int_t _thisStream
Definition: RooHelpers.h:87
RooHelpers::LocalChangeMsgLevel::LocalChangeMsgLevel
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
RooHelpers::HijackMessageStream::HijackMessageStream
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
RooHelpers::LocalChangeMsgLevel::fOldKillBelow
RooFit::MsgLevel fOldKillBelow
Definition: RooHelpers.h:54
RooHelpers::HijackMessageStream::_oldConf
std::vector< RooMsgService::StreamConfig > _oldConf
Definition: RooHelpers.h:86
RooAbsBinning
RooAbsBinning is the abstract base class for RooRealVar binning definitions.
Definition: RooAbsBinning.h:26
RooHelpers::LocalChangeMsgLevel::fExtraStream
int fExtraStream
Definition: RooHelpers.h:56
RooHelpers::getRangeOrBinningInterval
std::pair< double, double > getRangeOrBinningInterval(RooAbsArg const *arg, const char *rangeName)
Get the lower and upper bound of parameter range if arg can be casted to RooAbsRealLValue.
Definition: RooHelpers.cxx:155
oocoutW
#define oocoutW(o, a)
Definition: RooMsgService.h:47
RooHelpers::tokenise
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
RooHelpers::LocalChangeMsgLevel::fOldConf
std::vector< RooMsgService::StreamConfig > fOldConf
Definition: RooHelpers.h:55
RooHelpers::LocalChangeMsgLevel::~LocalChangeMsgLevel
~LocalChangeMsgLevel()
Definition: RooHelpers.cxx:43
RooFit::OutputStream
RooCmdArg OutputStream(std::ostream &os)
Definition: RooGlobalFunc.cxx:309
RooAbsBinning::isParameterized
virtual Bool_t isParameterized() const
Interface function.
Definition: RooAbsBinning.h:80
RooFit::Topic
RooCmdArg Topic(Int_t topic)
Definition: RooGlobalFunc.cxx:304
RooAbsArg
RooAbsArg is the common abstract base class for objects that represent a value and a "shape" in RooFi...
Definition: RooAbsArg.h:72
TNamed::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
RooHelpers
Definition: RooHelpers.h:30
RooAbsRealLValue
RooAbsRealLValue is the common abstract base class for objects that represent a real value that may a...
Definition: RooAbsRealLValue.h:31
RooAbsBinning::lowBound
virtual Double_t lowBound() const =0
RooAbsRealLValue.h
RooMsgService::instance
static RooMsgService & instance()
Return reference to singleton instance.
Definition: RooMsgService.cxx:366
RooFit::ObjectName
RooCmdArg ObjectName(const char *name)
Definition: RooGlobalFunc.cxx:305