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
29class RooAbsPdf;
30class RooAbsData;
31
32
33namespace 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.
64template<typename T>
65struct 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/// Check if the parameters have a range, and warn if the range extends below / above the set limits.
95void checkRangeOfParameters(const RooAbsReal* callingClass, std::initializer_list<const RooAbsReal*> pars,
96 double min = -std::numeric_limits<double>::max(), double max = std::numeric_limits<double>::max(),
97 bool limitsInAllowedRange = false, std::string const& extraMessage = "");
98
99
100/// Disable all caches for sub-branches in an expression tree.
101/// This is helpful when an expression with cached sub-branches needs to be integrated numerically.
103 /// Inhibit all dirty-state propagation, and assume every node as dirty.
104 /// \param[in] oldState Restore this state when going out of scope.
105 DisableCachingRAII(bool oldState):
106 _oldState(oldState) {
108 }
109
112 }
114};
115
116
117std::pair<double, double> getRangeOrBinningInterval(RooAbsArg const* arg, const char* rangeName);
118
119bool checkIfRangesOverlap(RooAbsPdf const& pdf, RooAbsData const& data, std::vector<std::string> const& rangeNames);
120
121std::string getColonSeparatedNameString(RooArgSet const& argSet);
122RooArgSet selectFromArgSet(RooArgSet const&, std::string const& names);
123
124}
125
126
127
128#endif /* ROOFIT_ROOFITCORE_INC_ROOHELPERS_H_ */
int Int_t
Definition: RtypesCore.h:45
RooAbsArg is the common abstract base class for objects that represent a value and a "shape" in RooFi...
Definition: RooAbsArg.h:72
static void setDirtyInhibit(Bool_t flag)
Control global dirty inhibit mode.
Definition: RooAbsArg.cxx:264
RooAbsData is the common abstract base class for binned and unbinned datasets.
Definition: RooAbsData.h:79
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition: RooAbsReal.h:61
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:35
Hijacks all messages with given level and topic (and optionally object name) while alive.
Definition: RooHelpers.h:74
std::ostringstream & stream()
Definition: RooHelpers.h:83
RooFit::MsgLevel _oldKillBelow
Definition: RooHelpers.h:88
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:66
std::ostringstream _str
Definition: RooHelpers.h:87
std::vector< RooMsgService::StreamConfig > _oldConf
Definition: RooHelpers.h:89
~HijackMessageStream()
Deregister the hijacked stream and restore the stream state of all previous streams.
Definition: RooHelpers.cxx:99
const HijackMessageStream & operator<<(const T &v) const
Definition: RooHelpers.h:78
Switches the message service to a different level while the instance is alive.
Definition: RooHelpers.h:42
RooFit::MsgLevel fOldKillBelow
Definition: RooHelpers.h:57
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:30
std::vector< RooMsgService::StreamConfig > fOldConf
Definition: RooHelpers.h:58
Mother of all ROOT objects.
Definition: TObject.h:37
double T(double x)
Definition: ChebyshevPol.h:34
MsgLevel
Verbosity level for RooMsgService::StreamConfig in RooMsgService.
Definition: RooGlobalFunc.h:58
MsgTopic
Topics for a RooMsgService::StreamConfig in RooMsgService.
Definition: RooGlobalFunc.h:60
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:183
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:118
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:269
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:167
std::string getColonSeparatedNameString(RooArgSet const &argSet)
Create a string with all sorted names of RooArgSet elements separated by colons.
Definition: RooHelpers.cxx:248
Disable all caches for sub-branches in an expression tree.
Definition: RooHelpers.h:102
DisableCachingRAII(bool oldState)
Inhibit all dirty-state propagation, and assume every node as dirty.
Definition: RooHelpers.h:105
Wrap an object into a TObject. Sometimes needed to avoid reinterpret_cast or enable RTTI.
Definition: RooHelpers.h:65