Belle II Software  release-08-01-10
validation.py
1 #!/usr/bin/env python3
2 
3 
10 
11 """\
12 This module contains classes for plotting validation results.
13 """
14 from pxd.utils.plots import plot_in_module_efficiency, plot_efficiency_vs_run,\
15  plot_module_efficiencies_in_DHHs, plot_efficiency_map, plot_ip_resolutions
16 import uproot
17 import ROOT
18 import numpy as np
19 
20 
21 def analyse_root_file(file_name, tree_name="tree"):
22  """
23  Function to analyse the ROOT file generated from validation algorithm
24  Parameters:
25  file_name (str): name of the ROOT file
26  tree_name (str): name of the TTree
27  """
28  # Get TTree
29  tfile = ROOT.TFile.Open(file_name)
30  tree = tfile.Get(tree_name)
31  # Read pandas.DataFrame from some branches
32  branches = ['exp', 'run', 'pxdid', 'uBin', 'vBin', 'nTrackPoints', 'nSelTrackPoints', 'nSelTrackClusters', 'nTrackClusters']
33  df = uproot.open(file_name)[tree_name].arrays(branches, library='pd')
34  experiment_number = df.exp.min()
35  run_range = (df.run.min(), df.run.max())
36  prefix = f"e{experiment_number:05}_r{run_range[0]:04}-{run_range[1]:04}_"
37 
38  # Calculate efficiencies for all regions
39  df.calculate_eff(num="nTrackClusters", den="nTrackPoints", output_var="eff")
40  df.calculate_eff(num="nSelTrackClusters", den="nSelTrackPoints", output_var="eff_sel")
41 
42  plot_in_module_efficiency(
43  df=df,
44  pxdid=1052,
45  y="eff_sel",
46  x="run",
47  cut="eff_sel>0&eff_sel_err_low<0.01",
48  yerr_low="eff_sel_err_low",
49  yerr_up="eff_sel_err_up",
50  save_to=prefix + "efficiency_in_selected_regions_H1052.png")
51 
52  # Calculate efficiency of the entire PXD
53  df_sum_all = df.groupby(['exp', 'run'])[["nTrackPoints", "nSelTrackPoints",
54  "nTrackClusters", "nSelTrackClusters"]].sum().reset_index()
55  df_sum_all.calculate_eff(num="nTrackClusters", den="nTrackPoints", output_var="eff")
56  df_sum_all.calculate_eff(num="nSelTrackClusters", den="nSelTrackPoints", output_var="eff_sel")
57 
58  plot_efficiency_vs_run(df=df_sum_all, max_err=0.01, save_to=prefix + "pxd_efficiency_vs_run.png")
59 
60  # Calculate efficiences of modules
61  df_module_sum = df.groupby(['exp', 'run', 'pxdid'])[["nTrackPoints", "nSelTrackPoints",
62  "nTrackClusters", "nSelTrackClusters"]].sum().reset_index()
63  df_module_sum["pxdid"] = df_module_sum["pxdid"].astype(np.int)
64  df_module_sum.calculate_eff(num="nTrackClusters", den="nTrackPoints", output_var="eff")
65  df_module_sum.calculate_eff(num="nSelTrackClusters", den="nSelTrackPoints", output_var="eff_sel")
66 
67  plot_module_efficiencies_in_DHHs(df=df_module_sum, save_to=prefix + "efficiency_vs_run.png")
68 
69  # 2d efficiency map
70  c1 = ROOT.TCanvas("c1", "c1", 900, 600)
71  ROOT.gStyle.SetPalette(ROOT.kDarkRainBow)
72  plot_efficiency_map(
73  tree=tree,
74  exp_runs=[
75  0,
76  9999],
77  num_name="hTrackClustersLayer1",
78  den_name="hTrackPointsLayer1",
79  save_to=prefix + "efficiency_l1.png",
80  canvas=c1)
81  plot_efficiency_map(
82  tree=tree,
83  exp_runs=[
84  0,
85  9999],
86  num_name="hTrackClustersLayer2",
87  den_name="hTrackPointsLayer2",
88  title="Efficiency of layer 2",
89  save_to=prefix + "efficiency_l2.png",
90  canvas=c1)
91  # ip resolutions
92  plot_ip_resolutions(tree=tree, save_to=prefix + "pxd_resolutions_vs_run.png")
93 
94 
95 if __name__ == "__main__":
96  import sys
97  file_name = sys.argv[1]
98  ROOT.gROOT.SetBatch(True)
99  analyse_root_file(file_name)