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