import ROOT,os,sys,getopt
import rootUtils as ut
import shipunit as u
import numpy as n
from ShipGeoConfig import ConfigRegistry
chi2CutOff  = 4.
PDG = ROOT.TDatabasePDG.Instance()
inputFile  = None
geoFile    = None
dy         = None
nEvents    = 99999
fiducialCut = False
measCut = 25

ROOT.gROOT.SetBatch(1)

try:
        opts, args = getopt.getopt(sys.argv[1:], "n:f:g:A:Y:i", ["nEvents=","geoFile="])
except getopt.GetoptError:
        # print help information and exit:
        print ' enter file name'
        sys.exit()
for o, a in opts:
        if o in ("-f"):
            inputFile = a
        if o in ("-g", "--geoFile"):
            geoFile = a
        if o in ("-Y"): 
            dy = float(a)
        if o in ("-n", "--nEvents="):
            nEvents = int(a)

if not dy:
  # try to extract from input file name
  tmp = inputFile.split('.')
  try:
    dy = float( tmp[1]+'.'+tmp[2] )
  except:
    dy = None
else:
 inputFile = 'ship.'+str(dy)+'.Pythia8-TGeant4_rec.root'

if not geoFile:
  geoFile = inputFile.replace('ship.','geofile_full.').replace('_rec.','.')
  
f     = ROOT.TFile(inputFile)
sTree = f.cbmsim



from LoopAnalyse import LoopAnalyse
la = LoopAnalyse(sTree, dy, geoFile, debug=0, 
		 plotField           = False,
		 plotTracking        = False,
		 plotVertex          = True, 
		 plotVertexDetailed  = False, 
		 plotMomentum        = True,
		 compareFits         = True)
la.Loop(nEvents)