7from collections
import Counter
11_error_log: Counter[str] = Counter()
17 if fname[0:4] ==
"/eos":
18 eospath = ROOT.gSystem.Getenv(
"EOSSHIP") + fname
19 f = ROOT.TFile.Open(eospath)
22 for akey
in f.GetListOfKeys():
28 if len(wanted) > 0
and hname
not in wanted:
31 cln = obj.Class().GetName()
33 h[hname] = obj.Clone()
37 rc = h[hname].Add(obj)
39 print(
"Error when adding histogram ", hname)
41 h[hname] = obj.Clone()
42 if h[hname].GetSumw2N() == 0:
44 h[hname].SetDirectory(ROOT.gROOT)
45 if cln
in {
"TH2D",
"TH2F"}:
46 for p
in [
"_projx",
"_projy"]:
47 if isinstance(hname, str):
50 projname = str(hname) + p
52 h[projname] = h[hname].ProjectionX()
54 h[projname] = h[hname].ProjectionY()
55 h[projname].SetName(name + p)
56 h[projname].SetDirectory(ROOT.gROOT)
82 h[key] = ROOT.TH3D(rkey, title, nbinsx, xmin, xmax, nbinsy, ymin, ymax, nbinsz, zmin, zmax)
84 h[key] = ROOT.TH2D(rkey, title, nbinsx, xmin, xmax, nbinsy, ymin, ymax)
86 h[key] = ROOT.TH1D(rkey, title, nbinsx, xmin, xmax)
87 h[key].SetDirectory(ROOT.gROOT)
97 ymin: float |
None =
None,
98 ymax: float |
None =
None,
107 elif ymin
is None or ymax
is None:
108 h[key] = ROOT.TProfile(rkey, title, nbinsx, xmin, xmax, option)
110 h[key] = ROOT.TProfile(rkey, title, nbinsx, xmin, xmax, ymin, ymax, option)
111 h[key].SetDirectory(ROOT.gROOT)
115 f = ROOT.TFile(fname,
"RECREATE")
117 if not hasattr(h[akey],
"Class"):
119 cln = h[akey].Class().GetName()
120 if "TH" in cln
or "TP" in cln:
122 if plusCanvas
and "TC" in cln:
127def bookCanvas(h, key=None, title: str =
"", nx: int = 900, ny: int = 600, cx: int = 1, cy: int = 1) ->
None:
132 h[key] = ROOT.TCanvas(key, title, nx, ny)
133 h[key].Divide(cx, cy)
137 """Copy obj into slot `index` of a TClonesArray, expanding it as needed.
139 Item assignment (arr[i] = obj) raises in ROOT >= 6.32; construct the slot
140 and copy-assign into it instead. TVectorD slots are resized to match obj
141 first (TVectorD::operator= requires matching sizes).
143 if arr.GetSize() <= index:
144 arr.Expand(index + 10)
145 slot = arr.ConstructedAt(index)
146 if hasattr(slot,
"ResizeTo"):
157 print(
"Summary of recorded incidents:")
159 print(e,
":", _error_log[e])
163 if isinstance(x, str):
167 if isinstance(tx, (list, tuple)):
171 if _f[0:4] ==
"/eos":
172 f = ROOT.gSystem.Getenv(
"EOSSHIP") + _f
175 test = ROOT.TFile.Open(f)
177 print(
"ERROR FileCheck: input file", f,
" does not exist. Missing authentication?")
179 thisType =
"tree" if test.FindObjectAny(
"cbmsim")
else "ntuple"
183 elif fileType != thisType:
184 print(
"ERROR FileCheck: Supplied list of files not all of tree or ntuple type")
188 print(
"ERROR FileCheck: File must be either a string or list of files")
193 if isinstance(inFile, str):
197 if isinstance(fileList, (list, tuple)):
200 with ROOT.TFile.Open(_f)
as test:
202 raise FileNotFoundError(f
"ERROR FileCheck: input file {_f} does not exist. Missing authentication?")
203 current_has_branch = bool(test.FindObjectAny(
"cbmsim")
and test[
"cbmsim"].FindBranch(branchName))
205 hasIt = current_has_branch
206 elif current_has_branch != hasIt:
208 f
"ERROR CheckForBranch: Mixed input files (some have branch {branchName} and some do not)."
212 print(
"ERROR CheckForBranch: File must be either a string or list of files")
217 """Check the FileSummary that run_fixedTarget.py writes for the hadron absorber field.
219 Returns the files without a FileSummary, and the files simulated with a hadron
220 absorber but without its field.
222 fileList = [inFile]
if isinstance(inFile, str)
else list(inFile)
223 noSummary, noField = [], []
225 with ROOT.TFile.Open(_f)
as f:
226 summary = f.Get(
"FileSummary")
230 fsr = json.loads(str(summary))
231 hasAbsorber = fsr.get(
"AddMuonShield")
or fsr.get(
"AddHadronAbsorberOnly")
232 if hasAbsorber
and not fsr.get(
"AddMuonShieldField"):
234 return noSummary, noField
238 """Return True if TDatabasePDG knows this PDG code or particle name."""
240 return bool(ROOT.TDatabasePDG.Instance().GetParticle(code))
244 """Return the TDatabasePDG entry for a PDG code or particle name; raise ValueError if it is unknown."""
245 particle = ROOT.TDatabasePDG.Instance().GetParticle(code)
247 raise ValueError(f
"Unknown particle: {code}")
bool is_known_particle(int|str code)
None writeHists(h, fname, bool plusCanvas=False)
tuple[list[str], list[str]] stage1AbsorberFieldOff(inFile)
None readHists(h, fname, wanted=None)
None bookHist(h, key=None, str title="", int nbinsx=100, float xmin=0, float xmax=1, int nbinsy=0, float ymin=0, float ymax=1, int nbinsz=0, float zmin=0, float zmax=1)
ROOT.TParticlePDG get_particle(int|str code)
bool checkForBranch(inFile, branchName)
None bookProf(h, key=None, str title="", int nbinsx=100, float xmin=0, float xmax=1, float|None ymin=None, float|None ymax=None, str option="")
None assignClonesArrayItem(arr, index, obj)
None bookCanvas(h, key=None, str title="", int nx=900, int ny=600, int cx=1, int cy=1)