ATLAS Offline Software
StandaloneBunchgroupHandler.py
Go to the documentation of this file.
1 #!/bin/env python
2 
3 # Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
4 #
5 # BunchgroupHandler
6 #
7 # David Berge - October 2010
8 #
9 # Contents:
10 # BunchgroupHandler - utility tool to access bunch-group information from COOL
11 #
12 
13 from __future__ import print_function
14 from builtins import range
15 from builtins import object
16 import sys
17 
18 from PyCool import cool
19 from CoolConvUtilities.AtlCoolLib import indirectOpen
20 
22 
23  def __init__(self,run):
24 
25  self.run = run
26  # physics bunch group BCIDs, keyed by LB
28  # BCID lookup, keyed by LB
29  self.bcidLookupBefore = {}
30  # BCID lookup, keyed by LB
31  self.bcidLookupAfter = {}
32  # LB lookup
33  self.lbLookup = {}
34 
35  self.verbose = False
36 
37  def createLookup(self,lb,bcidRange):
38  if self.verbose:
39  print("createLookup called")
40  self.bcidLookupBefore[lb] = []
41  self.bcidLookupAfter[lb] = []
42  for bcid in range(3564):
43  bcidListBefore = []
44  bcidListAfter = []
45  for offset in range(1,bcidRange+1):
46  checkBcid = bcid + offset
47  if checkBcid > 3563:
48  checkBcid -= 3564
49  if checkBcid in self.physicsBunchgroupBcids[lb]:
50  bcidListAfter.append(1)
51  else:
52  bcidListAfter.append(0)
53  self.bcidLookupAfter[lb].append(bcidListAfter)
54 
55  for offset in range(1,bcidRange+1):
56  checkBcid = bcid - offset
57  if checkBcid < 0:
58  checkBcid += 3564
59  if checkBcid in self.physicsBunchgroupBcids[lb]:
60  bcidListBefore.append(1)
61  else:
62  bcidListBefore.append(0)
63  self.bcidLookupBefore[lb].append(bcidListBefore)
64 
65  if self.verbose:
66  print("Bg run data, Run",self.run,"LB",lb)
67  for bcid in range(3564):
68  print("BCID",bcid,"before",self.bcidLookupBefore[lb][bcid])
69  print("BCID",bcid,"after",self.bcidLookupAfter[lb][bcid])
70 
72 
73  def __init__(self):
74 
75  # Database parameters
76  self.trigProdDbName = 'COOLONL_TRIGGER/COMP200'
77  self.bunchGroupContentFolder = '/TRIGGER/LVL1/BunchGroupContent'
78  self.bunchGroupDescFolder = '/TRIGGER/LVL1/BunchGroupDescription'
79 
80  # Database handle
81  self.trigProdDb = None
82 
83  # self.verbose = False
84  self.verbose = True
85 
86  # set physics bunch group
88 
89  # run objects
90  self.runObjects = dict()
91 
92  # remember how many runs we have, keep only runsToArchive in memory
93  self.runArchive = []
94  # number of runs we keep in memory
95  self.runsToArchive = 20
96 
97  # BCID range
98  self.bcidRange = 32
99 
100  def openDB(self):
101  # Open the trigger COOL database
102  try:
103  self.trigProdDb = indirectOpen(self.trigProdDbName, True, False)
104  if self.verbose: print('Connected to', self.trigProdDbName, 'for Trigger data')
105  except Exception as e:
106  print(e)
107  return False
108  return True
109 
110  def closeDB(self):
111  # Close the database
112  if self.trigProdDb is not None:
113  self.trigProdDb.closeDatabase()
114  self.trigProdDb = None
115 
116  def loadRunData(self, run):
117  self.openDB()
118  self.loadBunchgroups(run)
119  self.closeDB()
120  if len(self.runArchive) > self.runsToArchive:
121  if self.verbose:
122  print("Remove run",self.runArchive[0],"from memory")
123  del self.runObjects[self.runArchive[0]]
124  self.runArchive.pop(0)
125 
126  def loadBunchgroups(self, run):
127 
128  self.runObjects[run] = BunchgroupRunData(run)
129  self.runArchive.append(run)
130  self.runObjects[run].verbose = self.verbose
131 
132  if self.verbose:
133  print('loadBounchgroups called, run',run)
134 
135  bgFolder = self.trigProdDb.getFolder(self.bunchGroupContentFolder)
136  itr = bgFolder.browseObjects(run << 32,(run+1) << 32,cool.ChannelSelection.all())
137  while itr.goToNext() :
138  obj=itr.currentRef()
139  lb=(obj.since() & 0xffff)
140  self.runObjects[run].physicsBunchgroupBcids[lb] = []
141  bgcont = obj.payload()['BunchCode']
142  for bcid in range(3564):
143  if(bgcont[bcid]>>self.physicsBunchgroupBit & 1): self.runObjects[run].physicsBunchgroupBcids[lb].append(bcid)
144  self.runObjects[run].createLookup(lb,self.bcidRange)
145  if self.verbose:
146  print("LB",lb,"BCIDs",self.runObjects[run].physicsBunchgroupBcids[lb])
147 
148  def getTrainPosition(self,run,lb,bcid):
149 
150  if self.verbose:
151  print('getTrainPosition called')
152  before, after = self.getNeighbourBcids(run,lb,bcid)
153  return (len(before)+1)
154 
155  def getNeighbourBcids(self,run,lb,bcid):
156 
157  if self.verbose:
158  print('getNeighbourBcids called')
159 
160  before, after = self.getNeighbourPattern(run,lb,bcid)
161 
162  l1 = []
163  for bcidOffset in range(len(before)):
164  checkBcid = bcid - (bcidOffset+1)
165  if checkBcid < 0: checkBcid += 3564
166  if before[bcidOffset]:
167  if self.verbose:
168  print("BCID",checkBcid,"is filled")
169  l1.append(checkBcid)
170  l2 = []
171  for bcidOffset in range(len(after)):
172  checkBcid = bcid + (bcidOffset+1)
173  if checkBcid > 3563: checkBcid -= 3564
174  if after[bcidOffset]:
175  if self.verbose:
176  print("BCID",checkBcid,"is filled")
177  l2.append(checkBcid)
178  return l1, l2
179 
180 
181  def getNeighbourPattern(self,run,lb,bcid):
182 
183  if self.verbose:
184  print('getNeighbourPattern called')
185 
186  lbList = []
187  try:
188  lbList = list(self.runObjects[run].physicsBunchgroupBcids.keys())
189  except KeyError:
190  self.loadRunData(run)
191  lbList = list(self.runObjects[run].physicsBunchgroupBcids.keys())
192 
193  latestLb = -1
194  try:
195  latestLb = self.runObjects[run].lbLookup[lb]
196  except KeyError:
197  for testLb in sorted(lbList):
198  if latestLb < 0: latestLb = testLb
199  if lb > testLb: latestLb = testLb
200  if self.verbose:
201  print("LB",lb,"latest LB",latestLb)
202  self.runObjects[run].lbLookup[lb] = latestLb
203 
204  return (self.runObjects[run].bcidLookupBefore[latestLb][bcid], self.runObjects[run].bcidLookupAfter[latestLb][bcid])
205 
206 if __name__ == '__main__':
207 
208  import getopt
209 
210  def usage():
211  print("%s" % sys.argv[0])
212  print(" --help this printout")
213  print(" --run <runNo> run number")
214  print(" --bcid <bcid> BCID")
215  print(" --lb <lb> LB")
216 
217  try:
218  longopts=['run=','bcid=','lb=','help']
219  opts,args=getopt.getopt(sys.argv[1:],'',longopts)
220  except getopt.GetoptError:
221  usage()
222  sys.exit(2)
223 
224  run = -1
225  lb = -1
226  bcid = -1
227 
228  for o,a in opts:
229  if o in ('-h','--help'):
230  usage()
231  sys.exit(0)
232  if o == '--run':
233  run = int(a)
234  if o == '--bcid':
235  bcid = int(a)
236  if o == '--lb':
237  lb = int(a)
238 
239  if (run == -1 or bcid == -1 or lb == -1) :
240  usage()
241  sys.exit(2)
242 
244  bg.verbose = False
245  bg.loadRunData(run)
246 
247  l1, l2 = bg.getNeighbourBcids(run,lb,bcid)
248  print("Run",run,"neighbouring filled BCIDs for BCID",bcid,":",sorted(l1), sorted(l2))
StandaloneBunchgroupHandler.BunchgroupHandler.__init__
def __init__(self)
Definition: StandaloneBunchgroupHandler.py:73
StandaloneBunchgroupHandler.BunchgroupRunData.verbose
verbose
Definition: StandaloneBunchgroupHandler.py:35
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
StandaloneBunchgroupHandler.BunchgroupHandler.runObjects
runObjects
Definition: StandaloneBunchgroupHandler.py:90
StandaloneBunchgroupHandler.BunchgroupRunData.physicsBunchgroupBcids
physicsBunchgroupBcids
Definition: StandaloneBunchgroupHandler.py:27
StandaloneBunchgroupHandler.BunchgroupHandler.bunchGroupContentFolder
bunchGroupContentFolder
Definition: StandaloneBunchgroupHandler.py:77
StandaloneBunchgroupHandler.BunchgroupHandler.getNeighbourBcids
def getNeighbourBcids(self, run, lb, bcid)
Definition: StandaloneBunchgroupHandler.py:155
StandaloneBunchgroupHandler.BunchgroupHandler.physicsBunchgroupBit
physicsBunchgroupBit
Definition: StandaloneBunchgroupHandler.py:87
StandaloneBunchgroupHandler.BunchgroupHandler.loadRunData
def loadRunData(self, run)
Definition: StandaloneBunchgroupHandler.py:116
StandaloneBunchgroupHandler.BunchgroupRunData.run
run
Definition: StandaloneBunchgroupHandler.py:25
dumpHVPathFromNtuple.append
bool append
Definition: dumpHVPathFromNtuple.py:91
StandaloneBunchgroupHandler.BunchgroupRunData.lbLookup
lbLookup
Definition: StandaloneBunchgroupHandler.py:33
StandaloneBunchgroupHandler.BunchgroupHandler.trigProdDbName
trigProdDbName
Definition: StandaloneBunchgroupHandler.py:76
StandaloneBunchgroupHandler.BunchgroupHandler.getNeighbourPattern
def getNeighbourPattern(self, run, lb, bcid)
Definition: StandaloneBunchgroupHandler.py:181
StandaloneBunchgroupHandler.BunchgroupHandler.runArchive
runArchive
Definition: StandaloneBunchgroupHandler.py:93
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:195
histSizes.list
def list(name, path='/')
Definition: histSizes.py:38
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.
print
void print(char *figname, TCanvas *c1)
Definition: TRTCalib_StrawStatusPlots.cxx:25
StandaloneBunchgroupHandler.BunchgroupRunData.__init__
def __init__(self, run)
Definition: StandaloneBunchgroupHandler.py:23
StandaloneBunchgroupHandler.BunchgroupHandler
Definition: StandaloneBunchgroupHandler.py:71
StandaloneBunchgroupHandler.BunchgroupRunData.bcidLookupBefore
bcidLookupBefore
Definition: StandaloneBunchgroupHandler.py:29
StandaloneBunchgroupHandler.BunchgroupHandler.getTrainPosition
def getTrainPosition(self, run, lb, bcid)
Definition: StandaloneBunchgroupHandler.py:148
StandaloneBunchgroupHandler.BunchgroupHandler.openDB
def openDB(self)
Definition: StandaloneBunchgroupHandler.py:100
StandaloneBunchgroupHandler.BunchgroupHandler.runsToArchive
runsToArchive
Definition: StandaloneBunchgroupHandler.py:95
StandaloneBunchgroupHandler.BunchgroupHandler.bunchGroupDescFolder
bunchGroupDescFolder
Definition: StandaloneBunchgroupHandler.py:78
StandaloneBunchgroupHandler.BunchgroupRunData.bcidLookupAfter
bcidLookupAfter
Definition: StandaloneBunchgroupHandler.py:31
StandaloneBunchgroupHandler.BunchgroupHandler.closeDB
def closeDB(self)
Definition: StandaloneBunchgroupHandler.py:110
if
if(febId1==febId2)
Definition: LArRodBlockPhysicsV0.cxx:567
StandaloneBunchgroupHandler.usage
def usage()
Definition: StandaloneBunchgroupHandler.py:210
StandaloneBunchgroupHandler.BunchgroupRunData
Definition: StandaloneBunchgroupHandler.py:21
pickleTool.object
object
Definition: pickleTool.py:30
StandaloneBunchgroupHandler.BunchgroupHandler.loadBunchgroups
def loadBunchgroups(self, run)
Definition: StandaloneBunchgroupHandler.py:126
StandaloneBunchgroupHandler.BunchgroupHandler.trigProdDb
trigProdDb
Definition: StandaloneBunchgroupHandler.py:81
StandaloneBunchgroupHandler.BunchgroupHandler.bcidRange
bcidRange
Definition: StandaloneBunchgroupHandler.py:98
StandaloneBunchgroupHandler.BunchgroupHandler.verbose
verbose
Definition: StandaloneBunchgroupHandler.py:84
python.AtlCoolLib.indirectOpen
def indirectOpen(coolstr, readOnly=True, debug=False)
Definition: AtlCoolLib.py:130
StandaloneBunchgroupHandler.BunchgroupRunData.createLookup
def createLookup(self, lb, bcidRange)
Definition: StandaloneBunchgroupHandler.py:37