コード例 #1
0
    def generate_source(self):
        """
        Write the .cc file.
        """

        subst = {
            'NAMESPACE': NAMESPACE,
            'name': self.name,
            'parent': self.parent
        }

        src = FileOutput('{PACKDIR}/Objects/src/{name}.cc'.format(
            PACKDIR=PACKDIR, **subst))
        src.writeline('#include "../interface/{name}.h"'.format(**subst))

        if len(self.enums) != 0:
            src.newline()
            for enum in self.enums:
                enum.write_def(src, cls='{NAMESPACE}::{name}'.format(**subst))

        if len(self.constants) != 0:
            src.newline()
            for constant in self.constants:
                constant.write_def(src,
                                   cls='{NAMESPACE}::{name}'.format(**subst))

        src.newline()
        src.writeline('/*static*/')
        src.writeline('panda::utils::BranchList')
        src.writeline(
            '{NAMESPACE}::{name}::getListOfBranches()'.format(**subst))
        src.writeline('{')
        src.indent += 1
        src.writeline('utils::BranchList blist;')
        if self.parent not in ['Singlet', 'Element']:
            src.writeline(
                'blist += {parent}::getListOfBranches();'.format(**subst))
        if len(self.branches) != 0:
            src.writeline('blist += {{{bnames}}};'.format(bnames=', '.join(
                '"{name}"'.format(name=branch.name) for branch in self.branches
                if '!' not in branch.modifier)))
        src.writeline('return blist;')
        src.indent -= 1
        src.writeline('}')

        if self.is_singlet():
            src.newline()

            if self.instantiable:
                src.writeline(
                    '{NAMESPACE}::{name}::{name}(char const* _name/* = ""*/) :'
                    .format(**subst))
                src.indent += 1
                initializers = ['{parent}(_name)'.format(**subst)]
                for branch in self.branches:
                    branch.init_default(initializers, context='Singlet')
                src.writelines(initializers, ',')
                src.indent -= 1
                src.writeline('{')
                src.indent += 1
                for branch in self.branches:
                    branch.write_default_ctor(src, context='Singlet')
                src.indent -= 1
                src.writeline('}')
                src.newline()

                src.writeline(
                    '{NAMESPACE}::{name}::{name}({name} const& _src) :'.format(
                        **subst))
                src.indent += 1
                initializers = ['{parent}(_src.name_)'.format(**subst)]
                for branch in self.branches:
                    branch.init_copy(initializers, context='Singlet')
                src.writelines(initializers, ',')
                src.indent -= 1
                src.writeline('{')
                src.indent += 1
                for branch in self.branches:
                    branch.write_copy_ctor(src, context='Singlet')
                src.indent -= 1
                src.writeline('}')
                src.newline()

            src.writeline('{NAMESPACE}::{name}::~{name}()'.format(**subst))
            src.writeline('{')
            src.writeline('}')

            methods = [
                ('operator=', '{NAMESPACE}::{name}&'.format(**subst), [
                    ('{name} const&'.format(**subst), '_src')
                ], 'write_assign', '*this'),
                ('doSetStatus_', 'void', [
                    ('TTree&', '_tree'),
                    ('utils::BranchList const&', '_branches')
                ], 'write_set_status', None),
                ('doGetStatus_ const', 'panda::utils::BranchList', [
                    ('TTree&', '_tree')
                ], 'write_get_status', 'blist', [], 'utils::BranchList blist'),
                ('doSetAddress_', 'void', [
                    ('TTree&', '_tree'),
                    ('utils::BranchList const&', '_branches', '{"*"}'),
                    ('Bool_t', '_setStatus', 'kTRUE')
                ], 'write_set_address', None),
                ('doBook_', 'void', [('TTree&', '_tree'),
                                     ('utils::BranchList const&', '_branches',
                                      '{"*"}')], 'write_book', None),
                ('doInit_', 'void', [], 'write_init', None)
            ]

            for method in methods:
                src.newline()
                self._write_method(src,
                                   'Singlet',
                                   method,
                                   custom_block=(method[0] in ['doInit_']))

            src.newline()
            src.writeline('panda::utils::BranchList')
            src.writeline(
                '{NAMESPACE}::{name}::doGetBranchNames_(Bool_t _fullName) const'
                .format(**subst))
            src.writeline('{')
            src.indent += 1
            src.writeline('if (_fullName)')
            src.writeline('  return getListOfBranches().fullNames(name_);')
            src.writeline('else')
            src.writeline('  return getListOfBranches().fullNames();')
            src.indent -= 1
            src.writeline('}')

        #if self.is_singlet():
        else:
            size_lines = [
                'TString size(_dynamic ? "[" + _name + ".size]" : TString::Format("[%d]", nmax_));'
            ]
            methods = [('allocate', 'void', [('UInt_t', '_nmax')],
                        'write_allocate', None),
                       ('deallocate', 'void', [], 'write_deallocate', None),
                       ('setStatus', 'void', [
                           ('TTree&', '_tree'), ('TString const&', '_name'),
                           ('utils::BranchList const&', '_branches')
                       ], 'write_set_status', None),
                       ('getStatus const', 'panda::utils::BranchList', [
                           ('TTree&', '_tree'), ('TString const&', '_name')
                       ], 'write_get_status', 'blist', [],
                        'utils::BranchList blist'),
                       ('setAddress', 'void', [
                           ('TTree&', '_tree'), ('TString const&', '_name'),
                           ('utils::BranchList const&', '_branches', '{"*"}'),
                           ('Bool_t', '_setStatus', 'kTRUE')
                       ], 'write_set_address', None),
                       ('book', 'void', [
                           ('TTree&', '_tree'), ('TString const&', '_name'),
                           ('utils::BranchList const&', '_branches', '{"*"}'),
                           ('Bool_t', '_dynamic', 'kTRUE')
                       ], 'write_book', None, size_lines),
                       ('releaseTree', 'void', [('TTree&', '_tree'),
                                                ('TString const&', '_name')],
                        'write_release_tree', None),
                       ('resizeVectors_', 'void', [('UInt_t', '_size')],
                        'write_resize_vectors', None)]

            for method in methods:
                src.newline()
                self._write_method(src,
                                   'datastore',
                                   method,
                                   nestedcls='datastore')

            src.newline()
            src.newline()
            src.writeline('panda::utils::BranchList')
            src.writeline(
                '{NAMESPACE}::{name}::datastore::getBranchNames(TString const& _name/* = ""*/) const'
                .format(**subst))
            src.writeline('{')
            src.indent += 1
            src.writeline(
                'return {name}::getListOfBranches().fullNames(_name);'.format(
                    **subst))
            src.indent -= 1
            src.writeline('}')

            if self.instantiable:
                src.newline()
                src.writeline(
                    '{NAMESPACE}::{name}::{name}(char const* _name/* = ""*/) :'
                    .format(**subst))
                src.indent += 1
                initializers = [
                    '{parent}(new {name}Array(1, _name))'.format(**subst)
                ]
                for branch in self.branches:
                    branch.init_default(initializers, context='Element')
                src.writelines(initializers, ',')
                src.indent -= 1
                src.writeline('{')
                src.indent += 1
                for branch in self.branches:
                    branch.write_default_ctor(src, context='Element')
                src.indent -= 1
                src.writeline('}')

                src.newline()
                src.writeline(
                    '{NAMESPACE}::{name}::{name}({name} const& _src) :'.format(
                        **subst))
                src.indent += 1
                initializers = [
                    '{parent}(new {name}Array(1, gStore.getName(&_src)))'.
                    format(**subst)
                ]
                for branch in self.branches:
                    branch.init_copy(initializers, context='Element')
                src.writelines(initializers, ',')
                src.indent -= 1
                src.writeline('{')
                src.indent += 1
                src.writeline('{parent}::operator=(_src);'.format(**subst))
                if len(self.branches) != 0:
                    src.newline()
                    for branch in self.branches:
                        branch.write_copy_ctor(src, context='Element')
                src.indent -= 1
                src.writeline('}')

            src.newline()
            src.writeline(
                '{NAMESPACE}::{name}::{name}(datastore& _data, UInt_t _idx) :'.
                format(**subst))
            src.indent += 1
            initializers = ['{parent}(_data, _idx)'.format(**subst)]
            for branch in self.branches:
                branch.init_standard(initializers, context='Element')
            src.writelines(initializers, ',')
            src.indent -= 1
            src.writeline('{')
            src.indent += 1
            for branch in self.branches:
                branch.write_standard_ctor(src, context='Element')
            src.indent -= 1
            src.writeline('}')

            src.newline()
            src.writeline(
                '{NAMESPACE}::{name}::{name}(ArrayBase* _array) :'.format(
                    **subst))
            src.indent += 1
            initializers = ['{parent}(_array)'.format(**subst)]
            for branch in self.branches:
                branch.init_default(initializers, context='Element')
            src.writelines(initializers, ',')
            src.indent -= 1
            src.writeline('{')
            src.indent += 1
            for branch in self.branches:
                branch.write_default_ctor(src, context='Element')
            src.indent -= 1
            src.writeline('}')

            src.newline()
            src.writeline('{NAMESPACE}::{name}::~{name}()'.format(**subst))
            src.writeline('{')
            src.indent += 1
            src.writeline('destructor();')
            src.writeline('gStore.free(this);')
            src.indent -= 1
            src.writeline('}')

            src.newline()
            src.writeline('void')
            src.writeline('{NAMESPACE}::{name}::destructor()'.format(**subst))
            src.writeline('{')
            src.indent += 1
            src.write_custom_block('{name}.cc.destructor'.format(**subst))
            src.newline()
            src.writeline('{parent}::destructor();'.format(**subst))
            src.indent -= 1
            src.writeline('}')

            methods = [('operator=', '{NAMESPACE}::{name}&'.format(**subst), [
                ('{name} const&'.format(**subst), '_src')
            ], 'write_assign', '*this'),
                       ('doSetAddress_', 'void', [
                           ('TTree&', '_tree'), ('TString const&', '_name'),
                           ('utils::BranchList const&', '_branches', '{"*"}'),
                           ('Bool_t', '_setStatus', 'kTRUE')
                       ], 'write_set_address', None),
                       ('doBook_', 'void', [
                           ('TTree&', '_tree'), ('TString const&', '_name'),
                           ('utils::BranchList const&', '_branches', '{"*"}')
                       ], 'write_book', None),
                       ('doInit_', 'void', [], 'write_init', None)]

            for method in methods:
                src.newline()
                self._write_method(src,
                                   'Element',
                                   method,
                                   custom_block=(method[0] in ['doInit_']))

        src.newline()
        src.writeline('void')
        src.writeline(
            '{NAMESPACE}::{name}::print(std::ostream& _out/* = std::cout*/, UInt_t _level/* = 1*/) const'
            .format(**subst))
        src.writeline('{')
        src.indent += 1
        src.write_custom_block('{name}.cc.print'.format(**subst),
                               default='dump(_out);')
        src.indent -= 1
        src.writeline('}')

        src.newline()
        src.writeline('void')
        src.writeline(
            '{NAMESPACE}::{name}::dump(std::ostream& _out/* = std::cout*/) const'
            .format(**subst))
        src.writeline('{')
        src.indent += 1

        if self.parent not in ['Singlet', 'Element']:
            src.writeline('{parent}::dump(_out);'.format(parent=self.parent))
            src.newline()

        if len(self.branches) != 0:
            for branch in self.branches:
                branch.write_dump(src)

        src.indent -= 1
        src.writeline('}')

        if len(self.functions):
            src.newline()
            for function in self.functions:
                function.write_def(src, context=self.name)

        src.newline()
        src.write_custom_block('{name}.cc.global'.format(**subst))

        src.close()
コード例 #2
0
    def generate_header(self):
        """
        Write a header file.
        """

        inheritance = [self]
        while True:
            try:
                inheritance.insert(0, PhysicsObject.get(inheritance[0].parent))
            except KeyError:
                break

        header = FileOutput('{PACKDIR}/Objects/interface/{name}.h'.format(
            PACKDIR=PACKDIR, name=self.name))
        header.writeline('#ifndef {PACKAGE}_Objects_{name}_h'.format(
            PACKAGE=PACKAGE, name=self.name))
        header.writeline('#define {PACKAGE}_Objects_{name}_h'.format(
            PACKAGE=PACKAGE, name=self.name))
        header.writeline('#include "Constants.h"')

        included = ['#include "{name}.h"'.format(name=self.name)]
        if self.parent == 'Element':
            header.writeline('#include "../../Framework/interface/Element.h"')
        elif self.parent == 'Singlet':
            header.writeline('#include "../../Framework/interface/Singlet.h"')
        else:
            stmt = '#include "{parent}.h"'.format(parent=self.parent)
            if stmt not in included:
                header.writeline(stmt)
                included.append(stmt)

        header.writeline('#include "../../Framework/interface/Array.h"')
        header.writeline('#include "../../Framework/interface/Collection.h"')
        header.writeline('#include "../../Framework/interface/Ref.h"')
        header.writeline('#include "../../Framework/interface/RefVector.h"')

        for include in self.includes:
            if include.code not in included:
                include.write(header)
                included.append(include.code)

        for branch in self.branches:
            if hasattr(branch, 'refname'):  # is a RefBranch
                stmt = '#include "{obj}.h"'.format(obj=branch.objname)
                if stmt not in included:
                    header.writeline(stmt)
                    included.append(stmt)

        header.newline()
        header.writeline(
            'namespace {NAMESPACE} {{'.format(NAMESPACE=NAMESPACE))
        header.newline()
        header.indent += 1

        header.writeline('class {name} : public {parent} {{'.format(
            name=self.name, parent=self.parent))
        header.writeline('public:')
        header.indent += 1

        if len(self.enums) != 0:
            for enum in self.enums:
                enum.write_decl(header, context='class')
            header.newline()

        if len(self.constants) != 0:
            for constant in self.constants:
                constant.write_decl(header, context='class')
            header.newline()

        if not self.is_singlet():
            header.writeline(
                'struct datastore : public {parent}::datastore {{'.format(
                    parent=self.parent))
            header.indent += 1

            header.writeline('datastore() : {parent}::datastore() {{}}'.format(
                parent=self.parent))
            header.writeline('~datastore() { deallocate(); }')

            newline = False
            for ancestor in inheritance:
                if len(ancestor.branches) == 0:
                    continue

                if not newline:
                    header.newline()
                    newline = True

                if ancestor != self:
                    header.writeline('/* {name}'.format(name=ancestor.name))

                for branch in ancestor.branches:
                    branch.write_decl(header, context='datastore')

                if ancestor != self:
                    header.writeline('*/')

            header.newline()
            header.writeline('void allocate(UInt_t n) override;')
            header.writeline('void deallocate() override;')
            header.writeline(
                'void setStatus(TTree&, TString const&, utils::BranchList const&) override;'
            )
            header.writeline(
                'utils::BranchList getStatus(TTree&, TString const&) const override;'
            )
            header.writeline(
                'utils::BranchList getBranchNames(TString const& = "") const override;'
            )
            header.writeline(
                'void setAddress(TTree&, TString const&, utils::BranchList const& = {"*"}, Bool_t setStatus = kTRUE) override;'
            )
            header.writeline(
                'void book(TTree&, TString const&, utils::BranchList const& = {"*"}, Bool_t dynamic = kTRUE) override;'
            )
            header.writeline(
                'void releaseTree(TTree&, TString const&) override;')
            header.writeline('void resizeVectors_(UInt_t) override;')

            header.indent -= 1
            header.writeline('};')
            header.newline()

            header.writeline('typedef Array<{name}> array_type;'.format(
                name=self.name, parent=self.parent))
            header.writeline(
                'typedef Collection<{name}> collection_type;'.format(
                    name=self.name, parent=self.parent))
            header.newline()

        header.writeline(
            'typedef {parent} base_type;'.format(parent=self.parent))
        header.newline()

        # "boilerplate" functions (default ctor for non-SINGLE objects are pretty nontrivial though)
        if self.instantiable:
            header.writeline('{name}(char const* name = "");'.format(
                name=self.name))  # default constructor
            header.writeline('{name}({name} const&);'.format(
                name=self.name))  # copy constructor

        # standard constructors and specific functions
        if not self.is_singlet():
            # whereas elements of collections are constructed from an array and an index
            header.writeline(
                '{name}(datastore&, UInt_t idx);'.format(name=self.name))

        header.writeline('~{name}();'.format(name=self.name))  # destructor
        header.writeline('{name}& operator=({name} const&);'.format(
            name=self.name))  # assignment operator

        header.newline()

        header.writeline(
            'static char const* typeName() {{ return "{name}"; }}'.format(
                name=self.name))

        header.newline()

        header.writeline(
            'void print(std::ostream& = std::cout, UInt_t level = 1) const override;'
        )
        header.writeline(
            'void dump(std::ostream& = std::cout) const override;')

        header.newline()

        if len(self.functions) != 0:
            for function in self.functions:
                function.write_decl(header, context='class')
            header.newline()

        if self.is_singlet():
            context = 'Singlet'
        else:
            context = 'Element'

        has_public = False
        for ancestor in inheritance:
            if len(ancestor.branches) == 0:
                continue

            inherits_members = False

            for branch in ancestor.branches:
                if not hasattr(branch,
                               'refname') and branch.name.endswith('_'):
                    continue

                if not has_public:
                    has_public = True

                if not inherits_members:
                    if ancestor != self:
                        header.writeline(
                            '/* {name}'.format(name=ancestor.name))

                    inherits_members = True

                branch.write_decl(header, context=context)

            if inherits_members and ancestor != self:
                header.writeline('*/')

        if has_public:
            header.newline()

        has_protected = False
        for ancestor in inheritance:
            if len(ancestor.branches) == 0:
                continue

            inherits_members = False

            for branch in ancestor.branches:
                if hasattr(branch, 'refname') or not branch.name.endswith('_'):
                    continue

                if not has_protected:
                    header.indent -= 1
                    header.writeline('protected:')
                    header.indent += 1
                    has_protected = True

                if not inherits_members:
                    if ancestor != self:
                        header.writeline(
                            '/* {name}'.format(name=ancestor.name))

                    inherits_members = True

                branch.write_decl(header, context=context)

            if inherits_members and ancestor != self:
                header.writeline('*/')

        if has_protected:
            header.newline()

            header.indent -= 1
            header.writeline('public:')
            header.indent += 1

        header.write_custom_block('{name}.h.classdef'.format(name=self.name))

        header.newline()
        header.writeline('static utils::BranchList getListOfBranches();')

        if self.is_singlet():
            header.newline()
            header.indent -= 1
            header.writeline('protected:')
            header.indent += 1
            header.writeline(
                'void doSetStatus_(TTree&, utils::BranchList const&) override;'
            )
            header.writeline(
                'utils::BranchList doGetStatus_(TTree&) const override;')
            header.writeline(
                'utils::BranchList doGetBranchNames_(Bool_t) const override;')
            header.writeline(
                'void doSetAddress_(TTree&, utils::BranchList const& = {"*"}, Bool_t setStatus = kTRUE) override;'
            )
            header.writeline(
                'void doBook_(TTree&, utils::BranchList const& = {"*"}) override;'
            )
            header.writeline('void doInit_() override;')
            header.indent -= 1
        else:
            header.newline()
            header.writeline('void destructor() override;')
            header.newline()
            header.indent -= 1
            header.writeline('protected:')
            header.indent += 1
            header.writeline('{name}(ArrayBase*);'.format(name=self.name))
            header.newline()
            header.writeline(
                'void doSetAddress_(TTree&, TString const&, utils::BranchList const& = {"*"}, Bool_t setStatus = kTRUE) override;'
            )
            header.writeline(
                'void doBook_(TTree&, TString const&, utils::BranchList const& = {"*"}) override;'
            )
            header.writeline('void doInit_() override;')
            header.indent -= 1

        header.writeline('};')

        header.newline()

        if not self.is_singlet():
            header.writeline(
                'typedef Array<{name}> {name}Array;'.format(name=self.name))
            header.writeline(
                'typedef Collection<{name}> {name}Collection;'.format(
                    name=self.name))
            header.writeline(
                'typedef Ref<{name}> {name}Ref;'.format(name=self.name))
            header.writeline(
                'typedef RefVector<{name}> {name}RefVector;'.format(
                    name=self.name))
            header.newline()

        header.write_custom_block('{name}.h.global'.format(name=self.name))
        header.newline()

        header.indent -= 1
        header.writeline('}')
        header.newline()

        header.writeline('#endif')
        header.close()
コード例 #3
0
ファイル: tree.py プロジェクト: smdogra/PandaTree
    def generate_header(self):
        """
        Write the header file.
        """

        header = FileOutput('{PACKDIR}/Objects/interface/{name}.h'.format(
            PACKDIR=PACKDIR, name=self.name))
        header.writeline('#ifndef {PACKAGE}_Objects_{name}_h'.format(
            PACKAGE=PACKAGE, name=self.name))
        header.writeline('#define {PACKAGE}_Objects_{name}_h'.format(
            PACKAGE=PACKAGE, name=self.name))
        if self.parent == 'TreeEntry':
            header.writeline(
                '#include "../../Framework/interface/TreeEntry.h"')
        else:
            header.writeline(
                '#include "{parent}.h"'.format(parent=self.parent))
        header.writeline('#include "Constants.h"')

        included = []
        for objbranch in self.objbranches:
            if objbranch.objname not in included:
                header.writeline(
                    '#include "{brobj}.h"'.format(brobj=objbranch.objname))
                included.append(objbranch.objname)

        for include in self.includes:
            include.write(header)

        header.newline()
        header.writeline(
            'namespace {NAMESPACE} {{'.format(NAMESPACE=NAMESPACE))
        header.newline()
        header.indent += 1

        header.writeline('class {name} : public {parent} {{'.format(
            name=self.name, parent=self.parent))
        header.writeline('public:')
        header.indent += 1

        header.writeline(
            '{name}();'.format(name=self.name))  # default constructor
        header.writeline('{name}({name} const&);'.format(
            name=self.name))  # copy constructor
        header.writeline('~{name}() {{}}'.format(name=self.name))  # destructor
        header.writeline('{name}& operator=({name} const&);'.format(
            name=self.name))  # assignment operator

        header.newline()

        header.writeline(
            'void print(std::ostream& = std::cout, UInt_t level = 1) const override;'
        )
        header.writeline(
            'void dump(std::ostream& = std::cout) const override;')

        if len(self.functions) != 0:
            header.newline()
            for function in self.functions:
                function.write_decl(header, context='class')

        if len(self.objbranches) != 0:
            header.newline()
            for objbranch in self.objbranches:
                objbranch.write_decl(header)

        if len(self.branches) != 0:
            header.newline()
            for branch in self.branches:
                branch.write_decl(header, context='TreeEntry')

        header.newline()
        header.writeline('static utils::BranchList getListOfBranches();')

        header.newline()
        header.indent -= 1
        header.writeline('protected:')
        header.indent += 1
        header.writeline(
            'void doSetStatus_(TTree&, utils::BranchList const&) override;')
        header.writeline(
            'utils::BranchList doGetStatus_(TTree&) const override;')
        header.writeline(
            'utils::BranchList doGetBranchNames_() const override;')
        header.writeline(
            'void doSetAddress_(TTree&, utils::BranchList const&, Bool_t setStatus) override;'
        )
        header.writeline(
            'void doBook_(TTree&, utils::BranchList const&) override;')
        header.writeline('void doGetEntry_(TTree&, Long64_t) override;')
        header.writeline('void doInit_() override;')

        header.newline()
        header.indent -= 1
        header.writeline('public:')
        header.indent += 1
        header.write_custom_block('{name}.h.classdef'.format(name=self.name))

        header.indent -= 1

        header.writeline('};')

        header.newline()
        header.write_custom_block('{name}.h.global'.format(name=self.name))

        header.newline()

        header.indent -= 1
        header.writeline('}')
        header.newline()

        header.writeline('#endif')
        header.close()
コード例 #4
0
ファイル: tree.py プロジェクト: smdogra/PandaTree
    def generate_source(self):
        """
        Write the .cc file.
        """

        collections = [
            b for b in self.objbranches if b.conttype == 'Collection'
        ]

        src = FileOutput('{PACKDIR}/Objects/src/{name}.cc'.format(
            PACKDIR=PACKDIR, name=self.name))
        src.writeline(
            '#include "../interface/{name}.h"'.format(name=self.name))
        src.newline()

        src.writeline('{NAMESPACE}::{name}::{name}() :'.format(
            NAMESPACE=NAMESPACE, name=self.name))
        src.indent += 1
        src.writeline('{parent}()'.format(parent=self.parent, name=self.name))
        src.indent -= 1
        src.writeline('{')
        src.indent += 1
        if len(self.objbranches) != 0:
            src.writeline('std::vector<Object*> myObjects{{' + ', '.join(
                ['&{name}'.format(name=b.name)
                 for b in self.objbranches]) + '}};')
            src.writeline(
                'objects_.insert(objects_.end(), myObjects.begin(), myObjects.end());'
            )

        if len(collections) != 0:
            src.writeline(
                'std::vector<CollectionBase*> myCollections{{' +
                ', '.join(['&{name}'.format(name=b.name)
                           for b in collections]) + '}};')
            src.writeline(
                'collections_.insert(collections_.end(), myCollections.begin(), myCollections.end());'
            )

        if len(self.references) != 0:
            src.newline()
            for ref in self.references:
                ref.write_def(src, self.objbranches)

        src.indent -= 1
        src.writeline('}')
        src.newline()

        src.writeline(
            '{NAMESPACE}::{name}::{name}({name} const& _src) :'.format(
                NAMESPACE=NAMESPACE, name=self.name))
        src.indent += 1
        initializers = ['{parent}(_src)'.format(parent=self.parent)]
        for objbranch in self.objbranches:
            initializers.append(objbranch.cpyctor())
        for branch in self.branches:
            branch.init_copy(initializers, context='TreeEntry')
        src.writelines(initializers, ',')
        src.indent -= 1
        src.writeline('{')
        src.indent += 1
        if len(self.objbranches) != 0:
            src.writeline('std::vector<Object*> myObjects{{' + ', '.join(
                ['&{name}'.format(name=b.name)
                 for b in self.objbranches]) + '}};')
            src.writeline(
                'objects_.insert(objects_.end(), myObjects.begin(), myObjects.end());'
            )

        if len(collections) != 0:
            src.writeline(
                'std::vector<CollectionBase*> myCollections{{' +
                ', '.join(['&{name}'.format(name=b.name)
                           for b in collections]) + '}};')
            src.writeline(
                'collections_.insert(collections_.end(), myCollections.begin(), myCollections.end());'
            )

        if len(self.branches) != 0:
            src.newline()
            for branch in self.branches:
                if branch.is_array():
                    branch.write_assign(src, context='TreeEntry')

        if len(self.references) != 0:
            src.newline()
            for ref in self.references:
                ref.write_def(src, self.objbranches)

        src.write_custom_block('{name}.cc.copy_ctor'.format(name=self.name))

        src.indent -= 1
        src.writeline('}')
        src.newline()

        src.writeline('{NAMESPACE}::{name}&'.format(NAMESPACE=NAMESPACE,
                                                    name=self.name))
        src.writeline(
            '{NAMESPACE}::{name}::operator=({name} const& _src)'.format(
                NAMESPACE=NAMESPACE, name=self.name))
        src.writeline('{')
        src.indent += 1
        src.writeline('{parent}::operator=(_src);'.format(parent=self.parent))

        src.newline()
        src.write_custom_block('{name}.cc.operator='.format(name=self.name))
        src.newline()

        if len(self.branches) != 0:
            for branch in self.branches:
                branch.write_assign(src, context='TreeEntry')
            src.newline()
        if len(self.objbranches) != 0:
            for objbranch in self.objbranches:
                objbranch.write_assign(src)
            src.newline()
        if len(self.references) != 0:
            for ref in self.references:
                ref.write_def(src, self.objbranches)
            src.newline()

        src.writeline('return *this;')
        src.indent -= 1
        src.writeline('}')

        src.newline()
        src.writeline('void')
        src.writeline(
            '{NAMESPACE}::{name}::print(std::ostream& _out/* = std::cout*/, UInt_t _level/* = 1*/) const'
            .format(NAMESPACE=NAMESPACE, name=self.name))
        src.writeline('{')
        src.indent += 1
        src.write_custom_block('{name}.cc.print'.format(name=self.name),
                               default='dump(_out);')
        src.indent -= 1
        src.writeline('}')

        src.newline()
        src.writeline('void')
        src.writeline(
            '{NAMESPACE}::{name}::dump(std::ostream& _out/* = std::cout*/) const'
            .format(NAMESPACE=NAMESPACE, name=self.name))
        src.writeline('{')
        src.indent += 1

        if self.parent != 'TreeEntry':
            src.writeline('{parent}::dump(_out);'.format(parent=self.parent))
            src.newline()

        if len(self.branches) != 0:
            for branch in self.branches:
                branch.write_dump(src)
            src.newline()
        if len(self.objbranches) != 0:
            for objbranch in self.objbranches:
                objbranch.write_dump(src)
            src.newline()

        src.indent -= 1
        src.writeline('}')

        src.writeline('/*static*/')
        src.writeline('panda::utils::BranchList')
        src.writeline('{NAMESPACE}::{name}::getListOfBranches()'.format(
            NAMESPACE=NAMESPACE, name=self.name))
        src.writeline('{')
        src.indent += 1
        src.writeline('utils::BranchList blist;')
        if self.parent != 'TreeEntry':
            src.writeline('blist += {parent}::getListOfBranches();'.format(
                parent=self.parent))
            src.newline()

        src.writeline('blist += {{{bnames}}};'.format(bnames=', '.join(
            '"{name}"'.format(name=branch.name) for branch in self.branches
            if '!' not in branch.modifier)))
        for objbranch in self.objbranches:
            src.writeline(
                'blist += {otype}::getListOfBranches().fullNames("{name}");'.
                format(otype=objbranch.objname, name=objbranch.name))
        src.writeline('return blist;')
        src.indent -= 1
        src.writeline('}')
        src.newline()

        src.writeline('/*protected*/')
        src.writeline('void')
        src.writeline(
            '{NAMESPACE}::{name}::doSetStatus_(TTree& _tree, utils::BranchList const& _branches)'
            .format(NAMESPACE=NAMESPACE, name=self.name))
        src.writeline('{')
        src.indent += 1
        if self.parent != 'TreeEntry':
            src.writeline('{parent}::doSetStatus_(_tree, _branches);'.format(
                parent=self.parent))
        for branch in self.branches:
            branch.write_set_status(src, context='TreeEntry')
        src.indent -= 1
        src.writeline('}')
        src.newline()

        src.writeline('/*protected*/')
        src.writeline('panda::utils::BranchList')
        src.writeline(
            '{NAMESPACE}::{name}::doGetStatus_(TTree& _tree) const'.format(
                NAMESPACE=NAMESPACE, name=self.name))
        src.writeline('{')
        src.indent += 1
        src.writeline('utils::BranchList blist;')
        if self.parent != 'TreeEntry':
            src.writeline('blist += {parent}::doGetStatus_(_tree);'.format(
                parent=self.parent))
        src.newline()
        for branch in self.branches:
            branch.write_get_status(src, context='TreeEntry')
        src.writeline('return blist;')
        src.indent -= 1
        src.writeline('}')
        src.newline()

        src.writeline('/*protected*/')
        src.writeline('panda::utils::BranchList')
        src.writeline('{NAMESPACE}::{name}::doGetBranchNames_() const'.format(
            NAMESPACE=NAMESPACE, name=self.name))
        src.writeline('{')
        src.indent += 1
        src.writeline('return getListOfBranches();')
        src.indent -= 1
        src.writeline('}')
        src.newline()

        src.writeline('/*protected*/')
        src.writeline('void')
        src.writeline(
            '{NAMESPACE}::{name}::doSetAddress_(TTree& _tree, utils::BranchList const& _branches, Bool_t _setStatus)'
            .format(NAMESPACE=NAMESPACE, name=self.name))
        src.writeline('{')
        src.indent += 1
        if self.parent != 'TreeEntry':
            src.writeline(
                '{parent}::doSetAddress_(_tree, _branches, _setStatus);'.
                format(parent=self.parent))
            src.newline()

        for branch in self.branches:
            branch.write_set_address(src, context='TreeEntry')
        src.indent -= 1
        src.writeline('}')
        src.newline()

        src.writeline('/*protected*/')
        src.writeline('void')
        src.writeline(
            '{NAMESPACE}::{name}::doBook_(TTree& _tree, utils::BranchList const& _branches)'
            .format(NAMESPACE=NAMESPACE, name=self.name))
        src.writeline('{')
        src.indent += 1
        if self.parent != 'TreeEntry':
            src.writeline('{parent}::doBook_(_tree, _branches);'.format(
                parent=self.parent))
            src.newline()

        for branch in self.branches:
            branch.write_book(src, context='TreeEntry')
        src.indent -= 1
        src.writeline('}')
        src.newline()

        src.writeline('/*protected*/')
        src.writeline('void')
        src.writeline(
            '{NAMESPACE}::{name}::doGetEntry_(TTree& _tree, Long64_t _entry)'.
            format(NAMESPACE=NAMESPACE, name=self.name))
        src.writeline('{')
        src.indent += 1
        if self.parent != 'TreeEntry':
            src.writeline('{parent}::doGetEntry_(_tree, _entry);'.format(
                parent=self.parent))
            src.newline()

        src.write_custom_block('{name}.cc.doGetEntry_'.format(name=self.name))
        src.indent -= 1
        src.writeline('}')
        src.newline()

        src.writeline('void')
        src.writeline('{NAMESPACE}::{name}::doInit_()'.format(
            NAMESPACE=NAMESPACE, name=self.name))
        src.writeline('{')
        src.indent += 1
        if self.parent != 'TreeEntry':
            src.writeline('{parent}::doInit_();'.format(parent=self.parent))
            src.newline()

        for branch in self.branches:
            branch.write_init(src, context='TreeEntry')

        src.write_custom_block('{name}.cc.doInit_'.format(name=self.name))
        src.indent -= 1
        src.writeline('}')
        src.newline()

        if len(self.functions) != 0:
            src.newline()
            for function in self.functions:
                function.write_def(src, context=self.name)

        src.newline()
        src.write_custom_block('{name}.cc.global'.format(name=self.name))

        src.close()
コード例 #5
0
    def generate_header(self):
        header = FileOutput('{PACKDIR}/Objects/interface/{name}.h'.format(
            PACKDIR=PACKDIR, name=self.name))
        header.writeline('#ifndef {PACKAGE}_Objects_{name}_h'.format(
            PACKAGE=PACKAGE, name=self.name))
        header.writeline('#define {PACKAGE}_Objects_{name}_h'.format(
            PACKAGE=PACKAGE, name=self.name))

        header.writeline('#include "../../Framework/interface/Element.h"')

        header.newline()
        header.writeline(
            'namespace {NAMESPACE} {{'.format(NAMESPACE=NAMESPACE))
        header.newline()
        header.indent += 1

        header.writeline('class {name} {{'.format(name=self.name))
        header.writeline('public:')
        header.indent += 1

        if len(self.enums) != 0:
            for enum in self.enums:
                enum.write_decl(header, context='class')
            header.newline()

        if len(self.constants) != 0:
            for constant in self.constants:
                constant.write_decl(header, context='class')
            header.newline()

        header.writeline('struct datastore {')
        header.indent += 1

        for branch in self.branches:
            branch.write_decl(header, context='datastore')

        header.indent -= 1
        header.writeline('};')
        header.newline()

        header.writeline(
            '{name}(datastore&, UInt_t idx);'.format(name=self.name))
        #header.writeline('~{name}();'.format(name = self.name)) # destructor

        header.newline()

        has_public = False
        for branch in self.branches:
            if not hasattr(branch, 'refname') and branch.name.endswith('_'):
                continue

            has_public = True
            branch.write_decl(header, context='Element')

        if has_public:
            header.newline()

        has_protected = False
        for branch in self.branches:
            if hasattr(branch, 'refname') or not branch.name.endswith('_'):
                continue

            if not has_protected:
                header.indent -= 1
                header.writeline('protected:')
                header.indent += 1
                has_protected = True

            branch.write_decl(header, context='Element')

        if has_protected:
            header.newline()

        header.write_custom_block('{name}.h.classdef'.format(name=self.name))

        header.newline()
        header.writeline('static utils::BranchList getListOfBranches();')

        header.indent -= 1

        header.writeline('};')

        header.write_custom_block('{name}.h.global'.format(name=self.name))
        header.newline()

        header.indent -= 1
        header.writeline('}')
        header.newline()

        header.writeline('#endif')
        header.close()
コード例 #6
0
    def generate_source(self):
        subst = {'NAMESPACE': NAMESPACE, 'name': self.name}

        src = FileOutput('{PACKDIR}/Objects/src/{name}.cc'.format(
            PACKDIR=PACKDIR, **subst))
        src.writeline('#include "../interface/{name}.h"'.format(**subst))

        if len(self.enums) != 0:
            src.newline()
            for enum in self.enums:
                enum.write_def(src, cls='{NAMESPACE}::{name}'.format(**subst))

        if len(self.constants) != 0:
            src.newline()
            for constant in self.constants:
                constant.write_def(src,
                                   cls='{NAMESPACE}::{name}'.format(**subst))

        src.newline()
        src.writeline('/*static*/')
        src.writeline('panda::utils::BranchList')
        src.writeline(
            '{NAMESPACE}::{name}::getListOfBranches()'.format(**subst))
        src.writeline('{')
        src.indent += 1
        src.writeline('utils::BranchList blist;')
        if len(self.branches) != 0:
            src.writeline('blist += {{{bnames}}};'.format(bnames=', '.join(
                '"{name}"'.format(name=branch.name) for branch in self.branches
                if '!' not in branch.modifier)))
        src.writeline('return blist;')
        src.indent -= 1
        src.writeline('}')

        src.newline()
        src.writeline(
            '{NAMESPACE}::{name}::{name}(datastore& _data, UInt_t _idx) :'.
            format(**subst))
        src.indent += 1
        initializers = []
        for branch in self.branches:
            branch.init_standard(initializers, context='Element')
        src.writelines(initializers, ',')
        src.indent -= 1
        src.writeline('{')
        src.indent += 1
        for branch in self.branches:
            branch.write_standard_ctor(src, context='Element')
        src.indent -= 1
        src.writeline('}')

        src.newline()
        src.write_custom_block('{name}.cc.global'.format(**subst))
コード例 #7
0
ファイル: relval.py プロジェクト: dr-stringfellow/SUEPTree
def write_header(trees, file_name):

    # Track the number of plots and which objects have been plotted thus far
    num_plots = 0
    parsed_objs = set()

    # Open the writing tool
    writer = FileOutput(file_name)
    writer.writeline(header)

    for tree in trees:
        # We only want to write stuff in 'events' TTree with this tool
        if tree.name in ['Event', 'EventBase']:

            # Plot first branches of single variables in suep::Event (like npv, eventNum, etc.)
            for plot in plot_set(tree):
                if plot in parsed_objs:
                    continue

                parsed_objs.add(plot)

                writer.writeline(
                    template % (num_plots,            # Which plot we're defining
                                'common',             # The output directory of the plot
                                plot.rstrip('()'),    # The name of the plot file (without extension)
                                'std::vector<float> output {float(event.%s)};' % plot))  # The vector of values that fills the histogram
                num_plots += 1

            for obj in tree.objbranches:
                if obj.name in parsed_objs:
                    continue

                parsed_objs.add(obj.name)

                objdef = PhysicsObject.get(obj.objname)
                set_to_plot = plot_set(objdef)

                # Two ways to fill the histograms whether or not this is a collection
                action_string = 'std::vector<float> output {{float(event.' + obj.name + '.{branch})}};'
                if obj.conttype == 'Collection':

                    # Throw in size of the collection
                    plot = 'size()'
                    writer.writeline(template % (num_plots, obj.name, plot.rstrip('()'), action_string.format(branch=plot)))
                    num_plots += 1

                    action_string = """std::vector<float> output;
      for (auto& i : event.{objname})
        output.push_back(i.{{branch}});""".format(objname=obj.name)

                # Print sizes of references
                for refbranch in [b for o in parents(objdef) for b in o.branches]:
                    # Check if actually a refbranch
                    if hasattr(refbranch, 'refname'):
                        name = refbranch.name.rstrip('_')
                        member = 'size' if 'std::vector' in refbranch.type else 'isValid'
                        writer.writeline(template % (num_plots, obj.name, '%s_%s' % (name, member), action_string.format(init='', branch='%s.%s()' % (name, member))))
                        num_plots += 1

                for plot in set_to_plot:
                    assertion = ''
                    sortedby = obj.modifiers.get('sortedby', '').split(',')
                    if sortedby[0] == plot.rstrip('()'):
                        compare = sortedby[1] if len(sortedby) > 1 else 'greater'
                        assertion = '\n      assert(std::is_sorted(output.begin(), output.end(), std::{compare}<float>()));'.format(compare=compare)

                    writer.writeline(template % (num_plots, obj.name, plot.rstrip('()'),
                                                 action_string.format(branch=plot) + assertion))
                    num_plots += 1

    writer.writeline('\n#define NUM_PLOTS %i' % num_plots)
    writer.writeline(footer)