Belle II Software development
__init__.py
1#!/usr/bin/env python3
2
3
10
11import math
12
13from ROOT import gSystem
14import collections
15
16from ROOT import Belle2 # make Belle2 namespace available
17
18gSystem.Load('libtracking_trackFindingCDC')
19
20
22
23 """
24 Helper class to generated the svg image from the various tracking objects.
25 This is a tiny wrapper around the TrackFindingCDC.EventDataPlotter.
26 """
27
28 def __init__(self, animate=False):
29 """
30 Constructor method.
31 @param animate switch indicating if an animated SVG should be generated
32 """
33 top = -112
34 left = -112
35 right = 112
36 bottom = 112
37
38 default_bound = Belle2.TrackFindingCDC.BoundingBox(left, bottom, right, top)
39 default_width = 1120
40 default_height = 1120
41
42
43 self.animate = animate
44
46
47 self.eventdata_plotter.setBoundingBox(default_bound)
48 self.eventdata_plotter.setCanvasHeight(default_height)
49 self.eventdata_plotter.setCanvasWidth(default_width)
50
51 def clone(self):
52 """
53 Make a copy of the current status of the plotter.
54 """
55 cloned_plotter = CDCSVGPlotter(self.animate)
56 cloned_plotter.eventdata_plotter = Belle2.TrackFindingCDC.EventDataPlotter(self.eventdata_plotter)
57 return cloned_plotter
58
59 def styling_to_attribute_map(self, **styling):
60 """
61 Transfer the styling information to the attribute map
62 """
64
65 for key, value in list(styling.items()):
66 attribute_map[str(key)] = str(value)
67
68 return attribute_map
69
70 def draw_superlayer_boundaries(self, **styling):
71 """
72 Draw the CDC superlayer boundaries
73 """
74 attribute_map = self.styling_to_attribute_map(**styling)
75 self.eventdata_plotter.drawSuperLayerBoundaries(attribute_map)
76
78 """
79 Draw the interaction point
80 """
81 self.eventdata_plotter.drawInteractionPoint()
82
83 def draw_outer_cdc_wall(self, **styling):
84 """
85 Draw the CDC outer wall
86 """
87 attribute_map = self.styling_to_attribute_map(**styling)
88 self.eventdata_plotter.drawOuterCDCWall(attribute_map)
89
90 def draw_inner_cdc_wall(self, **styling):
91 """
92 Draw the CDC inner wall
93 """
94 attribute_map = self.styling_to_attribute_map(**styling)
95 self.eventdata_plotter.drawInnerCDCWall(attribute_map)
96
97 @staticmethod
98 def unpack_attributes(styling, i_obj=0, obj=None):
99 """Mapping function to unpack the attributes from the attribute maps. Mechanism and interface inspired by d3.js"""
100 result = {}
101 for (key, value) in list(styling.items()):
102 if isinstance(value, collections.abc.Callable):
103 result[key] = value(i_obj, obj)
104 elif isinstance(value, str):
105 result[key] = value
106 elif hasattr(value, '__getitem__'):
107 result[key] = value[i_obj % len(value)]
108 else:
109 result[key] = value
110 return result
111
112 def draw_iterable(self, iterable, **styling):
113 """
114 Draw one or more items with the specified styling
115 """
116 draw = self.draw
117 unpack_attributes = self.unpack_attributes
118 for (i_obj, obj) in enumerate(iterable):
119 obj_styling = unpack_attributes(styling, i_obj, obj)
120 draw(obj, **obj_styling)
121
122 def draw_storevector(self, storeobj_name, **styling):
123 """
124 Draw information in a vector from the DataStore with the specified styling
125 """
126 print('Drawing vector from DataStore:', storeobj_name, end=' ')
127 print()
128 pystoreobj = Belle2.PyStoreObj(storeobj_name)
129
130 if pystoreobj:
131 # Wrapper around std::vector like
132 wrapped_vector = pystoreobj.obj()
133 vector = wrapped_vector.get()
134
135 print('with', vector.size(), 'entries')
136 print('Attributes are')
137 for (key, value) in list(styling.items()):
138 print(str(key), ':', str(value))
139
140 self.draw_iterable(vector, **styling)
141
142 else:
143 print("### not present in the DataStore")
144 print("Current content of the DataStore")
145 print("StoreArrays:")
147 print("StoreObjPtr:")
149
150 def draw_storearray(self, storearray_name, **styling):
151 """
152 Draw information from a StoreArray with the specified styling
153 """
154 print('Drawing StoreArray:', storearray_name, end=' ')
155 print()
156 storearray = Belle2.PyStoreArray(storearray_name)
157 if storearray:
158 print('with', storearray.getEntries(), 'entries')
159 print('Attributes are')
160 for (key, value) in list(styling.items()):
161 print(str(key), ':', str(value))
162
163 self.draw_iterable(storearray, **styling)
164
165 else:
166 print("### not present in the DataStore")
167 print("Current content of the DataStore")
168 print("StoreArrays:")
170 print("StoreObjPtr:")
172
173 def draw(self, obj, **styling):
174 """
175 Draw an object with the specified styling
176 """
177 attribute_map = self.styling_to_attribute_map(**styling)
178 self.eventdata_plotter.draw(obj, attribute_map)
179
180 def saveSVGFile(self, svgFileName='display.svg'):
181 """
182 Save the current dom object representation to disk.
183 """
184
185 eventdata_plotter = self.eventdata_plotter
186
187 boundingBox = eventdata_plotter.getBoundingBox()
188
189 height = boundingBox.getHeight()
190 width = boundingBox.getWidth()
191
192 totalPoints = 1120 * 1120
193 svgHeight = round(math.sqrt(totalPoints * float(height) / width))
194 svgWidth = round(math.sqrt(totalPoints * float(width) / height))
195
196 eventdata_plotter.setCanvasHeight(svgHeight)
197 eventdata_plotter.setCanvasWidth(svgWidth)
198
199 return eventdata_plotter.save(svgFileName)
200
201 def savePNGFile(self, pngFileName='display.png'):
202 """
203 Save the current dom object representation to disk as a png.
204 """
205
206 import cairosvg
207 import os
208
209 temp_file_name = "tmp.svg"
210 eventdata_plotter = self.eventdata_plotter
211
212 boundingBox = eventdata_plotter.getBoundingBox()
213
214 height = boundingBox.getHeight()
215 width = boundingBox.getWidth()
216
217 totalPoints = 1120 * 1120
218 svgHeight = round(math.sqrt(totalPoints * float(height) / width))
219 svgWidth = round(math.sqrt(totalPoints * float(width) / height))
220
221 eventdata_plotter.setCanvasHeight(svgHeight)
222 eventdata_plotter.setCanvasWidth(svgWidth)
223
224 eventdata_plotter.save(temp_file_name)
225
226 with open(temp_file_name) as temp_file:
227 with open(pngFileName, "w") as output_file:
228 svg_code = temp_file.read()
229 cairosvg.svg2png(bytestring=svg_code, write_to=output_file)
230
231 os.remove(temp_file_name)
A (simplified) python wrapper for StoreArray.
Definition: PyStoreArray.h:72
static void printList(DataStore::EDurability durability=DataStore::EDurability::c_Event)
Print list of available arrays for given durability.
Definition: PyStoreArray.cc:34
a (simplified) python wrapper for StoreObjPtr.
Definition: PyStoreObj.h:67
static void printList(DataStore::EDurability durability=DataStore::EDurability::c_Event)
Print list of available objects for given durability.
Definition: PyStoreObj.cc:33
A two dimensional rectangle that keeps track of the extend of a drawing.
Definition: BoundingBox.h:21
A class that can plot event related data types.
Belle2::TrackFindingCDC::AttributeMap AttributeMap
A map type for attributes names to values for additional drawing information.
def draw_storevector(self, storeobj_name, **styling)
Definition: __init__.py:122
def saveSVGFile(self, svgFileName='display.svg')
Definition: __init__.py:180
def draw_storearray(self, storearray_name, **styling)
Definition: __init__.py:150
eventdata_plotter
Display the image using the event-data plotter.
Definition: __init__.py:45
def savePNGFile(self, pngFileName='display.png')
Definition: __init__.py:201
animate
Switch to indicating if an animated SVG should be generated.
Definition: __init__.py:43
def draw_iterable(self, iterable, **styling)
Definition: __init__.py:112
def unpack_attributes(styling, i_obj=0, obj=None)
Definition: __init__.py:98