Ejemplo n.º 1
0
Archivo: polyeff.py Proyecto: op3/hdtv
    def _set_fitInput(self, fitPairs):

        ln_fitPairs = Pairs(conv_func=log)

        for p in fitPairs:
            ln_fitPairs.add(p[0], p[1])

        _Efficiency._set_fitInput(self, ln_fitPairs)
Ejemplo n.º 2
0
Archivo: polyeff.py Proyecto: op3/hdtv
    def _get_fitInput(self):

        fitPairs = Pairs(conv_func=exp)

        for p in self._fitInput:
            fitPairs.add(p[0], p[1])

        return fitPairs
Ejemplo n.º 3
0
 def _get_background_pairs(self):
     if self.bgMarkers.IsPending():
         hdtv.ui.warning("Not all background regions are closed.")
     backgrounds = Pairs()
     for m in self.bgMarkers:
         try:
             backgrounds.add(m.p1.pos_uncal, m.p2.pos_uncal)
         except AttributeError:
             pos = m.p1.pos_cal if m.p1.pos_cal else m.p2.pos_cal
             hdtv.ui.warning("Background region at {:.2f} without second marker was ignored".format(pos))
     return backgrounds
Ejemplo n.º 4
0
 def RestorePeaks(self,
                  cal=None,
                  region=Pairs(),
                  peaks=list(),
                  chisquare=0.0,
                  coeffs=list()):
     """
     Create the Peak Fitter object and
     restore all peaks
     """
     # create the fitter
     peaklist = [p.pos.nominal_value for p in peaks]
     self.peakFitter = self.peakModel.GetFitter(region, peaklist, cal)
     # restore first the fitter and afterwards the peaks
     if self.bgFitter:
         # external background
         self.peakFitter.Restore(self.bgFitter, chisquare)
     else:
         # internal background
         values = ROOT.TArrayD(self.bgdeg + 1)
         errors = ROOT.TArrayD(self.bgdeg + 1)
         for i in range(0, self.bgdeg + 1):
             values[i] = coeffs[i].nominal_value
             errors[i] = coeffs[i].std_dev
         self.peakFitter.Restore(values, errors, chisquare)
     if not len(peaks) == self.peakFitter.GetNumPeaks():
         raise RuntimeError("Number of peaks does not match")
     for i, peak in enumerate(peaks):
         self.peakModel.RestoreParams(peak, self.peakFitter.GetPeak(i))
Ejemplo n.º 5
0
    def __init__(self, num_pars=0, pars=None, norm=True):
        pars = pars or []

        self._numPars = num_pars
        self.parameter = pars
        self.fCov = [[None for j in range(self._numPars)]
                     for i in range(self._numPars)
                     ]  # Simple matrix replacement
        self._dEff_dP = list()
        self.TGraph = TGraphErrors()

        # Normalization factors
        self._doNorm = norm
        self.norm = 1.0
        self.TF1.FixParameter(0, self.norm)  # Normalization
        self.TF1.SetRange(0, 10000)  # Default range for efficiency function

        self._fitInput = Pairs(lambda x: ufloat(x, 0))

        #         if self.parameter: # Parameters were given
        #             map(lambda i: self.TF1.SetParameter(i + 1, self.parameter[i]), range(1, len(pars))) # Set initial parameters
        #         else:
        #             self.parameter = [None for i in range(1, self._numPars + 1)]
        #
        self.TF1.SetParName(0, "N")  # Normalization

        for i in range(0, num_pars):
            self._dEff_dP.append(None)
            if num_pars <= len(string.ascii_lowercase):
                self.TF1.SetParName(i + 1, string.ascii_lowercase[i])
Ejemplo n.º 6
0
Archivo: fit.py Proyecto: op3/hdtv
    def Restore(self, spec):
        # do not call Erase() while setting spec!
        self._spec = weakref(spec)
        self.cal = spec.cal
        self.color = spec.color
        self.FixMarkerInUncal()
        if len(self.bgMarkers) > 0 and not self.bgMarkers.IsPending():
            backgrounds = Pairs()
            for m in self.bgMarkers:
                backgrounds.add(m.p1.pos_uncal, m.p2.pos_uncal)
            self.fitter.RestoreBackground(backgrounds=backgrounds,
                                          params=self.bgParams,
                                          chisquare=self.bgChi)
        region = sorted([
            self.regionMarkers[0].p1.pos_uncal,
            self.regionMarkers[0].p2.pos_uncal
        ])
        print(self.bgParams)
        if self.peaks:
            self.fitter.RestorePeaks(
                cal=self.cal,
                region=region,
                peaks=self.peaks,
                chisquare=self.chi,
                coeffs=self.bgParams,
            )
            # get background function
            func = self.fitter.peakFitter.GetBgFunc()
            self.dispBgFunc = ROOT.HDTV.Display.DisplayFunc(func, self.color)
            self.dispBgFunc.SetCal(self.cal)
            # get peak function
            func = self.fitter.peakFitter.GetSumFunc()
            self.dispPeakFunc = ROOT.HDTV.Display.DisplayFunc(func, self.color)
            self.dispPeakFunc.SetCal(self.cal)

            # restore display functions of single peaks
            for i in range(0, self.fitter.peakFitter.GetNumPeaks()):
                cpeak = self.fitter.peakFitter.GetPeak(i)
                func = cpeak.GetPeakFunc()
                self.peaks[i].displayObj = ROOT.HDTV.Display.DisplayFunc(
                    func, self.color)
                self.peaks[i].displayObj.SetCal(self.cal)

            # create non-existant integral
            if not self.integral:
                self.integral = hdtv.integral.Integrate(
                    self.spec, self.fitter.bgFitter, region)
Ejemplo n.º 7
0
 def FitBackground(self, spec, backgrounds=Pairs()):
     """
     Create Background Fitter object and do the background fit
     """
     # create fitter
     bgfitter = ROOT.HDTV.Fit.PolyBg(self.bgdeg)
     for bg in backgrounds:
         bgfitter.AddRegion(bg[0], bg[1])
     self.bgFitter = bgfitter
     # do the background fit
     self.bgFitter.Fit(spec.hist.hist)
Ejemplo n.º 8
0
 def FitPeaks(self, spec, region=Pairs(), peaklist=list()):
     """
     Create the Peak Fitter object and do the peak fit
     """
     # create the fitter
     self.peakFitter = self.peakModel.GetFitter(region, peaklist, spec.cal)
     # Do the fitpeak
     if self.bgFitter:
         # external background
         self.peakFitter.Fit(spec.hist.hist, self.bgFitter)
     else:
         # internal background
         self.peakFitter.Fit(spec.hist.hist, self.bgdeg)
Ejemplo n.º 9
0
 def RestoreBackground(self,
                       backgrounds=Pairs(),
                       params=list(),
                       chisquare=0.0):
     """
     Create Background Fitter object and
     restore the background polynom from coeffs
     """
     self.bgFitter = self.backgroundModel.GetFitter(nparams=len(params),
                                                    nbg=len(backgrounds))
     # restore the fitter
     valueArray = ROOT.TArrayD(len(params))
     errorArray = ROOT.TArrayD(len(params))
     for i, param in enumerate(params):
         valueArray[i] = param.nominal_value
         errorArray[i] = param.std_dev
     self.bgFitter.Restore(valueArray, errorArray, chisquare)
Ejemplo n.º 10
0
 def FitBackground(self, spec, backgrounds=Pairs()):
     """
     Create Background Fitter object and do the background fit
     """
     # create fitter
     self.bgFitter = self.backgroundModel.GetFitter(
         nparams=self.backgroundModel.fParStatus['nparams'],
         nbg=len(backgrounds))
     if self.bgFitter is None:
         msg = (
             "Background model %s needs at least %i background regions to execute a fit. Found %i."
             % (self.backgroundModel.name,
                self.backgroundModel.requiredBgRegions, len(backgrounds)))
         raise ValueError(msg)
     else:
         for bg in backgrounds:
             self.bgFitter.AddRegion(bg[0], bg[1])
         # do the background fit
         self.bgFitter.Fit(spec.hist.hist)
Ejemplo n.º 11
0
 def RestoreBackground(self,
                       backgrounds=Pairs(),
                       coeffs=list(),
                       chisquare=0.0):
     """
     Create Background Fitter object and
     restore the background polynom from coeffs
     """
     # create fitter
     bgfitter = ROOT.HDTV.Fit.PolyBg(self.bgdeg)
     for bg in backgrounds:
         bgfitter.AddRegion(bg[0], bg[1])
     self.bgFitter = bgfitter
     # restore the fitter
     valueArray = ROOT.TArrayD(len(coeffs))
     errorArray = ROOT.TArrayD(len(coeffs))
     for i, coeff in enumerate(coeffs):
         valueArray[i] = coeff.nominal_value
         errorArray[i] = coeff.std_dev
     self.bgFitter.Restore(valueArray, errorArray, chisquare)
Ejemplo n.º 12
0
Archivo: fitter.py Proyecto: op3/hdtv
 def FitBackground(self, spec, backgrounds=Pairs()):
     """
     Create Background Fitter object and do the background fit
     """
     # create fitter
     self.bgFitter = self.backgroundModel.GetFitter(
         integrate=self.peakModel.GetOption("integrate"),
         likelihood=self.peakModel.GetOption("likelihood"),
         nparams=self.backgroundModel.fParStatus["nparams"],
         nbg=len(backgrounds),
     )
     if self.bgFitter is None:
         msg = "Background model %s needs at least %i background regions to execute a fit. Found %i.".format(
             self.backgroundModel.name,
             self.backgroundModel.requiredBgRegions,
             len(backgrounds),
         )
         raise ValueError(msg)
     else:
         for bg in backgrounds:
             self.bgFitter.AddRegion(bg[0], bg[1])
         # do the background fit
         self.bgFitter.Fit(spec.hist.hist)