ATLAS Offline Software
Loading...
Searching...
No Matches
RunTileTBRec.py
Go to the documentation of this file.
1#!/usr/bin/env python
2#
3# Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
4#
5'''
6@file RunTileTBRec.py
7@brief Script to run Tile TestBeam Reconstruction/Monitoring
8'''
9
10from AthenaConfiguration.Enums import BeamType
11from AthenaConfiguration.AutoConfigFlags import GetFileMD
12from TileConfiguration.TileConfigFlags import TileRunType
13from TileRecEx import TileInputFiles
14from AthenaCommon.SystemOfUnits import GeV
15
16import sys
17
18epiLog = """
19Examples:
20
21 RunTileTBRec.py --run RUNNUMBER --evtMax 1
22 RunTileTBRec.py --filesInput=FILE1,FILE2 Exec.SkipEvents=100
23
24At least one should provide the following arguments or Athena configuration flags (flags have higher priority):
25 Input file(s), e.g.: --run RUNNUMBER | --filesInput=FILE1,FILE2 | Input.Files="['FILE1','FILE2']"
26"""
27
28if __name__ == '__main__':
29
30 # Setup logs
31 from AthenaCommon.Logging import logging
32 log = logging.getLogger('RunTileTBRec')
33 from AthenaCommon.Constants import INFO
34 log.setLevel(INFO)
35
36 # Set the Athena configuration flags
37 from AthenaConfiguration.AllConfigFlags import initConfigFlags
38
39 import argparse
40
41 flags = initConfigFlags()
42 parserParents = [flags.getArgumentParser(), TileInputFiles.getArgumentParser(add_help=False)]
43 parser = argparse.ArgumentParser(parents=parserParents, add_help=False, fromfile_prefix_chars='@', epilog=epiLog, formatter_class=argparse.RawTextHelpFormatter)
44
45 parser.add_argument('--preExec', help='Code to execute before locking configs')
46 parser.add_argument('--postExec', help='Code to execute after setup')
47 parser.add_argument('--postInclude', nargs='+', help='Configuration fragment to include after main job options')
48 parser.add_argument('--printConfig', action='store_true', help='Print detailed Athena configuration')
49 parser.add_argument('--dumpArguments', action='store_true', help='Print arguments and exit')
50 parser.add_argument('--outputVersion', type=str, default="", help='Version to be used in output files for ntuple and monitoring')
51 parser.add_argument('--outputDirectory', default='.', help='Output directory for produced files')
52
53 parser.add_argument('--frag-ids', dest='fragIDs', nargs="*", default=['0x100', '0x101', '0x200', '0x201', '0x402'],
54 help='Tile Frag IDs of modules to be monitored. Empty=ALL')
55 parser.add_argument('--demo-cabling', dest='demoCabling', type=int, default=2018, help='Time Demonatrator cabling to be used')
56 parser.add_argument('--nsamples', type=int, default=None, help='Number of samples')
57 parser.add_argument('--use-sqlite', dest='useSqlite', default='/afs/cern.ch/user/t/tiledemo/public/efmon/condb/tileSqlite.db',
58 help='Providing local SQlite file, conditions constants will be used from it')
59 parser.add_argument('--mon', default=False, help='Run Tile TB monitoring', action=argparse.BooleanOptionalAction)
60 parser.add_argument('--offline-units', type=int, choices=[0, 1, 2, 3], default=None,
61 help='Offline units in ntuple: 0 (ADC), 1 (pC), 2 (Cesium pC), 3 (MeV)')
62
63 # Set up Tile run type
64 run_type_group = parser.add_argument_group('Tile Run Type')
65 run_type = run_type_group.add_mutually_exclusive_group()
66 run_type.add_argument('--cis', action='store_true', help='Tile CIS run type')
67 run_type.add_argument('--mono-cis', action='store_true', dest='mono_cis', help='Tile mono CIS run type')
68 run_type.add_argument('--laser', action='store_true', help='Tile laser run type')
69 run_type.add_argument('--pedestals', action='store_true', help='Tile pedestals run type')
70 run_type.add_argument('--physics', action='store_true', help='Tile physics run type')
71
72 # Set up Tile reconstuction method
73 method = parser.add_argument_group('Tile reconstuction method')
74 method.add_argument('--opt2', default=False, help='Use Tile Opt2 reconstuction method', action=argparse.BooleanOptionalAction)
75 method.add_argument('--opt-atlas', dest='opt_atlas', default=False, help='Use Tile OptATLAS reconstuction method', action=argparse.BooleanOptionalAction)
76 method.add_argument('--fit', default=True, help='Use Tile Fit reconstuction method', action=argparse.BooleanOptionalAction)
77
78 args, _ = parser.parse_known_args()
79
80 if args.dumpArguments:
81 log.info('=====>>> FINAL ARGUMENTS FOLLOW')
82 print('{:40} : {}'.format('Argument Name', 'Value'))
83 for a, v in (vars(args)).items():
84 print(f'{a:40} : {v}')
85 sys.exit(0)
86
87 fragIDs = [int(fragID, base=16) for fragID in args.fragIDs]
88
89 # Initially the following flags are not set up (they must be provided)
90 flags.Input.Files = []
91
92 # Initial configuration flags from command line arguments (to be used to set up defaults)
93 flags.fillFromArgs(parser=parser)
94
95 # =======>>> Set the Athena configuration flags to defaults (can be overriden via comand line)
96 flags.Exec.MaxEvents = 3
97 flags.Common.isOnline = True
98 from AthenaConfiguration.TestDefaults import defaultGeometryTags
99 flags.GeoModel.AtlasVersion = defaultGeometryTags.RUN2
100 flags.DQ.useTrigger = False
101 flags.DQ.enableLumiAccess = False
102 flags.Exec.PrintAlgsSequence = True
103
104 flags.Tile.doFit = True
105 flags.Tile.useDCS = False
106 flags.Tile.NoiseFilter = 0
107 flags.Tile.correctTime = False
108 flags.Tile.correctTimeJumps = False
109 flags.Tile.BestPhaseFromCOOL = False
110 flags.Tile.doOverflowFit = False
111
112 flags.Tile.RunType = TileRunType.PHY
113 flags.Beam.Type = BeamType.Collisions
114 # Get beam energy from meta data (Tile TB setup: [GeV])
115 beamEnergy = GetFileMD(flags.Input.Files).get("beam_energy", 100)
116 flags.Beam.Energy = beamEnergy * GeV
117
118 # Set up the Tile input files
119 if not flags.Input.Files and args.run:
120 flags.Input.Files = TileInputFiles.findFilesFromAgruments(args)
121 if not flags.Input.Files:
122 log.error('Input files must be provided! For example: --filesInput=file1,file2,... or --run RUNNUMBER')
123 sys.exit(-1)
124
125 # Set up the Tile run type using arguments if it was not set up via configuration flags
126 if args.cis:
127 flags.Tile.RunType = TileRunType.CIS
128 elif args.mono_cis:
129 flags.Tile.RunType = TileRunType.MONOCIS
130 elif args.laser:
131 flags.Tile.RunType = TileRunType.LAS
132 elif args.pedestals:
133 flags.Tile.RunType = TileRunType.PED
134 elif args.physics:
135 flags.Tile.RunType = TileRunType.PHY
136
137 # Set up Tile reconstuction method
138 flags.Tile.doOpt2 = args.opt2
139 flags.Tile.doOptATLAS = args.opt_atlas
140 flags.Tile.doFit = args.fit
141
142 # =======>>> Override default configuration flags from command line arguments
143 flags.fillFromArgs(parser=parser)
144
145 runNumber = flags.Input.RunNumbers[0]
146 if not flags.Output.HISTFileName:
147 flags.Output.HISTFileName = f'{args.outputDirectory}/tiletbmon_{runNumber}{args.outputVersion}.root'
148
149 if args.preExec:
150 log.info(f'Executing preExec: {args.preExec}')
151 exec(args.preExec)
152
153 flags.lock()
154
155 log.info('=====>>> FINAL CONFIG FLAGS SETTINGS FOLLOW:')
156 flags.dump(pattern='Tile.*|Beam.*|Input.*|Exec.*|IOVDb.[D|G].*', evaluate=True)
157
158 # Configure number of samples
159 nSamples = args.nsamples
160 if not nSamples:
161 nSamples = 7 if flags.Tile.RunType.isBiGain() or runNumber <= 2110820 else 15
162 log.info(f'Auto configure number of samples: {nSamples}')
163
164 # =======>>> Initialize configuration object, add accumulator, merge, and run
165 from AthenaConfiguration.MainServicesConfig import MainServicesCfg
166 cfg = MainServicesCfg(flags)
167
168 # =======>>> Configure Tile raw data (digits) reading
169 from TileByteStream.TileByteStreamConfig import TileRawDataReadingCfg
170 cfg.merge( TileRawDataReadingCfg(flags, readMuRcv=False,
171 readDigits=True,
172 readRawChannel=True,
173 readDigitsFlx=True,
174 readBeamElem=True) )
175
176 # =======>>> Configure reconstruction of Tile TestBeam data
177 from TileTBRec.TileTestBeamRecoConfig import TileTestBeamRawChannelMakerCfg
178 cfg.merge( TileTestBeamRawChannelMakerCfg(flags, nsamples=nSamples) )
179 cfg.merge( TileTestBeamRawChannelMakerCfg(flags, nsamples=16, useFELIX=True) )
180
181 if args.useSqlite:
182 cfg.getService('IOVDbSvc').overrideTags += [
183 f'<prefix>/TILE</prefix> <db>sqlite://;schema={args.useSqlite};dbname={flags.IOVDb.DatabaseInstance}</db>',
184 # ROD folder does not exist in Sqlite file at the moment (should be added)
185 f'<prefix>/TILE/ONL01/STATUS/ROD</prefix> <db>COOLONL_TILE/{flags.IOVDb.DatabaseInstance}</db>'
186 ]
187
188 # =======>>> Configure Tile TestBeam monitoring
189 if args.mon:
190 from TileTBRec.TileTestBeamRecoConfig import TileTestBeamRecoCfg
191 cfg.merge( TileTestBeamRecoCfg(flags, useDemoCabling=args.demoCabling, nsamples=nSamples) )
192 cfg.merge( TileTestBeamRecoCfg(flags, useDemoCabling=args.demoCabling, nsamples=16, useFELIX=True) )
193
194 from TileMonitoring.RunTileTBMonitoring import TileTestBeamMonitoringCfg
195 cfg.merge(TileTestBeamMonitoringCfg(flags, fragIDs=fragIDs, useFELIX=True))
196
197 # =======>>> Configure Tile TestBeam h1000 Ntuple
198 ntupleFile = f'{args.outputDirectory}/tiletb_{runNumber}{args.outputVersion}.aan.root'
199 offlineUnits = args.offline_units
200 if not offlineUnits:
201 if flags.Tile.RunType in [TileRunType.PED, TileRunType.CIS]:
202 offlineUnits = 0 # ADC
203 elif flags.Tile.RunType in [TileRunType.LAS, TileRunType.MONOCIS]:
204 offlineUnits = 1 # pC
205 else:
206 offlineUnits = 3 # MeV
207 unitsName = {0: "ADC", 1: "pC", 2: "Cesium pC", 3: "MeV"}.get(offlineUnits)
208 log.info(f'Auto configure offline units: {offlineUnits} => {unitsName}')
209
210 calibrateEnergy = (offlineUnits != 0)
211
212 from TileTBRec.TileTBAANtupleConfig import TileTBAANtupleCfg
213 cfg.merge( TileTBAANtupleCfg(flags, outputFile=ntupleFile, OfflineUnits=offlineUnits, CalibrateEnergy=calibrateEnergy, NSamples=nSamples) )
214
215 # =======>>> Configure Tile CIS calibration
216 if flags.Tile.RunType in [TileRunType.CIS, TileRunType.MONOCIS]:
217 cisCalibFile = f'{args.outputDirectory}/tileCalibCIS_{runNumber}_CIS{args.outputVersion}.root'
218 from TileCalibAlgs.TileCisCalibAlgConfig import TileCisCalibAlgCfg
219 cfg.merge( TileCisCalibAlgCfg(flags, FileName=cisCalibFile) )
220
221 tileCisTool = cfg.getEventAlgo('TileCisCalibAlg').TileCalibTools['TileCisDefaultCalibTool']
222 if runNumber >= 2200000:
223 tileCisTool.FragIDsDemonstrators = [ 0x201, 0x402 ]
224 elif runNumber >= 2100000:
225 tileCisTool.FragIDsDemonstrators = [ 0x201 ]
226
227 # =======>>> Configure Tile noise calibration
228 if flags.Tile.RunType is TileRunType.PED:
229 from TileCalibAlgs.TileDigiNoiseCalibAlgConfig import TileDigiNoiseCalibAlgCfg
230 cfg.merge( TileDigiNoiseCalibAlgCfg(flags) )
231
232 # =======>>> Configure ROD to ROB mapping
233 # Scan first event for all fragments to create proper ROD to ROB map
234 cfg.getCondAlgo('TileHid2RESrcIDCondAlg').RODStatusProxy = None
235
236 # =======>>> Process post includes
237 if args.postInclude:
238 log.info(f'Process postInclude: {args.postInclude}')
239 from PyJobTransforms.TransformUtils import processPostInclude
240 processPostInclude(args, flags, cfg)
241
242 # =======>>> Any last things to do?
243 if args.postExec:
244 log.info(f'Executing postExec: {args.postExec}')
245 exec(args.postExec)
246
247 if args.printConfig:
248 cfg.printConfig(withDetails=True, summariseProps=True, printDefaults=True)
249
250 if args.config_only:
251 cfg.store(open('TileTestBeamMonitoring.pkl', 'wb'))
252 else:
253 sc = cfg.run()
254 # Success should be 0
255 sys.exit(not sc.isSuccess())
void print(char *figname, TCanvas *c1)
T * get(TKey *tobj)
get a TObject* from a TKey* (why can't a TObject be a TKey?)
Definition hcg.cxx:130
getArgumentParser(**kwargs)
findFilesFromAgruments(args)