10 __author__=
"Pavol Strizenec <pavol@cern.ch>, based on example from Walter Lampl"
11 __doc__ =
" An athena algorithm to merge two LAr blobs"
13 from AthenaPython.PyAthena
import StatusCode
14 import AthenaPython.PyAthena
as PyAthena
18 from ROOT
import Identifier, IdentifierHash
20 from PyCool
import cool
22 from array
import array
27 ''' Get the length of rows in the db, assuming that the number of entries will be divisible by some number of cells / SCs / other DB objects '''
28 factor_cells = thelen/ 195072.
29 factor_SC = thelen/34048.
30 factor_LATOME = thelen/116.
32 if factor_cells.is_integer():
33 factor =
int(factor_cells)
34 if factor_SC.is_integer():
35 factor =
int(factor_SC)
36 elif factor_LATOME.is_integer():
37 factor =
int(factor_LATOME)
42 for k
in payload.keys():
44 if isinstance(payload[k], cppyy.gbl.coral.Blob):
46 if verbose:
print(f
"{k} consists of {factor} item(s) per row")
47 contents[k][
"payload"] = payload[k]
48 contents[k][
"size"] = factor
50 contents[k][
"payload"] = payload[k]
51 contents[k][
"size"] =
None
54 def patchChannel(idhelper, channel, patchdet, patchFT, patchChan, verbose=False ):
55 chid = idhelper.channel_Id(IdentifierHash(channel))
56 chidval = chid.get_identifier32().get_compact()
57 name = idhelper.channel_name(chid)
58 FT = idhelper.feedthrough_name(chid)
61 if 0
in patchdet
and idhelper.isEMBchannel(chid):
62 if verbose:
print(name, chidval,
"is in EMB")
64 if 1
in patchdet
and idhelper.isEMECOW(chid):
65 if verbose:
print(name, chidval,
"is in EMECOW")
67 if 2
in patchdet
and idhelper.isEMECIW(chid):
68 if verbose:
print(name, chidval,
"is in EMECIW")
70 if 3
in patchdet
and idhelper.isHECchannel(chid):
71 if verbose:
print(name, chidval,
"is in HEC")
73 if 4
in patchdet
and idhelper.isFCALchannel(chid):
74 if verbose:
print(name, chidval,
"is in FCAL")
78 if verbose:
print(name, chidval,
"is in FT", FT)
80 if len(patchChan) > 0:
81 if chidval
in patchChan:
82 if verbose:
print(name, chidval,
"is in list of channels")
91 def __init__(self, name="LArBlobMergeAlg", **kw):
94 super(LArBlobMergeAlg,self).
__init__(**kw)
96 self.
indb=kw.get(
'inputdb',
"")
98 self.
outdb=kw.get(
'outputdb',
"")
100 self.
run=kw.get(
'runnum',999999)
107 self.
msg.
info(f
"indb: {self.indb}, patchdb:{self.patchdb}, patchdet: {self.patchdet}, patchFT: {self.patchFT}, patchChan: {self.patchChan}, outdb: {self.outdb}, folder: {self.folder}, run: {self.run}, issc: {self.issc}")
119 self.
_detStore = PyAthena.py_svc(
'StoreGateSvc/DetectorStore')
121 self.
msg.
error(
"Failed to get DetectorStore")
122 return StatusCode.Failure
124 self.
_condStore = PyAthena.py_svc(
'StoreGateSvc/ConditionStore')
126 self.
msg.
error(
"Failed to get ConditionStore")
127 return StatusCode.Failure
135 self.
msg.
error(
"Failed to get LArOnlineID")
136 return StatusCode.Failure
146 return StatusCode.Success
150 eid=ROOT.Gaudi.Hive.currentContext().eventID()
159 print(
"ERROR, failed to get LArCabling")
160 return StatusCode.Failure
166 return StatusCode.Success
171 return StatusCode.Success
176 return StatusCode.Success
180 from CoolConvUtilities.AtlCoolLib
import indirectOpen
185 self.
msg.
error(f
"Cannot connect to database {self.indb}")
186 raise RuntimeError(f
"ERROR: Cannot connect to database {self.indb}")
191 folder=inconn.getFolder(self.
folder)
192 runiov=self.
run << 32
193 obj=folder.findObject(runiov,0)
194 payload=obj.payload()
198 except Exception
as e:
199 self.
msg.warning(f
"Could not decode {self.folder} from {self.indb}\n{e}")
200 return StatusCode.Failure
207 if (inconn2
is None):
208 self.
msg.
error(f
"Cannot connect to database {self.patchdb}")
209 raise RuntimeError(f
"ERROR: Cannot connect to database {self.patchdb}")
212 folder=inconn2.getFolder(self.
folder)
213 runiov=self.
run << 32
214 obj=folder.findObject(runiov,0)
215 payload=obj.payload()
218 self.
msg.warning(f
"Could not decode {self.folder} from {self.patchdb}")
219 return StatusCode.Failure
222 dbSvc = cool.DatabaseSvcFactory.databaseService()
224 outconn = dbSvc.openDatabase(self.
outdb,
False)
225 outfolder = outconn.getFolder(self.
folder)
228 outconn = dbSvc.createDatabase(self.
outdb)
229 folder = inconn.getFolder(self.
folder)
230 fspec = folder.folderSpecification()
231 from CaloCondBlobAlgs
import CaloCondTools
232 desc = CaloCondTools.getAthenaFolderDescr()
233 outfolder = outconn.createFolder(self.
folder, fspec, desc,
True)
236 spec = cool.RecordSpecification()
238 for k
in contents.keys():
239 if isinstance(contents[k][
"payload"], cppyy.gbl.coral.Blob):
240 spec.extend( k, cool.StorageType.Blob16M )
241 elif isinstance(contents[k][
"payload"], int):
242 spec.extend( k, cool.StorageType.UInt32 )
244 self.
msg.
error(f
"CANNOT WORK OUT THE DATATYPE OF {k}: {contents[k]} FOR STORAGE")
245 return StatusCode.Failure
247 data = cool.Record( spec )
249 hash_max = self.
onlineID.channelHashMax()
252 btype=getattr(ROOT,
"coral::Blob")
255 blobvals = [ k
for k
in contents.keys()
if isinstance( contents[k][
"payload"], cppyy.gbl.coral.Blob) ]
256 blobvals2 = [ k
for k
in contents2.keys()
if isinstance( contents2[k][
"payload"], cppyy.gbl.coral.Blob) ]
258 if len(blobvals) != len(blobvals2):
259 self.
msg.
error(
"The same blob payloads are not present in the two dbs!\n(1) {blobvals}\n(2) {blobvals2}")
260 return StatusCode.Failure
263 vecLength = contents[bv][
"size"]
264 blobsize = vecLength*hash_max*fsize
265 outblob[bv] = btype()
266 outblob[bv].resize(blobsize)
268 if blobsize != len(contents[bv][
"payload"]):
269 self.
msg.
error(f
'Wrong size {len(contents[bv]["payload"])} of the input blob, should be {blobsize}')
270 return StatusCode.Failure
272 vVec[bv] = ROOT.std.vector(
'float')(vecLength*hash_max)
274 blobdata = contents[bv][
"payload"].
read()
276 for i
in range(0,hash_max):
278 for iv
in range(0, vecLength):
279 vVec[bv][i*vecLength + iv] =
float(struct.unpack(
'f',blobdata[(i*vecLength + iv)*fsize:((i*vecLength+iv)*fsize+fsize)])[0])
281 vVec[bv][i] =
float(struct.unpack(
'f',blobdata[i*fsize:(i*fsize+fsize)])[0])
283 if blobsize != len(contents2[bv][
"payload"]):
284 self.
msg.
error(f
'Wrong size {len(contents2[bv]["payload"])} of the input blob, should be {blobsize}')
285 return StatusCode.Failure
288 blobdata2 = contents2[bv][
"payload"].
read()
289 vecLength2 = contents2[bv][
"size"]
290 if vecLength != vecLength2:
291 self.
msg.
error(f
"Item {bv} has a different shape in the base and patch dbs! {vecLength} & {vecLength2}")
292 return StatusCode.Failure
295 for i
in range(0,hash_max):
299 for iv
in range(0, vecLength):
300 vVec[bv][i*vecLength + iv] =
float(struct.unpack(
'f',blobdata2[(i*vecLength + iv)*fsize:((i*vecLength+iv)*fsize+fsize)])[0])
302 vVec[bv][i] =
float(struct.unpack(
'f', blobdata2[i*fsize:(i*fsize+fsize)])[0])
305 writable_buf = { k:
array(
'f')
for k
in vVec.keys() }
307 for key
in vVec.keys():
309 writable_buf[key].
append(vVec[key][i])
311 buff[key] = writable_buf[key].tobytes()
313 for j
in range(0, len(buff[key])):
314 outblob[key][j] = buff[key][j]
316 data[key] = outblob[key]
318 tofill = [ k
for k
in contents.keys()
if k
not in outblob.keys() ]
321 if contents[tf][
"payload"] != contents2[tf][
"payload"]:
322 self.
msg.warning(f
'Setting {tf} to {contents[tf]["payload"]}, but beware: the value is {contents2[tf]["payload"]} in the patching db')
323 data[tf] = contents[tf][
"payload"]
326 outfolder.storeObject(self.
iovSince, self.
iovUntil, data, cool.ChannelId(0))
329 outconn.closeDatabase()
331 return StatusCode.Success