Belle II Software development
Belle2::Utils Namespace Reference

General utility functions. More...

Classes

class  Timer
 Small helper class that prints its lifetime when destroyed. More...
 
struct  VisitOverload
 Helper struct for the C++17 std::visit overload pattern to allow simple use of variants. More...
 

Functions

template<class... Ts>
 VisitOverload (Ts...) -> VisitOverload< Ts... >
 Function for the C++17 std::visit overload pattern to allow simple use of variants.
 
template<class T>
reduceTBranch (TBranch *branch, const std::function< T(T, T)> &f, T reduced=T())
 Reduce a branch of a TTree.
 
double getClock ()
 Return current value of the real-time clock.
 
double getCPUClock ()
 Return current value of the per-thread CPU clock.
 
unsigned long getVirtualMemoryKB ()
 Returns currently used virtual memory in KB, includes swapped and not occupied memory pages and memory-mapped files.
 
unsigned long getRssMemoryKB ()
 Returns the amount of memory the process actually occupies in the physical RAM of the machine.
 
std::string getCommandOutput (const std::string &command, const std::vector< std::string > &arguments={}, bool searchPath=true)
 Execute a shell command and return its output.
 

Detailed Description

General utility functions.

Function Documentation

◆ getClock()

double getClock ( )

Return current value of the real-time clock.

The returned value is meant to measure relative times and does not show absolute time values.

Note
See getCPUClock() for a higher-resolution clock unaffected by wait times.
Returns
Clock value in default time unit (ns)

Definition at line 70 of file Utils.cc.

71 {
72 timespec ts;
73 clock_gettime(CLOCK_REALTIME, &ts);
74 return (ts.tv_sec * Unit::s) + (ts.tv_nsec * Unit::ns);
75 }
static const double ns
Standard of [time].
Definition Unit.h:48
static const double s
[second]
Definition Unit.h:95

◆ 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 at line 104 of file Utils.cc.

106 {
107 namespace bp = boost::process::v1;
108 auto cmd = searchPath ? bp::search_path(command) : boost::filesystem::path(command);
109 bp::ipstream cmdOut;
110 bp::child child(cmd, bp::args(arguments), bp::std_in.close(), bp::std_out > cmdOut);
111 char buffer[4096];
112 std::string result;
113 while (child.running() && cmdOut.read(buffer, sizeof(buffer))) {
114 result.append(buffer, sizeof(buffer));
115 }
116 if (cmdOut.gcount()) result.append(buffer, cmdOut.gcount());
117 return result;
118 }

◆ getCPUClock()

double getCPUClock ( )

Return current value of the per-thread CPU clock.

Returns
CPU clock value in default time unit (ns)

Definition at line 76 of file Utils.cc.

77 {
78 timespec ts;
79 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
80 return (ts.tv_sec * Unit::s) + (ts.tv_nsec * Unit::ns);
81 }

◆ getRssMemoryKB()

unsigned long getRssMemoryKB ( )

Returns the amount of memory the process actually occupies in the physical RAM of the machine.

Definition at line 88 of file Utils.cc.

89 {
90 return getStatmSize().second;
91 }

◆ getVirtualMemoryKB()

unsigned long getVirtualMemoryKB ( )

Returns currently used virtual memory in KB, includes swapped and not occupied memory pages and memory-mapped files.

Definition at line 83 of file Utils.cc.

84 {
85 return getStatmSize().first;
86 }

◆ reduceTBranch()

template<class T>
T reduceTBranch ( TBranch * branch,
const std::function< T(T, T)> & f,
T reduced = T() )

Reduce a branch of a TTree.

Returns
reduced branch

Definition at line 35 of file Utils.h.

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 }