Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooHelpers.h
Go to the documentation of this file.
1/*
2 * Project: RooFit
3 * Author:
4 * Stephan Hageboeck, CERN 2019
5 *
6 * Copyright (c) 2023, CERN
7 *
8 * Redistribution and use in source and binary forms,
9 * with or without modification, are permitted according to the terms
10 * listed in LICENSE (http://roofit.sourceforge.net/license.txt)
11 */
12
13#ifndef RooFit_RooHelpers_h
14#define RooFit_RooHelpers_h
15
16#include <RooMsgService.h>
17#include <RooAbsArg.h>
18#include <RooAbsReal.h>
19
20#include <sstream>
21#include <vector>
22#include <string>
23#include <utility>
24
25class RooAbsPdf;
26class RooAbsData;
27
28namespace 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::LocalChangeMsgLevel changeMsgLvl(RooFit::WARNING);
35/// [ statements that normally generate a lot of output ]
36/// ~~~
38public:
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
42 /// all streams are printing. \param[in] removeTopics Message topics to be switched off \param[in]
43 /// overrideExternalLevel Override the user message level.
44 LocalChangeMsgLevel(RooFit::MsgLevel lvl = RooFit::DEBUG, unsigned int extraTopics = 0u,
45 unsigned int removeTopics = 0u, bool overrideExternalLevel = true);
46
48
49private:
51 std::vector<RooMsgService::StreamConfig> fOldConf;
52 int fExtraStream{-1};
53};
54
55/// Wrap an object into a TObject. Sometimes needed to avoid reinterpret_cast or enable RTTI.
56template <typename T>
57struct WrapIntoTObject : public TObject {
58 WrapIntoTObject(T &obj) : _payload(&obj) {}
60};
61
62/// Hijacks all messages with given level and topic (and optionally object name) while alive.
63/// Use this like an ostringstream afterwards. The messages can e.g. be retrieved using `str()`.
64/// Useful for unit tests / debugging.
66public:
67 HijackMessageStream(RooFit::MsgLevel level, RooFit::MsgTopic topics, const char *objectName = nullptr);
68 template <typename T>
69 const HijackMessageStream &operator<<(const T &v) const
70 {
71 _str << v;
72 return *this;
73 }
74 std::string str() { return _str.str(); }
75 std::ostringstream &stream() { return _str; };
77
78private:
79 std::ostringstream _str;
81 std::vector<RooMsgService::StreamConfig> _oldConf;
83};
84
85/// Check if the parameters have a range, and warn if the range extends below / above the set limits.
86void checkRangeOfParameters(const RooAbsReal *callingClass, std::initializer_list<const RooAbsReal *> pars,
87 double min = -std::numeric_limits<double>::max(),
88 double max = std::numeric_limits<double>::max(), bool limitsInAllowedRange = false,
89 std::string const &extraMessage = "");
90
91
92} // namespace RooHelpers
93
94#endif
Abstract base class for binned and unbinned datasets.
Definition RooAbsData.h:57
Abstract interface for all probability density functions.
Definition RooAbsPdf.h:40
Abstract base class for objects that represent a real value and implements functionality common to al...
Definition RooAbsReal.h:59
Hijacks all messages with given level and topic (and optionally object name) while alive.
Definition RooHelpers.h:65
std::ostringstream & stream()
Definition RooHelpers.h:75
RooFit::MsgLevel _oldKillBelow
Definition RooHelpers.h:80
std::vector< RooMsgService::StreamConfig > _oldConf
Definition RooHelpers.h:81
~HijackMessageStream()
Deregister the hijacked stream and restore the stream state of all previous streams.
const HijackMessageStream & operator<<(const T &v) const
Definition RooHelpers.h:69
Switches the message service to a different level while the instance is alive.
Definition RooHelpers.h:37
RooFit::MsgLevel fOldKillBelow
Definition RooHelpers.h:50
std::vector< RooMsgService::StreamConfig > fOldConf
Definition RooHelpers.h:51
Mother of all ROOT objects.
Definition TObject.h:41
MsgLevel
Verbosity level for RooMsgService::StreamConfig in RooMsgService.
MsgTopic
Topics for a RooMsgService::StreamConfig in RooMsgService.
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.
Wrap an object into a TObject. Sometimes needed to avoid reinterpret_cast or enable RTTI.
Definition RooHelpers.h:57