Esempio n. 1
0
    def set_run_start_stop(self,runstart_var=variables.Variable(None),runstop_var=variables.Variable(None)):
        """
        Let the simulation category know which 
        are the paramters describing the primary

        Keyword Args:
            runstart_var (pyevself.variables.variables.Variable): beginning of a run
            runstop_var (pyevself.variables.variables.Variable): beginning of a run

        """
        #FIXME
        for var,name in [(runstart_var,RUN_START),(runstop_var,RUN_STOP)]:
            if var.name is None:
                Logger.warning("No {0} available".format(name))
            elif name in self.vardict:
                Logger.info("..{0} already defined, skipping...".format(name))
                continue
            
            else:
                if var.name != name:
                    Logger.info("..renaming {0} to {1}..".format(var.name,name))
                    var.name = name
                newvar = deepcopy(var)
                self.vardict[name] = newvar

        self._runstartstop_set = True
Esempio n. 2
0
    def get_weights(self,model,model_kwargs = None):
        """
        Calculate weights for the variables in this category

        Args:
            model (callable): A model to be evaluated

        Keyword Args:
            model_kwargs (dict): Will be passed to model
        """
        if not self.mc_p_readout:
            self.read_mc_primary()

        if model_kwargs is None:
            model_kwargs = dict()
        func_kwargs = {MC_P_EN : self.get(MC_P_EN),\
                       MC_P_TY : self.get(MC_P_TY),\
                       MC_P_WE : self.get(MC_P_WE)}

        for key in MC_P_ZE,MC_P_GW,MC_P_TS,DATASETS:
            reg = key
            if key == DATASETS:
                reg = 'mc_datasets'
            try:
                func_kwargs[reg] = self.get(key)
            except KeyError:
                Logger.warning("No MCPrimary {0} informatiion! Trying to omit..".format(key))

        func_kwargs.update(model_kwargs)
        Logger.info("Getting weights for datasets {}".format(self.datasets.__repr__()))
        self._weights = pd.Series(self._weightfunction(model,self.datasets,\
                                 **func_kwargs))
Esempio n. 3
0
    def get_files(self,*args,**kwargs):
        """
        Load files for this category
        uses pyevsel.utils.files.harvest_files

        Args:
            *args (list of strings): Path to possible files

        Keyword Args:
            datasets (dict(dataset_id : nfiles)): i given, load only files from dataset dataset_id  set nfiles parameter to amount of L2 files the loaded files will represent
            force (bool): forcibly reload filelist (pre-readout vars will be lost)
            all other kwargs will be passed to
            utils.files.harvest_files
        """
        force = False
        if "force" in kwargs:
            force = kwargs.pop("force")
        if self.is_harvested:
            Logger.info("Variables have already been harvested!\
                         if you really want to reload the filelist,\
                         use 'force=True'.\
                         If you do so, all your harvested variables will be deleted!")
            if not force:
                return
            else:
                Logger.warning("..using force..")

        if "datasets" in kwargs:
            filtered_files = []
            self.datasets = kwargs.pop("datasets")
            files = harvest_files(*args,**kwargs)
            datasets = [self._ds_regexp(x) for x in files]
            assert len(datasets) == len(files)

            ds_files = zip(datasets,files)
            for k in self.datasets.keys():
                filtered_files.extend([x[1] for x in ds_files if x[0] == k])
            files = filtered_files
        else:
            files = harvest_files(*args,**kwargs)

        self.files = files
Esempio n. 4
0
    def add_ratio(self,names_upper,names_under,\
                  total_ratio=None,total_ratio_errors=None,\
                  log=False,label="data/$\Sigma$ bg"):
        """
        Add a ratio plot to the canvas

        """
        if not isinstance(names_upper,list):
            names_upper = [names_upper]
        if not isinstance(names_under,list):
            names_under = [names_under]

        name = "".join(names_upper) + "_" + "".join(names_under)
        first_upper = names_upper.pop()
        upper_hist = self.histograms[first_upper]
        upper_ws   = self.histograms[first_upper].stats.weightsum
        for name in names_upper:
            upper_hist += self.histograms[name] 
            upper_ws   += self.histograms[name].stats.weightsum
        first_under = names_under.pop()
        under_hist = self.histograms[first_under]
        under_ws = self.histograms[first_under].stats.weightsum
        for name in names_under:
            under_hist += self.histograms[name]
            under_ws   += self.histograms[name].stats.weightsum
    
        upper_hist.normalized()
        under_hist.normalized()
        ratio = d.histfuncs.histratio(upper_hist,under_hist,\
                                      log=False,ylabel=label)
        if total_ratio is None:
            total_ratio = upper_ws/under_ws
            Logger.info("Calculated scalar ratio of {:4.2f} from histos".format(total_ratio))

        #ratio.y[ratio.y > 0] = ratio.y[ratio.y > 0] + total_ratio -1
        self.histratios[name] = (ratio,total_ratio,total_ratio_errors,label)
        return name
Esempio n. 5
0
    def plot(self,heights=(.5,.2,.2),\
             axes_locator=((0,"c"),(1,"r"),(2,"h")),\
             combined_distro=True,\
             combined_ratio=True,\
             combined_cumul=True,
             log=True):
        '''
        Create the plot

        Args:
            heights:
            axes_locator:
            combined_distro:
            combined_ratio:
            combined_cumul:
            log:

        Returns:

        '''

        Logger.info("Found {} distributions".format(len(self.histograms)))
        Logger.info("Found {} ratios".format(len(self.histratios)))
        Logger.info("Found {} cumulative distributions".format(len(self.cumuls)))
        if not axes_locator:
            axes_locator = self._locate_axes(combined_cumul,combined_ratio,combined_distro)

        # calculate the amount of needed axes
        assert len(axes_locator) == len(heights), "Need to specify exactly as many heights as plots you want to have"

        self.canvas = c.YStackedCanvas(axeslayout=heights)
        
        cu_axes = filter(lambda x : x[1] == "c",axes_locator)
        h_axes = filter(lambda x : x[1] == "h",axes_locator)
        r_axes = filter(lambda x : x[1] == "r",axes_locator)
        maxheights = []
        minheights = []
        for ax in cu_axes:
            cur_ax = self.canvas.select_axes(ax[0])
            if combined_cumul:
                for k in self.cumuls.keys():
                    self._draw_distribution(cur_ax,k,cumulative=True,log=log)
                break
            else:
                k = self.cumuls[self.cumuls.keys()[ax[0]]]
                self._draw_distribution(cur_ax,cumulative=True,log=log)
        for ax in r_axes:
            cur_ax = self.canvas.select_axes(ax[0])
            if combined_ratio:
                for k in self.histratios.keys():
                    self._draw_histratio(k,cur_ax)
                break
            else:
                k = self.histratios[self.histratios.keys()[ax[0]]]
                self._draw_histratio(k,cur_ax)    

        for ax in h_axes:
            cur_ax = self.canvas.select_axes(ax[0])
            if combined_distro:
                for k in self.histograms.keys():
                    print "drawing..",k
                    self._draw_distribution(cur_ax,k,log=log)
                break
            else:
                k = self.histograms[self.histograms.keys()[ax[0]]]
                ymax, ymin = self._draw_distribution(cur_ax,k,log=log)
            cur_ax.set_ylim(ymin=ymin - 0.1*ymin,ymax=1.1*ymax)
        lgax = self.canvas.select_axes(-1)#most upper one
        lg = lgax.legend(**LoadConfig()['legend'])
        legendwidth = LoadConfig()
        legendwidth = legendwidth['legendwidth']
        lg.get_frame().set_linewidth(legendwidth)
        # plot the cuts
        if self.cuts:
            for ax in h_axes:
                self.indicate_cut(ax,arrow=True)
            for ax in r_axes + cu_axes:
                self.indicate_cut(ax,arrow=False)
        # cleanup
        leftplotedge = n.inf
        rightplotedge = -n.inf
        minplotrange = n.inf
        maxplotrange = -n.inf
        for h in self.histograms.values():
            if not h.bincenters[h.bincontent > 0].sum():
                continue
            if h.bincenters[h.bincontent > 0][0] < leftplotedge:
                leftplotedge = h.bincenters[h.bincontent > 0][0]
            if h.bincenters[h.bincontent > 0][-1] > rightplotedge:
                rightplotedge = h.bincenters[h.bincontent > 0][-1]
            if min(h.bincontent[h.bincontent > 0]) < minplotrange:
                minplotrange = min(h.bincontent[h.bincontent > 0])
            if max(h.bincontent[h.bincontent > 0]) > maxplotrange:
                maxplotrange = max(h.bincontent[h.bincontent > 0])

        if log:
            maxplotrange *= 8
        else:
            maxplotrange *= 1.2
        if n.isfinite(leftplotedge):
            self.canvas.limit_xrange(xmin=leftplotedge)
        if n.isfinite(rightplotedge):
            self.canvas.limit_xrange(xmax=rightplotedge)
        for ax in h_axes:
            self.canvas.select_axes(ax[0]).set_ylim(ymax=maxplotrange,ymin=minplotrange)

        #if n.isfinite(minplotrange):
        #    self.canvas.limit_yrange(ymin=minplotrange - 0.1*minplotrange)
        #if n.isfinite(maxplotrange):
        #    self.canvas.limit_yrange(ymax=maxplotrange)
        self.canvas.eliminate_lower_yticks()
        # set the label on the lowest axes
        self.canvas.axes[0].set_xlabel(self.label)