#!/usr/bin/python
import sys,os
import string

from ROOT import TChain,TH1F, TLorentzVector, TVector2, gROOT, TTree, AddressOf, TFile
from math import cosh

boosted= False
athena=True
hfor=False
isEvgen=False
data=False

count=0

if (len(sys.argv) < 2):
   print ' Error'

filename=string.split(sys.argv[2],",")
if 'input' in filename[0] and '.txt' in filename[0] :
   templist= open( filename[0] , 'r' ).readlines()
   filename=[]
   for item in templist:
      if item[0] == '#' :
         print item
      else :
         filename.append(item[:-1])

oname=sys.argv[1]
print "Running a test on ", filename, "output", oname
# run inside athena
if athena:
   gROOT.ProcessLine("gSystem->Load(\"libCintex.so\" )")
   gROOT.ProcessLine("Cintex::Cintex::Enable()")
   
treename="truth"

chain=TChain(treename)

for file in filename:
   chain.Add(file)
   
myfile=TFile(oname,"RECREATE")
########################################  
########################################             
# --- create output tree --- #
gROOT.ProcessLine(
   "struct MyCopy{\
   std::vector<float> mc_pt;\
   };");
from ROOT import MyCopy
mycopy = MyCopy()
########################################
max_events=5  
entries=chain.GetEntries()

chain.SetBranchStatus('*',0)
chain.SetBranchStatus( "mc_pt", 1 )
chain.SetBranchAddress("mc_pt",AddressOf(mycopy,'mc_pt'))

n_event=0;
frac_last=0

for jentry in xrange( 0, max_events ):
   nb = chain.GetEntry( jentry )
   if nb <= 0:
      continue
   print mycopy.mc_pt
   
   myfile.Close()
   
print "Completed"

print "exiting"
sys.exit(0)
