def Add(self, other):
     if self.__outputtype == "TDirectoryFile":
         othercontent = other.content()
         logging.info("self scaled %s, has content: %s" %
                      (self.name(), "Yes" if self.hascontent() else "No"))
         logging.info("other scaled %s, has content: %s" %
                      (other.name(), "Yes" if other.hascontent() else "No"))
         for c in self.__content.GetListOfKeys():
             for o in othercontent.GetListOfKeys():
                 if c.GetName() == o.GetName():
                     others = TList()
                     others.append(o.ReadObj())
                     c.ReadObj().Merge(others)
     else:
         others = TList()
         others.append(other.content())
         self.__content.Merge(others)
Example #2
0
   def test2Lists( self ):
      """Test list/TList behavior and compatibility"""

      l = TList()
      l.Add( TObjString('a') )
      l.Add( TObjString('b') )
      l.Add( TObjString('c') )
      l.Add( TObjString('d') )
      l.Add( TObjString('e') )
      l.Add( TObjString('f') )
      l.Add( TObjString('g') )
      l.Add( TObjString('h') )
      l.Add( TObjString('i') )
      l.Add( TObjString('j') )

      self.assertEqual( len(l), 10 )
      self.assertEqual( l[3], 'd' )
      self.assertEqual( l[-1], 'j' )
      self.assertRaises( IndexError, l.__getitem__,  20 )
      self.assertRaises( IndexError, l.__getitem__, -20 )

      self.assertEqual( list(l), ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'] )

      l[3] = TObjString('z')
      self.assertEqual( list(l), ['a', 'b', 'c', 'z', 'e', 'f', 'g', 'h', 'i', 'j'] )

      del l[2]
      self.assertEqual( list(l), ['a', 'b', 'z', 'e', 'f', 'g', 'h', 'i', 'j'] )

      self.assert_( TObjString('b') in l )
      self.assert_( not TObjString('x') in l )

      self.assertEqual( list(l[2:6]),   ['z', 'e', 'f', 'g'] )
      self.assertEqual( list(l[2:6:2]), ['z', 'f'] )
      self.assertEqual( list(l[-5:-2]), ['f', 'g', 'h'] )
      self.assertEqual( list(l[7:]),    ['i', 'j'] )
      self.assertEqual( list(l[:3]),    ['a', 'b', 'z'] )

      del l[2:4]
      self.assertEqual( list(l), ['a', 'b', 'f', 'g', 'h', 'i', 'j'] )

      l[2:5] = [ TObjString('1'), TObjString('2') ]
      self.assertEqual( list(l), ['a', 'b', '1', '2', 'i', 'j'] )

      l[6:6] = [ TObjString('3') ]
      self.assertEqual( list(l), ['a', 'b', '1', '2', 'i', 'j', '3'] )

      l.append( TObjString('4') )
      self.assertEqual( list(l), ['a', 'b', '1', '2', 'i', 'j', '3', '4'] )

      l.extend( [ TObjString('5'), TObjString('j') ] )
      self.assertEqual( list(l), ['a', 'b', '1', '2', 'i', 'j', '3', '4', '5', 'j'] )
      self.assertEqual( l.count( 'b' ), 1 )
      self.assertEqual( l.count( 'j' ), 2 )
      self.assertEqual( l.count( 'x' ), 0 )

      self.assertEqual( l.index( TObjString( 'i' ) ), 4 )
      self.assertRaises( ValueError, l.index, TObjString( 'x' ) )

      l.insert(  3, TObjString('6') )
      l.insert( 20, TObjString('7') )
      l.insert( -1, TObjString('8') )
      self.assertEqual( list(l), ['8', 'a', 'b', '1', '6', '2', 'i', 'j', '3', '4', '5', 'j', '7'] )
      self.assertEqual( l.pop(), '7' )
      self.assertEqual( l.pop(3), '1' )
      self.assertEqual( list(l), ['8', 'a', 'b', '6', '2', 'i', 'j', '3', '4', '5', 'j'] )

      l.remove( TObjString( 'j' ) )
      l.remove( TObjString( '3' ) )

      self.assertRaises( ValueError, l.remove, TObjString( 'x' ) )
      self.assertEqual( list(l), ['8', 'a', 'b', '6', '2', 'i', '4', '5', 'j'] )

      l.reverse()
      self.assertEqual( list(l), ['j', '5', '4', 'i', '2', '6', 'b', 'a', '8'] )

      l.sort()
      self.assertEqual( list(l), ['2', '4', '5', '6', '8', 'a', 'b', 'i', 'j'] )

      if sys.hexversion >= 0x3000000:
         l.sort( key=TObjString.GetName )
         l.reverse()
      else:
         l.sort( lambda a, b: cmp(b.GetName(),a.GetName()) )
      self.assertEqual( list(l), ['j', 'i', 'b', 'a', '8', '6', '5', '4', '2'] )

      l2 = l[:3]
      self.assertEqual( list(l2 * 3), ['j', 'i', 'b', 'j', 'i', 'b', 'j', 'i', 'b'] )
      self.assertEqual( list(3 * l2), ['j', 'i', 'b', 'j', 'i', 'b', 'j', 'i', 'b'] )

      l2 *= 3
      self.assertEqual( list(l2), ['j', 'i', 'b', 'j', 'i', 'b', 'j', 'i', 'b'] )

      l2 = l[:3]
      l3 = l[6:8]
      self.assertEqual( list(l2+l3), ['j', 'i', 'b', '5', '4'] )

      i = iter(l2)
      self.assertEqual( i.next(), 'j' )
      self.assertEqual( i.next(), 'i' )
      self.assertEqual( i.next(), 'b' )
      self.assertRaises( StopIteration, i.next )
Example #3
0
    def test2Lists(self):
        """Test list/TList behavior and compatibility"""

        l = TList()
        l.Add(TObjString('a'))
        l.Add(TObjString('b'))
        l.Add(TObjString('c'))
        l.Add(TObjString('d'))
        l.Add(TObjString('e'))
        l.Add(TObjString('f'))
        l.Add(TObjString('g'))
        l.Add(TObjString('h'))
        l.Add(TObjString('i'))
        l.Add(TObjString('j'))

        self.assertEqual(len(l), 10)
        self.assertEqual(l[3], 'd')
        self.assertEqual(l[-1], 'j')
        self.assertRaises(IndexError, l.__getitem__, 20)
        self.assertRaises(IndexError, l.__getitem__, -20)

        self.assertEqual(list(l),
                         ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'])

        l[3] = TObjString('z')
        self.assertEqual(list(l),
                         ['a', 'b', 'c', 'z', 'e', 'f', 'g', 'h', 'i', 'j'])

        del l[2]
        self.assertEqual(list(l),
                         ['a', 'b', 'z', 'e', 'f', 'g', 'h', 'i', 'j'])

        self.assert_(TObjString('b') in l)
        self.assert_(not TObjString('x') in l)

        self.assertEqual(list(l[2:6]), ['z', 'e', 'f', 'g'])
        self.assertEqual(list(l[2:6:2]), ['z', 'f'])
        self.assertEqual(list(l[-5:-2]), ['f', 'g', 'h'])
        self.assertEqual(list(l[7:]), ['i', 'j'])
        self.assertEqual(list(l[:3]), ['a', 'b', 'z'])

        del l[2:4]
        self.assertEqual(list(l), ['a', 'b', 'f', 'g', 'h', 'i', 'j'])

        l[2:5] = [TObjString('1'), TObjString('2')]
        self.assertEqual(list(l), ['a', 'b', '1', '2', 'i', 'j'])

        l[6:6] = [TObjString('3')]
        self.assertEqual(list(l), ['a', 'b', '1', '2', 'i', 'j', '3'])

        l.append(TObjString('4'))
        self.assertEqual(list(l), ['a', 'b', '1', '2', 'i', 'j', '3', '4'])

        l.extend([TObjString('5'), TObjString('j')])
        self.assertEqual(list(l),
                         ['a', 'b', '1', '2', 'i', 'j', '3', '4', '5', 'j'])
        self.assertEqual(l.count('b'), 1)
        self.assertEqual(l.count('j'), 2)
        self.assertEqual(l.count('x'), 0)

        self.assertEqual(l.index(TObjString('i')), 4)
        self.assertRaises(ValueError, l.index, TObjString('x'))

        l.insert(3, TObjString('6'))
        l.insert(20, TObjString('7'))
        l.insert(-1, TObjString('8'))
        if not self.legacy_pyroot:
            # The pythonisation of TSeqCollection in experimental PyROOT mimics the
            # behaviour of the Python list, in this case for insert.
            # The Python list insert always inserts before the specified index, so if
            # -1 is specified, insert will place the new element right before the last
            # element of the list.
            self.assertEqual(list(l), [
                'a', 'b', '1', '6', '2', 'i', 'j', '3', '4', '5', 'j', '8', '7'
            ])
            # Re-synchronize with current PyROOT's list
            l.insert(0, TObjString('8'))
            self.assertEqual(list(l), [
                '8', 'a', 'b', '1', '6', '2', 'i', 'j', '3', '4', '5', 'j',
                '8', '7'
            ])
            l.pop(-2)
            self.assertEqual(list(l), [
                '8', 'a', 'b', '1', '6', '2', 'i', 'j', '3', '4', '5', 'j', '7'
            ])
        else:
            self.assertEqual(list(l), [
                '8', 'a', 'b', '1', '6', '2', 'i', 'j', '3', '4', '5', 'j', '7'
            ])
        self.assertEqual(l.pop(), '7')
        self.assertEqual(l.pop(3), '1')
        self.assertEqual(
            list(l), ['8', 'a', 'b', '6', '2', 'i', 'j', '3', '4', '5', 'j'])

        l.remove(TObjString('j'))
        l.remove(TObjString('3'))

        self.assertRaises(ValueError, l.remove, TObjString('x'))
        self.assertEqual(list(l),
                         ['8', 'a', 'b', '6', '2', 'i', '4', '5', 'j'])

        l.reverse()
        self.assertEqual(list(l),
                         ['j', '5', '4', 'i', '2', '6', 'b', 'a', '8'])

        l.sort()
        self.assertEqual(list(l),
                         ['2', '4', '5', '6', '8', 'a', 'b', 'i', 'j'])

        if sys.hexversion >= 0x3000000:
            l.sort(key=TObjString.GetName)
            l.reverse()
        else:
            l.sort(lambda a, b: cmp(b.GetName(), a.GetName()))
        self.assertEqual(list(l),
                         ['j', 'i', 'b', 'a', '8', '6', '5', '4', '2'])

        l2 = l[:3]
        self.assertEqual(list(l2 * 3),
                         ['j', 'i', 'b', 'j', 'i', 'b', 'j', 'i', 'b'])
        self.assertEqual(list(3 * l2),
                         ['j', 'i', 'b', 'j', 'i', 'b', 'j', 'i', 'b'])

        l2 *= 3
        self.assertEqual(list(l2),
                         ['j', 'i', 'b', 'j', 'i', 'b', 'j', 'i', 'b'])

        l2 = l[:3]
        l3 = l[6:8]
        self.assertEqual(list(l2 + l3), ['j', 'i', 'b', '5', '4'])

        if sys.hexversion >= 0x3000000:
            next = '__next__'
        else:
            next = 'next'
        i = iter(l2)
        self.assertEqual(getattr(i, next)(), 'j')
        self.assertEqual(getattr(i, next)(), 'i')
        self.assertEqual(getattr(i, next)(), 'b')
        self.assertRaises(StopIteration, getattr(i, next))
Example #4
0
def init_pdfs(fit, params, prefix, signal):
    '''
    Initialize RooFit PDFs from parameter dictionary

    :param MassFit fit: - MassFit instance
    :param dict params: - parameter dictionary
    :param str prefix: - pdf name prefix
    :param bool signal: - signal pdf
    :return: (pdf, components) with pdf - Composit pdf, components - list of component pdfs
    :rtype: tuple
    '''
    # Build recursive composit pdf:
    # M(x) = f_sig*S(x) + (1-f_sig){f_peak*P(x) + (1-f_peak)*B(x)}
    #
    # This avoids problems with sum(fractions) > 1
    # See chapter 3 of RooFit Users Manual
    #
    # Store all auto generated obejcts in TLists. Otherwise python will destroy
    # them as soon as this function ends

    _pdf = None  #: Composit PDF to return
    _components = TList()  #: List of component PDFs
    _components_all = TList()  #: List of component PDFs
    _fracs = TList()  #: List of fractions
    _funcs = TList()  #: List of formula variables
    _params = { prefix+"_"+fit.mass_name: fit.mass }
    _i = 1
    _n_comps = len(params["components"])
    for _comp in params["components"]:
        # Create parameters
        _comp_params = TList()
        _comp_constrs = TList()
        for _par in _comp["params"]:
            _name = prefix + "_" + _par["name"]
            if _name in _params:
                _comp_params.append(_params[_name])
            else:
                if "random" in _par and _par["random"] == True:
                    # Generate starting value and validity range
                    _min = random.uniform(_par["min"], _par["value"])
                    _max = random.uniform(_par["value"], _par["max"])
                    _value = random.uniform(_min, _max)
                    _par["random"] = False
                    # Store obtained values
                    _par["min"] = _min
                    _par["max"] = _max
                    _par["value"] = _value
                    _var = RooRealVar(
                            _name, _par["title"], _value,
                            _min, _max, _par["units"]
                            )
                else:
                    _var = RooRealVar(
                            _name, _par["title"], _par["value"],
                            _par["min"], _par["max"], _par["units"]
                            )
                fit.parameters.push_back(_var)
                _comp_params.Add(_var)
                _params[_name] = _var
                if "constraint" in _par:
                    _constr = RooGaussian(
                            _name+"_constr",
                            _par["title"]+"_constr",
                            _var,
                            RooFit.RooConst(_par["constraint"]["mean"]),
                            RooFit.RooConst(_par["constraint"]["sigma"])
                            )
                    fit.constraints.push_back(_constr)
                    _comp_constrs.Add(_constr)
        # Store mean and sigma of core part of the signal PDF
        if _i == 1 and signal == True:
            fit.sig_mass = _comp_params.At(0)
            fit.sig_sigma = _comp_params.At(1)
        _name = prefix+"_"+_comp["name"]
        # Initialize component
        if _comp["type"] not in supported_pdfs:
            raise Exception("Unknown PDF type: %s" % _comp["type"])
        if _comp["type"] == "RooAddPdf":
            (_sum_pdf, _sum_components, _sum_params) = init_pdfs(fit, _comp, _comp['name'], signal)
            _components_all.AddAll(_sum_components)
            _components.Add(_sum_pdf)
            _params.update(_sum_params)
        elif _comp["type"] == "RooGaussian":
            _components.Add(RooGaussian(
                    _name,
                    _comp["title"],
                    fit.mass,
                    _comp_params.At(0),
                    _comp_params.At(1))
                    )
        elif _comp["type"] == "RooParamGaussian":
            _v = RooFormulaVar(
                    _name+"_variable",
                    "",
                    "@0*@1",
                    RooArgList(
                        _comp_params.At(1),
                        _comp_params.At(2))
                    )
            _funcs.Add(_v)
            _components.Add(RooGaussian(
                    _name,
                    _comp["title"],
                    fit.mass,
                    _comp_params.At(0),
                    _v)
                    )
        elif _comp["type"] == "RooParamGaussianOffset":
            _m = RooFormulaVar(
                    _name+"_var_mean",
                    "",
                    "@0+@1",
                    RooArgList(
                        _comp_params.At(0),
                        _comp_params.At(1))
                    )
            _s = RooFormulaVar(
                    _name+"_var_sigma",
                    "",
                    "@0*@1",
                    RooArgList(
                        _comp_params.At(2),
                        _comp_params.At(3))
                    )
            _funcs.Add(_m)
            _funcs.Add(_s)
            _components.Add(RooGaussian(
                    _name,
                    _comp["title"],
                    fit.mass,
                    _m,
                    _s)
                    )
        elif _comp["type"] == "RooPolynomial":
            if len(_comp_params):
                _components.Add(RooPolynomial(
                        _name,
                        _comp["title"],
                        fit.mass,
                        RooArgList(_comp_params))
                        )
            else:
                _components.Add(RooPolynomial(
                        _name,
                        _comp["title"],
                        fit.mass)
                        )
        elif _comp["type"] == "RooChebychev":
            _components.Add(RooChebychev(
                    _name,
                    _comp["title"],
                    fit.mass,
                    RooArgList(_comp_params))
                    )
        elif _comp["type"] == "RooExponential":
            _components.Add(RooExponential(
                    _name,
                    _comp["title"],
                    fit.mass,
                    _comp_params.At(0))
                    )
        elif _comp["type"] == "RooVoigtian":
            _components.Add(RooVoigtian(
                    _name,
                    _comp["title"],
                    fit.mass,
                    _comp_params.At(0),
                    _comp_params.At(1),
                    _comp_params.At(2))
                    )
        elif _comp["type"] == "RooCBShape":
            _components.Add(RooCBShape(
                    _name,
                    _comp["title"],
                    fit.mass,
                    _comp_params.At(0),
                    _comp_params.At(1),
                    _comp_params.At(2),
                    _comp_params.At(3))
                    )
        elif _comp["type"] == "RooParamCBShape":
            _v = RooFormulaVar(
                    _name+"_variable",
                    "",
                    "@0*@1",
                    RooArgList(
                        _comp_params.At(1),
                        _comp_params.At(2))
                    )
            _funcs.Add(_v)
            _components.Add(RooCBShape(
                    _name,
                    _comp["title"],
                    fit.mass,
                    _comp_params.At(0),
                    _v,
                    _comp_params.At(3),
                    _comp_params.At(4))
                    )
        elif _comp["type"] == "RooDoubleCBShape":
            _components.Add(RooDoubleCBShape(
                    _name,
                    _comp["title"],
                    fit.mass,
                    _comp_params.At(0),
                    _comp_params.At(1),
                    _comp_params.At(2),
                    _comp_params.At(3),
                    _comp_params.At(4),
                    _comp_params.At(5))
                    )
        elif _comp["type"] == "RooCutCBShape":
            _components.Add(RooCutCBShape(
                    _name,
                    _comp["title"],
                    fit.mass,
                    _comp_params.At(0),
                    _comp_params.At(1),
                    _comp_params.At(2),
                    _comp_params.At(3),
                    _comp_params.At(4))
                    )
        elif _comp["type"] == "RooArgusBG":
            _components.Add(RooArgusBG(
                    _name,
                    _comp["title"],
                    fit.mass,
                    _comp_params.At(0),
                    _comp_params.At(1))
                    )
        elif _comp["type"] == "RooRArgusBG":
            _v = RooFormulaVar(
                    _name+"_variable",
                    "",
                    "2*@0 - @1",
                    RooArgList(
                        _comp_params.At(0),
                        fit.mass)
                    )
            _funcs.Add(_v)
            _components.Add(RooArgusBG(
                    _name,
                    _comp["title"],
                    _v,
                    _comp_params.At(0),
                    _comp_params.At(1))
                    )
        elif _comp["type"] == "RooIpatia":
            _components.Add(RooIpatia(
                    _name,
                    _comp["title"],
                    fit.mass,
                    _comp_params.At(0),
                    _comp_params.At(1),
                    _comp_params.At(2),
                    _comp_params.At(3),
                    _comp_params.At(4),
                    _comp_params.At(5),
                    _comp_params.At(6))
                    )
        elif _comp["type"] == "RooIpatia2":
            _components.Add(RooIpatia2(
                    _name,
                    _comp["title"],
                    fit.mass,
                    _comp_params.At(0),
                    _comp_params.At(1),
                    _comp_params.At(2),
                    _comp_params.At(3),
                    _comp_params.At(4),
                    _comp_params.At(5),
                    _comp_params.At(6),
                    _comp_params.At(7),
                    _comp_params.At(8))
                    )
        elif _comp["type"] == "RooCassandra":
            _components.Add(RooCassandra(
                    _name,
                    _comp["title"],
                    fit.mass,
                    _comp_params.At(0),
                    _comp_params.At(1),
                    _comp_params.At(2),
                    _comp_params.At(3))
                    )
        elif _comp["type"] == "RooAsymCassandra":
            _components.Add(RooAsymCassandra(
                    _name,
                    _comp["title"],
                    fit.mass,
                    _comp_params.At(0),
                    _comp_params.At(1),
                    _comp_params.At(2),
                    _comp_params.At(3),
                    _comp_params.At(4))
                    )
        elif _comp["type"] == "RooDecay":
            _r = RooGaussModel(
                    _name+"_resolution",
                    _comp["title"]+"_resolution",
                    fit.mass,
                    _comp_params.At(0),
                    _comp_params.At(2)
                    )
            _components.Add(RooDecay(
                _name,
                _comp["title"],
                fit.mass,
                _comp_params.At(1),
                _r,
                RooDecay.DoubleSided)
                )
            _funcs.Add(_r)
        elif _comp["type"] == "RooDecayLeft":
            _r = RooGaussModel(
                    _name+"_resolution",
                    _comp["title"]+"_resolution",
                    fit.mass,
                    _comp_params.At(0),
                    _comp_params.At(2)
                    )
            _components.Add(RooDecay(
                _name,
                _comp["title"],
                fit.mass,
                _comp_params.At(1),
                _r,
                RooDecay.Flipped)
                )
            _funcs.Add(_r)
        elif _comp["type"] == "RooDecayRight":
            _r = RooGaussModel(
                    _name+"_resolution",
                    _comp["title"]+"_resolution",
                    fit.mass,
                    _comp_params.At(0),
                    _comp_params.At(2)
                    )
            _components.Add(RooDecay(
                _name,
                _comp["title"],
                fit.mass,
                _comp_params.At(1),
                _r,
                RooDecay.SingleSided)
                )
            _funcs.Add(_r)
        elif _comp["type"] == "RooExpGaussExp":
            _components.Add(RooExpGaussExp(
                    _name,
                    _comp["title"],
                    fit.mass,
                    _comp_params.At(0),
                    _comp_params.At(1),
                    _comp_params.At(2),
                    _comp_params.At(3))
                    )
        elif _comp["type"] == "RooHistPdf":
            if 'config' not in _comp:
                raise Exception("Missing 'config' section for RooHistPdf.")
            if 'file' not in _comp['config']:
                raise Exception("Missing 'file' key in the 'config' section for RooHistPdf.")
            if 'name' not in _comp['config']:
                raise Exception("Missing 'name' key in the 'config' section for RooHistPdf.")
            _f = TFile(_comp['config']['file'])
            _pdf = _f.Get(_comp['config']['name'])
            _components.Add(_pdf)
        elif _comp["type"] == "Conv_RooDecay_RooBreitWigner":
            _r1 = RooGaussModel(
                    _name+"_resolution_1",
                    _comp["title"]+"_resolution_1",
                    fit.mass,
                    _comp_params.At(0),
                    _comp_params.At(2)
                    )
            _f1 = RooDecay(
                _name+"_comp_1",
                _comp["title"]+"_comp_1",
                fit.mass,
                _comp_params.At(1),
                _r1,
                RooDecay.DoubleSided
                )
            _f2 = RooBreitWigner(
                _name+"_comp_2",
                _comp["title"]+"_comp_2",
                fit.mass,
                _comp_params.At(3),
                _comp_params.At(4)
                )
            _funcs.Add(_r1)
            _funcs.Add(_f1)
            _funcs.Add(_f2)
            _pdf = RooNumConvPdf(
                _name,
                _comp["title"],
                fit.mass,
                _f1,
                _f2
                )
            _components.Add(_pdf)
        elif _comp["type"] == "FFT_RooDecay_RooBreitWigner":
            # Define sampling frequency
            fit.mass.setBins(10000,"fft") ;
            _r1 = RooGaussModel(
                    _name+"_resolution_1",
                    _comp["title"]+"_resolution_1",
                    fit.mass,
                    _comp_params.At(0),
                    _comp_params.At(2)
                    )
            _f1 = RooDecay(
                _name+"_comp_1",
                _comp["title"]+"_comp_1",
                fit.mass,
                _comp_params.At(1),
                _r1,
                RooDecay.DoubleSided
                )
            _f2 = RooBreitWigner(
                _name+"_comp_2",
                _comp["title"]+"_comp_2",
                fit.mass,
                _comp_params.At(3),
                _comp_params.At(4)
                )
            _funcs.Add(_r1)
            _funcs.Add(_f1)
            _funcs.Add(_f2)
            _pdf = RooFFTConvPdf(
                _name,
                _comp["title"],
                fit.mass,
                _f1,
                _f2
                )
            _pdf.setBufferFraction(50.0)
            _components.Add(_pdf)
        elif _comp["type"] == "FFT_RooDecay_RooCBShape":
            # Define sampling frequency
            fit.mass.setBins(10000,"fft") ;
            _r1 = RooGaussModel(
                    _name+"_resolution_1",
                    _comp["title"]+"_resolution_1",
                    fit.mass,
                    _comp_params.At(0),
                    _comp_params.At(2)
                    )
            _f1 = RooDecay(
                _name+"_comp_1",
                _comp["title"]+"_comp_1",
                fit.mass,
                _comp_params.At(1),
                _r1,
                RooDecay.DoubleSided
                )
            _f2 = RooCBShape(
                _name+"_comp_2",
                _comp["title"]+"_comp_2",
                fit.mass,
                _comp_params.At(3),
                _comp_params.At(4),
                _comp_params.At(5),
                _comp_params.At(6)
                )
            _funcs.Add(_r1)
            _funcs.Add(_f1)
            _funcs.Add(_f2)
            _pdf = RooFFTConvPdf(
                _name,
                _comp["title"],
                fit.mass,
                _f1,
                _f2
                )
            _pdf.setBufferFraction(50.0)
            _components.Add(_pdf)
        elif _comp["type"] == "FFT_RooCassandra_RooGaussian":
            # Define sampling frequency
            fit.mass.setBins(10000,"fft") ;
            _f1 = RooCassandra(
                _name+"_comp_1",
                _comp["title"]+"_comp_1",
                fit.mass,
                _comp_params.At(0),
                _comp_params.At(1),
                _comp_params.At(2)
                )
            _f2 = RooGaussian(
                _name+"_comp_2",
                _comp["title"]+"_comp_2",
                fit.mass,
                _comp_params.At(3),
                _comp_params.At(4)
                )
            _funcs.Add(_f1)
            _funcs.Add(_f2)
            _pdf = RooFFTConvPdf(
                _name,
                _comp["title"],
                fit.mass,
                _f1,
                _f2
                )
            _pdf.setBufferFraction(50.0)
            _components.Add(_pdf)
        elif _comp["type"] == "FFT_RooAsymCassandra_RooGaussian":
            # Define sampling frequency
            fit.mass.setBins(10000,"fft") ;
            _f1 = RooAsymCassandra(
                _name+"_comp_1",
                _comp["title"]+"_comp_1",
                fit.mass,
                _comp_params.At(0),
                _comp_params.At(1),
                _comp_params.At(2),
                _comp_params.At(3),
                _comp_params.At(4)
                )
            _f2 = RooGaussian(
                _name+"_comp_2",
                _comp["title"]+"_comp_2",
                fit.mass,
                _comp_params.At(5),
                _comp_params.At(6)
                )
            _funcs.Add(_f1)
            _funcs.Add(_f2)
            #_pdf = RooFFTConvPdf(
            #    _name,
            #    _comp["title"],
            #    fit.mass,
            #    _f1,
            #    _f2
            #    )
            _pdf = RooFFTConvPdf(
                _name,
                _comp["title"],
                fit.mass,
                _f2,
                _f1
                )
            _pdf.setBufferFraction(50.0)
            _components.Add(_pdf)
        elif _comp["type"] == "FFT_RooAsymCassandra_RooDoubleGaussian":
            # Define sampling frequency
            fit.mass.setBins(10000,"fft") ;
            _v = RooFormulaVar(
                    _name+"_variable",
                    "",
                    "@0*@1",
                    RooArgList(
                        _comp_params.At(6),
                        _comp_params.At(7))
                    )
            _funcs.Add(_v)
            _f1 = RooAsymCassandra(
                _name+"_comp_1",
                _comp["title"]+"_comp_1",
                fit.mass,
                _comp_params.At(0),
                _comp_params.At(1),
                _comp_params.At(2),
                _comp_params.At(3),
                _comp_params.At(4)
                )
            _f2 = RooGaussian(
                _name+"_comp_2",
                _comp["title"]+"_comp_2",
                fit.mass,
                _comp_params.At(5),
                _comp_params.At(6)
                )
            _f3 = RooGaussian(
                _name+"_comp_3",
                _comp["title"]+"_comp_3",
                fit.mass,
                _comp_params.At(5),
                _v
                )
            _f4 = RooAddPdf(
               _name,
               _comp["title"],
               RooArgList(_f2, _f3),
               RooArgList(_comp_params.At(8))
               )
            _funcs.Add(_f1)
            _funcs.Add(_f2)
            _funcs.Add(_f3)
            _funcs.Add(_f4)
            _pdf = RooFFTConvPdf(
                _name,
                _comp["title"],
                fit.mass,
                _f4,
                _f1
                )
            _pdf.setBufferFraction(50.0)
            _components.Add(_pdf)
        elif _comp["type"] == "Conv_RooAsymCassandra_RooGaussian":
            _f1 = RooAsymCassandra(
                _name+"_comp_1",
                _comp["title"]+"_comp_1",
                fit.mass,
                _comp_params.At(0),
                _comp_params.At(1),
                _comp_params.At(2),
                _comp_params.At(3),
                _comp_params.At(4)
                )
            _f2 = RooGaussian(
                _name+"_comp_2",
                _comp["title"]+"_comp_2",
                fit.mass,
                _comp_params.At(5),
                _comp_params.At(6)
                )
            _funcs.Add(_f1)
            _funcs.Add(_f2)
            _pdf = RooNumConvPdf(
                _name,
                _comp["title"],
                fit.mass,
                _f2,
                _f1
                )
            # Does not work
            #_pdf.setConvolutionWindow(_comp_params.At(5), _comp_params.At(6), 6.0)
            _components.Add(_pdf)
        elif _comp["type"] == "FFT_RooAsymCassandra3_RooGaussian":
            # Define sampling frequency
            fit.mass.setBins(10000,"fft") ;
            _f1 = RooAsymCassandra3(
                _name+"_comp_1",
                _comp["title"]+"_comp_1",
                fit.mass,
                _comp_params.At(0),
                _comp_params.At(1),
                _comp_params.At(2),
                _comp_params.At(3),
                _comp_params.At(4),
                _comp_params.At(5),
                _comp_params.At(6)
                )
            _f2 = RooGaussian(
                _name+"_comp_2",
                _comp["title"]+"_comp_2",
                fit.mass,
                _comp_params.At(7),
                _comp_params.At(8)
                )
            _funcs.Add(_f1)
            _funcs.Add(_f2)
            _pdf = RooFFTConvPdf(
                _name,
                _comp["title"],
                fit.mass,
                _f2,
                _f1
                )
            _pdf.setBufferFraction(50.0)
            _components.Add(_pdf)
        elif _comp["type"] == "FFT_RooCassandra_RooCBShape":
            # Define sampling frequency
            fit.mass.setBins(50000,"fft") ;
            _f1 = RooCassandra(
                _name+"_comp_1",
                _comp["title"]+"_comp_1",
                fit.mass,
                _comp_params.At(0),
                _comp_params.At(1),
                _comp_params.At(2)
                )
            _f2 = RooCBShape(
                _name+"_comp_2",
                _comp["title"]+"_comp_2",
                fit.mass,
                _comp_params.At(3),
                _comp_params.At(4),
                _comp_params.At(5),
                _comp_params.At(6)
                )
            _funcs.Add(_f1)
            _funcs.Add(_f2)
            _pdf = RooFFTConvPdf(
                _name,
                _comp["title"],
                fit.mass,
                _f1,
                _f2
                )
            _pdf.setBufferFraction(100.0)
            _components.Add(_pdf)
        elif _comp["type"] == "FFT_RooCassandra3_RooCBShape":
            # Define sampling frequency
            fit.mass.setBins(50000,"fft") ;
            _f1 = RooCassandra3(
                _name+"_comp_1",
                _comp["title"]+"_comp_1",
                fit.mass,
                _comp_params.At(0),
                _comp_params.At(1),
                _comp_params.At(2),
                _comp_params.At(3)
                )
            _f2 = RooCBShape(
                _name+"_comp_2",
                _comp["title"]+"_comp_2",
                fit.mass,
                _comp_params.At(4),
                _comp_params.At(5),
                _comp_params.At(6),
                _comp_params.At(7)
                )
            _funcs.Add(_f1)
            _funcs.Add(_f2)
            _pdf = RooFFTConvPdf(
                _name,
                _comp["title"],
                fit.mass,
                _f1,
                _f2
                )
            _pdf.setBufferFraction(100.0)
            _components.Add(_pdf)
        elif _comp["type"] == "FFT_RooHistPdf_RooGaussian":
            # Load reference ROOT tree
            _ref_full_ch = TChain(_comp['config']['tree'])
            _ref_full_ch.Add(_comp['config']['files'])
            # Run selection
            _ref_ch = _ref_full_ch.CopyTree(_comp['config']['selection'])
            _bins = fit.mass.getBins()
            fit.mass.setBins(_comp['config']['bins'])
            # Create hist PDF
            _data = RooDataSet(
                    'ds_' + _name,
                    '',
                    RooArgSet(fit.mass),
                    RooFit.Import(_ref_ch)
                    )
            _hist = _data.binnedClone()
            _f1 = RooHistPdf(
                    name+'_histpdf',
                    _comp['title']+'_histpdf',
                    RooArgSet(fit.mass),
                    _hist,
                    2  # Order of interpolation function
                    )
            fit.mass.setBins(_bins)

            # Define sampling frequency
            fit.mass.setBins(10000,"fft") ;
            _f2 = RooGaussModel(
                _name+"_comp_2",
                _comp["title"]+"_comp_2",
                fit.mass,
                _comp_params.At(0),
                _comp_params.At(1)
                )
            _funcs.Add(_hist)
            _funcs.Add(_f1)
            _funcs.Add(_f2)
            _pdf = RooFFTConvPdf(
                _name,
                _comp["title"],
                fit.mass,
                _f2,
                _f1
                )
            _pdf.setBufferFraction(5.0)
            _components.Add(_pdf)
        elif _comp["type"] == "Conv_RooHistPdf_RooGaussian":
            if 'config' not in _comp:
                raise Exception("Missing 'config' section for RooHistPdf.")
            if 'file' not in _comp['config']:
                raise Exception("Missing 'file' key in the 'config' section for RooHistPdf.")
            if 'name' not in _comp['config']:
                raise Exception("Missing 'name' key in the 'config' section for RooHistPdf.")
            _file = TFile(_comp['config']['file'])
            _f1 = _file.Get(_comp['config']['name'])
            _f2 = RooGaussian(
                _name+"_comp_2",
                _comp["title"]+"_comp_2",
                fit.mass,
                _comp_params.At(0),
                _comp_params.At(1)
                )
            _funcs.Add(_f1)
            _funcs.Add(_f2)
            _pdf = RooNumConvPdf(
                _name,
                _comp["title"],
                fit.mass,
                _f2,
                _f1
                )
            _components.Add(_pdf)
        elif _comp["type"] == "FFT_RooDoubleCBShape_RooGaussian":
            # Define sampling frequency
            fit.mass.setBins(10000,"fft") ;
            _f1 = RooDoubleCBShape(
                _name+"_comp_1",
                _comp["title"]+"_comp_1",
                fit.mass,
                _comp_params.At(0),
                _comp_params.At(1),
                _comp_params.At(2),
                _comp_params.At(3),
                _comp_params.At(4),
                _comp_params.At(5)
                )
            _f2 = RooGaussian(
                _name+"_comp_2",
                _comp["title"]+"_comp_2",
                fit.mass,
                _comp_params.At(6),
                _comp_params.At(7)
                )
            _funcs.Add(_f1)
            _funcs.Add(_f2)
            _pdf = RooFFTConvPdf(
                _name,
                _comp["title"],
                fit.mass,
                _f1,
                _f2
                )
            _pdf.setBufferFraction(50.0)
            _components.Add(_pdf)
        elif _comp["type"] == "FFT_2xCBShape_RooGaussian":
            # Define sampling frequency
            fit.mass.setBins(10000,"fft") ;
            _cb_frac = RooRealVar(
                    _name+"_cb_frac",
                    _name+"_cb_frac",
                    0.1, 0.0, 1.0)
            _f1 = RooCBShape(
                _name+"_comp_1",
                _comp["title"]+"_comp_1",
                fit.mass,
                _comp_params.At(0),
                _comp_params.At(1),
                _comp_params.At(2),
                _comp_params.At(3)
                )
            _f2 = RooCBShape(
                _name+"_comp_2",
                _comp["title"]+"_comp_2",
                fit.mass,
                _comp_params.At(0),
                _comp_params.At(1),
                _comp_params.At(4),
                _comp_params.At(5)
                )
            _f3 = RooAddPdf(
                _name+"_comp_3",
                _comp["title"]+"_comp_3",
                RooArgList(_f1, _f2),
                RooArgList(_cb_frac),
                kTRUE)
            _f4 = RooGaussian(
                _name+"_comp_4",
                _comp["title"]+"_comp_4",
                fit.mass,
                _comp_params.At(6),
                _comp_params.At(7)
                )
            _funcs.Add(_f1)
            _funcs.Add(_f2)
            _funcs.Add(_f3)
            _funcs.Add(_f4)
            _funcs.Add(_cb_frac)
            _pdf = RooFFTConvPdf(
                _name,
                _comp["title"],
                fit.mass,
                _f3,
                _f4
                )
            _pdf.setBufferFraction(50.0)
            _components.Add(_pdf)
        else:
            raise Exception("Unknown PDF type: "+_comp["type"])

        if _i < _n_comps:
            _f_v = 0.1
            _f_min = 0.0
            _f_max = 1.0
            if "fraction" in _comp:
                _f_v = _comp["fraction"]["value"]
                _f_min = _comp["fraction"]["min"]
                _f_max = _comp["fraction"]["max"]
            _f = RooRealVar(
                    prefix+"_frac_"+_comp["name"],
                    prefix+" component "+_comp["name"]+" fraction",
                    _f_v, _f_min, _f_max)
            if "fraction" in _comp and "constraint" in _comp["fraction"]:
                _constr = RooGaussian(
                        _name+"_frac_constr",
                        _name+"_frac_constr",
                        _f,
                        RooFit.RooConst(_comp["fraction"]["constraint"]["mean"]),
                        RooFit.RooConst(_comp["fraction"]["constraint"]["sigma"])
                        )
                fit.constraints.push_back(_constr)
                _comp_constrs.Add(_constr)
            _fracs.Add(_f)

        _i += 1

    if _n_comps == 1:
        _pdf = _components.At(0)
    else:
        # Make sure we are creating a recursive composit PDF
        _pdf = RooAddPdf(
            prefix+"_pdf",
            prefix+" PDF",
            RooArgList(_components),
            RooArgList(_fracs),
            kTRUE)

    _components_all.AddAll(_components)
    return (_pdf, _components_all, _params)
Example #5
0
    all_params.update(bkg_params)

    nextra = None
    if 'extra' in fpars:
        prefix = "Ext"
        if len(fit_params) > 1:
            prefix = fit.name + "_" + prefix
        (extra_pdf, extra_components, extra_params) = init_pdfs(fit, fpars["extra"], prefix, False)
        extra_yield = fpars["extra"]["yield"]
        if 'params' in extra_yield:
            _extra_params = TList()
            _extra_constrs = TList()
            for _par in extra_yield["params"]:
                _name = _par["name"]
                if _name in all_params:
                    _extra_params.append(all_params[_name])
                else:
                    _var = RooRealVar(
                            _name, _par["title"], _par["value"],
                            _par["min"], _par["max"], _par["units"]
                            )
                    fit.parameters.push_back(_var)
                    _extra_params.Add(_var)
                    all_params[_name] = _var
                    if "constraint" in _par:
                        _constr = RooGaussian(
                                _name+"_constr",
                                _par["title"]+"_constr",
                                _var,
                                RooFit.RooConst(_par["constraint"]["mean"]),
                                RooFit.RooConst(_par["constraint"]["sigma"])
Example #6
0
    def test2Lists(self):
        """Test list/TList behavior and compatibility"""

        l = TList()
        l.Add(TObjString('a'))
        l.Add(TObjString('b'))
        l.Add(TObjString('c'))
        l.Add(TObjString('d'))
        l.Add(TObjString('e'))
        l.Add(TObjString('f'))
        l.Add(TObjString('g'))
        l.Add(TObjString('h'))
        l.Add(TObjString('i'))
        l.Add(TObjString('j'))

        self.assertEqual(len(l), 10)
        self.assertEqual(l[3], 'd')
        self.assertEqual(l[-1], 'j')
        self.assertRaises(IndexError, l.__getitem__, 20)
        self.assertRaises(IndexError, l.__getitem__, -20)

        self.assertEqual(list(l),
                         ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'])

        l[3] = TObjString('z')
        self.assertEqual(list(l),
                         ['a', 'b', 'c', 'z', 'e', 'f', 'g', 'h', 'i', 'j'])

        del l[2]
        self.assertEqual(list(l),
                         ['a', 'b', 'z', 'e', 'f', 'g', 'h', 'i', 'j'])

        self.assert_(TObjString('b') in l)
        self.assert_(not TObjString('x') in l)

        self.assertEqual(list(l[2:6]), ['z', 'e', 'f', 'g'])
        self.assertEqual(list(l[2:6:2]), ['z', 'f'])
        self.assertEqual(list(l[-5:-2]), ['f', 'g', 'h'])
        self.assertEqual(list(l[7:]), ['i', 'j'])
        self.assertEqual(list(l[:3]), ['a', 'b', 'z'])

        del l[2:4]
        self.assertEqual(list(l), ['a', 'b', 'f', 'g', 'h', 'i', 'j'])

        l[2:5] = [TObjString('1'), TObjString('2')]
        self.assertEqual(list(l), ['a', 'b', '1', '2', 'i', 'j'])

        l[6:6] = [TObjString('3')]
        self.assertEqual(list(l), ['a', 'b', '1', '2', 'i', 'j', '3'])

        l.append(TObjString('4'))
        self.assertEqual(list(l), ['a', 'b', '1', '2', 'i', 'j', '3', '4'])

        l.extend([TObjString('5'), TObjString('j')])
        self.assertEqual(list(l),
                         ['a', 'b', '1', '2', 'i', 'j', '3', '4', '5', 'j'])
        self.assertEqual(l.count('b'), 1)
        self.assertEqual(l.count('j'), 2)
        self.assertEqual(l.count('x'), 0)

        self.assertEqual(l.index(TObjString('i')), 4)
        self.assertRaises(ValueError, l.index, TObjString('x'))

        l.insert(3, TObjString('6'))
        l.insert(20, TObjString('7'))
        l.insert(-1, TObjString('8'))
        self.assertEqual(
            list(l),
            ['8', 'a', 'b', '1', '6', '2', 'i', 'j', '3', '4', '5', 'j', '7'])
        self.assertEqual(l.pop(), '7')
        self.assertEqual(l.pop(3), '1')
        self.assertEqual(
            list(l), ['8', 'a', 'b', '6', '2', 'i', 'j', '3', '4', '5', 'j'])

        l.remove(TObjString('j'))
        l.remove(TObjString('3'))

        self.assertRaises(ValueError, l.remove, TObjString('x'))
        self.assertEqual(list(l),
                         ['8', 'a', 'b', '6', '2', 'i', '4', '5', 'j'])

        l.reverse()
        self.assertEqual(list(l),
                         ['j', '5', '4', 'i', '2', '6', 'b', 'a', '8'])

        l.sort()
        self.assertEqual(list(l),
                         ['2', '4', '5', '6', '8', 'a', 'b', 'i', 'j'])

        if sys.hexversion >= 0x3000000:
            l.sort(key=TObjString.GetName)
            l.reverse()
        else:
            l.sort(lambda a, b: cmp(b.GetName(), a.GetName()))
        self.assertEqual(list(l),
                         ['j', 'i', 'b', 'a', '8', '6', '5', '4', '2'])

        l2 = l[:3]
        self.assertEqual(list(l2 * 3),
                         ['j', 'i', 'b', 'j', 'i', 'b', 'j', 'i', 'b'])
        self.assertEqual(list(3 * l2),
                         ['j', 'i', 'b', 'j', 'i', 'b', 'j', 'i', 'b'])

        l2 *= 3
        self.assertEqual(list(l2),
                         ['j', 'i', 'b', 'j', 'i', 'b', 'j', 'i', 'b'])

        l2 = l[:3]
        l3 = l[6:8]
        self.assertEqual(list(l2 + l3), ['j', 'i', 'b', '5', '4'])

        if sys.hexversion >= 0x3000000:
            next = '__next__'
        else:
            next = 'next'
        i = iter(l2)
        self.assertEqual(getattr(i, next)(), 'j')
        self.assertEqual(getattr(i, next)(), 'i')
        self.assertEqual(getattr(i, next)(), 'b')
        self.assertRaises(StopIteration, getattr(i, next))