387 def __call__(self, root_file):
388 ROOT.TH1.AddDirectory(False)
389 hist = find_histo(root_file, self.input_hist)
390 if not hist:
391 if crash_on_error:
392 raise RuntimeError(f"Histogram '{self.input_hist}' not found.")
393 else:
394 logging.error(f"Histogram '{self.input_hist}' not found.")
395 return
396
397 path_parts = self.new_path.
strip(
"/").
split(
"/")
398 *dir_path, new_name = path_parts
399 directory_path = "/".join(dir_path)
400 ensure_directory_exists(root_file, directory_path)
401 root_file.cd(directory_path)
402
403 if self.xlow is None or self.xhigh is None:
404 h_out = hist.Clone(new_name)
405 else:
406 ax = hist.GetXaxis()
407 bin_low =
max(1,
min(ax.FindBin(self.xlow), ax.GetNbins()))
408 bin_high =
max(1,
min(ax.FindBin(self.xhigh), ax.GetNbins()))
409 nbins = bin_high - bin_low
410 h_out = ROOT.TH1D(new_name, hist.GetTitle(), nbins + 1, float(self.xlow), float(self.xhigh))
411 for i in range(nbins + 1):
412 h_out.SetBinContent(i + 1, hist.GetBinContent(i + bin_low))
413
414 if self.x_label:
415 h_out.GetXaxis().SetTitle(self.x_label)
416 if self.y_label:
417 h_out.GetYaxis().SetTitle(self.y_label)
418 h_out.Write(new_name, ROOT.TObject.kOverwrite)
419 logging.info(f"Renamed and updated histogram '{self.input_hist}' → '{self.new_path}' in '{directory_path}' | X-axis: '{self.x_label}' | Y-axis: '{self.y_label}' | Range: ({self.xlow}, {self.xhigh})")
420
std::vector< std::string > split(const std::string &s, const std::string &t=":")