29 Take `since`, `until` and `runs` and turn them into parameters.
31 If nothing is specified, an infinite range is used.
32 If `runs` is an integer, just that run is used
33 If `runs` is a two-tuple, then it is used as a (from_run, to_run)
35 if runs
and (since
is None and until
is None):
36 from builtins
import int
37 if isinstance(runs, tuple):
38 since, until = (runs[0], 0), (runs[1], 0)
40 elif isinstance(runs, int):
41 since, until = (runs, 0), (runs+1, 0)
43 raise RuntimeError(
"Invalid type for `runs`, should be int or tuple")
46 raise RuntimeError(
"Specify (since and/or until), OR runs, not both")
49 if since
is None: since = 0
50 if until
is None: until = 2**63-1
52 if isinstance(since, tuple): since = RunLumi(*since)
53 if isinstance(until, tuple): until = RunLumi(*until)
55 if isinstance(since, str): since = TimestampType.from_string(since)
56 if isinstance(until, str): until = TimestampType.from_string(until)
58 if isinstance(since, datetime): since = TimestampType.from_date(since)
59 if isinstance(until, datetime): until = TimestampType.from_date(until)
61 assert since <= until,
"Bad query range (since > until?)"
65def fetch_iovs(folder_name, since=None, until=None, channels=None, tag="",
66 what="all", max_records=-1, with_channel=True, loud=False,
67 database=None, convert_time=False, named_channels=False,
68 selection=None, runs=None, with_time=False, unicode_strings=False):
70 Helper to fetch objects in a pythonic manner
71 `folder_name` may be an abbreviated name (DQMFONL) or a fully-qualified name
72 (e.g. /GLOBAL/DETSTATUS/DQMFONL)
73 `since`, `until` can be (run, lumi) tuples, or standard iov keys
74 `channels` can be a cool ChannelSelection object or a list of ids/names
76 `what` is a list of strings specifying which records should be fetched
77 if it is the string "all" (not a list), then all records are fetched,
78 and naming is turned on.
79 `max_records` specifies the maximum number of records to fetch. -1 means all
80 `with_channel` specifies whether the channel number should be in the result
82 `loud` specifies whether quick_retrieve (C++ function) should print its
83 status every 1000 objects
84 `database` can be used to specify an abbreviated database, or a connection
86 `convert_time` performs a conversion of `since` and `until` from runlumi
87 to nanoseconds since the epoch.
88 `named_channels` causes the iovs returned to contain strings in the channel
90 `selection` [NOT IMPLEMENTED YET] create a cool selection object
91 `runs` if it is an integer, it is a run number. If it is a tuple, it is a
93 `with_time` retrieve insertiontime for iovs
94 `unicode_strings` return unicode string objects, assuming database content
97 from .quick_retrieve
import quick_retrieve, browse_coracool, get_coracool_payload_spec
99 if channels == []:
return IOVSet()
103 channel_mapping =
None
104 if isinstance(folder_name, str):
105 folder = Databases.get_folder(folder_name, database)
109 folder_name = folder.fullPath()
111 log.error(
"Exception when interpreting folder: {0}".format(folder_name))
114 log.info(
"Querying %s", folder_name)
115 log.debug(
"Query range: [%s, %s]", since, until)
117 short_folder = folder.fullPath().
split(
"/")[-1]
119 time_based_folder =
"<timeStamp>time</timeStamp>" in folder.description()
120 coracool_folder =
"<coracool>" in folder.description()
121 iov_key_type = TimestampType
if time_based_folder
else RunLumiType
123 if time_based_folder
and (convert_time
or runs):
129 until =
min(until, RunLumi(100000000, 0))
133 since, until = runrange.first.StartTime, runrange.last.EndTime
134 return fetch_iovs(folder_name, since, until, channels, tag, what,
135 max_records, with_channel, loud,
136 database, convert_time=
False,
137 named_channels=named_channels, selection=selection,
139 unicode_strings=unicode_strings)
143 detstatus_names =
"DQMFOFL",
"DCSOFL",
"DQMFONL",
"SHIFTOFL",
"SHIFTONL",
"LBSUMM"
144 if any(short_folder.endswith(x)
for x
in detstatus_names):
145 channel_mapping =
None
147 _, _, channelmap = get_channel_ids_names(folder)
148 cm_reversed = {value: key
for key, value
in channelmap.items()}
149 channelmap.update(cm_reversed)
150 channel_mapping = channelmap
152 channels = make_channelselection(channels, channel_mapping)
154 field_name =
"%s_VAL" % short_folder
156 if not coracool_folder:
158 what = folder.folderSpecification().payloadSpecification().keys()
162 _, _, channelmap = get_channel_ids_names(folder)
164 folder.setPrefetchAll(
False)
167 sel = make_browse_objects_selection(folder, selection)
168 iterator = folder.browseObjects(since, until, channels, tag, sel)
170 iterator = folder.browseObjects(since, until, channels, tag)
174 fields.append(
"insertion_time")
176 fields.append(
"channel")
181 result = quick_retrieve(iterator, record, what, max_records, with_channel,
182 loud, iov_key_type, channelmap, with_time,
186 args = folder_name, database
187 database, folder_path = Databases.resolve_folder_string(*args)
190 assert database,
"Coracool folder - you must specify a database"
192 db = Databases.get_instance(database)
197 assert isinstance(what, list), (
"Coracool folder - you must specify "
198 "`what` to fetch (it cannot be inferred, as with non-coracool.)")
200 record = make_iov_type(field_name, [
"channel",
"elements"])
205 what, record, element, iov_key_type)
207 result =
IOVSet(result, iov_type=record, origin=short_folder)
211def write_iovs(folder_name, iovs, record, multiversion=True, tag="",
212 create=False, storage_buffer=False):
213 args = folder_name, multiversion, record, create
214 db, folder, payload = Databases.fetch_for_writing(*args)
217 folder.setupStorageBuffer()
219 total_iovs = len(iovs)
220 for i, iov
in enumerate(iovs):
221 for field_name, field_value
in zip(iov._fields[3:], iov[3:]):
222 payload[field_name] = field_value
224 folder.storeObject(iov.since, iov.until, payload, iov.channel, tag)
226 log.debug(
"Wrote %5i / %5i records", i, total_iovs)
229 log.debug(
"Flushing records to database...")
230 folder.flushStorageBuffer()
231 log.debug(
"... done")
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)