Belle II Software  release-08-01-10
Utils.h
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 #pragma once
9 
10 #include <TBranch.h>
11 
12 #include <string>
13 #include <sstream>
14 #include <functional>
15 
16 namespace Belle2 {
22  namespace Utils {
23 
25  // cppcheck-suppress syntaxError ; apparently this confuses cppcheck
26  template<class... Ts> struct VisitOverload : Ts... { using Ts::operator()...; };
28  template<class... Ts> VisitOverload(Ts...) -> VisitOverload<Ts...>;
29 
34  template<class T>
35  T reduceTBranch(TBranch* branch, const std::function<T(T, T)>& f, T reduced = T())
36  {
37  T object;
38  branch->SetAddress(&object);
39  int nevents = branch->GetEntries();
40  for (int i = 0; i < nevents; ++i) {
41  branch->GetEvent(i);
42  reduced = f(reduced, object);
43  }
44  return reduced;
45  }
46 
47 
57  double getClock();
58 
64  double getCPUClock();
65 
70  unsigned long getVirtualMemoryKB();
71 
76  unsigned long getRssMemoryKB();
77 
79  class Timer {
80  public:
82  explicit Timer(std::string text = "");
83  ~Timer();
84  private:
85  double m_startTime;
86  std::string m_text;
87  };
88 
92  std::string getCommandOutput(const std::string& command, const std::vector<std::string>& arguments = {}, bool searchPath = true);
93  }
94 
102 #define B2INFO_MEASURE_TIME(txt, ...) {\
103  std::stringstream __b2_timer_str__;\
104  __b2_timer_str__ << txt;\
105  ::Belle2::Utils::Timer __b2_timer__(__b2_timer_str__.str());\
106  {__VA_ARGS__;}\
107  }
108 
129 #if defined(__GNUC__) || defined(__ICL) || defined(__clang__)
130 #define branch_likely(x) __builtin_expect(!!(x), 1)
131 #define branch_unlikely(x) __builtin_expect(!!(x), 0)
132 #else
133 #define branch_likely(x) (x)
134 #define branch_unlikely(x) (x)
135 #endif
136 
138 } // Belle2 namespace
Small helper class that prints its lifetime when destroyed.
Definition: Utils.h:79
double m_startTime
time at start (in ns).
Definition: Utils.h:85
std::string m_text
identifying text (printed at end).
Definition: Utils.h:86
Timer(std::string text="")
Constructor, with some identifying text.
Definition: Utils.cc:89
T reduceTBranch(TBranch *branch, const std::function< T(T, T)> &f, T reduced=T())
Reduce a branch of a TTree.
Definition: Utils.h:35
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:100
double getCPUClock()
Return current value of the per-thread CPU clock.
Definition: Utils.cc:72
VisitOverload(Ts...) -> VisitOverload< Ts... >
Function for the C++17 std::visit overload pattern to allow simple use of variants.
double getClock()
Return current value of the real-time clock.
Definition: Utils.cc:66
unsigned long getRssMemoryKB()
Returns the amount of memory the process actually occupies in the physical RAM of the machine.
Definition: Utils.cc:84
unsigned long getVirtualMemoryKB()
Returns currently used virtual memory in KB, includes swapped and not occupied memory pages and memor...
Definition: Utils.cc:79
Abstract base class for different kinds of events.
Helper struct for the C++17 std::visit overload pattern to allow simple use of variants.
Definition: Utils.h:26