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 # This is the standard node description we want for this Tag entry
131 node_description='<timeStamp>time</timeStamp><addrHeader><address_header service_type="256" clid="40774348" /></addrHeader><typeName>AthenaAttributeList</typeName>'
132 # node_description = ""
133 # Create the tag
134 try:
135 tag = db.create_tag(
136 args.tag, # tag_name
137 "Test tag for demonstrating tag management operations", # description
138 payload_spec, # payload_spec
139 node_description, # node description
140 chai.IovType.Time, # iov_type
141 "crest-json-single-iov", # object_type
142 chai.Synchronization.All, # synchronization
143 chai.TagStatus.Unlocked # status
144 )
145 print(f" ✓ Tag created successfully: {tag.name}")
146 print(f" - Description: {tag.description}")
147 print(f" - IOV Type: {tag.iov_type}")
148 print(f" - Status: {tag.status}")
149 except Exception as e:
150 print(f" ✗ Failed to create tag: {e}")
151 sys.exit(1)
152
153
154 print(f" name : {tag.name}")
155 print(f" description : {tag.description}")
156 print(f" iov_type : {tag.iov_type}")
157 print(f" object_type : {tag.object_type}")
158 print(f" synchronization: {tag.synchronization}")
159 print(f" status : {tag.status}")
160 print(f" # IOVs : {len(tag)}")
161
162 # ------------------------------------------------------------------
163 # 4. Payload specification
164 # ------------------------------------------------------------------
165 sep("4. Payload specification")
166 pspec = tag.payload_spec
167 print(f" Fields ({len(pspec.fields.fields)}):")
168 for f in pspec.fields.fields:
169 print(f" {f.name}: {f.type}")
170
171 channel_names = pspec.channels.channel_names
172 print(f"\n Channels ({len(channel_names)}):")
173 items = list(channel_names.items())
174 shown = items[:10]
175 for cid, cname in shown:
176 suffix = f" ({cname})" if cname else ""
177 print(f" {cid}{suffix}")
178 if len(items) > 10:
179 print(f" ... and {len(items) - 10} more")
180
181 # ------------------------------------------------------------------
182 # 5. Upload IOVs
183 # ------------------------------------------------------------------
184 sep("5. IOVs")
185 print(f" Since: {args.since}")
186 # Load JSON file from disk
187 json_dataA = None
188 with open(args.dataA, "r") as f:
189 json_dataA = json.load(f)
190 # Store a compact, compressed representation of the JSON payload. The backend
191 # rejects very large raw JSON strings, but accepts compressed base64 content.
192 json_textA = json.dumps(json_dataA, separators=(",", ":"), sort_keys=True)
193
194 compressedA = gzip.compress(json_textA.encode("utf-8"))
195 cdataA = base64.b64encode(compressedA).decode("ascii")
196
197 #this is the uncompressed version
198 cdataA = str(json.dumps(json_dataA).encode('utf-8'))
199
200 print(f" Created clob dataA: {len(cdataA)} characters (compressed JSON)")
201 print(f"Type of dataA is {type(cdataA)}")
202 # Create container
203 container = create_container_with_clob(payload_spec, cdataA)
204 print(f" Container created with {container.num_channels} channel(s), {container.num_fields} field(s)")
205
206 # Store the container directly using add_payload
207 try:
208 since = args.since
209 until = args.until
210 # Store the container payload
211 tag.add_payload(container, since, until)
212
213 payload_json = container.to_json()
214 print(f" ✓ Stored IOV: since={since}, until={until}")
215 print(f" Payload size: {len(payload_json)} bytes")
216 print(f" String length: {len(json.dumps(json_dataA))} characters")
217
218 except Exception as e:
219 print(f" ✗ Failed to store IOV: {e}")
220
221 sep()
222 print("✓ Done")
223
224
225if __name__ == "__main__":
226 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)