ATLAS Offline Software
Loading...
Searching...
No Matches
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
9import os, sys, getopt, cppyy, logging
10os.environ['TERM'] = 'linux'
11
12def 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
31letters = "L:H:s:D:U:C:f:t:m:r:hzu"
32words = ["lowgain=","highgain=","schema=","dbname=","user=","comment=","folder=","tag=","module=","run=","help","zero","update"]
33
34try:
35 options,args = getopt.getopt(sys.argv[1:],letters,words)
36except 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
47pulseLG = "pulselo_physics.dat"
48pulseHG = "pulsehi_physics.dat"
49
50#=== output file
51dbname = "tileSqlite.db"
52
53#=== folder for pulse in COOL DB
54folder = "/TILE/OFL02/PULSESHAPE/PHY"
55
56#=== tag for pulse in COOL DB
57tag = "RUN2-HLT-UPD1-00"
58
59#=== module name with new pulse
60module = "AUX01"
61
62#=== run number for new IOV
63run = 0
64
65#=== create zero-sized records for all other modules in DB
66zeros = False
67
68#=== update or recreate output DB
69update = False
70
71#=== print help and exit
72help = False
73
74comment = None
75schema = None
76
77try:
78 user=os.getlogin()
79except Exception:
80 user="UnknownUser"
81
82
83#=== read command line parameters
84
85for 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
118part=['AUX','LBA','LBC','EBA','EBC']
119ros=part.index(module[:3])
120drawer=int(module[3:])-1
121
122if schema is None:
123 schema = 'sqlite://;schema=%s;dbname=CONDBR2' % (dbname)
124
125if '/TILE' not in folder:
126 folder='/TILE/OFL02/PULSESHAPE/'+folder
127
128if comment is None:
129 comment = "Pulses from %s %s for module %s" % (pulseLG,pulseHG,module)
130
131
132#=== read low gain pulse shape
133xlo = []
134ylo = []
135lines = open(pulseLG,"r").readlines()
136for 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
152xhi = []
153yhi = []
154lines = open(pulseHG,"r").readlines()
155for 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
171vecLo = cppyy.gbl.std.vector('float')()
172for x in xlo:
173 vecLo.push_back(x)
174for y in ylo:
175 vecLo.push_back(y)
176vecHi = cppyy.gbl.std.vector('float')()
177for x in xhi:
178 vecHi.push_back(x)
179for y in yhi:
180 vecHi.push_back(y)
181newPulse = cppyy.gbl.std.vector('std::vector<float>')()
182newPulse.push_back(vecLo)
183newPulse.push_back(vecHi)
184
185
186#=== write pulse shapes to COOL DB
187from TileCalibBlobPython import TileCalibTools
188from TileCalibBlobObjs.Classes import TileCalibUtils
189from TileCalibBlobPython.TileCalibTools import MAXRUN, MAXLBK
190
191#=== open the database
192db = TileCalibTools.openDbConn(schema,('UPDATE' if update else 'RECREATE'))
193blobWriter = TileCalibTools.TileBlobWriter(db,folder,'Flt')
194blobWriter.setLogLvl(logging.DEBUG)
195
196#=== create zero-sized blobs for all drawers
197if 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
204det = blobWriter.getDrawer(ros,drawer)
205det.init(newPulse,1,200)
206
207blobWriter.setComment(user,comment)
208folderTag = TileCalibUtils.getFullTag(folder, tag)
209blobWriter.register((run,0),(MAXRUN,MAXLBK),folderTag)
210
211#=== close the database connection
212db.closeDatabase()
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.
std::vector< std::string > split(const std::string &s, const std::string &t=":")
Definition hcg.cxx:177