Belle II Software  release-06-01-15
trackIsolationVariables.py
1 #!/usr/bin/env python3
2 
3 
10 
11 # Doxygen should skip this script
12 # @cond
13 
14 """
15 Example script to calculate track isolation variables.
16 
17 For each particle's track in the input charged stable particle list,
18 calculate the minimal distance to the other candidates' tracks at a given detector entry surface.
19 """
20 
21 import argparse
22 
23 
24 def argparser():
25  """ Argument parser
26  """
27 
28  import stdCharged as stdc
29 
30  parser = argparse.ArgumentParser(description=__doc__,
31  formatter_class=argparse.RawTextHelpFormatter)
32 
33  parser.add_argument("--std_charged",
34  type=str,
35  choices=stdc._chargednames,
36  default="pi",
37  help="The base name of the standard charged particle list to consider.")
38  parser.add_argument("--detectors",
39  type=str,
40  nargs="+",
41  default=["CDC", "PID", "ECL", "KLM"],
42  choices=["CDC", "PID", "ECL", "KLM"],
43  help="List of detectors at whose entry surface track isolation variables will be calculated.\n"
44  "Pass a space-separated list of names.\n"
45  "NB: 'PID' indicates TOP+ARICH entry surface.")
46  parser.add_argument("-d", "--debug",
47  action="store",
48  default=0,
49  type=int,
50  choices=list(range(11, 20)),
51  help="Run the TrackIsoCalculator module in debug mode. Pass the desired DEBUG level integer.")
52 
53  return parser
54 
55 
56 if __name__ == "__main__":
57 
58  # Argparse options.
59  #
60  # NB: Command line arguments are parsed before importing basf2, to avoid PyROOT hijacking them
61  # in case of overlapping option names.
62  args = argparser().parse_args()
63 
64  import basf2 as b2
65  import modularAnalysis as ma
66 
67  # Create path. Register necessary modules to this path.
68  path = b2.create_path()
69 
70  # Add input data and ParticleLoader modules to the path.
71  ma.inputMdstList("default", filelist=[b2.find_file("mdst14.root", "validation")], path=path)
72 
73  # Fill a particle list of charged stable particles.
74  # Apply (optionally) some quality selection.
75  plist_name = f"{args.std_charged}+:my_std_charged"
76  ma.fillParticleList(plist_name, "", path=path)
77  ma.applyCuts(plist_name, "abs(dr) < 2.0 and abs(dz) < 5.0 and p > 0.1", path=path)
78 
79  # 3D distance (default).
80  ma.calculateTrackIsolation(plist_name,
81  path,
82  *args.detectors,
83  alias="dist3DToClosestTrkAtSurface")
84  # 2D distance on rho-phi plane (chord length).
85  ma.calculateTrackIsolation(plist_name,
86  path,
87  *args.detectors,
88  use2DRhoPhiDist=True,
89  alias="dist2DRhoPhiToClosestTrkAtSurface")
90 
91  # Aliases for NTuple variables.
92  ntup_vars = [f"dist3DToClosestTrkAtSurface{det}" for det in args.detectors]
93  ntup_vars += [f"dist2DRhoPhiToClosestTrkAtSurface{det}" for det in args.detectors]
94 
95  # Dump isolation variables in a ntuple.
96  ma.variablesToNtuple(plist_name,
97  ntup_vars,
98  treename=args.std_charged,
99  filename="TrackIsolationVariables.root",
100  path=path)
101 
102  # Optionally activate debug mode for the TrackIsoCalculator module(s).
103  if args.debug:
104  for m in path.modules():
105  if "TrackIsoCalculator" in m.name():
106  m.set_log_level(b2.LogLevel.DEBUG)
107  m.set_debug_level(args.debug)
108 
109  path.add_module("Progress")
110 
111  # Process the data.
112  b2.process(path)
113 
114  print(b2.statistics)
115 
116 # @endcond