truth_ele_1gev_lcio.py (raw text file)
# Validation script: Read LCIO files
# Use Jas4pp as: fpad script.py
# Task: Calculate truth-level scattered electron energy 
# 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
from org.apache.commons.io import FileUtils

NFilesMax=10 # max number of files 
dataset="gev35ep_lepto6ard_dis1q2"
tag="rfull055"

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("E(ele)",50,0,10)

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
    # print " file event: ",evt.getEventNumber(), " run=",evt.getRunNumber()
    if (nEvent%100==0): print "# Event: ",nEvent
    col = evt.getCollection("MCParticle")
    nMc=col.getNumberOfElements()
    particles=[]
    pp=0
    nn=0
    for i in range(nMc): # loop over all particles 
      par=col.getElementAt(i)
      if(par.getGeneratorStatus() == 1): 
              vertex = par.getVertex();
              pdg=par.getPDG()
              momentum = par.getMomentum() 
              ee=par.getEnergy()
              mass=par.getMass()
              px=momentum[0]
              py=momentum[1]
              pz=momentum[2]
              charge=par.getCharge()
              p=LParticle(px,py,pz,ee,mass) 
              p.setCharge(charge)
              particles.append(p)
              #print nn, "pdg=",pdg, " ",px,py,pz, " charge=",charge 
              if (i>2 and abs(pdg)==11): # get scattered electron 
                                     h1.fill(ee);
                                     break  
    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("E_{ele} [GeV]")
c1.setNameY("Events")
c1.setRangeX(0,10)
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)