ATLAS Offline Software
Loading...
Searching...
No Matches
TRTCalib_calib_Skeleton.py
Go to the documentation of this file.
1# Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
2
3import sys, tarfile, glob, random, subprocess
4
5from PyJobTransforms.CommonRunArgsToFlags import commonRunArgsToFlags
6from PyJobTransforms.TransformUtils import processPreExec, processPreInclude, processPostExec, processPostInclude
7from TRT_CalibAlgs.TRTCalibrationMgrConfig import CalibConfig, TRT_CalibrationMgrCfg, TRT_StrawStatusCfg
8from AthenaConfiguration.MainServicesConfig import MainServicesCfg
9
10def nextstep(text):
11 print("\n"+"#"*100)
12 print("#")
13 print("# %s" % (text))
14 print("#")
15 print("#"*100,"\n")
16
17def 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
29def fromRunArgs(runArgs):
30
31 outputFile = runArgs.outputTAR_CALIBFile
32
33
34 nextstep("UNTAR input file")
35
36
37 print("Uncompressing files:")
38 try:
39 for file in runArgs.inputTARFile:
40 print("\t-",file)
41 tarfile.open(file).extractall(".")
42 except OSError as e:
43 print("ERROR: Failed uncompressing TAR file\n",e)
44 sys.exit(e.errno)
45
46
47 nextstep("Renaming *straw.txt and *tracktuple.root files for the final output")
48
49
50 listOfStrawFiles = glob.glob("*.merged.straw.txt")
51 listOfTrackFiles = glob.glob("*.tracktuple.root")
52
53 if not listOfStrawFiles or (not listOfTrackFiles and runArgs.piecetoken != "all"):
54 print("ERROR: Empty array of \"*.merged.straw.txt\" (size %d) or \"*.tracktuple.root\" (size %d) files" % (len(listOfStrawFiles), len(listOfTrackFiles)))
55 exit(1)
56
57 command = "mv -v %s %s.merged.straw.txt; " % (listOfStrawFiles[0],outputFile)
58
59 if(runArgs.piecetoken == "all"):
60 print("INFO: piecetoken equal to \"all\". Saving *tracktuple.root file")
61 command += "mv -v %s %s.tracktuple.root; " % (listOfTrackFiles[0],outputFile)
62 else:
63 print("INFO: piecetoken different than \"all\". Not need to rename *tracktuple.root file")
64
65 tryError(command, "Renaming *straw.txt and *tracktuple.root files\n")
66
67
68 nextstep("Calculating constants ATHENA")
69
70
71 myFile = []
72 # generating the RAW file path
73 if runArgs.rawfile:
74 myFile.append(runArgs.rawfile)
75
76 else:
77 if not runArgs.project and not runArgs.runnr and not runArgs.stream:
78 try:
79 splitInput = runArgs.inputTARFile[0].split('.')
80 project="----"
81 runnr ="----"
82 stream ="----"
83 if splitInput:
84 runArgs.project = splitInput[0]
85 runArgs.runnr = splitInput[1]
86 runArgs.stream = splitInput[2]
87 print("INFO: No input arguments are given to name the RAW file. Obtained from \"lsn\"...")
88 else:
89 print("ERROR: not able to find project=\"%s\" or runNumber=\"%s\" or stream=\"%s\" from \"lsn\" " % (project, runnr, stream))
90 exit(1)
91
92 except OSError as e:
93 print("ERROR: Failed trying to build RAW file name for Athena.\n",e)
94 sys.exit(e.errno)
95
96 if (not runArgs.project or not runArgs.runnr or not runArgs.stream):
97 print("ERROR: Raw file not provided, project=\"%s\" or runNumber=\"%s\" or stream=\"%s\" missing..." % (runArgs.project, runArgs.runnr, runArgs.stream))
98 print("Provide them!")
99 sys.exit(1)
100 else:
101
102 # the arquitecture behind the path is the one below. The star "*" is different depending on the stream rates
103 # for express_express the ending should be "merge.RAW/"
104 # for other streams could be "daq.RAW/"
105 rawpath = "/eos/atlas/atlastier0/rucio/%s/%s/%s/%s.%s.%s.%s" % (runArgs.project,runArgs.stream,runArgs.runnr.zfill(8),runArgs.project,runArgs.runnr.zfill(8),runArgs.stream,runArgs.step)
106 if len(glob.glob(rawpath)) == 0:
107 print("ERROR: Any folder under \"%s\". Please check that the path is correct" % rawpath)
108 sys.exit(1)
109 elif len(glob.glob(rawpath)) > 1:
110 print("ERROR: More than one folder under '%s'. Please specify the correct one with \"step\" argument in the transform" % rawpath)
111 for f in glob.glob(rawpath):
112 print(f"\t- {f}")
113 sys.exit(1)
114
115 globedFiles = glob.glob(rawpath+"*")
116 if not globedFiles:
117 print("ERROR: Not able to find any file under %s. Please check that the files exists" % (rawpath))
118 sys.exit(1)
119
120 myFile.append(random.choice(globedFiles))
121 print("RAW file selected for testing:",myFile)
122 if not myFile:
123 print("ERROR: provide a valid project=\"%s\" or runNumber=\"%s\" or stream=\"%s\"" % (runArgs.project, runArgs.runnr, runArgs.stream))
124 sys.exit(1)
125
126 from AthenaConfiguration.AllConfigFlags import initConfigFlags
127 flags=initConfigFlags()
128
129 commonRunArgsToFlags(runArgs, flags)
130
131 processPreInclude(runArgs, flags)
132 processPreExec(runArgs, flags)
133
134 # Change this to a the hardcoded data file
135 flags.Input.Files=myFile
136 flags.Output.HISTFileName="DontUse.remove"
137
138 # Only one event must be run
139 flags.Exec.MaxEvents=1
140
141 # Importing flags - Switching off detector parts and monitoring
142 CalibConfig(flags)
143
144 # To respect --athenaopts
145 flags.fillFromArgs()
146
147 # Reason why we need to clone and replace: https://gitlab.cern.ch/atlas/athena/-/merge_requests/68616#note_7614858
148 flags = flags.cloneAndReplace(
149 "Tracking.ActiveConfig",
150 f"Tracking.{flags.Tracking.PrimaryPassConfig.value}Pass",
151 # Keep original flags as some of the subsequent passes use
152 # lambda functions relying on them
153 keepOriginal=True)
154
155 flags.lock()
156 flags.dump()
157
158 cfg=MainServicesCfg(flags)
159
160 from ByteStreamCnvSvc.ByteStreamConfig import ByteStreamReadCfg
161 cfg.merge(ByteStreamReadCfg(flags))
162
163 from InDetConfig.TrackRecoConfig import InDetTrackRecoCfg
164 cfg.merge(InDetTrackRecoCfg(flags))
165
166 cfg.merge(TRT_CalibrationMgrCfg(flags,DoCalibrate=True, Hittuple=glob.glob("*.basic.root")[0], caltag=runArgs.piecetoken))
167 cfg.merge(TRT_StrawStatusCfg(flags))
168
169 processPostInclude(runArgs, flags, cfg)
170 processPostExec(runArgs, flags, cfg)
171
172 with open("ConfigTRTCalib_calib.pkl", "wb") as f:
173 cfg.store(f)
174
175 sc = cfg.run()
176 if not sc.isSuccess():
177 sys.exit(not sc.isSuccess())
178
179
180 nextstep("Renaming outputs from Calibrator")
181
182
183 command = "mv -v calibout.root %s.calibout.root ; " % (outputFile)
184 command += "mv -v calibout_rt.txt %s.calibout_rt.txt; " % (outputFile)
185 command += "mv -v calibout_t0.txt %s.calibout_t0.txt; " % (outputFile)
186 command += "mv -v calib_constants_out.txt %s.calib_constants_out.txt; " % (outputFile)
187
188 tryError(command, "ERROR: Failed renaming files in TRT calib step\n")
189
190
191 nextstep("TAR'ing files")
192
193 try:
194 # Getting list of files to be compressed
195 files_list=glob.glob(outputFile+".*")
196 # Compressing
197 tar = tarfile.open(outputFile, "w:gz")
198 print("Compressing files in %s output file:" % outputFile)
199 for file in files_list:
200 print("\t-",file)
201 tar.add(file)
202 tar.close()
203 except OSError as e:
204 print("ERROR: Failed compressing the output files\n",e)
205 sys.exit(e.errno)
206
207 # Prints all types of txt files present in a Path
208 print("\nListing files:")
209 for file in sorted(glob.glob("./*", recursive=True)):
210 print("\t-",file)
if(febId1==febId2)
void print(char *figname, TCanvas *c1)
std::vector< std::string > split(const std::string &s, const std::string &t=":")
Definition hcg.cxx:177