Logo ROOT   6.07/09
Reference Guide
Namespaces
DynamicSlice.py File Reference

Namespaces

 DynamicSlice
 

Detailed Description

View in nbviewer Open in SWAN Example of function called when a mouse event occurs in a pad.

When moving the mouse in the canvas, a second canvas shows the projection along X of the bin corresponding to the Y position of the mouse. The resulting histogram is fitted with a gaussian. A "dynamic" line shows the current bin position in Y. This more elaborated example can be used as a starting point to develop more powerful interactive applications exploiting CINT as a development engine.

Note that a class is used to hold on to the canvas that display the selected slice.

pict1_DynamicSlice.py.png
1 
2 import sys
3 
4 from ROOT import gRandom, gPad, gROOT, gVirtualX
5 from ROOT import kTRUE, kRed
6 from ROOT import TCanvas, TH2, TH2F, Double
7 
8 
9 class DynamicExec:
10 
11  def __init__( self ):
12  self._cX = None
13  self._cY = None
14  self._old = None
15 
16  def __call__( self ):
17 
18  h = gPad.GetSelected();
19  if not h:
20  return
21 
22  if not isinstance( h, TH2 ):
23  return
24 
25  gPad.GetCanvas().FeedbackMode( kTRUE )
26 
27  # erase old position and draw a line at current position
28  px = gPad.GetEventX()
29  py = gPad.GetEventY()
30 
31  uxmin, uxmax = gPad.GetUxmin(), gPad.GetUxmax()
32  uymin, uymax = gPad.GetUymin(), gPad.GetUymax()
33  pxmin, pxmax = gPad.XtoAbsPixel( uxmin ), gPad.XtoAbsPixel( uxmax )
34  pymin, pymax = gPad.YtoAbsPixel( uymin ), gPad.YtoAbsPixel( uymax )
35 
36  if self._old != None:
37  gVirtualX.DrawLine( pxmin, self._old[1], pxmax, self._old[1] )
38  gVirtualX.DrawLine( self._old[0], pymin, self._old[0], pymax )
39  gVirtualX.DrawLine( pxmin, py, pxmax, py )
40  gVirtualX.DrawLine( px, pymin, px, pymax )
41 
42  self._old = px, py
43 
44  upx = gPad.AbsPixeltoX( px )
45  x = gPad.PadtoX( upx )
46  upy = gPad.AbsPixeltoY( py )
47  y = gPad.PadtoY( upy )
48 
49  padsav = gPad
50 
51  # create or set the display canvases
52  if not self._cX:
53  self._cX = TCanvas( 'c2', 'Projection Canvas in X', 730, 10, 700, 500 )
54  else:
55  self._DestroyPrimitive( 'X' )
56 
57  if not self._cY:
58  self._cY = TCanvas( 'c3', 'Projection Canvas in Y', 10, 550, 700, 500 )
59  else:
60  self._DestroyPrimitive( 'Y' )
61 
62  self.DrawSlice( h, y, 'Y' )
63  self.DrawSlice( h, x, 'X' )
64 
65  padsav.cd()
66 
67  def _DestroyPrimitive( self, xy ):
68  proj = getattr( self, '_c'+xy ).GetPrimitive( 'Projection '+xy )
69  if proj:
70  proj.IsA().Destructor( proj )
71 
72  def DrawSlice( self, histo, value, xy ):
73  yx = xy == 'X' and 'Y' or 'X'
74 
75  # draw slice corresponding to mouse position
76  canvas = getattr( self, '_c'+xy )
77  canvas.SetGrid()
78  canvas.cd()
79 
80  bin = getattr( histo, 'Get%saxis' % xy )().FindBin( value )
81  hp = getattr( histo, 'Projection' + yx )( '', bin, bin )
82  hp.SetFillColor( 38 )
83  hp.SetName( 'Projection ' + xy )
84  hp.SetTitle( xy + 'Projection of bin=%d' % bin )
85  hp.Fit( 'gaus', 'ql' )
86  hp.GetFunction( 'gaus' ).SetLineColor( kRed )
87  hp.GetFunction( 'gaus' ).SetLineWidth( 6 )
88  canvas.Update()
89 
90 
91 if __name__ == '__main__':
92  # create a new canvas.
93  c1 = TCanvas('c1', 'Dynamic Slice Example', 10, 10, 700, 500 )
94  c1.SetFillColor( 42 )
95  c1.SetFrameFillColor( 33 )
96 
97  # create a 2-d histogram, fill and draw it
98  hpxpy = TH2F( 'hpxpy', 'py vs px', 40, -4, 4, 40, -4, 4 )
99  hpxpy.SetStats( 0 )
100  x, y = Double( 0.1 ), Double( 0.101 )
101  for i in range( 50000 ):
102  gRandom.Rannor( x, y )
103  hpxpy.Fill( x, y )
104  hpxpy.Draw( 'COL' )
105 
106  # Add a TExec object to the canvas (explicit use of __main__ is for IPython)
107  import __main__
108  __main__.slicer = DynamicExec()
109  c1.AddExec( 'dynamic', 'TPython::Exec( "slicer()" );' )
110  c1.Update()
Author
Rene Brun, Johann Cohen-Tanugi, Wim Lavrijsen

Definition in file DynamicSlice.py.