1 from __future__
import print_function
2 from __future__
import absolute_import
4 from .fitResidual
import fitResidual
5 from .drawHistoAllChambers
import drawHisto
7 layerCorrectionFactors = {
'SL1':(1.17,1.16,1.15,1.14),
8 'SL2':(1.83,1.20,1.20,1.83),
9 'SL3':(1.14,1.15,1.16,1.17)}
12 dir='DQMData/Run 1/DT/Run summary/DTCalibValidation',
13 option="HISTOPE1",draw=True):
21 slStr =
"SL%d" % slType
23 layerStr =
"Layer%d" % layerType
27 ROOT.TH1.AddDirectory(
False)
29 file = ROOT.TFile(fileName,
'read')
31 wheels = (-2,-1,0,1,2)
37 if slType == 2: nBins = 180
38 histoMean = ROOT.TH1F(
"h_ResMeanAll_%s_%s" % (slStr,layerStr),
"Mean of residuals",nBins,0,nBins)
39 histoSigma = ROOT.TH1F(
"h_ResSigmaAll_%s_%s" % (slStr,layerStr),
"Sigma of residuals",nBins,0,nBins)
42 if st == 4: nSectors = 14
43 if st == 4
and slType == 2:
continue
44 if verbose:
print(
"Station",st)
46 if verbose:
print(
"Wheel",wh)
47 for sec
in range(1,nSectors+1):
48 if verbose:
print(
"Sector",sec)
50 histoName =
"%s/Wheel%d/Station%d/Sector%d/%s/hResDist_STEP3_W%d_St%d_Sec%d_%s_%s" % (dir,wh,st,sec,slStr,wh,st,sec,slStr,layerStr)
51 print(
"Accessing",histoName)
52 histo = file.Get(histoName)
53 (histo,fitFunc) =
fitResidual(histo,nSigmas,verbose)
54 fitMean = fitFunc.GetParameter(1)
55 fitMeanErr = fitFunc.GetParError(1)
56 fitSigma = fitFunc.GetParameter(2)
57 fitSigmaErr = fitFunc.GetParError(2)
59 layerIdx = (layer - 1)
60 corrFactor = layerCorrectionFactors[slStr][layerIdx]
62 binHistoNew = (st - 1)*60 + (wh + 2)*nSectors + sec
63 if verbose:
print(
"Bin in summary histo",binHistoNew)
64 histoMean.SetBinContent(binHistoNew,fitMean)
65 histoMean.SetBinError(binHistoNew,fitMeanErr)
66 histoSigma.SetBinContent(binHistoNew,fitSigma*corrFactor)
67 histoSigma.SetBinError(binHistoNew,fitSigmaErr*corrFactor)
70 label =
"Wheel %d" % wh
71 if wh == -2: label +=
" MB%d" % st
72 histoMean.GetXaxis().SetBinLabel(binHistoNew,label)
73 histoSigma.GetXaxis().SetBinLabel(binHistoNew,label)
75 objectsMean =
drawHisto(histoMean,title=
"Mean of residuals (cm)",
76 ymin=mean_ymin,ymax=mean_ymax,option=option,draw=draw)
77 objectsSigma =
drawHisto(histoSigma,title=
"Resolution (cm)",
78 ymin=sig_ymin,ymax=sig_ymax,option=option,draw=draw)
80 return (objectsMean,objectsSigma)
83 dir='DQMData/Run 1/DT/Run summary/DTCalibValidation',type='mean',option='HISTOPE1'):
84 colors = (2,4,12,44,55,38,27,46)
85 markers = (24,25,26,27,28,30,32,5)
86 labels=[
'Layer 1',
'Layer 2',
'Layer 3',
'Layer 4']
88 if type ==
'mean': idx_type = 0
89 elif type ==
'sigma': idx_type = 1
90 else:
raise RuntimeError(
"Wrong option: %s" % type)
96 for layer
in range(1,5):
98 if not idx: draw =
True
101 histos.append(objs[idx_type][1])
102 histos[-1].SetName(
"%s_%d" % (histos[-1].GetName(),idx) )
104 canvas = objs[idx_type][0]
105 objects = objs[idx_type][2]
109 histos[-1].SetLineColor(colors[ (idx - 1) % len(colors) ])
110 histos[-1].SetMarkerColor(colors[ (idx - 1) % len(colors) ])
111 histos[-1].SetMarkerStyle(markers[ (idx - 1) % len(markers) ])
113 histos[-1].Draw(option +
"SAME")
117 legend = ROOT.TLegend(0.4,0.7,0.95,0.8)
118 for idx
in range( len(histos) ):
120 label = histo.GetName()
121 if len(labels): label = labels[idx]
122 legend.AddEntry(histo,label,
"LP")
127 legend.SetFillColor( canvas.GetFillColor() )
129 objects.append(legend)
135 wheels = (-2,-1,0,1,2)
138 slStr =
"SL%d" % slType
140 nBinsAve = len(stations)*len(wheels)
141 histoAverage = ROOT.TH1F(
"h_AverageAll_" + slStr,
"",nBinsAve,0,nBinsAve)
148 if st == 4: nSectors = 14
149 if st == 4
and slType == 2:
continue
151 binHistoAve = (st - 1)*5 + (wh + 2) + 1
152 label =
"Wheel %d" % wh
153 if wh == -2: label +=
" MB%d" % st
154 histoAverage.GetXaxis().SetBinLabel(binHistoAve,label)
156 averages[(st,wh)] = 0.
157 averagesSumw[(st,wh)] = 0.
158 for sec
in range(1,nSectors+1):
159 binHisto = (st - 1)*60 + (wh + 2)*nSectors + sec
160 for idx
in range( len(histos) ):
162 value = histo.GetBinContent( binHisto )
163 error = histo.GetBinError( binHisto )
164 averages[(st,wh)] += value/( error*error )
165 averagesSumw[(st,wh)] += 1./( error*error )
167 averages[(st,wh)] = averages[(st,wh)]/averagesSumw[(st,wh)]
168 averagesErr[(st,wh)] = math.sqrt( 1./averagesSumw[(st,wh)] )
169 histoAverage.SetBinContent(binHistoAve,averages[(st,wh)])
170 histoAverage.SetBinError(binHistoAve,averagesErr[(st,wh)])
171 print(
"Station %d, Wheel %d: %.4f +/- %.6f" % (st,wh,averages[(st,wh)],averagesErr[(st,wh)]))
173 canvasAverage = ROOT.TCanvas(
"c_" + histoAverage.GetName())
174 canvasAverage.SetGridx()
175 canvasAverage.SetGridy()
176 canvasAverage.SetFillColor( 0 )
183 histoAverage.GetYaxis().SetTitle(
"Mean of residuals (cm)")
184 histoAverage.GetYaxis().SetRangeUser(mean_ymin,mean_ymax)
185 elif type ==
'sigma':
186 histoAverage.GetYaxis().SetTitle(
"Resolution (cm)")
187 histoAverage.GetYaxis().SetRangeUser(sig_ymin,sig_ymax)
189 histoAverage.SetStats(0)
190 histoAverage.SetLineWidth(2)
191 histoAverage.SetMarkerStyle( 27 )
192 histoAverage.SetMarkerSize( 1.5 )
193 histoAverage.LabelsOption(
"d",
"X")
194 histoAverage.Draw(
"E2")
196 return ( (canvas,canvasAverage),(histos,histoAverage),objects )
198 def plotMean(fileName,sl,dir='DQMData/Run 1/DT/Run summary/DTCalibValidation',option='HISTOPE1'):
200 objs =
plot(fileName,sl,dir,type,option)
203 def plotSigma(fileName,sl,dir='DQMData/Run 1/DT/Run summary/DTCalibValidation',option='HISTOPE1'):
205 objs =
plot(fileName,sl,dir,type,option)
208 def plotSigmaAll(fileName,dir='DQMData/Run 1/DT/Run summary/DTCalibValidation',option='HISTOPE1',outputFileName=''):
209 colors = (2,4,12,44,55,38,27,46)
210 markers = (24,25,26,27,28,30,32,5)
213 labels = (
'R-#phi SL1',
'R-z SL2',
'R-#phi SL3')
220 if not idx: draw =
True
223 histos.append(objs[1][1])
224 histos[-1].SetName(
"%s_%d" % (histos[-1].GetName(),idx) )
231 histos[-1].SetLineColor(colors[ (idx - 1) % len(colors) ])
232 histos[-1].SetMarkerColor(colors[ (idx - 1) % len(colors) ])
233 histos[-1].SetMarkerStyle(markers[ (idx - 1) % len(markers) ])
235 histos[-1].Draw(option +
"SAME")
239 legend = ROOT.TLegend(0.4,0.7,0.95,0.8)
240 for idx
in range( len(histos) ):
242 label = histo.GetName()
243 if len(labels): label = labels[idx]
244 legend.AddEntry(histo,label,
"LP")
249 legend.SetFillColor( canvas.GetFillColor() )
251 if not objects: objects = [legend]
252 else: objects.append(legend)
255 outputFile = ROOT.TFile(outputFileName,
'recreate')
257 for histo
in histos: histo.Write()
261 return (canvas,histos,objects)
266 AddDirectoryStatus_ = ROOT.TH1.AddDirectoryStatus()
267 ROOT.TH1.AddDirectory(
False)
272 for file
in fileNames: rootFiles.append( ROOT.TFile(file,
'read') )
274 variables = [
'h_AverageAll_SL1_0',
275 'h_AverageAll_SL2_1',
276 'h_AverageAll_SL3_2']
278 colors = (1,2,4,12,44,55,38,27,46)
279 markers = (20,24,25,26,27,28,30,32,5)
285 for var
in variables:
286 print(
"Accessing",var)
303 for file
in rootFiles:
304 histos_tmp.append( file.Get(var) )
305 histos_tmp[-1].SetName(
"%s_%d" % (histos_tmp[-1].GetName(),idx) )
306 print(
"Created",histos_tmp[-1].GetName())
307 histos_tmp[-1].SetLineColor(colors[ idx % len(colors) ])
308 histos_tmp[-1].SetMarkerColor(colors[ idx % len(colors) ])
309 histos_tmp[-1].SetMarkerStyle(markers[ idx % len(markers) ])
310 histos_tmp[-1].SetMarkerSize(1.4)
312 histos.append( histos_tmp )
314 canvases.append( ROOT.TCanvas(
"c_" + var,var) )
315 canvases[-1].SetGridx()
316 canvases[-1].SetGridy()
317 canvases[-1].SetFillColor(0)
323 for histo
in histos[-1][1:]: histo.Draw(
"SAME")
328 legends.append( ROOT.TLegend(0.4,0.7,0.95,0.8) )
330 for histo
in histos[-1]:
331 legends[-1].AddEntry(histo,labels[idx],
"LP")
334 legends[-1].SetFillColor( canvases[-1].GetFillColor() )
335 legends[-1].Draw(
"SAME")
339 if not objects: objects = [legends]
340 else: objects.append(legends)
342 ROOT.TH1.AddDirectory(AddDirectoryStatus_)
344 return (canvases,histos,objects)