Belle II Software light-2607-kasei
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
16namespace Belle2 {
22 namespace Utils {
23
25 template<class... Ts> struct VisitOverload : Ts... { using Ts::operator()...; };
27 template<class... Ts> VisitOverload(Ts...) -> VisitOverload<Ts...>;
28
33 template<class T>
34 T reduceTBranch(TBranch* branch, const std::function<T(T, T)>& f, T reduced = T())
35 {
36 T object;
37 branch->SetAddress(&object);
38 int nevents = branch->GetEntries();
39 for (int i = 0; i < nevents; ++i) {
40 branch->GetEvent(i);
41 reduced = f(reduced, object);
42 }
43 return reduced;
44 }
45
46
56 double getClock();
57
63 double getCPUClock();
64
69 unsigned long getVirtualMemoryKB();
70
75 unsigned long getRssMemoryKB();
76
78 class Timer {
79 public:
81 explicit Timer(std::string text = "");
82 ~Timer();
83 private:
84 double m_startTime;
85 std::string m_text;
86 };
87
91 std::string getCommandOutput(const std::string& command, const std::vector<std::string>& arguments = {}, bool searchPath = true);
92 }
93
101#define B2INFO_MEASURE_TIME(txt, ...) {\
102 std::stringstream __b2_timer_str__;\
103 __b2_timer_str__ << txt;\
104 ::Belle2::Utils::Timer __b2_timer__(__b2_timer_str__.str());\
105 {__VA_ARGS__;}\
106 }
107
128#if defined(__GNUC__) || defined(__ICL) || defined(__clang__)
129#define branch_likely(x) __builtin_expect(!!(x), 1)
130#define branch_unlikely(x) __builtin_expect(!!(x), 0)
131#else
132#define branch_likely(x) (x)
133#define branch_unlikely(x) (x)
134#endif
135
137} // Belle2 namespace
double m_startTime
time at start (in ns).
Definition Utils.h:84
std::string m_text
identifying text (printed at end).
Definition Utils.h:85
Timer(std::string text="")
Constructor, with some identifying text.
Definition Utils.cc:98
General utility functions.
Definition Utils.h:22
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:109
T reduceTBranch(TBranch *branch, const std::function< T(T, T)> &f, T reduced=T())
Reduce a branch of a TTree.
Definition Utils.h:34
double getCPUClock()
Return current value of the per-thread CPU clock.
Definition Utils.cc:81
double getClock()
Return current value of the real-time clock.
Definition Utils.cc:75
unsigned long getRssMemoryKB()
Returns the amount of memory the process actually occupies in the physical RAM of the machine.
Definition Utils.cc:93
unsigned long getVirtualMemoryKB()
Returns currently used virtual memory in KB, includes swapped and not occupied memory pages and memor...
Definition Utils.cc:88
VisitOverload(Ts...) -> VisitOverload< Ts... >
Function for the C++17 std::visit overload pattern to allow simple use of variants.
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:25