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