HepSim

Repository with Monte Carlo simulations for particle physics

qcd_pythia8_mjj_ptbins.py (raw text file)
# HepSim script using DataMelt http://jwork.org/dmelt
# Part of =HepMC= : https://atlaswww.hep.anl.gov/asc/hepsim/
# S.Chekanov (ANL)
from java.lang import *
from java.awt import Color,Font
from jhplot import  HPlot,P1D,HLabel,H1D
from jhplot.io import HBook
from proto import FileMC   # import FileMC
from jhplot.utils import FileList
from hepsim import HepSim
from hephysics.hepsim import PromcUtil
from hephysics.jet import SCJet,ParticleD
import sys


NMax=1 # max number per pT. Set to -1 to run over all files

# default location 
default_www="http://faxbox.usatlas.org/group/hepsim/events/pp/100tev/qcd_pythia8_ptbins/"

# get data either from local file system or stream them from URL
url="" 
TotalEvents=10000000 # number of events per sample 
if len(sys.argv)>1:
   if sys.argv[1].startswith("http"):
         flist=HepSim.getList(sys.argv[1])
         url=sys.argv[1]
   else: flist=FileList.get(sys.argv[1],"promc")
   if (len(sys.argv)>2): TotalEvents=int(sys.argv[2])
else:
   url=default_www; flist=HepSim.getList(url)
if len(flist)==0: print "Error: No input file!"; sys.exit(0)
else: print "Reading "+str(len(flist))+" files. Nr events= ",TotalEvents

# calculate jet-jet mass 
ktjet=SCJet(0.5,1,-1,100,True) # anti-KT,E-mode, R=0.5,min pT=100 GeV,fast 
ktjet.setDebug(False)

# extract files with in certain pT ranges
xmin=HepSim.getRanges(flist,"_pt","_");

# create file list using pT ranges 
filelist=[]
histo=[]
for i in xmin:
     pt=[]
     for j in flist:
        d1=j.split("_");
        if (d1[2].find("pt"+str(i))>-1): pt.append(j)
     filelist.append(pt)
     hh= H1D("Mjj"+str(i),48,2000,50000)
     histo.append( hh )

print "Total number of pT ranges=",len(filelist)
Total=len(filelist)

for ipt in range(Total):
  ptlist=filelist[ipt]
  print ipt, ") pT>"+str(xmin[ipt]) 
  cross=0; xcross=0; nev=0;  Nfiles=len(ptlist)
  if (NMax>-1):  Nfiles=NMax           # limit to NMax files 
  for m in range(Nfiles):              # loop over all files in this list    
    file=FileMC(url+ptlist[m])         # get input file
    header = file.getHeader()
    un=float(header.getMomentumUnit()) # conversion units
    lunit=float(header.getLengthUnit())
    if m==0 and ipt==0:
       print "ProMC v=",file.getVersion(), "M unit=",un,"L unit=",lunit 
    if (nev>TotalEvents): print "Max Nr of events are done"; break # stop after some events
    for i in range(file.size()):
      if (nev>TotalEvents): break
      nev=nev+1
      if (nev%500==0):
           if (Nfiles==1): print "Event=",nev
           else: print "Event=",nev," done=",int((100.0*m)/Nfiles),"%", 'x-cross=',xcross 
      eve = file.read(i)
      pa = eve.getParticles()    # particle information
      ve = eve.getEvent()        # event information
      particles=PromcUtil.getParticleDList(file.getHeader(), pa, 1, -1, 1000);
      ktjet.buildJets(particles);
      jets=ktjet.getJetsSorted()
      # mass of 2 leading jets
      if (len(jets)>1):
                 pt1=jets[0].perp()
                 pt2=jets[1].perp()
                 x1=xmin[ipt]  
                 index=ipt+1
                 if indexx1 and pt12): 
                         jets[0].add(jets[1])
                         mass=jets[0].mass()
                         histo[ipt].fill(mass)

    stat = file.getStat()
    xcross=stat.getCrossSectionAccumulated()
    cross=cross+xcross;
    erro=stat.getCrossSectionErrorAccumulated();
    file.close()
  cross=cross/Nfiles
  lumi=nev/cross
  histo[ipt].scale(1.0/(histo[ipt].getBinSize()*lumi))
  print ipt, ") Cross section = %.3e pb"%cross, " Lumi=%.3e pb"%lumi

h1=histo[0] # combine 
h1.setTitle("QCD dijets")
for i in range(1,len(histo)):
    h1=h1.oper(histo[i],"+")
 
c1 = HPlot("HepSim",600,400) # plot histogram
c1.setNameX("M_{ jj} [GeV]")
c1.setNameY("d σ / d M_{ jj} [pb / GeV]")
c1.visible(True)
c1.setMarginLeft(100)
c1.setLogScale(1,True)
c1.setRangeX(1000,50000)
c1.setRangeY(10e-12,10e+2)
c1.draw(h1)
# h1.toTable() 

l1=HLabel("Pythia8 σ=%.3e pb"%cross, 0.5, 0.65, "NDC")
l1.setFont(Font("Helvetica", Font.PLAIN, 14))
c1.add(l1)
l2=HLabel("=HepSim=",0.65,0.84, "NDC")
l2.setFont(Font("Helvetica", Font.PLAIN, 14))
l2.setColor(Color.gray)
c1.add(l2)

# create file/image using name of the file
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)
for i in histo: file.write(i)
file.close()
c1.export(name+".svg");    print name+".svg created"
# h1.toFile(name+".txt");  print name+".txt created"
# sys.exit(0)

HEP.ANL.GOV