예제 #1
0
    def calcMeshWgts(self, dictOfMeshes, type="nMAD", kNN=200, nMADCut=4.0):
        """ utility routine to recalculate weights for each mesh entry 
        calculation is done in-place
        """

        outDict = {}
        # calculate nMAD over kNN for each point in each mesh
        # then calculate a new weight: =1 for nMAD<cut and =0 for nMAD>cut
        # try removing tails
        if type == "nMAD":
            plotList = []
            for iZ in range(4, 15 + 1):
                key = "z%dMesh" % (iZ)
                if dictOfMeshes.has_key(key):
                    thisMesh = dictOfMeshes[key]
                    nMADArr = thisMesh.calcWgtnMAD(kNN=kNN, nMADCut=nMADCut)

                    hnMAD = hfillhist("z%dnMAD" % (iZ), "z%d nMAD Distribution" % (iZ), nMADArr, 100, -10.0, 10.0)
                    plotList.append(hnMAD)

            # unique name for our canvas
            tstr = "canvas" + str(time.time())
            nplots = len(plotList)
            nCols = 4
            nRows = (nplots - 1) / nCols + 1
            canvas = TCanvas(tstr, tstr, 300 * nRows, 300 * nCols)
            canvas.Divide(nRows, nCols)

            for i in range(nplots):
                canvas.cd(i + 1)
                plotList[i].Draw()
                SetOwnership(plotList[i], False)

            outDict["canvas"] = canvas

        else:
            print "donutana Error: bad type ", type, " in calcMeshWgts"

        return outDict
예제 #2
0
    def analyzeMeshes(self, dictOfMeshes, doCull=False, cullCut=0.90):
        """ analyzeMeshes takes a dictionary with meshes as input, and fits it to the references
        output dictionary includes a deepcopy of the input meshes
        """

        # deepcopy the meshes! no longer adjusts in place
        dictOfMeshesCopy = {}
        for key in dictOfMeshes.keys():
            theMesh = copy.deepcopy(dictOfMeshes[key])
            dictOfMeshesCopy[key] = theMesh

        # then we analyze the Zernike polynomials by comparing them to stored references
        # analyzeDonuts returns a dictionary containing deltas and rotation angles for focus, astigmatism and coma
        # as well as the hexapod adjustments
        # it also makes a Canvas of plots for study

        dictOfResults = {}

        # loop over keys, fitting References
        for key in dictOfMeshesCopy.keys():

            # some special cases
            resultsKeyName = "%sResultDict" % (key.replace("Mesh", ""))
            meshName = key
            if key == "z4":
                dictOfResults[resultsKeyName] = self.fitToRefMesh(
                    self.meshDict[meshName], dictOfMeshesCopy[meshName], self.zangleconv
                )
            elif key == "rzero":
                dictOfResults[resultsKeyName] = self.analyzeRzero(rzeroMesh, chi2Mesh, neleMesh)
            elif key == "chi2":
                continue
            elif key == "nele":
                continue
            else:
                dictOfResults[resultsKeyName] = self.fitToRefMesh(self.meshDict[meshName], dictOfMeshesCopy[meshName])

        # if we want to cull, cull and refit!
        # use the fitted weight to cull - culltype="fit"
        if doCull:
            # this will take the wgt's from the fit and take their product
            # for each point, and cull at a product of cullCut
            self.cullAllMeshes(dictOfMeshesCopy, cullCut=cullCut)

            # loop over keys, fitting References
            for key in dictOfMeshesCopy.keys():

                # some special cases
                resultsKeyName = "%sResultDict" % (key.replace("Mesh", ""))
                meshName = key
                if key == "z4":
                    dictOfResults[resultsKeyName] = self.fitToRefMesh(
                        self.meshDict[meshName], dictOfMeshesCopy[meshName], self.zangleconv
                    )
                elif key == "rzero":
                    rzeroResultDict = self.analyzeRzero(rzeroMesh, chi2Mesh, neleMesh)
                elif key == "chi2":
                    continue
                elif key == "nele":
                    continue
                else:
                    dictOfResults[resultsKeyName] = self.fitToRefMesh(
                        self.meshDict[meshName], dictOfMeshesCopy[meshName]
                    )

        # analyze this data and extract the hexapod coefficients
        donutDict = self.calcHexapod(dictOfResults)

        if len(donutDict) == 0:
            goodCalc = False
        else:
            goodCalc = True

        # add the individual fit results here too
        for key in dictOfResults.keys():
            donutDict[key] = dictOfResults[key]

        # and add the meshes too
        for key in dictOfMeshesCopy.keys():
            donutDict[key] = dictOfMeshesCopy[key]

        # make a Canvas of plots for this image
        # plot Histogram of Difference before fit, after fit, and after fit vs. X,Y position
        if self.paramDict["histFlag"] and dictOfResults["z4ResultDict"].has_key("deltaArrayBefore") and goodCalc:

            # setup plots
            gStyle.SetStatH(0.32)
            gStyle.SetStatW(0.4)
            gStyle.SetOptStat(1111111)
            gStyle.SetMarkerStyle(20)
            gStyle.SetMarkerSize(0.5)
            gStyle.SetPalette(1)
            gROOT.ForceStyle()

            # loop over results, making plots for each
            nplots = 0
            plotDict = {}
            for key in dictOfResults.keys():
                theResultDict = dictOfResults[key]

                keyId = key.replace("ResultDict", "")
                # special cases
                if keyId == "z4":
                    nWavesBefore = 200.0
                    nWavesAfter = 200.0
                else:
                    nWavesBefore = 1.0
                    nWavesAfter = 0.2

                if keyId != "rzero":
                    nplots = nplots + 1
                    hBefore = hfillhist(
                        key + "Before",
                        "Delta " + key + ", Before Fit",
                        theResultDict["deltaArrayBefore"],
                        200,
                        -nWavesBefore,
                        nWavesBefore,
                    )
                    hAfter = hfillhist(
                        key + "zAfter",
                        "Delta " + key + ", After Fit",
                        theResultDict["deltaArrayAfter"],
                        200,
                        -nWavesAfter,
                        nWavesAfter,
                    )
                    hBefore2D = TGraph2D(
                        key + "Before2D",
                        "Delta " + key + ", Before Fit, vs. Position;X[mm];Y[mm]",
                        theResultDict["deltaArrayBefore"].shape[0],
                        theResultDict["deltaArrayX"],
                        theResultDict["deltaArrayY"],
                        theResultDict["deltaArrayBefore"],
                    )
                    hAfter2D = TGraph2D(
                        key + "After2D",
                        "Delta " + key + ", After Fit, vs. Position;X[mm];Y[mm]",
                        theResultDict["deltaArrayAfter"].shape[0],
                        theResultDict["deltaArrayX"],
                        theResultDict["deltaArrayY"],
                        theResultDict["deltaArrayAfter"],
                    )

                    plotList = [hBefore, hAfter, hBefore2D, hAfter2D]
                    plotDict[keyId] = plotList

            # the Canvas

            # unique name for our canvas
            tstr = "canvas" + str(time.time())

            canvas = TCanvas(tstr, tstr, 300 * nplots, 1000)
            canvas.Divide(nplots, 4)

            # plot em
            jZ = 0
            for iZ in range(4, 15 + 1):
                key = "z%d" % (iZ)
                if plotDict.has_key(key):
                    jZ = jZ + 1

                    plotList = plotDict[key]

                    icanvas = jZ + 0 * nplots
                    canvas.cd(icanvas)
                    plotList[0].Draw()
                    icanvas = jZ + 1 * nplots
                    canvas.cd(icanvas)
                    plotList[1].Draw()
                    icanvas = jZ + 2 * nplots
                    canvas.cd(icanvas)
                    plotList[2].Draw("zcolpcol")
                    icanvas = jZ + 3 * nplots
                    canvas.cd(icanvas)
                    plotList[3].Draw("zcolpcol")

            # set it so that python doesn't own these ROOT object
            for key in plotDict.keys():
                for plot in plotDict[key]:
                    SetOwnership(plot, False)

            # save canvas in the output Dictionary
            donutDict["canvas"] = canvas

        # all done
        return donutDict