def select_keys(origin, keys, transform=lambda s: s, **kwargs): """Select from mapping object <code>origin</code> the "interesting" keys. The keys, that result in the same <code>transform(key)</code> value are considered as non-distiguishable >>> dct = { ... } >>> res = select_keys ( dct , ( 'a' , 'b' ) , transform = lambda s : s.lower() ) """ selected = cidict(transform=transform) ## transformed keys kt = set([selected.the_key(k) for k in keys]) from ostap.core.core import items_loop for k, value in items_loop(origin): kk = selected.the_key(k) if kk in kt: selected[kk] = value origin.pop(k) for k, value in items_loop(kwargs): kk = selected.the_key(k) if kk in kt: selected[kk] = value return selected
def draw_options(**kwargs): """Collect predefined keys from the dictionary >>> def somefunc ( ... , **kwargs ) : ... draw_opts = draw_options ( kwargs ) """ options = {} for k, v in items_loop(kwargs): if k.lower() in keys: options[k.lower()] = v if k.lower() in ('draw', 'draw_option', 'draw_options'): if isinstance(v, dict): options.update(v) return options
def draw_options(**kwargs): """Collect predefined keys from the dictionary >>> def somefunc ( ... , **kwargs ) : ... draw_opts = draw_options ( kwargs ) """ options = {} for k, v in items_loop(kwargs): for key in keys: if key_compare(k, key): options[k] = v return options
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 , }
def __init__ ( self , sample , categories , xvar = None , name = None , title = '' ) : warnings.warn("Usage of obsolete Sim1D. Use SimFit instead") 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() ) self.__sample = sample self.__categories = {} # ===================================================================== ## components # ===================================================================== labels = sample.labels() _xvar = xvar 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 isinstance ( cmp , PDF ) : _xv = cmp.xvar if _xvar and not ( _xvar is _xv ) : self.error('Mismatch in XVAR!') elif not _xvar : _xvar = _xv self.__categories [ label ] = cmp elif isinstance ( cmp , ROOT.RooAbsPdf ) and _xvar : self.__categories [ label ] = Generic1D_pdf ( pdf = cmp , cmp = _xvar ) else : raise TypeError ( 'Can not find the category "%s"' % label ) # ===================================================================== assert _xvar, 'Unable to define "xvar"' ## initialize the base PDF.__init__ ( self , name , xvar = _xvar ) self.pdf = ROOT.RooSimultaneous ( 'sim_' + self.name , title , self.sample ) keys = self.categories.keys() for key in sorted ( keys ) : self.pdf.addPdf ( self.categories[key].pdf , key ) for k , pdf in items_loop ( self.categories ) : for c in pdf.signals : self.signals .add ( c ) for c in pdf.backgrounds : self.backgrounds.add ( c ) for c in pdf.crossterms1 : self.crossterms1.add ( c ) for c in pdf.crossterms2 : self.crossterms2.add ( c ) ## for c in pdf.alist1 : self.alist1.add ( c ) ## for c in pdf.alist2 : self.alist2.add ( c ) self.config = { 'name' : self.name , 'title' : title , 'sample' : self.sample , 'categories' : self.categories , 'xvar' : self.xvar , }