Belle II Software  release-05-02-19
Utils.h
1 #pragma once
2 
3 #include <TBranch.h>
4 
5 #include <string>
6 #include <sstream>
7 #include <functional>
8 
9 namespace Belle2 {
15  namespace Utils {
16 
18  // cppcheck-suppress syntaxError ; apparently this confuses cppcheck
19  template<class... Ts> struct VisitOverload : Ts... { using Ts::operator()...; };
21  template<class... Ts> VisitOverload(Ts...) -> VisitOverload<Ts...>;
22 
27  template<class T>
28  T reduceTBranch(TBranch* branch, const std::function<T(T, T)>& f, T reduced = T())
29  {
30  T object;
31  branch->SetAddress(&object);
32  int nevents = branch->GetEntries();
33  for (int i = 0; i < nevents; ++i) {
34  branch->GetEvent(i);
35  reduced = f(reduced, object);
36  }
37  return reduced;
38  }
39 
40 
50  double getClock();
51 
57  double getCPUClock();
58 
63  unsigned long getVirtualMemoryKB();
64 
69  unsigned long getRssMemoryKB();
70 
72  class Timer {
73  public:
75  explicit Timer(std::string text = "");
76  ~Timer();
77  private:
78  double m_startTime;
79  std::string m_text;
80  };
81 
85  std::string getCommandOutput(const std::string& command, const std::vector<std::string>& arguments = {}, bool searchPath = true);
86  }
87 
95 #define B2INFO_MEASURE_TIME(txt, ...) {\
96  std::stringstream __b2_timer_str__;\
97  __b2_timer_str__ << txt;\
98  ::Belle2::Utils::Timer __b2_timer__(__b2_timer_str__.str());\
99  {__VA_ARGS__;}\
100  }
101 
143 #if defined(__GNUC__) || defined(__ICL) || defined(__clang__)
144 #define branch_likely(x) __builtin_expect(!!(x), 1)
145 #define branch_unlikely(x) __builtin_expect(!!(x), 0)
146 #else
147 #define branch_likely(x) (x)
148 #define branch_unlikely(x) (x)
149 #endif
150 
152 } // Belle2 namespace
Belle2::Utils::getRssMemoryKB
unsigned long getRssMemoryKB()
Returns the amount of memory the process actually occupies in the physical RAM of the machine.
Definition: Utils.cc:76
Belle2::Utils::VisitOverload
Helper struct for the C++17 std::visit overload pattern to allow simple use of variants.
Definition: Utils.h:19
Belle2::Utils::VisitOverload
VisitOverload(Ts...) -> VisitOverload< Ts... >
Function for the C++17 std::visit overload pattern to allow simple use of variants.
Belle2::Utils::Timer::Timer
Timer(std::string text="")
Constructor, with some identifying text.
Definition: Utils.cc:81
Belle2::Utils::getCPUClock
double getCPUClock()
Return current value of the per-thread CPU clock.
Definition: Utils.cc:64
Belle2::Utils::getCommandOutput
std::string getCommandOutput(const std::string &command, const std::vector< std::string > &arguments={}, bool searchPath=true)
Execute a shell command and return its output.
Definition: Utils.cc:92
Belle2::Utils::reduceTBranch
T reduceTBranch(TBranch *branch, const std::function< T(T, T)> &f, T reduced=T())
Reduce a branch of a TTree.
Definition: Utils.h:28
Belle2::Utils::Timer
Small helper class that prints its lifetime when destroyed.
Definition: Utils.h:72
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::Utils::Timer::m_startTime
double m_startTime
time at start (in ns).
Definition: Utils.h:78
Belle2::Utils::getClock
double getClock()
Return current value of the real-time clock.
Definition: Utils.cc:58
Belle2::Utils::getVirtualMemoryKB
unsigned long getVirtualMemoryKB()
Returns currently used virtual memory in KB, includes swapped and not occupied memory pages and memor...
Definition: Utils.cc:71
Belle2::Utils::Timer::m_text
std::string m_text
identifying text (printed at end).
Definition: Utils.h:79