Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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::LocalChangeMsgLevel 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
117/// Struct to temporarily change the operation mode of a RooAbsArg until it
118/// goes out of scope.
120public:
121 ChangeOperModeRAII(RooAbsArg *arg, RooAbsArg::OperMode opMode) : _arg{arg}, _oldOpMode(arg->operMode())
122 {
123 arg->setOperMode(opMode, /*recurse=*/false);
124 }
125 ~ChangeOperModeRAII() { _arg->setOperMode(_oldOpMode, /*recurse=*/false); }
126
127private:
128 RooAbsArg *_arg = nullptr;
130};
131
132
133std::pair<double, double> getRangeOrBinningInterval(RooAbsArg const* arg, const char* rangeName);
134
135bool checkIfRangesOverlap(RooArgSet const& observables, std::vector<std::string> const& rangeNames);
136
137std::string getColonSeparatedNameString(RooArgSet const& argSet);
138RooArgSet selectFromArgSet(RooArgSet const&, std::string const& names);
139
140
141/// Clone RooAbsArg object and reattach to original parameters.
142template<class T>
143std::unique_ptr<T> cloneTreeWithSameParameters(T const& arg, RooArgSet const* observables=nullptr) {
144 std::unique_ptr<T> clone{static_cast<T *>(arg.cloneTree())};
145 RooArgSet origParams;
146 arg.getParameters(observables, origParams);
147 clone->recursiveRedirectServers(origParams);
148 return clone;
149}
150
151std::string getRangeNameForSimComponent(std::string const& rangeName, bool splitRange, std::string const& catName);
152
154 RooAbsPdf * binnedPdf = nullptr;
155 bool isBinnedL = false;
156};
157
159
160void getSortedComputationGraph(RooAbsReal const &func, RooArgSet &out);
161
162}
163
164#endif /* ROOFIT_ROOFITCORE_INC_ROOHELPERS_H_ */
RooAbsArg is the common abstract base class for objects that represent a value and a "shape" in RooFi...
Definition RooAbsArg.h:74
void setOperMode(OperMode mode, bool recurseADirty=true)
Set the operation mode of this node.
static void setDirtyInhibit(bool flag)
Control global dirty inhibit mode.
RooAbsData is the common abstract base class for binned and unbinned datasets.
Definition RooAbsData.h:59
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition RooAbsReal.h:62
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:55
Struct to temporarily change the operation mode of a RooAbsArg until it goes out of scope.
Definition RooHelpers.h:119
RooAbsArg::OperMode _oldOpMode
Definition RooHelpers.h:129
ChangeOperModeRAII(RooAbsArg *arg, RooAbsArg::OperMode opMode)
Definition RooHelpers.h:121
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
std::vector< RooMsgService::StreamConfig > _oldConf
Definition RooHelpers.h:89
~HijackMessageStream()
Deregister the hijacked stream and restore the stream state of all previous streams.
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
std::vector< RooMsgService::StreamConfig > fOldConf
Definition RooHelpers.h:58
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 getSortedComputationGraph(RooAbsReal const &func, RooArgSet &out)
Get the topologically-sorted list of all nodes in the computation graph.
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.
std::unique_ptr< T > cloneTreeWithSameParameters(T const &arg, RooArgSet const *observables=nullptr)
Clone RooAbsArg object and reattach to original parameters.
Definition RooHelpers.h:143
std::string getRangeNameForSimComponent(std::string const &rangeName, bool splitRange, std::string const &catName)
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.
bool checkIfRangesOverlap(RooArgSet const &observables, std::vector< std::string > const &rangeNames)
Check if there is any overlap when a list of ranges is applied to a set of observables.
BinnedLOutput getBinnedL(RooAbsPdf const &pdf)
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.
std::string getColonSeparatedNameString(RooArgSet const &argSet)
Create a string with all sorted names of RooArgSet elements separated by colons.
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