210def compactify(charm: bool | str, runMin=0, runMax=0, checkOnly=False) -> None:
211 globalPath = "/afs/cern.ch/project/lbcern/vol2/truf/muonBackground"
212 ecut = "10.0"
213 if charm:
214 allDirs = os.listdir(globalPath + "/charm")
215 allFiles = []
216 for r in range(int(runMin), int(runMax) + 1):
217
218 nr = "9000" + str(r)
219 subruns = []
220 badFiles = []
221 for x in allDirs:
222 for k in range(20):
223 sr = f"{k:0>2}"
224 if not x.find(nr + sr) < 0:
225 if not x.find("log") < 0:
226 continue
227 fn = "pythia8_Geant4_" + nr + sr + "_" + ecut + ".root"
228 file_path = globalPath + "/charm/" + x + "/" + fn
229 if os.path.exists(file_path):
230 try:
231 t = ROOT.TFile.Open(file_path)
232 if not t:
233 badFiles.append(file_path)
234 continue
235 if t.ReadKeys() == 0:
236 badFiles.append(file_path)
237 continue
238 if t.FindObjectAny("cbmsim"):
239 subruns.append(file_path)
240 except Exception:
241 badFiles.append(file_path)
242 continue
243 ldir = " "
244 for x in subruns:
245 ldir += x + " "
246 outputr = "pythia8_Geant4_charm_" + nr + "_" + ecut + ".root"
247 allFiles += outputr + " "
248 os.system("hadd " + outputr + " " + ldir)
249 output = "pythia8_Geant4_charm_" + str(runMin) + "_" + str(runMax) + "_" + ecut + ".root"
250 os.system("hadd " + output + " " + allFiles)
251 makeHistos(output)
252 else:
253 output = "pythia8_Geant4_" + str(runMin) + "-" + str(runMax) + "_" + ecut + ".root"
254 if not checkOnly:
255 ldir = ""
256 badFiles = []
257 for d in os.listdir(globalPath):
258 if d.find("run_fixedTarget") < 0:
259 continue
260 srun = d.split("run_fixedTarget_")[1]
261 run = int(srun)
262 if not run > runMax and not run < runMin:
263 f = globalPath + "/" + d + "/pythia8_Geant4_" + srun + "_" + ecut + ".root "
264 ftmp = f.replace(".root ", ".roottmp")
265 if os.path.exists(ftmp):
266 f = ftmp + " "
267 try:
268 t = ROOT.TFile.Open(f)
269 if not t:
270 badFiles.append(d)
271 continue
272 if t.ReadKeys() == 0:
273 badFiles.append(d)
274 continue
275 if t.FindObjectAny("cbmsim"):
276 ldir += f
277 except Exception:
278 badFiles.append(d)
279 continue
280 os.system("hadd " + output + " " + ldir)
281 makeHistos(output)
282
283