예제 #1
0
 def CheckNpred(self, Npred, Flux, FluxErr, detected_indices):
     """check if the errors are well computed using the Npred/sqrt(Npred) vs Flux/FluxErr relation
        and print corresponding point which failled"""
     _, TgrNpred = plotting.PlotNpred(Npred[detected_indices],
                                      Flux[detected_indices],
                                      FluxErr[detected_indices])
     func = ROOT.TF1("func", "pol1", np.min(np.sqrt(Npred)),
                     np.max(np.sqrt(Npred)))
     TgrNpred.Fit(func, "Q")
     for i in xrange(len(Flux)):
         if Flux[i] / FluxErr[i] > 2 * func.Eval(sqrt(Npred[i])):
             self._errorReading("problem in errors calculation for", i)
             print "Flux +/- error = ", Flux[i], " +/- ", FluxErr[i]
             print "V(Npred) = ", sqrt(Npred[i])
             print
     func.SetLineColor(15)
     func.SetLineStyle(2)
     return func
예제 #2
0
    def _PlotLC(self, folded=False):
        root_style.RootStyle()  #Nice plot style

        self.info("Reading files produced by enrico")
        LcOutPath = self.LCfolder + self.config['target']['name']

        #Result are stored into list. This allow to get rid of the bin which failled
        Time = []
        TimeErr = []
        Flux = []
        FluxErr = []
        Index = []
        IndexErr = []
        Cutoff = []
        CutoffErr = []
        FluxForNpred = []
        FluxErrForNpred = []
        Npred = []
        Npred_detected_indices = []
        TS = []

        # Find name used for index parameter
        if (self.config['target']['spectrum'] == 'PowerLaw'
                or self.config['target']['spectrum'] == 'PowerLaw2'):
            IndexName = 'Index'
            CutoffName = None
        elif (self.config['target']['spectrum'] == 'PLExpCutoff'
              or self.config['target']['spectrum'] == 'PLSuperExpCutoff'):
            IndexName = 'Index1'
            CutoffName = 'Cutoff'
            CutoffErrName = 'dCutoff'
        IndexErrName = 'd' + IndexName

        Nfail = 0
        for i in xrange(self.Nbin):
            CurConfig = get_config(self.configfile[i])
            #Read the result. If it fails, it means that the bins has not bin computed. A warning message is printed
            try:
                ResultDic = utils.ReadResult(CurConfig)
            except:
                self._errorReading("Fail reading config file", i)
                Nfail += 1
                continue

            #Update the time and time error array
            Time.append((ResultDic.get("tmax") + ResultDic.get("tmin")) / 2.)
            TimeErr.append(
                (ResultDic.get("tmax") - ResultDic.get("tmin")) / 2.)
            #Check is an ul have been computed. The error is set to zero for the TGraph.
            if ResultDic.has_key('Ulvalue'):
                Flux.append(ResultDic.get("Ulvalue"))
                FluxErr.append(0)
                Index.append(ResultDic.get(IndexName))
                IndexErr.append(0)
            else:
                Flux.append(ResultDic.get("Flux"))
                FluxErr.append(ResultDic.get("dFlux"))
                Index.append(ResultDic.get(IndexName))
                IndexErr.append(ResultDic.get(IndexErrName))
                if CutoffName is not None:
                    Cutoff.append(ResultDic.get(CutoffName))
                    CutoffErr.append(ResultDic.get(CutoffErrName))
            FluxErrForNpred.append(ResultDic.get("dFlux"))
            FluxForNpred.append(ResultDic.get("Flux"))
            #Get the Npred and TS values
            Npred.append(ResultDic.get("Npred"))
            TS.append(ResultDic.get("TS"))
            if (CurConfig['LightCurve']['TSLightCurve'] < float(
                    ResultDic.get("TS"))):
                Npred_detected_indices.append(i - Nfail)

        #change the list into np array
        TS = np.array(TS)
        Npred = np.array(Npred)
        Npred_detected = Npred[Npred_detected_indices]
        Time = np.array(Time)
        TimeErr = np.array(TimeErr)
        Flux = np.array(Flux)
        FluxErr = np.array(FluxErr)
        Index = np.array(Index)
        IndexErr = np.array(IndexErr)
        Cutoff = np.array(Cutoff)
        CutoffErr = np.array(CutoffErr)
        FluxForNpred = np.array(FluxForNpred)
        FluxErrForNpred = np.array(FluxErrForNpred)

        #Plots the diagnostic plots is asked
        # Plots are : Npred vs flux
        #             TS vs Time
        if self.config['LightCurve']['DiagnosticPlots'] == 'yes':
            fittedFunc = self.CheckNpred(
                Npred, FluxForNpred, FluxErrForNpred,
                Npred_detected_indices)  #check the errors calculation
            gTHNpred, TgrNpred = plotting.PlotNpred(Npred, FluxForNpred,
                                                    FluxErrForNpred)
            CanvNpred = _GetCanvas()
            gTHNpred.Draw()
            TgrNpred.Draw('zP')

            _, TgrNpred_detected = plotting.PlotNpred(
                Npred_detected, Flux[Npred_detected_indices],
                FluxErrForNpred[Npred_detected_indices])
            TgrNpred_detected.SetLineColor(2)
            TgrNpred_detected.SetMarkerColor(2)
            TgrNpred_detected.Draw('zP')
            fittedFunc.Draw("SAME")

            CanvNpred.Print(LcOutPath + "_Npred.png")
            CanvNpred.Print(LcOutPath + "_Npred.eps")
            CanvNpred.Print(LcOutPath + "_Npred.C")

            gTHTS, TgrTS = plotting.PlotTS(Time, TimeErr, TS)
            CanvTS = _GetCanvas()
            gTHTS.Draw()
            TgrTS.Draw('zP')
            CanvTS.Print(LcOutPath + '_TS.png')
            CanvTS.Print(LcOutPath + '_TS.eps')
            CanvTS.Print(LcOutPath + '_TS.C')

#    Plot the LC itself. This function return a TH2F for a nice plot
#    a TGraph and a list of TArrow for the ULs
        if folded:
            phase = np.linspace(0, 1, self.Nbin + 1)
            Time = (phase[1:] + phase[:-1]) / 2.
            TimeErr = (phase[1:] - phase[:-1]) / 2.
            gTHLC, TgrLC, ArrowLC = plotting.PlotFoldedLC(
                Time, TimeErr, Flux, FluxErr)
            gTHIndex, TgrIndex, ArrowIndex = plotting.PlotFoldedLC(
                Time, TimeErr, Index, IndexErr)
            if CutoffName is not None:
                gTHCutoff, TgrCutoff, ArrowCutoff = plotting.PlotFoldedLC(
                    Time, TimeErr, Cutoff, CutoffErr)
        else:
            gTHLC, TgrLC, ArrowLC = plotting.PlotLC(Time, TimeErr, Flux,
                                                    FluxErr)
            gTHIndex, TgrIndex, ArrowIndex = plotting.PlotLC(
                Time, TimeErr, Index, IndexErr)
            if CutoffName is not None:
                gTHCutoff, TgrCutoff, ArrowCutoff = plotting.PlotFoldedLC(
                    Time, TimeErr, Cutoff, CutoffErr)

        ### plot and save the flux LC
        CanvLC = ROOT.TCanvas()
        gTHLC.Draw()
        TgrLC.Draw('zP')

        #plot the ul as arrow
        for i in xrange(len(ArrowLC)):
            ArrowLC[i].Draw()

        # compute Fvar and probability of being cst

        self.info("Flux vs Time: infos")
        self.FitWithCst(TgrLC)
        self.Fvar(Flux, FluxErr)

        #Save the canvas in the LightCurve subfolder
        CanvLC.Print(LcOutPath + '_LC.png')
        CanvLC.Print(LcOutPath + '_LC.eps')
        CanvLC.Print(LcOutPath + '_LC.C')

        ### plot and save the Index LC
        CanvIndex = ROOT.TCanvas()
        gTHIndex.Draw()
        TgrIndex.Draw('zP')

        #plot the ul as arrow
        for i in xrange(len(ArrowIndex)):
            ArrowIndex[i].Draw()

        #Save the canvas in the LightCurve subfolder
        if self.config["LightCurve"]["SpectralIndex"] == 0:
            self.info("Index vs Time")
            self.FitWithCst(TgrIndex)
            CanvIndex.Print(LcOutPath + '_Index.png')
            CanvIndex.Print(LcOutPath + '_Index.eps')
            CanvIndex.Print(LcOutPath + '_Index.C')

        if len(Cutoff) > 0:
            ### plot and save the Cutoff LC
            CanvCutoff = ROOT.TCanvas()
            gTHCutoff.Draw()
            TgrCutoff.Draw('zP')

            #plot the ul as arrow
            for i in xrange(len(ArrowCutoff)):
                ArrowCutoff[i].Draw()

            print "Cutoff vs Time: infos"
            self.FitWithCst(TgrCutoff)
            CanvCutoff.Print(LcOutPath + '_Cutoff.png')
            CanvCutoff.Print(LcOutPath + '_Cutoff.eps')
            CanvCutoff.Print(LcOutPath + '_Cutoff.C')

        #Dump into ascii
        lcfilename = LcOutPath + "_results.dat"
        self.info("Write to Ascii file : " + lcfilename)
        WriteToAscii(Time, TimeErr, Flux, FluxErr, Index, IndexErr, Cutoff,
                     CutoffErr, TS, Npred, lcfilename)

        if self.config["LightCurve"]['ComputeVarIndex'] == 'yes':
            self.VariabilityIndex()
예제 #3
0
    def _PlotLC(self, folded=False):
        root_style.RootStyle()  #Nice plot style

        print "Reading files produced by enrico"
        LcOutPath = self.LCfolder + self.config['target']['name']

        #Result are stored into list. This allow to get rid of the bin which failled
        Time = []
        TimeErr = []
        Flux = []
        FluxErr = []
        FluxForNpred = []
        FluxErrForNpred = []
        Npred = []
        Npred_detected_indices = []
        TS = []

        for i in xrange(self.Nbin):
            CurConfig = get_config(self.configfile[i])
            #Read the result. If it fails, it means that the bins has not bin computed. A warning message is printed
            try:
                ResultDic = utils.ReadResult(CurConfig)
            except:
                self._errorReading("fail reading config file", i)
                continue

            #Update the time and time error array
            Time.append((ResultDic.get("tmax") + ResultDic.get("tmin")) / 2.)
            TimeErr.append(
                (ResultDic.get("tmax") - ResultDic.get("tmin")) / 2.)
            #Check is an ul have been computed. The error is set to zero for the TGraph.
            if ResultDic.has_key('Ulvalue'):
                Flux.append(ResultDic.get("Ulvalue"))
                FluxErr.append(0)
            else:
                Flux.append(ResultDic.get("Flux"))
                FluxErr.append(ResultDic.get("dFlux"))
            FluxErrForNpred.append(ResultDic.get("dFlux"))
            FluxForNpred.append(ResultDic.get("Flux"))
            #Get the Npred and TS values
            Npred.append(ResultDic.get("Npred"))
            TS.append(ResultDic.get("TS"))
            if (CurConfig['LightCurve']['TSLightCurve'] < float(
                    ResultDic.get("TS"))):
                Npred_detected_indices.append(i)

        #change the list into np array
        TS = np.array(TS)
        Npred = np.array(Npred)
        Npred_detected = Npred[Npred_detected_indices]
        Time = np.array(Time)
        TimeErr = np.array(TimeErr)
        Flux = np.array(Flux)
        FluxErr = np.array(FluxErr)
        FluxForNpred = np.array(FluxForNpred)
        FluxErrForNpred = np.array(FluxErrForNpred)

        fittedFunc = self.CheckNpred(
            Npred, FluxForNpred, FluxErrForNpred,
            Npred_detected_indices)  #check the errors calculation

        #Plots the diagnostic plots is asked
        # Plots are : Npred vs flux
        #             TS vs Time
        if self.config['LightCurve']['DiagnosticPlots'] == 'yes':
            gTHNpred, TgrNpred = plotting.PlotNpred(Npred, FluxForNpred,
                                                    FluxErrForNpred)
            CanvNpred = _GetCanvas()
            gTHNpred.Draw()
            TgrNpred.Draw('zP')

            _, TgrNpred_detected = plotting.PlotNpred(
                Npred_detected, Flux[Npred_detected_indices],
                FluxErrForNpred[Npred_detected_indices])
            TgrNpred_detected.SetLineColor(2)
            TgrNpred_detected.SetMarkerColor(2)
            TgrNpred_detected.Draw('zP')
            fittedFunc.Draw("SAME")

            CanvNpred.Print(LcOutPath + "_Npred.eps")
            CanvNpred.Print(LcOutPath + "_Npred.C")

            gTHTS, TgrTS = plotting.PlotTS(Time, TimeErr, TS)
            CanvTS = _GetCanvas()
            gTHTS.Draw()
            TgrTS.Draw('zP')
            CanvTS.Print(LcOutPath + '_TS.eps')
            CanvTS.Print(LcOutPath + '_TS.C')

#    Plot the LC itself. This function return a TH2F for a nice plot
#    a TGraph and a list of TArrow for the ULs
        if folded:
            phase = np.linspace(0, 1, self.Nbin + 1)
            Time = (phase[1:] + phase[:-1]) / 2.
            TimeErr = (phase[1:] - phase[:-1]) / 2.
            gTHLC, TgrLC, ArrowLC = plotting.PlotFoldedLC(
                Time, TimeErr, Flux, FluxErr)
        else:
            gTHLC, TgrLC, ArrowLC = plotting.PlotLC(Time, TimeErr, Flux,
                                                    FluxErr)
        CanvLC = ROOT.TCanvas()
        gTHLC.Draw()
        TgrLC.Draw('zP')

        #plot the ul as arrow
        for i in xrange(len(ArrowLC)):
            ArrowLC[i].Draw()

        # compute Fvar and probability of being cst
        self.FitWithCst(TgrLC)
        self.Fvar(Flux, FluxErr)

        #Save the canvas in the LightCurve subfolder
        CanvLC.Print(LcOutPath + '_LC.eps')
        CanvLC.Print(LcOutPath + '_LC.C')

        #Dump into ascii
        lcfilename = LcOutPath + "_results.dat"
        print "Write to Ascii file : ", lcfilename
        WriteToAscii(Time, TimeErr, Flux, FluxErr, TS, Npred, lcfilename)

        if self.config["LightCurve"]['ComputeVarIndex'] == 'yes':
            self.VariabilityIndex()