Invariant mass calculations with the vector package #117
Unanswered
JanFSchulte
asked this question in
Q&A
Replies: 1 comment 1 reply
-
Happy to report that this worked for me! |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi,
to preserve an email exchange I just had with @jpivarski:
Question:
I am currently trying to update the LPC Muon HATS for this year. The exercise is based on nanoAOD and was based on awkward 0.x and used the uproot_methods package to do TLorentzVector-related stuff. To not teach updated things, I am trying to get it work with awkward 1.x and using the vector package as a replacement for the TLorentzVectors.
Last years version of the exercise can be found here: https://github.com/Taiantuia/MuonHATSatLPC2020/blob/master/Exercise-2-Muon-momentum-scale-and-resolution-corrections.ipynb
I am basically trying to replace these two lines:
two_muon_p4=uproot_methods.TLorentzVectorArray.from_ptetaphim(two_opposite_good_muons.muon_pt, two_opposite_good_muons.muon_eta, two_opposite_good_muons.muon_phi, two_opposite_good_muons.muon_mass)
dimuon_p4=two_muon_p4[:,0]+two_muon_p4[:,1
Based on the vector documentation, I am creating an awkward array using
two_muon_p4=vector.awk({"pt":two_opposite_good_muons["Muon_pt"], "eta":two_opposite_good_muons["Muon_eta"],
"phi":two_opposite_good_muons["Muon_phi"], "M":two_opposite_good_muons["Muon_mass"]})
And from what I can see, I get the expected structure of nested arrays where there are two vectors in each sub-array. Now I am trying to combine them as before with
dimuon_p4=two_muon_p4[:,0]+two_muon_p4[:,1]
which fails with
ValueError: cannot broadcast records in this type of operation
I am currently stuck at this point as it is not clear to me how the correct syntax would be doing this with vector objects. Could you point me in the right direction?
Thanks a lot!
Jan
And answer:
An answer that could be moved there is this: we're still working out what would be the best ways to access it, which is something that has to be done with users, to see what works well in their workflows. I really don't like the name "vector.awk", and there are ways of using that function that builds incorrect Awkward Array structures. (To keep using this function, we'd at least have to have it sanitize the input so that what it builds is either right or an error.)
What about the following technique? Vector's Awkward backend is just a "behavior" in Awkward Array (see documentation), which can be installed globally for all Awkward Arrays with "vector.register_awkward()".
Then, any array with the right-named record and right-named fields would have all of the Lorentz vector functions. This array is pretty close to what you already have:
If you need to rename fields, there's ak.zip. It's a generic Awkward Array because the records don't have names. The Lorentz vector methods have all been registered to the name "Momentum4D", so we could just rename it that. (with_name is an argument of ak.zip, too.)
Now it knows about all of the Lorentz vector methods:
etc. By contrast, the original muons raises exceptions if you try to do these things (can't broadcast "+" through records, doesn't have a method or field named "deltaR"). As long as the record name is "Momentum4D," the field names are acceptable (x, y, rho, z, t, tau, etc. are geometrical names; px, py, pt, pz, energy, mass, etc. are momentum synonyms—interchangeable on an array that has been declared "Momentum4D", rather than "Vector4D"), and as long as the behaviors have been registered, these records are Lorentz vectors.
Beta Was this translation helpful? Give feedback.
All reactions