You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 6 Next »

This page holds a few example code-snippets for use in pyana analysis. The analysis is written in python and uses MatPlotLib.PyPlot for plotting of data. Compare with myana user examples to see how the same things can be done using the myana analysis framework.

Beamline data (Bld)

To read out energy, charge and position of the beam from the beamline data, use getEBeam(). It returns a class/structure that has the following members/fields:

       ebeam = evt.getEBeam()
        if ebeam :
            beamChrg = ebeam.fEbeamCharge
            beamEnrg = ebeam.fEbeamL3Energy
            beamPosX = ebeam.fEbeamLTUPosX
            beamPosY = ebeam.fEbeamLTUPosY
            beamAngX = ebeam.fEbeamLTUAngX
            beamAngY = ebeam.fEbeamLTUAngY
            beamPkCr = ebeam.fEbeamPkCurrBC2
            print "ebeam: ", beamChrg, beamEnrg, beamPosX, beamPosY, beamAngX, beamAngY, beamPkCr
        else :
            print "No EBeam object found"

To read out the energy from the front end enclosure (FEE) gas detector, use getFeeGasDet(). This returns and array of 4 numbers:

       fee_energy_array = evt.getFeeGasDet()
        gdENRC11 = fee_energy_array[0]
        gdENRC12 = fee_energy_array[1]
        gdENRC21 = fee_energy_array[2]
        gdENRC22 = fee_energy_array[3]
        print "GasDet energy ", gdENRC11, gdENRC12, gdENRC21, gdENRC22

To read out fit time and charge of the phase cavity, use getPhaseCavity() which returns a structure with the following fields:

        pc = evt.getPhaseCavity()
        if pc :
            pcFitTime1 = pc.fFitTime1
            pcFitTime2 = pc.fFitTime2
            pcCharge1 = pc.fCharge1
            pcCharge2 = pc.fCharge2
            print "PhaseCavity: ", pcFitTime1,  pcFitTime2, pcCharge1, pcCharge2
        else :
            print "No Phase Cavity object found"

Display images from princeton camera

When plotting with MatPlotLib, we don't need to set the limits of the histogram manually, thus we don't need to read the Princeton configuration for this. If we want to sum the image from several events, we must first define and initialize some variables:

   def __init__ ( self ):
        # initialize data
        self.address =  "SxrEndstation-0|Princeton-0"
        self.data = None

In each event, we add the image array returned from the getPrincetonValue function:

  def event ( self, evt, env ) :

       frame = evt.getPrincetonValue( self.address, env)
       if frame :
           # accumulate the data
           if self.data is None :
               self.data = np.float_(frame.data())
           else :
               self.data += frame.data()

At the end of the job, display/save the array:

   def endjob( self, env ) :
        plt.imshow( self.data/self.countpass, origin='lower')
        plt.colorbar()
        plt.show()

        # save the full image to a png file
        plt.imsave(fname="pyana_princ_image.png",arr=self.data, origin='lower')

Note that imsave saves the image only, pixel by pixel. If you want a view of the figure itself, lower resolution, you can save it from the interactive window you get from plt.show().

  • No labels