ndmspc v1.2.0-0.1.rc5
Loading...
Searching...
No Matches
NUtils.h
1#ifndef NdmspcCoreNUtils_H
2#define NdmspcCoreNUtils_H
3
4#include <set>
5#include <vector>
6#include <TFile.h>
7#include <TCanvas.h>
8#include <TAxis.h>
9#include <TMacro.h>
10#include <TH2.h>
11#include <TH3.h>
12#include <THnSparse.h>
13#include <TBufferJSON.h>
14#include <TString.h>
15#include "NLogger.h"
16
17namespace Ndmspc {
25class NUtils : TObject {
26
28 NUtils() {};
30 virtual ~NUtils() {};
31
32 public:
37 static bool EnableMT(Int_t numthreads = -1);
38
44 static bool AccessPathName(std::string path);
45
51 static bool IsFileSupported(std::string filename);
52
59 static int Cp(std::string source, std::string destination,Bool_t progressbar = kTRUE);
60 static bool CreateDirectory(const std::string & path);
61
69 static TFile * OpenFile(std::string filename, std::string mode = "READ", bool createLocalDir = true);
70
76 static std::string OpenRawFile(std::string filename);
77
84 static bool SaveRawFile(std::string filename, std::string content);
85
91 static TMacro * OpenMacro(std::string filename);
92
100 static bool LoadJsonFile(json & cfg, std::string filename);
101
102 using RawJsonInjections = std::vector<std::pair<std::vector<std::string>, std::string>>;
103
118 static std::string InjectRawJson(json & j, const RawJsonInjections & injections);
119
131 static void AddRawJsonInjection(json & j, const std::vector<std::string> & path, const std::string & rawJson,
132 const std::string & injectionsKey = "__raw_json_injections");
133
142 static bool CollectRawJsonInjections(const json & j, RawJsonInjections & injections,
143 const std::string & injectionsKey = "__raw_json_injections");
144
155 static std::string MergeRawJsonWithMetadata(const std::string & rawJson, const json & metadata);
156
164 static TH1 * ProjectTHnSparse(THnSparse * hns, const std::vector<int> & axes, Option_t * option = "");
165
175 static bool SetAxisRanges(THnSparse * sparse, std::vector<std::vector<int>> ranges = {}, bool withOverflow = false,
176 bool modifyTitle = false, bool reset = true);
177
187 static bool SetAxisRanges(THnSparse * sparse, std::map<int, std::vector<int>> ranges, bool withOverflow = false,
188 bool modifyTitle = false, bool reset = true);
189
200 static bool GetAxisRangeInBase(TAxis * a, int rebin, int rebin_start, int bin, int & min, int & max);
201
212 static bool GetAxisRangeInBase(TAxis * a, int min, int max, TAxis * base, int & minBase, int & maxBase);
213
221 static TAxis * CreateAxisFromLabels(const std::string & name, const std::string & title,
222 const std::vector<std::string> & labels);
223
231 static TAxis * CreateAxisFromLabelsSet(const std::string & name, const std::string & title,
232 const std::set<std::string> & labels);
233
241 static THnSparse * Convert(TH1 * h1, std::vector<std::string> names = {}, std::vector<std::string> titles = {});
242
250 static THnSparse * Convert(TH2 * h2, std::vector<std::string> names = {}, std::vector<std::string> titles = {});
251
259 static THnSparse * Convert(TH3 * h3, std::vector<std::string> names = {}, std::vector<std::string> titles = {});
260
270 static THnSparse * ReshapeSparseAxes(THnSparse * hns, std::vector<int> order, std::vector<TAxis *> newAxes = {},
271 std::vector<int> newPoint = {}, Option_t * option = "E");
272
280 static void GetTrueHistogramMinMax(const TH1 * h, double & min_val, double & max_val,
281 bool include_overflow_underflow = false);
282
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);
297
304 static std::vector<std::string> Tokenize(std::string_view input, const char delim);
305
312 static std::vector<int> TokenizeInt(std::string_view input, const char delim);
313
320 static std::string Join(const std::vector<std::string> & values, const char delim = ',');
321
328 static std::string Join(const std::vector<int> & values, const char delim = ',');
329
336 static std::vector<std::string> Find(std::string path, std::string filename = "");
337
344 static std::vector<std::string> FindLocal(std::string path, std::string filename = "");
345
352 static std::vector<std::string> FindEos(std::string path, std::string filename = "");
353
362 static std::set<std::string> Unique(std::vector<std::string> & paths, int axis, std::string path, char token = '/');
363
370 static std::vector<std::string> Truncate(std::vector<std::string> values, std::string value);
371
378 static std::vector<int> ArrayToVector(Int_t * v1, int size);
379
385 static void VectorToArray(std::vector<int> v1, Int_t * v2);
386
394 static std::string GetCoordsString(const std::vector<int> & coords, int index = -1, int width = 0);
395
403 static std::string GetCoordsString(const std::vector<size_t> & coords, int index = -1, int width = 0);
404
412 static std::string GetCoordsString(const std::vector<Long64_t> & coords, int index = -1, int width = 0);
413
421 static std::string GetCoordsString(const std::vector<std::string> & coords, int index = -1, int width = 0);
422
428 static void PrintPointSafe(const std::vector<int> & coords, int index = -1);
429
435 static std::vector<std::vector<int>> Permutations(const std::vector<int> & v);
436
442 static std::string GetJsonString(json j);
443
449 static int GetJsonInt(json j);
450
456 static double GetJsonDouble(json j);
457
462 static json GetSystemStats();
463
468 static json GetTFileIOStats();
469
475 static json GetNetDevStats();
476
482 static bool GetJsonBool(json j);
483
489 static std::vector<std::string> GetJsonStringArray(json j);
490
496 static std::string FormatTime(long long seconds);
497
506 static void ProgressBar(int current, int total, std::string prefix = "", std::string suffix = "", int barWidth = 50);
507
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);
519
531 static TCanvas * CreateCanvas(const std::string & name, const std::string & title, int width = 800, int height = 600);
532
541 static void SafeDeleteObjects(std::vector<TObject *> & objects);
542
551 static void SafeDeleteTList(TList *& lst);
552
560 static void SafeDeleteObject(TObject *& obj);
561
569 static THnSparse * CreateSparseFromParquetTaxi(const std::string & filename, THnSparse * hns = nullptr,
570 Int_t nMaxRows = -1);
571
573 ClassDef(NUtils, 0);
575
576}; // namespace NUtils
577} // namespace Ndmspc
578#endif
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.
Definition NUtils.cxx:621
static TFile * OpenFile(std::string filename, std::string mode="READ", bool createLocalDir=true)
Open a ROOT file.
Definition NUtils.cxx:719
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.
Definition NUtils.cxx:885
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.
Definition NUtils.cxx:1224
static std::vector< std::string > Truncate(std::vector< std::string > values, std::string value)
Truncate vector of strings by a value.
Definition NUtils.cxx:1142
static TH1 * ProjectTHnSparse(THnSparse *hns, const std::vector< int > &axes, Option_t *option="")
Project a THnSparse histogram onto specified axes.
Definition NUtils.cxx:1171
static bool IsFileSupported(std::string filename)
Check if a file is supported.
Definition NUtils.cxx:97
static std::vector< std::string > FindEos(std::string path, std::string filename="")
Find EOS files in a path matching filename.
Definition NUtils.cxx:1012
static bool LoadJsonFile(json &cfg, std::string filename)
Loads a JSON configuration file into the provided json object.
Definition NUtils.cxx:818
static json GetTFileIOStats()
Get TFile read/write statistics by inspecting ROOT's list of open files.
Definition NUtils.cxx:2078
static bool SaveRawFile(std::string filename, std::string content)
Save content to a raw file.
Definition NUtils.cxx:765
static std::string OpenRawFile(std::string filename)
Open a raw file and return its content as string.
Definition NUtils.cxx:735
static std::vector< std::string > FindLocal(std::string path, std::string filename="")
Find local files in a path matching filename.
Definition NUtils.cxx:990
static void SafeDeleteTList(TList *&lst)
Safely delete a TList and all its contents, bypassing ROOT's GarbageCollect.
Definition NUtils.cxx:2022
static void PrintPointSafe(const std::vector< int > &coords, int index=-1)
Print coordinates safely.
Definition NUtils.cxx:1634
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.
Definition NUtils.cxx:1752
static std::string MergeRawJsonWithMetadata(const std::string &rawJson, const json &metadata)
Merge raw JSON string with metadata fields.
Definition NUtils.cxx:945
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.
Definition NUtils.cxx:452
static json GetNetDevStats()
Get system-wide network interface totals (RX/TX bytes) in a cross-platform way. On Linux reads /proc/...
Definition NUtils.cxx:2134
static bool AccessPathName(std::string path)
Check if a path is accessible.
Definition NUtils.cxx:115
static std::vector< int > TokenizeInt(std::string_view input, const char delim)
Tokenize a string into integers by delimiter.
Definition NUtils.cxx:1099
static THnSparse * Convert(TH1 *h1, std::vector< std::string > names={}, std::vector< std::string > titles={})
Convert TH1 to THnSparse.
Definition NUtils.cxx:221
static TMacro * OpenMacro(std::string filename)
Open a macro file.
Definition NUtils.cxx:781
static std::vector< std::string > Tokenize(std::string_view input, const char delim)
Tokenize a string by delimiter.
Definition NUtils.cxx:1077
static bool CreateDirectory(const std::string &path)
Definition NUtils.cxx:688
static std::string FormatTime(long long seconds)
Format time in seconds to human-readable string.
Definition NUtils.cxx:1664
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.
Definition NUtils.cxx:185
static bool GetAxisRangeInBase(TAxis *a, int rebin, int rebin_start, int bin, int &min, int &max)
Get axis range in base for rebinned axis.
Definition NUtils.cxx:1342
static json GetSystemStats()
Get process CPU and RSS memory statistics using ROOT's gSystem::GetProcInfo.
Definition NUtils.cxx:2059
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.
Definition NUtils.cxx:1156
static std::vector< std::string > GetJsonStringArray(json j)
Get JSON value as array of strings.
Definition NUtils.cxx:1540
static std::string GetJsonString(json j)
Get JSON value as string.
Definition NUtils.cxx:1446
static int Cp(std::string source, std::string destination, Bool_t progressbar=kTRUE)
Copy a file from source to destination.
Definition NUtils.cxx:155
static bool CollectRawJsonInjections(const json &j, RawJsonInjections &injections, const std::string &injectionsKey="__raw_json_injections")
Collect raw JSON injection entries from metadata field.
Definition NUtils.cxx:928
NUtils()
Constructor.
Definition NUtils.h:28
static bool EnableMT(Int_t numthreads=-1)
Enable multi-threading with specified number of threads.
Definition NUtils.cxx:46
static int GetJsonInt(json j)
Get JSON value as integer.
Definition NUtils.cxx:1471
static std::string Join(const std::vector< std::string > &values, const char delim=',')
Join vector of strings into a single string with delimiter.
Definition NUtils.cxx:1115
static void ProgressBar(int current, int total, std::string prefix="", std::string suffix="", int barWidth=50)
Display progress bar.
Definition NUtils.cxx:1677
static std::string GetCoordsString(const std::vector< int > &coords, int index=-1, int width=0)
Get string representation of coordinates.
Definition NUtils.cxx:1592
static void SafeDeleteObjects(std::vector< TObject * > &objects)
Safely delete a vector of ROOT objects, bypassing GarbageCollect.
Definition NUtils.cxx:1957
static THnSparse * CreateSparseFromParquetTaxi(const std::string &filename, THnSparse *hns=nullptr, Int_t nMaxRows=-1)
Create THnSparse from Parquet Taxi file.
Definition NUtils.cxx:1949
static void SafeDeleteObject(TObject *&obj)
Safely delete a TObject, handling TList contents and TCanvas/TPad cleanup.
Definition NUtils.cxx:2044
static void VectorToArray(std::vector< int > v1, Int_t *v2)
Convert vector to array.
Definition NUtils.cxx:1568
static std::vector< int > ArrayToVector(Int_t *v1, int size)
Convert array to vector.
Definition NUtils.cxx:1555
static double GetJsonDouble(json j)
Get JSON value as double.
Definition NUtils.cxx:1494
static bool GetJsonBool(json j)
Get JSON value as boolean.
Definition NUtils.cxx:1517
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.
Definition NUtils.cxx:1400
static std::vector< std::string > Find(std::string path, std::string filename="")
Find files in a path matching filename.
Definition NUtils.cxx:967
static std::vector< std::vector< int > > Permutations(const std::vector< int > &v)
Generate all permutations of a vector.
Definition NUtils.cxx:1643
virtual ~NUtils()
Destructor.
Definition NUtils.h:30
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.
Definition NUtils.cxx:202
static std::string InjectRawJson(json &j, const RawJsonInjections &injections)
Definition NUtils.cxx:843
Global callback function for libwebsockets client events.