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 #include <vector>
26 #include <string>
27 
28 namespace RooHelpers {
29 
30 /// Switches the message service to a different level while the instance is alive.
31 /// Can also temporarily activate / deactivate message topics.
32 /// Use as
33 /// ~~~{.cpp}
34 /// RooHelpers::LocalChangeMessageLevel changeMsgLvl(RooFit::WARNING);
35 /// [ statements that normally generate a lot of output ]
36 /// ~~~
38  public:
39  /// Change message level (and topics) while this object is alive, reset when it goes out of scope.
40  /// \param[in] lvl The desired message level. Defaults to verbose.
41  /// \param[in] extraTopics Extra topics to be switched on. These will only switched on in the last stream to prevent all streams are printing.
42  /// \param[in] removeTopics Message topics to be switched off
43  /// \param[in] overrideExternalLevel Override the user message level.
45  unsigned int extraTopics = 0u,
46  unsigned int removeTopics = 0u,
47  bool overrideExternalLevel = true);
48 
50 
51  private:
53  std::vector<RooMsgService::StreamConfig> fOldConf;
54  int fExtraStream{-1};
55 };
56 
57 
58 /// Wrap an object into a TObject. Sometimes needed to avoid reinterpret_cast or enable RTTI.
59 template<typename T>
60 struct WrapIntoTObject : public TObject {
61  WrapIntoTObject(T& obj) : _payload(&obj) { }
63 };
64 
65 
66 /// Hijacks all messages with given level and topic (and optionally object name) while alive.
67 /// Use this like an ostringstream afterwards. The messages can e.g. be retrieved using `str()`.
68 /// Useful for unit tests / debugging.
70  public:
71  HijackMessageStream(RooFit::MsgLevel level, RooFit::MsgTopic topics, const char* objectName = nullptr);
72  template<typename T>
73  const HijackMessageStream& operator<<(const T& v) const {
74  _str << v;
75  return *this;
76  }
77  std::string str() { return _str.str(); }
78  std::ostringstream& stream() { return _str; };
80 
81  private:
82  std::ostringstream _str;
84  std::vector<RooMsgService::StreamConfig> _oldConf;
86 };
87 
88 
89 std::vector<std::string> tokenise(const std::string &str, const std::string &delims, bool returnEmptyToken = true);
90 
91 
92 /// Check if the parameters have a range, and warn if the range extends below / above the set limits.
93 void checkRangeOfParameters(const RooAbsReal* callingClass, std::initializer_list<const RooAbsReal*> pars,
94  double min = -std::numeric_limits<double>::max(), double max = std::numeric_limits<double>::max(),
95  bool limitsInAllowedRange = false, std::string extraMessage = "");
96 
97 
98 /// Disable all caches for sub-branches in an expression tree.
99 /// This is helpful when an expression with cached sub-branches needs to be integrated numerically.
101  /// Inhibit all dirty-state propagation, and assume every node as dirty.
102  /// \param[in] oldState Restore this state when going out of scope.
103  DisableCachingRAII(bool oldState):
104  _oldState(oldState) {
106  }
107 
110  }
111  bool _oldState;
112 };
113 
114 
115 
116 }
117 
118 
119 
120 #endif /* ROOFIT_ROOFITCORE_INC_ROOHELPERS_H_ */
RooAbsReal.h
RooMsgService.h
RooHelpers::HijackMessageStream::operator<<
const HijackMessageStream & operator<<(const T &v) const
Definition: RooHelpers.h:73
RooHelpers::HijackMessageStream::~HijackMessageStream
~HijackMessageStream()
Definition: RooHelpers.cxx:101
RooFit::DEBUG
@ DEBUG
Definition: RooGlobalFunc.h:65
RooHelpers::HijackMessageStream::str
std::string str()
Definition: RooHelpers.h:77
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
RooHelpers::HijackMessageStream::_str
std::ostringstream _str
Definition: RooHelpers.h:82
RooAbsArg.h
RooHelpers::HijackMessageStream::_oldKillBelow
RooFit::MsgLevel _oldKillBelow
Definition: RooHelpers.h:83
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
v
@ v
Definition: rootcling_impl.cxx:3635
RooHelpers::HijackMessageStream::_thisStream
Int_t _thisStream
Definition: RooHelpers.h:85
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::DisableCachingRAII::~DisableCachingRAII
~DisableCachingRAII()
Definition: RooHelpers.h:108
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:52
RooHelpers::DisableCachingRAII::DisableCachingRAII
DisableCachingRAII(bool oldState)
Inhibit all dirty-state propagation, and assume every node as dirty.
Definition: RooHelpers.h:103
RooHelpers::DisableCachingRAII::_oldState
bool _oldState
Definition: RooHelpers.h:111
RooHelpers::HijackMessageStream::_oldConf
std::vector< RooMsgService::StreamConfig > _oldConf
Definition: RooHelpers.h:84
RooHelpers::LocalChangeMsgLevel::fExtraStream
int fExtraStream
Definition: RooHelpers.h:54
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::HijackMessageStream
Hijacks all messages with given level and topic (and optionally object name) while alive.
Definition: RooHelpers.h:69
RooHelpers::LocalChangeMsgLevel::fOldConf
std::vector< RooMsgService::StreamConfig > fOldConf
Definition: RooHelpers.h:53
RooHelpers::LocalChangeMsgLevel::~LocalChangeMsgLevel
~LocalChangeMsgLevel()
Definition: RooHelpers.cxx:43
RooHelpers::WrapIntoTObject::_payload
T * _payload
Definition: RooHelpers.h:62
TObject
Mother of all ROOT objects.
Definition: TObject.h:37
ROOT::Math::Chebyshev::T
double T(double x)
Definition: ChebyshevPol.h:34
RooAbsArg::setDirtyInhibit
static void setDirtyInhibit(Bool_t flag)
Control global dirty inhibit mode.
Definition: RooAbsArg.cxx:264
RooHelpers::WrapIntoTObject::WrapIntoTObject
WrapIntoTObject(T &obj)
Definition: RooHelpers.h:61
RooHelpers::WrapIntoTObject
Wrap an object into a TObject. Sometimes needed to avoid reinterpret_cast or enable RTTI.
Definition: RooHelpers.h:60
RooHelpers
Definition: RooHelpers.h:28
RooHelpers::LocalChangeMsgLevel
Switches the message service to a different level while the instance is alive.
Definition: RooHelpers.h:37
RooHelpers::HijackMessageStream::stream
std::ostringstream & stream()
Definition: RooHelpers.h:78
RooHelpers::DisableCachingRAII
Disable all caches for sub-branches in an expression tree.
Definition: RooHelpers.h:100
int