uproot5 icon indicating copy to clipboard operation
uproot5 copied to clipboard

Reading from branches of extended TTree

Open lobis opened this issue 2 years ago • 0 comments

I would like to read data from a class (TRestAnalysisTree) that inherits from the standard TTree.

Currently this does not work but uproot correctly reads the object as a tree so perhaps it can also read the branch contents with a few changes.

Using latest uproot (5.0.11).

import uproot

file = uproot.open("simulation.analysis.root")

analysis_tree = file["AnalysisTree"] # is not a TTree but inherits from one
print(analysis_tree.keys()) # works fine
event_tree = file["EventTree"] # event_tree is a TTree
print(analysis_tree) # <TTree 'AnalysisTree' (72 branches) at 0x02e3ec6540d0>
print(event_tree) # <TTree 'EventTree' (4 branches) at 0x02e39dbc5c90>

analysis_branch = analysis_tree["g4Ana_yOriginPrimary"]
analysis_branch.array() # ERROR

The error:


tests\test_derived_tree.py:1 (test_derived_tree)
def test_derived_tree():
        import uproot
    
        input_file = "simulation.anaylsis.root"
    
        file = uproot.open(input_file)
    
        analysis_tree = file["AnalysisTree"]
        event_tree = file["EventTree"]
    
        print(analysis_tree.keys())
        print(event_tree.keys())
    
        print(event_tree["TRestGeant4EventBranch/TRestEvent/TObject/fUniqueID"].array(library="ak"))
    
        analysis_branch = analysis_tree["g4Ana_yOriginPrimary"]
        event_branch = event_tree["TRestGeant4EventBranch/TRestEvent/fOk"]
    
        #analysis_branch._file = event_branch._file
        #analysis_branch._parent = event_branch._parent
    
    
>       analysis_tree["g4Ana_yOriginPrimary"].array()

analysis_branch = <TBranch 'g4Ana_yOriginPrimary' at 0x022534bbf0d0>
analysis_tree = <TTree 'AnalysisTree' (72 branches) at 0x022534ba61d0>
event_branch = <TBranchElement 'fOk' at 0x022534c8d990>
event_tree = <TTree 'EventTree' (4 branches) at 0x022534c29c10>
file       = <ReadOnlyDirectory '/' at 0x022534212c10>
input_file = 'simulation.anaylsis.root'
uproot     = <module 'uproot' from 'C:\\Users\\lobis\\git\\uproot\\src\\uproot\\__init__.py'>

test_derived_tree.py:24: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
..\src\uproot\behaviors\TBranch.py:1768: in array
    decompression_executor, interpretation_executor = _regularize_executors(
        ak_add_doc = False
        array_cache = 'inherit'
        decompression_executor = None
        entry_start = 0
        entry_stop = 20
        interpretation = AsDtype('>f8')
        interpretation_executor = None
        library    = 'ak'
        self       = <TBranch 'g4Ana_yOriginPrimary' at 0x022534bbf0d0>
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

decompression_executor = None, interpretation_executor = None
file = <uproot.reading.DetachedFile object at 0x000002253421D650>

    def _regularize_executors(decompression_executor, interpretation_executor, file):
        if file is None:
            if decompression_executor is None:
                decompression_executor = uproot.source.futures.TrivialExecutor()
            if interpretation_executor is None:
                interpretation_executor = uproot.source.futures.TrivialExecutor()
        else:
            if decompression_executor is None:
>               decompression_executor = file.decompression_executor
E               AttributeError: 'DetachedFile' object has no attribute 'decompression_executor'

decompression_executor = None
file       = <uproot.reading.DetachedFile object at 0x000002253421D650>
interpretation_executor = None

..\src\uproot\behaviors\TBranch.py:2656: AttributeError

I think this come from the reading of the trees as there are some differences, for example:

print(event_tree._file) # <ReadOnlyFile 'simulation.anaylsis.root' at 0x02e3ffa25fd0>
print(analysis_tree._file) # <uproot.reading.DetachedFile object at 0x000002E3EC656A10>

Updating some of these private attributes (e.g. _file) fixes some of the errors but I could not get it working.

Sample root file (zip): simulation.anaylsis.zip

lobis avatar Aug 14 '23 12:08 lobis