Belle II Software development
ARICHImportChannelMaskToDB.py
1#!/usr/bin/env python3
2
3
10
11# Import channel mask from a histogram to conditions database
12# ARICHImportChannelMaskToDB.py -f a.root --hname ARICH/chHit --firstexp=3 --firstrun=2300 --lastexp=4 --lastrun=10
13
14import basf2 as b2
15import ROOT
16from ROOT.Belle2 import ARICHDatabaseImporter
17
18from optparse import OptionParser
19
20
21def GetChannelMask(h):
22 # Explicitly copy
23 nbins = h.GetNbinsX()
24 xlow = h.GetBinLowEdge(1)
25 xup = h.GetBinLowEdge(nbins + 1)
26 mf = ROOT.TF1('mf', 'pol0')
27 h.Fit(mf, 'LQ0')
28
29 hmask = ROOT.TH1S("ARICHChannelMask", "ARICHChannelMask", nbins, xlow, xup)
30 nhot = 0
31 ndead = 0
32 maxHits = int(mf.GetParameter(0) * 20)
33 minHits = 1
34 for bin in range(nbins + 1):
35 value = 1
36 if (h.GetBinContent(bin) > maxHits):
37 value = 0
38 nhot += 1
39 if (h.GetBinContent(bin) < minHits):
40 value = 0
41 ndead += 1
42 hmask.SetBinContent(bin, value)
43 print('<mean>{}</mean>'.format(mf.GetParameter(0)))
44 print('<max>{}</max>'.format(h.GetMaximum()))
45 print('<rms>{}</rms>'.format(h.GetRMS()))
46 print('<maxHits>{}<maxHits>'.format(maxHits))
47 print('<minHits>{}</minHits>'.format(minHits))
48 print('<nall>{}</nall>'.format(nbins))
49 print('<nhot>{}</nhot>'.format(nhot))
50 print('<ndead>{}</ndead>'.format(ndead))
51 return hmask
52
53
54# parameters
55parser = OptionParser()
56parser.add_option('-f', '--file', dest='filename', default='DQMhistograms.root')
57parser.add_option('--hname', dest='hname', default='ARICHDQM/chHit')
58parser.add_option('-e', '--firstexp', dest='firstexp', default=0, type="int", help="IntevalOfValidity first experiment")
59parser.add_option('--lastexp', dest='lastexp', default=-1, type="int", help="IntevalOfValidity last experiment")
60parser.add_option('-r', '--firstrun', dest='firstrun', default=0, type="int", help="IntevalOfValidity first run")
61parser.add_option('--lastrun', dest='lastrun', default=-1, type="int", help="IntevalOfValidity last run")
62parser.add_option('-d', '--dryrun', dest='dryrun', default=0, type="int", help="do not export to database")
63(options, args) = parser.parse_args()
64
65# set local database folder
66b2.use_local_database("localdb/database.txt", "localdb", 0, b2.LogLevel.INFO)
67
68print('<runinfo>')
69print('<run>{}</run>'.format(options.firstrun))
70print('<exp>{}</exp>'.format(options.firstexp))
71print('<fname>{}</fname>'.format(options.filename))
72
73
74# open file, get histogram
75f = ROOT.TFile(options.filename)
76h = f.Get(options.hname)
77print('</runinfo>')
78
79# get channel mask
80hmask = GetChannelMask(h)
81
82# run the importer
83if (options.dryrun == 0):
84 ARICHDatabaseImporter().importChannelMask(hmask, options.firstexp, options.firstrun, options.lastexp, options.lastrun)