Exemple #1
0
    def __init__(self):
        super(ScanDataFitter, self).__init__()

        from gempython.utils.nesteddict import nesteddict as ndict
        r.gStyle.SetOptStat(0)

        self.scanHistos = ndict()
        self.scanCount = ndict()
        self.scanFits = ndict()

        for vfat in range(0, 24):
            self.scanFits[0][vfat] = np.zeros(128)
            self.scanFits[1][vfat] = np.zeros(128)
            self.scanFits[2][vfat] = np.zeros(128)
            self.scanFits[3][vfat] = np.zeros(128)
            self.scanFits[4][vfat] = np.zeros(128)
            self.scanFits[5][vfat] = np.zeros(128)
            self.scanFits[6][vfat] = np.zeros(128, dtype=bool)
            for ch in range(0, 128):
                self.scanHistos[vfat][ch] = r.TH1D(
                    'scurve_%i_%i_h' % (vfat, ch),
                    'scurve_%i_%i_h' % (vfat, ch), 254, 0.5, 254.5)
                self.scanCount[vfat][ch] = 0

        self.fitValid = [np.zeros(128, dtype=bool) for vfat in range(24)]
        self.Nev = -1
Exemple #2
0
    def __init__(self):
        super(ScanDataFitter, self).__init__()

        from gempython.utils.nesteddict import nesteddict as ndict
        r.gStyle.SetOptStat(0)

        self.scanFuncs  = ndict()
        self.scanHistos = ndict()
        self.scanCount  = ndict()
        self.scanFits   = ndict()

        for vfat in range(0,24):
            self.scanFits[0][vfat] = np.zeros(128)
            self.scanFits[1][vfat] = np.zeros(128)
            self.scanFits[2][vfat] = np.zeros(128)
            self.scanFits[3][vfat] = np.zeros(128)
            self.scanFits[4][vfat] = np.zeros(128)
            self.scanFits[5][vfat] = np.zeros(128)
            self.scanFits[6][vfat] = np.zeros(128, dtype=bool)
            for ch in range(0,128):
                self.scanFuncs[vfat][ch] = r.TF1('scurveFit_vfat%i_chan%i'%(vfat,ch),'[3]*TMath::Erf((TMath::Max([2],x)-[0])/(TMath::Sqrt(2)*[1]))+[3]',1,253)
                self.scanHistos[vfat][ch] = r.TH1D('scurve_vfat%i_chan%i_h'%(vfat,ch),'scurve_vfat%i_chan%i_h'%(vfat,ch),254,0.5,254.5)
                self.scanCount[vfat][ch] = 0

        self.fitValid = [ np.zeros(128, dtype=bool) for vfat in range(24) ]
        self.Nev = -1
    def __init__(self, calDAC2Q_m=None, calDAC2Q_b=None, isVFAT3=False):
        """
        calDAC2Q_m - list of slope values for "fC = m * cal_dac + b" equation, ordered by vfat position
        calDAC2Q_b - as calDAC2Q_m but for intercept b
        isVFAT3 - if using VFAT3
        """

        super(ScanDataFitter, self).__init__()

        from gempython.utils.nesteddict import nesteddict as ndict
        r.gStyle.SetOptStat(0)

        self.Nev = ndict()
        self.scanFuncs = ndict()
        self.scanHistos = ndict()
        self.scanCount = ndict()
        self.scanFitResults = ndict()

        self.isVFAT3 = isVFAT3

        self.calDAC2Q_m = np.ones(24)
        if calDAC2Q_m is not None:
            self.calDAC2Q_m = calDAC2Q_m

        self.calDAC2Q_b = np.zeros(24)
        if calDAC2Q_b is not None:
            self.calDAC2Q_b = calDAC2Q_b

        for vfat in range(0, 24):
            self.scanFitResults[0][vfat] = np.zeros(128)
            self.scanFitResults[1][vfat] = np.zeros(128)
            self.scanFitResults[2][vfat] = np.zeros(128)
            self.scanFitResults[3][vfat] = np.zeros(128)
            self.scanFitResults[4][vfat] = np.zeros(128)
            self.scanFitResults[5][vfat] = np.zeros(128)
            self.scanFitResults[6][vfat] = np.zeros(128, dtype=bool)
            for ch in range(0, 128):
                self.scanCount[vfat][ch] = 0
                self.scanFuncs[vfat][ch] = r.TF1(
                    'scurveFit_vfat%i_chan%i' % (vfat, ch),
                    '[3]*TMath::Erf((TMath::Max([2],x)-[0])/(TMath::Sqrt(2)*[1]))+[3]',
                    self.calDAC2Q_m[vfat] * 1 + self.calDAC2Q_b[vfat],
                    self.calDAC2Q_m[vfat] * 253 + self.calDAC2Q_b[vfat])
                self.scanHistos[vfat][ch] = r.TH1D(
                    'scurve_vfat%i_chan%i_h' % (vfat, ch),
                    'scurve_vfat%i_chan%i_h' % (vfat, ch), 254,
                    self.calDAC2Q_m[vfat] * 0.5 + self.calDAC2Q_b[vfat],
                    self.calDAC2Q_m[vfat] * 254.5 + self.calDAC2Q_b[vfat])

        self.fitValid = [np.zeros(128, dtype=bool) for vfat in range(24)]

        return
filename = options.filename[:-5]
os.system("mkdir " + filename)

print filename
outfilename = options.outfilename

import ROOT as r
r.TH1.SetDefaultSumw2(False)
r.gROOT.SetBatch(True)
inF = r.TFile(filename+'.root')
outF = r.TFile(filename+'/'+outfilename, 'recreate')

VT1_MAX = 256

print 'Initializing Histograms'
vRate = ndict()
vRate2D = ndict()
for vfat in range(0,24):
    #1D distribution
    vRate[vfat] = r.TH1D("h_Rate_vs_vthr_VFAT%i"%vfat,"VFAT%i;CFG_THR_ARM_DAC #left[DAC Units#right];Rate #left(Hz#right)"%vfat,VT1_MAX+1,-0.5,VT1_MAX+0.5)
    vRate[vfat].GetXaxis().SetRangeUser(1e-1,1e9)

    #2D distribution
    vRate2D[vfat] = r.TH2D("h2DRate_vthr_vs_chan_VFAT%i"%vfat,"VFAT%i;vfatCH;CFG_THR_ARM_DAC #left[DAC Units#right];Rate #left(Hz#right)"%vfat,128,-0.5,127.5,VT1_MAX+1,-0.5,VT1_MAX+0.5)
    vRate2D[vfat].GetYaxis().SetRangeUser(1e-1,1e9)

print 'Filling Histograms'
for event in inF.rateTree :
    if event.vfatCH == 128:
        vRate[event.vfatN].Fill(event.vth,event.Rate)
    else:
                       args.detType)  # Set a dummy link for now
    amcBoard = vfatBoard.parentOH.parentAMC
    print('opened connection')

    import os
    if amcBoard.fwVersion < 3:
        print(
            "temperature monitoring of v2b electronics is not supported, exiting!!!"
        )
        exit(os.EX_USAGE)

    print("Getting VFAT Mask for All Links")
    ohVFATMaskArray = amcBoard.getMultiLinkVFATMask(args.ohMask)
    print("Getting CHIP IDs of all VFATs")
    from gempython.utils.nesteddict import nesteddict as ndict
    vfatIDvals = ndict()
    for ohN in range(0, 12):
        if (not ((args.ohMask >> ohN) & 0x1)):
            continue
        vfatBoard.parentOH.link = ohN
        vfatIDvals[ohN] = vfatBoard.getAllChipIDs(ohVFATMaskArray[ohN])

    # Configure DAC Monitoring
    print("configuring VFAT ADCs for temperature monitoring")
    tempSelect = 37
    if args.extTempVFAT:
        tempSelect = 38
    amcBoard.configureVFAT3DacMonitorMulti(tempSelect, args.ohMask)
    print("VFAT ADCs have been configured for temperature monitoring")

    print("Enabling SCA Monitoring")
Exemple #6
0
def plotTimeSeriesHV(args,qc8layer):
    """
    Makes time series plots of DCS data points

    args - namespace obtained from parsing the instane of ArgumentParser in main
    layer - string, either "top" or "bot"
    """


    # Define known constants
    knownLayers = [ "Top", "Bot" ]
    knownElectrodes = []
    for idx in range(1,4):
        for layer in knownLayers:
            knownElectrodes.append("G{0}{1}".format(idx,layer))
            pass
        pass
    knownElectrodes.append("Drift")
    knownObs = [ "Imon", "Vmon", "Status" ]
    
    if qc8layer not in knownLayers:
        raise RuntimeError("Layer '{0}' not understood, known values are {1}".format(qc8layer,knownLayers))

    # Try to load all histograms
    # ===================================================
    cName = "Chamber{0}_{1}_{2}".format(args.row,args.column,qc8layer)
    from gempython.gemplotting.utils.anautilities import getCyclicColor
    from gempython.utils.nesteddict import nesteddict as ndict
    dict_dcsPlots = ndict() # ["Electrode"]["Obs"] = TObject
    dict_legend = {} # [Obs] = TLegend
    for idx,electrode in enumerate(knownElectrodes):
        for obsData in knownObs:
            dirName = "{0}/Channel{1}".format(cName,electrode)
            plotName= "HV_{0}{1}_{2}_UTC_time".format(obsData,cName,electrode)
            try:
                dict_dcsPlots[electrode][obsData] = inF.Get("{0}/{1}".format(dirName,plotName))
                if args.debug:
                    print("Loaded plot: {0}/{1}".format(dirName,plotName))
            except AttributeError as error:
                printYellow("Distribution '{0}' not found in input file {1}. Skipping this distribution".format(
                    plotName,
                    args.inputfile))
                continue
            dict_dcsPlots[electrode][obsData].SetLineColor(getCyclicColor(idx))
            dict_dcsPlots[electrode][obsData].SetMarkerColor(getCyclicColor(idx))
            if obsData == "Vmon" or obsData == "Status":
                dict_dcsPlots[electrode][obsData].GetYaxis().SetRangeUser(0,1e3)
                if obsData == "Vmon":
                    dict_dcsPlots[electrode][obsData].GetYaxis().SetTitle("Vmon #left(V #right)")
            elif obsData == "Imon":
                dict_dcsPlots[electrode][obsData].GetYaxis().SetRangeUser(-2,2)
                dict_dcsPlots[electrode][obsData].GetYaxis().SetTitle("Imon #left(uA #right)")

            if obsData in dict_legend.keys():
                dict_legend[obsData].AddEntry(dict_dcsPlots[electrode][obsData],electrode,"LPE")
            else:
                dict_legend[obsData] = r.TLegend(0.7,0.75,0.9,0.9)
                dict_legend[obsData].AddEntry(dict_dcsPlots[electrode][obsData],electrode,"LPE")
            pass
        pass

    # Make output TCanvas objects - All Electrodes on one TCanvas per Observable
    # ===================================================
    dict_dcsCanvas = {} # ["Obs"] = TObject
    for obsData in knownObs:
        dict_dcsCanvas[obsData] = r.TCanvas("canv_{0}_{1}".format(obsData,cName),"{0}: {1}".format(cName,obsData),900,900)
        pass

    # Draw the observable onto the corresponding canvas
    for obsData in knownObs:
        drawOpt = None
        dict_dcsCanvas[obsData].cd()
        for electrode in knownElectrodes:
            if drawOpt is None:
                dict_dcsPlots[electrode][obsData].Draw("ALPE1")
                drawOpt = "sameLPE1"
            else:
                dict_dcsPlots[electrode][obsData].Draw(drawOpt)
                pass
            pass
        dict_legend[obsData].Draw("same")
        pass

    # Make output TCanvas objects - All observables on one TCanvas per Electrode
    # ===================================================
    dict_electrodeCanvas = {} # ["Electrode"] = TObject
    for electrode in knownElectrodes:
        dict_electrodeCanvas[electrode] = r.TCanvas("canv_{0}_{1}".format(electrode,cName),"{0}: {1}".format(cName,electrode),900,1800)
        dict_electrodeCanvas[electrode].Divide(1,3)

    # Draw the observable onto the corresponding canvas
    for electrode in knownElectrodes:
        for idx,obsData in enumerate(knownObs):
            dict_electrodeCanvas[electrode].cd(idx+1)
            dict_dcsPlots[electrode][obsData].Draw("ALPE1")
            pass
        pass

    # Make output TCanvas Objects - All electrodes and all observables on one TCanvas
    # ===================================================
    canv_Summary = r.TCanvas("canv_Summary_{0}".format(cName),"{0}: Summary".format(cName), 900,1800)
    canv_Summary.Divide(1,3)

    # Draw the observable onto the corresponding canvas
    for idx,obsData in enumerate(knownObs):
        drawOpt = None
        canv_Summary.cd(idx+1)
        for electrode in knownElectrodes:
            if drawOpt is None:
                dict_dcsPlots[electrode][obsData].Draw("ALPE1")
                drawOpt = "sameLPE1"
            else:
                dict_dcsPlots[electrode][obsData].Draw(drawOpt)
                pass
            pass
        dict_legend[obsData].Draw("same")
        pass

    # Make an output TFile
    # ===================================================
    if args.update:
        rootOpt = "UPDATE"
    else:
        rootOpt = "RECREATE"

    from gempython.gemplotting.utils.anautilities import getElogPath
    elogPath = getElogPath()
    if args.outfilename is None:
        outF = r.TFile("{0}/DCS_Plots.root".format(elogPath),rootOpt)
    else:
        outF = r.TFile(args.outfilename,rootOpt)
        pass

    #thisDir = outF.mkdir(cName)
    outF.mkdir(cName)
    thisDir = outF.GetDirectory(cName)
    thisDir.cd()
    canv_Summary.Write()
    canv_Summary.SaveAs("{0}/{1}.png".format(elogPath,canv_Summary.GetName()))
    for obsData in knownObs:
        dict_dcsCanvas[obsData].Write()
        dict_dcsCanvas[obsData].SaveAs("{0}/{1}.png".format(elogPath,dict_dcsCanvas[obsData].GetName()))
        pass

    for electrode in knownElectrodes:
        dict_electrodeCanvas[electrode].Write()
        dict_electrodeCanvas[electrode].SaveAs("{0}/{1}.png".format(elogPath,dict_electrodeCanvas[electrode].GetName()))
        pass
    
    return
ohboard = getOHObject(options.slot, options.gtx, options.shelf, options.debug)

if options.dirPath == None:
    dirPath = '%s/%s/trimming/z%f/%s' % (dataPath, chamber_config[options.gtx],
                                         ztrim, startTime)
else:
    dirPath = options.dirPath

# bias vfats
biasAllVFATs(ohboard, options.gtx, 0x0, enable=False)
writeAllVFATs(ohboard, options.gtx, "VThreshold1", options.vt1, 0)

CHAN_MIN = 0
CHAN_MAX = 128

masks = ndict()
for vfat in range(0, 24):
    for ch in range(CHAN_MIN, CHAN_MAX):
        masks[vfat][ch] = False

#Find trimRange for each VFAT
tRanges = ndict()
tRangeGood = ndict()
trimVcal = ndict()
trimCH = ndict()
goodSup = ndict()
goodInf = ndict()
for vfat in range(0, 24):
    tRanges[vfat] = 0
    tRangeGood[vfat] = False
    trimVcal[vfat] = 0
    # Get run number
    fields = args.infile.split("_")
    if "run" in fields[0]:
        runNo = fields[0]
    else:
        runNo = "runUnknown"
        pass

    # Make output dir in $ELOG_PATH
    from gempython.utils.wrappers import runCommand
    runCommand(["mkdir", "{0}/{1}".format(elogPath, runNo)])

    # Make nested containers
    from gempython.utils.nesteddict import nesteddict as ndict
    baseDir = ndict()  # baseDir[slot][oh] -> string
    vfatDirs = ["VFAT-{0}".format(x) for x in range(nVFATS)]

    allVFATsLatency = ndict()  #   allVFATsLatency[slot][oh]      -> histogram
    dictMapping = ndict()  #   dictMapping[slot][oh]          -> mapping dict
    latencyMean = ndict()  #   latencyMean[slot][oh]          -> histogram
    latencyRMS = ndict()  #   latencyRMS[slot][oh]           -> histogram
    vfatHitMulti = ndict()  #   vfatHitMulti[slot][oh][vfatN]  -> histogram
    vfatLatHists = ndict()  #   vfatHists[slot][oh][vfatN]     -> histogram
    vfatLatHists2D = ndict()  #   vfatHists2D[slot][oh][vfatN]   -> histogram

    # Get channel mapping?
    from gempython.gemplotting.utils.anautilities import getMapping, getSummaryCanvas
    if args.mapping is not None:
        print("Getting mapping")
        # Try to get the mapping data
Exemple #9
0
    pan_lookup[int(mapping[0])][int(mapping[2]) - 1] = int(mapping[3])

    if not (options.channels or options.PanPin):  #Readout Strips
        vfatCh_lookup[int(mapping[0])][int(mapping[1])] = int(mapping[2]) - 1
        pass
    elif options.channels:  #VFAT Channels
        vfatCh_lookup[int(mapping[0])][int(mapping[2]) -
                                       1] = int(mapping[2]) - 1
        pass
    elif options.PanPin:  #Panasonic Connector Pins
        vfatCh_lookup[int(mapping[0])][int(mapping[3])] = int(mapping[2]) - 1
        pass
    pass

print 'Initializing Histograms'
vSum = ndict()
hot_channels = []
for vfat in range(0, 24):
    hot_channels.append([])
    if not (options.channels or options.PanPin):
        vSum[vfat] = r.TH2D('vSum%i' % vfat,
                            'vSum%i;Strip;VThreshold1 [DAC units]' % vfat, 128,
                            -0.5, 127.5, VT1_MAX + 1, -0.5, VT1_MAX + 0.5)
        pass
    elif options.channels:
        vSum[vfat] = r.TH2D('vSum%i' % vfat,
                            'vSum%i;Channel;VThreshold1 [DAC units]' % vfat,
                            128, -0.5, 127.5, VT1_MAX + 1, -0.5, VT1_MAX + 0.5)
        pass
    elif options.PanPin:
        vSum[vfat] = r.TH2D(
startTime = datetime.datetime.now().strftime("%Y.%m.%d.%H.%M")
print startTime

ohboard = getOHObject(options.slot,options.gtx,options.shelf,options.debug)

if options.dirPath == None: dirPath = '%s/%s/trimming/z%f/%s'%(dataPath,chamber_config[options.gtx],ztrim,startTime)
else: dirPath = options.dirPath

# bias vfats
biasAllVFATs(ohboard,options.gtx,0x0,enable=False, debug=options.debug)
writeAllVFATs(ohboard, options.gtx, "VThreshold1", options.vt1, 0x0, options.debug)

CHAN_MIN = 0
CHAN_MAX = 128

masks = ndict()
for vfat in range(0,24):
    for ch in range(CHAN_MIN,CHAN_MAX):
        masks[vfat][ch] = False

#Find trimRange for each VFAT
tRanges    = ndict()
tRangeGood = ndict()
trimVcal = ndict()
trimCH   = ndict()
goodSup  = ndict()
goodInf  = ndict()
for vfat in range(0,24):
    tRanges[vfat] = 0
    tRangeGood[vfat] = False
    trimVcal[vfat] = 0
    supportedAnaTypes = ['scurveAna', 'trimAna']
    if options.anaType not in supportedAnaTypes:
        print("Invalid analysis specificed, please select only from the list:")
        print(supportedAnaTypes)
        exit(os.EX_USAGE)
        pass

    # Get info from input file
    parsedTuple = parseListOfScanDatesFile(options.filename,
                                           options.alphaLabels)
    listChamberAndScanDate = parsedTuple[0]

    # Define nested dictioniaries
    #   Outer key -> (chamberName,scandate) tuple
    #   Inner key -> vfat position
    dict_fitSum = ndict()
    dict_ScurveMean = ndict(
    )  # Inner key: (0,23) follows vfat #, -1 is summary over all det
    dict_ScurveSigma = ndict()
    dict_ScurveEffPed = ndict()

    dict_ScurveMeanByiEta = ndict()
    dict_ScurveSigmaByiEta = ndict()

    # Get the plots from all files
    for idx, chamberAndScanDatePair in enumerate(listChamberAndScanDate):
        # Setup the path
        dirPath = getDirByAnaType(options.anaType.strip("Ana"),
                                  chamberAndScanDatePair[0], options.ztrim)
        if not filePathExists(dirPath, chamberAndScanDatePair[1]):
            print 'Filepath %s/%s does not exist!' % (
Exemple #12
0
        nameX='scurve mean #left(fC#right)',
        nameY='ARM_TRIM_AMPLITUDE',
        isGblDac=False,
        description='Tree holding arming comparator trim data')
    trimDacArmTree.setDefaults(options, thisTime)

    calDacCalTree = gemDacCalTreeStructure(
        name='calDacCalibration',
        nameX='CFG_CAL_DAC',
        nameY='charge #left(fC#right)',
        storeRoot=True,
        description='Tree holding CFG_CAL_DAC Calibration')
    calDacCalTree.setDefaults(options, thisTime)

    print("Determining trimDAC to fC Calibration")
    dict_cal_trimDAC2fC_graph = ndict(
    )  # dict_cal_trimDAC2fC[vfat][chan] = TGraphErrors object
    dict_cal_trimDAC2fC_func = ndict(
    )  # dict_cal_trimDAC2fC[vfat][chan] = TF1 object
    for vfat in range(0, vfatsPerGemVariant[options.gemType]):
        func_charge_vs_calDac = r.TF1(
            "func_charge_vs_calDac_vfat%d" % (vfat), "[0]*x+[1]",
            calDac2Q_Slope[vfat] * 253 + calDac2Q_Intercept[vfat],
            calDac2Q_Slope[vfat] * 1 + calDac2Q_Intercept[vfat])
        func_charge_vs_calDac.SetParameter(0, calDac2Q_Slope[vfat])
        func_charge_vs_calDac.SetParameter(1, calDac2Q_Intercept[vfat])
        calDacCalTree.fill(func_dacFit=func_charge_vs_calDac,
                           vfatID=vfatIDvals[vfat],
                           vfatN=vfat)

        scurveMeansList = []
        for chan in range(chMin, chMax):
Exemple #13
0
    from gempython.gemplotting.mapping.chamberInfo import gemTypeMapping
    if 'gemType' not in inF.sbitDataTree.GetListOfBranches():
        gemType = "ge11"
    else:
        gemType = gemTypeMapping[rp.tree2array(tree=inF.sbitDataTree, branches =[ 'gemType' ] )[0][0]]
    print gemType
    ##### END
    from gempython.tools.hw_constants import vfatsPerGemVariant
    nVFATS = vfatsPerGemVariant[gemType]
    from gempython.gemplotting.mapping.chamberInfo import CHANNELS_PER_VFAT as maxChans
    
    print('Initializing histograms')
    from gempython.utils.nesteddict import nesteddict as ndict, flatten

    # Summary plots - 2D
    dict_h_vfatObsVsVfatPulsed = ndict() #Keys as: [isValid][calEnable][rate]

    # VFAT lvl plots - 1D
    dict_h_sbitMulti = ndict() #Keys as: [isValid][calEnable][rate][vfat]
    dict_h_sbitSize = ndict() #Keys as: [isValid][calEnable][rate][vfat]

    # VFAT lvl plots - 2D 
    dict_g_rateObsCTP7VsRatePulsed = ndict() #Keys as: [isValid][vfat]
    dict_g_rateObsFPGAVsRatePulsed = ndict() #Keys as: [isValid][vfat]
    dict_g_rateObsVFATVsRatePulsed = ndict() #Keys as: [isValid][vfat]
    dict_h_chanVsRatePulsed_ZRateObs = ndict() #Z axis is rate observed; Keys as: [isValid][calEnable][vfat]
    dict_h_sbitObsVsChanPulsed = ndict() #Keys as: [isValid][calEnable][rate][vfat]
    dict_h_sbitMultiVsSbitSize = ndict() #Keys as: [isValid][calEnable][rate][vfat]

    rateMap = {}
    from gempython.gemplotting.utils.anautilities import formatSciNotation
    os.system("mkdir " + filename)

    outfilename = options.outfilename
    GEBtype = options.GEBtype

    # Create the output File and TTree
    outF = r.TFile(filename + '/' + outfilename, 'recreate')
    if options.performFit:
        myT = r.TTree('scurveFitTree', 'Tree Holding FitData')

    tuple_calInfo = parseCalFile(options.calFile)
    calDAC2Q_Slope = tuple_calInfo[0]
    calDAC2Q_Intercept = tuple_calInfo[1]

    # Create output plot containers
    vSummaryPlots = ndict()
    vSummaryPlotsPanPin2 = ndict()
    vSummaryPlotsNoMaskedChan = ndict()
    vSummaryPlotsNoMaskedChanPanPin2 = ndict()
    vthr_list = getEmptyPerVFATList()
    trim_list = getEmptyPerVFATList()
    trimrange_list = getEmptyPerVFATList()

    # Set default histogram behavior
    r.TH1.SetDefaultSumw2(False)
    r.gROOT.SetBatch(True)
    r.gStyle.SetOptStat(1111111)

    # Initialize distributions
    for vfat in range(0, 24):
        vSummaryPlots[vfat] = r.TH2D(
Exemple #15
0
def compareLatResults(args):
    # Suppress all pop-ups from ROOT
    import ROOT as r
    r.gROOT.SetBatch(True)
    
    # Check Paths
    import os, sys
    from gempython.utils.wrappers import envCheck
    envCheck('DATA_PATH')
    envCheck('ELOG_PATH')
    elogPath = os.getenv('ELOG_PATH')

    # Get info from input file
    from gempython.gemplotting.utils.anautilities import getCyclicColor, getDirByAnaType, filePathExists, make3x8Canvas, parseListOfScanDatesFile
    parsedTuple = parseListOfScanDatesFile(args.filename,alphaLabels=args.alphaLabels)
    listChamberAndScanDate = parsedTuple[0]
    chamberName = listChamberAndScanDate[0][0]

    legPlot = r.TLegend(0.5,0.5,0.9,0.9)

    from gempython.utils.nesteddict import nesteddict as ndict
    dict_Histos = ndict()
    dict_Graphs = ndict()
    dict_MultiGraphs = {}
    
    print("Loading data from input list of scandates file")
    from gempython.gemplotting.utils.anaInfo import tree_names
    for idx,infoTuple in enumerate(listChamberAndScanDate):
        # Setup the path
        dirPath = getDirByAnaType("latency", infoTuple[0])
        if not filePathExists(dirPath, infoTuple[1]):
            print('Filepath {:s}/{:s} does not exist!'.format(dirPath, infoTuple[1]))
            print('Please cross-check, exiting!')
            sys.exit(os.EX_NOINPUT)
        filename = "{:s}/{:s}/{:s}".format(dirPath, infoTuple[1], tree_names["latencyAna"][0])
        if not os.path.isfile(filename):
            print('File {:s} does not exist!'.format(filename))
            print('Please cross-check, exiting!')
            sys.exit(os.EX_NOINPUT)

        # Load the file
        r.TH1.AddDirectory(False)
        scanFile   = r.TFile(filename,"READ")
        
        if scanFile.IsZombie():
            print("{:s} is a zombie!!!".format(filename))
            print("Please double check your input list of scandates: {:s}".format(args.filename))
            print("And then call this script again")
            raise IOError

        ###################
        # Get individual distributions
        ###################
        for vfat in range(24):
            baseDir = "VFAT_Plots/VFAT{0:d}".format(vfat)
            dict_Histos[infoTuple[2]][vfat] = scanFile.Get("{:s}/vfat{:d}HitsVsLat".format(baseDir,vfat))
            dict_Graphs[infoTuple[2]][vfat] = scanFile.Get("{:s}/g_N_vs_Lat_VFAT{:d}".format(baseDir,vfat))
            
            # Make the TMultiGraph Objects
            suffix = "VFAT{:d}".format(vfat)
            if idx == 0:
                dict_MultiGraphs[vfat] = r.TMultiGraph("mGraph_RateVsThrDac_vfat{:s}".format(suffix),suffix)

            # Set Style of TGraph
            dict_Graphs[infoTuple[2]][vfat].SetLineColor(getCyclicColor(idx))
            dict_Graphs[infoTuple[2]][vfat].SetMarkerColor(getCyclicColor(idx))
            dict_Graphs[infoTuple[2]][vfat].SetMarkerStyle(20+idx)
            dict_Graphs[infoTuple[2]][vfat].SetMarkerSize(0.8)

            # Add to the MultiGraph
            dict_MultiGraphs[vfat].Add(dict_Graphs[infoTuple[2]][vfat])

            ###################
            # Make an entry in the TLegend
            ###################
            if vfat == 0:
                print("infoTuple[2] = ", infoTuple[2])
                legPlot.AddEntry(
                        dict_Graphs[infoTuple[2]][vfat],
                        "{0}".format(infoTuple[2]),
                        "LPE")
                pass
            pass
    
    ###################
    # Make output ROOT file
    ###################
    outFileName = "{0}/compLatResults_{1}.root".format(elogPath,chamberName)
    outFile = r.TFile(outFileName,"RECREATE")

    ###################
    # Now Make plots
    ###################
    print("Making output plots")
    dict_canv = {}
    for vfat in range(24):
        # Make Output Canvas
        dict_canv[vfat] = r.TCanvas("canvLatResults_VFAT{0}".format(vfat),"Hits vs. CFG_LATENCY Value",700,700)
        dict_canv[vfat].cd()
        dict_MultiGraphs[vfat].Draw("APE1")
        dict_MultiGraphs[vfat].GetYaxis().SetTitle("Hits #left(A.U.#right)")
        dict_MultiGraphs[vfat].GetYaxis().SetRangeUser(0,args.eventsPerLat) # max is 40 MHz
        dict_MultiGraphs[vfat].GetXaxis().SetTitle("CFG_LATENCY #left(BX#right)")
        dict_MultiGraphs[vfat].Draw("APE1")

        # Draw Legend?
        if not args.noLeg:
            legPlot.Draw("same")
            pass

        # Make output image?
        if args.savePlots:
            dict_canv[vfat].SaveAs("{0}/{1}.png".format(elogPath,dict_canv[vfat].GetName()))
            pass

        # Store Output
        thisDirectory = outFile.mkdir("VFAT{0}".format(vfat))
        thisDirectory.cd()
        dict_MultiGraphs[vfat].Write()
        dict_canv[vfat].Write()
        pass

    # Make summary canvases, always save these
    canvLatResults_Summary = make3x8Canvas("canvLatResults_Summary",dict_MultiGraphs.values()[0:24],"APE1")
                
    # Draw Legend?
    if not args.noLeg:
        canvLatResults_Summary.cd(1)
        legPlot.Draw("same")
        pass

    # Save summary canvases (always)
    print("\nSaving Summary TCanvas Objects")
    #canvLatResults_Summary.SaveAs("{0}/{1}.png".format(elogPath,canvLatResults_Summary.GetName()))

    # Close output files
    outFile.Close()
    
    print("You can find all ROOT objects in:")
    print("\n\t{0}//compLatResults_{1}.root\n".format(elogPath,chamberName))

    print("You can find all plots in:")
    print("\n\t{0}\n".format(elogPath))

    print("Done")

    return
    myT.Branch( 'pedestal', pedestal, 'pedestal/F')
    ped_eff = array( 'f', [ 0 ] )
    myT.Branch( 'ped_eff', ped_eff, 'ped_eff/F')
    scurve_h = r.TH1D()
    myT.Branch( 'scurve_h', scurve_h)
    scurve_fit = r.TF1()
    myT.Branch( 'scurve_fit', scurve_fit)
    chi2 = array( 'f', [ 0 ] )
    myT.Branch( 'chi2', chi2, 'chi2/F')
    ndf = array( 'i', [ 0 ] )
    myT.Branch( 'ndf', ndf, 'ndf/I')
    Nhigh = array( 'i', [ 0 ] )
    myT.Branch( 'Nhigh', Nhigh, 'Nhigh/I')
    pass

vSummaryPlots = ndict()
vSummaryPlotsPanPin2 = ndict()
vSummaryPlotsPruned = ndict()
vSummaryPlotsPrunedPanPin2 = ndict()
vScurves = []
vScurveFits = []
vthr_list = []
trim_list = []
trimrange_list = []
lines = []

for vfat in range(0,24):
    vScurves.append([])
    vScurveFits.append([])
    vthr_list.append([])
    trim_list.append([])
Exemple #17
0
def compareSBitThreshResults(args):
    # Suppress all pop-ups from ROOT
    import ROOT as r
    r.gROOT.SetBatch(True)

    # Check Paths
    import os, sys
    from gempython.utils.wrappers import envCheck
    envCheck('DATA_PATH')
    envCheck('ELOG_PATH')
    elogPath = os.getenv('ELOG_PATH')

    # Get info from input file
    from gempython.gemplotting.utils.anautilities import getCyclicColor, getDirByAnaType, filePathExists, getSummaryCanvas, parseArmDacCalFile, parseListOfScanDatesFile
    parsedTuple = parseListOfScanDatesFile(args.filename,
                                           alphaLabels=args.alphaLabels)
    listChamberAndScanDate = parsedTuple[0]
    chamberName = listChamberAndScanDate[0][0]
    thrDacName = parsedTuple[1]

    gemType = "ge11"
    from gempython.tools.hw_constants import vfatsPerGemVariant

    # Parse calibration file if present
    arrayCalInfo = None
    if args.calFileARM is not None:
        arrayCalInfo = parseArmDacCalFile(
            args.calFileARM,
            gemType=gemType)  # arrayCalInfo[i][vfatN] for coef of x^(4-i)

    legPlot = r.TLegend(0.5, 0.5, 0.9, 0.9)

    from gempython.utils.nesteddict import nesteddict as ndict
    dict_Histos = ndict()
    dict_Graphs = ndict()
    dict_MultiGraphs = {}
    for idx, infoTuple in enumerate(listChamberAndScanDate):
        # Setup the path
        dirPath = getDirByAnaType("sbitRateor", infoTuple[0])
        if not filePathExists(dirPath, infoTuple[1]):
            print('Filepath {:s}/{:s} does not exist!'.format(
                dirPath, infoTuple[1]))
            print('Please cross-check, exiting!')
            sys.exit(os.EX_NOINPUT)
        filename = "{:s}/{:s}/SBitRatePlots.root".format(dirPath, infoTuple[1])

        # Load the file
        r.TH1.AddDirectory(False)
        scanFile = r.TFile(filename, "READ")

        if scanFile.IsZombie():
            print("{0} is a zombie!!!".format(filename))
            print(
                "Please double check your input list of scandates: {0}".format(
                    args.filename))
            print("And then call this script again")
            raise IOError

        ###################
        # Get individual distributions
        ###################
        for vfat in range(vfatsPerGemVariant[gemType]):
            dict_Histos[infoTuple[2]][vfat] = scanFile.Get(
                "VFAT{0}/THR_ARM_DAC/g1D_rate_vs_THR-ARM-DAC_vfat{0}".format(
                    vfat))
            dict_Graphs[infoTuple[2]][vfat] = r.TGraph(
                dict_Histos[infoTuple[2]][vfat])

            # Do we convert x-axis to charge units?
            if arrayCalInfo is not None:
                for pt in range(dict_Graphs[infoTuple[2]][vfat].GetN()):
                    valX = r.Double()
                    valY = r.Double()
                    dict_Graphs[infoTuple[2]][vfat].GetPoint(pt, valX, valY)
                    valX_Q = 0
                    for coef in range(0, 5):
                        valX_Q += arrayCalInfo[coef][vfat] * pow(
                            valX, 4 - coef)
                    dict_Graphs[infoTuple[2]][vfat].SetPoint(pt, valX_Q, valY)
                    pass
                pass

            # Make the TMultiGraph Objects
            suffix = "VFAT{0}".format(vfat)
            if idx == 0:
                dict_MultiGraphs[vfat] = r.TMultiGraph(
                    "mGraph_RateVsThrDac_vfat{0}".format(suffix), suffix)

            # Set Style of TGraph
            dict_Graphs[infoTuple[2]][vfat].SetLineColor(getCyclicColor(idx))
            dict_Graphs[infoTuple[2]][vfat].SetMarkerColor(getCyclicColor(idx))
            dict_Graphs[infoTuple[2]][vfat].SetMarkerStyle(20 + idx)
            dict_Graphs[infoTuple[2]][vfat].SetMarkerSize(0.8)

            # Add to the MultiGraph
            dict_MultiGraphs[vfat].Add(dict_Graphs[infoTuple[2]][vfat])

            ###################
            # Make an entry in the TLegend
            ###################
            if vfat == 0:
                legPlot.AddEntry(dict_Graphs[infoTuple[2]][vfat],
                                 "{0} = {1}".format(thrDacName,
                                                    infoTuple[2]), "LPE")
                pass
            pass

    ###################
    # Make output ROOT file
    ###################
    outFileName = "{0}/compSbitThresh_{1}.root".format(elogPath, chamberName)
    outFile = r.TFile(outFileName, "RECREATE")

    # Plot Containers
    dict_canv = {}

    ###################
    # Now Make plots
    ###################

    for vfat in range(vfatsPerGemVariant[gemType]):
        # Make Output Canvas
        dict_canv[vfat] = r.TCanvas("canvSBitRate_VFAT{0}".format(vfat),
                                    "SBIT Rate by THR DAC", 700, 700)
        dict_canv[vfat].cd()
        dict_canv[vfat].cd().SetLogy()
        dict_MultiGraphs[vfat].Draw("APE1")
        dict_MultiGraphs[vfat].GetYaxis().SetTitle("Rate #left(Hz#right)")
        dict_MultiGraphs[vfat].GetYaxis().SetRangeUser(1e-1,
                                                       5e8)  # max is 40 MHz
        if arrayCalInfo is not None:
            dict_MultiGraphs[vfat].GetXaxis().SetTitle(
                "Threshold #left(fC#right)")
            dict_MultiGraphs[vfat].GetXaxis().SetRangeUser(0, 20)
        else:
            dict_MultiGraphs[vfat].GetXaxis().SetTitle(
                "CFG_THR_ARM_DAC #left(DAC#right)")
            dict_MultiGraphs[vfat].GetXaxis().SetRangeUser(0, 125)
            pass
        dict_MultiGraphs[vfat].Draw("APE1")

        # Draw Legend?
        if not args.noLeg:
            legPlot.Draw("same")
            pass

        # Make output image?
        if args.savePlots:
            dict_canv[vfat].SaveAs("{0}/{1}.png".format(
                elogPath, dict_canv[vfat].GetName()))
            pass

        # Store Output
        thisDirectory = outFile.mkdir("VFAT{0}".format(vfat))
        thisDirectory.cd()
        dict_MultiGraphs[vfat].Write()
        dict_canv[vfat].Write()
        pass

    # Make summary canvases, always save these
    canvSBitRate_Summary = getSummaryCanvas(
        dict_MultiGraphs.values()[0:vfatsPerGemVariant[gemType]],
        name="canvSBitRate_Summary",
        drawOpt="APE1",
        gemType=gemType)
    for vfatCanv in range(1, vfatsPerGemVariant[gemType] + 1):
        canvSBitRate_Summary.cd(vfatCanv).SetLogy()

    # Draw Legend?
    if not args.noLeg:
        canvSBitRate_Summary.cd(1)
        legPlot.Draw("same")
        pass

    # Save summary canvases (always)
    print("\nSaving Summary TCanvas Objects")
    canvSBitRate_Summary.SaveAs("{0}/{1}.png".format(
        elogPath, canvSBitRate_Summary.GetName()))

    # Close output files
    outFile.Close()

    print("You can find all ROOT objects in:")
    print("\n\t{0}/compSbitThresh_{1}.root\n".format(elogPath, chamberName))

    print("You can find all plots in:")
    print("\n\t{0}\n".format(elogPath))

    print("Done")

    return
Exemple #18
0
    r.gROOT.SetBatch(True)
    r.gStyle.SetOptStat(1111111)

    # Loading the dictionary with the mapping
    from gempython.gemplotting.utils.anautilities import getSummaryCanvas, getSummaryCanvasByiEta, getMapping
    vfat_ch_strips = getMapping(mapping, isVFAT2=False, gemType=gemType)

    if args.debug:
        print("\nVFAT channels to strips \n"+mapping+"\nMAP loaded")

    # Loading and reversing the dictionary with (eta , phi) <-> vfatN
    from gempython.gemplotting.mapping.chamberInfo import chamber_iEta2VFATPos, chamber_maxiEtaiPhiPair
    from gempython.utils.nesteddict import nesteddict as ndict
    maxiEta = chamber_maxiEtaiPhiPair[gemType][0]
    maxiPhi = chamber_maxiEtaiPhiPair[gemType][1]
    etaphi_to_vfat = ndict()
    for i in range(maxiEta):
        etaphi_to_vfat[i+1] = {row:ieta for ieta,row in chamber_iEta2VFATPos[i+1].iteritems()}

    """
    Now it's time to load all the input files and to merge them into one root TTree
    The TTree file it's going to auto extend each time a new file is found
    A TFile it's going to hold this TTree
    """
    # Creating the output File and TTree
    outF = r.TFile(filename+'/'+outfilename, 'recreate')
    inT = r.TTree('Packed', 'Tree Holding packed raw data')

    # get starting time
    import time
    start_time = time.time()
(options, args) = parser.parse_args()
filename = options.filename[:-5]
os.system("mkdir " + filename)

print filename
outputfilename = options.outfilename

import ROOT as r
r.TH1.SetDefaultSumw2(False)
r.gROOT.SetBatch(True)
r.gStyle.SetOptStat(1111111)
inF = r.TFile(filename + '.root', "READ")

#Initializing Histograms
print 'Initializing Histograms'
dict_hVFATHitsVsLat = ndict()
for vfat in range(0, 24):
    #dict_hVFATHitsVsLat[vfat]   = r.TH1F("vfat%iHitsVsLat"%vfat,"vfat%i"%vfat,256,-0.5,255.5)
    dict_hVFATHitsVsLat[vfat] = r.TH1F("vfat%iHitsVsLat" % vfat,
                                       "vfat%i" % vfat, 1024, -0.5, 1023.5)
    pass

#Filling Histograms
print 'Filling Histograms'
latMin = 1000
latMax = -1
nTrig = -1
dict_vfatID = dict((vfat, 0) for vfat in range(0, 24))
listOfBranches = inF.latTree.GetListOfBranches()
for event in inF.latTree:
    dict_hVFATHitsVsLat[int(event.vfatN)].Fill(event.latency, event.Nhits)
(options, args) = parser.parse_args()
filename = options.filename[:-5]
os.system("mkdir " + filename)

print filename
outputfilename = options.outfilename

import ROOT as r
r.gROOT.SetBatch(True)
r.gStyle.SetOptStat(1111111)
inF = r.TFile(filename + '.root', "READ")

#Initializing Histograms
print 'Initializing Histograms'
dict_hVFATHitsVsLat = ndict()
for vfat in range(0, 24):
    dict_hVFATHitsVsLat[vfat] = r.TH1F("vfat%iHitsVsLat" % vfat,
                                       "vfat%i" % vfat, 256, -0.5, 255.5)
    pass

#Filling Histograms
print 'Filling Histograms'
latMin = 1000
latMax = -1
for event in inF.latTree:
    dict_hVFATHitsVsLat[int(event.vfatN)].Fill(event.lat, event.Nhits)
    if event.lat < latMin and event.Nhits > 0:
        latMin = event.lat
        pass
    elif event.lat > latMax:
Exemple #21
0
parser.add_option("-x",
                  "--chi2",
                  action="store_true",
                  dest="chi2_plots",
                  help="Make Chi2 plots",
                  metavar="chi2_plots")

(options, args) = parser.parse_args()
filename = options.filename[:-5]

import ROOT as r

r.gROOT.SetBatch(True)
inF = r.TFile(filename + '.root')

vSum = ndict()
vNoise = ndict()
vThreshold = ndict()
vChi2 = ndict()
vComparison = ndict()
vNoiseTrim = ndict()
vPedestal = ndict()

for vfat in range(0, 24):
    vNoise[vfat] = r.TH1D('Noise%i' % vfat, 'Noise%i;Noise [DAC units]' % vfat,
                          35, -0.5, 34.5)
    vPedestal[vfat] = r.TH1D('Pedestal%i' % vfat,
                             'Pedestal%i;Pedestal [DAC units]' % vfat, 256,
                             -0.5, 255.5)
    vThreshold[vfat] = r.TH1D('Threshold%i' % vfat,
                              'Threshold%i;Threshold [DAC units]' % vfat, 60,
Exemple #22
0
def anaUltraLatency(infilename,
                    debug=False,
                    latSigMaskRange=None,
                    latSigRange=None,
                    outputDir=None,
                    outfilename="latencyAna.root",
                    performFit=False):
    """
    Analyzes data taken by ultraLatency.py

    infilename      - Name of input TFile containing the latTree TTree
    debug           - If True prints additional debugging statements
    latSigMaskRange - Comma separated pair of values defining the region 
                      to be masked when trying to fit the noise, e.g. 
                      lat #notepsilon [40,44] is noise (lat < 40 || lat > 44)")
    latSigRange     - Comma separated pair of values defining expected 
                      signal range, e.g. lat #epsilon [41,43] is signal")
    outfilename  - Name of output TFile containing analysis results
    performFit      - Fit the latency distributions    
    """

    # Determine output filepath
    if outputDir is None:
        from gempython.gemplotting.utils.anautilities import getElogPath
        outputDir = getElogPath()
        pass

    # Redirect sys.stdout and sys.stderr if necessary
    from gempython.gemplotting.utils.multiprocUtils import redirectStdOutAndErr
    redirectStdOutAndErr("anaUltraLatency", outputDir)

    # Create the output File and TTree
    import ROOT as r
    outF = r.TFile(outputDir + "/" + outfilename, "RECREATE")
    if not outF.IsOpen():
        outF.Close()
        raise IOError(
            "Unable to open output file {1} check to make sure you have write permissions under {0}"
            .format(outputDir, outfilename))
    if outF.IsZombie():
        outF.Close()
        raise IOError(
            "Output file {1} is a Zombie, check to make sure you have write permissions under {0}"
            .format(outputDir, outfilename))
    myT = r.TTree('latFitTree', 'Tree Holding FitData')

    # Attempt to open input TFile
    inFile = r.TFile(infilename, "read")
    if not inFile.IsOpen():
        outF.Close()
        inFile.Close()
        raise IOError(
            "Unable to open input file {0} check to make sure you have read permissions"
            .format(infilename))
    if inFile.IsZombie():
        outF.Close()
        inFile.Close()
        raise IOError(
            "Input file {0} is a Zombie, check to make sure you have write permissions and file has expected size"
            .format(infilename))

    from gempython.tools.hw_constants import vfatsPerGemVariant
    # Get ChipID's
    import numpy as np
    import root_numpy as rp

    ##### FIXME
    from gempython.gemplotting.mapping.chamberInfo import gemTypeMapping
    if 'gemType' not in inFile.latTree.GetListOfBranches():
        gemType = "ge11"
    else:
        gemType = gemTypeMapping[rp.tree2array(tree=inFile.latTree,
                                               branches=['gemType'])[0][0]]
    print gemType
    ##### END
    from gempython.tools.hw_constants import vfatsPerGemVariant
    nVFATS = vfatsPerGemVariant[gemType]
    from gempython.gemplotting.mapping.chamberInfo import CHANNELS_PER_VFAT as maxChans

    listOfBranches = inFile.latTree.GetListOfBranches()
    if 'vfatID' in listOfBranches:
        array_chipID = np.unique(
            rp.tree2array(inFile.latTree, branches=['vfatID', 'vfatN']))
        dict_chipID = {}
        for entry in array_chipID:
            dict_chipID[entry['vfatN']] = entry['vfatID']
    else:
        dict_chipID = {vfat: 0 for vfat in range(nVFATS)}

    if debug:
        print("VFAT Position to ChipID Mapping")
        for vfat, vfatID in dict_chipID.iteritems():
            print(vfat, vfatID)

    # Set default histogram behavior
    r.TH1.SetDefaultSumw2(False)
    r.gROOT.SetBatch(True)
    r.gStyle.SetOptStat(1111111)

    #Initializing Histograms
    print('Initializing Histograms')
    from gempython.utils.gemlogger import printYellow
    from gempython.utils.nesteddict import nesteddict as ndict
    dict_hVFATHitsVsLat = ndict()
    for vfat in range(0, nVFATS):
        try:
            chipID = dict_chipID[vfat]
        except KeyError as err:
            chipID = 0
            if debug:
                printYellow(
                    "No CHIP_ID for VFAT{0}, If you don't expect data from this VFAT there's no problem"
                    .format(vfat))

        dict_hVFATHitsVsLat[vfat] = r.TH1F(
            "vfat{0}HitsVsLat".format(vfat),
            "VFAT {0}: chipID {1}".format(vfat, chipID), 1024, -0.5, 1023.5)
        pass

    #Filling Histograms
    print('Filling Histograms')
    latMin = 1000
    latMax = -1
    nTrig = -1
    for event in inFile.latTree:
        dict_hVFATHitsVsLat[int(event.vfatN)].Fill(event.latency, event.Nhits)
        if event.latency < latMin and event.Nhits > 0:
            latMin = event.latency
            pass
        elif event.latency > latMax:
            latMax = event.latency
            pass

        if nTrig < 0:
            nTrig = event.Nev
            pass
        pass

    from math import sqrt
    for vfat in range(0, nVFATS):
        for binX in range(1, dict_hVFATHitsVsLat[vfat].GetNbinsX() + 1):
            dict_hVFATHitsVsLat[vfat].SetBinError(
                binX, sqrt(dict_hVFATHitsVsLat[vfat].GetBinContent(binX)))

    hHitsVsLat_AllVFATs = dict_hVFATHitsVsLat[0].Clone("hHitsVsLat_AllVFATs")
    hHitsVsLat_AllVFATs.SetTitle("Sum over all VFATs")
    for vfat in range(1, nVFATS):
        hHitsVsLat_AllVFATs.Add(dict_hVFATHitsVsLat[vfat])

    # Set Latency Fitting Bounds - Signal
    latFitMin_Sig = latMin
    latFitMax_Sig = latMax
    if latSigRange is not None:
        listLatValues = map(lambda val: float(val), latSigRange.split(","))
        if len(listLatValues) != 2:
            raise IndexError(
                "You must specify exactly two values for determining latency signal range; values given: {0} do not meet this criterion"
                .format(listLatValues))
        else:
            latFitMin_Sig = min(listLatValues)
            latFitMax_Sig = max(listLatValues)

    # Set Latency Fitting Bounds - Noise
    latFitMin_Noise = latFitMin_Sig - 1
    latFitMax_Noise = latFitMax_Sig + 1
    if latSigMaskRange is not None:
        listLatValues = map(lambda val: float(val), latSigMaskRange.split(","))
        if len(listLatValues) != 2:
            raise IndexError(
                "You must specify exactly two values for determining latency signal range; values given: {0} do not meet this criterion"
                .format(listLatValues))
        else:
            latFitMin_Noise = min(listLatValues)
            latFitMax_Noise = max(listLatValues)

    # Make output TFile and TTree
    from array import array
    dirVFATPlots = outF.mkdir("VFAT_Plots")
    if 'detName' in listOfBranches:
        detName = r.vector('string')()
        detName.push_back(
            rp.tree2array(inFile.latTree, branches=['detName'])[0][0][0])
        myT.Branch('detName', detName)
    vfatN = array('i', [0])
    myT.Branch('vfatN', vfatN, 'vfatN/I')
    vfatID = array('L', [0])
    myT.Branch('vfatID', vfatID, 'vfatID/i')  #Hex Chip ID of VFAT
    hitCountMaxLat = array('f', [0])
    myT.Branch('hitCountMaxLat', hitCountMaxLat, 'hitCountMaxLat/F')
    hitCountMaxLatErr = array('f', [0])
    myT.Branch('hitCountMaxLatErr', hitCountMaxLatErr, 'hitCountMaxLatErr/F')
    maxLatBin = array('f', [0])
    myT.Branch('maxLatBin', maxLatBin, 'maxLatBin/F')
    hitCountBkg = array('f', [0])
    hitCountBkgErr = array('f', [0])
    hitCountSig = array('f', [0])
    hitCountSigErr = array('f', [0])
    SigOverBkg = array('f', [0])
    SigOverBkgErr = array('f', [0])
    if performFit:
        myT.Branch('hitCountBkg', hitCountBkg, 'hitCountBkg/F')
        myT.Branch('hitCountBkgErr', hitCountBkgErr, 'hitCountBkgErr/F')
        myT.Branch('hitCountSig', hitCountSig, 'hitCountSig/F')
        myT.Branch('hitCountSigErr', hitCountSigErr, 'hitCountSigErr/F')
        myT.Branch('SigOverBkg', SigOverBkg, 'SigOverBkg/F')
        myT.Branch('SigOverBkgErr', SigOverBkgErr, 'SigOverBkgErr/F')

    # Make output plots
    from math import sqrt
    dict_grNHitsVFAT = ndict()
    dict_fitNHitsVFAT_Sig = ndict()
    dict_fitNHitsVFAT_Noise = ndict()
    grNMaxLatBinByVFAT = r.TGraphErrors(len(dict_hVFATHitsVsLat))
    grMaxLatBinByVFAT = r.TGraphErrors(len(dict_hVFATHitsVsLat))
    grVFATSigOverBkg = r.TGraphErrors(len(dict_hVFATHitsVsLat))
    grVFATNSignalNoBkg = r.TGraphErrors(len(dict_hVFATHitsVsLat))
    r.gStyle.SetOptStat(0)
    if debug and performFit:
        print("VFAT\tSignalHits\tSignal/Noise")

    for vfat in dict_hVFATHitsVsLat:
        #if we don't have any data for this VFAT, we just need to initialize the TGraphAsymmErrors since it is drawn later
        if vfat not in dict_chipID:
            dict_grNHitsVFAT[vfat] = r.TGraphAsymmErrors()
            continue

        # Store VFAT info
        vfatN[0] = vfat
        vfatID[0] = dict_chipID[vfat]

        # Store Max Info
        hitCountMaxLat[0] = dict_hVFATHitsVsLat[vfat].GetBinContent(
            dict_hVFATHitsVsLat[vfat].GetMaximumBin())
        hitCountMaxLatErr[0] = sqrt(hitCountMaxLat[0])
        grNMaxLatBinByVFAT.SetPoint(vfat, vfat, hitCountMaxLat[0])
        grNMaxLatBinByVFAT.SetPointError(vfat, 0, hitCountMaxLatErr[0])

        maxLatBin[0] = dict_hVFATHitsVsLat[vfat].GetBinCenter(
            dict_hVFATHitsVsLat[vfat].GetMaximumBin())
        grMaxLatBinByVFAT.SetPoint(vfat, vfat, maxLatBin[0])
        grMaxLatBinByVFAT.SetPointError(vfat, 0, 0.5)  #could be improved upon

        # Initialize
        dict_fitNHitsVFAT_Sig[vfat] = r.TF1(
            "func_N_vs_Lat_VFAT{0}_Sig".format(vfat), "[0]", latFitMin_Sig,
            latFitMax_Sig)
        dict_fitNHitsVFAT_Noise[vfat] = r.TF1(
            "func_N_vs_Lat_VFAT{0}_Noise".format(vfat), "[0]", latMin, latMax)
        dict_grNHitsVFAT[vfat] = r.TGraphAsymmErrors(dict_hVFATHitsVsLat[vfat])
        dict_grNHitsVFAT[vfat].SetName("g_N_vs_Lat_VFAT{0}".format(vfat))

        # Fitting
        if performFit:
            # Fit Signal
            dict_fitNHitsVFAT_Sig[vfat].SetParameter(0, hitCountMaxLat[0])
            dict_fitNHitsVFAT_Sig[vfat].SetLineColor(r.kGreen + 1)
            dict_grNHitsVFAT[vfat].Fit(dict_fitNHitsVFAT_Sig[vfat], "QR")

            # Remove Signal Region
            latVal = r.Double()
            hitVal = r.Double()
            gTempDist = dict_grNHitsVFAT[vfat].Clone(
                "g_N_vs_Lat_VFAT{0}_NoSig".format(vfat))
            for idx in range(dict_grNHitsVFAT[vfat].GetN() - 1, 0, -1):
                gTempDist.GetPoint(idx, latVal, hitVal)
                if latFitMin_Noise < latVal and latVal < latFitMax_Noise:
                    gTempDist.RemovePoint(idx)

            # Fit Noise
            dict_fitNHitsVFAT_Noise[vfat].SetParameter(0, 0.)
            dict_fitNHitsVFAT_Noise[vfat].SetLineColor(r.kRed + 1)
            gTempDist.Fit(dict_fitNHitsVFAT_Noise[vfat], "QR")

            # Calc Signal & Signal/Noise
            hitCountBkg[0] = dict_fitNHitsVFAT_Noise[vfat].GetParameter(0)
            hitCountBkgErr[0] = dict_fitNHitsVFAT_Noise[vfat].GetParError(0)
            hitCountSig[0] = dict_fitNHitsVFAT_Sig[vfat].GetParameter(
                0) - hitCountBkg[0]
            hitCountSigErr[0] = sqrt(
                (dict_fitNHitsVFAT_Sig[vfat].GetParError(0))**2 +
                hitCountBkgErr[0]**2)

            SigOverBkg[0] = hitCountSig[0] / hitCountBkg[0]
            SigOverBkgErr[0] = sqrt((hitCountSigErr[0] / hitCountBkg[0])**2 +
                                    (hitCountBkgErr[0]**2 *
                                     (hitCountSig[0] / hitCountBkg[0]**2)**2))

            # Add to Plot
            grVFATSigOverBkg.SetPoint(vfat, vfat, SigOverBkg[0])
            grVFATSigOverBkg.SetPointError(vfat, 0, SigOverBkgErr[0])

            grVFATNSignalNoBkg.SetPoint(vfat, vfat, hitCountSig[0])
            grVFATNSignalNoBkg.SetPointError(vfat, 0, hitCountSigErr[0])

            # Print if requested
            if debug:
                print("{0}\t{1}\t{2}".format(vfat, hitCountSig[0],
                                             SigOverBkg[0]))
            pass

        # Format
        r.gStyle.SetOptStat(0)
        dict_grNHitsVFAT[vfat].SetMarkerStyle(21)
        dict_grNHitsVFAT[vfat].SetMarkerSize(0.7)
        dict_grNHitsVFAT[vfat].SetLineWidth(2)
        dict_grNHitsVFAT[vfat].GetXaxis().SetRangeUser(latMin, latMax)
        dict_grNHitsVFAT[vfat].GetXaxis().SetTitle("Lat")
        dict_grNHitsVFAT[vfat].GetYaxis().SetRangeUser(0, nTrig)
        dict_grNHitsVFAT[vfat].GetYaxis().SetTitle("N")

        # Write
        dirVFAT = dirVFATPlots.mkdir("VFAT{0}".format(vfat))
        dirVFAT.cd()
        dict_grNHitsVFAT[vfat].Write()
        dict_hVFATHitsVsLat[vfat].Write()
        if performFit:
            dict_fitNHitsVFAT_Sig[vfat].Write()
            dict_fitNHitsVFAT_Noise[vfat].Write()
        myT.Fill()
        pass

    # Store - Summary
    from gempython.gemplotting.utils.anautilities import getSummaryCanvas, addPlotToCanvas
    if performFit:
        canv_Summary = getSummaryCanvas(dict_grNHitsVFAT,
                                        name='canv_Summary',
                                        drawOpt='APE1',
                                        gemType=gemType)
        canv_Summary = addPlotToCanvas(canv_Summary, dict_fitNHitsVFAT_Noise,
                                       gemType)
        canv_Summary.SaveAs(outputDir + '/Summary.png')
    else:
        canv_Summary = getSummaryCanvas(dict_grNHitsVFAT,
                                        name='canv_Summary',
                                        drawOpt='APE1',
                                        gemType=gemType)
        canv_Summary.SaveAs(outputDir + '/Summary.png')

    # Store - Sig Over Bkg
    if performFit:
        canv_SigOverBkg = r.TCanvas("canv_SigOverBkg", "canv_SigOverBkg", 600,
                                    600)
        canv_SigOverBkg.cd()
        canv_SigOverBkg.cd().SetLogy()
        canv_SigOverBkg.cd().SetGridy()
        grVFATSigOverBkg.SetTitle("")
        grVFATSigOverBkg.SetMarkerStyle(21)
        grVFATSigOverBkg.SetMarkerSize(0.7)
        grVFATSigOverBkg.SetLineWidth(2)
        grVFATSigOverBkg.GetXaxis().SetTitle("VFAT Pos")
        grVFATSigOverBkg.GetYaxis().SetTitle("Sig / Bkg)")
        grVFATSigOverBkg.GetYaxis().SetTitleOffset(1.25)
        grVFATSigOverBkg.GetYaxis().SetRangeUser(1e-1, 1e2)
        grVFATSigOverBkg.GetXaxis().SetRangeUser(-0.5, nVFATS + 0.5)
        grVFATSigOverBkg.Draw("APE1")
        canv_SigOverBkg.SaveAs(outputDir + '/SignalOverBkg.png')

    # Store - Signal
    if performFit:
        canv_Signal = r.TCanvas("canv_Signal", "canv_Signal", 600, 600)
        canv_Signal.cd()
        grVFATNSignalNoBkg.SetTitle("")
        grVFATNSignalNoBkg.SetMarkerStyle(21)
        grVFATNSignalNoBkg.SetMarkerSize(0.7)
        grVFATNSignalNoBkg.SetLineWidth(2)
        grVFATNSignalNoBkg.GetXaxis().SetTitle("VFAT Pos")
        grVFATNSignalNoBkg.GetYaxis().SetTitle("Signal Hits")
        grVFATNSignalNoBkg.GetYaxis().SetTitleOffset(1.5)
        grVFATNSignalNoBkg.GetYaxis().SetRangeUser(0, nTrig)
        grVFATNSignalNoBkg.GetXaxis().SetRangeUser(-0.5, nVFATS + 0.5)
        grVFATNSignalNoBkg.Draw("APE1")
        canv_Signal.SaveAs(outputDir + '/SignalNoBkg.png')

    # Store - Sum over all VFATs
    canv_LatSum = r.TCanvas("canv_LatSumOverAllVFATs",
                            "canv_LatSumOverAllVFATs", 600, 600)
    canv_LatSum.cd()
    hHitsVsLat_AllVFATs.SetXTitle("Latency")
    hHitsVsLat_AllVFATs.SetYTitle("N")
    hHitsVsLat_AllVFATs.GetXaxis().SetRangeUser(latMin, latMax)
    hHitsVsLat_AllVFATs.Draw("hist")
    canv_LatSum.SaveAs(outputDir + '/LatSumOverAllVFATs.png')

    # Store - Max Hits By Lat Per VFAT
    canv_MaxHitsPerLatByVFAT = r.TCanvas("canv_MaxHitsPerLatByVFAT",
                                         "canv_MaxHitsPerLatByVFAT", 1200, 600)
    canv_MaxHitsPerLatByVFAT.Divide(2, 1)
    canv_MaxHitsPerLatByVFAT.cd(1)
    grNMaxLatBinByVFAT.SetTitle("")
    grNMaxLatBinByVFAT.SetMarkerStyle(21)
    grNMaxLatBinByVFAT.SetMarkerSize(0.7)
    grNMaxLatBinByVFAT.SetLineWidth(2)
    grNMaxLatBinByVFAT.GetXaxis().SetRangeUser(-0.5, nVFATS + 0.5)
    grNMaxLatBinByVFAT.GetXaxis().SetTitle("VFAT Pos")
    grNMaxLatBinByVFAT.GetYaxis().SetRangeUser(0, nTrig)
    grNMaxLatBinByVFAT.GetYaxis().SetTitle("Hit Count of Max Lat Bin")
    grNMaxLatBinByVFAT.GetYaxis().SetTitleOffset(1.7)
    grNMaxLatBinByVFAT.Draw("APE1")
    canv_MaxHitsPerLatByVFAT.cd(2)
    grMaxLatBinByVFAT.SetTitle("")
    grMaxLatBinByVFAT.SetMarkerStyle(21)
    grMaxLatBinByVFAT.SetMarkerSize(0.7)
    grMaxLatBinByVFAT.SetLineWidth(2)
    grMaxLatBinByVFAT.GetXaxis().SetTitle("VFAT Pos")
    grMaxLatBinByVFAT.GetYaxis().SetTitle("Max Lat Bin")
    grMaxLatBinByVFAT.GetYaxis().SetTitleOffset(1.2)
    grMaxLatBinByVFAT.GetXaxis().SetRangeUser(-0.5, nVFATS + 0.5)
    grMaxLatBinByVFAT.Draw("APE1")
    canv_MaxHitsPerLatByVFAT.SaveAs(outputDir + '/MaxHitsPerLatByVFAT.png')

    # Store - TObjects
    outF.cd()
    hHitsVsLat_AllVFATs.Write()
    grNMaxLatBinByVFAT.SetName("grNMaxLatBinByVFAT")
    grNMaxLatBinByVFAT.Write()
    grMaxLatBinByVFAT.SetName("grMaxLatBinByVFAT")
    grMaxLatBinByVFAT.Write()
    if performFit:
        grVFATSigOverBkg.SetName("grVFATSigOverBkg")
        grVFATSigOverBkg.Write()
        grVFATNSignalNoBkg.SetName("grVFATNSignalNoBkg")
        grVFATNSignalNoBkg.Write()
    myT.Write()
    outF.Close()
Exemple #23
0
def dacScanAllLinks(args, calTree, vfatBoard):
    """
    Performs a DAC scan on all VFATs on all unmasked OH's on amcBoard

    args - parsed arguments from an ArgumentParser instance
    calTree - instance of gemDacCalTreeStructure
    vfatBoard - instance of HwVFAT
    """

    # Get the AMC
    amcBoard = vfatBoard.parentOH.parentAMC
    nVFATs = vfatBoard.parentOH.nVFATs

    # Get DAC value
    dacSelect = args.dacSelect
    dacMax = maxVfat3DACSize[dacSelect][0]
    dacMin = 0
    calTree.nameX[0] = maxVfat3DACSize[dacSelect][1]
    calTree.dacSelect[0] = dacSelect

    # Get VFAT register values
    from gempython.utils.nesteddict import nesteddict as ndict
    ohVFATMaskArray = amcBoard.getMultiLinkVFATMask(args.ohMask)
    print("Getting CHIP IDs of all VFATs")
    vfatIDvals = ndict()
    irefVals = ndict()
    calSelPolVals = ndict()
    for ohN in range(0, amcBoard.nOHs):
        # Skip masked OH's
        if (not ((args.ohMask >> ohN) & 0x1)):
            calSelPolVals[ohN] = [0 for vfat in range(0, nVFATs)]
            irefVals[ohN] = [0 for vfat in range(0, nVFATs)]
            vfatIDvals[ohN] = [0 for vfat in range(0, nVFATs)]
        else:
            # update the OH in question
            vfatBoard.parentOH.link = ohN

            # Get the cal sel polarity
            calSelPolVals[ohN] = vfatBoard.readAllVFATs(
                "CFG_CAL_SEL_POL", ohVFATMaskArray[ohN])

            # Get the IREF values
            irefVals[ohN] = vfatBoard.readAllVFATs("CFG_IREF",
                                                   ohVFATMaskArray[ohN])

            # Get the chip ID's
            vfatIDvals[ohN] = vfatBoard.getAllChipIDs(ohVFATMaskArray[ohN])

    # Perform DAC Scan
    arraySize = amcBoard.nOHs * (dacMax - dacMin + 1) * nVFATs / args.stepSize
    scanData = (c_uint32 * arraySize)()
    print("Scanning DAC: {0} on all links".format(
        maxVfat3DACSize[dacSelect][1]))
    rpcResp = amcBoard.performDacScanMultiLink(scanData, dacSelect,
                                               args.stepSize, args.ohMask,
                                               args.extRefADC)
    if rpcResp != 0:
        raise Exception(
            'RPC response was non-zero, this inidcates an RPC exception occurred. DAC Scan of all links failed'
        )

    #try:
    if args.debug:
        print(
            "| detName | link | vfatN | vfatID | dacSelect | nameX | dacValX | dacValX_Err | nameY | dacValY | dacValY_Err |"
        )
        print(
            "| :-----: | :--: | :---: | :----: | :-------: |:-----: | :-----: | :---------: | :--: | :-----: | :---------: |"
        )
    for dacWord in scanData:
        # Get OH and skip if not in args.ohMask
        ohN = ((dacWord >> 23) & 0xf)
        if (not ((args.ohMask >> ohN) & 0x1)):
            continue

        # Get VFAT and skip if in ohVFATMaskArray[ohN]
        vfat = ((dacWord >> 18) & 0x1f)
        if ((ohVFATMaskArray[ohN] >> vfat) & 0x1):
            continue

        calTree.fill(
            calSelPol=calSelPolVals[ohN][vfat],
            dacValX=(dacWord & 0xff),
            dacValY=((dacWord >> 8) & 0x3ff),
            dacValY_Err=
            1,  # convert to physical units in analysis, LSB is the error on Y
            iref=irefVals[ohN][vfat],
            detName=chamber_config[(amcBoard.getShelf(), amcBoard.getSlot(),
                                    ohN)],
            link=ohN,
            shelf=amcBoard.getShelf(),
            slot=amcBoard.getSlot(),
            vfatID=vfatIDvals[ohN][vfat],
            vfatN=vfat)
        if args.debug:
            print(
                "| {0} | {1} | {2} | 0x{3:x} | {4} | {5} | {6} | {7} | {8} | {9} | {10} |"
                .format(calTree.detName[0], calTree.link[0], calTree.vfatN[0],
                        calTree.vfatID[0], calTree.dacSelect[0],
                        calTree.nameX[0], calTree.dacValX[0],
                        calTree.dacValX_Err[0], calTree.nameY[0],
                        calTree.dacValY[0], calTree.dacValY_Err[0]))
        pass

    printGreen("DAC scans for optohybrids in 0x{0:x} completed".format(
        args.ohMask))

    return
    def __init__(self,
                 calDAC2Q_m=None,
                 calDAC2Q_b=None,
                 isVFAT3=False,
                 nVFATS=24,
                 maxChi2=maxChi2Default):
        super(ScanDataFitter, self).__init__(nVFATS)

        from gempython.utils.nesteddict import nesteddict as ndict
        from gempython.gemplotting.mapping.chamberInfo import CHANNELS_PER_VFAT as maxChans
        r.gStyle.SetOptStat(0)

        self.Nev = ndict()
        self.scanFuncs = ndict()
        self.scanHistos = ndict()
        self.scanHistosChargeBins = ndict()
        self.scanCount = ndict()
        self.scanFitResults = ndict()

        self.isVFAT3 = isVFAT3
        self.nVFATS = nVFATS

        self.maxChi2 = maxChi2

        self.calDAC2Q_m = np.ones(self.nVFATS)
        if calDAC2Q_m is not None:
            self.calDAC2Q_m = calDAC2Q_m

        self.calDAC2Q_b = np.zeros(self.nVFATS)
        if calDAC2Q_b is not None:
            self.calDAC2Q_b = calDAC2Q_b

        for vfat in range(0, self.nVFATS):
            self.scanFitResults[0][vfat] = np.zeros(maxChans)
            self.scanFitResults[1][vfat] = np.zeros(maxChans)
            self.scanFitResults[2][vfat] = np.zeros(maxChans)
            self.scanFitResults[3][vfat] = np.zeros(maxChans)
            self.scanFitResults[4][vfat] = np.zeros(maxChans)
            self.scanFitResults[5][vfat] = np.zeros(maxChans)
            self.scanFitResults[6][vfat] = np.zeros(maxChans, dtype=bool)
            for ch in range(0, maxChans):
                self.scanCount[vfat][ch] = 0
                if self.isVFAT3:
                    self.scanFuncs[vfat][ch] = r.TF1(
                        'scurveFit_vfat{0}_chan{1}'.format(vfat, ch),
                        '[3]*TMath::Erf((TMath::Max([2],x)-[0])/(TMath::Sqrt(2)*[1]))+[3]',
                        self.calDAC2Q_m[vfat] * 253 + self.calDAC2Q_b[vfat],
                        self.calDAC2Q_m[vfat] * 1 + self.calDAC2Q_b[vfat])
                    self.scanHistos[vfat][ch] = r.TH1D(
                        'scurve_vfat{0}_chan{1}_h'.format(vfat, ch),
                        'scurve_vfat{0}_chan{1}_h'.format(vfat, ch), 254,
                        self.calDAC2Q_m[vfat] * 254.5 + self.calDAC2Q_b[vfat],
                        self.calDAC2Q_m[vfat] * 0.5 + self.calDAC2Q_b[vfat])
                else:
                    self.scanFuncs[vfat][ch] = r.TF1(
                        'scurveFit_vfat{0}_chan{1}'.format(vfat, ch),
                        '[3]*TMath::Erf((TMath::Max([2],x)-[0])/(TMath::Sqrt(2)*[1]))+[3]',
                        self.calDAC2Q_m[vfat] * 1 + self.calDAC2Q_b[vfat],
                        self.calDAC2Q_m[vfat] * 253 + self.calDAC2Q_b[vfat])
                    self.scanHistos[vfat][ch] = r.TH1D(
                        'scurve_vfat{0}_chan{1}_h'.format(vfat, ch),
                        'scurve_vfat{0}_chan{1}_h'.format(vfat, ch), 254,
                        self.calDAC2Q_m[vfat] * 0.5 + self.calDAC2Q_b[vfat],
                        self.calDAC2Q_m[vfat] * 254.5 + self.calDAC2Q_b[vfat])
                    pass
                self.scanHistosChargeBins[vfat][ch] = [
                    self.scanHistos[vfat][ch].GetXaxis().GetBinLowEdge(binX)
                    for binX in range(
                        1, self.scanHistos[vfat][ch].GetNbinsX() + 2)
                ]  #Include overflow
                pass
            pass

        self.fitValid = [
            np.zeros(maxChans, dtype=bool) for vfat in range(self.nVFATS)
        ]

        return