Belle II Software  release-08-01-10
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 
14 import basf2 as b2
15 import ROOT
16 from ROOT.Belle2 import ARICHDatabaseImporter
17 
18 from optparse import OptionParser
19 
20 
21 def 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
55 parser = OptionParser()
56 parser.add_option('-f', '--file', dest='filename', default='DQMhistograms.root')
57 parser.add_option('--hname', dest='hname', default='ARICHDQM/chHit')
58 parser.add_option('-e', '--firstexp', dest='firstexp', default=0, type="int", help="IntevalOfValidity first experiment")
59 parser.add_option('--lastexp', dest='lastexp', default=-1, type="int", help="IntevalOfValidity last experiment")
60 parser.add_option('-r', '--firstrun', dest='firstrun', default=0, type="int", help="IntevalOfValidity first run")
61 parser.add_option('--lastrun', dest='lastrun', default=-1, type="int", help="IntevalOfValidity last run")
62 parser.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
66 b2.use_local_database("localdb/database.txt", "localdb", 0, b2.LogLevel.INFO)
67 
68 print('<runinfo>')
69 print('<run>{}</run>'.format(options.firstrun))
70 print('<exp>{}</exp>'.format(options.firstexp))
71 print('<fname>{}</fname>'.format(options.filename))
72 
73 
74 # open file, get histogram
75 f = ROOT.TFile(options.filename)
76 h = f.Get(options.hname)
77 print('</runinfo>')
78 
79 # get channel mask
80 hmask = GetChannelMask(h)
81 
82 # run the importer
83 if (options.dryrun == 0):
84  ARICHDatabaseImporter().importChannelMask(hmask, options.firstexp, options.firstrun, options.lastexp, options.lastrun)