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