ATLAS Offline Software
HistControl.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
2 
3 import ROOT
4 import array
5 import string
6 import random
7 
8 
9 class HistControl( ROOT.TNamed ):
10 
11  def __init__( self, name = "HistControl", title = "Controls the histograms", doOutputFile=False):
12 
13  # Initialise the base class:
14  ROOT.TNamed.__init__( self, name, title )
15  self.SetName( name )
16  self.SetTitle( title )
17 
18  self.__OneDHists = {}
19  self.__TwoDHists = {}
20  self.__Graphs = {}
21  self.__TProfiles = {}
22  self.__OutputROOTFile = None
23 
24  self.__doOutputFile = doOutputFile
25 
26  return
27 
28  def BookTProfile(self, name, title, numbinx, xmin, xmax):
29  if name in self.__TProfiles.keys():
30  print 'already stored a tprofile object with name', name
31  return False
32  tprof = ROOT.TProfile(name, title, numbinx, xmin, xmax)
33  self.__TProfiles[ name ] = tprof
34  return True
35 
36  def BookHist1D(self, name, title, numbinx, xmin, xmax):
37 
38  if name in self.__OneDHists.keys():
39  print 'already stored a th1f object with name', name
40  return False
41  hist1d = ROOT.TH1F(name, title, numbinx, xmin, xmax)
42  self.__OneDHists[ name ] = hist1d
43  return True
44 
45 
46  def BookHist2D(self, name, title, numbinx, xmin, xmax, numbiny, ymin, ymax):
47  if name in self.__TwoDHists.keys():
48  print 'already stored a th2f object with name', name
49  return False
50  hist2d = ROOT.TH2F(name, title, numbinx, xmin, xmax, numbiny, ymin, ymax)
51  self.__TwoDHists[ name ] = hist2d
52  return True
53 
54 
55  #def BookGraph():
56 
57  def GetRandomString(self, N):
58  str = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(N))
59  print 'randomstring', str
60  return str
61 
62  def SaveTH1FToImage(self, histname, imagename, drawoptions = 'HIST',
63  colors = [ROOT.kRed, ROOT.kBlue, ROOT.kGreen, ROOT.kOrange, ROOT.kCyan, ROOT.kMagenta],
64  linewidth = 2):
65  canvas = ROOT.TCanvas('canvas'+self.GetRandomString(6))
66  canvas.cd(0)
67  if isinstance(histname, list):
68  maxy = -999.
69  for (index,hname) in enumerate(histname):
70  if self.__OneDHists[ hname ].GetMaximum() > maxy:
71  maxy = self.__OneDHists[ hname ].GetMaximum()
72  pass
73  pass
74  for (index,hname) in enumerate(histname):
75  self.__OneDHists[ hname ].SetMaximum(maxy*1.2)
76  pass
77  for (index,hname) in enumerate(histname):
78  self.__OneDHists[ hname ].SetLineWidth(linewidth)
79  if index >= len(colors):
80  print 'add more colors'
81  assert false
82  pass
83  self.__OneDHists[ hname ].SetLineColor(colors[index])
84  if index == 0:
85  self.__OneDHists[ hname ].Draw(drawoptions)
86  else:
87  self.__OneDHists[ hname ].Draw(drawoptions+'SAME')
88  pass
89  pass
90  pass
91  else:
92  self.__OneDHists[ hname ].SetLineWidth(linewidth)
93  self.__OneDHists[ hname ].SetLineColor(colors[0])
94  self.__OneDHists[ hname ].Draw(drawoptions)
95  pass
96  canvas.SaveAs('output/'+imagename+'.png')
97  pass
98 
99  def SaveTH2FToImage(self, histname, imagename, doth2f2graph=False, minY=1., maxY=-1.):
100  canvas = ROOT.TCanvas('canvas'+self.GetRandomString(6))
101  canvas.cd(0)
102  hist_frame = None
103  if (minY<maxY) and doth2f2graph:
104  hist_frame = ROOT.TH1F('hist_frame', self.__TwoDHists[ histname ].GetTitle(),
105  100, self.__TwoDHists[ histname ].GetXaxis().GetXmin(), self.__TwoDHists[ histname ].GetXaxis().GetXmax())
106  hist_frame.SetXTitle( self.__TwoDHists[ histname ].GetXaxis().GetTitle() )
107  hist_frame.SetYTitle( self.__TwoDHists[ histname ].GetYaxis().GetTitle() )
108  hist_frame.SetMinimum(minY)
109  hist_frame.SetMaximum(maxY)
110  if isinstance(histname, list):
111  print 'still need to figure this out'
112  pass
113  else:
114  if doth2f2graph:
115  graph_err = self.TwoDHist2Graph(histname, doErros=True)
116  graph = self.TwoDHist2Graph(histname, doErros=False)
117  if hist_frame is not None:
118  hist_frame.Draw()
119  graph.SetLineWidth(3)
120  graph_err.Draw('3SAME')
121  #graph.SetMarkerSize(0)
122  graph.Draw('CSAME')
123  else:
124  graph.Draw('ALP')
125  pass
126  else:
127  self.__TwoDHists[ histname ].Draw()
128  pass
129  pass
130  canvas.SaveAs('output/'+imagename+'.png')
131  pass
132 
133 
134  def getRMSs(self, hist):
135 
136  mean = hist.GetMean()
137  nentries = hist.GetEntries()
138  rmsDown = 0.
139  rmsUp = 0.
140  nDown = 0
141  nUp = 0
142  print 'mean', mean
143  for bin in range(1, hist.GetNbinsX()+1):
144  if hist.GetBinCenter(bin) < mean:
145  print 'low', hist.GetBinContent(bin)
146  nDown = nDown + hist.GetBinContent(bin)
147  rmsDown += float(hist.GetBinContent(bin))*(mean - float(hist.GetBinCenter(bin)))**2
148  else:
149  nUp = nUp + hist.GetBinContent(bin)
150  rmsUp += float(hist.GetBinContent(bin))*(mean - float(hist.GetBinCenter(bin)))**2
151  pass
152  pass
153  if nDown > 0:
154  rmsDown = ROOT.TMath.Sqrt(rmsDown/float(nentries))
155  else:
156  rmsDown = 0.
157  pass
158  if nUp > 0:
159  rmsUp = ROOT.TMath.Sqrt(rmsUp/float(nentries))
160  else:
161  rmsUp = 0.
162 
163  return (rmsDown,rmsUp)
164 
165 
166  def TwoDHist2Graph(self, name, doErros=False):
167  if not name in self.__TwoDHists.keys():
168  print 'No such TH2F as', name
169  return None
170 
171  hist = self.__TwoDHists[ name ]
172 
173  list_mean = []
174  list_x = []
175  list_xel = []
176  list_yel = []
177  list_xeh = []
178  list_yeh = []
179 
180  for bin in range(1, hist.GetNbinsX()+1 ):
181  hist_projY = hist.ProjectionY('pY'+str(bin), bin, bin)
182  if hist_projY.GetEntries() != 0:
183  mean = hist_projY.GetMean()
184  list_mean.append( mean )
185  list_x.append( hist.GetXaxis().GetBinCenter(bin) )
186  list_xel.append(0.)
187  list_xeh.append(0.)
188  if doErros:
189  (yel,yeh) = self.getRMSs(hist_projY)
190  else:
191  (yel,yeh) = (0., 0.)
192  pass
193  list_yel.append(yel)
194  list_yeh.append(yeh)
195  pass
196  hist_projY.Delete()
197  pass
198 
199  array_mean = array.array('d', list_mean)
200  array_x = array.array('d', list_x)
201  array_xel = array.array('d', list_xel)
202  array_yel = array.array('d', list_yel)
203  array_xeh = array.array('d', list_xeh)
204  array_yeh = array.array('d', list_yeh)
205 
206  if doErros:
207  graph_mean = ROOT.TGraphAsymmErrors(len(list_mean), array_x, array_mean, array_xel,array_xeh,array_yel,array_yeh)
208  graph_mean.SetFillColor(ROOT.kRed)
209  else:
210  graph_mean = ROOT.TGraph(len(list_mean), array_x, array_mean)
211  return graph_mean
212 
213 
214  def FillHist1D(self, name, value):
215  if name in self.__OneDHists:
216  self.__OneDHists[ name ].Fill(value)
217  else:
218  print 'No such th1f with name', name
219  pass
220  return
221 
222  def FillHist2D(self, name, valuex, valuey):
223  if name in self.__TwoDHists:
224  self.__TwoDHists[ name ].Fill(valuex, valuey)
225  else:
226  print 'No such th2f with name', name
227  pass
228  return
229 
230  def FillTProfile(self, name, valuex, valuey):
231  if name in self.__TProfiles:
232  self.__TProfiles[ name ].Fill(valuex, valuey)
233  else:
234  print 'No such tprofile with name', name
235  pass
236  return
237 
238 
239  def SaveAllToFile(self):
240  if not self.__doOutputFile:
241  print 'Cannot save hists to file. No output file set.'
242  return
243 
244  outputFile = ROOT.TFile(self.GetName()+'.root', 'RECREATE')
245  for name1D, hist1D in self.__OneDHists.iteritems():
246  if hist1D.GetEntries() != 0:
247  hist1D.Write()
248  for name2D, hist2D in self.__TwoDHists.iteritems():
249  if hist2D.GetEntries() != 0:
250  hist2D.Write()
251  for nametprof, histtprof in self.__TProfiles.iteritems():
252  histtprof.Write()
253  #for graph in self.__Graphs:
254  # graph.Write()
255  return
HistControl.HistControl.TwoDHist2Graph
def TwoDHist2Graph(self, name, doErros=False)
Definition: HistControl.py:166
python.Bindings.iteritems
iteritems
Definition: Control/AthenaPython/python/Bindings.py:812
HistControl.HistControl.GetRandomString
def GetRandomString(self, N)
Definition: HistControl.py:57
HistControl.HistControl.__init__
def __init__(self, name="HistControl", title="Controls the histograms", doOutputFile=False)
Definition: HistControl.py:11
HistControl.HistControl.__OneDHists
__OneDHists
Definition: HistControl.py:18
HistControl.HistControl
Definition: HistControl.py:9
HistControl.HistControl.BookHist1D
def BookHist1D(self, name, title, numbinx, xmin, xmax)
Definition: HistControl.py:36
HistControl.HistControl.__TProfiles
__TProfiles
Definition: HistControl.py:21
HistControl.HistControl.FillTProfile
def FillTProfile(self, name, valuex, valuey)
Definition: HistControl.py:230
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:195
HistControl.HistControl.__OutputROOTFile
__OutputROOTFile
Definition: HistControl.py:22
HistControl.HistControl.getRMSs
def getRMSs(self, hist)
Definition: HistControl.py:134
TCS::join
std::string join(const std::vector< std::string > &v, const char c=',')
Definition: Trigger/TrigT1/L1Topo/L1TopoCommon/Root/StringUtils.cxx:10
HistControl.HistControl.BookTProfile
def BookTProfile(self, name, title, numbinx, xmin, xmax)
Definition: HistControl.py:28
HistControl.HistControl.SaveTH1FToImage
def SaveTH1FToImage(self, histname, imagename, drawoptions='HIST', colors=[ROOT.kRed, ROOT.kBlue, ROOT.kGreen, ROOT.kOrange, ROOT.kCyan, ROOT.kMagenta], linewidth=2)
Definition: HistControl.py:62
HistControl.HistControl.BookHist2D
def BookHist2D(self, name, title, numbinx, xmin, xmax, numbiny, ymin, ymax)
Definition: HistControl.py:46
HistControl.HistControl.__TwoDHists
__TwoDHists
Definition: HistControl.py:19
HistControl.HistControl.FillHist2D
def FillHist2D(self, name, valuex, valuey)
Definition: HistControl.py:222
HistControl.HistControl.SaveAllToFile
def SaveAllToFile(self)
Definition: HistControl.py:239
HistControl.HistControl.SaveTH2FToImage
def SaveTH2FToImage(self, histname, imagename, doth2f2graph=False, minY=1., maxY=-1.)
Definition: HistControl.py:99
str
Definition: BTagTrackIpAccessor.cxx:11
python.Bindings.keys
keys
Definition: Control/AthenaPython/python/Bindings.py:790
HistControl.HistControl.__doOutputFile
__doOutputFile
Definition: HistControl.py:24
HistControl.HistControl.__Graphs
__Graphs
Definition: HistControl.py:20
readCCLHist.float
float
Definition: readCCLHist.py:83
HistControl.HistControl.FillHist1D
def FillHist1D(self, name, value)
Definition: HistControl.py:214