ndmspc  v1.1.1-1
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 <nlohmann/json.hpp>
14 using json = nlohmann::json;
15 
16 namespace Ndmspc {
24 class NUtils : TObject {
25 
27  NUtils() {};
29  virtual ~NUtils() {};
30 
31  public:
36  static bool EnableMT(Int_t numthreads = -1);
37 
43  static bool AccessPathName(std::string path);
44 
50  static bool IsFileSupported(std::string filename);
51 
58  static int Cp(std::string source, std::string destination);
59 
67  static TFile * OpenFile(std::string filename, std::string mode = "READ", bool createLocalDir = true);
68 
74  static std::string OpenRawFile(std::string filename);
75 
82  static bool SaveRawFile(std::string filename, std::string content);
83 
89  static TMacro * OpenMacro(std::string filename);
90 
98  static bool LoadJsonFile(json & cfg, std::string filename);
106  static TH1 * ProjectTHnSparse(THnSparse * hns, const std::vector<int> & axes, Option_t * option = "");
107 
117  static bool SetAxisRanges(THnSparse * sparse, std::vector<std::vector<int>> ranges = {}, bool withOverflow = false,
118  bool modifyTitle = false, bool reset = true);
119 
129  static bool SetAxisRanges(THnSparse * sparse, std::map<int, std::vector<int>> ranges, bool withOverflow = false,
130  bool modifyTitle = false, bool reset = true);
131 
142  static bool GetAxisRangeInBase(TAxis * a, int rebin, int rebin_start, int bin, int & min, int & max);
143 
154  static bool GetAxisRangeInBase(TAxis * a, int min, int max, TAxis * base, int & minBase, int & maxBase);
155 
163  static TAxis * CreateAxisFromLabels(const std::string & name, const std::string & title,
164  const std::vector<std::string> & labels);
165 
173  static TAxis * CreateAxisFromLabelsSet(const std::string & name, const std::string & title,
174  const std::set<std::string> & labels);
175 
183  static THnSparse * Convert(TH1 * h1, std::vector<std::string> names = {}, std::vector<std::string> titles = {});
184 
192  static THnSparse * Convert(TH2 * h2, std::vector<std::string> names = {}, std::vector<std::string> titles = {});
193 
201  static THnSparse * Convert(TH3 * h3, std::vector<std::string> names = {}, std::vector<std::string> titles = {});
202 
212  static THnSparse * ReshapeSparseAxes(THnSparse * hns, std::vector<int> order, std::vector<TAxis *> newAxes = {},
213  std::vector<int> newPoint = {}, Option_t * option = "E");
214 
222  static void GetTrueHistogramMinMax(const TH1 * h, double & min_val, double & max_val,
223  bool include_overflow_underflow = false);
224 
237  static TObjArray * AxesFromDirectory(const std::vector<std::string> paths, const std::string & findPath,
238  const std::string & fileName, const std::vector<std::string> & axesNames);
239 
246  static std::vector<std::string> Tokenize(std::string_view input, const char delim);
247 
254  static std::vector<int> TokenizeInt(std::string_view input, const char delim);
255 
262  static std::string Join(const std::vector<std::string> & values, const char delim = ',');
263 
270  static std::string Join(const std::vector<int> & values, const char delim = ',');
271 
278  static std::vector<std::string> Find(std::string path, std::string filename = "");
279 
286  static std::vector<std::string> FindLocal(std::string path, std::string filename = "");
287 
294  static std::vector<std::string> FindEos(std::string path, std::string filename = "");
295 
304  static std::set<std::string> Unique(std::vector<std::string> & paths, int axis, std::string path, char token = '/');
305 
312  static std::vector<std::string> Truncate(std::vector<std::string> values, std::string value);
313 
320  static std::vector<int> ArrayToVector(Int_t * v1, int size);
321 
327  static void VectorToArray(std::vector<int> v1, Int_t * v2);
328 
336  static std::string GetCoordsString(const std::vector<int> & coords, int index = -1, int width = 0);
337 
345  static std::string GetCoordsString(const std::vector<size_t> & coords, int index = -1, int width = 0);
346 
354  static std::string GetCoordsString(const std::vector<Long64_t> & coords, int index = -1, int width = 0);
355 
363  static std::string GetCoordsString(const std::vector<std::string> & coords, int index = -1, int width = 0);
364 
370  static void PrintPointSafe(const std::vector<int> & coords, int index = -1);
371 
377  static std::vector<std::vector<int>> Permutations(const std::vector<int> & v);
378 
384  static std::string GetJsonString(json j);
385 
391  static int GetJsonInt(json j);
392 
398  static double GetJsonDouble(json j);
399 
400 
405  static json GetSystemStats();
406 
411  static json GetTFileIOStats();
412 
418  static json GetNetDevStats();
419 
425  static bool GetJsonBool(json j);
426 
432  static std::vector<std::string> GetJsonStringArray(json j);
433 
439  static std::string FormatTime(long long seconds);
440 
449  static void ProgressBar(int current, int total, std::string prefix = "", std::string suffix = "", int barWidth = 50);
450 
460  static void ProgressBar(int current, int total, std::chrono::high_resolution_clock::time_point startTime,
461  std::string prefix = "", std::string suffix = "", int barWidth = 50);
462 
474  static TCanvas * CreateCanvas(const std::string & name, const std::string & title, int width = 800, int height = 600);
475 
484  static void SafeDeleteObjects(std::vector<TObject *> & objects);
485 
494  static void SafeDeleteTList(TList *& lst);
495 
503  static void SafeDeleteObject(TObject *& obj);
504 
505 
513  static THnSparse * CreateSparseFromParquetTaxi(const std::string & filename, THnSparse * hns = nullptr,
514  Int_t nMaxRows = -1);
515 
517  ClassDef(NUtils, 0);
519 
520 }; // namespace NUtils
521 } // namespace Ndmspc
522 #endif
Utility class providing static helper functions for file operations, histogram manipulations,...
Definition: NUtils.h:24
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:618
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:1075
static TFile * OpenFile(std::string filename, std::string mode="READ", bool createLocalDir=true)
Open a ROOT file.
Definition: NUtils.cxx:685
static int Cp(std::string source, std::string destination)
Copy a file from source to destination.
Definition: NUtils.cxx:152
static std::vector< std::string > Truncate(std::vector< std::string > values, std::string value)
Truncate vector of strings by a value.
Definition: NUtils.cxx:993
static TH1 * ProjectTHnSparse(THnSparse *hns, const std::vector< int > &axes, Option_t *option="")
Project a THnSparse histogram onto specified axes.
Definition: NUtils.cxx:1022
static bool IsFileSupported(std::string filename)
Check if a file is supported.
Definition: NUtils.cxx:94
static std::vector< std::string > FindEos(std::string path, std::string filename="")
Find EOS files in a path matching filename.
Definition: NUtils.cxx:863
static bool LoadJsonFile(json &cfg, std::string filename)
Loads a JSON configuration file into the provided json object.
Definition: NUtils.cxx:793
static json GetTFileIOStats()
Get TFile read/write statistics by inspecting ROOT's list of open files.
Definition: NUtils.cxx:1931
static bool SaveRawFile(std::string filename, std::string content)
Save content to a raw file.
Definition: NUtils.cxx:754
static std::string OpenRawFile(std::string filename)
Open a raw file and return its content as string.
Definition: NUtils.cxx:724
static std::vector< std::string > FindLocal(std::string path, std::string filename="")
Find local files in a path matching filename.
Definition: NUtils.cxx:841
static void SafeDeleteTList(TList *&lst)
Safely delete a TList and all its contents, bypassing ROOT's GarbageCollect.
Definition: NUtils.cxx:1876
static void PrintPointSafe(const std::vector< int > &coords, int index=-1)
Print coordinates safely.
Definition: NUtils.cxx:1485
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:1603
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:449
static json GetNetDevStats()
Get system-wide network interface totals (RX/TX bytes) in a cross-platform way. On Linux reads /proc/...
Definition: NUtils.cxx:1987
static bool AccessPathName(std::string path)
Check if a path is accessible.
Definition: NUtils.cxx:112
static std::vector< int > TokenizeInt(std::string_view input, const char delim)
Tokenize a string into integers by delimiter.
Definition: NUtils.cxx:950
static THnSparse * Convert(TH1 *h1, std::vector< std::string > names={}, std::vector< std::string > titles={})
Convert TH1 to THnSparse.
Definition: NUtils.cxx:218
static TMacro * OpenMacro(std::string filename)
Open a macro file.
Definition: NUtils.cxx:770
static std::vector< std::string > Tokenize(std::string_view input, const char delim)
Tokenize a string by delimiter.
Definition: NUtils.cxx:928
static std::string FormatTime(long long seconds)
Format time in seconds to human-readable string.
Definition: NUtils.cxx:1515
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:182
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:1193
static json GetSystemStats()
Get process CPU and RSS memory statistics using ROOT's gSystem::GetProcInfo.
Definition: NUtils.cxx:1912
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:1007
static std::vector< std::string > GetJsonStringArray(json j)
Get JSON value as array of strings.
Definition: NUtils.cxx:1391
static std::string GetJsonString(json j)
Get JSON value as string.
Definition: NUtils.cxx:1297
NUtils()
Constructor.
Definition: NUtils.h:27
static bool EnableMT(Int_t numthreads=-1)
Enable multi-threading with specified number of threads.
Definition: NUtils.cxx:43
static int GetJsonInt(json j)
Get JSON value as integer.
Definition: NUtils.cxx:1322
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:966
static void ProgressBar(int current, int total, std::string prefix="", std::string suffix="", int barWidth=50)
Display progress bar.
Definition: NUtils.cxx:1528
static std::string GetCoordsString(const std::vector< int > &coords, int index=-1, int width=0)
Get string representation of coordinates.
Definition: NUtils.cxx:1443
static void SafeDeleteObjects(std::vector< TObject * > &objects)
Safely delete a vector of ROOT objects, bypassing GarbageCollect.
Definition: NUtils.cxx:1808
static THnSparse * CreateSparseFromParquetTaxi(const std::string &filename, THnSparse *hns=nullptr, Int_t nMaxRows=-1)
Create THnSparse from Parquet Taxi file.
Definition: NUtils.cxx:1800
static void SafeDeleteObject(TObject *&obj)
Safely delete a TObject, handling TList contents and TCanvas/TPad cleanup.
Definition: NUtils.cxx:1898
static void VectorToArray(std::vector< int > v1, Int_t *v2)
Convert vector to array.
Definition: NUtils.cxx:1419
static std::vector< int > ArrayToVector(Int_t *v1, int size)
Convert array to vector.
Definition: NUtils.cxx:1406
static double GetJsonDouble(json j)
Get JSON value as double.
Definition: NUtils.cxx:1345
static bool GetJsonBool(json j)
Get JSON value as boolean.
Definition: NUtils.cxx:1368
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:1251
static std::vector< std::string > Find(std::string path, std::string filename="")
Find files in a path matching filename.
Definition: NUtils.cxx:818
static std::vector< std::vector< int > > Permutations(const std::vector< int > &v)
Generate all permutations of a vector.
Definition: NUtils.cxx:1494
virtual ~NUtils()
Destructor.
Definition: NUtils.h:29
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:199