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.
84 HijackMessageStream::HijackMessageStream(RooFit::MsgLevel level, RooFit::MsgTopic topics, const char* objectName) :
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),
98  RooFit::OutputStream(*this),
99  objectName ? RooFit::ObjectName(objectName) : RooCmdArg());
100 }
101 
102 HijackMessageStream::~HijackMessageStream() {
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.
118 void 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 }
RooCmdArg
Definition: RooCmdArg.h:27
RooHelpers.h
RooFit::InputArguments
@ InputArguments
Definition: RooGlobalFunc.h:68
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:130
TClass.h
RooAbsReal
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::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:36
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:96
RooHelpers::LocalChangeMsgLevel::fOldKillBelow
RooFit::MsgLevel fOldKillBelow
Definition: RooHelpers.h:76
RooHelpers::LocalChangeMsgLevel::fExtraStream
int fExtraStream
Definition: RooHelpers.h:78
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:74
RooHelpers::LocalChangeMsgLevel::fOldConf
std::vector< RooMsgService::StreamConfig > fOldConf
Definition: RooHelpers.h:77
RooHelpers::LocalChangeMsgLevel::~LocalChangeMsgLevel
~LocalChangeMsgLevel()
Definition: RooHelpers.cxx:55
RooFit::OutputStream
RooCmdArg OutputStream(std::ostream &os)
Definition: RooGlobalFunc.cxx:303
RooFit::Topic
RooCmdArg Topic(Int_t topic)
Definition: RooGlobalFunc.cxx:298
TNamed::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:53
RooHelpers
Definition: RooAbsReal.h:46
RooAbsRealLValue
Definition: RooAbsRealLValue.h:31
RooAbsRealLValue.h
RooMsgService::instance
static RooMsgService & instance()
Return reference to singleton instance.
Definition: RooMsgService.cxx:363
RooFit::ObjectName
RooCmdArg ObjectName(const char *name)
Definition: RooGlobalFunc.cxx:299