# To Simulate charged-particle tracks using Heed, after avalanche of the ionized electrons will be simulated in single layer GEM detector, but i getting error

**URL:** <https://root-forum.cern.ch/t/to-simulate-charged-particle-tracks-using-heed-after-avalanche-of-the-ionized-electrons-will-be-simulated-in-single-layer-gem-detector-but-i-getting-error/61786>\
**Category:** Garfield++\
**Created:** [September 25, 2024, 8:14pm UTC](https://root-forum.cern.ch/t/to-simulate-charged-particle-tracks-using-heed-after-avalanche-of-the-ionized-electrons-will-be-simulated-in-single-layer-gem-detector-but-i-getting-error/61786 "2024-09-25T20:14:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dk\_ntm](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/54ee81/32.png) [@Dk\_ntm](https://root-forum.cern.ch/u/Dk_ntm)\
**Post date:** [September 25, 2024, 8:14pm UTC](https://root-forum.cern.ch/t/to-simulate-charged-particle-tracks-using-heed-after-avalanche-of-the-ionized-electrons-will-be-simulated-in-single-layer-gem-detector-but-i-getting-error/61786/1 "2024-09-25T20:14:17Z")

</div>

Create the TrackHeed object for primary ionization  
track = ROOT.Garfield.TrackHeed()  
track.SetParticle(‘muon’) # Define the particle type (muon)  
track.SetEnergy(180.e9) # Set particle energy (180 GeV for muon)  
track.SetSensor(sensor)  
track.Initialise(gas, True)# Attach the sensor to TrackHeed  
track.EnableElectricField()

aval = ROOT.Garfield.AvalancheMicroscopic()  
aval.SetSensor(sensor)  
#aval.SetCollisionSteps(100)

zi = 0.5 \* lem\_th + lem\_cpth + 0.1 # Starting z position  
ri = (lem\_pitch / 2) \* np.random.uniform() # Random radius within the LEM pitch  
thetai = np.random.uniform() \* 2 \* np.pi # Random angle  
xi = ri \* np.cos(thetai)  
yi = ri \* np.sin(thetai)

# Launch the charged particle through Heed.

track.NewTrack(xi, yi, zi, 0., 0., 0., 0.)

# Retrieve the clusters of ionization produced by Heed

clusters = track.GetClusters()  
print(“Number of ionization clusters:”, len(clusters))

# Iterate over each cluster

for cluster in clusters:  
# Instead of getting number of electrons, directly process the cluster  
n\_electrons = cluster.GetElectrons() # Assuming this method exists to get electron details  
if n\_electrons is not None:  
print(f"Cluster contains {len(n\_electrons)} electrons.")

```
    # Loop over each electron in this cluster
    for electron in n_electrons:
        # Retrieve the position of the electron
        x_e, y_e, z_e, t_e = electron.GetPositionX(), electron.GetPositionY(), electron.GetPositionZ(), electron.GetTime()
        
        # Optionally, you can print the position and time of the electron
        print(f"Electron: Position: ({x_e}, {y_e}, {z_e}), Time: {t_e}")
        
        # Simulate the avalanche for each electron
        aval.AvalancheElectron(x_e, y_e, z_e, t_e, 0., 0., 0., 0.)

```

## print(“… avalanche complete with”, aval.GetNumberOfElectronEndpoints(), “electron tracks.\n”) when i running this code i’m encountering with error

AttributeError Traceback (most recent call last)  
Cell In[22], line 4  
1 # Iterate over each cluster  
2 for cluster in clusters:  
3 # Instead of getting number of electrons, directly process the cluster  
----\> 4 n\_electrons = cluster.GetElectrons() # Assuming this method exists to get electron details  
5 if n\_electrons is not None:  
6 print(f"Cluster contains {len(n\_electrons)} electrons.")

AttributeError: ‘Cluster’ object has no attribute ‘GetElectrons’

please someone help me my code is correct or incorrect? give some suggestions. I’m totally beginner in #Garfield++

---

<div class="post-metadata">

**Author:** ![hschindl](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/h/977dab/32.png) [@hschindl](https://root-forum.cern.ch/u/hschindl)\
**Post date:** [September 25, 2024, 8:52pm UTC](https://root-forum.cern.ch/t/to-simulate-charged-particle-tracks-using-heed-after-avalanche-of-the-ionized-electrons-will-be-simulated-in-single-layer-gem-detector-but-i-getting-error/61786/2 "2024-09-25T20:52:39Z")

</div>

Hi,  
as you can see from the error message by the interpreter, a `Cluster` object does not have a function `GetElectrons`. To loop over the electrons in a cluster you can do something like this:

```auto
for cluster in clusters:
  for electron in cluster.electrons:
    # ...

```

As suggested in the other thread, please have a look at one of the examples:

> **[Examples/DriftTube/mdt.py · master · garfield / garfieldpp · GitLab](https://gitlab.cern.ch/garfield/garfieldpp/-/blob/master/Examples/DriftTube/mdt.py?ref_type=heads#L78)**
>
> http://garfieldpp.web.cern.ch/garfieldpp/

---

<div class="post-metadata">

**Author:** ![Piet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/piet/32/12536_2.png) [@Piet](https://root-forum.cern.ch/u/Piet)\
**Post date:** [September 25, 2024, 8:55pm UTC](https://root-forum.cern.ch/t/to-simulate-charged-particle-tracks-using-heed-after-avalanche-of-the-ionized-electrons-will-be-simulated-in-single-layer-gem-detector-but-i-getting-error/61786/3 "2024-09-25T20:55:54Z")

</div>

Dear @Dk_ntm

We are very happy you are new to garfield and you find the software valuable to perform your research. We are als happy on the forum to provide help, guidance and discussion. Could you please write clearly what exactly you want to do, and what problem you encounter / what concept you do not understand? You can put snippets of code in your message, or even attach a file with code, but please do not throw in code like this, we are not AI that scans through your code and highlight mistakes and search for the questions or your intentions.

This said, if you are new to garfield, you are very well invited to have a look at the [documentation](https://garfieldpp.web.cern.ch/garfieldpp/documentation/), especially take your time (just one evening) to read through the [manual](https://garfieldpp.web.cern.ch/garfieldpp/documentation/UserGuide.pdf), that is pretty well written, and use the [doxygen](https://garfieldpp.web.cern.ch/documentation/doxygen/html/namespaceGarfield.html) that shows you which are the methods available in each class and what they return. Third, please have a look at the [examples](https://garfieldpp.web.cern.ch/garfieldpp/examples/).

In your specific case I would believe this is way to optimistic programming and is a recipe for running into troubles:

> [@Dk\_ntm](#):
>
> # Assuming this method exists to get electron details

as you can se [here](https://garfieldpp.web.cern.ch/documentation/doxygen/html/classGarfield_1_1TrackHeed.html#structGarfield_1_1TrackHeed_1_1Cluster) cluster is a struct and you can access its vector of electrons through `cluster.electrons`. This you could have found out going through this [example](https://gitlab.cern.ch/garfield/garfieldpp/blob/master/Examples/Heed/edep.C#L67)

Kind regards  
Piet

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [October 9, 2024, 8:56pm UTC](https://root-forum.cern.ch/t/to-simulate-charged-particle-tracks-using-heed-after-avalanche-of-the-ionized-electrons-will-be-simulated-in-single-layer-gem-detector-but-i-getting-error/61786/4 "2024-10-09T20:56:01Z")

</div>

Closed on October 9, 2024, 8:56pm UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
