from os import walk #Used to list all files in directory

debug = False

jobID = str(input("Enter the jobID:"))
PATH = "/home/lhcb/kopecna/B2KstarMuMu/code/ewp-Bplus2Kstmumu-AngAna/FCNCfitter/condor/output/"

PATH = PATH + jobID +"/"

_, _, fileList = next(walk(PATH), (None, None, [])) 
#Walk returns dirpath, dirnames, filenames, so we don't care about the first two

if (debug): print(fileList)

nFiles = len(fileList)
if (nFiles==0):
    raise ValueError('No files found.')

if (debug): print(nFiles)

nFailedHesse = 0
nFailedSquaredHesse = 0

for outFile in fileList:
    with open(PATH+outFile) as f:
        #Read leaves a cursor at the end of the file, reading twice then is not an option
        #Let's be safe here and instead of saving f.read in memore, let's call seek to return the cursor to the beginning
        if '[warning]	 Hesse returns 2: Full, forced pos def' in f.read(): nFailedHesse+=1
        f.seek(0)
        if "[warning]	 Squared Hesse returns 2: Full, forced pos def" in f.read(): nFailedSquaredHesse+=1

if (debug):
    print ("Failed hesse: ",nFailedHesse)        
    print ("Failed sqaured hesse: ",nFailedSquaredHesse)        

print ("Failed hesse: {0:.1f}% ".format(nFailedHesse/nFiles*100.0))        
print ("Failed sqaured hesse: {0:.1f} %".format(nFailedSquaredHesse/nFiles*100.0))