def main(argv=None):
    if argv is None:
        argv = sys.argv[1:]

    args = parse_command_line(argv)

    logging.info('Preparing to flatten {0}'.format(args.analysis))

    grid = False
    if 'INPUT' in os.environ and 'OUTPUT' in os.environ:
        print 'We are on the grid'
        inputFileList = os.environ['INPUT']
        outputFile = os.environ['OUTPUT']
        print 'inputfilelist', inputFileList
        print 'outputfile', outputFile
        # figureout the sample
        with open(inputFileList,'r') as f:
            inputfiles = [x.strip() for x in f.readlines()]
            jobparams = inputfiles[0].split('/')
            sample = jobparams[-2]
        print 'inputfiles', inputfiles
        print 'jobparams', jobparams
        print 'sample', sample
        grid = True
    else:
        directories = getSampleDirectories(args.analysis,args.samples)
        logging.info('Will flatten {0} samples'.format(len(directories)))

    if grid:
        flatten(args.analysis,
                sample,
                #inputFileList=inputFileList,
                outputFile=outputFile,
                shift=args.shift,
                )
    elif args.j>1 and hasProgress:
        multi = MultiProgress(args.j)
        for directory in directories:
            sample = directory.split('/')[-1]
            if sample.endswith('.root'): sample = sample[:-5]
            multi.addJob(sample,flatten,args=(args.analysis,sample,),kwargs={'shift':args.shift,'multi':True,'skipHists':args.skipHists,})
        multi.retrieve()
    else:
        for directory in directories:
            sample = directory.split('/')[-1]
            if sample.endswith('.root'): sample = sample[:-5]
            flatten(args.analysis,
                    sample,
                    shift=args.shift,
                    multi=False,
                    skipHists=args.skipHists,
                    )

    logging.info('Finished')
def main(argv=None):
    if argv is None:
        argv = sys.argv[1:]

    args = parse_command_line(argv)

    logging.info('Preparing to flatten {0}'.format(args.analysis))

    grid = False
    if 'INPUT' in os.environ and 'OUTPUT' in os.environ:
        inputFileList = os.environ['INPUT']
        outputFile = os.environ['OUTPUT']
        # figureout the sample
        with open(inputFileList, 'r') as f:
            inputfiles = [x.strip() for x in f.readlines()]
            jobparams = inputfiles[0].split('/')
            sample = jobparams[-2]
        grid = True
    else:
        directories = getSampleDirectories(args.analysis, args.samples)
        logging.info('Will flatten {0} samples'.format(len(directories)))

    if grid:
        flatten(
            args.analysis,
            sample,
            #inputFileList=inputFileList,
            outputFile=outputFile,
            shift=args.shift,
        )
    elif args.j > 1 and hasProgress:
        multi = MultiProgress(args.j)
        for directory in directories:
            sample = directory.split('/')[-1]
            if sample.endswith('.root'): sample = sample[:-5]
            multi.addJob(sample,
                         flatten,
                         args=(
                             args.analysis,
                             sample,
                         ),
                         kwargs={
                             'shift': args.shift,
                             'multi': True,
                         })
        multi.retrieve()
    else:
        for directory in directories:
            sample = directory.split('/')[-1]
            if sample.endswith('.root'): sample = sample[:-5]
            flatten(
                args.analysis,
                sample,
                shift=args.shift,
                multi=False,
            )

    logging.info('Finished')
    endcapCut = 'fabs(e_eta)>1.479'
    lowpt = 'e_pt<50'
    highpt = 'e_pt>100'

    eFlatten.addSelection(promptCut)
    eFlatten.addSelection(fakeCut, postfix='fake')
    eFlatten.addSelection(' && '.join([promptCut, barrelCut]),
                          postfix='barrel')
    eFlatten.addSelection(' && '.join([fakeCut, barrelCut]),
                          postfix='barrel_fake')
    eFlatten.addSelection(' && '.join([promptCut, endcapCut]),
                          postfix='endcap')
    eFlatten.addSelection(' && '.join([fakeCut, endcapCut]),
                          postfix='endcap_fake')
    eFlatten.addSelection(' && '.join([promptCut, lowpt]), postfix='lowpt')
    eFlatten.addSelection(' && '.join([fakeCut, lowpt]), postfix='lowpt_fake')
    eFlatten.addSelection(' && '.join([promptCut, highpt]), postfix='highpt')
    eFlatten.addSelection(' && '.join([fakeCut, highpt]),
                          postfix='highpt_fake')

    eFlatten.flattenAll(progressbar=pbar)


multi = MultiProgress(16)

for directory in glob.glob('{0}/*'.format(sourceDirectory)):
    sample = directory.split('/')[-1]
    multi.addJob(sample, flatten, args=(directory, ))
    #flatten(directory)
multi.retrieve()
def main(argv=None):
    if argv is None:
        argv = sys.argv[1:]

    args = parse_command_line(argv)

    logging.info('Preparing to flatten {0}'.format(args.analysis))

    grid = False
    if 'INPUT' in os.environ and 'OUTPUT' in os.environ:
        inputFileList = os.environ['INPUT']
        outputFile = os.environ['OUTPUT']
        # figureout the sample
        with open(inputFileList,'r') as f:
            inputfiles = [x.strip() for x in f.readlines()]
            jobparams = inputfiles[0].split('/')
            if '.root' in jobparams[-1]:
                sample = jobparams[-2]
                njobs = 1
                job = 0
            else:
                sample = jobparams[-4]
                njobs = int(jobparams[-2])
                job = int(jobparams[-1])
        grid = True
    else:
        directories = getSampleDirectories(args.analysis,args.samples)
        logging.info('Will flatten {0} samples'.format(len(directories)))

    if grid:
        histParams = getSelectedHistParams(args.analysis,args.hists,sample,shift=args.shift,countOnly=args.countOnly)
        histSelections = getSelectedHistSelections(args.analysis,args.selections,sample,shift=args.shift,countOnly=args.countOnly)
        flatten(args.analysis,
                sample,
                histParams=histParams,
                histSelections=histSelections,
                #inputFileList=inputFileList,
                outputFile=outputFile,
                shift=args.shift,
                countOnly=args.countOnly,
                njobs=njobs,
                job=job,
                intLumi=args.intLumi,
                )
    elif args.j>1 and hasProgress:
        multi = MultiProgress(args.j)
        for directory in directories:
            sample = directory.split('/')[-1]
            if sample.endswith('.root'): sample = sample[:-5]
            histParams = getSelectedHistParams(args.analysis,args.hists,sample,shift=args.shift,countOnly=args.countOnly)
            histSelections = getSelectedHistSelections(args.analysis,args.selections,sample,shift=args.shift,countOnly=args.countOnly)
            multi.addJob(sample,flatten,args=(args.analysis,sample,),kwargs={'histParams':histParams,'histSelections':histSelections,'shift':args.shift,'countOnly':args.countOnly,'multi':True,'intLumi':args.intLumi,})
        multi.retrieve()
    else:
        for directory in directories:
            sample = directory.split('/')[-1]
            if sample.endswith('.root'): sample = sample[:-5]
            histParams = getSelectedHistParams(args.analysis,args.hists,sample,shift=args.shift,countOnly=args.countOnly)
            histSelections = getSelectedHistSelections(args.analysis,args.selections,sample,shift=args.shift,countOnly=args.countOnly)
            flatten(args.analysis,
                    sample,
                    histParams=histParams,
                    histSelections=histSelections,
                    shift=args.shift,
                    countOnly=args.countOnly,
                    multi=False,
                    #useProof=args.useProof,
                    intLumi=args.intLumi,
                    )

    logging.info('Finished')
        eFlatten.addHistogram(histName,**params)
    
    promptCut = 'e_genMatch==1 && e_genIsPrompt==1'
    fakeCut = '(e_genMatch==0 || (e_genMatch==1 && e_genIsFromHadron))'
    barrelCut = 'fabs(e_eta)<1.479'
    endcapCut = 'fabs(e_eta)>1.479'
    lowpt = 'e_pt<50'
    highpt = 'e_pt>100'

    eFlatten.addSelection(promptCut)
    eFlatten.addSelection(fakeCut,postfix='fake')
    eFlatten.addSelection(' && '.join([promptCut,barrelCut]),postfix='barrel')
    eFlatten.addSelection(' && '.join([fakeCut,barrelCut]),postfix='barrel_fake')
    eFlatten.addSelection(' && '.join([promptCut,endcapCut]),postfix='endcap')
    eFlatten.addSelection(' && '.join([fakeCut,endcapCut]),postfix='endcap_fake')
    eFlatten.addSelection(' && '.join([promptCut,lowpt]),postfix='lowpt')
    eFlatten.addSelection(' && '.join([fakeCut,lowpt]),postfix='lowpt_fake')
    eFlatten.addSelection(' && '.join([promptCut,highpt]),postfix='highpt')
    eFlatten.addSelection(' && '.join([fakeCut,highpt]),postfix='highpt_fake')

    eFlatten.flattenAll(progressbar=pbar)

multi = MultiProgress(16)

for directory in glob.glob('{0}/*'.format(sourceDirectory)):
    sample = directory.split('/')[-1]
    multi.addJob(sample,flatten,args=(directory,))
    #flatten(directory)
multi.retrieve()