Belle II Software  release-06-00-14
arich.py
1 # -*- coding: utf-8 -*-
2 
3 
10 
11 '''
12 Validation of ARICH channel masking calibration.
13 '''
14 
15 
16 import basf2
17 from prompt import ValidationSettings
18 import ROOT
19 from ROOT.Belle2 import ARICHCalibrationChecker
20 import sys
21 import subprocess
22 import math
23 import json
24 
25 
26 settings = ValidationSettings(name='ARICH channel masks',
27  description=__doc__,
28  download_files=['stdout'],
29  expert_config={"chunk_size": 100})
30 
31 
32 def run_validation(job_path, input_data_path, requested_iov, expert_config):
33  '''
34  Run the validation.
35  '''
36 
37  expert_config = json.loads(expert_config)
38  chunk_size = expert_config["chunk_size"]
39 
40  # Ignore the ROOT command line options.
41  ROOT.PyConfig.IgnoreCommandLineOptions = True # noqa
42 
43  # Run ROOT in batch mode.
44  ROOT.gROOT.SetBatch(True)
45  # And unset the stat box.
46  ROOT.gStyle.SetOptStat(0)
47 
48  # Path to the database.txt file.
49  database_file = f'{job_path}/ARICHChannelMasks/outputdb/database.txt'
50 
51  # Check the list of runs from the file database.txt.
52  exp_run_dict = {}
53  previous_exp = -666
54  with open(database_file) as f:
55  for line in f:
56  fields = line.split(' ')
57  if (fields[0] == 'dbstore/ARICHChannelMask'):
58  iov = fields[2].split(',')
59  exp = int(iov[0])
60  run = int(iov[1])
61  if (exp != previous_exp):
62  exp_run_dict[exp] = [run]
63  previous_exp = exp
64  else:
65  exp_run_dict[exp].append(run)
66 
67  # Tweak the IoV range if the first run is 0.
68  # This is needed for display purposes.
69  for exp, run_list in exp_run_dict.items():
70  run_list.sort()
71  if len(run_list) > 1:
72  if run_list[0] == 0 and run_list[1] > 5:
73  run_list[0] = run_list[1] - 5
74 
75  # Run the ARICHCalibrationChecker class.
76  for exp, run_list in exp_run_dict.items():
77  for run in run_list:
78  checker = ARICHCalibrationChecker()
79  checker.setExperimentRun(exp, run)
80  checker.setTestingPayload(database_file)
81  basf2.B2INFO(f'Creating arich channel mask results tree for experiment {exp}, run {run}.')
82  checker.setChannelMaskResultsFile(f'channel_mask_exp{exp}_run{run}.root')
83  checker.checkChannelMask()
84 
85  # Run the validation.
86  for exp, run_list in exp_run_dict.items():
87  # For each experiment, merge the files in chunks of some runs.
88  chunks = math.ceil(len(run_list) / chunk_size)
89  for chunk in range(chunks):
90  file_name = f'channel_mask_exp{exp}_chunk{chunk}.root'
91  run_file_names = [
92  f'channel_mask_exp{exp}_run{run}.root' for run in run_list[chunk * chunk_size:(chunk + 1) * chunk_size]]
93  subprocess.run(['hadd', '-f', file_name] + run_file_names, check=True)
94  input_file = ROOT.TFile(f'{file_name}')
95  output_file = ROOT.TFile(f'histograms_{file_name}', 'recreate')
96  output_file.cd()
97  tree = input_file.Get('arich_masked')
98  assert isinstance(tree, ROOT.TTree) == 1
99  canvas = ROOT.TCanvas(f'canvas_exp{exp}_chunk{chunk}', 'canvas', 800, 500)
100  canvas.cd()
101  graphs = []
102  mg = ROOT.TMultiGraph("mg", "ARICH masked channels")
103  leg = ROOT.TLegend(0.9, 0.5, 0.98, 0.9)
104 
105  # draw graphs for each sector
106  for i in range(0, 6):
107  n = tree.Draw(f'frac_masked_sector[{i}]:run', "", "goff")
108  graphs.append(ROOT.TGraph(n, tree.GetV2(), tree.GetV1()))
109  graphs[i].SetMarkerStyle(7)
110  graphs[i].SetMarkerColor(i + 1)
111  graphs[i].SetLineWidth(0)
112  graphs[i].SetTitle(f'sector {i+1}')
113  leg.AddEntry(graphs[i])
114  mg.Add(graphs[i])
115 
116  # draw graph for total
117  n = tree.Draw(f'frac_masked:run', "", "goff")
118  graphs.append(ROOT.TGraph(n, tree.GetV2(), tree.GetV1()))
119  graphs[6].SetMarkerStyle(20)
120  graphs[6].SetMarkerColor(6 + 1)
121  graphs[6].SetLineWidth(0)
122  graphs[6].SetTitle('total')
123  leg.AddEntry(graphs[6])
124  mg.Add(graphs[6])
125 
126  mg.Draw("ap")
127  mg.GetXaxis().SetTitle(f'Exp. {exp} -- Run number')
128  mg.GetYaxis().SetTitle('fraction of masked channels')
129  leg.Draw()
130  ROOT.gPad.SetGridx()
131  ROOT.gPad.SetGridy()
132  canvas.Write()
133  canvas.SaveAs(file_name.replace("root", "pdf"))
134 
135  input_file.Close()
136  output_file.Close()
137  # delete run files
138  subprocess.run(['rm', '-rf'] + run_file_names, check=True)
139 
140 
141 if __name__ == "__main__":
142  run_validation(*sys.argv[1:])