ATLAS Offline Software
LArHVMapDbFiller.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
2 
3 # @author: G.Unal
4 # @date: July 2008
5 #
6 #
7 
8 from __future__ import print_function
9 
10 __version__ = "$Id: LArHVMapDbFiller.py"
11 __author__ = "G.Unal"
12 
13 import sys, os
14 from PyCool import cool
15 import six
16 
17 class LArHVMapDbFillerError(Exception):
18  def __init__(self, value):
19  self.value = value
20  def __str__(self):
21  return repr(self.value)
22 
23 
25  """
26  A simple class used to fill SQLite db file with parameters for simulation
27  and/or digitization.
28  """
29 
30  def __init__(self):
31  object.__init__(self)
32 
33  self.beginTime = None
34  self.endTime = None
35  self.FileName = None
36  self.FolderTag = None
37  self.Params = {}
38  return
39 
40  #
41  def setBegin(self,run,lumi):
42  print (" setBegin run,lumi ",run)
43  self.beginTime = (int(run) << 32) + int(lumi)
44  print (" time ",self.beginTime)
45  return
46 
47  def setEnd(self,run,lumi):
48  print ("setEnd runmlumi ",run)
49  self.endTime = (int(run) << 32) + int(lumi)
50  print (" time ",self.endTime)
51  return
52 
53  def setFileName(self, fileName):
54  self.FileName = fileName
55  return
56 
57  def setFolderTag(self,folderTag):
58  self.FolderTag = folderTag
59  return
60 
61  def readFile(self):
62  value = ''
63  f = open(self.FileName,'r')
64  for lines in f:
65  value += lines
66  self.Params['LArHVMap'] = value
67  return
68 
69  def genDb(self):
70 
71  dbFileName = 'HVmap.db'
72  dbName = 'COMP200'
73  folderPath = '/LAR/IdentifierOfl/HVLineToElectrodeMap'
74  # Do checks
75  if self.beginTime is None:
76  raise LArHVMapDbFillerError ('Must set begin run number before generating db')
77  if self.endTime is None:
78  self.endTime = cool.ValidityKeyMax
79  if self.FileName is None:
80  raise LArHVMapDbFillerError ('Must give an input file for LArHVToElectrode.data')
81  if self.FolderTag is None:
82  raise LArHVMapDbFillerError ('Must give a folder tag')
83 
84  self.readFile()
85 
86  if len(self.Params) == 0:
87  raise LArHVMapDbFillerError ('No parameters for db ' + dbName)
88 
89  # remove existing db, if any
90  try:
91  os.remove(dbFileName)
92  print ("LArHVMapDbFiller.genDb: Removed db", dbFileName)
93  except OSError:
94  pass
95 
96  # get database service and open database
97  dbSvc=cool.DatabaseSvcFactory.databaseService()
98 
99  # build dbstring - database accessed via physical name
100  dbstring="sqlite://;schema=" + dbFileName + ";dbname=" + dbName
101  try:
102  db=dbSvc.createDatabase(dbstring)
103  except Exception as e:
104  print ('LArHVMapDbFiller.genDb: Problem creating database',e)
105  sys.exit(-1)
106  print ("LArHVMapDbFiller.genDb: Created database",dbstring)
107 
108  # setup a folder payload specification
109  spec=cool.RecordSpecification()
110  for key in self.Params:
111  spec.extend(key, cool.StorageType.String16M)
112 
113  # folder meta-data - note for Athena this has a special meaning
114  desc='<timeStamp>run-lumi</timeStamp><addrHeader><address_header service_type="71" clid="40774348" /></addrHeader><typeName>AthenaAttributeList</typeName>'
115  # create the folder - single version
116  # last argument is createParents - if true, automatically creates parent
117  # folders if needed
118  # note this will not work if the database already exists - delete mycool.db first
119  myfolder=db.createFolder(folderPath, spec, desc, cool.FolderVersioning.MULTI_VERSION,True)
120  # now fill in simlation parameters
121  data=cool.Record(spec)
122  for k, v in six.iteritems (self.Params):
123  data[k] = v
124 
125  print ("LArHVMapDbFiller.genDb: Recording parameters", data)
126 
127  # store object with IOV valid from 0-10, channel 3
128  myfolder.storeObject(self.beginTime, self.endTime, data, 0, self.FolderTag)
129  print ("LArHVMapDbFiller.genDb: Stored object")
130 
131  # finalize
132  db.closeDatabase()
133 
134  # Try to dump out db as test
135  self.dumpDb(dbstring)
136 
137  def dumpDb(self, dbstring):
138 
139  print ("LArHVMapDbFiller.dumpDb: Dumping database:", dbstring)
140  # get database service and open database
141  dbSvc = cool.DatabaseSvcFactory.databaseService()
142  # database accessed via physical name
143  try:
144  db = dbSvc.openDatabase(dbstring,False)
145  except Exception as e:
146  print ('LArHVMapDbFiller.dumpDb: Problem opening database',e)
147  sys.exit(-1)
148 
149  # Loop over folders
150  folders = db.listAllNodes()
151  for ff in folders:
152  # Get Folder
153  try:
154  f = db.getFolder(ff)
155  print ("LArHVMapDbFiller.dumpDb: Dumping folder " + str(ff))
156  except Exception:
157  #print ("Skipping " + str(ff))
158  continue
159 
160  # get tags
161  # tags = f.listTags()
162  # print ("for tags ",)
163  # for tag in tags: print (tag)
164 
165  # for tag in tags:
166 
167  nobjs = f.countObjects( cool.ValidityKeyMin,
168  cool.ValidityKeyMax,
169  cool.ChannelSelection.all())
170 
171  print ("LArHVMapDbFiller.dumpDb: number of IOV payloads", nobjs)
172 
173  objs = f.browseObjects( cool.ValidityKeyMin,
174  cool.ValidityKeyMax,
175  cool.ChannelSelection.all())
176  i = 0
177  while objs.goToNext():
178  obj = objs.currentRef()
179  print ("LArHVMapDbFiller.dumpDb: Payload", i,)
180  print ("since [r,l]: [", obj.since() ,']',)
181  print ("until [r,l]: [", obj.until() ,']',)
182  print ("payload", obj.payload(),)
183  print ("chan",obj.channelId() )
184  i += 1
185 
186  objs.close()
187 
188  # finish
189  db.closeDatabase()
LArHVMapDbFiller.LArHVMapDbFiller.readFile
def readFile(self)
Definition: LArHVMapDbFiller.py:61
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
LArHVMapDbFiller.LArHVMapDbFiller.FolderTag
FolderTag
Definition: LArHVMapDbFiller.py:36
LArHVMapDbFiller.LArHVMapDbFillerError.value
value
Definition: LArHVMapDbFiller.py:19
LArHVMapDbFiller.LArHVMapDbFiller.__init__
def __init__(self)
Definition: LArHVMapDbFiller.py:30
LArHVMapDbFiller.LArHVMapDbFillerError.__init__
def __init__(self, value)
Definition: LArHVMapDbFiller.py:18
LArHVMapDbFiller.LArHVMapDbFiller.setFolderTag
def setFolderTag(self, folderTag)
Definition: LArHVMapDbFiller.py:57
LArHVMapDbFiller.LArHVMapDbFiller.setFileName
def setFileName(self, fileName)
Definition: LArHVMapDbFiller.py:53
LArHVMapDbFiller.LArHVMapDbFiller.setBegin
def setBegin(self, run, lumi)
Definition: LArHVMapDbFiller.py:41
LArHVMapDbFiller.LArHVMapDbFiller.genDb
def genDb(self)
Definition: LArHVMapDbFiller.py:69
LArHVMapDbFiller.LArHVMapDbFiller.dumpDb
def dumpDb(self, dbstring)
Definition: LArHVMapDbFiller.py:137
LArHVMapDbFiller.LArHVMapDbFiller
Definition: LArHVMapDbFiller.py:24
PyAthena::repr
std::string repr(PyObject *o)
returns the string representation of a python object equivalent of calling repr(o) in python
Definition: PyAthenaUtils.cxx:106
LArHVMapDbFiller.LArHVMapDbFiller.beginTime
beginTime
Definition: LArHVMapDbFiller.py:33
LArHVMapDbFiller.LArHVMapDbFiller.Params
Params
Definition: LArHVMapDbFiller.py:37
LArHVMapDbFiller.LArHVMapDbFiller.FileName
FileName
Definition: LArHVMapDbFiller.py:35
LArHVMapDbFiller.LArHVMapDbFiller.setEnd
def setEnd(self, run, lumi)
Definition: LArHVMapDbFiller.py:47
Trk::open
@ open
Definition: BinningType.h:40
pickleTool.object
object
Definition: pickleTool.py:30
str
Definition: BTagTrackIpAccessor.cxx:11
LArHVMapDbFiller.LArHVMapDbFillerError
Definition: LArHVMapDbFiller.py:17
LArHVMapDbFiller.LArHVMapDbFillerError.__str__
def __str__(self)
Definition: LArHVMapDbFiller.py:20
LArHVMapDbFiller.LArHVMapDbFiller.endTime
endTime
Definition: LArHVMapDbFiller.py:34