source: fact/tools/pyscripts/pyfact/hist.py@ 13148

Last change on this file since 13148 was 13143, checked in by neise, 13 years ago
included python -tt option. thus checking for tabs/spaces
File size: 6.2 KB
Line 
1#!/usr/bin/python -tt
2#
3# Werner Lustermann
4# ETH Zurich
5#
6
7from ROOT import TH1F, TFile, TObjArray
8import numpy as np
9
10
11class histogramList( object ):
12 """ maintain a list of ROOT histograms """
13
14 def __init__( self, name ):
15 """ set the name and create empty lists """
16 self.name = name # name of the list
17 self.list = [] # list of the histograms
18 self.dict = {} # dictionary of histograms
19 self.hList = TObjArray() # list of histograms for ROOT
20
21 self.book()
22
23 def __call__(self, tag, value):
24 """ fill value in the correct histo """
25
26 self.dict[tag].Fill(value)
27
28 def __del__(self):
29 for h in self.list:
30 h.Delete()
31
32 def add( self, tag, h ):
33 self.list.append( h )
34 self.dict[tag] = h
35 self.hList.Add( h )
36
37 def h1d( self, name, title, Nbin, first, last, xtitle, ytitle ):
38
39 h = TH1F( name, title, Nbin, first, last );
40 h.GetXaxis().SetTitle( xtitle );
41 h.GetYaxis().SetTitle( ytitle );
42
43 # print 'self.add( ',name, ',', h, ' )'
44 self.add( name, h )
45
46 def book( self ):
47 """ booking of histograms
48
49 to be overwritten by the user with hist own definitions
50 """
51 # print 'histogramList::book'
52 pass
53
54
55class hist( object ):
56 """
57 Use numpy histogram function for histogram filling
58 Allows multiple calls for a single histogram
59 Create ROOT histo
60 """
61 def __init__( self, name, title, nbins, first, last,
62 xtitle = 'x', ytitle = 'y' ):
63
64 self.nbins = nbins
65 self.first = first
66 self.last = last
67 self.name = name
68 self.title = title
69 self.xtitle = xtitle
70 self.ytitle = ytitle
71 self.y = np.zeros( self.nbins ) # initialize all bins to ZERO
72
73 self.rh = 0
74
75 def fill( self, x ):
76 h, b = np.histogram( x, self.nbins, (self.first,self.last) )
77 self.b = b
78 self.y += h
79
80 def __call__(self, x):
81 self.fill(x)
82
83
84 def zero( self ):
85 self.y[:] = 0.
86
87 def mkroot( self ):
88
89 if self.rh == 0:
90 self.rh = TH1F( self.name, self.title, self.nbins,
91 self.first, self.last )
92 self.rh.GetXaxis().SetTitle( self.xtitle );
93 self.rh.GetYaxis().SetTitle( self.ytitle );
94
95 for i in range( self.nbins ):
96 self.rh.SetBinContent( i+1, self.y[i] )
97
98
99class hist_array( object ):
100 """ array of 1d histograms data
101
102 y : np.array( nhist, nbins ), each row is representing a 1d histogram
103
104 all histograms have identical binning
105 histogramming of data vectors is done using np.histogram
106
107 creating of ROOT histograms is optional
108 """
109 def __init__( self, nhist, nbins, first, last, name = 'name',
110 title = 'title', xtitle = 'x', ytitle = 'y', root = True):
111 """ store histogram info and initialize data array
112 nhist : number of histograms
113 nbins : number of bins in a histogram
114 first : first bin
115 last : last bin
116 name : name of the histogram
117 title : title of the histogram
118 xtitle : label for the x axe
119 ytitle : label for the y axe
120
121 y : np.array( nhist, nbins ), each row is representing a 1d histogram
122 """
123 self.nhist = nhist
124 self.nbins = nbins
125 self.first = first
126 self.last = last
127 self.name = name
128 self.title = title
129 self.xtitle = xtitle
130 self.ytitle = ytitle
131
132 t = np.zeros( self.nhist * self.nbins ) # initialize array ZERO
133 self.y = t.reshape( nhist, nbins ) # shape array of histograms
134
135 # creating of ROOT histograms
136 if root == True:
137 self.bookRootHistos()
138 else:
139 self.list = 0
140
141 def __del__(self):
142 """ delete the ROOT histogram objects """
143
144 for h in self.list:
145 h.Delete()
146
147 def fill( self, hist, x ):
148 """ compute histogram for new data and add them to existing histogram
149
150 hist : index of the histogram { 0:nhist }
151 x : data vector
152 """
153
154 h, b = np.histogram( x, self.nbins, ( self.first, self.last ),
155 new = True )
156 self.y[ hist ] += h
157
158 def fillall( self, x ):
159 """ compute all histograms for now data array and add them to existing histograms
160
161 x : np.array( nhist, nbins ), data array in the shape of the histogram array
162 """
163 for hist in range( self.nhist ):
164 self.fill( hist, x[hist, :] )
165
166 def zero( self ):
167 """ set histogram array to ZERO
168 """
169 self.y.flat[:] = 0.
170
171 def bookRootHistos( self ):
172 """ book ROOT histograms equivalent to the histogram array
173
174 self:
175 list : list containing the ROOT histogram objects
176 hList : ROOT TObjArray containing all histograms
177 """
178 self.list = [ x for x in range( self.nhist ) ]
179 self.hList = TObjArray()
180
181 for hist in range( self.nhist ):
182
183 hname = self.name + ' ' + str( hist )
184 htitle = self.title + ' ' + str( hist )
185 self.list[hist] = TH1F( hname, htitle,
186 self.nbins, self.first, self.last )
187
188 self.list[hist].GetXaxis().SetTitle( self.xtitle )
189 self.list[hist].GetYaxis().SetTitle( self.ytitle )
190 self.hList.Add( self.list[hist] )
191
192 def SetBinContent( self ):
193 """ set the ROOT contents
194 """
195 if self.list == 0:
196 print 'no root histograms booked'
197 else:
198 for hist in range( self.nhist ):
199 for i in range( self.nbins ):
200 self.list[hist].SetBinContent( i+1, self.y[hist, i] )
201
202
203def SaveHistograms( histogramLists, fname = 'histo.root', opt = 'RECREATE' ):
204 """
205 Saves all histograms in all given histogram lists to a root file
206 Each histogram list is saved to a separate directory
207 """
208 rf = TFile( fname, opt)
209
210 for list in histogramLists:
211 rf.mkdir( list.name )
212 rf.cd( list.name )
213 list.hList.Write()
214
215 rf.Close()
Note: See TracBrowser for help on using the repository browser.