# Get the track ids from primary mcparticles

**URL:** <https://allpix-squared-forum.web.cern.ch/t/get-the-track-ids-from-primary-mcparticles/232>\
**Category:** Uncategorized\
**Created:** [November 24, 2021, 9:56am UTC](https://allpix-squared-forum.web.cern.ch/t/get-the-track-ids-from-primary-mcparticles/232 "2021-11-24T09:56:26Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![asantra](https://allpix-squared-forum.web.cern.ch/letter_avatar_proxy/v4/letter/a/ecccb3/32.png) [@asantra](https://allpix-squared-forum.web.cern.ch/u/asantra)\
**Post date:** [November 24, 2021, 9:56am UTC](https://allpix-squared-forum.web.cern.ch/t/get-the-track-ids-from-primary-mcparticles/232/1 "2021-11-24T09:56:27Z")

</div>

Hello,  
I used the _DepositionReader_ module to feed Geant4 energy depositions to Allpix2. I have provided the particle `pdg_id`, `track_id` and `parent_id` with `create_mcparticles` set to be true. Now I am analyzing the digitized output of Allpix. I want to know how I can get the `track_id` of the primary particles in the digitized output. I am trying to do something like this:

> **[tools/root\_analysis\_macros/constructComparisonTree.C · arka-dev · Arka Santra...](https://gitlab.cern.ch/asantra/allpix-squared/-/blob/arka-dev/tools/root_analysis_macros/constructComparisonTree.C#L469-478)**
>
> Generic Pixel Detector Simulation Framework - https://cern.ch/allpix-squared/

but I see that all the `particleTrack`s are nullptrs in this case. I also don’t see any getter to access the particle track\_id in the [MCTrack](https://gitlab.cern.ch/asantra/allpix-squared/-/blob/arka-dev/src/objects/MCTrack.hpp) class. How should I proceed?

Thank you very much,  
Arka

---

<div class="post-metadata">

**Author:** ![simonspa](https://allpix-squared-forum.web.cern.ch/user_avatar/allpix-squared-forum.web.cern.ch/simonspa/32/597_2.png) [@simonspa](https://allpix-squared-forum.web.cern.ch/u/simonspa)\
**Post date:** [November 24, 2021, 3:10pm UTC](https://allpix-squared-forum.web.cern.ch/t/get-the-track-ids-from-primary-mcparticles/232/2 "2021-11-24T15:10:30Z")

</div>

Hi @asantra

the actual `track_id`s provided in the input ROOT tree are not stored. We only use them to figure out the relations between the individual `MCParticle` objects. They are then reflected by their `getParent()` relations.

The relevant code that does the sorting can be found here: [src/modules/DepositionReader/DepositionReaderModule.cpp · master · Allpix Squared / Allpix Squared · GitLab](https://gitlab.cern.ch/allpix-squared/allpix-squared/-/blob/master/src/modules/DepositionReader/DepositionReaderModule.cpp#L334-346)

If you give me a bit of background why you are looking for these IDs maybe we can figure out a way to get your analysis running without that information.

/Simon

---

<div class="post-metadata">

**Author:** ![simonspa](https://allpix-squared-forum.web.cern.ch/user_avatar/allpix-squared-forum.web.cern.ch/simonspa/32/597_2.png) [@simonspa](https://allpix-squared-forum.web.cern.ch/u/simonspa)\
**Post date:** [November 24, 2021, 3:13pm UTC](https://allpix-squared-forum.web.cern.ch/t/get-the-track-ids-from-primary-mcparticles/232/3 "2021-11-24T15:13:00Z")

</div>

Hi @asantra

sorry, just saw the link to your analysis code now. Unfortunately right now the _DepositionReader_ does not generate `MCTrack` objects - simply because a lot more information would be necessary in the trees to generate the relevant object.

You are basically looking for the energy of the `MCParticle`?

/Simon

---

<div class="post-metadata">

**Author:** ![asantra](https://allpix-squared-forum.web.cern.ch/letter_avatar_proxy/v4/letter/a/ecccb3/32.png) [@asantra](https://allpix-squared-forum.web.cern.ch/u/asantra)\
**Post date:** [November 24, 2021, 3:53pm UTC](https://allpix-squared-forum.web.cern.ch/t/get-the-track-ids-from-primary-mcparticles/232/4 "2021-11-24T15:53:51Z")

</div>

Hi @simonspa ,  
My goal is to feed the digitized data (clusters) from Allpix to the tracking algorithm. Then reconstruct the track energy from the tracking algorithm and compare that with the truth track energy. For this comparison, the unique track id will be really helpful in order to resolve ambiguity.

In the `DepositionReader` module, can I add something [here](https://gitlab.cern.ch/allpix-squared/allpix-squared/-/blob/master/src/modules/DepositionReader/DepositionReaderModule.cpp#L294-295) in the MCParticle to store `track_id`? Then get back `track_id` from MCParticle in the digitized output? If this is done, then that is good to go for us for now.

Best,  
Arka

---

<div class="post-metadata">

**Author:** ![simonspa](https://allpix-squared-forum.web.cern.ch/user_avatar/allpix-squared-forum.web.cern.ch/simonspa/32/597_2.png) [@simonspa](https://allpix-squared-forum.web.cern.ch/u/simonspa)\
**Post date:** [November 24, 2021, 4:36pm UTC](https://allpix-squared-forum.web.cern.ch/t/get-the-track-ids-from-primary-mcparticles/232/5 "2021-11-24T16:36:19Z")

</div>

Hi @asantra

yes, of course that would be possible. You could juts add another member to the `MCParticle` object and store the `track_id` in there. I’d suggest to add the track Id then to the MCParticle constructor and set a default value, i.e.

```cpp
MCParticle::MCParticle( /* ... */, int track_id = -1) { /* ... */ }

```

such that you don’t have to change the code everywhere we generate MCParticles. Then you can use the additional information in the DepositionReader to set that info.

/Simon

---

<div class="post-metadata">

**Author:** ![asantra](https://allpix-squared-forum.web.cern.ch/letter_avatar_proxy/v4/letter/a/ecccb3/32.png) [@asantra](https://allpix-squared-forum.web.cern.ch/u/asantra)\
**Post date:** [November 25, 2021, 4:23pm UTC](https://allpix-squared-forum.web.cern.ch/t/get-the-track-ids-from-primary-mcparticles/232/6 "2021-11-25T16:23:27Z")

</div>

Hi @simonspa ,  
Thank you for the tip. I tried to implement this:

A. In **MCParticle** class:

1. I have [added](https://gitlab.cern.ch/asantra/allpix-squared/-/blob/arka-dev/src/objects/MCParticle.hpp#L157) the `track_id_` variable to the `MCParticle` class.
2. The constructor of `MCParticle` class now contains `track_id`. [This](https://gitlab.cern.ch/asantra/allpix-squared/-/blob/arka-dev/src/objects/MCParticle.hpp#L35-42) is in the header file and [this](https://gitlab.cern.ch/asantra/allpix-squared/-/blob/arka-dev/src/objects/MCParticle.cpp#L17-27) is in the cpp file.
3. Added a [getter](https://gitlab.cern.ch/asantra/allpix-squared/-/blob/arka-dev/src/objects/MCParticle.hpp#L83) for `track_id` in the MCParticle class.

B. In **DepositionReader** module:

1. Opened a [map](https://gitlab.cern.ch/asantra/allpix-squared/-/blob/arka-dev/src/modules/DepositionReader/DepositionReaderModule.cpp#L214) to store mcparticle track id.
2. [Here](https://gitlab.cern.ch/asantra/allpix-squared/-/blob/arka-dev/src/modules/DepositionReader/DepositionReaderModule.cpp#L331-335) mcparticle track\_ids are stored.

The setup is compiling and running fine. But when I get the track id from `MCParticle::getParticleTrackID`, I see garbage values. For example, this is the input track ids:

![Screen Shot 2021-11-25 at 17.52.17](https://allpix-squared-forum.web.cern.ch/uploads/default/original/1X/370f3a92717328168478a65ce9c2adfd3ed2613b.png)

and this is what I get from the processed output (x-axis is multiplied by 1e6):

 ![Screen Shot 2021-11-25 at 17.52.32](https://allpix-squared-forum.web.cern.ch/uploads/default/original/1X/555b01319e9715e63fa421db00d1b707deb5c0d5.png)

I am not sure why this is happening. Is there any other place where I am supposed to add `track_id`? I did not change anything in `DatabaseWriter` module, but my understanding is that this is not needed because I am not writing to SQL database. Any heads up will be greatly appreciated.

Thanks once again!  
Arka

---

<div class="post-metadata">

**Author:** ![simonspa](https://allpix-squared-forum.web.cern.ch/user_avatar/allpix-squared-forum.web.cern.ch/simonspa/32/597_2.png) [@simonspa](https://allpix-squared-forum.web.cern.ch/u/simonspa)\
**Post date:** [November 26, 2021, 9:33am UTC](https://allpix-squared-forum.web.cern.ch/t/get-the-track-ids-from-primary-mcparticles/232/7 "2021-11-26T09:33:20Z")

</div>

Hi @asantra

your code looks fine. I would increment the `ClassDefOverride(MCParticle, 9);` to `ClassDefOverride(MCParticle, 10);` because you added a new member. This is ROOT-internal versioning of objects - so skipping the increment might load the object without the new member.

Apart from that I don’t see anything obvious you would need to change. The `DatabaseWriter` should be of no concern to you, as you stated correctly.

Do you see the log output from [here](https://gitlab.cern.ch/asantra/allpix-squared/-/blob/arka-dev/src/modules/DepositionReader/DepositionReaderModule.cpp#L290) with the IDs you are expecting?

/Simon

---

<div class="post-metadata">

**Author:** ![asantra](https://allpix-squared-forum.web.cern.ch/letter_avatar_proxy/v4/letter/a/ecccb3/32.png) [@asantra](https://allpix-squared-forum.web.cern.ch/u/asantra)\
**Post date:** [November 29, 2021, 2:10pm UTC](https://allpix-squared-forum.web.cern.ch/t/get-the-track-ids-from-primary-mcparticles/232/8 "2021-11-29T14:10:07Z")

</div>

Hi @simonspa ,  
Apparently changing `ClassDefOverride(MCParticle, 9);` to `ClassDefOverride(MCParticle, 10);` did the trick. I now get sensible MCParticle track ids in the output. This solves my problem!  
Many thanks,  
Arka

---

<div class="post-metadata">

**Author:** ![simonspa](https://allpix-squared-forum.web.cern.ch/user_avatar/allpix-squared-forum.web.cern.ch/simonspa/32/597_2.png) [@simonspa](https://allpix-squared-forum.web.cern.ch/u/simonspa)\
**Post date:** [November 29, 2021, 2:44pm UTC](https://allpix-squared-forum.web.cern.ch/t/get-the-track-ids-from-primary-mcparticles/232/9 "2021-11-29T14:44:39Z")

</div>

Perfect, then it really was ROOT getting confused about which members to expect in the class. 🙂
