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 66 of file Utils.cc.

67 {
68 timespec ts;
69 clock_gettime(CLOCK_REALTIME, &ts);
70 return (ts.tv_sec * Unit::s) + (ts.tv_nsec * Unit::ns);
71 }
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 100 of file Utils.cc.

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

◆ getCPUClock()

double getCPUClock ( )

Return current value of the per-thread CPU clock.

Returns
CPU clock value in default time unit (ns)

Definition at line 72 of file Utils.cc.

73 {
74 timespec ts;
75 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
76 return (ts.tv_sec * Unit::s) + (ts.tv_nsec * Unit::ns);
77 }

◆ getRssMemoryKB()

unsigned long getRssMemoryKB ( )

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

Definition at line 84 of file Utils.cc.

85 {
86 return getStatmSize().second;
87 }

◆ 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 79 of file Utils.cc.

80 {
81 return getStatmSize().first;
82 }

◆ reduceTBranch()

T reduceTBranch ( TBranch *  branch,
const std::function< T(T, T)> &  f,
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 }