OMSim
Geant4 for IceCube optical module studies
Tools Namespace Reference

A collection of helper functions for numerical operations. More...

Functions

std::vector< double > arange (double p_start, double p_stop, double p_step)
 Generates a sequence of numbers. More...
 
TGraph * create1dInterpolator (const std::vector< double > &p_X, const std::vector< double > &p_y, const std::string &p_name)
 Creates a TGraph interpolator from x and y data points. More...
 
TGraph * create1dInterpolator (const std::string &p_filename)
 Creates a TGraph interpolator from x and y in file. More...
 
TH2D * create2DHistogramFromDataFile (const std::string &p_filePath)
 Create a histogram from provided data. More...
 
void ensureDirectoryExists (const std::string &p_filePath)
 Ensure that the directory of a file to be created exists. More...
 
std::pair< std::vector< double >, std::vector< double > > histogram (const std::vector< double > &data, const std::variant< int, std::vector< double >> &bins, const std::optional< std::pair< double, double >> &p_range, const std::vector< double > &p_weights)
 Compute the histogram of a dataset. More...
 
G4String getThreadIDStr ()
 
std::vector< std::vector< double > > loadtxt (const std::string &p_filePath, bool p_unpack, size_t p_skipRows, char p_delimiter, char p_comments)
 Reads numerical data from a file and returns it as a 2D vector. Similar to numpy.loadtxt. More...
 
std::vector< double > linspace (double p_start, double p_stop, int p_num, bool p_endpoint)
 Generates a linearly spaced vector. More...
 
std::vector< double > logspace (double p_start, double p_stop, int p_num, double base, bool p_endpoint)
 Generates a logarithmically spaced vector. More...
 
void sortVectorByReference (std::vector< G4double > &p_referenceVector, std::vector< G4double > &p_sortVector)
 Sorts two vectors (p_sortVector & p_referenceVector) based on the order of values in p_referenceVector. More...
 
double median (std::vector< double > p_vec)
 
double mean (const std::vector< double > &p_vec, const std::vector< double > &p_weights={})
 
double std (const std::vector< double > &vec, const std::vector< double > &p_weights={})
 
void throwError (const G4String &message)
 
std::vector< G4String > splitStringByDelimiter (G4String const &p_string, char p_delim)
 
std::vector< G4String > splitStringByDelimiter (char *p_char, char p_delim)
 

Variables

std::string visualisationURL = "https://icecube.github.io/OMSim/md_extra_doc_2_technicalities.html#autotoc_md30"
 

Detailed Description

This namespace provides static methods for common numerical tasks such as loading data from a file, and generating linearly and logarithmically spaced sequences.

Function Documentation

◆ arange()

std::vector< double > Tools::arange ( double  p_start,
double  p_stop,
double  p_step 
)
Parameters
p_startThe p_start of the interval. The interval includes this value.
p_stopThe end of the interval. The interval does not include this value.
p_stepThe spacing between values. For any output out, this is the distance between two adjacent values, out[i+1] - out[i].
Returns
A vector of evenly spaced values.
Exceptions
std::invalid_argumentif p_step is zero.

◆ create1dInterpolator() [1/2]

TGraph * Tools::create1dInterpolator ( const std::string &  p_filename)
Parameters
p_filename
Returns
Pointer to the new TGraph object.
Note
Caller is responsible for deleting the returned TGraph!

◆ create1dInterpolator() [2/2]

TGraph * Tools::create1dInterpolator ( const std::vector< double > &  p_X,
const std::vector< double > &  p_y,
const std::string &  p_name 
)
Parameters
p_XVector of x-coordinates.
p_yVector of y-coordinates.
p_nameOptional name for the TGraph. It should be unique!
Returns
Pointer to the new TGraph object.
Note
Caller is responsible for deleting the returned TGraph!

◆ create2DHistogramFromDataFile()

TH2D * Tools::create2DHistogramFromDataFile ( const std::string &  p_filePath)

Loads the data from a given path and constructs a histogram based on the data.

Parameters
p_filePathPath to the data file.
pTH2DNameName of the histogram.
Returns
Pointer to the created histogram.
Note
Caller is responsible for deleting the returned TH2D!

◆ ensureDirectoryExists()

void Tools::ensureDirectoryExists ( const std::string &  p_filePath)
Parameters
p_filePathThe path of file.

◆ getThreadIDStr()

G4String Tools::getThreadIDStr ( )
Returns
Thread id which called method as string

◆ histogram()

std::pair< std::vector< double >, std::vector< double > > Tools::histogram ( const std::vector< double > &  data,
const std::variant< int, std::vector< double >> &  bins,
const std::optional< std::pair< double, double >> &  p_range,
const std::vector< double > &  p_weights 
)
Parameters
dataInput data. The histogram is computed over the data.
binsThe number of equal-width bins in the given range (10, by default). If bins is a sequence, it defines the bin edges, including the rightmost edge.
rangeThe lower and upper range of the bins. If not provided, range is simply (min(data), max(data)). Values outside the range are ignored.
Returns
A pair of vectors:
  • The first vector contains the values of the histogram (counts in each bin).
  • The second vector contains the bin edges.
Note
This function behaves similarly to NumPy's numpy.histogram:
  • It returns the values of the histogram and the bin edges.
  • The last bin includes the right edge.
  • Values outside the range are ignored.
  • If bins is an integer, it represents the number of bins. If it's a vector, it represents the bin edges.

◆ linspace()

std::vector< double > Tools::linspace ( double  p_start,
double  p_stop,
int  p_num,
bool  p_endpoint 
)

Creates a vector of equally spaced values between p_start and end.

Parameters
p_startThe starting value of the sequence.
p_stopThe end value of the sequence, unless p_endpoint is False. In that case, the sequence consists of all but the last of p_num + 1 evenly spaced samples, so that p_stop is excluded. Note that the p_step size changes when p_endpoint is False.
p_numThe number of points to generate in the sequence.
p_endpointIf True (default), p_stop is the last sample. Otherwise, it is not included.
Returns
A vector of linearly spaced values.
Exceptions
std::invalid_argumentif p_num is less than 2.

◆ loadtxt()

std::vector< std::vector< double > > Tools::loadtxt ( const std::string &  p_filePath,
bool  p_unpack,
size_t  p_skipRows,
char  p_delimiter,
char  p_comments 
)
Parameters
p_filePathThe path to the input file.
p_unpackOptional. If true, the returned data is transposed, i.e., unpacked into columns. Default is true.
p_skipRowsOptional. The number of lines to skip at the beginning of the file. Default is 0.
p_delimiterThe character used to separate values in each line of the input file.
p_commentsOptional. The character used to indicate the p_start of a comment. Default is '#'.
Returns
A 2D vector of doubles. The outer vector groups all columns (or rows if 'unpack' is false), and each inner vector represents one of the columns (or one of the rows if 'unpack' is false) of data file.
Exceptions
std::runtime_errorif the file cannot be opened.

◆ logspace()

std::vector< double > Tools::logspace ( double  p_start,
double  p_stop,
int  p_num,
double  base,
bool  p_endpoint 
)

Creates a vector of values that are logarithmically spaced. The sequence starts at base^p_start and ends at base^p_stop.

Parameters
p_startThe starting value of the sequence (as a power of base).
p_stopThe ending value of the sequence (as a power of base).
p_numThe number of points to generate in the sequence. Default is 50.
baseThe base of the log space. Default is 10.0.
p_endpointWhether to include the p_stop value in the output. Default is true.
Returns
A vector of logarithmically spaced values.
Exceptions
std::invalid_argumentif p_num is negative.
Note
This function behaves similarly to NumPy's np.logspace:
  • If p_num is 0, returns an empty vector.
  • p_start and p_stop are used as powers of base.
  • The sequence includes base^p_start and base^p_stop if p_endpoint is true.

◆ sortVectorByReference()

void Tools::sortVectorByReference ( std::vector< G4double > &  p_referenceVector,
std::vector< G4double > &  p_sortVector 
)
Parameters
p_referenceVectorThe ordering of these values will determine the final order of both vectors.
p_sortVectorThe vector to be sorted according to the p_referenceVector.
Exceptions
std::invalid_argumentif the vectors do not have the same size.