-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathROOT_utils.py
89 lines (76 loc) · 3.17 KB
/
ROOT_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import ROOT
def create_lorentz_vector(pt, eta, phi, mass):
vec = ROOT.TLorentzVector()
vec.SetPtEtaPhiM(pt, eta, phi, mass)
return vec
def single_entry_sum_particles(tree, branches, truthM, truthID, MET_ids=[]):
"""
Sums the lorentz vectors for each particle, excluding MET particles,
Parameters:
-----------
tree : ROOT.TTree
Contains particle information with branches for Id, Pt, Eta, and Phi
branches : list of str
List of branch name prefixes. Each prefix should have corresponding
branches - prefix+'Id', prefix+'Pt', prefix+'Eta', prefix+'Phi'
truthM : list
List of true mass values corresponding to truthID indices
truthID : list
List of particle IDs that map to the truthM masses
MET_ids : list, optional
List of Missing Transverse Energy (MET) IDs to exclude from feature extraction
Returns:
--------
TLorentzVector
Vector sum of all particles in the current TTree entry
"""
sum_vec = ROOT.TLorentzVector()
for name in branches:
Id = list(getattr(tree, name + "Id"))
Pt = list(getattr(tree, name + "Pt"))
Eta = list(getattr(tree, name + "Eta"))
Phi = list(getattr(tree, name + "Phi"))
for j in range(len(Id)):
if (Id[j] in MET_ids):
continue
mass = truthM[truthID.index(Id[j])]
vec = create_lorentz_vector(Pt[j], Eta[j], Phi[j], mass)
sum_vec += vec
return sum_vec
def single_entry_extract_features(tree, branches, truthM, truthID, MET_ids=[], clip_eta=False):
"""
Extracts particle features, excluding MET particles, and optionally filtering out events with extreme eta
Parameters:
-----------
tree : ROOT.TTree
Contains particle information with branches for Id, Pt, Eta, and Phi
branches : list of str
List of branch name prefixes. Each prefix should have corresponding
branches - prefix+'Id', prefix+'Pt', prefix+'Eta', prefix+'Phi'
truthM : list
List of true mass values corresponding to truthID indices
truthID : list
List of particle IDs that map to the truthM masses
MET_ids : list, optional
List of Missing Transverse Energy (MET) IDs to exclude from feature extraction
clip_eta : bool, optional
If True, returns empty list for particles with |eta| > 3.4
Returns:
--------
list
Flattened list of features in the order [pt, eta, phi, mass] for each particle
"""
features = []
for name in branches:
Id = list(getattr(tree, name + "Id"))
Pt = list(getattr(tree, name + "Pt"))
Eta = list(getattr(tree, name + "Eta"))
Phi = list(getattr(tree, name + "Phi"))
for j in range(len(Id)):
if (Id[j] in MET_ids):
continue
if (clip_eta and abs(Eta[j]) > 3.4):
return [] # Aggressive strategy: Drop the entire event if any particle has Eta > 3.4
mass = truthM[truthID.index(Id[j])] # Find index of ID, access corresponding mass value at that index
features += [Pt[j], Eta[j], Phi[j], mass]
return features