Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement four-vector tools #9

Open
sam-may opened this issue Mar 2, 2021 · 4 comments
Open

Implement four-vector tools #9

sam-may opened this issue Mar 2, 2021 · 4 comments
Labels
enhancement New feature or request

Comments

@sam-may
Copy link
Collaborator

sam-may commented Mar 2, 2021

It will be useful to have the options to compute four-vector related quantities.

For example, we may want to build an H->TauTau candidate out of two hadronic taus/leptons and compute its pT and eta, compute its deltaR with respect to photons/diphoton, etc.

Given that we already have many useful quantities saved in the skims (gg_pt, gg_eta, SVFit quantities), this is not super urgent, but will be necessary if we want to do things without remaking skims.

I'd suggest we make a PhysicsTools directory inside Preselection and build either a class or a set of functions in e.g. four_vector_utils.py. The main functionality we'd want is the ability to add two four vectors together and then return the resulting four vectors properties (pT, eta, phi, mass). Once we have this, we can do things like compute dR(H->TauTau, H->gg) with existing tools for calculating delta R (these need to be cleaned up as well).

@sam-may sam-may added the enhancement New feature or request label Mar 2, 2021
@fgolf
Copy link

fgolf commented Mar 4, 2021 via email

@sam-may
Copy link
Collaborator Author

sam-may commented Mar 4, 2021

Hi Frank, this would definitely be useful, please do point us to it.

@aminnj
Copy link

aminnj commented Mar 4, 2021

You can also use uproot3_methods:

import numpy as np

# prints warning because of awkward1 vs awkward0, but harmless
import uproot3_methods

# also TLorentzVector (for single p4)
# also accepts jagged arrays (of awkward0 type, so might need to do awkard1.to_awkward0(arr))
# also from_cartesian, ...
p4s = uproot3_methods.TLorentzVectorArray.from_ptetaphim(
    np.array([25., 25., 25.]),
    np.array([0., 0., 0.]),
    np.array([0., 0., 0.]),
    np.array([0., 0., 0.]),
    )

# also boosts and many other things
print((p4s + p4s).pt)
print((p4s + p4s).mass)
print(p4s.delta_phi(p4s))
print(p4s.delta_r(p4s))

and if I understand correctly, the longterm replacement for that is vector, but the readme says it's still under heavy development.

A useful code snippet is https://github.com/aminnj/pdroot/blob/d8b6908e3bffe2c451333efcc119a05929474dc9/pdroot/accessors.py#L61-L73:

@pd.api.extensions.register_dataframe_accessor("p4")
class LorentzVectorAccessor:
    def __init__(self, pandas_obj):
        self._obj = pandas_obj

    def __call__(self, which):
        components = [f"{which}_{x}" for x in ["pt", "eta", "phi", "mass"]]
        missing_columns = set(components) - set(self._obj.columns)
        if len(missing_columns):
            raise AttributeError("Missing columns: {}".format(missing_columns))
        arrays = (self._obj[c] for c in components)
        return uproot3_methods.TLorentzVectorArray.from_ptetaphim(*arrays)

If you do things with pandas, this will add an accessor (like df.mystring.str.split()) for LorentzVectors. That is, if you have
columns mu_pt, mu_eta, ... in a pandas DataFrame, you can do df.p4("mu") to get the uproot3_methods.TLorentzVectorArray array, and thus do things like (df.p4("mu1")+df.p4("mu2")).mass. You can of course replace the __call__ with whatever you you want in terms of LV classes.

@sam-may
Copy link
Collaborator Author

sam-may commented Mar 5, 2021

This is great, thanks Nick!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants