File: diffhisto.py

package info (click to toggle)
vdt 0.4.4-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 620 kB
  • sloc: cpp: 2,382; ansic: 1,589; python: 1,126; csh: 16; makefile: 8
file content (153 lines) | stat: -rw-r--r-- 4,533 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# !/usr/bin/env python

import optparse
import re

#-------------------------------------------------------------------------------

def getTypeLength(cmpfile):
  # read header, determine type length in bits
  typelength = 0
  typelength_s =""
  cmpfile.readline()
  tmp = cmpfile.readline()
  if tmp[0] == "D":
    typelength = 64
    typelength_s = "double"
  else:
    typelength = 32 
    typelength_s = "float"
  return typelength,typelength_s

#-------------------------------------------------------------------------------  

def getYaxisRange(name):
  lr = -5000
  hr = 5000
  if "Asin" in name or "Acos" in name:
    lr = -1
    hr = 1
  if "Log" in name or "Isqrt" in name:
    lr = 0   
  if "Exp" in name:
    lr = -705
    hr = 705
  if "Expf" in name:
    lr = -85
    hr = 85
  return (lr,hr)

#-------------------------------------------------------------------------------      

def getFilenamesFromDir(nick,dirname):
  import os
  return filter (lambda filename: re.match("comparison__" + nick + "__(.*).txt",filename), os.listdir(dirname))

#-------------------------------------------------------------------------------    

def fill_histos(cmpfile,histo1D,histo2D):
  #read lines and process them   
  while 1:
    line = cmpfile.readline()
    if line == "":
      break
    m=re.match("(.*) (.*) (.*) (.*) (.*)",line)
    inputval=float(m.group(5))
    db=int(m.group(4))
    # fill histograms
    histo1D.Fill(db)
    histo2D.Fill(inputval,db)  

#-------------------------------------------------------------------------------    

def compare(nick,dirname):
  import ROOT
  
  # Some globals for the style
  ROOT.gROOT.SetStyle("Plain")
  ROOT.gROOT.SetBatch()
  ROOT.gStyle.SetPalette(1)
   # Do it for all function variants
  ofile = ROOT.TFile("%s_histos.root" %nick,"RECREATE")
  ofile.cd()
  
  #python3 returns an iterator instead of a a list
  # convert to list to be able to run on p2 & p3
  filenames = list( getFilenamesFromDir(nick,dirname) )
  print ( str( len(filenames) ) + " files found." )
  for filename in filenames:      
    print ( "Studying " + filename )
    cmpfile = open(filename)
    typelength,typelength_s =  getTypeLength(cmpfile)
    
    m=re.match("comparison__" + nick + "__(.*).txt" ,filename)
    fcn_name=m.group(1)
    
    # read rest of header   
    for i in range(1,5):
        cmpfile.readline()
    
    # xaxis range
    xmin,xmax=(-0.5,typelength+0.5)
    xNbins=typelength+1
    
    # set up Root 1D histo
    dbhisto = ROOT.TH1F("Diffbit_"+fcn_name, 
                  fcn_name+" diffbit for "+nick+";Diffbit;#",
                  xNbins,
                  xmin,xmax)
    dbhisto.SetLineColor(ROOT.kBlue)
    dbhisto.SetLineWidth(2)
    dbhisto.GetYaxis().SetTitleOffset(1)

    # setup diffbit VS input histogram
    ymin,ymax=getYaxisRange(fcn_name)

    dbVSinhisto = ROOT.TH2F("DiffVsInput_"+fcn_name, 
                    fcn_name+" diffbit vs input for "+nick+";Input;Diffbit",
                    100,ymin,ymax,
                    xNbins,xmin,xmax)
    
    fill_histos(cmpfile,dbhisto,dbVSinhisto)
    
    # draw and save 1D histogram
    dbcanvas = ROOT.TCanvas("dbcanv_" + fcn_name, fcn_name + " diffbit for " + nick + " canvas" ,600,600)
    dbcanvas.cd()
    dbcanvas.SetLogy()
    dbhisto.Draw()
    dbcanvas.Print(nick + "_" + str(typelength_s) + "_" + fcn_name + "_dbhisto.png" )
    dbhisto.Write()


    # draw and save 2D histogram
    dbVSincanvas = ROOT.TCanvas("dbVSincanv_"+ fcn_name, fcn_name + " diffbit for " + nick + " canvas",600,600)
    dbVSincanvas.cd()
    dbVSinhisto.Draw("COLZ")
    dbVSincanvas.Print(nick + "_" + str(typelength_s) + "_" + fcn_name + "_dbVSinhisto.png" )
    
    dbVSinhisto.Write()
    
  ofile.Close()    

#-------------------------------------------------------------------------------      
    
def create_parser():
  import sys
  # set up cmd options
  cmdParser = optparse.OptionParser(usage="%prog -n=<nick>")
  cmdParser.add_option("-n","--nick",dest="nick",help="Determines which comparison files should be used.",default="")
  cmdParser.add_option("-d","--dir",dest="dirname",help="Directory to be looked at.",default="./")
  #parse options and retrieve needed parameters
  (options,args) = cmdParser.parse_args()
  
  if(options.nick == ""):
    cmdParser.print_help()
    sys.exit(1)

  return options

#-------------------------------------------------------------------------------    
  
if __name__ == "__main__":
  options = create_parser()
  compare(options.nick,options.dirname)