Segmentation Violation when coverting CORSIKA DAT file to ROOT file

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 (1.9 KB)

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

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

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

    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

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!

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:

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

Cheers,
Vincenzo

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