Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:11:22

0001 /*
0002  * Project: RooFit
0003  * Author:
0004  *   Stephan Hageboeck, CERN 2019
0005  *
0006  * Copyright (c) 2023, CERN
0007  *
0008  * Redistribution and use in source and binary forms,
0009  * with or without modification, are permitted according to the terms
0010  * listed in LICENSE (http://roofit.sourceforge.net/license.txt)
0011  */
0012 
0013 #ifndef RooFit_RooHelpers_h
0014 #define RooFit_RooHelpers_h
0015 
0016 #include <RooMsgService.h>
0017 #include <RooAbsArg.h>
0018 #include <RooAbsReal.h>
0019 
0020 #include <sstream>
0021 #include <vector>
0022 #include <string>
0023 #include <utility>
0024 
0025 class RooAbsPdf;
0026 class RooAbsData;
0027 
0028 namespace RooHelpers {
0029 
0030 /// Switches the message service to a different level while the instance is alive.
0031 /// Can also temporarily activate / deactivate message topics.
0032 /// Use as
0033 /// ~~~{.cpp}
0034 /// RooHelpers::LocalChangeMsgLevel changeMsgLvl(RooFit::WARNING);
0035 /// [ statements that normally generate a lot of output ]
0036 /// ~~~
0037 class LocalChangeMsgLevel {
0038 public:
0039    /// Change message level (and topics) while this object is alive, reset when it goes out of scope.
0040    /// \param[in] lvl The desired message level. Defaults to verbose.
0041    /// \param[in] extraTopics Extra topics to be switched on. These will only switched on in the last stream to prevent
0042    /// all streams are printing. \param[in] removeTopics Message topics to be switched off \param[in]
0043    /// overrideExternalLevel Override the user message level.
0044    LocalChangeMsgLevel(RooFit::MsgLevel lvl = RooFit::DEBUG, unsigned int extraTopics = 0u,
0045                        unsigned int removeTopics = 0u, bool overrideExternalLevel = true);
0046 
0047    ~LocalChangeMsgLevel();
0048 
0049 private:
0050    RooFit::MsgLevel fOldKillBelow;
0051    std::vector<RooMsgService::StreamConfig> fOldConf;
0052    int fExtraStream{-1};
0053 };
0054 
0055 /// Wrap an object into a TObject. Sometimes needed to avoid reinterpret_cast or enable RTTI.
0056 template <typename T>
0057 struct WrapIntoTObject : public TObject {
0058    WrapIntoTObject(T &obj) : _payload(&obj) {}
0059    T *_payload;
0060 };
0061 
0062 /// Hijacks all messages with given level and topic (and optionally object name) while alive.
0063 /// Use this like an ostringstream afterwards. The messages can e.g. be retrieved using `str()`.
0064 /// Useful for unit tests / debugging.
0065 class HijackMessageStream {
0066 public:
0067    HijackMessageStream(RooFit::MsgLevel level, RooFit::MsgTopic topics, const char *objectName = nullptr);
0068    template <typename T>
0069    const HijackMessageStream &operator<<(const T &v) const
0070    {
0071       _str << v;
0072       return *this;
0073    }
0074    std::string str() { return _str.str(); }
0075    std::ostringstream &stream() { return _str; };
0076    ~HijackMessageStream();
0077 
0078 private:
0079    std::ostringstream _str;
0080    RooFit::MsgLevel _oldKillBelow;
0081    std::vector<RooMsgService::StreamConfig> _oldConf;
0082    Int_t _thisStream;
0083 };
0084 
0085 /// Check if the parameters have a range, and warn if the range extends below / above the set limits.
0086 void checkRangeOfParameters(const RooAbsReal *callingClass, std::initializer_list<const RooAbsReal *> pars,
0087                             double min = -std::numeric_limits<double>::max(),
0088                             double max = std::numeric_limits<double>::max(), bool limitsInAllowedRange = false,
0089                             std::string const &extraMessage = "");
0090 
0091 
0092 } // namespace RooHelpers
0093 
0094 #endif