how to add two LorentzVector? #205
-
I am trying to add two jets together to form a W boson in the >=4 jets category. I load a paquent file, and the events['SelectedJet_*'] type are all cat_4jets_jets_pt = events['SelectedJet_pt'][ak.num(events['SelectedJet_pt']) >= 4]
cat_4jets_jets_eta = events['SelectedJet_eta'][ak.num(events['SelectedJet_eta']) >= 4]
cat_4jets_jets_phi = events['SelectedJet_phi'][ak.num(events['SelectedJet_phi']) >= 4]
cat_4jets_jets_mass = events['SelectedJet_mass'][ak.num(events['SelectedJet_mass']) >= 4]
cat_4jets_jets_LV = vector.awk({
"pt": cat_4jets_jets_pt,
"phi": cat_4jets_jets_eta,
"eta": cat_4jets_jets_phi,
"M": cat_4jets_jets_mass,
})
cat_4jets_jets_LV[0,1] + cat_4jets_jets_LV[0,2] And the last line has a bug : ValueError: cannot broadcast records in this type of operation |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
@Saransh-cpp, this is a problem with the >>> import awkward as ak
>>> pt = ak.Array([[1, 2], [], [3]])
>>> eta = ak.Array([[1, 2], [], [3]])
>>> phi = ak.Array([[1, 2], [], [3]])
>>> mass = ak.Array([[1, 2], [], [3]]) then what I want to create is three lists of vectors, two in the first list and one in the last. But >>> wrong_types = vector.awk({"pt": pt, "eta": eta, "phi": phi, "mass": mass})
>>> print(wrong_types.type)
3 * Momentum4D["rho": var * int64, "phi": var * int64, "eta": var * int64, "tau": var * int64] This is three vectors, and the attributes have list type. Components of vectors may be integers or floats, but they shouldn't be allowed to be lists! What we really wanted in this case was the ak.zip of these four jagged arrays: >>> right_types = vector.awk(ak.zip({"pt": pt, "eta": eta, "phi": phi, "mass": mass}))
>>> print(right_types.type)
3 * var * Momentum4D["rho": int64, "phi": int64, "eta": int64, "tau": int64] so that we now have three lists of vectors. The problem (which @ZhenxuanZhang-Jensen ran into) is that Note: with this correction, it's possible to add vectors. >>> right_types[0, 1] + right_types[2, 0]
<MomentumRecord4D ... phi: 2.61, eta: 2.83, tau: 6.57} type='Momentum4D["rho": f...'> The object and NumPy backends do type-checking. The Awkward Array backend is harder to check like that (if the vectors are buried inside a data structure, they're not represented by a Python type, so we can't run code to check their correctness; also, we don't want correctness checks after every slice!). However, this We wouldn't want the fields of vectors to be boolean or complex, either. (I don't think the object or NumPy backends check that, though.) |
Beta Was this translation helpful? Give feedback.
@Saransh-cpp, this is a problem with the
vector.awk
constructor: it allows users to construct arrays of vectors with incorrect types without any warning. For instance, if I have four jagged arrays with the same list lengths,then what I want to create is three lists of vectors, two in the first list and one in the last. But
vector.awk
is too literal: