CMS 3D CMS Logo

MergeOccDeadROC.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import ROOT
4 from ROOT import TBufferFile, TH1F, TProfile, TProfile2D, TH2F, TFile, TH1D, TH2D
5 import re
6 import os
7 
8 
9 def draw_line(lineList,x1,x2,y1,y2,width=1,style=1,color=1):
10  from ROOT import TLine
11  l=TLine(x1,y1,x2,y2)
12  l.SetBit(ROOT.kCanDelete)
13  l.SetLineWidth(width)
14  l.SetLineStyle(style)
15  l.SetLineColor(color)
16  l.Draw()
17  lineList.append(l)
18 
19 def draw_box(boxList,xl,xr,yl,yr,opacity=1,color=1,style=1001,lstyle=1,lw=3):
20  from ROOT import TBox
21  b=TBox(xl,yl,xr,yr)
22  b.SetBit(ROOT.kCanDelete)
23  b.SetFillStyle(style)
24  b.SetFillColorAlpha(color, opacity)
25  b.SetLineColor(color)
26  b.SetLineWidth(lw)
27  b.SetLineStyle(lstyle)
28  b.Draw()
29  boxList.append(b)
30 
31 def renderPluginBPIX(lineList,layer) :
32  from ROOT import TCanvas,TLine
33  nlad=[6,14,22,32]
34  coordSign=[(-1,-1),(-1,1),(1,-1),(1,1)]
35  for xsign,ysign in coordSign:
36  xlow = xsign*0.5
37  xhigh= xsign*(0.5+4)
38  ylow = ysign*0.5
39  yhigh= ysign*(0.5 + nlad[layer-1])
40  # Outside Box
41  draw_line(lineList,xlow, xhigh, ylow, ylow) # bottom
42  draw_line(lineList,xlow, xhigh, yhigh, yhigh) # top
43  draw_line(lineList,xlow, xlow, ylow, yhigh) # left
44  draw_line(lineList,xhigh, xhigh, ylow, yhigh) # right
45  # Inner Horizontal lines
46  for lad in range(nlad[layer-1]):
47  lad+=1
48  if lad != nlad[layer-1]:
49  y = ysign * (lad+0.5)
50  draw_line(lineList,xlow, xhigh, y, y)
51  y = ysign * (lad);
52  draw_line(lineList,xlow, xhigh, y, y, 1, 3);
53  # Inner Vertical lines
54  for mod in range(3) :
55  mod+=1
56  x = xsign * (mod + 0.5);
57  draw_line(lineList,x, x, ylow, yhigh);
58 
59  # Draw ROC0
60  for mod in range(4):
61  mod+=1
62  for lad in range(nlad[layer-1]):
63  lad+=1
64  if ysign==1:
65  flipped = not(lad%2==0)
66  else :
67  flipped = not(lad%2==1)
68  if flipped : roc0_orientation = -1
69  else : roc0_orientation = 1
70  if xsign==-1 : roc0_orientation *= -1
71  if ysign==-1 : roc0_orientation *= -1
72  x1 = xsign * (mod+0.5)
73  x2 = xsign * (mod+0.5 - 1./8);
74  y1 = ysign * (lad)
75  y2 = ysign * (lad + roc0_orientation*1./2)
76  if layer == 1 and xsign == -1 :
77  x1 = xsign * (mod-0.5)
78  x2 = xsign * (mod-0.5 + 1./8)
79  y1 = ysign * (lad)
80  y2 = ysign * (lad - roc0_orientation*1./2)
81 
82  draw_line(lineList,x1, x2, y1, y1, 1)
83  draw_line(lineList,x2, x2, y1, y2, 1)
84 
85  else:
86  draw_line(lineList,x1, x2, y1, y1, 1)
87  draw_line(lineList,x2, x2, y1, y2, 1)
88 
89 def maskBPixROC(boxList,xsign,ysign,layer,lad,mod,roc):
90  if roc<8 :
91  rocShiftX=roc*1./8
92  rocShiftY=0
93  else :
94  rocShiftX=(15-roc)*1./8
95  rocShiftY=1./2
96  if ysign==1:
97  flipped = not(lad%2==0)
98  else :
99  flipped = not(lad%2==1)
100  if flipped : roc0_orientation = -1
101  else : roc0_orientation = 1
102  if xsign==-1 : roc0_orientation *= -1
103  if ysign==-1 : roc0_orientation *= -1
104  x1 = xsign * (mod+0.5-rocShiftX)
105  x2 = xsign * (mod+0.5 - 1./8-rocShiftX);
106  y1 = ysign * (lad-roc0_orientation*rocShiftY)
107  y2 = ysign * (lad + roc0_orientation*1./2-roc0_orientation*rocShiftY)
108  if layer == 1 and xsign == -1 :
109  x1 = xsign * (mod-0.5)-rocShiftX
110  x2 = xsign * (mod-0.5 + 1./8)-rocShiftX
111  y1 = ysign * (lad +rocShiftY)
112  y2 = ysign * (lad - roc0_orientation*1./2+rocShiftY)
113  draw_box(boxList,min(x1,x2),max(x1,x2),min(y1, y2),max(y1,y2),0.75)
114 
115 
116 
117 def renderPluginFPIX(lineList,ring) :
118  from ROOT import TCanvas,TLine
119  coordSign=[(-1,-1),(-1,1),(1,-1),(1,1)]
120  for dsk in range(3) :
121  dsk+=1
122  for xsign,ysign in coordSign:
123  for bld in range(5+ring*6):
124  bld+=1
125  # Panel 2 has dashed mid-plane
126  x1 = xsign * (0.5 + dsk - 1)
127  x2 = xsign * (0.5 + dsk)
128  sign = ysign
129  y1 = ysign * (bld + sign*0.5)
130  y2 = ysign * (bld)
131  yp2_mid = ysign * (bld - sign*0.25)
132  y3 = ysign * (bld - sign*0.5)
133  draw_line(lineList,x1, x2, y1, y1)
134  draw_line(lineList,x1, x2, y2, y2)
135  draw_line(lineList,x1, x2, yp2_mid, yp2_mid,1,2)
136  draw_line(lineList,x1, x2, y3, y3)
137  # Vertical lines
138  x = xsign * (0.5 + dsk - 1)
139  draw_line(lineList,x, x, y1, y2)
140  draw_line(lineList,x, x, y2, y3)
141  if ring==2 :
142  x = xsign * (0.5 + dsk)
143  draw_line(lineList,x, x, y1, y2)
144  draw_line(lineList,x, x, y2, y3)
145  #Make a BOX around ROC 0
146  x1 = xsign * (0.5 + dsk - 1/8.)
147  x2 = xsign * (0.5 + dsk)
148  y1_p1 = ysign * (bld + sign*0.25)
149  y2_p1 = ysign * (bld + sign*0.25 + xsign*ysign*0.25)
150  draw_line(lineList,x1, x2, y1_p1, y1_p1, 1)
151  draw_line(lineList,x1, x1, y1_p1, y2_p1, 1)
152  y1_p2 = ysign * (bld - sign*0.25)
153  y2_p2 = ysign * (bld - sign*0.25 - xsign*ysign*0.25)
154  draw_line(lineList,x1, x2, y1_p2, y1_p2)
155  draw_line(lineList,x1, x1, y1_p2, y2_p2)
156 
157 def maskFPixROC(boxList,xsign,ysign,dsk,bld,pnl,roc) :
158  from ROOT import TCanvas,TLine
159  if roc<8 :
160  rocShiftX=roc*1./8
161  rocShiftY=0
162  else :
163  rocShiftX=(15-roc)*1./8
164  rocShiftY=1./4
165  sign=ysign
166  x1 = xsign * (0.5 + dsk - 1/8.-rocShiftX)
167  x2 = xsign * (0.5 + dsk-rocShiftX)
168  if pnl==1:
169  y1 = ysign * (bld + sign*0.25)-xsign*rocShiftY
170  y2 = ysign * (bld + sign*0.25 + xsign*ysign*0.25)-xsign*rocShiftY
171  else:
172  y1 = ysign * (bld - sign*0.25)+xsign*rocShiftY
173  y2 = ysign * (bld - sign*0.25 - xsign*ysign*0.25)+xsign*rocShiftY
174  draw_box(boxList,min(x1,x2),max(x1,x2),min(y1,y2),max(y1,y2),0.75)
175 
176 
177 def dqm_get_dataset(server, match, run, type="offline_data"):
178  datareq = urllib2.Request(('%s/data/json/samples?match=%s') % (server, match))
179  datareq.add_header('User-agent', ident)
180  # Get data
181  data = eval(re.sub(r"\bnan\b", "0", urllib2.build_opener(X509CertOpen()).open(datareq).read()),
182  { "__builtins__": None }, {})
183  ret = ""
184  for l in data['samples']:
185  if l['type'] == type:
186  for x in l['items']:
187  if int(x['run']) == int(run):
188  ret=x['dataset']
189  break
190  print ret
191  return ret
192 
193 
194 
195 
196 def main():
197  import sys
198  import os
199  import ROOT
200 
201  if len(sys.argv) != 2:
202  print "input file needed!"
203  return
204  else:
205  filename=sys.argv[1]
206  print filename+" -- "+filename[19:25]
207  runNum=filename[19:25]
208 
209  dir="DQMData/Run " + runNum + "/PixelPhase1/Run summary/Pahse1_MechanicalView/"
210  dirFED="DQMData/Run " + runNum + "/PixelPhase1/Run summary/FED/"
211 
212 
213  dirBPix=dir + "PXBarrel/"
214  dirFPix=dir + "PXForward/"
215 
216  hoccB="digi_occupancy_per_SignedModuleCoord_per_SignedLadderCoord_PXLayer_"
217  hoccF="digi_occupancy_per_SignedDiskCoord_per_SignedBladePanelCoord_PXRing_"
218  hdeadB="Dead Channels per ROC_per_SignedModuleCoord_per_SignedLadderCoord_PXLayer_"
219  hdeadF="Dead Channels per ROC_per_SignedDiskCoord_per_SignedBladePanelCoord_PXRing_"
220 
221  ROOT.gROOT.SetBatch(1)
222  ROOT.gStyle.SetOptStat(0)
223  ROOT.gStyle.SetPalette(1) #104 kTemperatureMap // 55 kRainBow
224  ROOT.gStyle.SetNumberContours(128)
225  rootf=ROOT.TFile(filename)
226 
227 
228 
229  c=ROOT.TCanvas("c","c",1250,1000)
230  #BPIX
231  print "----> Build maps for BPix"
232  histOccList=[]
233  histDeadList=[]
234  for lyr in range(1,5):
235  histOccList.append(rootf.FindObjectAny(hoccB+str(lyr)))
236  histDeadList.append(rootf.FindObjectAny(hdeadB+str(lyr)))
237  for hist1, hist2 in zip(histOccList, histDeadList):
238  if hist1 != None or hist2 !=None:
239  hist1.Draw("colz")
240  match=re.search('(?<=PXLayer_)[0-9]',hist1.GetName())
241  if match != None and "per_SignedModuleCoord_per_SignedLadderCoord" in hist1.GetName():
242  lyr=int(match.group(0))
243  hist1.SetTitle("Digi Occupancy Layer {0}".format(lyr))
244  boxList=[]
245  lineList=[]
246  renderPluginBPIX(lineList,lyr)
247  lineWd=3
248  if lyr==4 :
249  lineWd=2
250  if lyr==1:
251  tbmRoc=4
252  else:
253  tbmRoc=8
254  binTBM=[]
255  for biny in range(1,hist2.GetNbinsY()+1):
256  if len(binTBM)!=0:
257  x1=hist2.GetXaxis().GetBinLowEdge(binTBM[0])
258  x2=hist2.GetXaxis().GetBinUpEdge(binTBM[len(binTBM)-1])
259  y1=hist2.GetYaxis().GetBinLowEdge(biny-1)
260  y2=hist2.GetYaxis().GetBinUpEdge(biny-1)
261  if hist2.GetBinContent(binTBM[0],biny-1) >= 0.97*hist2.GetMaximum():
262  draw_box(boxList,x1,x2,y1,y2,0.2,1,0,1,lineWd)
263  else:
264  draw_box(boxList,x1,x2,y1,y2,0.2,633,0,1,lineWd)
265  binTBM=[]
266  for binx in range(1,hist2.GetNbinsX()+1):
267  if hist2.GetBinContent(binx,biny)!=0:
268  if len(binTBM)==0:
269  binTBM.append(binx)
270  else:
271  if len(binTBM)==tbmRoc:
272  x1=hist2.GetXaxis().GetBinLowEdge(binTBM[0])
273  x2=hist2.GetXaxis().GetBinUpEdge(binTBM[len(binTBM)-1])
274  y1=hist2.GetYaxis().GetBinLowEdge(biny)
275  y2=hist2.GetYaxis().GetBinUpEdge(biny)
276  if hist2.GetBinContent(binTBM[0],biny) >= 0.97*hist2.GetMaximum():
277  draw_box(boxList,x1,x2,y1,y2,0.2,1,0,1,lineWd)
278  else:
279  draw_box(boxList,x1,x2,y1,y2,0.2,633,0,1,lineWd)
280  binTBM=[]
281  binTBM.append(binx)
282  else:
283  binTBM.append(binx)
284  else:
285  if len(binTBM)!=0:
286  x1=hist2.GetXaxis().GetBinLowEdge(binTBM[0])
287  x2=hist2.GetXaxis().GetBinUpEdge(binTBM[len(binTBM)-1])
288  y1=hist2.GetYaxis().GetBinLowEdge(biny)
289  y2=hist2.GetYaxis().GetBinUpEdge(biny)
290  if hist2.GetBinContent(binTBM[0],biny) >= 0.97*hist2.GetMaximum():
291  draw_box(boxList,x1,x2,y1,y2,0.2,1,0,1,lineWd)
292  else:
293  draw_box(boxList,x1,x2,y1,y2,0.2,633,0,1,lineWd)
294  c.SaveAs('MergedOccupancyDeadROC_BPix_Layer{0}_TBM.pdf'.format(lyr))
295  os.system('gs -dBATCH -dNOPAUSE -sDEVICE=png16m -dUseCropBox -sOutputFile=MergedOccupancyDeadROC_BPix_Layer{0}_TBM.png -r144 -q MergedOccupancyDeadROC_BPix_Layer{0}_TBM.pdf'.format(lyr))
296  os.system('rm -f MergedOccupancyDeadROC_BPix_Layer{0}_TBM.pdf'.format(lyr))
297  else :
298  print "Some Error in get the histograms for FPIX"
299  #FPIX
300  print "----> Build maps for FPix"
301  for rng in range(1,3):
302  histOccList.append(rootf.FindObjectAny(hoccF+str(rng)))
303  histDeadList.append(rootf.FindObjectAny(hdeadF+str(rng)))
304  for hist1, hist2 in zip(histOccList, histDeadList):
305  if hist1 != None or hist2 !=None:
306  hist1.Draw("colz")
307  match=re.search('(?<=PXRing_)[0-9]',hist1.GetName())
308  if match != None and "per_SignedDiskCoord_per_SignedBladePanelCoord" in hist1.GetName():
309  ring=int(match.group(0))
310  hist1.SetTitle("Digi Occupancy Ring {0}".format(ring))
311  boxList=[]
312  lineList=[]
313  renderPluginFPIX(lineList,ring)
314  lineWd=3
315  if ring==2 :
316  lineWd=2
317  tbmRoc=8
318  binTBM=[]
319  for biny in range(1,hist2.GetNbinsY()+1):
320  if len(binTBM)!=0:
321  x1=hist2.GetXaxis().GetBinLowEdge(binTBM[0])
322  x2=hist2.GetXaxis().GetBinUpEdge(binTBM[len(binTBM)-1])
323  y1=hist2.GetYaxis().GetBinLowEdge(biny-1)
324  y2=hist2.GetYaxis().GetBinUpEdge(biny-1)
325  if hist2.GetBinContent(binTBM[0],biny-1) >= 0.97*hist2.GetMaximum():
326  draw_box(boxList,x1,x2,y1,y2,0.2,1,0,1,lineWd)
327  else:
328  draw_box(boxList,x1,x2,y1,y2,0.2,633,0,1,lineWd)
329  binTBM=[]
330  for binx in range(1,hist2.GetNbinsX()+1):
331  if hist2.GetBinContent(binx,biny)!=0:
332  if len(binTBM)==0:
333  binTBM.append(binx)
334  else:
335  if len(binTBM)==tbmRoc:
336  x1=hist2.GetXaxis().GetBinLowEdge(binTBM[0])
337  x2=hist2.GetXaxis().GetBinUpEdge(binTBM[len(binTBM)-1])
338  y1=hist2.GetYaxis().GetBinLowEdge(biny)
339  y2=hist2.GetYaxis().GetBinUpEdge(biny)
340  if hist2.GetBinContent(binTBM[0],biny) >= 0.97*hist2.GetMaximum():
341  draw_box(boxList,x1,x2,y1,y2,0.2,1,0,1,lineWd)
342  else:
343  draw_box(boxList,x1,x2,y1,y2,0.2,633,0,1,lineWd)
344  binTBM=[]
345  binTBM.append(binx)
346  else:
347  binTBM.append(binx)
348  else:
349  if len(binTBM)!=0:
350  x1=hist2.GetXaxis().GetBinLowEdge(binTBM[0])
351  x2=hist2.GetXaxis().GetBinUpEdge(binTBM[len(binTBM)-1])
352  y1=hist2.GetYaxis().GetBinLowEdge(biny)
353  y2=hist2.GetYaxis().GetBinUpEdge(biny)
354  if hist2.GetBinContent(binTBM[0],biny) >= 0.97*hist2.GetMaximum():
355  draw_box(boxList,x1,x2,y1,y2,0.2,1,0,1,lineWd)
356  else:
357  draw_box(boxList,x1,x2,y1,y2,0.2,633,0,1,lineWd)
358  c.SaveAs('MergedOccupancyDeadROC_FPix_Ring{0}_TBM.pdf'.format(ring))
359  os.system('gs -dBATCH -dNOPAUSE -sDEVICE=png16m -dUseCropBox -sOutputFile=MergedOccupancyDeadROC_FPix_Ring{0}_TBM.png -r144 -q MergedOccupancyDeadROC_FPix_Ring{0}_TBM.pdf'.format(ring))
360  os.system('rm -f MergedOccupancyDeadROC_FPix_Ring{0}_TBM.pdf'.format(ring))
361 
362  else :
363  print "Some Error in get the histograms for FPIX"
364 
365 
366 
367 
368 if __name__ == '__main__':
369  main()
def draw_box(boxList, xl, xr, yl, yr, opacity=1, color=1, style=1001, lstyle=1, lw=3)
def draw_line(lineList, x1, x2, y1, y2, width=1, style=1, color=1)
def maskFPixROC(boxList, xsign, ysign, dsk, bld, pnl, roc)
OutputIterator zip(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp)
T min(T a, T b)
Definition: MathUtil.h:58
def maskBPixROC(boxList, xsign, ysign, layer, lad, mod, roc)
def dqm_get_dataset(server, match, run, type="offline_data")
def renderPluginFPIX(lineList, ring)
Definition: main.py:1
def renderPluginBPIX(lineList, layer)
#define str(s)