示例#1
0
    def __init__(
            self,
            name,
            efficiency,  ## the function or PDF 
            cut,
            xvar=None,
            yvar=None,
            accept='accept'):

        if isinstance(efficiency, PDF2):
            eff_pdf = efficiency
            xvar = efficiency.xvar
            eff_fun = None
        elif isinstance(efficiency, ROOT.RooAbsReal):
            okx = xvar and isinstance(xvar, ROOT.RooAbsReal)
            oky = yvar and isinstance(yvar, ROOT.RooAbsReal)
            assert oix and oky, 'Invalid efficiency/xvar/yvar setting!'
            eff_pdf = Generic2D_pdf(efficiency, xvar, yvar, special=True)
            eff_fun = None if isinstance(efficiency,
                                         ROOT.RooAbsPdf) else efficiency
        else:
            raise AttributeError(
                'Invalid efficiency/xvar/yvat combination  %s/%s/%s/%s' %
                (efficiency, type(efficiency), xvar, yvar))

        self.__xvar = xvar
        self.__yvar = yvar
        Efficiency.__init__(self, name, eff_pdf, eff_fun, (xvar, yvar), cut,
                            accept)
示例#2
0
    def __init__(self, pdf, xvar=None, yvar=None, fraction=1.e-5, name=''):

        if isinstance(pdf, PDF2):
            self.__old_pdf = pdf
            if xvar and not xvar is pdf.xvar: self.warning("mismatch in xvar?")
            if yvar and not yvar is pdf.yvar: self.warning("mismatch in yvar?")
            xvar = pdf.xvar
            yvar = pdf.yvar
        elif insinstance(pdf, ROOT.RooAbsPdf) and xvar and yvar:
            self.__old_pdf = Generic2D_pdf(pdf, xvar=xvar, yvar=yvar)
        else:
            raise TypeError("Unknown type of pdf %s/%s" % (pdf, type(pdf)))

        assert isinstance(xvar,
                          ROOT.RooAbsReal), "``xvar'' must be ROOT.RooAbsReal"
        assert isinstance(yvar,
                          ROOT.RooAbsReal), "``yvar'' must be ROOT.RooAbsReal"

        name = name if name else self.generate_name('Adjust2D_' +
                                                    self.old_pdf.name)

        ## initialize the base
        PDF2.__init__(self, name=name, xvar=xvar, yvar=yvar)

        em = self.old_pdf.pdf.extendMode()
        if 1 == em: self.warning("PDF  ``canBeExtended''")
        elif 2 == em: self.warning("PDF ``mustBeExtended''")

        ## make the real adjustment
        self.__adjustment = Adjust2D(name,
                                     pdf=self.old_pdf.pdf,
                                     xvar=xvar,
                                     yvar=yvar,
                                     fraction=fraction)

        self.pdf = self.adjustment.pdf

        self.config = {
            'xvar': self.xvar,
            'yvar': self.yvar,
            'name': self.name,
            'pdf': self.old_pdf,
            'fraction': self.fraction
        }
示例#3
0
    def __init__  ( self              ,
                    name              ,
                    eff_pdf           ,                    
                    eff_fun           ,
                    vars              ,
                    cut               ,
                    accept = 'accept' ,
                    scale  = None     ) :

        self.__name   = str(name)
        self.__cut    = cut
        self.__accept = str(accept) 

        assert eff_pdf or eff_fun ,'Function or PDF must be specified!'
        
        assert isinstance ( cut , ROOT.RooCategory ) , "``Cut'' is not RooCategory!"

        self.__eff_pdf = eff_pdf
        self.__eff_fun = eff_fun
        self.__vars    = ROOT.RooArgSet( *vars )
        
        self.__scale   = 1
        
        if not self.eff_fun :

            if scale is None :
                scale = 0.001 , 1.e-10 , 1.e+5 
                mnmx  = self.eff_pdf.minmax ()
                if mnmx :
                    mn , mx = mnmx
                    scale   = 0.25 / mx , 1.e-9 / mx , 10 / mx
                    
            if   isinstance ( scale , ROOT.RooAbsReal ) :
                self.__scale = scale
            else : 
                self.__scale = ROOT.RooRealVar ( 'effscale_%s' % self.name , 'scale factor for efficiency (%s)' % self.name , *scale )

            self.__lst     = ROOT.RooArgList ( self.__scale , self.__eff_pdf.pdf )
            _s = self.scale.GetName()
            _p = self.eff_pdf.pdf.GetName() 
            self.__eff_fun = ROOT.RooFormulaVar (
                'Eff_%s' % self.name , '%s*%s'  % ( _s , _p ) , self.__lst )

        ## create the main PDF: RooEfficiency 
        self.__pdf =  ROOT.RooEfficiency (
            PDF.roo_name ( 'eff_' )       ,
            "Efficiency  %s"  % self.name ,
            self.eff_fun                  ,
            self.cut                      ,
            self.accept                   )

        if 3 == len ( vars ) : 
            ## pdf-object for fit 
            self.__pdf_fit = Generic3D_pdf ( pdf   = self.pdf ,
                                             xvar  = vars[0]  ,
                                             yvar  = vars[1]  ,
                                             zvar  = vars[2]  ,
                                             name  = PDF.generate_name ( 'eff_fit_%s'   % self.name ) ,
                                             special        = True  ,
                                             add_to_signals = False )
            ## pdf-object for drawing
            self.__pdf_draw = Generic3D_pdf ( pdf   = self.eff_fun   ,
                                              xvar  = vars[0]        ,
                                              yvar  = vars[1]        ,
                                              zvar  = vars[2]        ,
                                              name  = PDF.generate_name ( 'eff_draw_%s'  % self.name ) ,
                                              special        = True  ,
                                              add_to_signals = False )
        elif 2 == len (  vars ) :
            ## pdf-object for fit 
            self.__pdf_fit  = Generic2D_pdf ( pdf   = self.pdf ,
                                              xvar  = vars[0]  ,
                                              yvar  = vars[1]  ,
                                              name  = PDF.generate_name ( 'eff_fit_%s'   % self.name ) ,
                                              special        = True  ,
                                              add_to_signals = False )
            ## pdf-object for drawing
            self.__pdf_draw = Generic2D_pdf ( pdf   = self.eff_fun   ,
                                              xvar  = vars[0]        ,
                                              yvar  = vars[1]        ,
                                              name  = PDF.generate_name ( 'eff_draw_%s'  % self.name ) ,
                                              special        = True  ,
                                              add_to_signals = False )
        elif 1 == len (  vars ) :
            ## pdf-object for fit 
            self.__pdf_fit  = Generic1D_pdf ( pdf   = self.pdf ,
                                              xvar  = vars[0]  ,
                                              name  = PDF.generate_name ( 'eff_fit_%s'   % self.name ) ,
                                              special        = True  ,
                                              add_to_signals = False )
            ## pdf-object for drawing
            self.__pdf_draw = Generic1D_pdf ( pdf   = self.eff_fun   ,
                                              xvar  = vars[0]        ,
                                              name  = PDF.generate_name ( 'eff_draw_%s'  % self.name ) ,
                                              special        = True  ,
                                              add_to_signals = False )
        else :
            raise AttributeError("Invalid length of vars: %s" % str( vars ) )
        
        ## th fit results from the last fit
        self.__fit_result = None
示例#4
0
    def __init__ ( self              ,
                   sample            , 
                   categories        ,
                   name       = None , 
                   title      = ''   ) :
        """Helper pdf-like class to simplify the creation and usage of simultaneous PDF
        
        >>> sample = ROOT.RooCategory( 'sample', 'fitting sample' , 'A' , 'B' )
        >>> pdfA   = ... ## pdf for the sample 'A'
        >>> pdfB   = ... ## pdf for the sample 'B'
        >>> simfit = SimFit (  sample , { 'A' : pdfA , 'B' : pdfB } )
        """
        
        if isinstance ( sample , ( tuple , list ) ) :
            _cat = ROOT.RooCategory ( 'sample' , 'sample' )
            for i in sample : _cat.defineType ( i ) 
            sample =  _cat
            
        assert isinstance ( sample , ROOT.RooCategory ),\
               'Invalid type for "sample":' % ( sample ,  type ( sample ) )
        
        if not name  : name  = 'SimFit_'                 +          sample.GetName()
        if not title : title = 'Simultaneous PDF(%s,%s)' % ( name , sample.GetName() )

        ## propagatethe name 
        self.name = name
        
        self.__sample       = sample 
        self.__categories   = {}

        # =====================================================================
        ## components
        # =====================================================================
        labels = sample.labels()

        from ostap.fitting.basic import PDF 
        from ostap.fitting.fit2d import PDF2
        from ostap.fitting.fit3d import PDF3

        _xv = None 
        for label in labels :
            
            cmp   = None 
            if isinstance ( categories , dict ) : cmp = categories [ label ]
            else :
                for ii in categories :
                    if ii[0] == label :
                        cmp = ii[1]
                        break

            if not isinstance  ( cmp , PDF  ) :
                raise TypeError ( 'Can not find the proper category component: "%s"' % label ) 
            
            self.__categories [ label ] = cmp
            _xv = cmp.xvar
            
        sim_pdf     = PDF ( self.name , xvar = _xv )
        sim_pdf.pdf = ROOT.RooSimultaneous ( 'Sim_' + self.name , title , self.sample )
        
        keys = self.categories.keys()
        for key in sorted ( keys ) :
            sim_pdf.pdf.addPdf ( self.categories[key].pdf , key )

        self.__pdf = sim_pdf 
        
        for k , cmp in items_loop ( self.categories ) :
            
            for c in cmp.signals      : self.pdf.signals    .add ( c ) 
            for c in cmp.backgrounds  : self.pdf.backgrounds.add ( c ) 
            for c in cmp.crossterms1  : self.pdf.crossterms1.add ( c ) 
            for c in cmp.crossterms2  : self.pdf.crossterms2.add ( c )
            
            self.pdf.draw_options.update ( cmp.draw_options )
            
        # =====================================================================
        ##  drawing helpers
        # =====================================================================
        
        self.__drawpdfs   = {}
        for key in sorted ( keys ) :

            cmp = self.categories [ key ] 
            if isinstance  ( cmp , PDF3 ) :
                from ostap.fitting.fit3d import Generic3D_pdf                
                dpdf = Generic3D_pdf ( sim_pdf.pdf ,
                                       cmp.xvar    ,
                                       cmp.yvar    ,
                                       cmp.zvar    ,
                                       add_to_signals = False )
            elif isinstance  ( cmp , PDF2 ) :
                from ostap.fitting.fit2d import Generic2D_pdf                                
                dpdf = Generic2D_pdf ( sim_pdf.pdf ,
                                       cmp.xvar    ,
                                       cmp.yvar    ,
                                       add_to_signals = False )
            elif isinstance  ( cmp , PDF  ) :
                from ostap.fitting.basic import Generic1D_pdf   
                dpdf = Generic1D_pdf ( sim_pdf.pdf ,
                                       cmp.xvar    ,
                                       add_to_signals = False )
                
            for c in cmp.signals     : dpdf.signals    .add ( c ) 
            for c in cmp.backgrounds : dpdf.backgrounds.add ( c ) 
            for c in cmp.crossterms1 : dpdf.crossterms1.add ( c ) 
            for c in cmp.crossterms2 : dpdf.crossterms2.add ( c )


            dpdf.draw_options.update ( cmp.draw_options )
                        
            self.__drawpdfs [ key ]  = dpdf

        self.config = {
            'name'       : self.name       ,
            'title'      : title           ,            
            'sample'     : self.sample     ,
            'categories' : self.categories ,
            }