LDMX Software
|
The DetDescr repository can optionally be built to enable Python bindings to most of the functionalities of the various DetectorID classes. This requires Boost.Python to be installed, which is part of the the LDMX-software container distribution since version 3.3. Since older versions of the container environment don't come with this library, the feature needs to be explicitly enabled as a CMake option.
Note: Due to a bug, this option currently does not work if you built LDMX-sw with Sanitizer support (one of the -DENABLE_SANITIZER_X=ON
settings). If your Python session crashes after trying to import the libDetDescr
, try recompiling LDMX-sw with Sanitizer support disabled.
To use the DetectorID bindings, you import the libDetDescr.so
shared library as if it was a regular Python module.
To create a particular kind of DetectorID, you can either create a default ID, create it from a Raw integer (which you would typically obtain from the LDMX-sw events), or from the different pieces of the ID
Once you have a valid DetectorID, you can retrieve the corresponding pieces from methods with the same name as the piece and the raw value from the .raw()
method. For example, say that we have an HcalDigiID
that we read from an HcalDigis collection and we wanted to know what the corresponding HcalID
would be.
This would tell us that
numpy
Many python-based analyses use numpy
or numpy
-like libraries (e.g. pandas
and awkward
) which "vectorize" the operation of applying the same function to all of the elements of an array. We can do the same thing here in order to leverage the translation of raw ID numbers (which are stored in the data files) into different detector information. Below is an example of using this class to extract the layer index for each raw id.
This isn't very performant on larger arrays; however, it does allow the user to avoid a raw python for
loop. Effectively, we are using a non-ufunc extension. We could look at incorporating numpy into our Python bindings (via Boost.Python NumPy) to make this as performant as possible.
Boost.Python will automatically generate some documentation for each kind of DetectorID that you can access through the built-in help system in Python.
When you describe a module or object, you'll get a brief description of each of the available methods including their documentation and corresponding C++ signatures. This documentation might look a little bit silly at first glance, since most methods will contain an additional first argument. This is the implicit this
/self
parameter and you can ignore it when using the DetectorID functionality.
As an example, the Cell()
member function of the EcalID class is described as.
The ways you can construct a given DetectorID type is documented by the __init__
function. Since C++ supports overloading constructors while python doesn't, the signature will list the different versions one after another inside a wrapper __init__(...)
function.
For EcalID, this would give us
This tells us that we can construct an EcalID from