Belle II Software  release-08-01-10
MaterialRay.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 
11 
12 from matplotlib import pyplot as pl
13 from basf2 import logging, LogLevel, create_path, process
14 from hist_utils import hist2array
15 import ROOT as root
16 import numpy as np
17 import matplotlib as mpl
18 mpl.use("Agg")
19 
20 # Don't show all the info messages
21 logging.log_level = LogLevel.ERROR
22 
23 # create path
24 main = create_path()
25 # We need to process one event so we need the EventInfoSetter
26 main.add_module("EventInfoSetter", evtNumList=[1])
27 # We need the geometry parameters
28 main.add_module("Gearbox")
29 # as well as the geometry. We assign regions to each creator which allows to
30 # split the material budget by region instead of material.
31 geometry = main.add_module(
32  "Geometry",
33  logLevel=LogLevel.INFO,
34  assignRegions=True)
35 # MaterialScan uses the Geant4 setup which is created by the FullSim module so
36 # we need this as well
37 main.add_module('FullSim')
38 # And finally the MaterialScan module
39 materialscan = main.add_module("MaterialScan", logLevel=LogLevel.INFO)
40 materialscan.param({
41  # Filename for output File
42  'Filename': 'MaterialRay.root',
43  'spherical': False,
44  'planar': False,
45 
46  'ray': True,
47  'ray.theta': 140,
48  'ray.phi': 0,
49  # Alternatively one can set the direction of the ray directly
50  # 'ray.direction': [1, 0, 0],
51  # Max depth for the scan in cm (x axis limit on the histogram). 0 = no limit
52  'ray.maxDepth': 0,
53  # Bin width (roughly) for the output histogram in cm
54  'ray.sampleDepth': 0.1,
55  # Opening angle for the rays: If <=0 only one ray is shot. Otherwise all
56  # rays will be distributed randomly in a cone with this opening angle in
57  # degrees, distributed uniformly in the area (flat in cos(theta)
58  'ray.opening': 0.05,
59  # Number of rays to shoot if opening>0
60  'ray.count': 1000,
61 })
62 
63 # Do the MaterialScan, can take some time depending on the number of steps
64 process(main)
65 
66 # Done, start plotting
67 rmaterial_file = root.TFile("MaterialRay.root")
68 
69 
70 def plot_hist(region, **argk):
71  """Get the histogram for a given region from the file and plot it. Return the histogram data and edges"""
72  h = rmaterial_file.Get("Ray/%s_x0" % region)
73  if not h:
74  return None
75  data, edges = hist2array(h, return_edges=True)
76  data = np.append(data, 0)
77  edges[-1][-1] = h.GetXaxis().GetXmax()
78  # now plot
79  pl.plot(edges[0], data, drawstyle="steps-post", **argk)
80  return data, edges[0]
81 
82 
83 # Get the all materials plot:
84 all_data, mat_edges = plot_hist("All_Regions", label="All regions", c="k")
85 
86 # Now plot the different regions separately
87 for region in ["BeamPipe", "PXD", "SVD", "CDC", "ARICH", "TOP", "ECL", "EKLM", "BKLM", "STR", "COIL"]:
88  plot_hist(region, label=region)
89 
90 # Log scale anyone?
91 # pl.yscale("log")
92 pl.ylim(ymin=0)
93 pl.xlim(xmin=mat_edges[0], xmax=mat_edges[-1] + 1)
94 pl.xlabel("Flight length [cm]")
95 pl.ylabel("Radiation length [$X_0 / %.3g$ cm]" % (mat_edges[1] - mat_edges[0]))
96 pl.legend(loc="best")
97 pl.tight_layout()
98 pl.savefig("MaterialRay.pdf")