jets_lcio.py (raw text file)# Example: create jets from PandoraPFA
# Use Jas4pp as: fpad script.py
# Task: Calculate jet pT
# 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.physics.vec import BasicHepLorentzVector,HepLorentzVector
from hep.lcio.event import *
from hep.lcio.io import *
from java.util 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=5 # max number of files
dataset="gev250ee_pythia6_qcd_all"
tag="rfull101"
from hep.physics.jet import DurhamJetFinder,FixNumberOfJetsFinder
fjet=DurhamJetFinder(0.05)
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("pT(jet)",50,0,200)
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("PandoraPFOCollection")
nPFA = col.getNumberOfElements()
particles=ArrayList() # list of 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()
typep=pa.getType() # type of PF
p=BasicHepLorentzVector(ee,p4[0],p4[1],p4[2])
particles.add(p) # add particle to the list
if (particles.size()>1): # ask for >1 particles
fjet.setEvent(particles)
alljets=[] # make a new list with jets
for i in range(fjet.njets()):
# print "Jet=",i," Nr of particles in jet =",fjet.nParticlesPerJet(i)
pjet=fjet.jet(i)
ee=pjet.t() # magnitude
p3=pjet.v3() # 3-vector
pl=LParticle(p3.x(),p3.y(),p3.z(),ee) # convert to a class with kinematic methods
if (pl.perp()>20): alljets.append(pl)
for i1 in range(len(alljets)):
p1=alljets[i1]
pt=p1.perp()
phi=p1.phi()
rap=p1.rapidity()
h1.fill(pt)
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("pT(jet) [GeV]")
c1.setNameY("Events")
c1.setRangeX(0,200)
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)