#!/usr/bin/env python
import os,string,sys,commands,time,ConfigParser
from ROOT import *
from array import array

#histotolook=['/Muons/Run summary/cosmicMuons/NumberOfTracks_sta','/SiStrip/Run summary/Tracks/NumberOfTracks_CKFTk','/SiStrip/Run summary/Tracks/NumberOfTracks_CosmicTk','/L1T/Run summary/L1TGMT/Regional_trigger']
#bintolook=[1,1,1,2]

#histotolook=['/L1T/Run summary/L1TGMT/Regional_trigger']
#bintolook=[2]

HTMLFILE='dqmcosm.html'
HTMLDIR='/afs/cern.ch/user/m/malgeril/www/dqm_stats'

CONFIGFILE='DQMscan.cfg'

CONFIG = ConfigParser.ConfigParser()
print 'Reading configuration file from ',CONFIGFILE

CONFIG.read(CONFIGFILE)

HISTOS_CFG=CONFIG.get('Common','Histos')
TITLE_CFG=CONFIG.get('Common','Title')
WHAT_CFG=CONFIG.get('Common','What')
RUNFILE=CONFIG.get('Common','Runfile')
DQMDIR=CONFIG.get('Common','DQMDir')
TAG=CONFIG.get('Common','TAG')

#histotolook=['/JetMET/Run summary/CaloMETAnalyzer/METTask_CaloMET','/JetMET/Run summary/IterativeConeJets/E']
#bintolook=[-1,-1] # -1 means average

histotolook=string.split(HISTOS_CFG.replace('\n',''),',')
bintolook=string.split(WHAT_CFG.replace('\n',''),',')
title=string.split(TITLE_CFG.replace('\n',''),',')


#sys.exit(1)

#RUNFILE='sel_list_ev_Cosmics_Commissioning08-PromptReco-v2_RECO.66480-99999'
#DQMDIR='/afs/cern.ch/cms/CAF/CMSCOMM/COMM_DQM/data/Cosmics'

DQMLIST='dqmlist'

#main
#first make list of dqm files:
os.system('find '+DQMDIR+' | grep '+TAG+' > '+DQMLIST)

run=[]
event=[]
file=[]
runlist=open(RUNFILE,'r')
numdqmfiles=0

runmin=99999
runmax=0

for line in runlist:
    run.append(line.split()[0])
    event.append(line.split()[1])
    file.append(commands.getoutput('grep '+run[len(run)-1]+' '+DQMLIST))
#    print file[len(file)-1],run[len(run)-1],event[len(event)-1]
    if file[len(file)-1]=='':
        print 'No DQM file available for run '+run[len(run)-1]
    else:
        numdqmfiles=numdqmfiles+1
        if int(run[len(run)-1])<runmin : 
            runmin=int(run[len(run)-1])
        if int(run[len(run)-1])>runmax: 
            runmax=int(run[len(run)-1])
runlist.close()

print 'Using '+str(numdqmfiles)+' dqmfiles in the run range: '+str(runmin)+'-'+str(runmax)
# now we have the full list
# let's open and derive numbers, and create plots
#for ifile in range(0,len(file)):

numhisto=len(histotolook)
numfile=len(file)

noanaev=0
totev=0
evindqm=[0]*numhisto
entries=[0]*numhisto*numfile
bin=[0]*numhisto*numfile
totbin=[0]*numhisto


# loop on dqm files
for i in range(0,numfile):
    totev=totev+int(event[i])
    if file[i]=='':
        #            print 'No DQM file available for run '+run[i]
        noanaev=noanaev+int(event[i])
        for histo in range(0,numhisto):
            entries[i*numhisto+histo]=-1
            bin[i*numhisto+histo]=-1
        continue

#        print 'Opening: '+file[i]
    f1 = TFile(file[i])

# now loop on histos    
    for histo in range(0,numhisto):

#        print 'Analysing '+histotolook[histo]
        histostr='DQMData/Run '+run[i]+histotolook[histo]

        myh = gDirectory.Get(histostr)
        try:
            entries_curr=myh.GetEntries()
        except:
            print 'Problems with '+histostr
            continue

#        print histo,i
        entries[i*numhisto+histo]=entries_curr
        evindqm[histo]=evindqm[histo]+entries_curr

        # define content
        if bintolook[histo].replace('-','').isdigit() :
            # it's a number
            realbin=int(bintolook[histo])
            bin_curr=myh.GetBinContent(abs(realbin))
            # iin case it's negative
            if realbin<0:
                totbin[histo]=totbin[histo]+entries_curr-bin_curr
                if entries_curr!=0:
                    bin_curr=1.-bin_curr/entries_curr
                else:
                    bin_curr=0
#                print bin_curr
            else:
                totbin[histo]=totbin[histo]+bin_curr
                if entries_curr!=0:
                    bin_curr=bin_curr/entries_curr
                else:
                    bin_curr=0

        if bintolook[histo]=='mean':
            bin_curr=myh.GetMean()
        if bintolook[histo]=='rms':
            bin_curr=myh.GetRMS()
            
        bin[i*numhisto+histo]=bin_curr
        #        print i,histo,bin[i][histo]
        # print entries_curr,nomuon_curr
    f1.Close()


# make all tgraph
# now loop on histos
for histo in range(0,numhisto):
    ymax=0
    # for tgraph
    x=array('d')
    y=array('d')
    label=[]
    numpoint=0
    for i in range(0,len(run)):
        if file[i]=='':
            continue
        x.append(i)
        y.append(bin[i*numhisto+histo])
        label.append(run[i])
        numpoint=numpoint+1
#        print i,histo,bin[i][histo]
        if bin[i*numhisto+histo]>ymax:
            ymax=bin[i*numhisto+histo]
            
    ymax=ymax+0.01
    gr=TGraph(int(numpoint),x,y)
#    TAG=string.split(histotolook[histo],'/')[-1]
    TAG=title[histo]
    print TAG
    gr.SetTitle(TAG)
    gr.SetMarkerColor(2)
    gr.SetMarkerStyle(20)
    gr.SetMarkerSize(0.8)
    c1 = TCanvas("c1","c1",1600,900)
    plot = TH1F(TAG,TAG,numpoint,0,numpoint)
    
    gStyle.SetOptStat(0)   
    for ibin in range(0,numpoint):
        plot.GetXaxis().SetBinLabel(ibin+1,label[ibin])
    
    plot.LabelsOption("v","X")
    plot.SetMaximum(ymax)
    plot.Draw()
    gr.Draw("PL")
    c1.Update()
    c1.Print(TAG+'.png')

    print 'Summary for '+histotolook[histo]
    print 'Tot number of events= ',totev
    print 'Number of events in skipped file= ',noanaev
    print 'Number of events in DQM files = ',evindqm[histo]

    print 'Number of events with in selected bin = ',totbin[histo]
    eff=totbin[histo]/evindqm[histo]
    print 'Efficiency = ',eff
    print 'Number of expected events in selected bin = ',eff*totev
    print '+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'



#==================== HTML part

# create html main file
htmlfile=open(HTMLFILE,'w')
htmlfile.write('<html>\n')
htmlfile.write('<body>\n')
htmlfile.write('<h3> Poor man trend plots from DQM offline </h3>\n')
htmlfile.write('<h3> Last updated: '+time.ctime()+' </h3>\n')
    


htmlfile.write('<h5>\n')

for histo in range(0,numhisto):
    TAG=title[histo]
    picfile=TAG+'.png'
    
    htmlfile.write('<a href="'+picfile+'">\n')
    htmlfile.write('<img src="'+picfile+'" alt="'+TAG+'" width="24%" />\n')
    htmlfile.write('</a>\n')

    print 'Copying file: '+picfile
    os.system('cp '+picfile+' '+HTMLDIR)
    
    htmlfile.write('</body>\n</html>\n')
htmlfile.close()

print 'Copying file: '+HTMLFILE
os.system('cp '+HTMLFILE+' '+HTMLDIR)
