ATLAS Offline Software
WritePulseShapeToCool.py
Go to the documentation of this file.
1 #!/bin/env python
2 #
3 # Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
4 #
5 # WritePulseShapeToCool.py
6 # Sanya Solodkov 2023-02-23
7 #
8 
9 import os, sys, getopt, cppyy, logging
10 os.environ['TERM'] = 'linux'
11 
12 def usage():
13  print ("How to use: ",sys.argv[0]," [OPTION] ... ")
14  print ("Write pulse shapes to COOL")
15  print ("")
16  print ("-h, --help shows this help")
17  print ("-f, --folder= specify folder to use f.i. /TILE/OFL02/PULSESHAPE/PHY or only PHY")
18  print ("-t, --tag= specify tag to use, f.i. RUN2-HLT-UPD1-00")
19  print ("-r, --run= specify run number, default is 0")
20  print ("-m, --module= specify module name for which pulse shape will be written, default is AUX01")
21  print ("-L, --lowgain= specify the text file with new pulse shape for low gain")
22  print ("-H, --highgain= specify the text file with new pulse shape for high gain")
23  print ("-s, --schema= specify schema to use, f.i. 'sqlite://;schema=tileSqlite.db;dbname=CONDBR2'")
24  print ("-D, --dbname= specify dbname part of schema if schema only contains file name, default is tileSqlite.db")
25  print ("-U, --user= specify username for comment")
26  print ("-C, --comment= specify comment which should be written to DB")
27  print ("-z, --zero if present, means that zero-sized blob is written for missing drawers")
28  print ("-u --update set this flag if output sqlite file should be updated, otherwise it'll be recreated")
29 
30 
31 letters = "L:H:s:D:U:C:f:t:m:r:hzu"
32 words = ["lowgain=","highgain=","schema=","dbname=","user=","comment=","folder=","tag=","module=","run=","help","zero","update"]
33 
34 try:
35  options,args = getopt.getopt(sys.argv[1:],letters,words)
36 except getopt.GetoptError as err:
37  print ()
38  print (str(err))
39  print ()
40  usage()
41  sys.exit(2)
42 
43 
44 #=== all defaults
45 
46 #=== input files
47 pulseLG = "pulselo_physics.dat"
48 pulseHG = "pulsehi_physics.dat"
49 
50 #=== output file
51 dbname = "tileSqlite.db"
52 
53 #=== folder for pulse in COOL DB
54 folder = "/TILE/OFL02/PULSESHAPE/PHY"
55 
56 #=== tag for pulse in COOL DB
57 tag = "RUN2-HLT-UPD1-00"
58 
59 #=== module name with new pulse
60 module = "AUX01"
61 
62 #=== run number for new IOV
63 run = 0
64 
65 #=== create zero-sized records for all other modules in DB
66 zeros = False
67 
68 #=== update or recreate output DB
69 update = False
70 
71 #=== print help and exit
72 help = False
73 
74 comment = None
75 schema = None
76 
77 try:
78  user=os.getlogin()
79 except Exception:
80  user="UnknownUser"
81 
82 
83 #=== read command line parameters
84 
85 for o, a in options:
86  a = a.strip()
87  if o in ("-h","--help"):
88  usage()
89  sys.exit(2)
90  elif o in ("-L","--lowgain"):
91  pulseLG = a
92  elif o in ("-H","--highgain"):
93  pulseHG = a
94  elif o in ("-s","--schema"):
95  schema = a
96  elif o in ("-D","--dbname"):
97  dbname = a
98  elif o in ("-f","--folder"):
99  folder = a
100  elif o in ("-t","--tag"):
101  tag = a
102  elif o in ("-m","--module"):
103  module = a
104  elif o in ("-U","--user"):
105  user = a
106  elif o in ("-C","--comment"):
107  comment = a
108  elif o in ("-r","--run"):
109  run = int(a)
110  elif o in ("-z","--zero"):
111  zeros = True
112  elif o in ("-u","--update"):
113  update = True
114  else:
115  raise RuntimeError("unhandled option")
116 
117 
118 part=['AUX','LBA','LBC','EBA','EBC']
119 ros=part.index(module[:3])
120 drawer=int(module[3:])-1
121 
122 if schema is None:
123  schema = 'sqlite://;schema=%s;dbname=CONDBR2' % (dbname)
124 
125 if '/TILE' not in folder:
126  folder='/TILE/OFL02/PULSESHAPE/'+folder
127 
128 if comment is None:
129  comment = "Pulses from %s %s for module %s" % (pulseLG,pulseHG,module)
130 
131 
132 #=== read low gain pulse shape
133 xlo = []
134 ylo = []
135 lines = open(pulseLG,"r").readlines()
136 for line in lines:
137  fields = line.strip().split()
138  #=== ignore empty and comment lines
139  if not len(fields) :
140  continue
141  if fields[0].startswith("#"):
142  continue
143  if fields[0].startswith("*"):
144  continue
145  if len(fields) != 2 :
146  continue
147 
148  xlo.append(float(fields[0]))
149  ylo.append(float(fields[1]))
150 
151 #=== read high gain pulse shape
152 xhi = []
153 yhi = []
154 lines = open(pulseHG,"r").readlines()
155 for line in lines:
156  fields = line.strip().split()
157  #=== ignore empty and comment lines
158  if not len(fields) :
159  continue
160  if fields[0].startswith("#"):
161  continue
162  if fields[0].startswith("*"):
163  continue
164  if len(fields) != 2 :
165  continue
166 
167  xhi.append(float(fields[0]))
168  yhi.append(float(fields[1]))
169 
170 #=== build pulseshape vectors for db
171 vecLo = cppyy.gbl.std.vector('float')()
172 for x in xlo:
173  vecLo.push_back(x)
174 for y in ylo:
175  vecLo.push_back(y)
176 vecHi = cppyy.gbl.std.vector('float')()
177 for x in xhi:
178  vecHi.push_back(x)
179 for y in yhi:
180  vecHi.push_back(y)
181 newPulse = cppyy.gbl.std.vector('std::vector<float>')()
182 newPulse.push_back(vecLo)
183 newPulse.push_back(vecHi)
184 
185 
186 #=== write pulse shapes to COOL DB
187 from TileCalibBlobPython import TileCalibTools
188 from TileCalibBlobObjs.Classes import TileCalibUtils
189 from TileCalibBlobPython.TileCalibTools import MINRUN, MINLBK, MAXRUN, MAXLBK
190 
191 #=== open the database
192 db = TileCalibTools.openDbConn(schema,('UPDATE' if update else 'RECREATE'))
193 blobWriter = TileCalibTools.TileBlobWriter(db,folder,'Flt')
194 blobWriter.setLogLvl(logging.DEBUG)
195 
196 #=== create zero-sized blobs for all drawers
197 if zeros:
198  util = cppyy.gbl.TileCalibUtils()
199  for r in range(util.max_ros()):
200  for d in range(util.getMaxDrawer(r)):
201  blobWriter.zeroBlob(r,d)
202 
203 #=== write pulse shape to one drawer
204 det = blobWriter.getDrawer(ros,drawer)
205 det.init(newPulse,1,200)
206 
207 blobWriter.setComment(user,comment)
208 folderTag = TileCalibUtils.getFullTag(folder, tag)
209 blobWriter.register((run,0),(MAXRUN,MAXLBK),folderTag)
210 
211 #=== close the database connection
212 db.closeDatabase()
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:195
WritePulseShapeToCool.usage
def usage()
Definition: WritePulseShapeToCool.py:12
Trk::open
@ open
Definition: BinningType.h:40
readCCLHist.int
int
Definition: readCCLHist.py:84
str
Definition: BTagTrackIpAccessor.cxx:11
readCCLHist.float
float
Definition: readCCLHist.py:83
Trk::split
@ split
Definition: LayerMaterialProperties.h:38
TileCalibUtils::getFullTag
static std::string getFullTag(const std::string &folder, const std::string &tag)
Returns the full tag string, composed of camelized folder name and tag part.
Definition: TileCalibUtils.cxx:33