#!/usr/bin/env python2

import os, sys, os.path, glob


try:
    pkgpath = sys.argv[1]
except IndexError:
    pkgpath = ".."

# First, remove old files.
old_files = glob.glob('oa_pg_gamma_00001234-5678*')
for i in old_files:
   os.remove(i)

# Next simulate DsECAL muons (use example cfg file).
examplefile = os.getenv("ICEDUSTCONTROLROOT") + "/examples/comet_particle_gun.cfg"

status = os.system("RunIcedustControl -c %s" % (examplefile))
if status != 0:
    sys.exit(status)

# Check that we have an output oaAnalysis file and that this file has
# a tree with the right number of entries.
script = os.getenv("ICEDUSTCONTROLROOT") + "/validate.d/checkOAAnalTree.C"
valid = False
for file in glob.glob('oa_pg_gamma_00001234-5678*_anal_000_bsd*.root'):
    print "found file " + file
    valid = True
    return_code = os.system("root -b -q -l file:"+file+" " + script)
    if return_code != 0:
        print 'Failed quick check of oaAnalysis tree.  Return code = ' +str(return_code)
        print "FAIL"
        sys.exit(return_code)    

if valid == False:
    print "Did not find output oaAnalysis file of form oa_pg_gamma_00001234-5678*"
    print "FAIL"
    sys.exit(1)        



sys.exit(0)