5 from __future__
import print_function
9 Calculate beamspot DQ defects automatically from result of beamspot fit and
10 produce locally sqlite file that can later be merged into HEAD. Also dump
11 results to txt file for linking to web page
14 __author__ =
'Carl Gwilliam'
16 __usage__ =
'%prog [options] nt.root [nt.root ...]'
23 from optparse
import OptionParser
24 parser = OptionParser(usage=__usage__, version=__version__)
25 parser.add_option(
'-b',
'--batch', dest=
'batch', action=
'store_true', default=
False, help=
'run in batch mode')
26 parser.add_option(
'-i',
'--interactive', dest=
'interactive', action=
'store_true', default=
False, help=
'interactive')
27 parser.add_option(
'-e',
'--extend', dest=
'extend', action=
'store_true', default=
False, help=
'extend existing SQLite file (default: delete any existing file)')
28 parser.add_option(
'',
'--noaverage', dest=
'noaverage', action=
'store_true', default=
False, help=
'do not write per-run average beam spot with large width')
29 parser.add_option(
'',
'--statlist', dest=
'statlist', default=
'59', help=
'comma separated list of status word values to accept (must have known fitId)')
30 parser.add_option(
'',
'--lbmin', dest=
'lbmin', type=
'int', default=
None, help=
'Minimum LB to consider')
31 parser.add_option(
'',
'--lbmax', dest=
'lbmax', type=
'int', default=
None, help=
'Maximum LB to consider')
32 parser.add_option(
'-o',
'--output', dest=
'output', default=
'dqflags.db', help=
'name of output COOL SQLite file for DQ (default: dqflags.db')
33 parser.add_option(
'-t',
'--tag', dest=
'tag', default=
'nominal', help=
'COOL tag (default: nominal)')
34 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)')
36 (options,args) = parser.parse_args()
38 parser.error(
'wrong number of command line arguments')
43 os.unsetenv(
'DISPLAY')
45 from InDetBeamSpotExample
import ROOTUtils
48 from InDetBeamSpotExample.Utils
import getRunFromName
53 for a
in options.statlist.split(
','):
54 statList.append(
int(a))
57 fitIdList.append( fitIDInt )
59 print (
'ERROR: Status word value of %i has no known fitId entry' %
int(a))
75 print (
'\nDetermining if average beamspot parameters available')
77 print (
'\nExtracting from file ',filename,
':')
79 f = ROOT.TFile(filename)
80 if f.Get(
'BeamSpotNt'):
81 bsNt = BeamSpotNt(filename)
82 elif f.Get(
'Beamspot/Beamspots'):
83 bsNt = BeamSpotFinderNt(filename)
85 for bs
in bsNt.allData():
88 if options.lbmin
and options.lbmin>bs.lbStart:
90 if options.lbmax
and options.lbmax<bs.lbEnd+1:
92 if not bs.status
in statList:
95 fitStatusInt =
int(
format( bs.status,
'08b')[-2:], 2)
96 fitIDInt =
int(
format( bs.status,
'08b')[:4], 2)
98 print (
'[%i, %3i - %3i]: %5i vertices, fitStatus = %i, fitID = %i' % (bs.run,bs.lbStart,bs.lbEnd+1,bs.nEvents,fitStatusInt,fitIDInt))
100 minRun =
min(bs.run,minRun)
101 maxRun =
max(bs.run,maxRun)
105 print (
'*** Skipping unsuccessful fit ...\n')
107 if not fitIDInt
in fitIdList:
108 print (
'*** Skipping fit with ID =',fitIDInt,
'\n')
119 from InDetBeamSpotExample.DQUtilities
import IDBSDefectWriter
120 idbsDefects = IDBSDefectWriter(options.output,
not options.extend)
122 if nEntries>0
and not options.noaverage:
123 print (
'\nInitially Setting DQ defect to ID_BS_RUNAVERAGE for runs %s ... %s with average beamspot available' %(minRun, maxRun))
124 idbsDefects.defectType(
'ID_BS_RUNAVERAGE')
126 print (
'\nInitially setting DQ defect to ID_BS_NOBEAMSPOT for runs %s ... %s without average beamspot available' % (minRun, maxRun))
127 idbsDefects.defectType(
'ID_BS_NOBEAMSPOT')
134 for i
in range(minRun,maxRun):
137 for filename
in args:
138 print (
'Copying beam spot data from',filename)
139 f = ROOT.TFile(filename)
140 if f.Get(
'BeamSpotNt'):
141 bsNt = BeamSpotNt(filename)
142 elif f.Get(
'Beamspot/Beamspots'):
143 bsNt = BeamSpotFinderNt(filename)
145 for bs
in bsNt.allData():
147 if options.lbmin
and options.lbmin>bs.lbStart:
149 if options.lbmax
and options.lbmax<bs.lbEnd+1:
152 if bs.status
in statList
and bs.lbStart
not in good_lbs[bs.run]:
154 idbsDefects.add(minRun, maxRun, bs.lbStart, bs.lbEnd+1)
155 good_lbs[bs.run][bs.lbStart]=1
158 print (
'WARNING: Skipping entry with status word %3s, lbStart=%4d, lbEnd=%4d' % (bs.status, bs.lbStart, bs.lbEnd))
163 idbsDefects.complete(minRun, maxRun)
168 idbsDefects.writeDefects(nonpresent = options.absent)
169 idbsDefects.dump(options.output)
171 if options.lbmin
or options.lbmax:
172 print (
'\n**** WARNING: ONLY CONSIDERED ENTRIES IN RANGE [%s,%s] ****\n' % (options.lbmin,options.lbmax))
175 if options.interactive:
176 os.environ[
'PYTHONINSPECT'] =
'1'