1 #ifndef NdmspcCoreNUtils_H
2 #define NdmspcCoreNUtils_H
12 #include <THnSparse.h>
13 #include <TBufferJSON.h>
37 static bool EnableMT(Int_t numthreads = -1);
59 static int Cp(std::string source, std::string destination,Bool_t progressbar = kTRUE);
69 static TFile *
OpenFile(std::string filename, std::string mode =
"READ",
bool createLocalDir =
true);
76 static std::string
OpenRawFile(std::string filename);
84 static bool SaveRawFile(std::string filename, std::string content);
91 static TMacro *
OpenMacro(std::string filename);
100 static bool LoadJsonFile(json & cfg, std::string filename);
102 using RawJsonInjections = std::vector<std::pair<std::vector<std::string>, std::string>>;
118 static std::string
InjectRawJson(json & j,
const RawJsonInjections & injections);
131 static void AddRawJsonInjection(json & j,
const std::vector<std::string> & path,
const std::string & rawJson,
132 const std::string & injectionsKey =
"__raw_json_injections");
143 const std::string & injectionsKey =
"__raw_json_injections");
164 static TH1 *
ProjectTHnSparse(THnSparse * hns,
const std::vector<int> & axes, Option_t * option =
"");
175 static bool SetAxisRanges(THnSparse * sparse, std::vector<std::vector<int>> ranges = {},
bool withOverflow =
false,
176 bool modifyTitle =
false,
bool reset =
true);
187 static bool SetAxisRanges(THnSparse * sparse, std::map<
int, std::vector<int>> ranges,
bool withOverflow =
false,
188 bool modifyTitle =
false,
bool reset =
true);
200 static bool GetAxisRangeInBase(TAxis * a,
int rebin,
int rebin_start,
int bin,
int & min,
int & max);
212 static bool GetAxisRangeInBase(TAxis * a,
int min,
int max, TAxis * base,
int & minBase,
int & maxBase);
222 const std::vector<std::string> & labels);
232 const std::set<std::string> & labels);
241 static THnSparse *
Convert(TH1 * h1, std::vector<std::string> names = {}, std::vector<std::string> titles = {});
250 static THnSparse *
Convert(TH2 * h2, std::vector<std::string> names = {}, std::vector<std::string> titles = {});
259 static THnSparse *
Convert(TH3 * h3, std::vector<std::string> names = {}, std::vector<std::string> titles = {});
270 static THnSparse *
ReshapeSparseAxes(THnSparse * hns, std::vector<int> order, std::vector<TAxis *> newAxes = {},
271 std::vector<int> newPoint = {}, Option_t * option =
"E");
281 bool include_overflow_underflow =
false);
295 static TObjArray *
AxesFromDirectory(
const std::vector<std::string> paths,
const std::string & findPath,
296 const std::string & fileName,
const std::vector<std::string> & axesNames);
304 static std::vector<std::string>
Tokenize(std::string_view input,
const char delim);
312 static std::vector<int>
TokenizeInt(std::string_view input,
const char delim);
320 static std::string
Join(
const std::vector<std::string> & values,
const char delim =
',');
328 static std::string
Join(
const std::vector<int> & values,
const char delim =
',');
336 static std::vector<std::string>
Find(std::string path, std::string filename =
"");
344 static std::vector<std::string>
FindLocal(std::string path, std::string filename =
"");
352 static std::vector<std::string>
FindEos(std::string path, std::string filename =
"");
362 static std::set<std::string>
Unique(std::vector<std::string> & paths,
int axis, std::string path,
char token =
'/');
370 static std::vector<std::string>
Truncate(std::vector<std::string> values, std::string value);
394 static std::string
GetCoordsString(
const std::vector<int> & coords,
int index = -1,
int width = 0);
403 static std::string
GetCoordsString(
const std::vector<size_t> & coords,
int index = -1,
int width = 0);
412 static std::string
GetCoordsString(
const std::vector<Long64_t> & coords,
int index = -1,
int width = 0);
421 static std::string
GetCoordsString(
const std::vector<std::string> & coords,
int index = -1,
int width = 0);
428 static void PrintPointSafe(
const std::vector<int> & coords,
int index = -1);
435 static std::vector<std::vector<int>>
Permutations(
const std::vector<int> & v);
496 static std::string
FormatTime(
long long seconds);
506 static void ProgressBar(
int current,
int total, std::string prefix =
"", std::string suffix =
"",
int barWidth = 50);
517 static void ProgressBar(
int current,
int total, std::chrono::high_resolution_clock::time_point startTime,
518 std::string prefix =
"", std::string suffix =
"",
int barWidth = 50);
531 static TCanvas *
CreateCanvas(
const std::string & name,
const std::string & title,
int width = 800,
int height = 600);
570 Int_t nMaxRows = -1);
Utility class providing static helper functions for file operations, histogram manipulations,...
static void GetTrueHistogramMinMax(const TH1 *h, double &min_val, double &max_val, bool include_overflow_underflow=false)
Get minimum and maximum value of histogram bins.
static bool SetAxisRanges(THnSparse *sparse, std::vector< std::vector< int >> ranges={}, bool withOverflow=false, bool modifyTitle=false, bool reset=true)
Set axis ranges for THnSparse using vector of ranges.
static TFile * OpenFile(std::string filename, std::string mode="READ", bool createLocalDir=true)
Open a ROOT file.
static void AddRawJsonInjection(json &j, const std::vector< std::string > &path, const std::string &rawJson, const std::string &injectionsKey="__raw_json_injections")
Add one raw JSON injection entry into metadata field.
static std::vector< std::string > Truncate(std::vector< std::string > values, std::string value)
Truncate vector of strings by a value.
static TH1 * ProjectTHnSparse(THnSparse *hns, const std::vector< int > &axes, Option_t *option="")
Project a THnSparse histogram onto specified axes.
static bool IsFileSupported(std::string filename)
Check if a file is supported.
static std::vector< std::string > FindEos(std::string path, std::string filename="")
Find EOS files in a path matching filename.
static bool LoadJsonFile(json &cfg, std::string filename)
Loads a JSON configuration file into the provided json object.
static json GetTFileIOStats()
Get TFile read/write statistics by inspecting ROOT's list of open files.
static bool SaveRawFile(std::string filename, std::string content)
Save content to a raw file.
static std::string OpenRawFile(std::string filename)
Open a raw file and return its content as string.
static std::vector< std::string > FindLocal(std::string path, std::string filename="")
Find local files in a path matching filename.
static void SafeDeleteTList(TList *&lst)
Safely delete a TList and all its contents, bypassing ROOT's GarbageCollect.
static void PrintPointSafe(const std::vector< int > &coords, int index=-1)
Print coordinates safely.
static TCanvas * CreateCanvas(const std::string &name, const std::string &title, int width=800, int height=600)
Create a ROOT TCanvas with specified name, title, and dimensions.
static std::string MergeRawJsonWithMetadata(const std::string &rawJson, const json &metadata)
Merge raw JSON string with metadata fields.
static THnSparse * ReshapeSparseAxes(THnSparse *hns, std::vector< int > order, std::vector< TAxis * > newAxes={}, std::vector< int > newPoint={}, Option_t *option="E")
Reshape axes of THnSparse.
static json GetNetDevStats()
Get system-wide network interface totals (RX/TX bytes) in a cross-platform way. On Linux reads /proc/...
static bool AccessPathName(std::string path)
Check if a path is accessible.
static std::vector< int > TokenizeInt(std::string_view input, const char delim)
Tokenize a string into integers by delimiter.
static THnSparse * Convert(TH1 *h1, std::vector< std::string > names={}, std::vector< std::string > titles={})
Convert TH1 to THnSparse.
static TMacro * OpenMacro(std::string filename)
Open a macro file.
static std::vector< std::string > Tokenize(std::string_view input, const char delim)
Tokenize a string by delimiter.
static bool CreateDirectory(const std::string &path)
static std::string FormatTime(long long seconds)
Format time in seconds to human-readable string.
static TAxis * CreateAxisFromLabels(const std::string &name, const std::string &title, const std::vector< std::string > &labels)
Create a TAxis from a list of labels.
static bool GetAxisRangeInBase(TAxis *a, int rebin, int rebin_start, int bin, int &min, int &max)
Get axis range in base for rebinned axis.
static json GetSystemStats()
Get process CPU and RSS memory statistics using ROOT's gSystem::GetProcInfo.
static std::set< std::string > Unique(std::vector< std::string > &paths, int axis, std::string path, char token='/')
Get unique values from vector of strings at specified axis.
static std::vector< std::string > GetJsonStringArray(json j)
Get JSON value as array of strings.
static std::string GetJsonString(json j)
Get JSON value as string.
static int Cp(std::string source, std::string destination, Bool_t progressbar=kTRUE)
Copy a file from source to destination.
static bool CollectRawJsonInjections(const json &j, RawJsonInjections &injections, const std::string &injectionsKey="__raw_json_injections")
Collect raw JSON injection entries from metadata field.
static bool EnableMT(Int_t numthreads=-1)
Enable multi-threading with specified number of threads.
static int GetJsonInt(json j)
Get JSON value as integer.
static std::string Join(const std::vector< std::string > &values, const char delim=',')
Join vector of strings into a single string with delimiter.
static void ProgressBar(int current, int total, std::string prefix="", std::string suffix="", int barWidth=50)
Display progress bar.
static std::string GetCoordsString(const std::vector< int > &coords, int index=-1, int width=0)
Get string representation of coordinates.
static void SafeDeleteObjects(std::vector< TObject * > &objects)
Safely delete a vector of ROOT objects, bypassing GarbageCollect.
static THnSparse * CreateSparseFromParquetTaxi(const std::string &filename, THnSparse *hns=nullptr, Int_t nMaxRows=-1)
Create THnSparse from Parquet Taxi file.
static void SafeDeleteObject(TObject *&obj)
Safely delete a TObject, handling TList contents and TCanvas/TPad cleanup.
static void VectorToArray(std::vector< int > v1, Int_t *v2)
Convert vector to array.
static std::vector< int > ArrayToVector(Int_t *v1, int size)
Convert array to vector.
static double GetJsonDouble(json j)
Get JSON value as double.
static bool GetJsonBool(json j)
Get JSON value as boolean.
static TObjArray * AxesFromDirectory(const std::vector< std::string > paths, const std::string &findPath, const std::string &fileName, const std::vector< std::string > &axesNames)
Creates an array of axes objects from files in specified directories.
static std::vector< std::string > Find(std::string path, std::string filename="")
Find files in a path matching filename.
static std::vector< std::vector< int > > Permutations(const std::vector< int > &v)
Generate all permutations of a vector.
virtual ~NUtils()
Destructor.
static TAxis * CreateAxisFromLabelsSet(const std::string &name, const std::string &title, const std::set< std::string > &labels)
Create a TAxis from a set of labels.
static std::string InjectRawJson(json &j, const RawJsonInjections &injections)