# Segmentation violation root analyse

**URL:** <https://root-forum.cern.ch/t/segmentation-violation-root-analyse/36583>\
**Category:** Newbie\
**Created:** [October 30, 2019, 10:49am UTC](https://root-forum.cern.ch/t/segmentation-violation-root-analyse/36583 "2019-10-30T10:49:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Maxx](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/maxx/32/20072_2.png) [@Maxx](https://root-forum.cern.ch/u/Maxx)\
**Post date:** [October 30, 2019, 10:49am UTC](https://root-forum.cern.ch/t/segmentation-violation-root-analyse/36583/1 "2019-10-30T10:49:03Z")

</div>

Hello,  
my goal is to analyse a root file from a SPECT/PET scan and know for example how many photons where not detected. I am sure there are easier ways to do this specific task, but it is just an example of how to get the error.

_ROOT Version:_ 6.14 as part of vGate 8.2  
_Platform:_ Ubuntu 18.04.2 LTS

To avoid learning too much cpp in a short period of time I used the existing root analyse tools in Gatecontrib/imaging/ROOT\_Analyse. So chances are I just copied something or deleted something I should not have.

As stated in the readme I first run “make” and then “./test1 test\_single.root”  
The program then runs, outputs the desired numbers and then crashes with a segmentation violation. So in theory I don’t need to fix it as I get my results, but is quite unsatisfactory to always have the program crash.

Output:

```auto
 generated: 289
 notabsorbed: 125

 ***Break*** segmentation violation

===========================================================
There was a crash.
This is the entire stack trace of all threads:
===========================================================
#0 0x00007ff2592fa687 in __GI___ waitpid (pid=15785, stat_loc=stat_loc
entry=0x7ffea00044a8, options=options
entry=0) at ../sysdeps/unix/sysv/linux/waitpid.c:30
#1 0x00007ff259265067 in do_system (line=<optimized out>) at ../sysdeps/posix/system.c:149
#2 0x00007ff25a49ca8b in TUnixSystem::StackTrace() () from /usr/share/cern/root-install/lib/libCore.so
#3 0x00007ff25a49f474 in TUnixSystem::DispatchSignals(ESignals) () from /usr/share/cern/root-install/lib/libCore.so
#4 <signal handler called>
#5 0x0000562efe8c9ab7 in main ()
===========================================================

The lines below might hint at the cause of the crash.
You may get help by asking at the ROOT forum http://root.cern.ch/forum
Only if you are really convinced it is a bug in ROOT then please submit a
report at http://root.cern.ch/bugs Please post the ENTIRE stack trace
from above as an attachment in addition to anything else
that might help us fixing this issue.
===========================================================
#5 0x0000562efe8c9ab7 in main ()
===========================================================

```

In other topics there were always more #lines which gave a hint to the problem. Searching the Internet didn’t yield any similar results, so here I am. 😃

my Code:

```auto
#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>

#include <TROOT.h>
#include <TFile.h>
#include <TTree.h>
#include <TH1.h>
#include <TH3.h>
#include <TCanvas.h>
#include <TStyle.h>
#include <TLatex.h>
#include <TApplication.h>
#include <stdbool.h>

using namespace std;
/*!
 *	\fn void printHelpAndQuit( std::string const* msg )
 *	\param msg error message
 */
void printHelpAndQuit( std::string const* msg )
{
	std::cerr << *msg << std::endl;
	std::cerr << "Usage: PET_Analyse <root>" << std::endl;
	std::cerr << "<root> root file to analyse" << std::endl;
	exit( EXIT_FAILURE );
}

int main( int argc, char** argv )
{
	if( argc < 2 )
	{
		std::string const msg = "Arguments missing!!!";
		printHelpAndQuit( &msg );
	}

	gROOT->Reset();

	// Open the root file and check if it exists
	std::string const inputRootFile( argv[1] );
	TFile* f = new TFile( inputRootFile.c_str(), "READ" );
	if( f->IsZombie() )
	{
		std::cerr << "Cannot open the file '" << inputRootFile
						  << "'" << std::endl;
		exit( EXIT_FAILURE );
	}

	TApplication theApp( "Application", &argc, argv );	
	
	TTree* Hits = (TTree*)gDirectory->Get( "Hits" );

	// Declaration of leaves types - TTree Hits
    Int_t trackID;
    Int_t parentID;
    Double_t trackLocalTime;
    Double_t time;
    Float_t edep;
    Float_t stepLength;
    Float_t trackLength;
    Float_t posX;
    Float_t posY;
    Float_t posZ;
    Float_t localPosX;
    Float_t localPosY;
    Float_t localPosZ;
    Float_t momDirX;
    Float_t momDirY;
    Float_t momDirZ;
    Int_t gantryID;
    Int_t rsectorID;
    Int_t moduleID;
    Int_t submoduleID;
    Int_t crystalID;
    Int_t layerID;
    Int_t photonID;
    Int_t nPhantomCompton;
    Int_t nCrystalRayleigh;
    Int_t primaryID;
    Float_t sourcePosX;
    Float_t sourcePosY;
    Float_t sourcePosZ;
    Int_t sourceID;
    Int_t eventID;
    Int_t runID;
    Float_t axialPos;
    Float_t rotationAngle;
    Int_t volumeID;
	Char_t processName;
    Char_t comptVolName;
    Char_t RayleighVolName;
    

// Set branch addresses - TTree Hits
    Hits->SetBranchAddress("trackID", &trackID);
    Hits->SetBranchAddress("parentID", &parentID);
    Hits->SetBranchAddress("trackLocalTime", &trackLocalTime);
    Hits->SetBranchAddress("time", &time);
    Hits->SetBranchAddress("edep", &edep);
    Hits->SetBranchAddress("stepLength", &stepLength);
    Hits->SetBranchAddress("trackLength", &trackLength);
    Hits->SetBranchAddress("posX", &posX);
    Hits->SetBranchAddress("posY", &posY);
    Hits->SetBranchAddress("posZ", &posZ);
    Hits->SetBranchAddress("localPosX", &localPosX);
    Hits->SetBranchAddress("localPosY", &localPosY);
    Hits->SetBranchAddress("localPosZ", &localPosZ);
    Hits->SetBranchAddress("momDirX", &momDirX);
    Hits->SetBranchAddress("momDirY", &momDirY);
    Hits->SetBranchAddress("momDirZ", &momDirZ);
    Hits->SetBranchAddress("gantryID", &gantryID);
    Hits->SetBranchAddress("rsectorID", &rsectorID);
    Hits->SetBranchAddress("moduleID", &moduleID);
    Hits->SetBranchAddress("submoduleID", &submoduleID);
    Hits->SetBranchAddress("crystalID", &crystalID);
    Hits->SetBranchAddress("layerID", &layerID);
    Hits->SetBranchAddress("photonID", &photonID);
    Hits->SetBranchAddress("nPhantomCompton", &nPhantomCompton);
    Hits->SetBranchAddress("nCrystalRayleigh", &nCrystalRayleigh);
    Hits->SetBranchAddress("primaryID", &primaryID);
    Hits->SetBranchAddress("sourcePosX", &sourcePosX);
    Hits->SetBranchAddress("sourcePosY", &sourcePosY);
    Hits->SetBranchAddress("sourcePosZ", &sourcePosZ);
    Hits->SetBranchAddress("sourceID", &sourceID);
    Hits->SetBranchAddress("eventID", &eventID);
    Hits->SetBranchAddress("runID", &runID);
    Hits->SetBranchAddress("axialPos", &axialPos);
    Hits->SetBranchAddress("rotationAngle", &rotationAngle);
    Hits->SetBranchAddress("volumeID", &volumeID);
    Hits->SetBranchAddress("processName", &processName);
    Hits->SetBranchAddress("comptVolName", &comptVolName);
    Hits->SetBranchAddress("RayleighVolName", &RayleighVolName);

	Int_t nentries = Hits->GetEntries();
	//std::cout << "nentries: " << nentries << std::endl;

	Int_t nbytes( 0 );

	// Loop on the events in the TTree Hits

    //additional variables - do I have to declare them here?
    Int_t prevEventID = -1;
    Int_t prevRunID = -1;
    Int_t nnotabsorbed = 0;
    Int_t ngenerated = 0;

	for (Int_t i= 0; i<nentries; i++) {
		nbytes += Hits->GetEntry(i);
		if (eventID != prevEventID) {
            if (runID == prevRunID) {
                nnotabsorbed += eventID-prevEventID-1;
            } else {
                nnotabsorbed += eventID;
                ngenerated += prevEventID+1; // +1 as the eventIDs start at 0
            }
            prevRunID = runID;
            prevEventID = eventID;
        }	
			
	}

    ngenerated += eventID+1;

    cout << " generated: " << ngenerated << endl;
    cout << " notabsorbed: " << nnotabsorbed << endl;

    delete f;
    delete Hits;

	return 0;
}

```

if you need more informations please tell me.

Cheers,  
Maxx

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [October 30, 2019, 1:17pm UTC](https://root-forum.cern.ch/t/segmentation-violation-root-analyse/36583/2 "2019-10-30T13:17:23Z")

</div>

Remove the “`delete Hits;`” statement (this tree is automatically deleted as soon as you “`delete f;`”).

---

<div class="post-metadata">

**Author:** ![Maxx](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/maxx/32/20072_2.png) [@Maxx](https://root-forum.cern.ch/u/Maxx)\
**Post date:** [October 30, 2019, 1:33pm UTC](https://root-forum.cern.ch/t/segmentation-violation-root-analyse/36583/3 "2019-10-30T13:33:12Z")

</div>

Still a segmentation error. But a new line in the stack trace of all threads

#0 0x00007f44f9257687 in \_\_GI\_\_\_waitpid (pid=16661, stat\_loc=stat\_loc  
entry=0x7fff1e3833a8, options=options  
entry=0) at …/sysdeps/unix/sysv/linux/waitpid.c:30  
#1 0x00007f44f91c2067 in do\_system (line=) at …/sysdeps/posix/system.c:149  
#2 0x00007f44fa3f9a8b in TUnixSystem::StackTrace() () from /usr/share/cern/root-install/lib/libCore.so  
#3 0x00007f44fa3fc474 in TUnixSystem::DispatchSignals(ESignals) () from /usr/share/cern/root-install/lib/libCore.so  
#4  
#5 \_\_GI\_\_\_libc\_free (mem=0xffffffff00000001) at malloc.c:3103  
#6 0x0000564c927d5dbb in main ()

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [October 30, 2019, 1:56pm UTC](https://root-forum.cern.ch/t/segmentation-violation-root-analyse/36583/4 "2019-10-30T13:56:31Z")

</div>

Try to remove “`gROOT->Reset();`” and make the “`TApplication theApp(...);`” statement the first in “main” (before anything else).

---

<div class="post-metadata">

**Author:** ![Maxx](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/maxx/32/20072_2.png) [@Maxx](https://root-forum.cern.ch/u/Maxx)\
**Post date:** [October 30, 2019, 2:54pm UTC](https://root-forum.cern.ch/t/segmentation-violation-root-analyse/36583/5 "2019-10-30T14:54:26Z")

</div>

I didn’t try that, but when I saw the “TApplication theApp();” command I realised that I deleted “theApp.Run()” in the end of the code as I thought it wasn’t important. turns out it is. I put the line before the “delete f;” and now it works. Thank you for your help.

(It is still not closing. I have to press ctrl+c to get back to entering commands but as the other Analysing tools have that as well I am sure there is something in the Internet that will help me.)
