ndmspc 0.20250304.0
Loading...
Searching...
No Matches
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
10using std::ifstream;
11
13ClassImp(Ndmspc::Utils);
15
16namespace Ndmspc {
17
18TFile * 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
57std::string Utils::OpenRawFile(std::string filename)
58{
62
63 std::string content;
64 TFile * f = OpenFile(TString::Format("%s?filetype=raw", filename.c_str()).Data());
65 if (!f) return "";
66
67 // Printf("%lld", f->GetSize());
68
69 int buffsize = 4096;
70 char buff[buffsize + 1];
71
72 Long64_t buffread = 0;
73 while (buffread < f->GetSize()) {
74 if (buffread + buffsize > f->GetSize()) buffsize = f->GetSize() - buffread;
75
76 // Printf("Buff %lld %d", buffread, buffsize);
77 f->ReadBuffer(buff, buffread, buffsize);
78 buff[buffsize] = '\0';
79 content += buff;
80 buffread += buffsize;
81 }
82 f->Close();
83 return content;
84}
85bool Utils::SaveRawFile(std::string filename, std::string content)
86{
90
91 TFile * f = OpenFile(TString::Format("%s?filetype=raw", filename.c_str()).Data(), "RECREATE");
92 if (!f) return false;
93
94 f->WriteBuffer(content.c_str(), content.size());
95
96 f->Close();
97 return true;
98}
99
100TMacro * Utils::OpenMacro(std::string filename)
101{
105
106 std::string content = OpenRawFile(filename);
107 if (content.empty()) {
108
109 Printf("Error: Problem opening macro '%s' ...", filename.c_str());
110 return nullptr;
111 }
112 Printf("Using macro '%s' ...", filename.c_str());
113 TUrl url(filename.c_str());
114 std::string basefilename = gSystem->BaseName(url.GetFile());
115 basefilename.pop_back();
116 basefilename.pop_back();
117 TMacro * m = new TMacro();
118 m->SetName(basefilename.c_str());
119 m->AddLine(content.c_str());
120 return m;
121}
122
123// void Utils::RebinBins(int & min, int & max, int rebin)
124//
125// {
126// ///
127// /// Rebin bins
128// ///
129// Printf("%d %d %d", min, max, rebin);
130//
131// Int_t binMin = min;
132// Int_t binMax = max;
133// Int_t binDiff = binMax - binMin;
134//
135// if (rebin > 1) {
136// binMin = 1 + ((binMin - 1) * rebin);
137// binMax = ((binMin - 1) + rebin * (binDiff + 1));
138// }
139//
140// min = binMin;
141// max = binMax;
142// }
143std::string Utils::GetCutsPath(json cuts)
144{
148
149 std::string path = "";
150 std::string rebinStr = "";
151 for (auto & cut : cuts) {
152 Int_t rebin = 1;
153 Int_t rebin_start = 1;
154 Int_t rebin_minimum = 1;
155 if (cut["enabled"].is_boolean() && cut["enabled"].get<bool>() == false) continue;
156 if (cut["rebin"].is_number_integer()) rebin = cut["rebin"].get<Int_t>();
157 if (cut["rebin_start"].is_number_integer()) rebin_start = cut["rebin_start"].get<Int_t>();
158
159 if (rebin_start > 1) {
160 rebin_minimum = (rebin_start % rebin);
161 if (rebin_minimum == 0) rebin_minimum = 1;
162 }
163 path += cut["axis"].get<std::string>() + "_";
164 // Printf("rebin_minimum=%d rebin_start=%d rebin=%d", rebin_minimum, rebin_start, rebin);
165 rebinStr += std::to_string(rebin) + "-" + std::to_string(rebin_minimum) + "_";
166 }
167 path[path.size() - 1] = '/';
168 rebinStr[rebinStr.size() - 1] = '/';
169 path += rebinStr;
170
171 // Printf("Path: %s", path.c_str());
172 // exit(1);
173 return path;
174}
175Int_t Utils::GetBinFromBase(Int_t bin, Int_t rebin, Int_t rebin_start)
176{
180
181 if (rebin == 1) return bin;
182 // Printf("GetBinFromBase %d %d %d", bin, rebin, rebin_start);
183
184 return (bin / rebin) + 1;
185
186 // Int_t binLocal = (bin / rebin);
187 // Int_t rebin_minimum = 1;
188 // if (rebin_start > 1) {
189 // rebin_minimum = (rebin_start % rebin);
190 // if (rebin_minimum == 0) rebin_minimum = 1;
191 // // return binLocal + rebin_minimum;
192 // }
193 // // Printf("binLocal=%d", binLocal + rebin_minimum);
194 // return binLocal + rebin_minimum;
195}
196
197int Utils::SetResultValueError(json cfg, THnSparse * output, std::string name, Int_t * point, double val, double err,
198 bool normalizeToWidth, bool onlyPositive, double times)
199{
203
204 int verbose = 0;
205 if (!cfg["user"]["verbose"].is_null() && cfg["user"]["verbose"].is_number_integer()) {
206 verbose = cfg["user"]["verbose"].get<int>();
207 }
208
209 bool isValNan = TMath::IsNaN(val);
210 bool isErrNaN = TMath::IsNaN(err);
211
212 if (isValNan || isErrNaN) {
213 if (verbose >= 0)
214 Printf("Error: SetResultValueError %s val=%f[isNaN=%d] err=%f[isNan=%d]", name.c_str(), val, isValNan, err,
215 isErrNaN);
216 return -2;
217 }
218
219 if (onlyPositive && val < 0) {
220 return -3;
221 }
222
223 if (times > 0 && times * std::abs(val) < err) {
224 if (verbose >= 0)
225 Printf("Warning: Skipping '%s' because 'times * val < err' ( %f * %f < %f ) ...", name.c_str(), times,
226 std::abs(val), err);
227 return -4;
228 }
229
230 if (normalizeToWidth) {
231 int nDimsCuts = 0;
232 for (auto & cut : cfg["ndmspc"]["cuts"]) {
233 if (cut["enabled"].is_boolean() && cut["enabled"].get<bool>() == false) continue;
234 nDimsCuts++;
235 }
236 // Printf("1 %f %f", val, err);
237 for (int iCut = 0; iCut < nDimsCuts; iCut++) {
238 double w = output->GetAxis(iCut + 1)->GetBinWidth(point[iCut + 1]);
239 val /= w;
240 err /= w;
241 // Printf("%d %f %f w=%f", iCut + 1, val, err, w);
242 }
243 }
244
245 int idx = output->GetAxis(0)->FindBin(name.c_str());
246 if (idx <= 0) {
247 return idx;
248 }
249 point[0] = idx;
250 Long64_t binId = output->GetBin(point);
251 output->SetBinContent(binId, val);
252 output->SetBinError(binId, err);
253
254 return idx;
255}
256std::vector<std::string> Utils::Tokenize(std::string_view input, const char delim)
257{
261
262 std::vector<std::string> out;
263
264 for (auto found = input.find(delim); found != std::string_view::npos; found = input.find(delim)) {
265 out.emplace_back(input, 0, found);
266 input.remove_prefix(found + 1);
267 }
268
269 if (not input.empty()) out.emplace_back(input);
270
271 return out;
272}
273
274} // namespace Ndmspc
Utils object.
Definition Utils.h:20
static std::string GetCutsPath(json cuts)
Definition Utils.cxx:143
static int SetResultValueError(json cfg, THnSparse *output, std::string name, Int_t *point, double val, double err, bool normalizeToWidth=false, bool onlyPositive=false, double times=1)
Definition Utils.cxx:197
static std::string OpenRawFile(std::string filename)
Definition Utils.cxx:57
static TFile * OpenFile(std::string filename, std::string mode="READ", bool createLocalDir=true)
Definition Utils.cxx:18
static Int_t GetBinFromBase(Int_t bin, Int_t rebin, Int_t rebin_start)
Definition Utils.cxx:175
static TMacro * OpenMacro(std::string filename)
Definition Utils.cxx:100
static std::vector< std::string > Tokenize(std::string_view input, const char delim)
Definition Utils.cxx:256
static bool SaveRawFile(std::string filename, std::string content)
Definition Utils.cxx:85