CMS 3D CMS Logo

Functions | Variables

plotResidualsPerLayer Namespace Reference

Functions

def plot
def plotFromFile
def plotMean
def plotResLayer
def plotSigma
def plotSigmaAll

Variables

dictionary layerCorrectionFactors

Function Documentation

def plotResidualsPerLayer::plot (   fileName,
  sl,
  dir = 'DQMData/Run 1/DT/Run summary/DTCalibValidation',
  type = 'mean',
  option = 'HISTOPE1' 
)

Definition at line 80 of file plotResidualsPerLayer.py.

00082                                                                                                      :
00083     colors = (2,4,12,44,55,38,27,46)
00084     markers = (24,25,26,27,28,30,32,5)
00085     labels=['Layer 1','Layer 2','Layer 3','Layer 4']
00086     idx_type = None
00087     if type == 'mean': idx_type = 0
00088     elif type == 'sigma': idx_type = 1
00089     else: raise RuntimeError, "Wrong option: %s" % type
00090 
00091     idx = 0
00092     canvas = None
00093     objects = None
00094     histos = []
00095     for layer in range(1,5):
00096         draw = False
00097         if not idx: draw = True
00098 
00099         objs = plotResLayer(fileName,sl,layer,dir,option,draw)
00100         histos.append(objs[idx_type][1])
00101         histos[-1].SetName( "%s_%d" % (histos[-1].GetName(),idx) )
00102         if not idx:
00103             canvas = objs[idx_type][0]
00104             objects = objs[idx_type][2]
00105 
00106         canvas.cd()
00107         if idx:
00108             histos[-1].SetLineColor(colors[ (idx - 1) % len(colors) ])
00109             histos[-1].SetMarkerColor(colors[ (idx - 1) % len(colors) ])
00110             histos[-1].SetMarkerStyle(markers[ (idx - 1) % len(markers) ])
00111 
00112             histos[-1].Draw(option + "SAME")
00113 
00114         idx += 1
00115 
00116     legend = ROOT.TLegend(0.4,0.7,0.95,0.8)
00117     for idx in range( len(histos) ):
00118         histo = histos[idx]
00119         label = histo.GetName()
00120         if len(labels): label = labels[idx]
00121         legend.AddEntry(histo,label,"LP")
00122 
00123         idx += 1
00124 
00125     canvas.cd()
00126     legend.SetFillColor( canvas.GetFillColor() )
00127     legend.Draw("SAME")
00128     objects.append(legend)
00129 
00130     # Compute averages
00131     # (Wh-2 MB1 Sec1 ... Wh-2 MB1 Sec12 ... Wh-1 MB1 Sec1 ... Wh-1 MB1 Sec12 ...)
00132     # (Wh-2 MB2 Sec1 ... Wh-2 MB2 Sec12 ... Wh-1 MB2 Sec1 ... Wh-1 MB1 Sec12 ...) ...  
00133     import math
00134     wheels = (-2,-1,0,1,2)
00135     stations = (1,2,3,4)
00136     slType = sl
00137     slStr = "SL%d" % slType
00138 
00139     nBinsAve = len(stations)*len(wheels)
00140     histoAverage = ROOT.TH1F("h_AverageAll_" + slStr,"",nBinsAve,0,nBinsAve)
00141     averages = {}
00142     averagesErr = {}
00143     averagesSumw = {}
00144     print "Averages:"
00145     for st in stations:
00146         nSectors = 12
00147         if st == 4: nSectors = 14
00148         if st == 4 and slType == 2: continue 
00149         for wh in wheels:
00150             binHistoAve = (st - 1)*5 + (wh + 2) + 1
00151             label = "Wheel %d" % wh
00152             if wh == -2: label += " MB%d" % st  
00153             histoAverage.GetXaxis().SetBinLabel(binHistoAve,label) 
00154 
00155             averages[(st,wh)] = 0.
00156             averagesSumw[(st,wh)] = 0.
00157             for sec in range(1,nSectors+1):
00158                 binHisto = (st - 1)*60 + (wh + 2)*nSectors + sec
00159                 for idx in range( len(histos) ):
00160                     histo = histos[idx]
00161                     value = histo.GetBinContent( binHisto ) 
00162                     error = histo.GetBinError( binHisto ) 
00163                     averages[(st,wh)]     += value/( error*error ) 
00164                     averagesSumw[(st,wh)] += 1./( error*error )
00165             # Average per (st,wh)
00166             averages[(st,wh)] = averages[(st,wh)]/averagesSumw[(st,wh)]
00167             averagesErr[(st,wh)] = math.sqrt( 1./averagesSumw[(st,wh)] )
00168             histoAverage.SetBinContent(binHistoAve,averages[(st,wh)])
00169             histoAverage.SetBinError(binHistoAve,averagesErr[(st,wh)])
00170             print "Station %d, Wheel %d: %.4f +/- %.6f" % (st,wh,averages[(st,wh)],averagesErr[(st,wh)])
00171      
00172     canvasAverage = ROOT.TCanvas("c_" + histoAverage.GetName())
00173     canvasAverage.SetGridx()
00174     canvasAverage.SetGridy()
00175     canvasAverage.SetFillColor( 0 )
00176     canvasAverage.cd()
00177     mean_ymin = -0.02
00178     mean_ymax =  0.02
00179     sig_ymin = 0.
00180     sig_ymax = 0.1
00181     if type == 'mean':
00182         histoAverage.GetYaxis().SetTitle("Mean of residuals (cm)")
00183         histoAverage.GetYaxis().SetRangeUser(mean_ymin,mean_ymax)
00184     elif type == 'sigma':
00185         histoAverage.GetYaxis().SetTitle("Resolution (cm)")
00186         histoAverage.GetYaxis().SetRangeUser(sig_ymin,sig_ymax)
00187 
00188     histoAverage.SetStats(0)
00189     histoAverage.SetLineWidth(2)
00190     histoAverage.SetMarkerStyle( 27 )
00191     histoAverage.SetMarkerSize( 1.5 )
00192     histoAverage.LabelsOption("d","X")
00193     histoAverage.Draw("E2")           
00194 
00195     return ( (canvas,canvasAverage),(histos,histoAverage),objects )

def plotResidualsPerLayer::plotFromFile (   fileNames,
  labels = [] 
)

Definition at line 262 of file plotResidualsPerLayer.py.

00263                                      :
00264 
00265     AddDirectoryStatus_ = ROOT.TH1.AddDirectoryStatus()
00266     ROOT.TH1.AddDirectory(False)
00267 
00268     #fileData = ROOT.TFile(fileNameData,'read')
00269     #fileMC = ROOT.TFile(fileNameMC,'read')
00270     rootFiles = []
00271     for file in fileNames: rootFiles.append( ROOT.TFile(file,'read') ) 
00272 
00273     variables = ['h_AverageAll_SL1_0',
00274                  'h_AverageAll_SL2_1',
00275                  'h_AverageAll_SL3_2']
00276 
00277     colors = (1,2,4,12,44,55,38,27,46)
00278     markers = (20,24,25,26,27,28,30,32,5)
00279     objects = None
00280     canvases = []
00281     legends = []
00282     histos = []
00283     idx_var = 0
00284     for var in variables:
00285         print "Accessing",var 
00286         #histoData = fileData.Get(var)
00287         #histoData.SetName(histoData.GetName() + "_data")
00288         #histoMC = fileMC.Get(var)
00289         #histoMC.SetName(histoMC.GetName() + "_mc")
00290         #histoData.SetLineColor(1)
00291         #histoData.SetMarkerStyle(20)
00292         #histoData.SetMarkerSize(1.4)
00293         #histoData.SetMarkerColor(1)
00294 
00295         #histoMC.SetLineColor(2)
00296         #histoMC.SetMarkerStyle(24)
00297         #histoMC.SetMarkerSize(1.4)
00298         #histoMC.SetMarkerColor(2)
00299 
00300         histos_tmp = []
00301         idx = 0
00302         for file in rootFiles:
00303             histos_tmp.append( file.Get(var) )
00304             histos_tmp[-1].SetName( "%s_%d" % (histos_tmp[-1].GetName(),idx) )
00305             print "Created",histos_tmp[-1].GetName()
00306             histos_tmp[-1].SetLineColor(colors[ idx % len(colors) ]) 
00307             histos_tmp[-1].SetMarkerColor(colors[ idx % len(colors) ]) 
00308             histos_tmp[-1].SetMarkerStyle(markers[ idx % len(markers) ]) 
00309             histos_tmp[-1].SetMarkerSize(1.4)
00310             idx += 1
00311         histos.append( histos_tmp )
00312 
00313         canvases.append( ROOT.TCanvas("c_" + var,var) ) 
00314         canvases[-1].SetGridx()
00315         canvases[-1].SetGridy()
00316         canvases[-1].SetFillColor(0) 
00317         canvases[-1].cd()
00318         #histoData.Draw()
00319         #histoMC.Draw("SAME")
00320         #histos.append( (histoData,histoMC) )
00321         histos[-1][0].Draw()
00322         for histo in histos[-1][1:]: histo.Draw("SAME")
00323             
00324         if len(labels):
00325             #labelData = labels[0]
00326             #labelMC = labels[1]
00327             legends.append( ROOT.TLegend(0.4,0.7,0.95,0.8) )
00328             idx = 0
00329             for histo in histos[-1]:
00330                 legends[-1].AddEntry(histo,labels[idx],"LP")
00331                 idx += 1
00332 
00333             legends[-1].SetFillColor( canvases[-1].GetFillColor() )
00334             legends[-1].Draw("SAME")
00335  
00336         idx_var += 1
00337 
00338     if not objects: objects = [legends]
00339     else:           objects.append(legends)
00340 
00341     ROOT.TH1.AddDirectory(AddDirectoryStatus_)
00342 
00343     return (canvases,histos,objects)
def plotResidualsPerLayer::plotMean (   fileName,
  sl,
  dir = 'DQMData/Run 1/DT/Run summary/DTCalibValidation',
  option = 'HISTOPE1' 
)

Definition at line 196 of file plotResidualsPerLayer.py.

00197                                                                                                 :
00198     type = 'mean'
00199     objs = plot(fileName,sl,dir,type,option)
00200     return objs

def plotResidualsPerLayer::plotResLayer (   fileName,
  sl,
  layer,
  dir = 'DQMData/Run 1/DT/Run summary/DTCalibValidation',
  option = "HISTOPE1",
  draw = True 
)

Definition at line 9 of file plotResidualsPerLayer.py.

00012                                                       :
00013 
00014     mean_ymin = -0.02
00015     mean_ymax =  0.02
00016     sig_ymin = 0.
00017     sig_ymax = 0.1
00018 
00019     slType = sl
00020     slStr = "SL%d" % slType
00021     layerType = layer
00022     layerStr = "Layer%d" % layerType
00023     verbose = False
00024     nSigmas = 2
00025 
00026     ROOT.TH1.AddDirectory(False)
00027 
00028     file = ROOT.TFile(fileName,'read')
00029 
00030     wheels = (-2,-1,0,1,2)
00031     stations = (1,2,3,4)
00032 
00033     # (Wh-2 MB1 Sec1 ... Wh-2 MB1 Sec12 ... Wh-1 MB1 Sec1 ... Wh-1 MB1 Sec12 ...)
00034     # (Wh-2 MB2 Sec1 ... Wh-2 MB2 Sec12 ... Wh-1 MB2 Sec1 ... Wh-1 MB1 Sec12 ...) ...  
00035     nBins = 250
00036     if slType == 2: nBins = 180
00037     histoMean = ROOT.TH1F("h_ResMeanAll_%s_%s" % (slStr,layerStr),"Mean of residuals",nBins,0,nBins)
00038     histoSigma = ROOT.TH1F("h_ResSigmaAll_%s_%s" % (slStr,layerStr),"Sigma of residuals",nBins,0,nBins)
00039     for st in stations:
00040         nSectors = 12
00041         if st == 4: nSectors = 14
00042         if st == 4 and slType == 2: continue 
00043         if verbose: print "Station",st
00044         for wh in wheels:
00045             if verbose: print "Wheel",wh 
00046             for sec in range(1,nSectors+1):
00047                 if verbose: print "Sector",sec
00048                 # Get histogram
00049                 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) 
00050                 print "Accessing",histoName
00051                 histo = file.Get(histoName)
00052                 (histo,fitFunc) = fitResidual(histo,nSigmas,verbose)
00053                 fitMean = fitFunc.GetParameter(1)
00054                 fitMeanErr = fitFunc.GetParError(1)
00055                 fitSigma = fitFunc.GetParameter(2)
00056                 fitSigmaErr = fitFunc.GetParError(2)
00057 
00058                 layerIdx = (layer - 1)
00059                 corrFactor = layerCorrectionFactors[slStr][layerIdx]
00060 
00061                 binHistoNew = (st - 1)*60 + (wh + 2)*nSectors + sec
00062                 if verbose: print "Bin in summary histo",binHistoNew
00063                 histoMean.SetBinContent(binHistoNew,fitMean)
00064                 histoMean.SetBinError(binHistoNew,fitMeanErr)
00065                 histoSigma.SetBinContent(binHistoNew,fitSigma*corrFactor)
00066                 histoSigma.SetBinError(binHistoNew,fitSigmaErr*corrFactor)
00067   
00068                 if sec == 1:
00069                     label = "Wheel %d" % wh
00070                     if wh == -2: label += " MB%d" % st  
00071                     histoMean.GetXaxis().SetBinLabel(binHistoNew,label) 
00072                     histoSigma.GetXaxis().SetBinLabel(binHistoNew,label)
00073 
00074     objectsMean = drawHisto(histoMean,title="Mean of residuals (cm)",
00075                                       ymin=mean_ymin,ymax=mean_ymax,option=option,draw=draw)
00076     objectsSigma = drawHisto(histoSigma,title="Resolution (cm)",
00077                                         ymin=sig_ymin,ymax=sig_ymax,option=option,draw=draw)
00078 
00079     return (objectsMean,objectsSigma)

def plotResidualsPerLayer::plotSigma (   fileName,
  sl,
  dir = 'DQMData/Run 1/DT/Run summary/DTCalibValidation',
  option = 'HISTOPE1' 
)

Definition at line 201 of file plotResidualsPerLayer.py.

00202                                                                                                  :
00203     type = 'sigma'
00204     objs = plot(fileName,sl,dir,type,option)
00205     return objs

def plotResidualsPerLayer::plotSigmaAll (   fileName,
  dir = 'DQMData/Run 1/DT/Run summary/DTCalibValidation',
  option = 'HISTOPE1',
  outputFileName = '' 
)

Definition at line 206 of file plotResidualsPerLayer.py.

00207                                                                                                                    :
00208     colors = (2,4,12,44,55,38,27,46)
00209     markers = (24,25,26,27,28,30,32,5)
00210 
00211     slList = (1,2,3)
00212     labels = ('R-#phi SL1','R-z SL2','R-#phi SL3') 
00213     canvas = None
00214     objects = None
00215     histos = []
00216     idx = 0
00217     for sl in slList:
00218         draw = False
00219         if not idx: draw = True
00220 
00221         objs = plotSigma(fileName,sl,dir,option)
00222         histos.append(objs[1][1])
00223         histos[-1].SetName( "%s_%d" % (histos[-1].GetName(),idx) )
00224         if not idx:
00225             canvas = objs[0][1]
00226             #objects = objs[2][1]
00227 
00228         canvas.cd()
00229         if idx:
00230             histos[-1].SetLineColor(colors[ (idx - 1) % len(colors) ])
00231             histos[-1].SetMarkerColor(colors[ (idx - 1) % len(colors) ])
00232             histos[-1].SetMarkerStyle(markers[ (idx - 1) % len(markers) ])
00233 
00234             histos[-1].Draw(option + "SAME")
00235 
00236         idx += 1
00237         
00238     legend = ROOT.TLegend(0.4,0.7,0.95,0.8)
00239     for idx in range( len(histos) ):
00240         histo = histos[idx]
00241         label = histo.GetName()
00242         if len(labels): label = labels[idx]
00243         legend.AddEntry(histo,label,"LP")
00244 
00245         idx += 1
00246 
00247     canvas.cd()
00248     legend.SetFillColor( canvas.GetFillColor() )
00249     legend.Draw("SAME")
00250     if not objects: objects = [legend]
00251     else:           objects.append(legend)
00252 
00253     if outputFileName:
00254         outputFile = ROOT.TFile(outputFileName,'recreate')
00255         outputFile.cd()
00256         for histo in histos: histo.Write()
00257         outputFile.Close()
00258         return 0
00259     else:       
00260         return (canvas,histos,objects)
00261 
#def plotDataVsMCFromFile(fileNameData,fileNameMC,labels=[]):

Variable Documentation

Initial value:
00001 {'SL1':(1.17,1.16,1.15,1.14),
00002                           'SL2':(1.83,1.20,1.20,1.83),
00003                           'SL3':(1.14,1.15,1.16,1.17)}

Definition at line 5 of file plotResidualsPerLayer.py.