Belle II Software development
MonitoringHist Class Reference

Public Member Functions

 __init__ (self, filename, dirname)
 
 sum (self, name)
 
 mean (self, name)
 
 std (self, name)
 
 min (self, name)
 
 max (self, name)
 

Public Attributes

dict values = {}
 Dictionary of bin-contents for each histogram.
 
dict centers = {}
 Dictionary of bin-centers for each histogram.
 
dict two_dimensional = {}
 Dictionary of 2D mode for each histogram.
 
dict nbins = {}
 Dictionary of number of bins for each histogram.
 
 valid = os.path.isfile(filename)
 Indicates if the histograms were successfully read.
 

Detailed Description

Reads all TH1F and TH2F from a ROOT file
and puts them into a more accessible format.

Definition at line 163 of file monitoring.py.

Constructor & Destructor Documentation

◆ __init__()

__init__ ( self,
filename,
dirname )
Reads histograms from the given file
@param filename the name of the ROOT file

Definition at line 169 of file monitoring.py.

169 def __init__(self, filename, dirname):
170 """
171 Reads histograms from the given file
172 @param filename the name of the ROOT file
173 """
174 # Always avoid the top-level 'import ROOT'.
175 import ROOT # noqa
176
177 self.values = {}
178
179 self.centers = {}
180
181 self.two_dimensional = {}
182
183 self.nbins = {}
184
185 self.valid = os.path.isfile(filename)
186
187 if not self.valid:
188 return
189
190 f = ROOT.TFile.Open(filename, 'read')
191 d = f.Get(ROOT.Belle2.MakeROOTCompatible.makeROOTCompatible(dirname))
192
193 for key in d.GetListOfKeys():
194 name = ROOT.Belle2.MakeROOTCompatible.invertMakeROOTCompatible(key.GetName())
195 hist = key.ReadObj()
196 if not (isinstance(hist, ROOT.TH1D) or isinstance(hist, ROOT.TH1F) or
197 isinstance(hist, ROOT.TH2D) or isinstance(hist, ROOT.TH2F)):
198 continue
199 self.two_dimensional[name] = isinstance(hist, ROOT.TH2D) or isinstance(hist, ROOT.TH2F)
200 if self.two_dimensional[name]:
201 nbins = (hist.GetNbinsX(), hist.GetNbinsY())
202 self.centers[name] = [[hist.GetXaxis().GetBinCenter(i) for i in range(nbins[0] + 2)],
203 [hist.GetYaxis().GetBinCenter(i) for i in range(nbins[1] + 2)]]
204 self.values[name] = [[hist.GetBinContent(i, j) for i in range(nbins[0] + 2)] for j in range(nbins[1] + 2)]
205 self.nbins[name] = nbins
206 else:
207 nbins = hist.GetNbinsX()
208 self.centers[name] = np.array([hist.GetBinCenter(i) for i in range(nbins + 2)])
209 self.values[name] = np.array([hist.GetBinContent(i) for i in range(nbins + 2)])
210 self.nbins[name] = nbins
211

Member Function Documentation

◆ max()

max ( self,
name )
Calculates the maximum of a given histogram
@param name key of the histogram

Definition at line 278 of file monitoring.py.

278 def max(self, name):
279 """
280 Calculates the maximum of a given histogram
281 @param name key of the histogram
282 """
283 if name not in self.centers:
284 return np.nan
285 if self.two_dimensional[name]:
286 tempmax = -np.inf
287 for i in range(len(self.values[name])):
288 for j in range(len(self.values[name][i])):
289 if self.values[name][i][j] > tempmax:
290 tempmax = self.centers[name][i][j]
291 return tempmax
292 nonzero = np.nonzero(self.values[name])[0]
293 if len(nonzero) == 0:
294 return np.nan
295 return self.centers[name][nonzero[-1]]
296
297

◆ mean()

mean ( self,
name )
Calculates the mean of a given histogram
@param name key of the histogram

Definition at line 227 of file monitoring.py.

227 def mean(self, name):
228 """
229 Calculates the mean of a given histogram
230 @param name key of the histogram
231 """
232 if name not in self.centers:
233 return np.nan
234 if self.two_dimensional[name]:
235 tempsum = 0
236 for i in range(len(self.values[name])):
237 for j in range(len(self.values[name][i])):
238 tempsum += self.centers[name][i][j] * self.values[name][i][j]
239 return tempsum / self.sum(name)
240 return np.average(self.centers[name], weights=self.values[name])
241

◆ min()

min ( self,
name )
Calculates the minimum of a given histogram
@param name key of the histogram

Definition at line 259 of file monitoring.py.

259 def min(self, name):
260 """
261 Calculates the minimum of a given histogram
262 @param name key of the histogram
263 """
264 if name not in self.centers:
265 return np.nan
266 if self.two_dimensional[name]:
267 tempmin = np.inf
268 for i in range(len(self.values[name])):
269 for j in range(len(self.values[name][i])):
270 if self.values[name][i][j] < tempmin:
271 tempmin = self.centers[name][i][j]
272 return tempmin
273 nonzero = np.nonzero(self.values[name])[0]
274 if len(nonzero) == 0:
275 return np.nan
276 return self.centers[name][nonzero[0]]
277

◆ std()

std ( self,
name )
Calculates the standard deviation of a given histogram
@param name key of the histogram

Definition at line 242 of file monitoring.py.

242 def std(self, name):
243 """
244 Calculates the standard deviation of a given histogram
245 @param name key of the histogram
246 """
247 if name not in self.centers:
248 return np.nan
249 if self.two_dimensional[name]:
250 avg = self.mean(name)
251 tempsum = 0
252 for i in range(len(self.values[name])):
253 for j in range(len(self.values[name][i])):
254 tempsum += self.values[name][i][j] * (self.centers[name][i][j] - avg)**2
255 return np.sqrt(tempsum / self.sum(name))
256 avg = np.average(self.centers[name], weights=self.values[name])
257 return np.sqrt(np.average((self.centers[name] - avg)**2, weights=self.values[name]))
258
STL namespace.

◆ sum()

sum ( self,
name )
Calculates the sum of a given histogram (== sum of all entries)
@param name key of the histogram

Definition at line 212 of file monitoring.py.

212 def sum(self, name):
213 """
214 Calculates the sum of a given histogram (== sum of all entries)
215 @param name key of the histogram
216 """
217 if name not in self.centers:
218 return np.nan
219 if self.two_dimensional[name]:
220 tempsum = 0
221 for i in range(len(self.values[name])):
222 for j in range(len(self.values[name][i])):
223 tempsum += self.values[name][i][j]
224 return tempsum
225 return np.sum(self.values[name])
226

Member Data Documentation

◆ centers

dict centers = {}

Dictionary of bin-centers for each histogram.

Definition at line 179 of file monitoring.py.

◆ nbins

dict nbins = {}

Dictionary of number of bins for each histogram.

Definition at line 183 of file monitoring.py.

◆ two_dimensional

dict two_dimensional = {}

Dictionary of 2D mode for each histogram.

Definition at line 181 of file monitoring.py.

◆ valid

valid = os.path.isfile(filename)

Indicates if the histograms were successfully read.

Definition at line 185 of file monitoring.py.

◆ values

dict values = {}

Dictionary of bin-contents for each histogram.

Definition at line 177 of file monitoring.py.


The documentation for this class was generated from the following file: