Beispiel #1
0
def createTable(name, directory, schema, title=None,
                separate_branches=False, branch_name="default"):
    """Create a new table.

    A new table is implemented in a Root file by a Root tree (a
    'TTree' instance).  

    'name' -- The name under which to store the tree.

    'directory' -- The Root 'Directory' in which to store the tree. 

    'schema' -- The table schema.

    'title' -- The tree's title.  If 'None', the name is used.

    'separate_branches' -- If true, the leaf corresponding to each
    column is places on a separate branch with the same name as the
    column.  Otherwise, all leaves are combined into a single
    branch, named by 'branch_name'.

    'branch_name' -- If 'separate_branches' is false, this is the
    name of the branch containing all the leaves.

    'with_metdata' -- Load and store metadata.
    """

    # Make sure the table is being put in a Root directory.
    if not isinstance(directory, Directory):
        raise TypeError, "directory is not in a Root file"
    # Make sure the directory is writable.
    if not directory.writable:
        raise hep.fs.AccessError, "%s is not writable" % directory

    # Clean up arguments.
    if title is None:
        # No title specified; use the tree name.
        title = name
    # Make sure 'separate_branches' is boolean.
    separate_branches = bool(separate_branches)

    # Build the tree.
    address = _getSwigPointerAddress(
        directory._Directory__tdirectory.this)
    tree = ext.createTree(
        address, name, title, schema, separate_branches, branch_name,
        8192, directory.file.with_metadata)
    # Note the file on the tree, so that the file isn't closed until
    # the tree has gone away.
    tree.file = directory.file
    return tree
Beispiel #2
0
def loadPdtFile(path):
    """Load particle data from a PDT table file.

    'path' -- The path to a PDT table file (typically named "pdt.table"
    or similar.

    returns -- A 'Table' instance containing the particle data."""

    # Start with a blank table.
    table = Table()

    # Process each line in the file.
    for line in open(path):
        if line.startswith("end"):
            # Stop processing at the "end" line.
            break
        if line.strip() == "":
            # Empty line; skip it.
            continue
        if line.startswith("*"):
            # Comment line; skip it.
            continue

        # Break the line into parts at whitespace.
        parts = line.split()
        # The first part tells what kind of line this is.
        command = parts[0]

        if command == "add":
            if parts[1] == "p":
                # Add a new particle
                table.add(Particle(
                    type=parts[2],
                    name=parts[3],
                    id=int(parts[4]),
                    mass=float(parts[5]),
                    width=float(parts[6]),
                    max_Dm=float(parts[7]),
                    charge=int(parts[8]) / 3.,
                    spin=int(parts[9]) / 2.,
                    range=float(parts[10]) * 0.001,
                    is_stable=False,
                    ))
            else:
                _warning("add of unknown object '%s'" % parts[1])

        elif command == "sets":
            if parts[1] == "p":
                # Set an attribute of an existing particle.
                name = parts[2]
                try:
                    particle = table[name]
                except KeyError:
                    _warning("no such particle '%s' in sets" % name)
                else:
                    if parts[3] == "isStable":
                        setattr(particle, "is_stable",
                                bool(int(parts[4])))
                    else:
                        setattr(particle, parts[3],
                                _sloppyConvertString(parts[4]))
            else:
                _warning("sets of unknown object '%s'" % parts[1])

        else:
            _warning("unknown command '%s'" % command)

    for particle in table.values():
        id = particle.id
        try:
            particle.charge_conjugate = table.findId(-id)
        except:
            particle.charge_conjugate = particle

    return table