jets_antiKT5_fcchh_lcio.py (raw text file)
# Reconstruct antiKT R=0.5 jets using PandoraPFA and high-pT jets  
# Use Jas4pp as: fpad script.py
# 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
from hephysics.jet import ParticleD
import sys
from org.apache.commons.io import FileUtils

NFilesMax=10 # max number of files 
dataset="tev100_qcd_pythia8_ptall"
tag="rfull009"

# http://jwork.org/dmelt/api/doc.php/hephysics/jet/JetN2
from hephysics.jet import JetN2
ktjet=JetN2(0.5,"antikt",50)     # initialize jets with R=0.5, E-mode, anti-KT,min pT=100
print ktjet.info()               # print its settings


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(antiKT jets)",25,0,500)
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
    strVec = evt.getCollectionNames()
    if nEvent == 1:
            for col in  strVec:
                           print col
    col = evt.getCollection("PandoraPFOCollection")
    nPFA = col.getNumberOfElements()
    # if (nEvent>2000): break
    alljets=[] # make a new list with jets 
    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 
          # http://jwork.org/dmelt/api/doc.php/hephysics/jet/ParticleD.html
          p=ParticleD(p4[0],p4[1],p4[2],ee);
          particles.add(p) # add particle to the list 
    ktjet.buildJets(particles)
    jets=ktjet.getJetsSorted()      # get a list with sorted jets
    if (len(jets)>0):
                print "pT of a leading jet =",jets[0].perp()," GeV"
                h1.fill(jets[0].perp())
    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,500)
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)