8 Calculate beamspot DQ defects automatically from result of beamspot fit and
9 produce locally sqlite file that can later be merged into HEAD. Also dump
10 results to txt file for linking to web page
13 __author__ =
'Carl Gwilliam'
15 __usage__ =
'%prog [options] nt.root [nt.root ...]'
22 from optparse
import OptionParser
23 parser = OptionParser(usage=__usage__, version=__version__)
24 parser.add_option(
'-b',
'--batch', dest=
'batch', action=
'store_true', default=
False, help=
'run in batch mode')
25 parser.add_option(
'-i',
'--interactive', dest=
'interactive', action=
'store_true', default=
False, help=
'interactive')
26 parser.add_option(
'-e',
'--extend', dest=
'extend', action=
'store_true', default=
False, help=
'extend existing SQLite file (default: delete any existing file)')
27 parser.add_option(
'',
'--noaverage', dest=
'noaverage', action=
'store_true', default=
False, help=
'do not write per-run average beam spot with large width')
28 parser.add_option(
'',
'--statlist', dest=
'statlist', default=
'59', help=
'comma separated list of status word values to accept (must have known fitId)')
29 parser.add_option(
'',
'--lbmin', dest=
'lbmin', type=
'int', default=
None, help=
'Minimum LB to consider')
30 parser.add_option(
'',
'--lbmax', dest=
'lbmax', type=
'int', default=
None, help=
'Maximum LB to consider')
31 parser.add_option(
'-o',
'--output', dest=
'output', default=
'dqflags.db', help=
'name of output COOL SQLite file for DQ (default: dqflags.db')
32 parser.add_option(
'-t',
'--tag', dest=
'tag', default=
'nominal', help=
'COOL tag (default: nominal)')
33 parser.add_option(
'-a',
'--absent', dest=
'absent', action=
'store_true', default=
False, help=
'Write absent (as well as present) defects to the DB (default: False)')
35 (options,args) = parser.parse_args()
37 parser.error(
'wrong number of command line arguments')
42 os.unsetenv(
'DISPLAY')
44 from InDetBeamSpotExample
import ROOTUtils
47 from InDetBeamSpotExample.Utils
import getRunFromName
52 for a
in options.statlist.split(
','):
53 statList.append(
int(a))
56 fitIdList.append( fitIDInt )
58 print (
'ERROR: Status word value of %i has no known fitId entry' %
int(a))
74 print (
'\nDetermining if average beamspot parameters available')
76 print (
'\nExtracting from file ',filename,
':')
78 f = ROOT.TFile(filename)
79 if f.Get(
'BeamSpotNt'):
80 bsNt = BeamSpotNt(filename)
81 elif f.Get(
'Beamspot/Beamspots'):
82 bsNt = BeamSpotFinderNt(filename)
84 for bs
in bsNt.allData():
87 if options.lbmin
and options.lbmin>bs.lbStart:
89 if options.lbmax
and options.lbmax<bs.lbEnd+1:
91 if not bs.status
in statList:
94 fitStatusInt =
int(
format( bs.status,
'08b')[-2:], 2)
95 fitIDInt =
int(
format( bs.status,
'08b')[:4], 2)
97 print (
'[%i, %3i - %3i]: %5i vertices, fitStatus = %i, fitID = %i' % (bs.run,bs.lbStart,bs.lbEnd+1,bs.nEvents,fitStatusInt,fitIDInt))
99 minRun =
min(bs.run,minRun)
100 maxRun =
max(bs.run,maxRun)
104 print (
'*** Skipping unsuccessful fit ...\n')
106 if not fitIDInt
in fitIdList:
107 print (
'*** Skipping fit with ID =',fitIDInt,
'\n')
118 from InDetBeamSpotExample.DQUtilities
import IDBSDefectWriter
119 idbsDefects = IDBSDefectWriter(options.output,
not options.extend)
121 if nEntries>0
and not options.noaverage:
122 print (
'\nInitially Setting DQ defect to ID_BS_RUNAVERAGE for runs %s ... %s with average beamspot available' %(minRun, maxRun))
123 idbsDefects.defectType(
'ID_BS_RUNAVERAGE')
125 print (
'\nInitially setting DQ defect to ID_BS_NOBEAMSPOT for runs %s ... %s without average beamspot available' % (minRun, maxRun))
126 idbsDefects.defectType(
'ID_BS_NOBEAMSPOT')
133 for i
in range(minRun,maxRun):
136 for filename
in args:
137 print (
'Copying beam spot data from',filename)
138 f = ROOT.TFile(filename)
139 if f.Get(
'BeamSpotNt'):
140 bsNt = BeamSpotNt(filename)
141 elif f.Get(
'Beamspot/Beamspots'):
142 bsNt = BeamSpotFinderNt(filename)
144 for bs
in bsNt.allData():
146 if options.lbmin
and options.lbmin>bs.lbStart:
148 if options.lbmax
and options.lbmax<bs.lbEnd+1:
151 if bs.status
in statList
and bs.lbStart
not in good_lbs[bs.run]:
153 idbsDefects.add(minRun, maxRun, bs.lbStart, bs.lbEnd+1)
154 good_lbs[bs.run][bs.lbStart]=1
157 print (
'WARNING: Skipping entry with status word %3s, lbStart=%4d, lbEnd=%4d' % (bs.status, bs.lbStart, bs.lbEnd))
162 idbsDefects.complete(minRun, maxRun)
167 idbsDefects.writeDefects(nonpresent = options.absent)
168 idbsDefects.dump(options.output)
170 if options.lbmin
or options.lbmax:
171 print (
'\n**** WARNING: ONLY CONSIDERED ENTRIES IN RANGE [%s,%s] ****\n' % (options.lbmin,options.lbmax))
174 if options.interactive:
175 os.environ[
'PYTHONINSPECT'] =
'1'