import sys
import os
import math
import time
import argparse
import ROOT as R
# from multiprocessing.dummy import Pool as ThreadPool
import pathos.multiprocessing as ThreadPool

from util import *

# R.gInterpreter.Declare('#include "MergeHelper.cc"')

parser = argparse.ArgumentParser()
parser.add_argument("-m", "--MultiProcess",       dest="multi",    default=0,              type=int,    help="enable multi-processing threads (defalt = 0)" )
parser.add_argument("-i", "--InputFile",          dest="inFile",   default="sample.txt",                help="input MC signal sample (defalt = sample.txt)" )
parser.add_argument("-o", "--OutputDir",          dest="outDir",   default="",                        help="output directory (defalt = )" )
parser.add_argument("-r", "--TestRatio",          dest="testRatio",default=0.2,         type=float,  help="ratio of jets used for testing (defalt = 0.200)" )
parser.add_argument("-t", "--Type",               dest="dataType", default="Offline",                   help="data type (defalt = Offline)" )
parser.add_argument("-n", "--NumberOfJets",       dest="NJets",    default=1000,           type=int,    help="number of jets in one file (defalt = 1000)" )
parser.add_argument("-v", "--verbose",            dest="verb",     default=0,              type=int,    help="different verbose level (defalt = 0)" )
parser.add_argument("-b", "--BatchMode",          dest="batch",    default=1,                           help="showing and save figures in pdf (defalt = 1)" )
args = parser.parse_args()

if args.multi != 0:
    R.EnableImplicitMT(args.multi)
    print('using ' + str(args.multi) + ' threads in MT Mode!')
else:
    print('not using MT')

Sample_Lines = ReadSamples(args.inFile)
Sample_Number= len(Sample_Lines)
Sample_type, Training_Samples, Testing_Samples = ClassifySamples(Sample_Lines)

if args.dataType == 'Offline':
    tree_name = 'deepntuplizer/tree'
elif args.dataType == 'Online':
    tree_name = 'ttree'
else:
    print('Please specify a valid data type! (Online or Offline)...EXIT!')
    exit()
    
eosGlobalOutDir='/eos/user/'+os.environ['USER'][0]+'/'+os.environ['USER']+'/DeepNtuples'
if len(args.outDir):
    eosGlobalOutDir=args.outDir
    if not os.path.isdir(eosGlobalOutDir):
        print('please specify a valid output path')
        sys.exit(-1)
        
globalOutDir=eosGlobalOutDir+'/'+time.strftime('%a_%H%M%S')#+'_'+args.jobdir
os.system('mkdir -p '+globalOutDir)
globalOutDir=os.path.realpath(globalOutDir)
    
DoSplitingandMerge = Split_Merge(Training_Samples, Testing_Samples, globalOutDir, tree_name, args.NJets, args.testRatio, args.multi)


import sys
import os
import math
import time
import argparse
import ROOT as R
# from multiprocessing.dummy import Pool as ThreadPool
import pathos.multiprocessing as ThreadPool

from util import *

# R.gInterpreter.Declare('#include "MergeHelper.cc"')

parser = argparse.ArgumentParser()
parser.add_argument("-m", "--MultiProcess",       dest="multi",    default=0,              type=int,    help="enable multi-processing threads (defalt = 0)" )
parser.add_argument("-i", "--InputFile",          dest="inFile",   default="sample.txt",                help="input MC signal sample (defalt = sample.txt)" )
parser.add_argument("-o", "--OutputDir",          dest="outDir",   default="",                        help="output directory (defalt = )" )
parser.add_argument("-r", "--TestRatio",          dest="testRatio",default=0.2,         type=float,  help="ratio of jets used for testing (defalt = 0.200)" )
parser.add_argument("-t", "--Type",               dest="dataType", default="Offline",                   help="data type (defalt = Offline)" )
parser.add_argument("-n", "--NumberOfJets",       dest="NJets",    default=1000,           type=int,    help="number of jets in one file (defalt = 1000)" )
parser.add_argument("-v", "--verbose",            dest="verb",     default=0,              type=int,    help="different verbose level (defalt = 0)" )
parser.add_argument("-b", "--BatchMode",          dest="batch",    default=1,                           help="showing and save figures in pdf (defalt = 1)" )
args = parser.parse_args()

if args.multi != 0:
    R.EnableImplicitMT(args.multi)
    print('using ' + str(args.multi) + ' threads in MT Mode!')
else:
    print('not using MT')

Sample_Lines = ReadSamples(args.inFile)
Sample_Number= len(Sample_Lines)
Sample_type, Training_Samples, Testing_Samples = ClassifySamples(Sample_Lines)

if args.dataType == 'Offline':
    tree_name = 'deepntuplizer/tree'
elif args.dataType == 'Online':
    tree_name = 'ttree'
else:
    print('Please specify a valid data type! (Online or Offline)...EXIT!')
    exit()
    
eosGlobalOutDir='/eos/user/'+os.environ['USER'][0]+'/'+os.environ['USER']+'/DeepNtuples'
if len(args.outDir):
    eosGlobalOutDir=args.outDir
    if not os.path.isdir(eosGlobalOutDir):
        print('please specify a valid output path')
        sys.exit(-1)
        
globalOutDir=eosGlobalOutDir+'/'+time.strftime('%a_%H%M%S')#+'_'+args.jobdir
os.system('mkdir -p '+globalOutDir)
globalOutDir=os.path.realpath(globalOutDir)
    
DoSplitingandMerge = Split_Merge(Training_Samples, Testing_Samples, globalOutDir, tree_name, args.NJets, args.testRatio, args.multi)


# from multiprocessing.dummy import Pool as ThreadPool # R.gInterpreter.Declare('#include "MergeHelper.cc"') #+'_'+args.jobdir