Beispiel #1
0
def compareTwoHists(file1,histName1,file2,histName2,tag='',fitFunctionName=None,legs=None):
    print 'open TFiles'
    tFiles = [ TFile(file1), TFile(file2)]
    print 'opened ', len(tFiles), ' TFiles'
    histos = [tFiles[0].Get(histName1), tFiles[1].Get(histName2)]
    print 'compare ', len(histos),' root histos '

    compareRootHists.compareHists(histos,legends=legs,normalize=True,fitName=fitFunctionName,t=tag,pad=None,myTextSize=0.05)

    for tf in tFiles:        
        print 'closing ', tf.GetName()
        tf.Close()
    return
Beispiel #2
0
def compareHists(files,histName,tag='',fitFunctionName=None, legends=None):
    print 'compareHists for ', len(files), ' for histName ', histName, ' tag ', tag
    print 'open TFiles'
    tFiles = []
    for f in files:
        tFiles.append(TFile(f))
    print 'opened ', len(tFiles)

    histos = []
    for tf in tFiles:
        print 'tf ', tf.GetName()
        h = tf.Get(histName)
        histos.append(h)
            
    print 'compare ', len(histos),' root histos'

    compareRootHists.compareHists(histos,legends=legends,normalize=True,fitName=fitFunctionName,t=tag,pad=None,myTextSize=0.05)

    for tf in tFiles:        
        print 'closing ', tf.GetName()
        tf.Close()
    return
Beispiel #3
0
def compareSensorHists(files,histName,tag='',half=None,singleCanvas=None,beamspot=False,legends=None):
    print 'compareSensorHists for ', len(files), ' for histName ', histName, ' tag ', tag, ' beamspot ', beamspot
    sensorHistNames = []
    sensors = hps_utils.getSensorNames(beamspot)
    for s in sensors:
        if hps_utils.getLayer > 3 and hps_utils.getHoleSlot(s) == 'slot':
            continue
        #if hps_utils.getHalf(s) != 'b' or hps_utils.getLayer(s) !=2 or hps_utils.getAxialStereo(s) != 'axial':
        #    continue
        #if hps_utils.getHalf(s) != 't' or hps_utils.getLayer(s) !=6 or hps_utils.getHoleSlot(s) != 'slot' and hps_utils.getAxialStereo(s) != 'slot':
        #    continue
        #if beamspot and hps_utils.getLayer(s)==0:
        #    if hps_utils.getHalf(s) != 'b':
        #        print 'this beamspot sensor is weird! ', s
        #        sys.exit(1)
        if hps_utils.getHalf(s) == 't' and half != 'top':
            continue
        if hps_utils.getHalf(s) == 'b' and half != 'bottom':
            continue
        addOn = ''
        if hps_utils.getHalf(s) == 't':
            addOn = '_top'
        else:
            addOn = '_bot'
        sensorHistNames.append(histName + s + addOn)
    print sensorHistNames
    print 'open TFiles'
    tFiles = []
    for f in files:
        tFiles.append(TFile(f))
    print 'opened ', len(tFiles)
    c = None
    print 'singleCanvas ', singleCanvas
    if singleCanvas != None:
        cName = 'c_' + histName + '_' + half + '_' + tag
        c = TCanvas(cName, cName, 10, 10, 690*2, 390*2)
        c.Divide(2,12)
    currentPad = None

    graphBinNames = {}
    graphMean = []
    graphRMS = []
    for tf in tFiles:
        print 'tf ', tf.GetName()
        grM = TGraphErrors()
        grM.SetName('grMean_' + tf.GetName())
        grR = TGraphErrors()
        grR.SetName('grRMS_' + tf.GetName())
        graphMean.append( grM )
        graphRMS.append( grR )
    
    for hName in sensorHistNames:
        histos = []
        print 'get ', hName
        for tf in tFiles:
            print 'tf ', tf.GetName()
            h = tf.Get(hName)
            histos.append(h)
            
        print 'compare ', len(histos),' root histos'
        if c != None:
            i = hps_utils.getCanvasIdxTwoCols(hName,beamspot)
            currentPad = c.cd(i)
        print currentPad
        compareRootHists.compareHists(histos,legends=legends,normalize=True,fitName={'name':'gaus','rms':2},t=tag,pad=currentPad,myTextSize=0.1)
        print 'make graphs of mean and RMS'
        for ih in range(len(histos)):
            h = histos[ih]
            f = h.GetFunction('fg_' + h.GetName())
            if f != None:
                mean = f.GetParameter(1)
                meanError = f.GetParError(1)
                rms = f.GetParameter(2)
                rmsError = f.GetParError(2)
                ipoint = graphMean[ih].GetN()
                graphMean[ih].SetPoint(ipoint, ipoint, mean)
                graphRMS[ih].SetPoint(ipoint, ipoint, rms)
                graphMean[ih].SetPointError(ipoint, 0., meanError)
                graphRMS[ih].SetPointError(ipoint, 0., rmsError)
                graphBinNames[ ipoint ] = hps_utils.getshortsensorname( hName )
                print 'mean ', mean, ' RMS ', rms, ' fg ', f.GetName()
            else:
                print 'No fg in histo ', ih, ' name ', h.GetName()
        
        print 'done comparing ', len(histos),' root histos'
    if c != None:
        c.SaveAs(cName + '.png')

    cGrName = 'c_summary' + histName + '_' + half + '_' + tag 
    cGr = TCanvas(cGrName, cGrName, 10, 10, 690*2, 390*2)
    cGr.Divide(1,2)
    grMaxValMean = -1000000.
    grMaxValRMS = -10000000.
    grMinValMean = 1000000.
    grMinValRMS = 10000000.
    
    for igr in range(len(graphMean)):
        grM = graphMean[ igr ]
        grR = graphRMS [ igr ]
        limVals = plotutils.getGraphMaxMinVal(grM)
        print limVals
        if limVals[1] > grMaxValMean:
            grMaxValMean = limVals[1]
        if limVals[0] < grMinValMean:
            grMinValMean = limVals[0]
        limVals = plotutils.getGraphMaxMinVal(grR)
        if limVals[1] > grMaxValRMS:
            grMaxValRMS = limVals[1]
        if limVals[0] < grMinValMean:
            grMinValRMS = limVals[0]

    print grMaxValMean, grMinValMean
    
    for igr in range(len(graphMean)):
        grM = graphMean[ igr ]
        grR = graphRMS [ igr ]
        #print 'igr ', igr, ' name ', grM.GetName() 
        plotutils.setGraphStyle(grM,igr+1)
        plotutils.setGraphStyle(grR,igr+1)
        plotutils.setBinLabelsDict(grM,graphBinNames)
        plotutils.setBinLabelsDict(grR,graphBinNames)
        if igr == 0:
            cGr.cd(1)
            gPad.SetBottomMargin(0.3)
            grM.GetHistogram().SetMaximum(grMaxValMean*1.2)
            grM.GetHistogram().SetMinimum(grMinValMean*1.2)
            grM.Draw('AXPL')
            cGr.cd(2)
            gPad.SetBottomMargin(0.3)
            grR.SetMaximum(grMaxValRMS)
            grR.SetMinimum(grMinValRMS)
            grR.Draw('AXPL')
        else:
            cGr.cd(1)
            grM.Draw('PL,same')
            cGr.cd(2)
            grR.Draw('PL,same')

    if legends:
        styles = [ 'PL' for x in range( len( graphMean ) ) ] 
        l = plotutils.getLegendList(0.13,0.75,0.25,0.85,graphMean,legends,styles)
        l.Draw()
    

    cGr.SaveAs(cGr.GetName() + '.png')

    #ans = raw_input('press anything')
    
        
    for tf in tFiles:        
        print 'closing ', tf.GetName()
        tf.Close()
    return