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