# Segmentation Violation when coverting CORSIKA DAT file to ROOT file

**URL:** <https://root-forum.cern.ch/t/segmentation-violation-when-coverting-corsika-dat-file-to-root-file/57850>\
**Category:** Newbie\
**Created:** [January 15, 2024, 4:46pm UTC](https://root-forum.cern.ch/t/segmentation-violation-when-coverting-corsika-dat-file-to-root-file/57850 "2024-01-15T16:46:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![garlicrayburst](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/garlicrayburst/32/35451_2.png) [@garlicrayburst](https://root-forum.cern.ch/u/garlicrayburst)\
**Post date:** [January 15, 2024, 4:46pm UTC](https://root-forum.cern.ch/t/segmentation-violation-when-coverting-corsika-dat-file-to-root-file/57850/1 "2024-01-15T16:46:21Z")

</div>

Dear experts,

I recently started using CORSIKA and I tried using their example file to plot energy from the output DAT file. I using CorsikaRead option and the CorsikaPlotter.cc file that was used is attached.  
[CorsikaPlotter3.cc](https://root-forum.cern.ch/uploads/short-url/rXO8ZVxTmTbp1BZAEnnF2hjq1O4.cc) (1.9 KB)

I recieved a segmentation violation during the conversion and I am unable to figure out the cause.

 ![crash_CRSplotter](https://root-forum.cern.ch/uploads/default/original/3X/0/4/04f60cc7a254798e83c9d3005854fbeec3ca5b8d.png)

I am not able to work further on my analysis due to this, so any help is greatly appreciated!

---

<div class="post-metadata">

**Author:** ![vpadulan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/vpadulan/32/25315_2.png) [@vpadulan](https://root-forum.cern.ch/u/vpadulan)\
**Post date:** [January 15, 2024, 10:12pm UTC](https://root-forum.cern.ch/t/segmentation-violation-when-coverting-corsika-dat-file-to-root-file/57850/2 "2024-01-15T22:12:27Z")

</div>

Dear @garlicrayburst ,

Thank you for reaching out to the forum! I see you are trying to fill a ROOT histogram from the data contained in the input Corsika file (through the MCorsikaReader specifically). By taking a quick look at the program you provided, I noticed the following in the `PlotParticles` function

```cpp
    for (iEntry = Data.ParticlesEnd();
	 iEntry != Data.ParticlesBegin();
	 ++iEntry)

```

I am not familiar with the `MParticleBlock` class, but can it be that the conditions in the loop are inverted here? That is, `ParticlesBegin()` should go before `ParticlesEnd()`?

Otherwise, the stacktrace you report does not show anything going wrong in ROOT specifically.

Cheers,  
Vincenzo

---

<div class="post-metadata">

**Author:** ![garlicrayburst](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/garlicrayburst/32/35451_2.png) [@garlicrayburst](https://root-forum.cern.ch/u/garlicrayburst)\
**Post date:** [January 16, 2024, 6:31am UTC](https://root-forum.cern.ch/t/segmentation-violation-when-coverting-corsika-dat-file-to-root-file/57850/3 "2024-01-16T06:31:24Z")

</div>

So that worked! Thank you so much! I don’t know how I missed that. Sometimes you just need someone who doesn’t have tunnel vision from staring at the code!

---

<div class="post-metadata">

**Author:** ![vpadulan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/vpadulan/32/25315_2.png) [@vpadulan](https://root-forum.cern.ch/u/vpadulan)\
**Post date:** [January 16, 2024, 7:15am UTC](https://root-forum.cern.ch/t/segmentation-violation-when-coverting-corsika-dat-file-to-root-file/57850/4 "2024-01-16T07:15:55Z")

</div>

Dear @garlicrayburst ,

Glad we could get to a solution right away. May I also provide a further suggestion, if the `MParticleBlock` class allows for range-loop iteration, you could avoid this type of error altogether:

```cpp
for (const auto &entry: Data){
// deal with entries
}

```

Cheers,  
Vincenzo

---

<div class="post-metadata">

**Author:** ![garlicrayburst](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/garlicrayburst/32/35451_2.png) [@garlicrayburst](https://root-forum.cern.ch/u/garlicrayburst)\
**Post date:** [January 16, 2024, 7:50am UTC](https://root-forum.cern.ch/t/segmentation-violation-when-coverting-corsika-dat-file-to-root-file/57850/5 "2024-01-16T07:50:09Z")

</div>

Okay great! I will look into that! Thank you for all your help!
