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