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