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