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
import ROOT
from .drawHistoAllChambers import drawHisto

def binNumber(station,sector):
    start = (station - 1)*12
    return start + sector

def plot(fileName,sl,ymin=300,ymax=360,option="HISTOP",draw=True):

    slType = sl
    slStr = "SL%d" % slType
    verbose = False

    ROOT.TH1.AddDirectory(False)

    file = ROOT.TFile(fileName,'read')

    wheels = (-2,-1,0,1,2)
    stations = (1,2,3,4)

    histosWheel = {}
    for wh in wheels:
        histoName = 'Wheel%d_%s_TTrig' % (wh,slStr)
        print("Accessing",histoName)
        histosWheel[wh] = file.Get(histoName)

    # (Wh-2 MB1 Sec1 ... Wh-2 MB1 Sec12 ... Wh-1 MB1 Sec1 ... Wh-1 MB1 Sec12 ...)
    # (Wh-2 MB2 Sec1 ... Wh-2 MB2 Sec12 ... Wh-1 MB2 Sec1 ... Wh-1 MB1 Sec12 ...) ...  
    nBins = 250
    if slType == 2: nBins = 180
    histo = ROOT.TH1F("h_TTrigAll","TTrig",nBins,0,nBins)
    for st in stations:
        nSectors = 12
        if st == 4: nSectors = 14 
        if st == 4 and slType == 2: continue
        if verbose: print("Station",st)
        for wh in wheels:
            if verbose: print("Wheel",wh) 
            for sec in range(1,nSectors+1):
                if verbose: print("Sector",sec)
                binHisto = binNumber(st,sec)
                if verbose: print("Bin from histos:",binHisto) 
                value = histosWheel[wh].GetBinContent(binHisto)

                binHistoNew = (st - 1)*60 + (wh + 2)*nSectors + sec
                if verbose: print("Bin final",binHistoNew)
                histo.SetBinContent(binHistoNew,value) 

                if sec == 1:
                    label = "Wheel %d" % wh
                    if wh == -2: label += " MB%d" % st  
                    histo.GetXaxis().SetBinLabel(binHistoNew,label) 

    objects = drawHisto(histo,
                        title="t_{Trig} (ns)",
                        ymin=ymin,ymax=ymax,option=option,draw=draw)

    return objects

def compare(fileNames,sl,ymin=300,ymax=360,labels=[]):
    option = "HISTOP"
    colors = (2,4,12,44,55,38,27,46)
    markers = (24,25,26,27,28,30,32,5)

    idx = 0
    canvas = None
    objects = None
    histos = []
    for fileName in fileNames:
        draw = False
        if not idx: draw = True

        objs = plot(fileName,sl,ymin,ymax,option,draw)
        histos.append(objs[1])
        histos[-1].SetName( "%s_%d" % (histos[-1].GetName(),idx) )
        if not idx:
            canvas = objs[0]
            objects = objs[2]

        canvas.cd()
        if idx:
            histos[-1].SetLineColor(colors[ (idx - 1) % len(colors) ])
            histos[-1].SetMarkerColor(colors[ (idx - 1) % len(colors) ])
            histos[-1].SetMarkerStyle(markers[ (idx - 1) % len(markers) ])

            histos[-1].Draw(option + "SAME")

        idx += 1

    legend = ROOT.TLegend(0.4,0.7,0.95,0.8)
    for idx in range( len(histos) ):
        histo = histos[idx]
        label = histo.GetName()
        if len(labels): label = labels[idx]
        legend.AddEntry(histo,label,"LP")

        idx += 1

    canvas.cd()
    legend.SetFillColor( canvas.GetFillColor() )
    legend.Draw("SAME")

    objects.append(legend)

    return (canvas,histos,objects)

def compareDiff(fileNames,sl,ymin=-15.,ymax=15.):
    option = "HISTOP"
    colors = (2,4,9,12,38,44,46,55)
    markers = (24,25,26,27,28,30,32,5)

    idx = 0
    canvases = [None,None]
    objects = None
    histoRef = None
    histos = []
    histosDist = []
    for fileName in fileNames:
        objs = plot(fileName,sl,300,360,'',False)
        histos.append( objs[1].Clone(objs[1].GetName() + "_diff") )
        histos[-1].SetName( "%s_%d" % (histos[-1].GetName(),idx) )
        if not idx:
            histoRef = objs[1]
            histos[-1].Reset()
        else:
            histos[-1].Add(histoRef,-1.) 

        draw = False
        if not idx: draw = True

        objs = drawHisto(histos[-1],
                         title="t_{Trig} difference (ns)",
                         ymin=ymin,ymax=ymax,option=option,draw=draw)

        if not idx: 
            canvases[0] = objs[0]
            objects = objs[2]

        if idx:
            canvases[0].cd()
            histos[-1].SetLineColor(colors[ (idx - 1) % len(colors) ])
            histos[-1].SetMarkerColor(colors[ (idx - 1) % len(colors) ])
            histos[-1].SetMarkerStyle(markers[ (idx - 1) % len(markers) ])

            histos[-1].Draw(option + "SAME")

            histosDist.append( ROOT.TH1F(histos[-1].GetName() + "_dist","tTrig distribution",200,ymin,ymax) )
            for ibin in range(1,histos[-1].GetNbinsX()+1):
                histosDist[-1].Fill( histos[-1].GetBinContent(ibin) )

            histosDist[-1].SetLineColor(colors[ (idx - 1) % len(colors) ])
            histosDist[-1].SetMarkerColor(colors[ (idx - 1) % len(colors) ])
            histosDist[-1].SetMarkerStyle(markers[ (idx - 1) % len(markers) ])

        idx += 1


    canvases[1] = ROOT.TCanvas("c_tTrigDist")
    canvases[1].SetGridy()
    canvases[1].SetFillColor(0)
    canvases[1].cd()
    option = "HISTO"
    idx = 0
    for histo in histosDist:
        if not idx:
            histo.GetXaxis().SetTitle("t_{Trig} difference (ns)")
            histo.GetYaxis().SetTitle("Number of chambers")
            histo.Draw(option)
        else:
            histo.Draw(option + "SAME") 
        idx += 1

    return (canvases,histos,histosDist,objects)