Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
hist105_TExec_dynamic_slice.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_hist
3## \notebook
4## Example of function called when a mouse event occurs in a pad.
5## When moving the mouse in the canvas, a second canvas shows the
6## projection along X of the bin corresponding to the Y position
7## of the mouse. The resulting histogram is fitted with a gaussian.
8## A "dynamic" line shows the current bin position in Y.
9## This more elaborated example can be used as a starting point
10## to develop more powerful interactive applications exploiting Cling
11## as a development engine.
12##
13## Note that a class is used to hold on to the canvas that display
14## the selected slice.
15##
16## \macro_image
17## \macro_code
18##
19## \author Rene Brun, Johann Cohen-Tanugi, Wim Lavrijsen, Enric Tejedor, Sergey Linev
20
21import ctypes
22import array
23
24from ROOT import gRandom, gPad, gROOT, TCanvas, TH2, TH2F, kRed
25
26class DynamicExec:
27
28 def __init__( self ):
29 self._cX = None
30 self._cY = None
31
32 def __call__( self ):
33
34 h = gPad.GetSelected();
35 if not h:
36 return
37
38 if not isinstance( h, TH2 ):
39 return
40
41 px = gPad.GetEventX()
42 py = gPad.GetEventY()
43 upx = gPad.AbsPixeltoX( px )
44 upy = gPad.AbsPixeltoY( py )
45
46 uxmin, uxmax = gPad.GetUxmin(), gPad.GetUxmax()
47 uymin, uymax = gPad.GetUymin(), gPad.GetUymax()
48
49 posx = array.array('d', [uxmin, uxmax])
50 posy = array.array('d', [upy, upy])
51 gPad.PaintPolyLine(2, posx, posy, 'i_slicex')
52 posx = array.array('d', [upx, upx])
53 posy = array.array('d', [uymin, uymax])
54 gPad.PaintPolyLine(2, posx, posy, 'i_slicey')
55 gPad.Modified()
56 gPad.Update()
57
58 x = gPad.PadtoX( upx )
59 y = gPad.PadtoY( upy )
60
61 padsav = gPad
62
63 # create or set the display canvases
64 if not self._cX:
65 self._cX = TCanvas( 'c2', 'Projection Canvas in X', 730, 10, 700, 500 )
66 else:
67 self._DestroyPrimitive( 'X' )
68
69 if not self._cY:
70 self._cY = TCanvas( 'c3', 'Projection Canvas in Y', 10, 550, 700, 500 )
71 else:
72 self._DestroyPrimitive( 'Y' )
73
74 self.DrawSlice( h, y, 'Y' )
75 self.DrawSlice( h, x, 'X' )
76
77 padsav.cd()
78
79 def _DestroyPrimitive( self, xy ):
80 proj = getattr( self, '_c'+xy ).GetPrimitive( 'Projection '+xy )
81 if proj:
82 proj.IsA().Destructor( proj )
83
84 def DrawSlice( self, histo, value, xy ):
85 yx = xy == 'X' and 'Y' or 'X'
86
87 # draw slice corresponding to mouse position
88 canvas = getattr( self, '_c'+xy )
89 canvas.SetGrid()
90 canvas.cd()
91
92 bin = getattr( histo, 'Get%saxis' % xy )().FindBin( value )
93 hp = getattr( histo, 'Projection' + yx )( '', bin, bin )
94 hp.SetFillColor( 38 )
95 hp.SetName( 'Projection ' + xy )
96 hp.SetTitle( xy + 'Projection of bin=%d' % bin )
97 hp.Fit( 'gaus', 'ql' )
98 hp.GetFunction( 'gaus' ).SetLineColor( kRed )
99 hp.GetFunction( 'gaus' ).SetLineWidth( 6 )
100 canvas.Update()
101
102
103if __name__ == '__main__':
104
105 gROOT.SetWebDisplay('off')
106
107 # create a new canvas.
108 c1 = TCanvas('c1', 'Dynamic Slice Example', 10, 10, 700, 500 )
109 c1.SetFillColor( 42 )
110 c1.SetFrameFillColor( 33 )
111
112 # create a 2-d histogram, fill and draw it
113 hpxpy = TH2F( 'hpxpy', 'py vs px', 40, -4, 4, 40, -4, 4 )
114 hpxpy.SetStats( 0 )
115 x, y = ctypes.c_double( 0.1 ), ctypes.c_double( 0.101 )
116 for i in range( 50000 ):
117 # pass ctypes doubles by reference, then retrieve their modified values with .value
118 gRandom.Rannor( x, y )
119 hpxpy.Fill( x.value, y.value )
120 hpxpy.Draw( 'COL' )
121
122 # Add a TExec object to the canvas (explicit use of __main__ is for IPython)
123 import __main__
124 __main__.slicer = DynamicExec()
125 c1.AddExec( 'dynamic', 'TPython::Exec( "slicer()" );' )
126 c1.Update()
Option_t Option_t SetLineWidth
Option_t Option_t SetLineColor
The Canvas class.
Definition TCanvas.h:23
2-D histogram with a float per channel (see TH1 documentation)
Definition TH2.h:345