7 @author Laura.Sargsyan@cern.ch 2025-04-16
8 CopyBlobFromCrest.py - Tool for retrieving TileCal calibration data from CREST
12 This script connects to the CREST service to fetch calibration data (blobs) for TileCal. Users can specify which calibration tag, run number, luminosity
13 block, and channels to retrieve for fine-grained control of the data. The fetched calibration data is then saved into a JSON file whose name typically
14 includes metadata such as the tag, run, and luminosity block.
16 Command-line Parameters:
17 -----------------------
19 CREST server URL. Default: http://crest-j23.cern.ch:8080/api-v5.0
22 Calibration tag to fetch from CREST (e.g., TileOfl02CalibCes-RUN2-UPD4-29). Default: TileOfl02CalibCes-RUN2-UPD4-29)
25 The run number. Default: 2147483647 (retrieves the latest IOV).
28 Luminosity block number. Default: 0
31 Space or comma-separated list of COOL channel IDs to retrieve (e.g., 0,1,2). Default: [0..275] plus channel 1000.
34 Output filename (JSON). If omitted, a default is generated, typically
35 structured as: <tag>.<run>.<lumi>.json. For example: UPD4.2147483647.0.json
39 python CopyBlobFromCrest.py --tag TileOfl02CalibCes-RUN2-UPD4-29 --channel 0,1,2 --output fetched_data.json
43 1. Connects to the specified CREST service (-s/--server).
44 2. Fetches calibration data (blobs) for the given tag, run, lumi block,
46 3. Saves the data into a JSON file with a filename either provided via
47 --output or auto-generated from the tag, run, and lumi.
49 The output JSON file stores the calibration data keyed by the channel IDs.
50 Each channel’s data includes the calibration blob(s) retrieved from CREST.
54 from TileCalibBlobPython
import TileCalibCrest
55 from TileCalibBlobPython.TileCalibLogger
import getLogger
61 logLevel = logging.DEBUG
62 log.setLevel(logLevel)
65 log1.setLevel(logLevel)
70 Fetch calibration data from TileBlobReaderCrest.
73 server (str): CREST server URL.
74 tag (str): Tag identifying the calibration data.
75 run (int): Run number to fetch data for.
76 lumi (int): Lumi block to fetch data for.
77 channels (list of int): List of COOL channel numbers.
80 list: List of calibration data entries retrieved.
83 log.info(
"Initializing TileBlobReaderCrest...")
85 if folderTag.startswith(
"Tile")
or folderTag.startswith(
"CALO"):
89 blob_reader = TileCalibCrest.TileBlobReaderCrest(schema, folderPath, tag, run, lumi,copyBlob=
True)
93 blobs = blob_reader.payload
94 log.info(f
"Successfully fetched {len(blobs)} calibration entries.")
97 except Exception
as e:
98 log.error(f
"Error while retrieving calibration data: {e}")
103 Process a list of channel identifiers, handling comma-separated values.
105 Args: channel_list (list): List of channel identifiers, which may contain comma-separated value
106 Returns: list: Processed list of individual channel identifiers
108 processed_channels = []
110 for item
in channel_list:
112 if isinstance(item, str)
and ',' in item:
113 parts = [part.strip()
for part
in item.split(
',')]
114 processed_channels.extend(parts)
116 processed_channels.append(
str(item))
118 return processed_channels
124 run_str =
str(args.run)
125 lumi_str =
str(args.lumi)
128 tag = args.tag
if args.tag
else "default"
131 if hasattr(args,
'channel')
and args.channel:
133 processed_channels = args.channel[0].
split(
',')
134 channel_str =
'-'.
join(processed_channels)
135 return f
"{tag}.{run_str}.{lumi_str}.ch{channel_str}.json"
138 return f
"{tag}.{run_str}.{lumi_str}.json"
144 Save fetched calibration data to a file.
147 blobs : dict containing calibration data blobs
148 output_file : str Path to the output file where data will be saved
149 requested_channels : list, optional
150 List of specific channels to include in the output file.
151 If None, all channels from the blobs will be included.
154 log.info(f
"Saving calibration data to {output_file}")
156 if requested_channels
is not None:
161 filtered_blobs = {channel: data
for channel, data
in blobs.items()
162 if channel
in processed_channels}
164 blobs_to_save = filtered_blobs
167 blobs_to_save = blobs
169 with open(output_file,
"w")
as f:
170 json.dump(blobs_to_save, f)
172 log.info(
"Calibration data successfully written to output file.")
174 except Exception
as e:
175 log.error(f
"Error while saving calibration data: {e}")
179 Set up command-line argument parser.
182 argparse.ArgumentParser: Configured argument parser
184 parser = argparse.ArgumentParser(description=
"Read TileCal blobs from CREST and convert them to JSON format.")
186 parser = argparse.ArgumentParser(description=
"Read TileCal blobs from CREST and convert them to JSON format.")
189 default=
"http://crest-j23.cern.ch:8080/api-v5.0",
190 help=
"Specify CREST server URL (default is %(default)s)."
195 default=
'TileOfl02CalibCes-RUN2-UPD4-29',
196 help=
"Specify the tag to use (e.g., RUN2-UPD4-04). Default is %(default)s."
202 help=
"Specify the run number (default is the latest IOV, %(default)s)."
208 help=
"Specify the lumi block number (default is %(default)s)."
214 help=
"Specify COOL channels (default includes all channels from 0-275 and 1000). Use space or comma-separated values."
220 help=
"Specify the output JSON file for saving calibration data. If not provided, will use '[tag].[run].[lumi].json'"
224 args = parser.parse_args()
228 if __name__ ==
"__main__":
232 log.info(
"Starting the calibration data fetching process.")
240 channels=args.channel,
247 log.info(f
"Finished fetching and saving calibration data in {ofile} successfully.")
248 except Exception
as ex:
249 log.error(f
"An error occurred during execution: {ex}")