Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
#!/usr/bin/env python3

import ROOT
from ROOT import TBufferFile, TH1F, TProfile, TProfile2D, TH2F, TFile, TH1D, TH2D
import re
import os


def draw_line(lineList,x1,x2,y1,y2,width=1,style=1,color=1):
    from ROOT import TLine
    l=TLine(x1,y1,x2,y2)
    l.SetBit(ROOT.kCanDelete)
    l.SetLineWidth(width)
    l.SetLineStyle(style)
    l.SetLineColor(color)
    l.Draw()
    lineList.append(l)

def draw_box(boxList,xl,xr,yl,yr,opacity=1,color=1,style=1001,lstyle=1,lw=3):
    from ROOT import TBox
    b=TBox(xl,yl,xr,yr)
    b.SetBit(ROOT.kCanDelete)
    b.SetFillStyle(style)
    b.SetFillColorAlpha(color, opacity)
    b.SetLineColor(color)
    b.SetLineWidth(lw)
    b.SetLineStyle(lstyle)
    b.Draw()
    boxList.append(b)

def renderPluginBPIX(lineList,layer) :
    from ROOT import TCanvas,TLine
    nlad=[6,14,22,32]
    coordSign=[(-1,-1),(-1,1),(1,-1),(1,1)]
    for xsign,ysign in coordSign:
        xlow = xsign*0.5
        xhigh= xsign*(0.5+4)
        ylow = ysign*0.5
        yhigh= ysign*(0.5 + nlad[layer-1])
        # Outside Box
        draw_line(lineList,xlow,  xhigh,  ylow,  ylow) # bottom
        draw_line(lineList,xlow,  xhigh, yhigh, yhigh) # top
        draw_line(lineList,xlow,   xlow,  ylow, yhigh) # left
        draw_line(lineList,xhigh, xhigh,  ylow, yhigh) # right
        # Inner Horizontal lines
        for lad in range(nlad[layer-1]):
            lad+=1
            if lad != nlad[layer-1]:
                y = ysign * (lad+0.5)
                draw_line(lineList,xlow, xhigh,  y,  y)
            y = ysign * (lad);
            draw_line(lineList,xlow, xhigh,  y,  y, 1, 3);
        # Inner Vertical lines
        for mod in range(3) : 
            mod+=1
            x = xsign * (mod + 0.5);
            draw_line(lineList,x, x,  ylow,  yhigh);

        # Draw ROC0
        for mod in range(4):
            mod+=1
            for lad in range(nlad[layer-1]):
                lad+=1
                if ysign==1:
                    flipped = not(lad%2==0)
                else :
                    flipped = not(lad%2==1)
                if flipped : roc0_orientation = -1
                else : roc0_orientation = 1
                if xsign==-1 : roc0_orientation *= -1
                if ysign==-1 : roc0_orientation *= -1
                x1 = xsign * (mod+0.5)
                x2 = xsign * (mod+0.5 - 1./8);
                y1 = ysign * (lad)
                y2 = ysign * (lad + roc0_orientation*1./2)
                if layer == 1 and xsign == -1 :
                    x1 = xsign * (mod-0.5)
                    x2 = xsign * (mod-0.5 + 1./8)
                    y1 = ysign * (lad)
                    y2 = ysign * (lad - roc0_orientation*1./2)

                    draw_line(lineList,x1, x2, y1, y1, 1)
                    draw_line(lineList,x2, x2, y1, y2, 1)
                  
                else:
                    draw_line(lineList,x1, x2, y1, y1, 1)
                    draw_line(lineList,x2, x2, y1, y2, 1)

def maskBPixROC(boxList,xsign,ysign,layer,lad,mod,roc):
    if roc<8 : 
        rocShiftX=roc*1./8
        rocShiftY=0
    else : 
        rocShiftX=(15-roc)*1./8
        rocShiftY=1./2
    if ysign==1:
        flipped = not(lad%2==0)
    else :
        flipped = not(lad%2==1)
    if flipped : roc0_orientation = -1
    else : roc0_orientation = 1
    if xsign==-1 : roc0_orientation *= -1
    if ysign==-1 : roc0_orientation *= -1
    x1 = xsign * (mod+0.5-rocShiftX)
    x2 = xsign * (mod+0.5 - 1./8-rocShiftX);
    y1 = ysign * (lad-roc0_orientation*rocShiftY)
    y2 = ysign * (lad + roc0_orientation*1./2-roc0_orientation*rocShiftY)
    if layer == 1 and xsign == -1 :
        x1 = xsign * (mod-0.5)-rocShiftX
        x2 = xsign * (mod-0.5 + 1./8)-rocShiftX
        y1 = ysign * (lad +rocShiftY)
        y2 = ysign * (lad - roc0_orientation*1./2+rocShiftY)
    draw_box(boxList,min(x1,x2),max(x1,x2),min(y1, y2),max(y1,y2),0.75)


                  
def renderPluginFPIX(lineList,ring) :
    from ROOT import TCanvas,TLine
    coordSign=[(-1,-1),(-1,1),(1,-1),(1,1)]
    for dsk in range(3) :
        dsk+=1
        for xsign,ysign in coordSign:            
            for bld in range(5+ring*6):
                bld+=1
                # Panel 2 has dashed mid-plane
                x1      = xsign * (0.5 + dsk - 1)
                x2      = xsign * (0.5 + dsk)
                sign = ysign
                y1      = ysign * (bld + sign*0.5)
                y2      = ysign * (bld)
                yp2_mid = ysign * (bld - sign*0.25)
                y3      = ysign * (bld - sign*0.5)
                draw_line(lineList,x1, x2, y1, y1)
                draw_line(lineList,x1, x2, y2, y2)
                draw_line(lineList,x1, x2, yp2_mid, yp2_mid,1,2)
                draw_line(lineList,x1, x2, y3, y3)
                # Vertical lines
                x = xsign * (0.5 + dsk - 1)
                draw_line(lineList,x,  x,  y1,  y2)
                draw_line(lineList,x,  x,  y2,  y3)
                if ring==2 :
                    x = xsign * (0.5 + dsk)
                    draw_line(lineList,x,  x,  y1,  y2)
                    draw_line(lineList,x,  x,  y2,  y3)
                #Make a BOX around ROC 0
                x1 = xsign * (0.5 + dsk - 1/8.)
                x2 = xsign * (0.5 + dsk)
                y1_p1 = ysign * (bld + sign*0.25)
                y2_p1 = ysign * (bld + sign*0.25 + xsign*ysign*0.25)
                draw_line(lineList,x1, x2, y1_p1, y1_p1, 1)
                draw_line(lineList,x1, x1, y1_p1, y2_p1, 1)
                y1_p2 = ysign * (bld - sign*0.25)
                y2_p2 = ysign * (bld - sign*0.25 - xsign*ysign*0.25)
                draw_line(lineList,x1, x2, y1_p2, y1_p2)
                draw_line(lineList,x1, x1, y1_p2, y2_p2)

def maskFPixROC(boxList,xsign,ysign,dsk,bld,pnl,roc) :
    from ROOT import TCanvas,TLine       
    if roc<8 : 
        rocShiftX=roc*1./8
        rocShiftY=0
    else : 
        rocShiftX=(15-roc)*1./8
        rocShiftY=1./4
    sign=ysign
    x1 = xsign * (0.5 + dsk - 1/8.-rocShiftX)
    x2 = xsign * (0.5 + dsk-rocShiftX)
    if pnl==1:
        y1 = ysign * (bld + sign*0.25)-xsign*rocShiftY
        y2 = ysign * (bld + sign*0.25 + xsign*ysign*0.25)-xsign*rocShiftY
    else:
        y1 = ysign * (bld - sign*0.25)+xsign*rocShiftY
        y2 = ysign * (bld - sign*0.25 - xsign*ysign*0.25)+xsign*rocShiftY
    draw_box(boxList,min(x1,x2),max(x1,x2),min(y1,y2),max(y1,y2),0.75)


def dqm_get_dataset(server, match, run, type="offline_data"):
    datareq = urllib2.Request(('%s/data/json/samples?match=%s') % (server, match))
    datareq.add_header('User-agent', ident)
    # Get data                                                                                                                              
    data = eval(re.sub(r"\bnan\b", "0", urllib2.build_opener(X509CertOpen()).open(datareq).read()),
                       { "__builtins__": None }, {})
    ret = ""
    for l in data['samples']:
        if l['type'] == type:
            for x in l['items']:
                if int(x['run']) == int(run):
                    ret=x['dataset']
                    break
    print(ret)
    return ret




def main():
    import sys
    import os
    import ROOT

    if len(sys.argv) != 3:
        print("input files needed!")
        return
    else:
        filename=sys.argv[1]
        pclfile=sys.argv[2]
    print(filename+" -- "+filename[19:25])
    print(pclfile+" -- "+pclfile[19:25])
    runNum=filename[19:25]

    dir="DQMData/Run " + runNum + "/PixelPhase1/Run summary/Pahse1_MechanicalView/"
    dirERROR="DQMData/Run " + runNum + "/PixelPhase1/Run summary/SiPixelQualityPCL/BadROC_Other/"


    dirBPix=dir + "PXBarrel/"
    dirFPix=dir + "PXForward/"

    hoccB="digi_occupancy_per_SignedModuleCoord_per_SignedLadderCoord_PXLayer_"
    hoccF="digi_occupancy_per_SignedDiskCoord_per_SignedBladePanelCoord_PXRing_"
    hdeadB="Dead Channels per ROC_per_SignedModuleCoord_per_SignedLadderCoord_PXLayer_"
    hdeadF="Dead Channels per ROC_per_SignedDiskCoord_per_SignedBladePanelCoord_PXRing_"
        
    ROOT.gROOT.SetBatch(1)    
    ROOT.gStyle.SetOptStat(0)
    ROOT.gStyle.SetPalette(52) #104 kTemperatureMap // 55 kRainBow // 97 kRust // 57 kBird
    color=[]
    for i in range(0,255):
        color.append(ROOT.TColor.GetColorPalette(i))
    ROOT.gStyle.SetPalette(1) #104 kTemperatureMap // 55 kRainBow
    ROOT.gStyle.SetNumberContours(128)
    rootf=ROOT.TFile(filename)
    rootp=ROOT.TFile(pclfile)
    


    c=ROOT.TCanvas("c","c",1250,1000)
    #BPIX
    print("----> Build maps for BPix")
    histOccList=[]
    histDeadList=[]
    for lyr in range(1,5):
        histOccList.append(rootf.FindObjectAny(hoccB+str(lyr)))
        histDeadList.append(rootp.Get(dirERROR+hdeadB+str(lyr)))
    for hist1, hist2 in zip(histOccList, histDeadList):
        if hist1 != None or hist2 !=None:
            hist1.Draw("colz")
            match=re.search('(?<=PXLayer_)[0-9]',hist1.GetName())
            if match != None and "per_SignedModuleCoord_per_SignedLadderCoord" in hist1.GetName():
                lyr=int(match.group(0))
                hist1.SetTitle("Digi Occupancy Layer {0}".format(lyr))
                boxList=[]
                lineList=[]
                renderPluginBPIX(lineList,lyr)
                lineWd=3
                if lyr==4 :
                    lineWd=2
                if lyr==1: 
                    tbmRoc=4
                else:
                    tbmRoc=8
                binTBM=[]
                singleROC=0
                maxx=hist2.GetMaximum()
                for biny in range(1,hist2.GetNbinsY()+1):
                    if len(binTBM)!=0:
                        x1=hist2.GetXaxis().GetBinLowEdge(binTBM[0])
                        x2=hist2.GetXaxis().GetBinUpEdge(binTBM[len(binTBM)-1])
                        y1=hist2.GetYaxis().GetBinLowEdge(biny-1)
                        y2=hist2.GetYaxis().GetBinUpEdge(biny-1)
                        draw_box(boxList,x1,x2,y1,y2,0.2,color[100+int((224-100)*(1-hist2.GetBinContent(binTBM[0],biny-1)/maxx))],0,1,lineWd)
                    binTBM=[]
                    singleROC=0
                    for binx in range(1,hist2.GetNbinsX()+1):
                        if len(binTBM)==0:
                            check=True
                        elif hist2.GetBinContent(binx,biny)==hist2.GetBinContent(binTBM[len(binTBM)-1],biny):
                            check=True
                        else:
                            check=False
                        if hist2.GetBinContent(binx,biny)!=0 and check:
                            if len(binTBM)==0:
                                binTBM.append(binx)
                            else:
                                if len(binTBM)==(tbmRoc): 
                                    x1=hist2.GetXaxis().GetBinLowEdge(binTBM[0])
                                    x2=hist2.GetXaxis().GetBinUpEdge(binTBM[len(binTBM)-1])
                                    y1=hist2.GetYaxis().GetBinLowEdge(biny)
                                    y2=hist2.GetYaxis().GetBinUpEdge(biny)
                                    draw_box(boxList,x1,x2,y1,y2,0.2,color[100+int((224-100)*(1-hist2.GetBinContent(binTBM[0],biny)/maxx))],0,1,lineWd)
                                    binTBM=[]
                                    singleROC=0
                                    binTBM.append(binx)
                                else:
                                    binTBM.append(binx)
                        else:
                            if len(binTBM)!=0:
                                x1=hist2.GetXaxis().GetBinLowEdge(binTBM[0])
                                x2=hist2.GetXaxis().GetBinUpEdge(binTBM[len(binTBM)-1])
                                y1=hist2.GetYaxis().GetBinLowEdge(biny)
                                y2=hist2.GetYaxis().GetBinUpEdge(biny)
                                draw_box(boxList,x1,x2,y1,y2,0.2,color[100+int((224-100)*(1-hist2.GetBinContent(binTBM[0],biny)/maxx))],0,1,lineWd)
                                binTBM=[]
                                if hist2.GetBinContent(binx,biny)!=0:
                                    binTBM.append(binx)
                c.SaveAs('MergedPCLOther_BPix_Layer{0}_TBM.pdf'.format(lyr))
                os.system('gs -dBATCH -dNOPAUSE -sDEVICE=png16m -dUseCropBox -sOutputFile=MergedPCLOther_BPix_Layer{0}_TBM.png -r144 -q MergedPCLOther_BPix_Layer{0}_TBM.pdf'.format(lyr))
                os.system('rm -f MergedPCLOther_BPix_Layer{0}_TBM.pdf'.format(lyr))
        else :
            print("Some Error in get the histograms for FPIX")
    #FPIX
    print("----> Build maps for FPix")
    for rng in range(1,3):
        histOccList.append(rootf.FindObjectAny(hoccF+str(rng)))
        histDeadList.append(rootp.Get(dirERROR+hdeadF+str(rng)))
    for hist1, hist2 in zip(histOccList, histDeadList):
        if hist1 != None or hist2 !=None:
            hist1.Draw("colz")
            match=re.search('(?<=PXRing_)[0-9]',hist1.GetName())
            if match != None and "per_SignedDiskCoord_per_SignedBladePanelCoord" in hist1.GetName():
                ring=int(match.group(0))
                hist1.SetTitle("Digi Occupancy Ring {0}".format(ring))
                boxList=[]
                lineList=[]
                renderPluginFPIX(lineList,ring)
                lineWd=3
                if ring==2 :
                    lineWd=2
                tbmRoc=8
                binTBM=[]
                maxx=hist2.GetMaximum()
                for biny in range(1,hist2.GetNbinsY()+1):
                    if len(binTBM)!=0:
                        x1=hist2.GetXaxis().GetBinLowEdge(binTBM[0])
                        x2=hist2.GetXaxis().GetBinUpEdge(binTBM[len(binTBM)-1])
                        y1=hist2.GetYaxis().GetBinLowEdge(biny-1)
                        y2=hist2.GetYaxis().GetBinUpEdge(biny-1)
                        draw_box(boxList,x1,x2,y1,y2,0.2,color[100+int((224-100)*(1-hist2.GetBinContent(binTBM[0],biny-1)/maxx))],0,1,lineWd)
                    binTBM=[]
                    for binx in range(1,hist2.GetNbinsX()+1):
                        if len(binTBM)==0:
                            check=True
                        elif hist2.GetBinContent(binx,biny)==hist2.GetBinContent(binTBM[len(binTBM)-1],biny):
                            check=True
                        else:
                            check=False
                        if hist2.GetBinContent(binx,biny)!=0 and check:
                            if len(binTBM)==0:
                                binTBM.append(binx)
                            else:
                                if len(binTBM)==tbmRoc: 
                                    x1=hist2.GetXaxis().GetBinLowEdge(binTBM[0])
                                    x2=hist2.GetXaxis().GetBinUpEdge(binTBM[len(binTBM)-1])
                                    y1=hist2.GetYaxis().GetBinLowEdge(biny)
                                    y2=hist2.GetYaxis().GetBinUpEdge(biny)
                                    draw_box(boxList,x1,x2,y1,y2,0.2,color[100+int((224-100)*(1-hist2.GetBinContent(binTBM[0],biny)/maxx))],0,1,lineWd)
                                    binTBM=[]
                                    binTBM.append(binx)
                                else:
                                    binTBM.append(binx)
                        else:
                            if len(binTBM)!=0:
                                x1=hist2.GetXaxis().GetBinLowEdge(binTBM[0])
                                x2=hist2.GetXaxis().GetBinUpEdge(binTBM[len(binTBM)-1])
                                y1=hist2.GetYaxis().GetBinLowEdge(biny)
                                y2=hist2.GetYaxis().GetBinUpEdge(biny)
                                draw_box(boxList,x1,x2,y1,y2,0.2,color[100+int((224-100)*(1-hist2.GetBinContent(binTBM[0],biny)/maxx))],0,1,lineWd)
                                binTBM=[]
                                if hist2.GetBinContent(binx,biny)!=0:
                                    binTBM.append(binx)
                c.SaveAs('MergedPCLOther_FPix_Ring{0}_TBM.pdf'.format(ring))
                os.system('gs -dBATCH -dNOPAUSE -sDEVICE=png16m -dUseCropBox -sOutputFile=MergedPCLOther_FPix_Ring{0}_TBM.png -r144 -q MergedPCLOther_FPix_Ring{0}_TBM.pdf'.format(ring))
                os.system('rm -f MergedPCLOther_FPix_Ring{0}_TBM.pdf'.format(ring))

        else :
            print("Some Error in get the histograms for FPIX")




if __name__ == '__main__':
    main()