Logo ROOT  
Reference Guide
RooHelpers.h
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#ifndef ROOFIT_ROOFITCORE_INC_ROOHELPERS_H_
18#define ROOFIT_ROOFITCORE_INC_ROOHELPERS_H_
19
20#include "RooMsgService.h"
21#include "RooAbsArg.h"
22#include "RooAbsReal.h"
23
24#include <sstream>
25
26namespace RooHelpers {
27
28/// Switches the message service to a different level while the instance is alive.
29/// Can also temporarily activate / deactivate message topics.
30/// Use as
31/// ~~~{.cpp}
32/// RooHelpers::LocalChangeMessageLevel changeMsgLvl(RooFit::WARNING);
33/// [ statements that normally generate a lot of output ]
34/// ~~~
36 public:
37 /// Change message level (and topics) while this object is alive, reset when it goes out of scope.
38 /// \param[in] lvl The desired message level. Defaults to verbose.
39 /// \param[in] extraTopics Extra topics to be switched on. These will only switched on in the last stream to prevent all streams are printing.
40 /// \param[in] removeTopics Message topics to be switched off
41 /// \param[in] overrideExternalLevel Override the user message level.
43 unsigned int extraTopics = 0u,
44 unsigned int removeTopics = 0u,
45 bool overrideExternalLevel = true);
46
48
49 private:
51 std::vector<RooMsgService::StreamConfig> fOldConf;
52 int fExtraStream{-1};
53};
54
55
56/// Hijacks all messages with given level and topic (and optionally object name) while alive.
57/// Use this like an ostringstream afterwards. Useful for unit tests and debugging.
58class HijackMessageStream : public std::ostringstream {
59 public:
60 HijackMessageStream(RooFit::MsgLevel level, RooFit::MsgTopic topics, const char* objectName = nullptr);
61
62 virtual ~HijackMessageStream();
63
64 private:
66 std::vector<RooMsgService::StreamConfig> _oldConf;
68};
69
70
71std::vector<std::string> tokenise(const std::string &str, const std::string &delims, bool returnEmptyToken = true);
72
73
74
75class CachingError : public std::exception {
76 public:
77 CachingError(const std::string& newMessage) :
78 std::exception(),
79 _messages()
80 {
81 _messages.push_back(newMessage);
82 }
83
84 CachingError(CachingError&& previous, const std::string& newMessage) :
85 std::exception(),
86 _messages{std::move(previous._messages)}
87 {
88 _messages.push_back(newMessage);
89 }
90
91 const char* what() const noexcept override {
92 std::stringstream out;
93 out << "**Caching Error** in\n";
94
95 std::string indent;
96 for (auto it = _messages.rbegin(); it != _messages.rend(); ++it) {
97 std::string message = *it;
98 auto pos = message.find('\n', 0);
99 while (pos != std::string::npos) {
100 message.insert(pos+1, indent);
101 pos = (message.find('\n', pos+1));
102 }
103
104 out << indent << message << "\n";
105 indent += " ";
106 }
107
108 out << std::endl;
109
110 std::string* ret = new std::string(out.str()); //Make it survive this method
111
112 return ret->c_str();
113 }
114
115
116 private:
117 std::vector<std::string> _messages;
118};
119
120
122 public:
123 template <class T,
124 typename std::enable_if<std::is_base_of<RooAbsArg, T>::value>::type* = nullptr >
126 _stream << arg.ClassName() << "::" << arg.GetName() << " " << &arg << " ";
127 arg.printArgs(_stream);
128 return *this;
129 }
130
131 template <class T,
132 typename std::enable_if< ! std::is_base_of<RooAbsArg, T>::value>::type* = nullptr >
134 _stream << arg;
135 return *this;
136 }
137
138 operator std::string() const {
139 return _stream.str();
140 }
141
142 std::ostream& stream() {
143 return _stream;
144 }
145
146 private:
147 std::ostringstream _stream;
148};
149
150
151/// Check if the parameters have a range, and warn if the range extends below / above the set limits.
152void checkRangeOfParameters(const RooAbsReal* callingClass, std::initializer_list<const RooAbsReal*> pars,
153 double min = -std::numeric_limits<double>::max(), double max = std::numeric_limits<double>::max(),
154 bool limitsInAllowedRange = false, std::string extraMessage = "");
155
156
157/// Helper class to access a batch-related part of RooAbsReal's interface, which should not leak to the outside world.
159 public:
160 static void clearBatchMemory(RooAbsReal& theReal) {
161 theReal.clearBatchMemory();
162 }
163
164 static void checkBatchComputation(const RooAbsReal& theReal, std::size_t evtNo,
165 const RooArgSet* normSet = nullptr, double relAccuracy = 1.E-13) {
166 theReal.checkBatchComputation(evtNo, normSet, relAccuracy);
167 }
168};
169
170
171}
172
173
174#endif /* ROOFIT_ROOFITCORE_INC_ROOHELPERS_H_ */
static void indent(ostringstream &buf, int indent_level)
int type
Definition: TGX11.cxx:120
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition: RooAbsReal.h:60
void clearBatchMemory()
Definition: RooAbsReal.h:412
void checkBatchComputation(std::size_t evtNo, const RooArgSet *normSet=nullptr, double relAccuracy=1.E-13) const
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:28
Helper class to access a batch-related part of RooAbsReal's interface, which should not leak to the o...
Definition: RooHelpers.h:158
static void clearBatchMemory(RooAbsReal &theReal)
Definition: RooHelpers.h:160
static void checkBatchComputation(const RooAbsReal &theReal, std::size_t evtNo, const RooArgSet *normSet=nullptr, double relAccuracy=1.E-13)
Definition: RooHelpers.h:164
CachingError(const std::string &newMessage)
Definition: RooHelpers.h:77
CachingError(CachingError &&previous, const std::string &newMessage)
Definition: RooHelpers.h:84
std::vector< std::string > _messages
Definition: RooHelpers.h:117
const char * what() const noexcept override
Definition: RooHelpers.h:91
FormatPdfTree & operator<<(const T &arg)
Definition: RooHelpers.h:125
std::ostream & stream()
Definition: RooHelpers.h:142
std::ostringstream _stream
Definition: RooHelpers.h:147
Hijacks all messages with given level and topic (and optionally object name) while alive.
Definition: RooHelpers.h:58
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
Switches the message service to a different level while the instance is alive.
Definition: RooHelpers.h:35
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
double T(double x)
Definition: ChebyshevPol.h:34
MsgLevel
Verbosity level for RooMsgService::StreamConfig in RooMsgService.
Definition: RooGlobalFunc.h:65
MsgTopic
Topics for a RooMsgService::StreamConfig in RooMsgService.
Definition: RooGlobalFunc.h:67
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