CMS 3D CMS Logo

plot_utils.py
Go to the documentation of this file.
1 from ROOT import TStyle, kWhite, kTRUE
2 from ROOT import kGray, kAzure, kMagenta, kOrange, kWhite
3 from ROOT import kRed, kBlue, kGreen, kPink, kYellow
4 from ROOT import TLine, TLatex
5 
6 from collections import namedtuple, OrderedDict
7 from math import sin, cos, tan, atan, exp, pi
8 
9 Plot_params = namedtuple('Params',
10  ['plotNumber',
11  'abscissa', 'ordinate',
12  'ymin', 'ymax',
13  'xmin', 'xmax',
14  'quotaName', 'iDrawEta',
15  'histoMin', 'histoMax',
16  'zLog', 'iRebin'])
17 plots = {}
18 plots.setdefault('x_vs_eta', Plot_params(10, '#eta', 'x/X_{0}', 0.0, 2.575, -4.0, 4.0, '', 0, 0., 0., 0, 1))
19 plots.setdefault('x_vs_phi', Plot_params(20, '#varphi [rad]', 'x/X_{0}', 0.0, 6.2, -4.0, 4.0, '', 0, 0., 0., 0, 1))
20 plots.setdefault('x_vs_R', Plot_params(40, 'R [cm]', 'x/X_{0}', 0.0, 70.0, 0.0, 1200.0, '', 0, 0., 0., 0, 1))
21 plots.setdefault('l_vs_eta', Plot_params(1010, '#eta', 'x/#lambda_{I}', 0.0, 0.73, -4.0, 4.0, '', 0, 0., 0., 0, 1))
22 plots.setdefault('l_vs_phi', Plot_params(1020, '#varphi [rad]', 'x/#lambda_{I}', 0.0, 1.2, -4.0, 4.0, '', 0, 0., 0., 0, 1))
23 plots.setdefault('l_vs_R', Plot_params(1040, '#R [cm]', 'x/#lambda_{I}', 0.0, 7.5, 0.0, 1200.0, '', 0, 0., 0., 0, 1))
24 plots.setdefault('x_vs_eta_vs_phi', Plot_params(30, '#eta', '#varphi', 0., 0., 0., 0., 'x/X_{0}', 0, -1., -1., 0, 1))
25 plots.setdefault('l_vs_eta_vs_phi', Plot_params(1030, '#eta', '#varphi', 0., 0., 0., 0., 'x/#lambda_{I}', 0, -1, -1, 0, 1))
26 plots.setdefault('x_vs_z_vs_Rsum', Plot_params(50, 'z [mm]', 'R [mm]', 0., 0., 0., 0., '#Sigmax/X_{0}', 1, 0., 2.5, 0, 0))
27 plots.setdefault('x_vs_z_vs_R', Plot_params(60, 'z [mm]', 'R [mm]', 0., 0., 0., 0., '1/X_{0}', 1, 0.00001, 0.01, 1, 0))
28 plots.setdefault('l_vs_z_vs_Rsum', Plot_params(1050, 'z [mm]', 'R [mm]', 0., 0., 0., 0., '#Sigmax/#lambda_{I}', 1, 0., 1., 0, 0))
29 plots.setdefault('l_vs_z_vs_R', Plot_params(1060, 'z [mm]', 'R [mm]', 0., 0., 0., 0., '1/#lambda_{I}', 1, 0.001, 0.9, 1, 0))
30 plots.setdefault('x_over_l_vs_eta', Plot_params(10, '#eta', '(x/X_{0})/(x/#lambda_{I})', 0., 0., 0., 0., '', 0, -1, -1, 0, 0))
31 plots.setdefault('x_over_l_vs_phi', Plot_params(20, '#varphi [rad]', '(x/X_{0})/(x/#lambda_{I})', 0., 0., 0., 0., '', 0, -1, -1, 0, 0))
32 
33 # Conversion name from the label (key) to the components in CMSSW/Geometry
34 _LABELS2COMPS = {'BeamPipe': 'BEAM',
35  'Tracker': 'Tracker',
36  'Pixel': ['PixelBarrel', 'PixelForwardZplus', 'PixelForwardZminus'],
37  'PixBar': 'PixelBarrel',
38  'PixFwd': ['PixelForwardZplus', 'PixelForwardZminus', 'PixelForward'],
39  'PixFwdMinus': 'PixelForwardZminus',
40  'PixFwdPlus': 'PixelForwardZplus',
41  'TIB': 'TIB',
42  'TOB': 'TOB',
43  'TIDB': 'TIDB',
44  'TIDF': 'TIDF',
45  'TEC': 'TEC',
46  'InnerServices': ['TIBTIDServicesF', 'TIBTIDServicesB'],
47  'TkStrct': ['TrackerOuterCylinder', 'TrackerBulkhead'],
48  'Phase2PixelBarrel': 'Phase2PixelBarrel',
49  'Phase2OTBarrel': 'Phase2OTBarrel',
50  'Phase2PixelEndcap': 'Phase2PixelEndcap',
51  'Phase2OTForward': 'Phase2OTForward'}
52 
53 # Compounds are used to stick together different part of the Tracker
54 # detector, so that their cumulative material description can be
55 # derived. The key name can be generic, while the names in the
56 # associated list must be such that an appropriate material
57 # description file, in ROOT format, is present while producing the
58 # cumulative plot. A missing element will invalidate the full
59 # procedure.
60 COMPOUNDS = OrderedDict()
61 COMPOUNDS["Tracker"] = ["Tracker"]
62 COMPOUNDS["TrackerSum"] = ["TIB", "TIDF", "TIDB",
63  "BeamPipe", "InnerServices",
64  "TOB", "TEC",
65  "TkStruct",
66  "PixBar", "PixFwdPlus", "PixFwdMinus"]
67 COMPOUNDS["TrackerSumPhaseII"] = ["BeamPipe",
68  "Phase2PixelBarrel",
69  "Phase2OTBarrel", "Phase2OTForward",
70  "Phase2PixelEndcap"]
71 COMPOUNDS["Pixel"] = ["PixBar", "PixFwdMinus", "PixFwdPlus"]
72 COMPOUNDS["Strip"] = ["TIB", "TIDF", "TIDB", "InnerServices", "TOB", "TEC"]
73 COMPOUNDS["InnerTracker"] = ["TIB", "TIDF", "TIDB", "InnerServices"]
74 
75 # The DETECTORS must be the single component of the tracker for which
76 # the user can ask for the corresponding material description.
77 DETECTORS = OrderedDict()
78 DETECTORS["BeamPipe"] = kGray+2
79 DETECTORS["InnerServices"] = kGreen+2
80 DETECTORS["PixBar"] = kAzure-5
81 DETECTORS["Phase1PixelBarrel"] = kAzure-5
82 DETECTORS["Phase2PixelBarrel"] = kAzure-5
83 DETECTORS["PixFwdPlus"] = kAzure-9
84 DETECTORS["PixFwdMinus"] = kAzure-9
85 DETECTORS["Phase2PixelEndcap"] = kAzure-9
86 DETECTORS["Phase2OTBarrel"] = kMagenta-2
87 DETECTORS["Phase2OTForward"] = kOrange-2
88 DETECTORS["TIB"] = kMagenta-6
89 DETECTORS["TIDF"] = kMagenta+2
90 DETECTORS["TIDB"] = kMagenta+2
91 DETECTORS["TOB"] = kOrange+10
92 DETECTORS["TEC"] = kOrange-2
93 
94 # sDETS are the label of the Tracker elements in the Reconstruction
95 # geometry. They are all used to derive the reconstruction material
96 # profile to be compared to the one obtained directly from the
97 # simulation. A missing key in the real reconstruction geometry is not
98 # a problem, since this will imply that the corresponding plotting
99 # routine will skip that missing part. For this reason this map can be
100 # made as inclusive as possible with respect to the many
101 # reconstruction geometries in CMSSW.
102 sDETS = OrderedDict()
103 sDETS["PXB"] = kRed
104 sDETS["PXF"] = kBlue
105 sDETS["TIB"] = kGreen
106 sDETS["TID"] = kYellow
107 sDETS["TOB"] = kOrange
108 sDETS["TEC"] = kPink
109 
110 # hist_label_to_num contains the logical names of the Tracker detector
111 # that holds material. They are therefor not aware of which detector
112 # they belong to, but they are stored in specific plots in all the
113 # mat*root files produced. The numbering of the plots is identical
114 # across all files.
115 hist_label_to_num = OrderedDict()
116 hist_label_to_num['SUP'] = [100, 13, 'Support'] # [Index , color, legend label]
117 hist_label_to_num['SEN'] = [200, 27, 'Sensitive']
118 hist_label_to_num['CAB'] = [300, 46, 'Cables']
119 hist_label_to_num['COL'] = [400, 38, 'Cooling']
120 hist_label_to_num['ELE'] = [500, 30, 'Electronics']
121 hist_label_to_num['OTH'] = [600, 42, 'Other']
122 hist_label_to_num['AIR'] = [700, 29, 'Air']
123 
125  """Function to setup a TDR-like style"""
126 
127  tdrStyle = TStyle("tdrStyle","Style for P-TDR")
128 
129  # For the canvas:
130  tdrStyle.SetCanvasBorderMode(0)
131  tdrStyle.SetCanvasColor(kWhite)
132  tdrStyle.SetCanvasDefH(600) #Height of canvas
133  tdrStyle.SetCanvasDefW(600) #Width of canvas
134  tdrStyle.SetCanvasDefX(0) #Position on screen
135  tdrStyle.SetCanvasDefY(0)
136 
137  # For the Pad:
138  tdrStyle.SetPadBorderMode(0)
139  tdrStyle.SetPadColor(kWhite)
140  tdrStyle.SetPadGridX(False)
141  tdrStyle.SetPadGridY(False)
142  tdrStyle.SetGridColor(0)
143  tdrStyle.SetGridStyle(3)
144  tdrStyle.SetGridWidth(1)
145 
146  # For the frame:
147  tdrStyle.SetFrameBorderMode(0)
148  tdrStyle.SetFrameBorderSize(1)
149  tdrStyle.SetFrameFillColor(0)
150  tdrStyle.SetFrameFillStyle(0)
151  tdrStyle.SetFrameLineColor(1)
152  tdrStyle.SetFrameLineStyle(1)
153  tdrStyle.SetFrameLineWidth(1)
154 
155  # For the histo:
156  tdrStyle.SetHistLineColor(1)
157  tdrStyle.SetHistLineStyle(0)
158  tdrStyle.SetHistLineWidth(1)
159  tdrStyle.SetEndErrorSize(2)
160  tdrStyle.SetErrorX(0.)
161  tdrStyle.SetMarkerStyle(20)
162 
163  #For the fit/function:
164  tdrStyle.SetOptFit(1)
165  tdrStyle.SetFitFormat("5.4g")
166  tdrStyle.SetFuncColor(2)
167  tdrStyle.SetFuncStyle(1)
168  tdrStyle.SetFuncWidth(1)
169 
170  #For the date:
171  tdrStyle.SetOptDate(0)
172 
173  # For the statistics box:
174  tdrStyle.SetOptFile(0)
175  tdrStyle.SetOptStat(0); # To display the mean and RMS: SetOptStat("mr")
176  tdrStyle.SetStatColor(kWhite)
177  tdrStyle.SetStatFont(42)
178  tdrStyle.SetStatFontSize(0.025)
179  tdrStyle.SetStatTextColor(1)
180  tdrStyle.SetStatFormat("6.4g")
181  tdrStyle.SetStatBorderSize(1)
182  tdrStyle.SetStatH(0.1)
183  tdrStyle.SetStatW(0.15)
184 
185  # Margins:
186  tdrStyle.SetPadTopMargin(0.05)
187  tdrStyle.SetPadBottomMargin(0.13)
188  tdrStyle.SetPadLeftMargin(0.16)
189  tdrStyle.SetPadRightMargin(0.02)
190 
191  # For the Global title:
192  tdrStyle.SetOptTitle(0)
193  tdrStyle.SetTitleFont(42)
194  tdrStyle.SetTitleColor(1)
195  tdrStyle.SetTitleTextColor(1)
196  tdrStyle.SetTitleFillColor(10)
197  tdrStyle.SetTitleFontSize(0.05)
198 
199  # For the axis titles:
200  tdrStyle.SetTitleColor(1, "XYZ")
201  tdrStyle.SetTitleFont(42, "XYZ")
202  tdrStyle.SetTitleSize(0.06, "XYZ")
203  tdrStyle.SetTitleXOffset(0.9)
204  tdrStyle.SetTitleYOffset(1.25)
205 
206  # For the axis labels:
207  tdrStyle.SetLabelColor(1, "XYZ")
208  tdrStyle.SetLabelFont(42, "XYZ")
209  tdrStyle.SetLabelOffset(0.007, "XYZ")
210  tdrStyle.SetLabelSize(0.05, "XYZ")
211 
212  # For the axis:
213  tdrStyle.SetAxisColor(1, "XYZ")
214  tdrStyle.SetStripDecimals(kTRUE)
215  tdrStyle.SetTickLength(0.03, "XYZ")
216  tdrStyle.SetNdivisions(510, "XYZ")
217  tdrStyle.SetPadTickX(1) # To get tick marks on the opposite side of the frame
218  tdrStyle.SetPadTickY(1)
219 
220  # Change for log plots:
221  tdrStyle.SetOptLogx(0)
222  tdrStyle.SetOptLogy(0)
223  tdrStyle.SetOptLogz(0)
224 
225  # Postscript options:
226  tdrStyle.SetPaperSize(20.,20.)
227 
228  tdrStyle.cd()
229 
231  """Function to draw the eta.
232 
233  Function to draw the eta references on top of an already existing
234  TCanvas. The lines and labels drawn are collected inside a list and
235  the list is returned to the user to extend the live of the objects
236  contained, otherwise no lines and labels will be drawn, since they
237  will be garbage-collected as soon as this function returns.
238  """
239 
240  # Add eta labels
241  keep_alive = []
242  etas = [-3.4, -3.0, -2.8, -2.6, -2.4, -2.2,
243  -2.0, -1.8, -1.6, -1.4, -1.2, -1., -0.8, -0.6, -0.4, -0.2, 0., 0.2,
244  0.4, 0.6, 0.8, 1., 1.2, 1.4, 1.6, 1.8, 2., 2.2, 2.4, 2.6, 2.8, 3.0,
245  3.4]
246 
247  etax = 2940.
248  etay = 1240.
249  lineL = 100.
250  offT = 10.
251 
252  for ieta in etas:
253  th = 2*atan(exp(-ieta))
254  talign = 21
255 
256  #IP
257  lineh = TLine(-20.,0.,20.,0.)
258  lineh.Draw()
259  linev = TLine(0.,-10.,0.,10.)
260  linev.Draw()
261  keep_alive.append(lineh)
262  keep_alive.append(linev)
263 
264  x1 = 0
265  y1 = 0
266  if ieta>-1.6 and ieta<1.6:
267  x1 = etay/tan(th)
268  y1 = etay
269  elif ieta <=-1.6:
270  x1 = -etax
271  y1 = -etax*tan(th)
272  talign = 11
273  elif ieta>=1.6:
274  x1 = etax
275  y1 = etax*tan(th)
276  talign = 31
277  x2 = x1+lineL*cos(th)
278  y2 = y1+lineL*sin(th)
279  xt = x2
280  yt = y2+offT
281 
282  line1 = TLine(x1,y1,x2,y2)
283  line1.Draw()
284  keep_alive.append(line1)
285 
286  text = "%3.1f" % ieta
287  t1 = TLatex(xt, yt, '%s' % ('#eta = 0' if ieta == 0 else text))
288  t1.SetTextSize(0.03)
289  t1.SetTextAlign(talign)
290  t1.Draw()
291  keep_alive.append(t1)
292  return keep_alive
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
def drawEtaValues()
Definition: plot_utils.py:230
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
Tan< T >::type tan(const T &t)
Definition: Tan.h:22
def setTDRStyle()
Definition: plot_utils.py:124