Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

A quick walk-through of the tools that exist for analysis of xtc files with python.
The main focus is on pyana, and the examples are from and for XPP primarily,
but may be useful examples to other experiments too.

Toggle Cloak
idtoc
outline of contents

Cloak
idtoc

The Basics

Python

http://docs.python.org/tutorial/

...

  • Fetching the IPIMB and PhaseCavity information:
    All the other information that we need, is available through the evt object, and
    event member function is the place to get it:
    Code Block
    none
    none
        def event( self, evt, env ) :
    

    Use "XppSb3Ipm-1|Ipimb-0" (a.k.a. IPM3) sum of all channels for normalization and filtering
    Code Block
    none
    none
            ipmN_raw = evt.get(TypeId.Type.Id_IpimbData, "XppSb3Ipm-1|Ipimb-0")
            ipmN_fex = evt.get(TypeId.Type.Id_IpmFex, "XppSb3Ipm-1|Ipimb-0")
    
            ipmN_norm = ipmN_fex.sum
    

    Use "XppSb3Pim-1|Ipimb-0" (a.k.a. PIM3) channel 1 as signal
    Code Block
    none
    none
            ipmS_raw = evt.get(TypeId.Type.Id_IpimbData, "XppSb3Pim-1|Ipimb-0" )
            ipmS_fex = evt.get(TypeId.Type.Id_IpmFex, "XppSb3Pim-1|Ipimb-0" )
    
            ipm_sig = ipmS_fex.channel[1]
    

    Get the phase cavity:
    Code Block
    none
    none
            pc = evt.getPhaseCavity()
            phasecav1 = pc.fFitTime1
            phasecav2 = pc.fFitTime2
            charge1 = pc.fCharge1
            charge2 = pc.fCharge2
    

    Compute delay time and fill histograms
    Code Block
    none
    none
            delaytime = self.current_pv_value + phasecav1*1e3    
    
            # The "histograms" are nothing but python lists. Append to them, and turn them into arrays at the end. 
            self.h_ipm_rsig.append( ipm_sig )
            self.h_ipm_nsig.append( ipm_sig/ipm_norm )
            self.h_delaytime.append( delaytime )
    

Image peak finding

Here are a collection of useful algorithms for image analysis: http://docs.scipy.org/doc/scipy/reference/ndimage.html

This particular example is done with a CSPad image, but only a single section is available. For more typical CSPad module, see next section.

  • For each event, fetch the CsPad information, and get the image array:
    Code Block
    none
    none
    
       def event( self, evt, env ) :
    
            elements = evt.getCsPadQuads(self.source, env)
            image = elements[0].data().reshape(185, 388)
    

  • Select a region of interest. If none is given (optional module parameter), set RoI to be the whole image.
    Code Block
    none
    none
    
            # Region of Interest (RoI)
            if self.roi is None: 
                self.roi = [ 0, image.shape[1], 0, image.shape[0] ] # [x1,x2,y1,y2]
    
            print "ROI   [x1, x2, y1, y2] = ", self.roi
    

  • Using only the RoI subset of the image, compute center-of-mass using one of the SciPy.ndimamge algorithms. Add to it the position of the RoI to get the CMS in global pixel coordinates:
    Code Block
    none
    none
    
            roi_array = image[self.roi[2]:self.roi[3],self.roi[0]:self.roi[1]]
            cms = scipy.ndimage.measurements.center_of_mass(roi_array)
            print "Center-of-mass of the ROI: (x, y) = (%.2f, %.2f)" %(self.roi[0]+cms[1],self.roi[2]+cms[0])
    

  • Here's an example how you can make an interactive plot and select the Region of Interest with the mouse. Here we plot the image in two axes (subpads on the canvas). The first will always show the full image. In the second axes, you can select a rectangular region in "Zoom" mode (click on the Toolbar's Zoom button). The selected region will be drawn on top of the full image to the left, while the right plot will zoom into the selected region:
    Code Block
    none
    none
    
           fig = plt.figure(1,figsize=(16,5))
            axes1 = fig.add_subplot(121)
            axes2 = fig.add_subplot(122)
    
            axim1 = axes1.imshow(image)
            axes1.set_title("Full image")
    
            axim2 = axes2.imshow(roi_array, extent=(self.roi[0],self.roi[1],self.roi[3],self.roi[2]))
            axes2.set_title("Region of Interest")
            
            # rectangular ROI selector
            rect = UpdatingRect([0, 0], 0, 0, facecolor='None', edgecolor='red', picker=10)
            rect.set_bounds(*axes2.viewLim.bounds)
            axes1.add_patch(rect)
            
            # Connect for changing the view limits
            axes2.callbacks.connect('xlim_changed', rect)
            axes2.callbacks.connect('ylim_changed', rect)
    

  • To compute the center-of-mass of the selected region, revert back to non-zoom mode (hit the 'zoom' button again) and click on the rectangle. The rectangle is connected to the 'onpick' function which updates self.roi and computes the center-of-mass:
    Code Block
    none
    none
    
            def onpick(event):
                xrange = axes2.get_xbound()
                yrange = axes2.get_ybound()
                self.roi = [ xrange[0], xrange[1], yrange[0], yrange[1]]
    
                roi_array = image[self.roi[2]:self.roi[3],self.roi[0]:self.roi[1]]
                cms = scipy.ndimage.measurements.center_of_mass(roi_array)
    
                print "Center-of-mass of the ROI: (x, y) = (%.2f, %.2f)" % (self.roi[0]+cms[1],self.roi[2]+cms[0])
    
            fig.canvas.mpl_connect('pick_event', onpick)
            
            plt.draw()
    

CSPad images and tile arangements

...