jpsi_mass.py (raw text file)
# Validation script: Read LCIO files
# Use Jas4pp as: fpad script.py
# Task: Calculate the mass of JPsi 
# S.Chekanov (ANL)

from org.lcsim.lcio import LCIOReader
from hep.io.sio import SIOReader
from hep.lcio.implementation.sio import SIOLCReader
from hep.lcio.implementation.io import LCFactory
from hep.lcio.event import * 
from hep.lcio.io import *
from java.awt import Color,Font
from jhplot import *  # import graphics
from jhplot.io import HBook
from hephysics.particle import LParticle
from java.lang import System
from java.io import *
from java.net import URL
from hepsim import HepSim
import sys,math 
from  java.awt import Color
from org.apache.commons.io import FileUtils

NFilesMax=20 # max number of files 
dataset="gev141ep_epjpsi_jpsi_gamgluon"
tag="rfull054"

www=""
if len(sys.argv)>1:
   flist=FileList.get(sys.argv[1],"slcio")
   if (len(sys.argv)>2): FilesMax=int(sys.argv[2])
else:
   sites=HepSim.urlRedirector(dataset)
   www=sites[0]+"/"+tag
   flist=HepSim.getList(www)
if len(flist)==0: print "Error: No input file!"; sys.exit(0)
else: print "Reading "+str(len(flist))+" files. Nr of files= ",NFilesMax

h1=H1D("Mass [GeV]",100,2.5,3.5)

factory = LCFactory.getInstance()
nEvent=0
for n in range(len(flist)):
  if (n+1>NFilesMax): break
  print "Open file=", flist[n] 
  url=URL(www+"/"+flist[n]) 
  xfile=File("/tmp/tmp.slcio");  
  FileUtils.copyURLToFile(url, xfile)
  reader = factory.createLCReader()
  reader.open(xfile.getAbsolutePath()) 
  while(1):
    evt=reader.readNextEvent()
    if (evt == None): break
    nEvent=nEvent+1
    if (nEvent%100==0): print "# Event: ",nEvent
    params=evt.getParameters()
    Q2true=params.getFloatVal("EVGEN:DIS:Q2")  

    # get reconstructed electrons and muons using PFA
    col = evt.getCollection("PandoraPFOCollection")
    nPFA = col.getNumberOfElements()
    particles=[]
    for i in range(nPFA):
          # http://www.lcsim.org/sites/lcsim/apidocs/org/lcsim/lcio/SIOReconstructedParticle.html
          # http://www.lcsim.org/sites/lcsim/apidocs/org/lcsim/event/ReconstructedParticle.html
          pa=col.getElementAt(i)
          charge=pa.getCharge()
          p4=pa.getMomentum()
          ee=pa.getEnergy()
          p=LParticle(p4[0],p4[1],p4[2],ee)
          p.setCharge(charge)
          if (abs(pa.getType())==11 or abs(pa.getType())==13): particles.append(p)
    if (len(particles)>1):
       for i1 in range(0,len(particles)-1):
          p1=particles[i1]
          ch1=p1.getCharge()
          for i2 in range(i1+1,len(particles)):
              p2=particles[i2]
              ch2=p2.getCharge()
              p1.add(p2)
              if (ch1*ch2<0): 
                          mass=p1.calcMass()
                          h1.fill(mass)

    del col
    del evt 
  reader.close() # close the file
  del reader
  xfile.delete() 
  System.gc() # if you low on memory, manual JVM cleanup for every 5th file 

c1=HPlot() # plotting part
c1.visible()
c1.setAutoRange()
c1.setMarginLeft(90)
c1.setNameX("Dilepton mass  [GeV]")
c1.setNameY("Events")
c1.setRangeX(2.5,3.5)
c1.draw(h1)  

l2=HLabel("=HepSim=",0.9,0.86, "NDC")
l2.setColor(Color.gray)
l2.setFont(Font("Helvetica", Font.PLAIN, 14))
c1.add(l2)

name="output"
if len(sys.argv[0])>0: name=sys.argv[0].replace(".py","")
file=HBook(name+".jdat","w"); print name+".jdat created"
file.write(h1)
file.close()
c1.export(name+".svg");    print name+".svg created"
# xsec.toFile(name+".txt");  print name+".txt created"
# sys.exit(0)