ATLAS Offline Software
TRTCalib_merge_Skeleton.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
2 
3 import sys, glob, subprocess, tarfile
4 
5 def nextstep(text):
6  print("\n"+"#"*100)
7  print("#")
8  print("# %s" % (text))
9  print("#")
10  print("#"*100,"\n")
11 
12 def tryError(command, error):
13  try:
14  print(" Running: %s\n" % (command))
15  stdout, stderr = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
16  print("OUTPUT: \n%s" % (stdout.decode('ascii')))
17  print("ERRORS: %s" % ("NONE" if stderr.decode('ascii')=='' else "\n"+stderr.decode('ascii')))
18  if stderr:
19  exit(1)
20  except OSError as e:
21  print(error,e)
22  sys.exit(e.errno)
23 
24 def fromRunArgs(runArgs):
25 
26 
27  nextstep("UNTAR files")
28 
29 
30  print("Uncompressing files:")
31  try:
32  for file in runArgs.inputTARFile:
33  print("\t-",file)
34  tarfile.open(file).extractall(".")
35  except OSError as e:
36  print("ERROR: Failed uncompressing TAR file\n",e)
37  sys.exit(e.errno)
38 
39 
40  nextstep("Generating the tracktuple and StrawStatus file")
41 
42 
43  command = 'TRTCalib_bhadd merged_histo.root %s' % ("".join(("%s " % str(file)) for file in glob.glob("*.basic.root") ))
44  tryError(command,"ERROR: Failed in process TRTCalib_bhadd\n")
45 
46 
47  nextstep("Renaming Binary output file")
48 
49 
50  command = 'mv -v merged_histo.root.part0 %s.basic.root' % (runArgs.outputTAR_MERGEDFile)
51  tryError(command,"ERROR: Renaming binary file \"merged_histo.root.part0\"\n")
52 
53 
54  nextstep("Merging *.tracktuple.root files")
55 
56 
57  command = 'hadd -f %s.tracktuple.root %s' % (runArgs.outputTAR_MERGEDFile, "".join(("%s " % str(file)) for file in glob.glob("*.tracktuple.root") ))
58  tryError(command,"ERROR: Failed in process merging *.tracktuple.root files\n")
59 
60 
61  nextstep("Merging *.straw.txt files")
62 
63 
64  command = 'TRTCalib_StrawStatus_merge %s.merged.straw.txt %s' % (runArgs.outputTAR_MERGEDFile, "".join(("%s " % str(file)) for file in glob.glob("*.straw.txt") ))
65  tryError(command,"ERROR: Failed in process merging *.straw.txt files\n")
66 
67 
68  nextstep("TAR'ing files")
69 
70 
71  try:
72  # Getting list of files to be compressed
73  files_list=glob.glob(runArgs.outputTAR_MERGEDFile+".*")
74  # Compressing
75  tar = tarfile.open(runArgs.outputTAR_MERGEDFile, "w:gz")
76  print("\nCompressing files in %s output file:" % runArgs.outputTAR_MERGEDFile)
77  for file in files_list:
78  print("\t-",file)
79  tar.add(file)
80  tar.close()
81  except OSError as e:
82  print("ERROR: Failed compressing the output files\n",e)
83  sys.exit(e.errno)
84 
85 
86  # Prints all types of txt files present in a Path
87  print("\nListing files:")
88  for file in sorted(glob.glob("./*", recursive=True)):
89  print("\t-",file)
TRTCalib_merge_Skeleton.fromRunArgs
def fromRunArgs(runArgs)
Definition: TRTCalib_merge_Skeleton.py:24
TRTCalib_merge_Skeleton.tryError
def tryError(command, error)
Definition: TRTCalib_merge_Skeleton.py:12
calibdata.exit
exit
Definition: calibdata.py:236
DerivationFramework::TriggerMatchingUtils::sorted
std::vector< typename T::value_type > sorted(T begin, T end)
Helper function to create a sorted vector from an unsorted one.
TCS::join
std::string join(const std::vector< std::string > &v, const char c=',')
Definition: Trigger/TrigT1/L1Topo/L1TopoCommon/Root/StringUtils.cxx:10
str
Definition: BTagTrackIpAccessor.cxx:11
dbg::print
void print(std::FILE *stream, std::format_string< Args... > fmt, Args &&... args)
Definition: SGImplSvc.cxx:70
TRTCalib_merge_Skeleton.nextstep
def nextstep(text)
Definition: TRTCalib_merge_Skeleton.py:5