import os, sys, re

from datetime import date
from optparse import OptionParser


from xml.dom.minidom import parse, parseString
import xml.etree.ElementTree as ET

import markup

from markup import oneliner



FILENAME = "filterstats.html"

def write_new_file() :
    title = "Filtered Simulation efficiency statistics"
    header = "" 
    footer = "TODO: automatic extraction of event-type and generation statistics from xml file?????"

    table = ( "Production", "Events in", "Events out", "Efficiency" )


    page = markup.page()
    page.init( title=title, header=header, footer=footer, css= "http://lhcb-release-area.web.cern.ch/LHCb-release-area/DOC/css/lhcb.css") 
    page.h2( "Filtered Simulation efficiency statistics ( by production )")
    page.br()
    page.table( border="1" ) 
    #table headers
    page.tr() 
    page.th( table )
    page.tr.close()
    page.table.close()
    page.br()
    

    #print page
    f = open(FILENAME,'w')
    f.write(page.str())
    f.close()


    return
    

def append_to_file( efflist, lines ) :
 
        v = lines.index('</body>\n')

        newrow = oneliner.tr("")
        newrows = [ newrow[:4]+"\n" ] 
        for d in efflist :
            newtuple = ( d ["prod"], d["in"], d["out"], d["eff"] ) 
            newrows.append( oneliner.td( newtuple )+"\n" )
        newrows += [ newrow[4:]+'\n' ]
             

        newlines = lines[ : v-3 ] + newrows + lines [v-3 : ] 

        htmlfile = open(FILENAME,'w') 
        for line in newlines :
            htmlfile.write(line)
        htmlfile.close()



if __name__=="__main__" :
    opt = OptionParser()
    
    opt.add_option("-o", "--outfile",
                   action="store", type="string", dest="files",
                   default=None,
                   metavar='<OUTFILE>',
                   help="output file [default : NONE ]")
    opt.add_option("-r", "--regen",
                   action="store_true", dest="regen",
                   default=False,
                   metavar='<REGEN>',
                   help="regen file [default : NONE ]")

    (options, args) = opt.parse_args()


    if len(args) == 0 :
        print "no xml files ! "
    else : 
        print "xml files to process", args

    files = args
    print files

    effs = []



    for f in files :
        #xml file to process
        xf = ET.parse(f)
        #base node
        root = xf.getroot()
        inevts = 0 
        for ff in root.find("input").findall("file") :
            #print ff, ff.tag, ff.attrib, ff.text
            inevts += int(ff.text) 
            prodid1 = int(ff.attrib["name"].split("/")[5])
        outevts = 0
        for ff in root.find("output").findall("file") :
            #print ff, ff.tag, ff.attrib, ff.text
            outevts += int(ff.text)
            prodid2 = int(ff.attrib["name"].split("_")[0][4:])
        eff = float(outevts)/float(inevts)
        from math import sqrt
        err = eff * sqrt( 1.0/float(inevts) + 1.0/(float(outevts) ) )
        print f, " in %d out %d with efficiency %g +/- %g"%(inevts,outevts,eff,err)
        effdict = { "prod" : prodid2, "in" : inevts , "out": outevts, "eff" : eff , "err" : err } 
        effs.append( effdict ) 
    

    try :
        
        if options.regen :
            raise ValueError

        htmlfile = open(FILENAME,'r') 
        htmlfile.close()

    except :
        print "no file, generate new file" 
        write_new_file()

    htmlfile = open(FILENAME,'r') 
    lines = htmlfile.readlines()
    htmlfile.close()

    append_to_file ( effs, lines  ) 

