ATLAS Offline Software
GetLBInfoFromCOOL.py
Go to the documentation of this file.
1 #
2 #Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3 #
4 
5 from PyCool import cool
6 # from PyCool import coral
7 
8 #==================================================================================================================
9 class Folder:
10  def __init__(self, dbconnect, name):
11 
12  self.name = name
13  readonly = True
14 
15  try:
16  self.db = cool.DatabaseSvcFactory.databaseService().openDatabase(dbconnect, readonly)
17  except Exception as e:
18  print ('ERROR::Can not open DB or get chain folder because of exception: %s' ,e)
19  raise Exception(e)
20 
21  if not self.db.existsFolder(name):
22  raise Exception('Folder does not exist: "%s"' ,name)
23 
24  self.folder = self.db.getFolder(name)
25 
26 #==================================================================================================================
27 class LumiBlock:
28  def __init__(self, cool_obj):
29 
30  self.run, self.lb = getRunLBFromObj(cool_obj)
31 
32  self.start_time = cool_obj.payload()['StartTime']
33  self.end_time = cool_obj.payload()['EndTime']
34  self.key_since = cool_obj.since()
35  self.key_until = cool_obj.until()
36 
37  self.inst_lumi_valid = None
38  self.inst_lumi_algid = None
39  self.inst_lumi_offline = None
40 
41  self.is_atlas_physics = None
42  self.is_stable_beams = None
43  self.l1_live_fraction = None
44 
45  self.beam1_intensity = -1.0
46  self.beam2_intensity = -1.0
47 
48  def ReadLumiInfo(self, cool_obj):
49  self.inst_lumi = cool_obj.payload()['LBAvInstLumi']
50  self.inst_lumi_valid = cool_obj.payload()['Valid']
51  self.inst_lumi_algid = cool_obj.payload()['AlgorithmID']
52 
53  def ReadPhysicsLumiInfo(self, payload):
54  if self.is_atlas_physics is not None:
55  raise Exception('ReadPhysicsLumiInfo - duplicate lb=%d' ,self.lb)
56 
57  self.is_atlas_physics = payload['AtlasPhysics']
58  self.is_stable_beams = payload['StableBeams']
59  self.l1_live_fraction = payload['LiveFraction']
60  self.beam1_intensity = payload['Beam1Intensity']
61  self.beam2_intensity = payload['Beam2Intensity']
62  self.inst_lumi_offline = payload['InstLumiAll']
63  self.inst_lumi_offline_algid = payload['AlgorithmID']
64 
65  def GetLBSecs(self):
66  if not (self.end_time > self.start_time):
67  raise Exception('GetLBSecs - invalid start/end times')
68  return (self.end_time - self.start_time)*1.0e-9
69 
70  def GetInstLumi(self):
71  if self.inst_lumi_valid == 0:
72  return self.inst_lumi
73 
74  return 0.0
75 
76  def GetIntLumi(self):
77  if self.inst_lumi_valid == 0:
78  return self.inst_lumi*self.GetLBSecs()
79 
80  return 0.0
81 
82 #==================================================================================================================
83 class Run:
84  def __init__(self, run_number, lbs):
85  self.run_number = run_number
86  self.lbs = lbs
87  self.lb_first = None
88  self.lb_last = None
89 
90  self.int_lumi = 0
91  self.peak_inst_lumi = 0.0
92 
93  for lb in lbs:
94  self.peak_inst_lumi = max([self.peak_inst_lumi, lb.GetInstLumi()])
95 
96  self.int_lumi += lb.GetIntLumi()
97 
98  if self.lb_first is None:
99  self.lb_first = lb
100  self.lb_last = lb
101  else:
102  if lb.lb < self.lb_first.lb: self.lb_first = lb
103  if lb.lb > self.lb_last.lb: self.lb_last = lb
104 
105  def GetRunStartTime(self):
106  return self.lb_first.start_time
107 
108  def GetRunEndTime(self):
109  return self.lb_last.end_time
110 
111  def GetIntLumiInvPB(self):
112  return self.int_lumi*1.0e-6
113 
114  def GetPeakInstLumi(self):
115  return self.peak_inst_lumi
116 
117  def AsStr(self):
118  ip = ('%.0f' ,(self.GetPeakInstLumi())).rjust(5)
119  il = ('%.3f' ,(self.GetIntLumiInvPB())).rjust(8)
120 
121  s = 'Run %s peak lumi=%s int lumi=%s' ,(self.run_number, ip, il)
122 
123  return s
124 
125 
126 #==================================================================================================================
128  run = (obj.since() >> 32)
129  lb = (obj.since() & 0xffff)
130  return (run, lb)
131 
132 #==================================================================================================================
133 def getRunLB(beg_run, end_run):
134  print('INFO::getRunLB - will search for ATLAS runs in the following range:')
135  print('INFO:: beg run: ' ,(beg_run))
136  print('INFO:: end run: ' ,(end_run))
137 
138  f = Folder('COOLONL_TRIGGER/CONDBR2', '/TRIGGER/LUMI/LBLB')
139 
140  fit = f.folder.browseObjects(beg_run << 32, end_run << 32, cool.ChannelSelection.all())
141 
142  runs = {}
143 
144  while fit.goToNext():
145  lb = LumiBlock(fit.currentRef())
146 
147  try:
148  runs[lb.run]
149  except KeyError:
150  runs[lb.run] = {}
151 
152  runs[lb.run][lb.lb] = lb
153 
154  print('INFO::getRunLB - found ', len(runs), ' ATLAS runs')
155 
156  return runs
157 
158 #==================================================================================================================
160 
161  print('INFO::getRunOnlineLumi - will read online luminosity for ', len(runs), ' ATLAS runs')
162 
163  f = Folder('COOLONL_TRIGGER/CONDBR2', '/TRIGGER/LUMI/OnlPrefLumi')
164  icount = 0
165 
166  for run_, lbs in runs.items():
167 
168  fit = f.folder.browseObjects(run_ << 32, (run_ << 32) + 100000, cool.ChannelSelection(0))
169 
170  while fit.goToNext():
171  obj = fit.currentRef()
172 
173  run = (obj.since() >> 32)
174  lb = (obj.since() & 0xffff)
175 
176  if run != run_:
177  raise Exception('getRunOnlineLumi - logic error: run=', run, ', run_=', run_)
178 
179  if lb == 0:
180  continue
181 
182  try:
183  lblock = lbs[lb]
184  except KeyError:
185  print('WANRING::getRunOnlineLumi - failed to find LumiBlock for run, lb: ', run, lb)
186  continue
187 
188  lblock.ReadLumiInfo(obj)
189 
190  icount += 1
191 
192 #==================================================================================================================
193 def GetLumiInfoDic(beg_run, end_run):
194  collect_runs = getRunLB(beg_run, end_run)
195  physics_runs = []
196 
197  getRunOnlineLumi(collect_runs)
198  physics_runs = [Run(run, lbs.values()) for run,lbs in collect_runs.items()]
199 
200  if len(physics_runs) == 0:
201  print('WARNING::Found 0 physics runs')
202  return
203 
204  physics_runs.sort(key = lambda x: x.run_number)
205 
206  RunsLumiDic = {}
207  for run in physics_runs:
208  LumiDic = {}
209 
210  key_since = run.GetRunStartTime() - 100000
211  key_until = run.GetRunEndTime() + 100000
212 
213  f = Folder('COOLOFL_TRIGGER/CONDBR2', '/TRIGGER/OFLLUMI/LumiAccounting')
214  taglist=f.folder.listTags()
215  filledTags = {}
216  for tag in taglist:
217  objs = f.folder.browseObjects(key_since, key_until, cool.ChannelSelection.all(), tag)
218  if len(objs) != 0:
219  filledTags[tag] = len(objs)
220 
221  if len(filledTags) == 0:
222  print ("ERROR: no filled tag")
223  return
224  elif len(filledTags) > 1:
225  print ("WARNING: the number filled tags is more than 1")
226 
227  filledTags = sorted(filledTags.items(),key=lambda x:x[1],reverse=True)
228  objs = f.folder.browseObjects(key_since, key_until, cool.ChannelSelection.all(), filledTags[0][0])
229  icount = 0
230  for obj in objs:
231  crun = obj.payload()['Run']
232  clb = obj.payload()['LumiBlock']
233 
234  if crun != run.run_number:
235  continue
236 
237  for lb in run.lbs:
238  if lb.lb == clb:
239  lb.ReadPhysicsLumiInfo(obj.payload())
240  icount += 1
241  break
242 
243  i_LB = obj.payload()['LumiBlock']
244  LumiDic[i_LB] = {
245  'AtlasPhysics' : obj.payload()['AtlasPhysics'],
246  'InstLumi' : obj.payload()['InstLumi']/10000.,
247  'LiveFraction' : obj.payload()['LiveFraction'],
248  'Duration' : lb.GetLBSecs(),
249  'start_time' : lb.start_time,
250  'end_time' : lb.end_time
251  }
252 
253  if len(run.lbs) != icount:
254  print('WARNING::getRunPhysicsLumi - number of missed lbs=%d' ,(len(run.lbs) - icount))
255  print('WARNING:: number of run lbs=%d' ,(len(run.lbs)))
256  print('WARNING:: number of matched lbs=%d' ,(icount))
257 
258  RunsLumiDic = LumiDic
259 
260  return RunsLumiDic
261 
262 #==================================================================================================================
263 def printLumiInfo(RunsLumiDic):
264  # for run, lumiDic in RunsLumiDic.items():
265  for lb, lumiInfo in RunsLumiDic.items():
266  # s = 'Run %d lb=%4d - AtlasPhysics=%d, InstLumi=%f, LiveFraction=%f, Duration=%f' %(
267  # run,
268  s = 'lb=%4d - AtlasPhysics=%d, InstLumi=%f, LiveFraction=%f, Duration=%f' %(
269  lb,
270  lumiInfo['AtlasPhysics'],
271  lumiInfo['InstLumi'],
272  lumiInfo['LiveFraction'],
273  lumiInfo['Duration']
274  )
275 
276  print (s)
277 
278 #==================================================================================================================
279 if __name__ == '__main__':
280  print ("GetRunInfoFromCOOL: Hello, World !")
GetLBInfoFromCOOL.Run.AsStr
def AsStr(self)
Definition: GetLBInfoFromCOOL.py:117
GetLBInfoFromCOOL.LumiBlock.ReadPhysicsLumiInfo
def ReadPhysicsLumiInfo(self, payload)
Definition: GetLBInfoFromCOOL.py:53
GetLBInfoFromCOOL.LumiBlock.inst_lumi_valid
inst_lumi_valid
Definition: GetLBInfoFromCOOL.py:37
GetLBInfoFromCOOL.LumiBlock.key_until
key_until
Definition: GetLBInfoFromCOOL.py:35
GetLBInfoFromCOOL.LumiBlock.start_time
start_time
Definition: GetLBInfoFromCOOL.py:32
GetLBInfoFromCOOL.Run.GetRunStartTime
def GetRunStartTime(self)
Definition: GetLBInfoFromCOOL.py:105
GetLBInfoFromCOOL.Folder.name
name
Definition: GetLBInfoFromCOOL.py:12
GetLBInfoFromCOOL.Folder.__init__
def __init__(self, dbconnect, name)
Definition: GetLBInfoFromCOOL.py:10
GetLBInfoFromCOOL.Run
Definition: GetLBInfoFromCOOL.py:83
GetLBInfoFromCOOL.Folder
Definition: GetLBInfoFromCOOL.py:9
GetLBInfoFromCOOL.Run.int_lumi
int_lumi
Definition: GetLBInfoFromCOOL.py:90
GetLBInfoFromCOOL.Run.lb_last
lb_last
Definition: GetLBInfoFromCOOL.py:88
GetLBInfoFromCOOL.LumiBlock.key_since
key_since
Definition: GetLBInfoFromCOOL.py:34
max
constexpr double max()
Definition: ap_fixedTest.cxx:33
GetLBInfoFromCOOL.LumiBlock.inst_lumi_offline
inst_lumi_offline
Definition: GetLBInfoFromCOOL.py:39
GetLBInfoFromCOOL.Run.lbs
lbs
Definition: GetLBInfoFromCOOL.py:86
GetLBInfoFromCOOL.LumiBlock.GetLBSecs
def GetLBSecs(self)
Definition: GetLBInfoFromCOOL.py:65
GetLBInfoFromCOOL.Run.GetIntLumiInvPB
def GetIntLumiInvPB(self)
Definition: GetLBInfoFromCOOL.py:111
GetLBInfoFromCOOL.Run.GetRunEndTime
def GetRunEndTime(self)
Definition: GetLBInfoFromCOOL.py:108
GetLBInfoFromCOOL.LumiBlock.inst_lumi_algid
inst_lumi_algid
Definition: GetLBInfoFromCOOL.py:38
GetLBInfoFromCOOL.LumiBlock.ReadLumiInfo
def ReadLumiInfo(self, cool_obj)
Definition: GetLBInfoFromCOOL.py:48
GetLBInfoFromCOOL.LumiBlock.lb
lb
Definition: GetLBInfoFromCOOL.py:30
GetLBInfoFromCOOL.LumiBlock.is_atlas_physics
is_atlas_physics
Definition: GetLBInfoFromCOOL.py:41
GetLBInfoFromCOOL.GetLumiInfoDic
def GetLumiInfoDic(beg_run, end_run)
Definition: GetLBInfoFromCOOL.py:193
GetLBInfoFromCOOL.LumiBlock.inst_lumi_offline_algid
inst_lumi_offline_algid
Definition: GetLBInfoFromCOOL.py:63
GetLBInfoFromCOOL.getRunLBFromObj
def getRunLBFromObj(obj)
Definition: GetLBInfoFromCOOL.py:127
GetLBInfoFromCOOL.printLumiInfo
def printLumiInfo(RunsLumiDic)
Definition: GetLBInfoFromCOOL.py:263
GetLBInfoFromCOOL.LumiBlock.__init__
def __init__(self, cool_obj)
Definition: GetLBInfoFromCOOL.py:28
GetLBInfoFromCOOL.LumiBlock
Definition: GetLBInfoFromCOOL.py:27
ReadCoolUPD4.openDatabase
def openDatabase(dbstring)
Definition: ReadCoolUPD4.py:21
GetLBInfoFromCOOL.getRunLB
def getRunLB(beg_run, end_run)
Definition: GetLBInfoFromCOOL.py:133
GetLBInfoFromCOOL.LumiBlock.l1_live_fraction
l1_live_fraction
Definition: GetLBInfoFromCOOL.py:43
GetLBInfoFromCOOL.Run.run_number
run_number
Definition: GetLBInfoFromCOOL.py:85
GetLBInfoFromCOOL.LumiBlock.is_stable_beams
is_stable_beams
Definition: GetLBInfoFromCOOL.py:42
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.
GetLBInfoFromCOOL.Folder.folder
folder
Definition: GetLBInfoFromCOOL.py:24
print
void print(char *figname, TCanvas *c1)
Definition: TRTCalib_StrawStatusPlots.cxx:25
GetLBInfoFromCOOL.LumiBlock.end_time
end_time
Definition: GetLBInfoFromCOOL.py:33
GetLBInfoFromCOOL.LumiBlock.inst_lumi
inst_lumi
Definition: GetLBInfoFromCOOL.py:49
GetLBInfoFromCOOL.Run.GetPeakInstLumi
def GetPeakInstLumi(self)
Definition: GetLBInfoFromCOOL.py:114
GetLBInfoFromCOOL.LumiBlock.GetIntLumi
def GetIntLumi(self)
Definition: GetLBInfoFromCOOL.py:76
GetLBInfoFromCOOL.LumiBlock.beam1_intensity
beam1_intensity
Definition: GetLBInfoFromCOOL.py:45
GetLBInfoFromCOOL.getRunOnlineLumi
def getRunOnlineLumi(runs)
Definition: GetLBInfoFromCOOL.py:159
GetLBInfoFromCOOL.LumiBlock.beam2_intensity
beam2_intensity
Definition: GetLBInfoFromCOOL.py:46
GetLBInfoFromCOOL.Run.peak_inst_lumi
peak_inst_lumi
Definition: GetLBInfoFromCOOL.py:91
GetLBInfoFromCOOL.LumiBlock.GetInstLumi
def GetInstLumi(self)
Definition: GetLBInfoFromCOOL.py:70
GetLBInfoFromCOOL.Run.lb_first
lb_first
Definition: GetLBInfoFromCOOL.py:87
GetLBInfoFromCOOL.Folder.db
db
Definition: GetLBInfoFromCOOL.py:16
GetLBInfoFromCOOL.Run.__init__
def __init__(self, run_number, lbs)
Definition: GetLBInfoFromCOOL.py:84