ndmspc  0.20250128.0
Utils.cxx
1 #include <TSystem.h>
2 #include <TFile.h>
3 #include <TMacro.h>
4 #include <TString.h>
5 #include <cstring>
6 #include <fstream>
7 #include <string>
8 #include "Utils.h"
9 
10 using std::ifstream;
11 
13 ClassImp(Ndmspc::Utils);
15 
16 namespace Ndmspc {
17 
18 TFile * Utils::OpenFile(std::string filename, std::string mode, bool createLocalDir)
19 {
23 
24  filename = gSystem->ExpandPathName(filename.c_str());
25  if (createLocalDir) {
26  // Printf("%s", filename.c_str());
27  if (!mode.compare("RECREATE") || !mode.compare("UPDATE") || !mode.compare("WRITE")) {
28 
29  TString filenameT(filename.c_str());
30  bool isLocalFile = filenameT.BeginsWith("file://");
31  if (isLocalFile) {
32  // Remove file:// prefix
33  filenameT.ReplaceAll("file://", "");
34  }
35  else {
36  isLocalFile = !filenameT.Contains("://");
37  }
38 
39  if (isLocalFile) {
40 
41  std::string pwd = gSystem->pwd();
42  if (filenameT[0] != '/') filenameT = pwd + "/" + filenameT;
43  filenameT.ReplaceAll("?remote=1&", "?");
44  filenameT.ReplaceAll("?remote=1", "");
45  filenameT.ReplaceAll("&remote=1", "");
46  TUrl url(filenameT.Data());
47 
48  std::string filenameLocal = gSystem->GetDirName(url.GetFile()).Data();
49  // Printf("Ndmspc::Utils::OpenRootFile: Creating directory '%s' ...", filenameLocal.c_str());
50  gSystem->mkdir(filenameLocal.c_str(), kTRUE);
51  }
52  }
53  }
54  return TFile::Open(filename.c_str(), mode.c_str());
55 }
56 
57 std::string Utils::OpenRawFile(std::string filename)
58 {
59  std::string content;
60  TFile * f = TFile::Open(TString::Format("%s?filetype=raw", filename.c_str()));
61  if (!f) return "";
62 
63  // Printf("%lld", f->GetSize());
64 
65  int buffsize = 4096;
66  char buff[buffsize + 1];
67 
68  Long64_t buffread = 0;
69  while (buffread < f->GetSize()) {
70  if (buffread + buffsize > f->GetSize()) buffsize = f->GetSize() - buffread;
71 
72  // Printf("Buff %lld %d", buffread, buffsize);
73  f->ReadBuffer(buff, buffread, buffsize);
74  buff[buffsize] = '\0';
75  content += buff;
76  buffread += buffsize;
77  }
78  f->Close();
79  return content;
80 }
81 
82 TMacro * Utils::OpenMacro(std::string filename)
83 {
84 
85  std::string content = OpenRawFile(filename);
86  if (content.empty()) {
87 
88  Printf("Error: Problem opening macro '%s' ...", filename.c_str());
89  return nullptr;
90  }
91  Printf("Using macro '%s' ...", filename.c_str());
92  TUrl url(filename.c_str());
93  std::string basefilename = gSystem->BaseName(url.GetFile());
94  basefilename.pop_back();
95  basefilename.pop_back();
96  TMacro * m = new TMacro();
97  m->SetName(basefilename.c_str());
98  m->AddLine(content.c_str());
99  return m;
100 }
101 
102 // void Utils::RebinBins(int & min, int & max, int rebin)
103 //
104 // {
105 // ///
106 // /// Rebin bins
107 // ///
108 // Printf("%d %d %d", min, max, rebin);
109 //
110 // Int_t binMin = min;
111 // Int_t binMax = max;
112 // Int_t binDiff = binMax - binMin;
113 //
114 // if (rebin > 1) {
115 // binMin = 1 + ((binMin - 1) * rebin);
116 // binMax = ((binMin - 1) + rebin * (binDiff + 1));
117 // }
118 //
119 // min = binMin;
120 // max = binMax;
121 // }
122 std::string Utils::GetCutsPath(json cuts)
123 {
124  std::string path = "";
125  std::string rebinStr = "";
126  for (auto & cut : cuts) {
127  Int_t rebin = 1;
128  Int_t rebin_start = 1;
129  Int_t rebin_minimum = 1;
130  if (cut["enabled"].is_boolean() && cut["enabled"].get<bool>() == false) continue;
131  if (cut["rebin"].is_number_integer()) rebin = cut["rebin"].get<Int_t>();
132  if (cut["rebin_start"].is_number_integer()) rebin_start = cut["rebin_start"].get<Int_t>();
133 
134  if (rebin_start > 1) {
135  rebin_minimum = (rebin_start % rebin);
136  if (rebin_minimum == 0) rebin_minimum = 1;
137  }
138  path += cut["axis"].get<std::string>() + "_";
139  // Printf("rebin_minimum=%d rebin_start=%d rebin=%d", rebin_minimum, rebin_start, rebin);
140  rebinStr += std::to_string(rebin) + "-" + std::to_string(rebin_minimum) + "_";
141  }
142  path[path.size() - 1] = '/';
143  rebinStr[rebinStr.size() - 1] = '/';
144  path += rebinStr;
145 
146  // Printf("Path: %s", path.c_str());
147  // exit(1);
148  return std::move(path);
149 }
150 Int_t Utils::GetBinFromBase(Int_t bin, Int_t rebin, Int_t rebin_start)
151 {
152  if (rebin == 1) return bin;
153  // Printf("GetBinFromBase %d %d %d", bin, rebin, rebin_start);
154 
155  return (bin / rebin) + 1;
156 
157  // Int_t binLocal = (bin / rebin);
158  // Int_t rebin_minimum = 1;
159  // if (rebin_start > 1) {
160  // rebin_minimum = (rebin_start % rebin);
161  // if (rebin_minimum == 0) rebin_minimum = 1;
162  // // return binLocal + rebin_minimum;
163  // }
164  // // Printf("binLocal=%d", binLocal + rebin_minimum);
165  // return binLocal + rebin_minimum;
166 }
167 
168 int Utils::SetResultValueError(json cfg, THnSparse * output, std::string name, Int_t * point, double val, double err,
169  bool normalizeToWidth, bool onlyPositive, double times)
170 {
171 
172  int verbose = 0;
173  if (!cfg["user"]["verbose"].is_null() && cfg["user"]["verbose"].is_number_integer()) {
174  verbose = cfg["user"]["verbose"].get<int>();
175  }
176 
177  bool isValNan = TMath::IsNaN(val);
178  bool isErrNaN = TMath::IsNaN(err);
179 
180  if (isValNan || isErrNaN) {
181  if (verbose >= 0)
182  Printf("Error: SetResultValueError %s val=%f[isNaN=%d] err=%f[isNan=%d]", name.c_str(), val, isValNan, err,
183  isErrNaN);
184  return -2;
185  }
186 
187  if (onlyPositive && val < 0) {
188  return -3;
189  }
190 
191  if (times > 0 && times * std::abs(val) < err) {
192  if (verbose >= 0)
193  Printf("Warning: Skipping '%s' because 'times * val < err' ( %f * %f < %f ) ...", name.c_str(), times,
194  std::abs(val), err);
195  return -4;
196  }
197 
198  if (normalizeToWidth) {
199  int nDimsCuts = 0;
200  for (auto & cut : cfg["ndmspc"]["cuts"]) {
201  if (cut["enabled"].is_boolean() && cut["enabled"].get<bool>() == false) continue;
202  nDimsCuts++;
203  }
204  // Printf("1 %f %f", val, err);
205  for (int iCut = 0; iCut < nDimsCuts; iCut++) {
206  double w = output->GetAxis(iCut + 1)->GetBinWidth(point[iCut + 1]);
207  val /= w;
208  err /= w;
209  // Printf("%d %f %f w=%f", iCut + 1, val, err, w);
210  }
211  }
212 
213  int idx = output->GetAxis(0)->FindBin(name.c_str());
214  if (idx <= 0) {
215  return idx;
216  }
217  point[0] = idx;
218  Long64_t binId = output->GetBin(point);
219  output->SetBinContent(binId, val);
220  output->SetBinError(binId, err);
221 
222  return idx;
223 }
224 std::vector<std::string> Utils::Tokenize(std::string_view input, const char delim)
225 {
226  std::vector<std::string> out;
227 
228  for (auto found = input.find(delim); found != std::string_view::npos; found = input.find(delim)) {
229  out.emplace_back(input, 0, found);
230  input.remove_prefix(found + 1);
231  }
232 
233  if (not input.empty()) out.emplace_back(input);
234 
235  return out;
236 }
237 
238 } // namespace Ndmspc
Utils object.
Definition: Utils.h:20
static TFile * OpenFile(std::string filename, std::string mode="READ", bool createLocalDir=true)
Definition: Utils.cxx:18