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 #include <utility>
28 
29 class RooAbsPdf;
30 class RooAbsData;
31 
32 
33 namespace RooHelpers {
34 
35 /// Switches the message service to a different level while the instance is alive.
36 /// Can also temporarily activate / deactivate message topics.
37 /// Use as
38 /// ~~~{.cpp}
39 /// RooHelpers::LocalChangeMessageLevel changeMsgLvl(RooFit::WARNING);
40 /// [ statements that normally generate a lot of output ]
41 /// ~~~
43  public:
44  /// Change message level (and topics) while this object is alive, reset when it goes out of scope.
45  /// \param[in] lvl The desired message level. Defaults to verbose.
46  /// \param[in] extraTopics Extra topics to be switched on. These will only switched on in the last stream to prevent all streams are printing.
47  /// \param[in] removeTopics Message topics to be switched off
48  /// \param[in] overrideExternalLevel Override the user message level.
50  unsigned int extraTopics = 0u,
51  unsigned int removeTopics = 0u,
52  bool overrideExternalLevel = true);
53 
55 
56  private:
58  std::vector<RooMsgService::StreamConfig> fOldConf;
59  int fExtraStream{-1};
60 };
61 
62 
63 /// Wrap an object into a TObject. Sometimes needed to avoid reinterpret_cast or enable RTTI.
64 template<typename T>
65 struct WrapIntoTObject : public TObject {
66  WrapIntoTObject(T& obj) : _payload(&obj) { }
68 };
69 
70 
71 /// Hijacks all messages with given level and topic (and optionally object name) while alive.
72 /// Use this like an ostringstream afterwards. The messages can e.g. be retrieved using `str()`.
73 /// Useful for unit tests / debugging.
75  public:
76  HijackMessageStream(RooFit::MsgLevel level, RooFit::MsgTopic topics, const char* objectName = nullptr);
77  template<typename T>
78  const HijackMessageStream& operator<<(const T& v) const {
79  _str << v;
80  return *this;
81  }
82  std::string str() { return _str.str(); }
83  std::ostringstream& stream() { return _str; };
85 
86  private:
87  std::ostringstream _str;
89  std::vector<RooMsgService::StreamConfig> _oldConf;
91 };
92 
93 
94 std::vector<std::string> tokenise(const std::string &str, const std::string &delims, bool returnEmptyToken = true);
95 
96 
97 /// Check if the parameters have a range, and warn if the range extends below / above the set limits.
98 void checkRangeOfParameters(const RooAbsReal* callingClass, std::initializer_list<const RooAbsReal*> pars,
99  double min = -std::numeric_limits<double>::max(), double max = std::numeric_limits<double>::max(),
100  bool limitsInAllowedRange = false, std::string const& extraMessage = "");
101 
102 
103 /// Disable all caches for sub-branches in an expression tree.
104 /// This is helpful when an expression with cached sub-branches needs to be integrated numerically.
106  /// Inhibit all dirty-state propagation, and assume every node as dirty.
107  /// \param[in] oldState Restore this state when going out of scope.
108  DisableCachingRAII(bool oldState):
109  _oldState(oldState) {
111  }
112 
115  }
116  bool _oldState;
117 };
118 
119 
120 std::pair<double, double> getRangeOrBinningInterval(RooAbsArg const* arg, const char* rangeName);
121 
122 bool checkIfRangesOverlap(RooAbsPdf const& pdf, RooAbsData const& data, std::vector<std::string> const& rangeNames);
123 
124 std::string getColonSeparatedNameString(RooArgSet const& argSet);
125 RooArgSet selectFromArgSet(RooArgSet const&, std::string const& names);
126 
127 }
128 
129 
130 
131 #endif /* ROOFIT_ROOFITCORE_INC_ROOHELPERS_H_ */
RooAbsReal.h
RooMsgService.h
RooHelpers::HijackMessageStream::operator<<
const HijackMessageStream & operator<<(const T &v) const
Definition: RooHelpers.h:78
RooAbsData
RooAbsData is the common abstract base class for binned and unbinned datasets.
Definition: RooAbsData.h:49
RooHelpers::HijackMessageStream::~HijackMessageStream
~HijackMessageStream()
Deregister the hijacked stream and restore the stream state of all previous streams.
Definition: RooHelpers.cxx:122
RooFit::DEBUG
@ DEBUG
Definition: RooGlobalFunc.h:58
RooHelpers::HijackMessageStream::str
std::string str()
Definition: RooHelpers.h:82
RooHelpers::HijackMessageStream::_str
std::ostringstream _str
Definition: RooHelpers.h:87
RooAbsArg.h
RooHelpers::HijackMessageStream::_oldKillBelow
RooFit::MsgLevel _oldKillBelow
Definition: RooHelpers.h:88
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:60
RooFit::MsgLevel
MsgLevel
Verbosity level for RooMsgService::StreamConfig in RooMsgService.
Definition: RooGlobalFunc.h:58
v
@ v
Definition: rootcling_impl.cxx:3664
RooHelpers::HijackMessageStream::_thisStream
Int_t _thisStream
Definition: RooHelpers.h:90
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:29
RooHelpers::DisableCachingRAII::~DisableCachingRAII
~DisableCachingRAII()
Definition: RooHelpers.h:113
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:89
RooHelpers::LocalChangeMsgLevel::fOldKillBelow
RooFit::MsgLevel fOldKillBelow
Definition: RooHelpers.h:57
RooHelpers::DisableCachingRAII::DisableCachingRAII
DisableCachingRAII(bool oldState)
Inhibit all dirty-state propagation, and assume every node as dirty.
Definition: RooHelpers.h:108
RooHelpers::selectFromArgSet
RooArgSet selectFromArgSet(RooArgSet const &, std::string const &names)
Construct a RooArgSet of objects in a RooArgSet whose names match to those in the names string.
Definition: RooHelpers.cxx:292
RooHelpers::getColonSeparatedNameString
std::string getColonSeparatedNameString(RooArgSet const &argSet)
Create a string with all sorted names of RooArgSet elements separated by colons.
Definition: RooHelpers.cxx:271
RooHelpers::DisableCachingRAII::_oldState
bool _oldState
Definition: RooHelpers.h:116
RooHelpers::HijackMessageStream::_oldConf
std::vector< RooMsgService::StreamConfig > _oldConf
Definition: RooHelpers.h:89
RooHelpers::LocalChangeMsgLevel::fExtraStream
int fExtraStream
Definition: RooHelpers.h:59
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:190
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 const &extraMessage="")
Check if the parameters have a range, and warn if the range extends below / above the set limits.
Definition: RooHelpers.cxx:141
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:67
RooHelpers::HijackMessageStream
Hijacks all messages with given level and topic (and optionally object name) while alive.
Definition: RooHelpers.h:74
RooHelpers::LocalChangeMsgLevel::fOldConf
std::vector< RooMsgService::StreamConfig > fOldConf
Definition: RooHelpers.h:58
RooHelpers::LocalChangeMsgLevel::~LocalChangeMsgLevel
~LocalChangeMsgLevel()
Definition: RooHelpers.cxx:48
RooHelpers::WrapIntoTObject::_payload
T * _payload
Definition: RooHelpers.h:67
TObject
Mother of all ROOT objects.
Definition: TObject.h:37
ROOT::Math::Chebyshev::T
double T(double x)
Definition: ChebyshevPol.h:34
RooAbsArg
RooAbsArg is the common abstract base class for objects that represent a value and a "shape" in RooFi...
Definition: RooAbsArg.h:72
RooAbsPdf
Definition: RooAbsPdf.h:41
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:66
RooHelpers::WrapIntoTObject
Wrap an object into a TObject. Sometimes needed to avoid reinterpret_cast or enable RTTI.
Definition: RooHelpers.h:65
RooHelpers
Definition: RooHelpers.h:33
RooHelpers::LocalChangeMsgLevel
Switches the message service to a different level while the instance is alive.
Definition: RooHelpers.h:42
RooHelpers::checkIfRangesOverlap
bool checkIfRangesOverlap(RooAbsPdf const &pdf, RooAbsData const &data, std::vector< std::string > const &rangeNames)
Check if there is any overlap when a list of ranges is applied to a set of observables.
Definition: RooHelpers.cxx:206
RooHelpers::HijackMessageStream::stream
std::ostringstream & stream()
Definition: RooHelpers.h:83
RooArgSet
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:33
RooHelpers::DisableCachingRAII
Disable all caches for sub-branches in an expression tree.
Definition: RooHelpers.h:105
int