ATLAS Offline Software
MuonSpectrometer/MuonCalib/MuonCalibDbOperations/python/ReadCool.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
2 
3 
4 import CoolConvUtilities.AtlCoolLib as AtlCoolLib
5 from PyCool import cool,coral
6 import zlib,StringIO
7 import sys
8 import re
9 from MuonCalibDbOperations.MuonCalibResolveTag import ResolveTag
10 from MuonCalibDbOperations.MuonCalibConvertTimeSlewing import TimeSlewingApplied, NoTs2Ts, GasmonDriftTimeOffsetT0
11 
12 sys.argv=[sys.argv[0], '-b']
13 
14 from ROOT import TGraphErrors, TSpline3, gDirectory
15 from MuonCalibIdentifier.MuonFixedIdUnpack import MuonFixedIdUnpack
16 
17 def UnpackData(data):
18  if type(data)==str:
19  return data
20  if type(data)==coral.Blob:
21  blob=StringIO.StringIO()
22  for i in range(data.size()):
23  blob.write(chr(data[i]))
24  unpacked=zlib.decompress(blob.getvalue())
25  return unpacked
26 
27 def iov_keygen(iov):
28  return iov[0]*10000000 + iov[1]
29 
30 def DumpFolderSummary(db_string, folder, tag, run=None):
31 
32  try:
33  db=AtlCoolLib.indirectOpen(db_string, readOnly=True, debug=True)
34  except Exception as e:
35  print ('Problem opening database',e)
36  sys.exit(-1)
37 
38 #get folder and tag
39  cool_folder=db.getFolder(folder)
40  cool_tag=ResolveTag(cool_folder, tag)
41 
42  counters={}
43  if run:
44  objs=cool_folder.browseObjects((run<<32), ((run+1)<<32), cool.ChannelSelection.all(), cool_tag)
45  else:
46  objs=cool_folder.browseObjects(0, (999999<<32), cool.ChannelSelection.all(), cool_tag)
47  for obj in objs:
48  file_col=obj.payload()['file']
49  file_items=re.split('[A-Z,a-z]*', file_col)
50  site="default"
51  head_id=-1
52  if len(file_items)==2:
53  try:
54  head_id = int(file_items[1])
55  site = re.sub("[0-9]", "", file_col)
56  except ValueError:
57  site=file_col
58  ts="NOTS"
59  if TimeSlewingApplied(obj):
60  ts="TS"
61  ident=(obj.since()>>32, obj.until()>>32, site, head_id, ts)
62  if ident not in counters:
63  counters[ident] = 0
64  counters[ident] +=1
65  for ident in sorted(counters.keys(), key=iov_keygen):
66  print ("[", ident[0], ",", ident[1], "[", ident[2], ident[3], ident[4], ":" , counters[ident])
67 
68 
69 def ReadRtCool(db_string, folder, tag, run_number):
70 
71  try:
72  db=AtlCoolLib.indirectOpen(db_string, readOnly=True, debug=True)
73  except Exception as e:
74  print ('Problem opening database',e)
75  sys.exit(-1)
76 
77 #get folder and tag
78  cool_folder=db.getFolder(folder)
79  cool_tag=ResolveTag(cool_folder, tag)
80 
81 #data to be filled
82  graphs={}
83  splines={}
84  iovs=set([])
85 
86  myiov=(run_number<<32)
87 
88  objs=cool_folder.browseObjects(myiov,myiov, cool.ChannelSelection.all(), cool_tag)
89  for obj in objs:
90  iov=(obj.since(), obj.until())
91  iovs.add(iov)
92  sp=UnpackData(obj.payload()['data']).split('\n')
93  sp1=sp[0].split(',')
94  chamber=int(sp1[0])
95  n_points=int(sp1[1])
96  sp2=sp[1].split(',')
97  graphs[chamber]=TGraphErrors(n_points)
98  up=MuonFixedIdUnpack(chamber)
99  if up.stationNameString()=='XXX':
100  print ("Invalid station name in ", obj.payload()['file'])
101  sys.exit(1)
102  nm=up.stationNameString() + "_" + str(up.stationPhi()) + "_" + str(up.stationEta())
103 # print (nm)
104  ts_applied=TimeSlewingApplied(obj)
105  for i in range(0, n_points):
106  r=float(sp2[3*i])
107  t=float(sp2[3*i+1])
108  s=float(sp2[3*i+2])
109  if not ts_applied:
110  t_new=NoTs2Ts(r, t)
111  t=t_new
112  graphs[chamber].SetPoint(i, r, t)
113  graphs[chamber].SetPointError(i, 0, s)
114  splines[chamber]=TSpline3("sp_" + nm, graphs[chamber])
115  if gDirectory.IsWritable():
116  splines[chamber].Write("sp_" + nm)
117  graphs[chamber].Write("gr_" + nm)
118 
119  return graphs, splines, iovs
120 
121 
122 def ReadT0Cool(db_string, folder, tag, run_number):
123 
124  try:
125  db=AtlCoolLib.indirectOpen(db_string, readOnly=True, debug=True)
126  except Exception as e:
127  print ('Problem opening database',e)
128  sys.exit(-1)
129 
130 #get folder and tag
131  cool_folder=db.getFolder(folder)
132  cool_tag=ResolveTag(cool_folder, tag)
133  myiov=(run_number<<32)
134 #data
135  t0_values={}
136 
137  objs=cool_folder.browseObjects(myiov,myiov, cool.ChannelSelection.all(), cool_tag)
138 
139  for obj in objs:
140  sp=UnpackData(obj.payload()['data']).split('\n')
141  sp1=sp[0].split(',')
142  ch_sp=sp1[0][2:].split("_")
143  ch_id=(ch_sp[0], int(ch_sp[1]), int(ch_sp[2]))
144  ntubes=int(sp1[-1])
145 
146  t0_items=sp[1].split(",")
147  t0_values[ch_id] = []
148  ts_applied=TimeSlewingApplied(obj)
149  for i in range(0, ntubes):
150  t0=float(t0_items[3*i+0])
151  if not ts_applied:
152  t0+= GasmonDriftTimeOffsetT0
153  t0_values[ch_id].append((t0, int(t0_items[3*i+1]), float(t0_items[3*i+2])))
154 
155  return t0_values
156 
157 
158 def GetFolderTag(db_string, folder):
159  dbSvc=cool.DatabaseSvcFactory.databaseService()
160  try:
161  db=dbSvc.openDatabase(db_string)
162  except Exception as e:
163  print ('Problem opening database',e)
164  sys.exit(-1)
165 
166 #get folder and tag
167  cool_folder=db.getFolder(folder)
168  for x in cool_folder.listTags():
169  return x
170 
DerivationFramework::TriggerMatchingUtils::sorted
std::vector< typename R::value_type > sorted(const R &r, PROJ proj={})
Helper function to create a sorted vector from an unsorted range.
MuonCalibConvertTimeSlewing.NoTs2Ts
def NoTs2Ts(r, t)
Definition: MuonCalibConvertTimeSlewing.py:24
MuonFixedIdUnpack
Definition: MuonFixedIdUnpack.py:1
ReadCool.DumpFolderSummary
def DumpFolderSummary(db_string, folder, tag, run=None)
Definition: MuonSpectrometer/MuonCalib/MuonCalibDbOperations/python/ReadCool.py:30
dumpHVPathFromNtuple.append
bool append
Definition: dumpHVPathFromNtuple.py:91
python.CaloAddPedShiftConfig.type
type
Definition: CaloAddPedShiftConfig.py:42
ReadCool.ReadT0Cool
def ReadT0Cool(db_string, folder, tag, run_number)
Definition: MuonSpectrometer/MuonCalib/MuonCalibDbOperations/python/ReadCool.py:122
ReadCool.UnpackData
def UnpackData(data)
Definition: MuonSpectrometer/MuonCalib/MuonCalibDbOperations/python/ReadCool.py:17
MuonFixedIdUnpack
MuonCalibConvertTimeSlewing.TimeSlewingApplied
def TimeSlewingApplied(obj)
Definition: MuonCalibConvertTimeSlewing.py:27
ReadCool.iov_keygen
def iov_keygen(iov)
Definition: MuonSpectrometer/MuonCalib/MuonCalibDbOperations/python/ReadCool.py:27
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:194
CxxUtils::set
constexpr std::enable_if_t< is_bitmask_v< E >, E & > set(E &lhs, E rhs)
Convenience function to set bits in a class enum bitmask.
Definition: bitmask.h:232
ReadCool.GetFolderTag
def GetFolderTag(db_string, folder)
Definition: MuonSpectrometer/MuonCalib/MuonCalibDbOperations/python/ReadCool.py:158
python.CaloAddPedShiftConfig.int
int
Definition: CaloAddPedShiftConfig.py:45
str
Definition: BTagTrackIpAccessor.cxx:11
ReadCool.ReadRtCool
def ReadRtCool(db_string, folder, tag, run_number)
Definition: MuonSpectrometer/MuonCalib/MuonCalibDbOperations/python/ReadCool.py:69
MuonCalibResolveTag.ResolveTag
def ResolveTag(folder, tag)
Definition: MuonCalibResolveTag.py:4
Trk::split
@ split
Definition: LayerMaterialProperties.h:38
python.LArMinBiasAlgConfig.float
float
Definition: LArMinBiasAlgConfig.py:65