Particle
The Particle
class serves as a base class designed to simplify the process of converting NTuple information into a Python object.
It offers a flexible approach to initializing particle attributes directly from the branches of TTree event entries or
from keyword arguments (kwargs) that provide values, evaluable expressions, or paths to callable methods for determining the attributes.
If specific class methods are required, you can always create a new class that inherits from this base class and invoke its constructor.
The following example demonstrates two different ways to create a particle instance:
(key, value)
for key, value in self.__dict__.items()
if key not in {"index", "name"}
)
)
if __name__ == "__main__":
import os
# Example usage
input_file= os.path.abspath(
os.path.join(
os.path.dirname(__file__),
"../../test/ntuples/DTDPGNtuple_12_4_2_Phase2Concentrator_thr6_Simulation_99.root",
)
)
# case 1 : Directly set attributes
particle = Particle(index=0, wh=-2, sc=1, st=1, detector_side={ "expr": "'+z' if wh > 0 else '-z'"})
print(particle)
# case 2 : Set attributes from a TTree event entry. Input file is a dt ntuple here
from ROOT import TFile
Output
>> Particle 0 info -->
+ Wh: -2, Sc: 1, St: 1, Detector_side: -z
>> GenMuon 0 info -->
+ Pt: 258.0812683105469, Eta: -1.9664770364761353, Phi: 0.5708979964256287, Charge: -1
There are some specific classes already implemented (see particles) that define their own useful class methods,
you can take them as reference. If you want to implement your own class, you can also use the dtpr
CLI command
to generate a skeleton for a particle that inherits from the Particle
class as follows:
dtpr create-particle -o [output_folder] --name TestParticle
This will create a new file called testparticle.py
in the specified output folder with the following
content:
# Generic Particle template generated by dtpr-package on Wed Apr 09 22:54:25 2025.
# Author:
# [Your Name]
# Version:
# 0.0
#
# This class is a template for a generic particle class. It inherits from dtpr.base.Particle which
# provides needed methods such as '__str__' '_init_from_ev' and '_init_from_dict'. Implement other
# class methods as needed.
from dtpr.base import Particle
class TestParticle(Particle):
def __init__(self, index, ev=None, branches=None, **kwargs):
"""
Initialize a TestParticle instance.
description here...
:param index: The index of the TestParticle.
:type index: int
.
. (add more parameters here if needed)
.
"""
super().__init__(index, ev, branches, **kwargs)
if __name__ == '__main__':
# Test the class here
particle_instance = TestParticle(1)
print(particle_instance)
By inheriting from Particle
, the class gains the ability to initialize its attributes seamlessly
from TTree branches or dictionary-like mappings. Additionally, it includes a custom __str__
method
that provides a visually enhanced string representation by utilizing the color_msg
function from
dtpr.utils.functions
to apply color formatting to the output. The class is also equipped with a
__eq__
method, enabling comparison between two instances.