inputFileNames = [f  for i in inputs for f in glob.glob(i if i.endswith(ext) else i+'/*'+ext)]
assert channel in validChannels,"Invalid channel %s (should be one of %s)" % (channel, str(validChannels))
inputFiles = [r.TFile.Open(f) for f in inputFileNames]

if verbose :
    print "Options:\n" \
          + '\n'.join(["%s : %s" % (o, eval(o))
                       for o in ['channel', 'printData', 'printTotBkg',
                                 'rawcnt', 'referenceHisto', 'referenceSyst','selRegexp',
                                 'pickleFile']])
    print 'Input files:\n'+'\n'.join(inputFileNames)

# navigate the files and collect the histos
referenceType = HistoType(pr='', ch=channel, var=referenceHisto, syst=referenceSyst)
histosByType = collections.defaultdict(list)
classifier = HistoNameClassifier()

histoNames = []
for fname, infile in zip(inputFileNames, inputFiles) :
    sample = guessGroupFromFilename(fname)
    setType, setSample = setHistoType, setHistoSample
    def getType(histoName) : return classifier.histoType(histoName)
    def isRightType(histo) : return referenceType.matchAllAvailabeAttrs(histo.type)
    histonamesCached = len(histoNames)>0
    if not histonamesCached : histoNames = getAllHistoNames(infile, onlyTH1=True, nameStem=histoname)
    histos = filter(isRightType, map(lambda hn :
                                     setSample(setType(infile.Get(hn), getType(hn)), sample),
                                     histoNames))
    if not histonamesCached : histoNames = [h.GetName() for h in histos] # after filtering
    organizeHistosByType(histosByType, histos)
refHistos = histosByType # already filtered histonames, all histosByType are refHistos