Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Wiki Markup
{composition-setup
}
cloak.toggle.type = text
cloak.toggle.open = + show
cloak.toggle.close = - hide
{composition-setup}

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.

...

Code Block
none
none
Scanning....
Start parsing files:
['/reg/d/psdm/xpp/xppi0310/xtc/e81-r0098-s00-c00.xtc', '/reg/d/psdm/xpp/xppi0310/xtc/e81-r0098-s01-c00.xtc']
  14826 datagrams read in 4.120000 s .   .   .   .   .   .   .
-------------------------------------------------------------
XtcScanner information:
  - 61 calibration cycles.
  - Events per calib cycle:
   [240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240]

Information from  1  control channels found:
fs2:ramp_angsft_target
Information from  11  devices found
                      BldInfo:EBeam:             EBeamBld_V1 (14641)
            BldInfo:FEEGasDetEnergy:             FEEGasDetEnergy (14563)   Any (78)
             BldInfo:NH2-SB1-IPM-01:             SharedIpimb (14641)
                BldInfo:PhaseCavity:             PhaseCavity (14641)
     DetInfo:EpicsArch-0|NoDevice-0:             Epics_V1 (107580)
         DetInfo:NoDetector-0|Evr-0:             EvrConfig_V4 (62)   EvrData_V3 (14640)
        DetInfo:XppSb2Ipm-1|Ipimb-0:             IpimbConfig_V1 (1)   IpmFexConfig_V1 (1)   IpimbData_V1 (14640)   IpmFex_V1 (14640)
        DetInfo:XppSb3Ipm-1|Ipimb-0:             IpimbConfig_V1 (1)   IpmFexConfig_V1 (1)   IpimbData_V1 (14640)   IpmFex_V1 (14640)
        DetInfo:XppSb3Pim-1|Ipimb-0:             IpimbConfig_V1 (1)   IpmFexConfig_V1 (1)   IpimbData_V1 (14640)   IpmFex_V1 (14640)
        DetInfo:XppSb4Pim-1|Ipimb-0:             IpimbConfig_V1 (1)   IpmFexConfig_V1 (1)   IpimbData_V1 (14640)   IpmFex_V1 (14640)
                          ProcInfo::             RunControlConfig_V1 (62)
XtcScanner is done!
-------------------------------------------------------------

xtcexplorer

XTC Explorer - Old - GUI interface that builds pyana modules for you.

...

Panel
saving to HDF5 file
saving to HDF5 file
Code Block
none
none
import h5py

ofile = h5py.File("output_file_name.h5",'w')
group = ofile.create_group("MyGroup")
group.create_dataset('delaytime',data=np.array(self.h_delaytime))
group.create_dataset('rawsignal',data=np.array(self.h_ipm_rsig))
group.create_dataset('normsignal',data=np.array(self.h_ipm_nsig))
ofile.close()

For more examples, see How to access HDF5 data from Python and http://code.google.com/p/h5py/

...

Like the other frameworks, pyana is an executable that loops through the XTC file and calls all
requested user modules at certain transitions. All the analysts need to do is to fill in the
relevant functions in their user analysis module:

Wiki Markup
{toggle-cloak
:id
=kappe}
code (pyana outline)

...

idkappe

...

For the following two examples, check out the latest version of the pyana_examples package:

...


addpkg pyana_examples HEAD
scons

(Note, if you don't already have a Kerberos ticket, you need to issue a 'kinit' command before 'addpkg'. You will be prompted for your unix password.)

Datatypes, and how to find data from your detector/device in the xtc file.

Pyana and psana has follows this naming scheme for labeling the datatypes from various devices. You can find the
names by investigating the xtc file with the above-mentioned tools (pyxtcreader, xtcscanner, xtcexplorer).
To see some examples of how to fetch the various data types in pyana (or psana), look at Devices and Datatypes.

Point detector delay scan

...

Wiki Markup
{cloak:id=kappe}
Code Block

# useful imports
import numpy as np
import matplotlib.pyplot as plt
from pypdsdata.xtc import TypeId

class mymodule (object) :
    """Class whose instance will be used as a user analysis module. """

    def __init__ ( self,
                   source = ""
                   threshold = "" ):
        """Class constructor.
        The parameters to the constructor are passed from pyana configuration file.
        If parameters do not have default values  here then the must be defined in
        pyana.cfg. All parameters are passed as strings, convert to correct type before use.

        @param source         name of device, format 'Det-ID|Dev-ID'
        @param threshold      threshold value (remember to convert from string)
        """
        self.source = source
        self.threshold = float(threshold)

    def beginjob( self, evt, env ) :
        """This method is called once at the beginning of the job. It should
        do a one-time initialization possible extracting values from event
        data (which is a Configure object) or environment.

        @param evt    event data object
        @param env    environment object
        """
        pass

    def beginrun( self, evt, env ) :
        """This optional method is called if present at the beginning of the new run.

        @param evt    event data object
        @param env    environment object
        """
        pass

    def begincalibcycle( self, evt, env ) :
        """This optional method is called if present at the beginning
        of the new calibration cycle.

        @param evt    event data object
        @param env    environment object
        """
        pass

    def event( self, evt, env ) :
        """This method is called for every L1Accept transition.

        @param evt    event data object
        @param env    environment object
        """
        pass

    def endcalibcycle( self, env ) :
        """This optional method is called if present at the end of the
        calibration cycle.

        @param env    environment object
        """
        pass

    def endrun( self, env ) :
        """This optional method is called if present at the end of the run.

        @param env    environment object
        """
        pass

    def endjob( self, env ) :
        """This method is called at the end of the job. It should do
        final cleanup, e.g. close all open files.

        @param env    environment object
        """
        pass
Wiki Markup
{cloak}
Panel

For the following two examples, check out the latest version of the pyana_examples package:

Code Block
none
none

addpkg pyana_examples HEAD
scons

(Note, if you don't already have a Kerberos ticket, you need to issue a 'kinit' command before 'addpkg'. You will be prompted for your unix password.)

Datatypes, and how to find data from your detector/device in the xtc file.

Pyana and psana has follows this naming scheme for labeling the datatypes from various devices. You can find the
names by investigating the xtc file with the above-mentioned tools (pyxtcreader, xtcscanner, xtcexplorer).
To see some examples of how to fetch the various data types in pyana (or psana), look at Devices and Datatypes.

Point detector delay scan

The python code for this pyana module resides in pyana_examples/src/xppt_delayscan.py.  In this example, we do a point detector delay scan, where we get the time as scan points via a control PV, and where time rebinning based on phase cavity measurement is used to improve the time resolution. One IPIMB device (a.k.a. IPM3) is used for normalization (i0, I Zero) (parameter name ipimb_norm) and another IPIMB device (a.k.a. PIM3) channel 1 is used as the signal (parameter name ipimb_sig).

Panel

Open an editor and save the following in a file named pyana.cfg:

Code Block
none
none

[pyana]

modules = pyana_examples.xppt_delayscan

[pyana_examples.xppt_delayscan]
controlpv = fs2:ramp_angsft_target
ipimb_norm = XppSb3Ipm-1|Ipimb-0
ipimb_sig = XppSb3Pim-1|Ipimb-0
threshold = 0.1
outputfile = point_scan_delay.npy

If you look at the code (pyana_examples/src/xppt_delayscan.py) you'll notice there are no detector names in there. The names of the detectors in the XTC file are passed as parameters from the configuration file above. The ipimb_norm parameter represents the IPIMB diode used for normalization, and the configuration files set its value to "XppSb3Ipm-1|Ipimb-0" (a.k.a. IPM3). Similarly, the IPIMB diode used for signal is represented by the pipmb_sig and its value set to "XppSb3Pim-1|Ipimb-0" (a.k.a. PIM3). By changing these parameter values, the pyana_examples/src/xppt_delayscan.py module can easily be used for other experiments or instruments.

Run pyana (start with 200 events):

Code Block
none
none

pyana -n 200 /reg/d/psdm/XPP/xppi0310/xtc/e81-r0098-s0*

Wiki Markup
{toggle-cloak:id=delayscan}
Highlighting of some code snippets from xppt_delayscan.py:

Wiki Markup
{cloak:id=delayscan}
  • Fetching the ControlPV information:
    ControlPV is available from the env object, and since it only changes at the beginning
    of each calibration cycle, the begincalibcycle function is the appropriate place to get it:
    Code Block
    none
    none
    
        def begincalibcycle( self, evt, env ) :
    

    The ControlConfig object may contain several pvControl and pvMonitor objects. In this case
    there's only one, but make sure the name matches anyway:
    Code Block
    none
    none
    
            ctrl_config = env.getConfig(TypeId.Type.Id_ControlConfig)
    
            for ic in range (0, ctrl_config.npvControls() ):
                cpv = ctrl_config.pvControl(ic)
                if cpv.name()=="fs2:ramp_angsft_target":
    
                    # store the value in a class variable (visible in every class method)
                    self.current_pv_value = cpv.value() )
    
  • 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

...


...

Open an editor and save the following in a file named pyana.cfg:

  • Code Block
    none
    none

...


[pyana]

modules = pyana_examples.xppt_delayscan

[pyana_examples.xppt_delayscan]
controlpv = fs2:ramp_angsft_target
ipimb_norm = XppSb3Ipm-1|Ipimb-0
ipimb_sig = XppSb3Pim-1|Ipimb-0
threshold = 0.1
outputfile = point_scan_delay.npy

...

  • 
            ipmS_raw = evt.get(TypeId.Type.Id_IpimbData, "XppSb3Pim-1|Ipimb-0"

...

  •  )
            ipmS_fex = evt.get(TypeId.Type.Id_IpmFex, "XppSb3Pim-1|Ipimb-0"

...

Run pyana (start with 200 events):

  •  )
    
            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 )
    
Wiki Markup
{cloak}

...


pyana -n 200 /reg/d/psdm/XPP/xppi0310/xtc/e81-r0098-s0*

Toggle Cloak
iddelayscan
Highlighting of some code snippets from xppt_delayscan.py:

Cloak
iddelayscan
Fetching the ControlPV information:
ControlPV is available from the env object, and since it only changes at the beginning
of each calibration cycle, the begincalibcycle function is the appropriate place to get it:
none
The ControlConfig object may contain several pvControl and pvMonitor objects. In this case
there's only one, but make sure the name matches anyway: noneFetching 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:
none
Use "XppSb3Ipm-1|Ipimb-0" (a.k.a. IPM3) sum of all channels for normalization and filtering
none
Use "XppSb3Pim-1|Ipimb-0" (a.k.a. PIM3) channel 1 as signal
none
Get the phase cavity:
none
Compute delay time and fill histograms
none

Image peak finding

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

...