-
Notifications
You must be signed in to change notification settings - Fork 67
How can I follow a TRef? #324
Comments
Actually, I've never thought about how to follow a TRef, so in that sense, the feature does not exist in uproot. However, if you give me the file and point out where the TRef is that you're interested in, I'll see if it can easily be added. I'm sure the TRef contains a number, but the question is how to interpret that number. It would also help if you could tell me what that TRef is supposed to point to, which would help me figure out what relationship that number has with the object it references. |
Wow, that would be very kind of you. Inside the root file there are TRef at "Delphes"->"Track"->"Track.Particle" event_num | track_num | particle_num The other file there is an SQLite database that I created from the root file... because I couldn't find anything that could directly read TRef's in python3. ExRootAnalysis (part of Delphes) was used to make it in c++. The database may be a bit imperfect because it was constructed with a lot of fuzzy equalities, but it should be 99.9% accurate. Including it because there are lots of GUI SQL browsers that you could use if you wanted a quick look at the relations. |
From your file, I implemented Here's an example of how to use them: import uproot
t = uproot.open("issue324.root")["Delphes"]
refs = t["Track.Particle"].array()
refs
# <JaggedArray [
# [<TRef 752> <TRef 766> <TRef 780> ... <TRef 1813> <TRef 1367> <TRef 1666>]
# ...
# [<TRef 745> <TRef 762> <TRef 783> ... <TRef 1863> <TRef 1713> <TRef 1717>]]> These refs[0][0].id
# 752 and following the normal rules of jagged arrays, you can ask for all of those ids in a single swipe: refs.id
# <JaggedArray [
# [752 766 780 ... 1813 1367 1666]
# ...
# [745 762 783 ... 1863 1713 1717]]> Also following the normal rules of jagged arrays, you can pass them into a particle's attributes to pick the particles they refer to, except that these references start at pt = t["Particle.PT"].array()
pt[refs.id - 1]
# <JaggedArray [
# [0.7637838 1.1044897 5.463864 ... 4.252923 1.9702696 9.213475]
# ...
# [1.2523094 0.37887865 0.7390242 ... 1.0288503 3.4785874 1.804613]]> We can see this by looking at the particles' t["Particle.fUniqueID"].array()
# <JaggedArray [
# [1 2 3 ... 1811 1812 1813]
# ...
# [1 2 3 ... 1871 1872 1873]]> and verify that they are strictly off-by-one (in each event, for all events): (t["Particle.fUniqueID"].array() - 1 ==
t["Particle.fUniqueID"].array().localindex
).all().all()
# True I don't know if it is always true that the |
This is now uproot 3.9.1. |
Hi,
It is not always the case, in fact it's just a lucky coincidence that is specific to a particular configuration of Delphes. In general, Delphes overrides this behavior by resetting the counter at the beginning of every event it processes. It also as a global Factory object that preemptively assigns The lucky coincidence is that in the default configuration, usually the references to particles are references to the event-level collection However, if you were to, say, filter out some of these particles before writing them out, they What's worse (and actually the genesis of issue #513), is that it's impossible to use this method to obtain a list of Tower objects from jets. Since Tower objects are created much later on during the event, they have large, random |
@cshimmin Thank you for writing this! Working on your issue got me thinking, "Maybe that's what those Or maybe it should be an option ( |
Apologies for the naive question, I cannot seem to grasp how to follow a TRef to find the component it points to.
For example, I have a root file created with delphes (detector simulator). It has particles (MC truth objects), tracks and towers (observations from the simulated detector). The tracks and towers keep a TRef that points to the particle that created them. I want to follow this TRef and learn the index of the particle that made them. Getting the particle itself would probably be enough to figure out it's index (with some fuzzy comparisons).
The only method I can see on the TRef object is
tref.read(source, cursor, context, parent)
which has no doc-string available.How should I follow a TRef in uproot?
The text was updated successfully, but these errors were encountered: