Belle II Software  release-06-01-15
MaterialRay.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 
11 
12 from root_numpy import hist2array
13 from matplotlib import pyplot as pl
14 from basf2 import logging, LogLevel, create_path, process
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  # Now we have to play around a bit: pl.plot(edges[:-1], data, drawstyle="steps-post") does work but
77  # the last bin does not get a top line so we need to add a 0 to the
78  # histogram to go back down to where we were
79  data = np.append(data, 0)
80  # now plot
81  pl.plot(edges[0], data, drawstyle="steps-post", **argk)
82  return data, edges[0]
83 
84 
85 # Get the all materials plot:
86 all_data, mat_edges = plot_hist("All_Regions", label="All regions", c="k")
87 
88 # Now plot the different regions separately
89 for region in ["BeamPipe", "PXD", "SVD", "CDC", "ARICH", "TOP", "ECL", "EKLM", "BKLM", "STR", "COIL"]:
90  plot_hist(region, label=region)
91 
92 # Log scale anyone?
93 # pl.yscale("log")
94 pl.ylim(ymin=0)
95 pl.xlim(xmin=mat_edges[0], xmax=mat_edges[-1] + 1)
96 pl.xlabel("Flight length [cm]")
97 pl.ylabel("Radiation length [$X_0 / %.3g$ cm]" % (mat_edges[1] - mat_edges[0]))
98 pl.legend(loc="best")
99 pl.tight_layout()
100 pl.savefig("MaterialRay.pdf")