ATLAS Offline Software
ParameterDbFiller.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
2 
3 # @author: RD Schaffer <R.D.Schaffer@cern.ch>
4 # @date: May 2008
5 #
6 #
7 
8 from __future__ import print_function
9 
10 __version__ = "$Id: ParameterDbFiller.py,v 1.3 2008-11-13 12:25:23 schaffer Exp $"
11 __author__ = "RD Schaffer <R.D.Schaffer@cern.ch>"
12 
13 import sys
14 import os
15 import collections
16 import six
17 from PyCool import cool
18 
19 class ParameterDbFillerError(Exception):
20  def __init__(self, value):
21  self.value = value
22  def __str__(self):
23  return repr(self.value)
24 
25 
27  """
28  A simple class used to fill SQLite db file with parameters for simulation
29  and/or digitization.
30  """
31 
32  def __init__(self):
33  object.__init__(self)
34 
35  self.beginRun = None
36  self.endRun = None
37  self.simParams = collections.OrderedDict()
38  self.digitParams = collections.OrderedDict()
39  self.digitParams64 = collections.OrderedDict()
40  return
41 
42  #
43  def setBeginRun(self, beginRun):
44  self.beginRun = beginRun << 32
45  return
46 
47  def setEndRun(self, endRun):
48  self.endRun = endRun << 32
49  return
50 
51  def addSimParam(self, name, value):
52  self.simParams[name] = value
53  return
54 
55  def addDigitParam(self, name, value):
56  self.digitParams[name] = value
57  return
58 
59  def addDigitParam64(self, name, value):
60  self.digitParams64[name] = value
61  return
62 
63  def genSimDb(self, dbFileName = None):
64  # Allow to define specific file name, otherwise use
65  if dbFileName is None:
66  # Set to default value
67  dbFileName = "SimParams.db"
68 
69  # Generate db: args - file name, dbname, params, folder path
70  self.genDb(dbFileName, 'SIMPARAM', self.simParams, '/Simulation/Parameters')
71 
72 
73  def genDigitDb(self, dbFileName = None):
74  # Allow to define specific file name, otherwise use
75  if dbFileName is None:
76  # Set to default value
77  dbFileName = "DigitParams.db"
78 
79  # Generate db: args - file name, dbname, params, folder path
80  self.genDb(dbFileName, 'DIGPARAM', self.digitParams, '/Digitization/Parameters', self.digitParams64)
81 
82 
83  def genDb(self, dbFileName, dbName, params, folderPath, params64 = {}):
84 
85  # Do checks
86  if self.beginRun is None:
87  raise ParameterDbFillerError ('Must set begin run number before generating db')
88  if self.endRun is None:
89  raise ParameterDbFillerError ('Must set end run number before generating db')
90  if len(params) == 0:
91  raise ParameterDbFillerError ('No parameters for db ' + dbName)
92 
93  # remove existing db, if any
94  try:
95  os.remove(dbFileName)
96  print ("ParameterDbFiller.genDb: Removed db", dbFileName)
97  except Exception:
98  pass
99 
100  # get database service and open database
101  dbSvc=cool.DatabaseSvcFactory.databaseService()
102 
103  # build dbstring - database accessed via physical name
104  dbstring="sqlite://;schema=" + dbFileName + ";dbname=" + dbName
105  try:
106  db=dbSvc.createDatabase(dbstring)
107  except Exception as e:
108  print ('ParameterDbFiller.genDb: Problem creating database',e)
109  sys.exit(-1)
110  print ("ParameterDbFiller.genDb: Created database",dbstring)
111 
112  # setup a folder payload specification
113  spec=cool.RecordSpecification()
114  for key in params:
115  spec.extend(key, cool.StorageType.String4k)
116  pass
117  # add in spec for long strings
118  for key in params64:
119  spec.extend(key, cool.StorageType.String64k)
120 
121  # folder meta-data - note for Athena this has a special meaning
122  desc = '<timeStamp>run-event</timeStamp><addrHeader><address_header service_type="71" clid="40774348" /></addrHeader><typeName>AthenaAttributeList</typeName>'
123  # create the folder - single version
124  # last argument is createParents - if true, automatically creates parent
125  # folders if needed
126  # note this will not work if the database already exists - delete mycool.db first
127  # myfolder=db.createFolder(folderPath, spec, desc, cool.FolderVersioning.SINGLE_VERSION,True)
128  folderSpec = cool.FolderSpecification(cool.FolderVersioning.SINGLE_VERSION, spec)
129  myfolder = db.createFolder(folderPath, folderSpec, desc, True)
130 
131  # now fill in parameters
132  data = cool.Record(spec)
133  for k, v in six.iteritems(params):
134  data[k] = v
135  for k, v in six.iteritems(params64):
136  data[k] = v
137 
138  print ("ParameterDbFiller.genDb: Recording parameters", data)
139 
140  # store object with IOV valid from 0-10, channel 3
141  myfolder.storeObject(self.beginRun, self.endRun, data, 0)
142  print ("ParameterDbFiller.genDb: Stored object")
143 
144  # finalize
145  db.closeDatabase()
146 
147  # Try to dump out db as test
148  self.dumpDb(dbstring)
149 
150  def dumpDb(self, dbstring):
151 
152  print ("ParameterDbFiller.dumpDb: Dumping database:", dbstring)
153  # get database service and open database
154  dbSvc = cool.DatabaseSvcFactory.databaseService()
155  # database accessed via physical name
156  try:
157  db = dbSvc.openDatabase(dbstring,False)
158  except Exception as e:
159  print ('ParameterDbFiller.dumpDb: Problem opening database',e)
160  sys.exit(-1)
161 
162  # Loop over folders
163  folders = db.listAllNodes()
164  for ff in folders:
165  # Get Folder
166  if (not db.existsFolder(ff)):
167  continue
168  try:
169  f = db.getFolder(ff)
170  print ("ParameterDbFiller.dumpDb: Dumping folder " + str(ff))
171  except Exception:
172  #print ("Skipping " + str(ff))
173  continue
174 
175  # get tags
176  # tags = f.listTags()
177  # print ("for tags ", end='')
178  # for tag in tags: print (tag)
179 
180  # for tag in tags:
181 
182  nobjs = f.countObjects( cool.ValidityKeyMin,
183  cool.ValidityKeyMax,
184  cool.ChannelSelection.all())
185 
186  print ("ParameterDbFiller.dumpDb: number of IOV payloads", nobjs)
187 
188  objs = f.browseObjects( cool.ValidityKeyMin,
189  cool.ValidityKeyMax,
190  cool.ChannelSelection.all())
191  i = 0
192  while objs.goToNext():
193  obj = objs.currentRef()
194  print ("ParameterDbFiller.dumpDb: Payload", i, end='')
195  print ("since [r,l]: [", obj.since() >> 32,',',obj.since()%0x100000000,']', end='')
196  print ("until [r,l]: [", obj.until() >> 32,',',obj.until()%0x100000000,']', end='')
197  print ("payload", obj.payload(), end='')
198  print ("chan",obj.channelId() )
199  i += 1
200 
201  objs.close()
202 
203  # finish
204  db.closeDatabase()
python.ParameterDbFiller.ParameterDbFiller.genDigitDb
def genDigitDb(self, dbFileName=None)
Definition: ParameterDbFiller.py:73
python.ParameterDbFiller.ParameterDbFiller
Definition: ParameterDbFiller.py:26
python.ParameterDbFiller.ParameterDbFiller.__init__
def __init__(self)
Definition: ParameterDbFiller.py:32
python.ParameterDbFiller.ParameterDbFiller.beginRun
beginRun
Definition: ParameterDbFiller.py:35
python.ParameterDbFiller.ParameterDbFiller.digitParams
digitParams
Definition: ParameterDbFiller.py:38
python.ParameterDbFiller.ParameterDbFiller.addDigitParam
def addDigitParam(self, name, value)
Definition: ParameterDbFiller.py:55
python.ParameterDbFiller.ParameterDbFillerError.__str__
def __str__(self)
Definition: ParameterDbFiller.py:22
python.ParameterDbFiller.ParameterDbFiller.genDb
def genDb(self, dbFileName, dbName, params, folderPath, params64={})
Definition: ParameterDbFiller.py:83
python.ParameterDbFiller.ParameterDbFiller.addDigitParam64
def addDigitParam64(self, name, value)
Definition: ParameterDbFiller.py:59
python.ParameterDbFiller.ParameterDbFillerError.value
value
Definition: ParameterDbFiller.py:21
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
python.ParameterDbFiller.ParameterDbFiller.genSimDb
def genSimDb(self, dbFileName=None)
Definition: ParameterDbFiller.py:63
python.ParameterDbFiller.ParameterDbFiller.addSimParam
def addSimParam(self, name, value)
Definition: ParameterDbFiller.py:51
python.ParameterDbFiller.ParameterDbFiller.simParams
simParams
Definition: ParameterDbFiller.py:37
python.ParameterDbFiller.ParameterDbFiller.setEndRun
def setEndRun(self, endRun)
Definition: ParameterDbFiller.py:47
python.ParameterDbFiller.ParameterDbFillerError.__init__
def __init__(self, value)
Definition: ParameterDbFiller.py:20
python.ParameterDbFiller.ParameterDbFiller.setBeginRun
def setBeginRun(self, beginRun)
Definition: ParameterDbFiller.py:43
python.ParameterDbFiller.ParameterDbFiller.digitParams64
digitParams64
Definition: ParameterDbFiller.py:39
python.ParameterDbFiller.ParameterDbFiller.dumpDb
def dumpDb(self, dbstring)
Definition: ParameterDbFiller.py:150
pickleTool.object
object
Definition: pickleTool.py:30
str
Definition: BTagTrackIpAccessor.cxx:11
python.ParameterDbFiller.ParameterDbFillerError
Definition: ParameterDbFiller.py:19
python.ParameterDbFiller.ParameterDbFiller.endRun
endRun
Definition: ParameterDbFiller.py:36