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.
import sys
import ctypes
from ROOT import gRandom, gPad, gROOT, gVirtualX
from ROOT import kTRUE, kRed
from ROOT import TCanvas, TH2, TH2F
def __init__( self ):
self._cX = None
self._cY = None
self._old = None
def __call__( self ):
if not h:
return
return
if self._old != None:
self._old = px, py
padsav = gPad
if not self._cX:
self._cX =
TCanvas(
'c2',
'Projection Canvas in X', 730, 10, 700, 500 )
else:
self._DestroyPrimitive( 'X' )
if not self._cY:
self._cY =
TCanvas(
'c3',
'Projection Canvas in Y', 10, 550, 700, 500 )
else:
self._DestroyPrimitive( 'Y' )
self.DrawSlice( h, y, 'Y' )
self.DrawSlice( h, x, 'X' )
proj =
getattr( self,
'_c'+xy ).GetPrimitive(
'Projection '+xy )
if proj:
def DrawSlice( self, histo, value, xy ):
yx = xy == 'X' and 'Y' or 'X'
bin =
getattr( histo,
'Get%saxis' % xy )().FindBin( value )
hp =
getattr( histo,
'Projection' + yx )(
'', bin, bin )
if __name__ == '__main__':
c1 =
TCanvas(
'c1',
'Dynamic Slice Example', 10, 10, 700, 500 )
hpxpy =
TH2F(
'hpxpy',
'py vs px', 40, -4, 4, 40, -4, 4 )
import __main__
c1.AddExec(
'dynamic',
'TPython::Exec( "slicer()" );' )
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t SetLineWidth
Option_t Option_t SetLineColor
2-D histogram with a float per channel (see TH1 documentation)
- Author
- Rene Brun, Johann Cohen-Tanugi, Wim Lavrijsen, Enric Tejedor
Definition in file DynamicSlice.py.