ATLAS Offline Software
PlotLibrary.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
4 
5 """
6 A library with miscellaneous beam spot related utilities and plots.
7 """
8 __author__ = 'Juerg Beringer'
9 __version__ = '$Id $'
10 
11 
12 from math import sqrt
13 from time import mktime, strptime
14 from numpy import array
15 
16 import ROOT
17 from InDetBeamSpotExample import ROOTUtils
18 
19 
20 #
21 # Plot beam spot size estimated from emittance and beta*
22 #
23 def LHCBeamSpotSize(lhcTimeString, # E.g. ['Apr 4 14:27:30 2010'] (list)
24  lhcBeta1, # beta* in m (scalar), beam 1
25  lhcEmit1, # Norm. emittance in um rad (list), beam 1
26  lhcBeta2, # dito beam 2
27  lhcEmit2, # dito beam 2
28  betaGamma = 3730. # Default value of betaGamma for 7 TeV
29  ):
30  lhcTime = array([ mktime(strptime(s,'%b %d %H:%M:%S %Y')) for s in lhcTimeString])
31  print (lhcTime)
32 
33  # Normalized emittance quoted in um rad; lhcEps arrays are emittance in m rad
34  lhcEps1 = lhcEmit1 * 1.0e-6 / betaGamma
35  lhcEps2 = lhcEmit2 * 1.0e-6 / betaGamma
36 
37  # Beam sizes in mm
38  lhcSigmaBeam1 = sqrt(lhcBeta1*lhcEps1) * 1.0e3
39  print ('Size of beam 1 (mm): ',lhcSigmaBeam1)
40  lhcSigmaBeam2 = sqrt(lhcBeta2*lhcEps2) * 1.0e3
41  print ('Size of beam 2 (mm): ',lhcSigmaBeam2)
42 
43  # Beam spot size in mm
44  lhcSigma = 1. / sqrt(1./lhcSigmaBeam1**2 + 1./lhcSigmaBeam2**2)
45  print ('Beam spot size (mm): ',lhcSigma)
46 
47  # Draw on existing plot
48  gr = ROOTUtils.protect( ROOT.TGraph(len(lhcTime),lhcTime,lhcSigma) )
49  gr.SetMarkerColor(2)
50  gr.SetLineColor(2)
51  #gr.SetMarkerSize(1.0)
52  #gr.SetMarkerStyle(25)
53  gr.SetMarkerSize(1.5)
54  gr.SetMarkerStyle(20)
55  gr.Draw('PSAME')
56  return [(gr,'Expected from #sqrt{#epsilon #beta*}','P')]
57 
58 
59 #
60 # Plot normalized emittance with new axis on right hand side of plot
61 #
62 def LHCEmittance(lhcTimeString, # E.g. ['Apr 4 14:27:30 2010'] (list)
63  lhcEmit1, # Norm. emittance in um rad (list), beam 1
64  lhcEmit2, # dito beam 2
65  minEmittance = 0.,
66  maxEmittance = 8.
67  ):
68  lhcTime = array([ mktime(strptime(s,'%b %d %H:%M:%S %Y')) for s in lhcTimeString])
69  ROOT.gPad.SetTicky(0) # Remove tick marks on right side y axis
70  ROOT.gPad.SetRightMargin(0.1)
71  #maxEmittance = max(max(lhcEmit1),max(lhcEmit2))*1.7
72  hmin = ROOT.gPad.GetUymin()
73  hmax = ROOT.gPad.GetUymax()
74 
75  lhcEmit1Data = (lhcEmit1-minEmittance)/(maxEmittance-minEmittance)*(hmax-hmin)+hmin
76  gr1 = ROOTUtils.protect( ROOT.TGraph(len(lhcTime),lhcTime,lhcEmit1Data) )
77  gr1.SetMarkerColor(4)
78  gr1.SetLineColor(4)
79  gr1.SetMarkerStyle(25)
80  gr1.Draw('PSAME')
81  #gr1.Draw('PLSAME')
82 
83  lhcEmit2Data = (lhcEmit2-minEmittance)/(maxEmittance-minEmittance)*(hmax-hmin)+hmin
84  gr2 = ROOTUtils.protect( ROOT.TGraph(len(lhcTime),lhcTime,lhcEmit2Data) )
85  gr2.SetMarkerColor(4)
86  gr2.SetLineColor(4)
87  gr2.SetMarkerStyle(26)
88  gr2.Draw('PSAME')
89  #gr2.Draw('PLSAME')
90 
91  raxis = ROOTUtils.protect( ROOT.TGaxis(ROOT.gPad.GetUxmax(),hmin,ROOT.gPad.GetUxmax(),hmax,minEmittance,maxEmittance,510,'+L') )
92  raxis.SetLineColor(4)
93  raxis.SetTitle('Invariant Emittance (#mum rad)')
94  raxis.SetTitleColor(4)
95  raxis.SetTitleSize(0.05)
96  raxis.SetTextFont(42)
97  raxis.SetLabelFont(42)
98  raxis.SetLabelSize(0.05)
99  raxis.SetLabelColor(4)
100  raxis.Draw()
101  return [(gr1,'Emittance beam 1','P'),(gr2,'Emittance beam 2','P')]
102 
103 
104 #
105 # Plots and data for fill 1022
106 #
107 fill1022TimeString = ['Apr 4 14:27:30 2010', 'Apr 4 22:27:30 2010', 'Apr 5 3:57:30 2010','Apr 5 7:57:30 2010' ]
108 fill1022Emit1X = array([ 1.45, 1.8, 1.92, 2.0 ])
109 fill1022Emit1Y = array([ 1.6, 2.1, 2.45, 2.55 ])
110 fill1022Emit2X = array([ 1.8, 2.27, 2.27, 2.5 ])
111 fill1022Emit2Y = array([ 2.6, 4.6, 5.4, 5.8 ])
112 fill1022Beta1X = 11.54
113 fill1022Beta1Y = 12.74
114 fill1022Beta2X = 9.89
115 fill1022Beta2Y = 12.70
116 
117 # Beam spot size errors, based on measured beta* errors and assumed 5% error on emittance
118 # See LuminousSizeError.nb Mathematica notebook
119 # Witold - forget errors; machine doesn't have a clue; so we'll omit them from the plot
120 #lhcTimeErr = array([0.,0.,0.,0.])
121 #lhcSigmaXErr = array([.0031, .0031, .0031, .0031])
122 #lhcSigmaYErr = array([.0037, .0041, .0041, .0042])
123 #if plot=='sigx':
124 # gr = ROOTUtils.protect( ROOT.TGraphErrors(len(lhcTime),lhcTime,lhcSigmaX,lhcTimeErr,lhcSigmaXErr) )
125 #if plot=='sigy':
126 # gr = ROOTUtils.protect( ROOT.TGraphErrors(len(lhcTime),lhcTime,lhcSigmaY,lhcTimeErr,lhcSigmaYErr) )
127 
128 def LHCBeamSpotSizeFill1022(plot='sigx'):
129  if plot=='sigx':
130  return LHCBeamSpotSize(fill1022TimeString,fill1022Beta1X,fill1022Emit1X,fill1022Beta2X,fill1022Emit2X)
131  if plot=='sigy':
132  return LHCBeamSpotSize(fill1022TimeString,fill1022Beta1Y,fill1022Emit1Y,fill1022Beta2Y,fill1022Emit2Y)
133 
134 def LHCEmittanceFill1022(plot='sigx'):
135  if plot=='sigx':
136  return LHCEmittance(fill1022TimeString,fill1022Emit1X,fill1022Emit2X,0.1,8.5)
137  if plot=='sigy':
138  return LHCEmittance(fill1022TimeString,fill1022Emit1Y,fill1022Emit2Y,0.1,8.5)
139 
140 
141 
142 #
143 # Plots and data for fill 1058
144 #
145 fill1058TimeString = ['Apr 25 6:03:00 2010', 'Apr 24 6:57:00 2010']
146 fill1058Emit1X = array([ 5.05, 2.6])
147 fill1058Emit1Y = array([ 5.04, 2.6])
148 fill1058Emit2X = array([ 3.88, 2.5])
149 fill1058Emit2Y = array([10.64, 4.9])
150 fill1058Beta1X = 2.28
151 fill1058Beta1Y = 2.02
152 fill1058Beta2X = 1.92
153 fill1058Beta2Y = 2.10
154 
155 def LHCBeamSpotSizeFill1058(plot='sigx'):
156  if plot=='sigx':
157  return LHCBeamSpotSize(fill1058TimeString,fill1058Beta1X,fill1058Emit1X,fill1058Beta2X,fill1058Emit2X)
158  if plot=='sigy':
159  return LHCBeamSpotSize(fill1058TimeString,fill1058Beta1Y,fill1058Emit1Y,fill1058Beta2Y,fill1058Emit2Y)
160 
161 
162 
163 # Test code for modules
164 if __name__ == '__main__':
python.PlotLibrary.LHCEmittanceFill1022
def LHCEmittanceFill1022(plot='sigx')
Definition: PlotLibrary.py:134
python.PlotLibrary.LHCEmittance
def LHCEmittance(lhcTimeString, lhcEmit1, lhcEmit2, minEmittance=0., maxEmittance=8.)
Definition: PlotLibrary.py:62
ROOTUtils.protect
def protect(obj)
Definition: roofit/ROOTUtils.py:17
python.PlotLibrary.LHCBeamSpotSize
def LHCBeamSpotSize(lhcTimeString, lhcBeta1, lhcEmit1, lhcBeta2, lhcEmit2, betaGamma=3730. # Default value of betaGamma for 7 TeV)
Definition: PlotLibrary.py:23
python.PlotLibrary.LHCBeamSpotSizeFill1022
def LHCBeamSpotSizeFill1022(plot='sigx')
Definition: PlotLibrary.py:128
array
python.PlotLibrary.LHCBeamSpotSizeFill1058
def LHCBeamSpotSizeFill1058(plot='sigx')
Definition: PlotLibrary.py:155