|
| 1 | +# pylint: disable=missing-function-docstring |
| 2 | +""" |
| 3 | +""" |
| 4 | + |
| 5 | +import argparse |
| 6 | +import math |
| 7 | +from array import array |
| 8 | + |
| 9 | +from ROOT import ( # pylint: disable=import-error,no-name-in-module |
| 10 | + gROOT, |
| 11 | + TFile, |
| 12 | + TH1F |
| 13 | +) |
| 14 | + |
| 15 | + |
| 16 | +def main(): |
| 17 | + """ |
| 18 | + Main function. |
| 19 | + """ |
| 20 | + gROOT.SetBatch(True) |
| 21 | + |
| 22 | + parser = argparse.ArgumentParser(description="Arguments to pass") |
| 23 | + parser.add_argument("filename", help="input file with histogram") |
| 24 | + parser.add_argument("histname", help="histogram name pattern") |
| 25 | + parser.add_argument("outname", help="output file for the new histogram") |
| 26 | + parser.add_argument("maxval", type=float, help="maxval in histogram") |
| 27 | + args = parser.parse_args() |
| 28 | + |
| 29 | + with TFile(args.filename) as fin, TFile(args.outname, "recreate") as fout: |
| 30 | + objnames = fin.GetListOfKeys() |
| 31 | + print(f"objnames : {objnames}") |
| 32 | + histnames = [key.GetName() for key in fin.GetListOfKeys() if args.histname in key.GetName()] |
| 33 | + print(f"histnames: {histnames}") |
| 34 | + for histname in histnames: |
| 35 | + hist = fin.Get(histname) |
| 36 | + hist.SetDirectory(0) |
| 37 | + last_bin = hist.GetXaxis().FindBin(args.maxval) |
| 38 | + bins = [] |
| 39 | + for binn in range(1, last_bin + 1): |
| 40 | + bins.append(hist.GetBinLowEdge(binn)) |
| 41 | + print(f"Hist bins {bins}") |
| 42 | + hist2 = TH1F(histname, "", len(bins) - 1, array('d', bins)) |
| 43 | + for binn in range(1, last_bin + 1): |
| 44 | + hist2.SetBinContent(binn + 1, hist.GetBinContent(binn + 1)) |
| 45 | + hist2.SetBinError(binn + 1, hist.GetBinError(binn + 1)) |
| 46 | + #print(f"Setting bin {binn + 1} low edge {hist2.GetBinLowEdge(binn + 1)} up edge {hist2.GetXaxis().GetBinUpEdge(binn + 1)} content to content from bin {binn + 1}: {hist2.GetBinContent(binn + 1)}") |
| 47 | + hist2.SetMarkerSize(hist.GetMarkerSize()) |
| 48 | + hist2.SetMarkerColor(hist.GetMarkerColor()) |
| 49 | + hist2.SetMarkerStyle(hist.GetMarkerStyle()) |
| 50 | + hist2.SetLineWidth(hist.GetLineWidth()) |
| 51 | + hist2.SetLineColor(hist.GetLineColor()) |
| 52 | + hist2.SetLineStyle(hist.GetLineStyle()) |
| 53 | + fout.cd() |
| 54 | + hist2.Write() |
| 55 | + |
| 56 | + |
| 57 | +if __name__ == "__main__": |
| 58 | + main() |
0 commit comments