ATLAS Offline Software
Classes | Functions | Variables
python.db Namespace Reference

Classes

class  Databases
 
class  DefectsDB
 

Functions

def make_safe_fields (fields)
 
def get_query_range (since, until, runs)
 
def fetch_iovs (folder_name, since=None, until=None, channels=None, tag="", what="all", max_records=-1, with_channel=True, loud=False, database=None, convert_time=False, named_channels=False, selection=None, runs=None, with_time=False, unicode_strings=False)
 
def write_iovs (folder_name, iovs, record, multiversion=True, tag="", create=False, storage_buffer=False)
 

Variables

 log
 
string DEFAULT_DBNAME = "CONDBR2"
 

Function Documentation

◆ fetch_iovs()

def python.db.fetch_iovs (   folder_name,
  since = None,
  until = None,
  channels = None,
  tag = "",
  what = "all",
  max_records = -1,
  with_channel = True,
  loud = False,
  database = None,
  convert_time = False,
  named_channels = False,
  selection = None,
  runs = None,
  with_time = False,
  unicode_strings = False 
)
Helper to fetch objects in a pythonic manner
`folder_name` may be an abbreviated name (DQMFONL) or a fully-qualified name
    (e.g. /GLOBAL/DETSTATUS/DQMFONL)
`since`, `until` can be (run, lumi) tuples, or standard iov keys
`channels` can be a cool ChannelSelection object or a list of ids/names
`tag` COOL folder tag
`what` is a list of strings specifying which records should be fetched
    if it is the string "all" (not a list), then all records are fetched,
    and naming is turned on.
`max_records` specifies the maximum number of records to fetch. -1 means all
`with_channel` specifies whether the channel number should be in the result
    list of each tuple
`loud` specifies whether quick_retrieve (C++ function) should print its
    status every 1000 objects
`database` can be used to specify an abbreviated database, or a connection 
    string
`convert_time` performs a conversion of `since` and `until` from runlumi
    to nanoseconds since the epoch.
`named_channels` causes the iovs returned to contain strings in the channel 
    identifier
`selection` [NOT IMPLEMENTED YET] create a cool selection object
`runs` if it is an integer, it is a run number. If it is a tuple, it is a
    run range.
`with_time` retrieve insertiontime for iovs
`unicode_strings` return unicode string objects, assuming database content 
                  is UTF-8

Definition at line 67 of file DQUtils/python/db.py.

67 def fetch_iovs(folder_name, since=None, until=None, channels=None, tag="",
68  what="all", max_records=-1, with_channel=True, loud=False,
69  database=None, convert_time=False, named_channels=False,
70  selection=None, runs=None, with_time=False, unicode_strings=False):
71  """
72  Helper to fetch objects in a pythonic manner
73  `folder_name` may be an abbreviated name (DQMFONL) or a fully-qualified name
74  (e.g. /GLOBAL/DETSTATUS/DQMFONL)
75  `since`, `until` can be (run, lumi) tuples, or standard iov keys
76  `channels` can be a cool ChannelSelection object or a list of ids/names
77  `tag` COOL folder tag
78  `what` is a list of strings specifying which records should be fetched
79  if it is the string "all" (not a list), then all records are fetched,
80  and naming is turned on.
81  `max_records` specifies the maximum number of records to fetch. -1 means all
82  `with_channel` specifies whether the channel number should be in the result
83  list of each tuple
84  `loud` specifies whether quick_retrieve (C++ function) should print its
85  status every 1000 objects
86  `database` can be used to specify an abbreviated database, or a connection
87  string
88  `convert_time` performs a conversion of `since` and `until` from runlumi
89  to nanoseconds since the epoch.
90  `named_channels` causes the iovs returned to contain strings in the channel
91  identifier
92  `selection` [NOT IMPLEMENTED YET] create a cool selection object
93  `runs` if it is an integer, it is a run number. If it is a tuple, it is a
94  run range.
95  `with_time` retrieve insertiontime for iovs
96  `unicode_strings` return unicode string objects, assuming database content
97  is UTF-8
98  """
99  from .quick_retrieve import quick_retrieve, browse_coracool, get_coracool_payload_spec
100 
101  if channels == []: return IOVSet()
102 
103  since, until = get_query_range(since, until, runs)
104 
105  channel_mapping = None
106  if isinstance(folder_name, str):
107  folder = Databases.get_folder(folder_name, database)
108  else:
109  try:
110  folder = folder_name
111  folder_name = folder.fullPath()
112  except Exception:
113  log.error("Exception when interpreting folder: {0}".format(folder_name))
114  raise
115 
116  log.info("Querying %s", folder_name)
117  log.debug("Query range: [%s, %s]", since, until)
118 
119  short_folder = folder.fullPath().split("/")[-1]
120 
121  time_based_folder = "<timeStamp>time</timeStamp>" in folder.description()
122  coracool_folder = "<coracool>" in folder.description()
123  iov_key_type = TimestampType if time_based_folder else RunLumiType
124 
125  if time_based_folder and (convert_time or runs):
126  # Perform a conversion of the run IoV to a time-based one.
127  # Note: probably inadvisable to do this for long ranges since
128  # it has to retrieve all of the luminosity blocks that took place
129  # in the query range.
130 
131  until = min(until, RunLumi(100000000, 0))
132  runrange = fetch_iovs("LBLB", since, until)
133  if runrange:
134  # If the runrange is empty for some reason, fall back.
135  since, until = runrange.first.StartTime, runrange.last.EndTime
136  return fetch_iovs(folder_name, since, until, channels, tag, what,
137  max_records, with_channel, loud,
138  database, convert_time=False,
139  named_channels=named_channels, selection=selection,
140  with_time=with_time,
141  unicode_strings=unicode_strings)
142  else:
143  return IOVSet()
144 
145  detstatus_names = "DQMFOFL", "DCSOFL", "DQMFONL", "SHIFTOFL", "SHIFTONL", "LBSUMM"
146  if any(short_folder.endswith(x) for x in detstatus_names):
147  channel_mapping = None # get channel mapping from channel_mapping.py
148  else:
149  _, _, channelmap = get_channel_ids_names(folder)
150  cm_reversed = dict((value, key) for key, value in six.iteritems(channelmap))
151  channelmap.update(cm_reversed)
152  channel_mapping = channelmap
153 
154  channels = make_channelselection(channels, channel_mapping)
155 
156  field_name = "%s_VAL" % short_folder
157 
158  if not coracool_folder:
159  if what == "all":
160  what = folder.folderSpecification().payloadSpecification().keys()
161 
162  channelmap = None
163  if named_channels:
164  _, _, channelmap = get_channel_ids_names(folder)
165 
166  folder.setPrefetchAll(False)
167 
168  if selection:
169  sel = make_browse_objects_selection(folder, selection)
170  iterator = folder.browseObjects(since, until, channels, tag, sel)
171  else:
172  iterator = folder.browseObjects(since, until, channels, tag)
173 
174  fields = []
175  if with_time:
176  fields.append("insertion_time")
177  if with_channel:
178  fields.append("channel")
179  fields.extend(what)
180 
181  record = make_iov_type(field_name, make_safe_fields(fields))
182 
183  result = quick_retrieve(iterator, record, what, max_records, with_channel,
184  loud, iov_key_type, channelmap, with_time,
185  unicode_strings)
186 
187  else:
188  args = folder_name, database
189  database, folder_path = Databases.resolve_folder_string(*args)
190 
191  # Coracool
192  assert database, "Coracool folder - you must specify a database"
193 
194  db = Databases.get_instance(database)
195  spec = get_coracool_payload_spec(db, folder_path)
196  if what == "all":
197  what = spec.keys()
198 
199  assert isinstance(what, list), ("Coracool folder - you must specify "
200  "`what` to fetch (it cannot be inferred, as with non-coracool.)")
201 
202  record = make_iov_type(field_name, ["channel", "elements"])
203 
204  element = namedtuple("element", " ".join(make_safe_fields(what)))
205 
206  result = browse_coracool(db, folder_path, since, until, channels, "",
207  what, record, element, iov_key_type)
208 
209  result = IOVSet(result, iov_type=record, origin=short_folder)
210 
211  return result
212 

◆ get_query_range()

def python.db.get_query_range (   since,
  until,
  runs 
)
Take `since`, `until` and `runs` and turn them into parameters.

If nothing is specified, an infinite range is used.
If `runs` is an integer, just that run is used
If `runs` is a two-tuple, then it is used as a (from_run, to_run)

Definition at line 29 of file DQUtils/python/db.py.

29 def get_query_range(since, until, runs):
30  """
31  Take `since`, `until` and `runs` and turn them into parameters.
32 
33  If nothing is specified, an infinite range is used.
34  If `runs` is an integer, just that run is used
35  If `runs` is a two-tuple, then it is used as a (from_run, to_run)
36  """
37  if runs and (since is None and until is None):
38  from builtins import int
39  if isinstance(runs, tuple):
40  since, until = (runs[0], 0), (runs[1], 0)
41 
42  elif isinstance(runs, int):
43  since, until = (runs, 0), (runs+1, 0)
44  else:
45  raise RuntimeError("Invalid type for `runs`, should be int or tuple")
46 
47  elif runs:
48  raise RuntimeError("Specify (since and/or until), OR runs, not both")
49 
50  else:
51  if since is None: since = 0
52  if until is None: until = 2**63-1
53 
54  if isinstance(since, tuple): since = RunLumi(*since)
55  if isinstance(until, tuple): until = RunLumi(*until)
56 
57  if isinstance(since, str): since = TimestampType.from_string(since)
58  if isinstance(until, str): until = TimestampType.from_string(until)
59 
60  if isinstance(since, datetime): since = TimestampType.from_date(since)
61  if isinstance(until, datetime): until = TimestampType.from_date(until)
62 
63  assert since <= until, "Bad query range (since > until?)"
64 
65  return since, until
66 

◆ make_safe_fields()

def python.db.make_safe_fields (   fields)

Definition at line 26 of file DQUtils/python/db.py.

26 def make_safe_fields(fields):
27  return [(field + "_") if iskeyword(field) else field for field in fields]
28 

◆ write_iovs()

def python.db.write_iovs (   folder_name,
  iovs,
  record,
  multiversion = True,
  tag = "",
  create = False,
  storage_buffer = False 
)

Definition at line 213 of file DQUtils/python/db.py.

213 def write_iovs(folder_name, iovs, record, multiversion=True, tag="",
214  create=False, storage_buffer=False):
215  args = folder_name, multiversion, record, create
216  db, folder, payload = Databases.fetch_for_writing(*args)
217 
218  if storage_buffer:
219  folder.setupStorageBuffer()
220 
221  total_iovs = len(iovs)
222  for i, iov in enumerate(iovs):
223  for field_name, field_value in zip(iov._fields[3:], iov[3:]):
224  payload[field_name] = field_value
225 
226  folder.storeObject(iov.since, iov.until, payload, iov.channel, tag)
227  if not i % 1000:
228  log.debug("Wrote %5i / %5i records", i, total_iovs)
229 
230  if storage_buffer:
231  log.debug("Flushing records to database...")
232  folder.flushStorageBuffer()
233  log.debug("... done")
234 

Variable Documentation

◆ DEFAULT_DBNAME

string python.db.DEFAULT_DBNAME = "CONDBR2"

Definition at line 24 of file DQUtils/python/db.py.

◆ log

python.db.log

Definition at line 17 of file DQDefects/python/db.py.

python.db.get_query_range
def get_query_range(since, until, runs)
Definition: DQUtils/python/db.py:29
python.db.make_safe_fields
def make_safe_fields(fields)
Definition: DQUtils/python/db.py:26
vtune_athena.format
format
Definition: vtune_athena.py:14
python.db.fetch_iovs
def fetch_iovs(folder_name, since=None, until=None, channels=None, tag="", what="all", max_records=-1, with_channel=True, loud=False, database=None, convert_time=False, named_channels=False, selection=None, runs=None, with_time=False, unicode_strings=False)
Definition: DQUtils/python/db.py:67
python.channel_mapping.make_channelselection
def make_channelselection(cs, mapping=None)
Definition: channel_mapping.py:81
python.channel_mapping.get_channel_ids_names
def get_channel_ids_names(folder)
Definition: channel_mapping.py:102
python.sugar.runlumi.RunLumi
RunLumi
Definition: runlumi.py:131
python.db.write_iovs
def write_iovs(folder_name, iovs, record, multiversion=True, tag="", create=False, storage_buffer=False)
Definition: DQUtils/python/db.py:213
python.quick_retrieve.browse_coracool
browse_coracool
Definition: quick_retrieve.py:17
min
#define min(a, b)
Definition: cfImp.cxx:40
TCS::join
std::string join(const std::vector< std::string > &v, const char c=',')
Definition: Trigger/TrigT1/L1Topo/L1TopoCommon/Root/StringUtils.cxx:10
python.selection.make_browse_objects_selection
def make_browse_objects_selection(folder, selection)
Definition: selection.py:82
python.quick_retrieve.quick_retrieve
quick_retrieve
Definition: quick_retrieve.py:16
python.sugar.iovtype.make_iov_type
def make_iov_type(name, variables, bases=(IOVType,), _memoized={})
Definition: iovtype.py:114
python.Bindings.keys
keys
Definition: Control/AthenaPython/python/Bindings.py:798
python.quick_retrieve.get_coracool_payload_spec
get_coracool_payload_spec
Definition: quick_retrieve.py:18
Trk::split
@ split
Definition: LayerMaterialProperties.h:38