source: fact/tools/pyscripts/sandbox/dneise/rdietlic/hist.py@ 14098

Last change on this file since 14098 was 13368, checked in by neise, 13 years ago
initial commit of my sandbox
File size: 4.4 KB
Line 
1#!/usr/bin/python
2#
3# Werner Lustermann
4# ETH Zurich
5#
6
7from ROOT import TH1F, TObjArray
8
9import numpy as np
10
11
12class histogramList( object ):
13 """
14 """
15 def __init__( self, name ):
16 """ set the name and create empty lists """
17 self.name = name # name of the list
18 self.list = [] # list of the histograms
19 self.dict = {} # dictionary of histograms
20 self.hList = TObjArray() # list of histograms for ROOT
21
22 self.book()
23
24 def __del__(self):
25 for h in self.list:
26 h.Delete()
27
28 def add( self, tag, h ):
29 self.list.append( h )
30 self.dict[tag] = h
31 self.hList.Add( h )
32
33 def h1d( self, name, title, Nbin, first, last, xtitle, ytitle ):
34
35 h = TH1F( name, title, Nbin, first, last );
36 h.GetXaxis().SetTitle( xtitle );
37 h.GetYaxis().SetTitle( ytitle );
38
39 # print 'self.add( ',name, ',', h, ' )'
40 self.add( name, h )
41
42 def book( self ):
43 # print 'histogramList::book'
44 pass
45
46
47class hist( object ):
48 """
49 Use numpy histogram function for histogram filling
50 Allows multiple calls for a single histogram
51 Create ROOT histo
52 """
53 def __init__( self, nbins, first, last, name = 'name', title = 'title',
54 xtitle = 'x', ytitle = 'y' ):
55
56 self.nbins = nbins
57 self.first = first
58 self.last = last
59 self.name = name
60 self.title = title
61 self.xtitle = xtitle
62 self.ytitle = ytitle
63 self.y = np.zeros( self.nbins ) # initialize all bins to ZERO
64
65 self.rh = 0
66
67 def fill( self, x ):
68 h, b = np.histogram( x, self.nbins, self.first, self.last,
69 new = True )
70 self.y += h
71
72 def zero( self ):
73 self.y[:] = 0.
74
75 def mkroot( self ):
76
77 if self.rh == 0:
78 self.rh = TH1F( self.name, self.title, self.nbins,
79 self.first, self.last )
80 self.rh.GetXaxis().SetTitle( xtitle );
81 self.rh.GetYaxis().SetTitle( ytitle );
82
83 for i in range( self.nbins ):
84 self.rh.SetBinContent( i+1, self.y[i] )
85
86
87class hist_array( object ):
88 """
89 """
90 def __init__( self, nhist, nbins, first, last, name = 'name',
91 title = 'title', xtitle = 'x', ytitle = 'y', root = True):
92 """
93 - store the histogram info in the object
94 - initialize data array
95 """
96 self.nhist = nhist
97 self.nbins = nbins
98 self.first = first
99 self.last = last
100 self.name = name
101 self.title = title
102 self.xtitle = xtitle
103 self.ytitle = ytitle
104
105 t = np.zeros( self.nhist * self.nbins ) # initialize array ZERO
106 self.y = t.reshape( nhist, nbins ) # shape array of histograms
107
108 if root == True:
109 self.bookRootHistos()
110 else:
111 self.list = 0
112
113 def __del__(self):
114 for h in self.list:
115 h.Delete()
116
117 def fill( self, hist, x ):
118 h, b = np.histogram( x, self.nbins, ( self.first, self.last ),
119 new = True )
120 self.y[ hist ] += h
121
122 def fillall( self, x ):
123 for hist in range( self.nhist ):
124 self.fill( hist, x[hist, :] )
125
126 def zero( self ):
127 self.y.flat[:] = 0.
128
129 def bookRootHistos( self ):
130
131 self.list = [ x for x in range( self.nhist ) ]
132 self.hList = TObjArray()
133
134 for hist in range( self.nhist ):
135
136 hname = self.name + ' ' + str( hist )
137 htitle = self.title + ' ' + str( hist )
138 self.list[hist] = TH1F( hname, htitle,
139 self.nbins, self.first, self.last )
140
141 self.list[hist].GetXaxis().SetTitle( self.xtitle )
142 self.list[hist].GetYaxis().SetTitle( self.ytitle )
143 self.hList.Add( self.list[hist] )
144
145 def SetBinContent( self ):
146
147 if self.list == 0:
148 print 'no root histograms booked'
149 else:
150 for hist in range( self.nhist ):
151 for i in range( self.nbins ):
152 self.list[hist].SetBinContent( i+1, self.y[hist, i] )
153
154
155class hist_list( object ):
156
157 def __init__( self, booking, nhist, hname ):
158
159 self.list = []
160
161 for i in range(nhist):
162 self.list.append( booking( str( hname )+"_"+str(i+1)))
163
164
165
166
Note: See TracBrowser for help on using the repository browser.