ATLAS Offline Software
TRTCalib_accu_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, os, glob, subprocess, tarfile
4 from PyJobTransforms.CommonRunArgsToFlags import commonRunArgsToFlags
5 from PyJobTransforms.TransformUtils import processPreExec, processPreInclude, processPostExec, processPostInclude
6 from TRT_CalibAlgs.TRTCalibrationMgrConfig import CalibConfig, TRT_CalibrationMgrCfg, TRT_StrawStatusCfg
7 from AthenaConfiguration.MainServicesConfig import MainServicesCfg
8 
9 
10 def nextstep(text):
11  print("\n"+"#"*100)
12  print("#")
13  print("# %s" % (text))
14  print("#")
15  print("#"*100,"\n")
16 
17 def tryError(command, error):
18  try:
19  print(" Running: %s\n" % (command))
20  stdout, stderr = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
21  print("OUTPUT: \n%s" % (stdout.decode('ascii')))
22  print("ERRORS: %s" % ("NONE" if stderr.decode('ascii')=='' else "\n"+stderr.decode('ascii')))
23  if stderr:
24  exit(1)
25  except OSError as e:
26  print(error,e)
27  sys.exit(e.errno)
28 
29 def fromRunArgs(runArgs):
30 
31 
32  nextstep("Building tracks: ATHENA")
33 
34 
35  from AthenaConfiguration.AllConfigFlags import initConfigFlags
36  flags=initConfigFlags()
37 
38  commonRunArgsToFlags(runArgs, flags)
39 
40  processPreInclude(runArgs, flags)
41  processPreExec(runArgs, flags)
42 
43  if "TRTCalibAccu_ART_Output" in runArgs.outputTARFile:
44  print("WARNING - Testing the TRTCalib_accu transform")
45  else:
46  print("INFO - Running TRTCalib_accu transform")
47 
48  flags.Input.Files=runArgs.inputRAWFile
49  flags.Output.HISTFileName=runArgs.outputTARFile
50 
51  #Importing flags - Switching off detector parts and monitoring
52  CalibConfig(flags)
53 
54  # To respect --athenaopts
55  flags.fillFromArgs()
56 
57  # Reason why we need to clone and replace: https://gitlab.cern.ch/atlas/athena/-/merge_requests/68616#note_7614858
58  flags = flags.cloneAndReplace(
59  "Tracking.ActiveConfig",
60  f"Tracking.{flags.Tracking.PrimaryPassConfig.value}Pass",
61  # Keep original flags as some of the subsequent passes use
62  # lambda functions relying on them
63  keepOriginal=True)
64 
65  flags.lock()
66  flags.dump()
67 
68  cfg=MainServicesCfg(flags)
69 
70  from ByteStreamCnvSvc.ByteStreamConfig import ByteStreamReadCfg
71  cfg.merge(ByteStreamReadCfg(flags))
72 
73  from InDetConfig.TrackRecoConfig import InDetTrackRecoCfg
74  cfg.merge(InDetTrackRecoCfg(flags))
75 
76  cfg.merge(TRT_CalibrationMgrCfg(flags,calibconstants=runArgs.calibconstants))
77  cfg.merge(TRT_StrawStatusCfg(flags))
78 
79  processPostInclude(runArgs, flags, cfg)
80  processPostExec(runArgs, flags, cfg)
81 
82  with open("ConfigTRTCalib_accu.pkl", "wb") as f:
83  cfg.store(f)
84 
85  sc = cfg.run()
86 
87  if not sc.isSuccess():
88  sys.exit(not sc.isSuccess())
89 
90  outputFile = runArgs.outputTARFile
91 
92  nextstep("Generating the tracktuple and StrawStatus file")
93 
94 
95  os.rename(outputFile, outputFile+'.basic.root')
96  command = 'TRTCalib_bhadd dumfile %s.basic.root' % (outputFile)
97 
98  tryError(command,"ERROR: Failed in process TRTCalib_bhadd\n")
99 
100 
101  nextstep("Renaming outputs from Athena and TRTCalib_bhadd")
102 
103 
104  command = "mv -v tracktuple.root %s.tracktuple.root ; " % (outputFile)
105  command += "mv -v %s %s.straw.txt" % (glob.glob('TRT_StrawStatusOutput.*newFormat.txt')[0],outputFile)
106 
107  tryError(command,"ERROR: Failed in renaming\n")
108 
109 
110  nextstep("TAR'ing files")
111 
112 
113  try:
114  # Getting list of files to be compressed
115  files_list=glob.glob(outputFile+".*")
116  # Compressing
117  tar = tarfile.open(outputFile, "w:gz")
118  print("Compressing files in %s output file:" % outputFile)
119  for file in files_list:
120  print("\t-",file)
121  tar.add(file)
122  tar.close()
123  except OSError as e:
124  print("ERROR: Failed compressing the output files\n",e)
125  sys.exit(e.errno)
126 
127  # Prints all types of txt files present in a Path
128  print("\nListing files:")
129  for file in sorted(glob.glob("./*", recursive=True)):
130  print("\t-",file)
python.TransformUtils.processPreExec
def processPreExec(runArgs, flags)
Definition: Tools/PyJobTransforms/python/TransformUtils.py:41
python.TransformUtils.processPostExec
def processPostExec(runArgs, flags, cfg)
Definition: Tools/PyJobTransforms/python/TransformUtils.py:50
TRTCalib_accu_Skeleton.tryError
def tryError(command, error)
Definition: TRTCalib_accu_Skeleton.py:17
python.TransformUtils.processPostInclude
def processPostInclude(runArgs, flags, cfg)
Definition: Tools/PyJobTransforms/python/TransformUtils.py:69
python.ByteStreamConfig.ByteStreamReadCfg
def ByteStreamReadCfg(flags, type_names=None)
Definition: Event/ByteStreamCnvSvc/python/ByteStreamConfig.py:25
python.TransformUtils.processPreInclude
def processPreInclude(runArgs, flags)
Definition: Tools/PyJobTransforms/python/TransformUtils.py:62
TRTCalib_accu_Skeleton.nextstep
def nextstep(text)
Definition: TRTCalib_accu_Skeleton.py:10
TRTCalibrationMgrConfig.TRT_StrawStatusCfg
def TRT_StrawStatusCfg(flags, name='InDet_TRT_StrawStatus', **kwargs)
Definition: TRTCalibrationMgrConfig.py:57
python.MainServicesConfig.MainServicesCfg
def MainServicesCfg(flags, LoopMgr='AthenaEventLoopMgr')
Definition: MainServicesConfig.py:260
python.CommonRunArgsToFlags.commonRunArgsToFlags
def commonRunArgsToFlags(runArgs, configFlags)
Definition: CommonRunArgsToFlags.py:12
calibdata.exit
exit
Definition: calibdata.py:236
TRTCalib_accu_Skeleton.fromRunArgs
def fromRunArgs(runArgs)
Definition: TRTCalib_accu_Skeleton.py:29
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.
TRTCalibrationMgrConfig.TRT_CalibrationMgrCfg
def TRT_CalibrationMgrCfg(flags, name='TRT_CalibrationMgr', calibconstants='', Hittuple='', caltag='', **kwargs)
Definition: TRTCalibrationMgrConfig.py:9
python.TrackRecoConfig.InDetTrackRecoCfg
def InDetTrackRecoCfg(flags)
Main ID tracking config #####################.
Definition: TrackRecoConfig.py:791
TRTCalibrationMgrConfig.CalibConfig
def CalibConfig(flags)
Definition: TRTCalibrationMgrConfig.py:74
Trk::open
@ open
Definition: BinningType.h:40
python.AllConfigFlags.initConfigFlags
def initConfigFlags()
Definition: AllConfigFlags.py:19
dbg::print
void print(std::FILE *stream, std::format_string< Args... > fmt, Args &&... args)
Definition: SGImplSvc.cxx:70