plot_impactresponse.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 from math import log10
4 import ROOT
5 
6 def bilogify(hist, lmin = None):
7  """
8  Modify histogram so that it is rescaled in a "bilog" manner.
9  Negative values are -log10(-z). An offset in log10 is returned
10  and represents the 0.
11  """
12  nx,ny = hist.GetNbinsX(), hist.GetNbinsY()
13  zmax = max(abs(hist.GetMinimum()), abs(hist.GetMaximum()))
14 
15  if lmin is None:
16  zmin = sum([abs(hist.GetBinContent(99,iy)) for iy in range(ny)]) / ny
17  lmin = int(log10(zmin))
18 
19  lmax = 1+int(log10(zmax))
20 
21  for ix in range(hist.GetNbinsX()):
22  for iy in range(hist.GetNbinsY()):
23  val = hist.GetBinContent(ix, iy)
24  if val == 0.0:
25  hist.SetBinContent(ix, iy, 0.0)
26  continue
27 
28  sign = 1.0
29  if val > 0: # opposite sign to match Xin
30  sign = -1.0
31  lval = log10(abs(val))
32  if lval < lmin:
33  lval = 0.0
34  else:
35  lval -= lmin
36  lval *= sign
37  hist.SetBinContent(ix, iy, lval)
38  continue
39  continue
40 
41  lhmax = lmax - lmin
42 
43  hist.SetMaximum(lhmax)
44  hist.SetMinimum(-lhmax)
45  return lmin
46 
47 
48 from array import array
49 stops = array('d',[ 0.00, 0.45, 0.50, 0.55, 1.00 ])
50 reds = array('d',[ 0.00, 0.00, 1.00, 1.00, 0.51 ])
51 greens =array('d',[ 0.00, 0.81, 1.00, 0.20, 0.00 ])
52 blues =array('d',[ 0.51, 1.00, 1.00, 0.00, 0.00 ])
53 
54 #ROOT.gStyle.SetPalette(ROOT.kVisibleSpectrum)
55 ROOT.gStyle.SetNumberContours(100)
56 
57 def set_palette(which = "custom"):
58  if not which or which == "custom":
59  ROOT.TColor.CreateGradientColorTable(len(stops), stops, reds, greens, blues, 100)
60  return
61  ROOT.gStyle.SetPalette(which)
62 
63 
64 fp = ROOT.TFile.Open("build/util/test_impactresponse.root")
65 assert (fp)
66 fp.ls()
67 
68 outname = "test_impactresponse.pdf"
69 
70 c = ROOT.TCanvas()
71 c.SetRightMargin(0.15)
72 c.Print(outname+"[","pdf")
73 c.SetGridx()
74 #c.SetGridy()
75 
76 limits = dict(fr = [0.5e-12, 0.3e-12, 0.8e-12],
77  dr = [9.0e-12, 9.0e-12, 20.0e-12])
78 lmins = dict(fr = [-17, -17, -17],
79  dr = [-15, -15, -15])
80 
81 
82 wline = ROOT.TLine() # wire line
83 wline.SetLineColorAlpha(1, 0.5)
84 wline.SetLineStyle(1)
85 
86 hline = ROOT.TLine() # half line
87 hline.SetLineColorAlpha(2, 0.5)
88 hline.SetLineStyle(2)
89 def draw_wires():
90  for wpitch in range(0,36,3):
91  wline.DrawLine(0, wpitch, 100, wpitch)
92  wline.DrawLine(0, -wpitch, 100, -wpitch)
93  if wpitch:
94  hline.DrawLine(0, wpitch-1.5, 100, wpitch-1.5)
95  hline.DrawLine(0, -wpitch+1.5, 100, -wpitch+1.5)
96 
97 for iplane, letter in enumerate("UVW"):
98  for name in ["fr","dr"]:
99  hist = fp.Get("h%s_%c" % (name, letter))
100  assert(hist)
101 
102  set_palette()
103  hist.Draw("colz")
104  draw_wires()
105  lim = limits[name][iplane]
106  hist.GetZaxis().SetRangeUser(-lim, lim)
107  c.Print(outname,"pdf")
108 
109  set_palette(ROOT.kRainBow)
110  lminin = lmins[name][iplane]
111  lminout = bilogify(hist, lminin)
112  print name,iplane,lminin,lminout
113  title = hist.GetTitle()
114  title += " [sign(z)(log10(abs(z)) %d)]" % lminout
115  hist.SetTitle(title)
116  hist.Draw("colz")
117  draw_wires()
118  c.Print(outname,"pdf")
119 
120 c.Print(outname+"]","pdf")
auto enumerate(Iterables &&...iterables)
Range-for loop helper tracking the number of iteration.
Definition: enumerate.h:69
T abs(T value)
def set_palette(which="custom")
def bilogify(hist, lmin=None)
static int max(int a, int b)
auto array(Array const &a)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:228