ATLAS Offline Software
Loading...
Searching...
No Matches
chai_addDID.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2"""
3Test CREST Tag Content: ITkPixModIDMap-RUN4-00-00-TEST
4
5This script connects to the CREST server at https://atlas-crest-dev.cern.ch/api-v6.4
6and investigates the tag ITkPixModIDMap-RUN4-00-00-TEST.
7It can create the tag ITkPixModIDMap-RUN4-00-00-TEST, its payload specifications and upload IOV.
8
9Usage:
10 python chai_addDID.py
11 python chai_addDID.py --since 0
12 python chai_addDID.py --tag ITkPixModIDMap-RUN4-00-00-TEST --db crest:https://atlas-crest-dev.cern.ch/api-v6.4
13"""
14
15import chai
16import argparse
17import sys
18import json
19import base64
20import gzip
21
22DEFAULT_TAG = "ITkPixModIDMap-RUN4-00-00-TEST"
23DEFAULT_DB = "crest:https://atlas-crest-dev.cern.ch/api-v6.4"
24
25
26def sep(title=""):
27 line = "=" * 70
28 print(f"\n{line}")
29 if title:
30 print(title)
31 print(line)
32
33
34
35def create_container_with_clob(payload_spec, clob_dataA: str):
36 """
37 Create a container with the clob data.
38
39 Args:
40 payload_spec: PayloadSpec for the container
41 clob_data: bytes object to store
42
43 Returns:
44 Container with the clob data
45 """
46 container = chai.Container(payload_spec)
47
48 print(f"Type of dataA is {type(clob_dataA)}")
49 # Add as string to channel 0
50 container[0].push_string(clob_dataA)
51 return container
52
53def main(args = None):
54 parser = argparse.ArgumentParser(
55 description="Create ITkPixModIDMap-RUN4-00-00-TEST tag content via chai",
56 )
57 parser.add_argument("--tag", default=DEFAULT_TAG, help=f"Tag name (default: {DEFAULT_TAG})")
58 parser.add_argument("--db", default=DEFAULT_DB, help=f"DB connection string (default: {DEFAULT_DB})")
59 parser.add_argument("--since", type=int, default=0,
60 help="Start time for IOVs (default: 0)")
61 parser.add_argument("--until", type=lambda x: int(x, 0), default=0xFFFFFFFFFFFFFFFF,
62 help="End time for IOVs (default: 0xFFFFFFFFFFFFFFFF)")
63 parser.add_argument("--dataA", type=str, default="data.json",
64 help="Path to the JSON file containing the data for A-side (default: data.json)")
65 args = parser.parse_args(args)
66 since = args.since if args.since > 0 else None
67 until = args.until if args.until > 0 else None
68
69 if(args.since > 0 and args.until > 0 and until < since):
70 print(f"✗ Error: until = {until} and since = {since}")
71 sys.exit(1)
72
73 sep("CHAI – Tag Content Creation")
74 print(f" Tag : {args.tag}")
75 print(f" DB : {args.db}")
76 print(f" Data, side-A: {args.dataA}")
77 # ------------------------------------------------------------------
78 # 1. Connect
79 # ------------------------------------------------------------------
80 sep("1. Connect to CREST")
81 try:
82 db = chai.Database(args.db)
83 print(f"✓ Connected")
84 except Exception as exc:
85 print(f"✗ Connection failed: {exc}")
86 sys.exit(1)
87
88 # ------------------------------------------------------------------
89 # 2. Retrieve tag
90 # ------------------------------------------------------------------
91 sep("2. Retrieve / Create a tag")
92
93 try:
94 # Try to get the tag
95 existing_tag = db.get_tag(args.tag)
96 print(f" Tag '{args.tag}' already exists")
97 print(f" Removing existing tag...")
98 db.delete_tag(args.tag)
99 print(f" ✓ Tag removed successfully")
100 except RuntimeError as e:
101 # Tag doesn't exist, which is fine
102 print(f" Tag '{args.tag}' does not exist (this is expected)")
103
104 # ------------------------------------------------------------------
105 # 3. Tag metadata
106 # ------------------------------------------------------------------
107 sep("3. Tag metadata")
108 # Define payload specification with 2 columns (one string, one int)
109 print(" Creating payload specification:")
110 print(" - Column 1: 'cabling' (String)")
111
112 fields = [
113 chai.Field("cabling", chai.Type.String)
114 ]
115 field_spec = chai.FieldSpec(fields)
116
117 # Define 2 channels
118 print(" Creating channel specification:")
119 print(" - Channel 0: 'Channel_AC'")
120
121 channels = [
122 chai.Channel(0, "Channel_AC"),
123 ]
124 channel_spec = chai.ChannelSpec(channels)
125
126 # Combine into payload specification
127 payload_spec = chai.PayloadSpec(field_spec, channel_spec)
128 print(f" ✓ Payload specification created with {len(field_spec)} fields and {len(channel_spec)} channels")
129
130 # Create the tag
131 try:
132 tag = db.create_tag(
133 args.tag, # tag_name
134 "Test tag for demonstrating tag management operations", # description
135 payload_spec, # payload_spec
136 "", # node description
137 chai.IovType.Time, # iov_type
138 "crest-json-single-iov", # object_type
139 chai.Synchronization.All, # synchronization
140 chai.TagStatus.Unlocked # status
141 )
142 print(f" ✓ Tag created successfully: {tag.name}")
143 print(f" - Description: {tag.description}")
144 print(f" - IOV Type: {tag.iov_type}")
145 print(f" - Status: {tag.status}")
146 except Exception as e:
147 print(f" ✗ Failed to create tag: {e}")
148 sys.exit(1)
149
150
151 print(f" name : {tag.name}")
152 print(f" description : {tag.description}")
153 print(f" iov_type : {tag.iov_type}")
154 print(f" object_type : {tag.object_type}")
155 print(f" synchronization: {tag.synchronization}")
156 print(f" status : {tag.status}")
157 print(f" # IOVs : {len(tag)}")
158
159 # ------------------------------------------------------------------
160 # 4. Payload specification
161 # ------------------------------------------------------------------
162 sep("4. Payload specification")
163 pspec = tag.payload_spec
164 print(f" Fields ({len(pspec.fields.fields)}):")
165 for f in pspec.fields.fields:
166 print(f" {f.name}: {f.type}")
167
168 channel_names = pspec.channels.channel_names
169 print(f"\n Channels ({len(channel_names)}):")
170 items = list(channel_names.items())
171 shown = items[:10]
172 for cid, cname in shown:
173 suffix = f" ({cname})" if cname else ""
174 print(f" {cid}{suffix}")
175 if len(items) > 10:
176 print(f" ... and {len(items) - 10} more")
177
178 # ------------------------------------------------------------------
179 # 5. Upload IOVs
180 # ------------------------------------------------------------------
181 sep("5. IOVs")
182 print(f" Since: {args.since}")
183 # Load JSON file from disk
184 json_dataA = None
185 with open(args.dataA, "r") as f:
186 json_dataA = json.load(f)
187 # Store a compact, compressed representation of the JSON payload. The backend
188 # rejects very large raw JSON strings, but accepts compressed base64 content.
189 json_textA = json.dumps(json_dataA, separators=(",", ":"), sort_keys=True)
190
191 compressedA = gzip.compress(json_textA.encode("utf-8"))
192 cdataA = base64.b64encode(compressedA).decode("ascii")
193
194 #this is the uncompressed version
195 cdataA = str(json.dumps(json_dataA).encode('utf-8'))
196
197 print(f" Created clob dataA: {len(cdataA)} characters (compressed JSON)")
198 print(f"Type of dataA is {type(cdataA)}")
199 # Create container
200 container = create_container_with_clob(payload_spec, cdataA)
201 print(f" Container created with {container.num_channels} channel(s), {container.num_fields} field(s)")
202
203 # Store the container directly using add_payload
204 try:
205 since = args.since
206 until = args.until
207 # Store the container payload
208 tag.add_payload(container, since, until)
209
210 payload_json = container.to_json()
211 print(f" ✓ Stored IOV: since={since}, until={until}")
212 print(f" Payload size: {len(payload_json)} bytes")
213 print(f" String length: {len(json.dumps(json_dataA))} characters")
214
215 except Exception as e:
216 print(f" ✗ Failed to store IOV: {e}")
217
218 sep()
219 print("✓ Done")
220
221
222if __name__ == "__main__":
223 main()
if(pathvar)
void print(char *figname, TCanvas *c1)
int main()
Definition hello.cxx:18
sep(title="")
create_container_with_clob(payload_spec, str clob_dataA)