18TFile *
Utils::OpenFile(std::string filename, std::string mode,
bool createLocalDir)
24 filename = gSystem->ExpandPathName(filename.c_str());
27 if (!mode.compare(
"RECREATE") || !mode.compare(
"UPDATE") || !mode.compare(
"WRITE")) {
29 TString filenameT(filename.c_str());
30 bool isLocalFile = filenameT.BeginsWith(
"file://");
33 filenameT.ReplaceAll(
"file://",
"");
36 isLocalFile = !filenameT.Contains(
"://");
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());
48 std::string filenameLocal = gSystem->GetDirName(url.GetFile()).Data();
50 gSystem->mkdir(filenameLocal.c_str(), kTRUE);
54 return TFile::Open(filename.c_str(), mode.c_str());
57std::string Utils::OpenRawFile(std::string filename)
60 TFile * f = TFile::Open(TString::Format(
"%s?filetype=raw", filename.c_str()));
66 char buff[buffsize + 1];
68 Long64_t buffread = 0;
69 while (buffread < f->GetSize()) {
70 if (buffread + buffsize > f->GetSize()) buffsize = f->GetSize() - buffread;
73 f->ReadBuffer(buff, buffread, buffsize);
74 buff[buffsize] =
'\0';
82TMacro * Utils::OpenMacro(std::string filename)
85 std::string content = OpenRawFile(filename);
86 if (content.empty()) {
88 Printf(
"Error: Problem opening macro '%s' ...", filename.c_str());
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());
122std::string Utils::GetCutsPath(json cuts)
124 std::string path =
"";
125 std::string rebinStr =
"";
126 for (
auto & cut : cuts) {
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>();
134 if (rebin_start > 1) {
135 rebin_minimum = (rebin_start % rebin);
136 if (rebin_minimum == 0) rebin_minimum = 1;
138 path += cut[
"axis"].get<std::string>() +
"_";
140 rebinStr += std::to_string(rebin) +
"-" + std::to_string(rebin_minimum) +
"_";
142 path[path.size() - 1] =
'/';
143 rebinStr[rebinStr.size() - 1] =
'/';
148 return std::move(path);
150Int_t Utils::GetBinFromBase(Int_t bin, Int_t rebin, Int_t rebin_start)
152 if (rebin == 1)
return bin;
155 return (bin / rebin) + 1;
168int Utils::SetResultValueError(json cfg, THnSparse * output, std::string name, Int_t * point,
double val,
double err,
169 bool normalizeToWidth,
bool onlyPositive,
double times)
173 if (!cfg[
"user"][
"verbose"].is_null() && cfg[
"user"][
"verbose"].is_number_integer()) {
174 verbose = cfg[
"user"][
"verbose"].get<
int>();
177 bool isValNan = TMath::IsNaN(val);
178 bool isErrNaN = TMath::IsNaN(err);
180 if (isValNan || isErrNaN) {
182 Printf(
"Error: SetResultValueError %s val=%f[isNaN=%d] err=%f[isNan=%d]", name.c_str(), val, isValNan, err,
187 if (onlyPositive && val < 0) {
191 if (times > 0 && times * std::abs(val) < err) {
193 Printf(
"Warning: Skipping '%s' because 'times * val < err' ( %f * %f < %f ) ...", name.c_str(), times,
198 if (normalizeToWidth) {
200 for (
auto & cut : cfg[
"ndmspc"][
"cuts"]) {
201 if (cut[
"enabled"].is_boolean() && cut[
"enabled"].get<bool>() ==
false)
continue;
205 for (
int iCut = 0; iCut < nDimsCuts; iCut++) {
206 double w = output->GetAxis(iCut + 1)->GetBinWidth(point[iCut + 1]);
213 int idx = output->GetAxis(0)->FindBin(name.c_str());
218 Long64_t binId = output->GetBin(point);
219 output->SetBinContent(binId, val);
220 output->SetBinError(binId, err);
224std::vector<std::string> Utils::Tokenize(std::string_view input,
const char delim)
226 std::vector<std::string> out;
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);
233 if (not input.empty()) out.emplace_back(input);
static TFile * OpenFile(std::string filename, std::string mode="READ", bool createLocalDir=true)