import os
import time
from datetime import datetime
import calendar

temp_dir = "/disk/data1/hep/elena/data/ST/Aging"
pwd = os.getcwd()

os.chdir(temp_dir)
files = filter(os.path.isfile, os.listdir(temp_dir))
files = [os.path.join(temp_dir, f) for f in files]
files.sort(key=lambda x: os.path.getmtime(x))

#dec15 = time.mktime(datetime.strptime('15/12/2015', '%d/%m/%Y').timetuple())
# UTC time (ignore time zone)
dec15 = float(calendar.timegm(datetime.strptime('15/12/2015', '%d/%m/%Y').timetuple()))


def modtime(f):
    return os.path.getmtime(f)

files_to_hadd = []
for f in files:
    if modtime(f) > dec15 and 'temp_' in f:
        files_to_hadd.append(f)

#print files_to_hadd
target = temp_dir + '/CCEScan.root'
haddstring = "hadd -f %s %s" % (target, " ".join(files_to_hadd))

print haddstring
os.system(haddstring)
