Versions Compared

Key

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

This section will guide you trough the steps necessary to perform simple analysis in a Python analysis framework (pyana). For more detailed description of pyana consult Pyana User Manual.

Pyana is based on Python with many internal parts such as access to XTC data written in C++. There is very little overhead for accessing data from Python and pyana may be adequate for many analysis tasks. User module in pyana is an instance of Python class which defines few specific methods:

  • beginjob(self, evt, env) – called once at the beginning of the job
  • beginrun(self, evt, env) – called at the beginning of every run
  • begincalibcycle(self, evt, env) – called at the beginning of every scan
  • event(self, evt, env) – called on every event
  • endcalibcycle(self, env) – called after every scan
  • endrun(self, env) – called after every run
  • endjob(self, env) – called at the end of job

To start with the doing analysis one should have user release setup first as explained in Packages and Releases. If you have not done it yet create a package for your analysis with some unique name:

Code Block
newrel ana-current my_ana_rel   # to make user release
cd my_ana_rel
sit_setup                       # never forget this!
newpkg my_ana_pkg               # to make package for analysis code

Good place to start writing analysis module is to create skeleton module from existing template:

Code Block
mkdir my_ana_pkg/src
codegen -l pyana-module my_ana_pkg my_ana_mod

This will create file my_ana_pkg/src/my_ana_mod.py which you need to edit and fill with some useful code. Start your favorite editor:

Code Block
vi my_ana_pkg/src/my_ana_mod.py

For example purposes we are going to use beam line data and print few interesting values on every event. For that the code will be quite simple (excluding comments):

Code Block
class my_ana_mod (object) :

    def __init__ ( self ) :
        pass

    def beginjob( self, evt, env ) :
        pass

    def event( self, evt, env ) :

        time = evt.getTime()   # get event time object
        ebeam = evt.getEBeam() # get EBeam object

        # dump objects        
        print "time: %s, charge: %g, energy=%g" % \
                (time, ebeam.fEbeamCharge, ebeam.fEbeamL3Energy)

    def endjob( self, evt) :
        pass

After you finished editing your module build everything in the release:

Code Block
scons

To run this analysis start pyana giving it module name and input files:

Code Block
pyana -m my_ana_pkg.my_ana_mod /reg/d/psdm/AMO/amo14110/xtc/e43-r0100-s0*

which should produce output similar to this:

Code Block
time: <T:1280631930.026688363>, charge: 0.251309, energy=4386.32
time: <T:1280631930.043360843>, charge: 0.233403, energy=4393.24
time: <T:1280631930.060033392>, charge: 0.246535, energy=4400.88
time: <T:1280631930.076703847>, charge: 0.247861, energy=4396.93
time: <T:1280631930.093381914>, charge: 0.248759, energy=4380.69
time: <T:1280631930.110053067>, charge: 0.240726, energy=4401.18
time: <T:1280631930.126720938>, charge: 0.239678, energy=4387.77
time: <T:1280631930.143398446>, charge: 0.243547, energy=4384.99
...

This is probably the simplest module that you can create and it does not use any interesting features such as parameters. Here is an example of slightly more advanced module, it's job is to count fraction of events with beam energy above certain threshold. Replace code in my_ana_pkg/src/my_ana_mod.py with this:

Code Block
class my_ana_mod (object) :

    def __init__ ( self, threshold ) :
        # parameters
        self.threshold = float(threshold)
        
        # statistics
        self.count = 0
        self.total = 0

    def beginjob( self, evt, env ) :
        pass

    def event( self, evt, env ) :

        ebeam = evt.getEBeam()
        if ebeam:
            energy = ebeam.fEbeamL3Energy
            if energy > self.threshold: self.count += 1
            self.total += 1

    def endjob( self, evt) :
        print "Fraction of events with energy>%g is %g%%" % \
            (self.threshold, self.count*100.0/self.total)

That module requires input parameter (threshold) from configuration file. Create file pyana.cfg in current directory with this contents:

Code Block
[pyana]
modules = my_ana_pkg.my_ana_mod

[my_ana_pkg.my_ana_mod]
threshold = 4400

And run the job, module name is specified in config file so it is not needed on the command line:

Code Block
% scons
% pyana /reg/d/psdm/AMO/amo14110/xtc/e43-r0100-s0*
Fraction of events with energy>4400 is 16.1945%

More interesting example is to run two instances of the same module in a job with different set of parameters. For this just replace the content of pyana.cfg with this:

Code Block
[pyana]
modules = my_ana_pkg.my_ana_mod:thresh4390 my_ana_pkg.my_ana_mod:thresh4400

[my_ana_pkg.my_ana_mod:thresh4390]
threshold = 4390

[my_ana_pkg.my_ana_mod:thresh4400]
threshold = 4400

and run again:

Code Block
% pyana /reg/d/psdm/AMO/amo14110/xtc/e43-r0100-s0*
Fraction of events with energy>4390 is 56.5572%
Fraction of events with energy>4400 is 16.1945%