Newbie Break Segmentation violation: libRint libCore libCling


I have just started working with root two days ago. I have created this macro that im gonna write up here since i am not allowed to post links as a new user. Its supposed to fill a 10 by 10 vector with random values between 0 and 2 pi, and then take every two elements in a column and substract them. It is an intro exercise I’m trying to do as preparation for my bachelor thesis, and ive never worked with root or c++ before. The file is called test3.C

#include <random>
#include <vector>
#include <iostream>

using namespace std;

#include "TStyle.h"
#include "TH1.h"
#include "TF1.h"
#include "TRandom3.h"
#include "TCanvas.h"
#include "iostream"

void test3(Int_t nEvents) 

	// create histogram that we will fill with random values
	TH1D* hNormal = new TH1D("hNormal", "ROOT func generated constant distribution; x; Counts", 100, 0, 2*TMath::Pi());
	// Define the function we want to generate
	TF1* normFunc = new TF1("normFunc", "100", 0, 2*TMath::Pi());

	vector<double> vec;
	for(Int_t n = 0; n < nEvents+1; n++) 
			cout << "event " << n+1 << endl;
		Double_t e = normFunc->GetRandom();
	Int_t n = 10;
	Int_t m = 10;
	vector<double> row(m);
	vector<vector<double>> mat(n, row);

	for(Int_t i = 0; i < n; i++)
		for(Int_t j = 0; j < m; j++)
		mat[i][j] = vec[(i+1)*(j+1)];
				cout << "event " << (i+1)*(j+1) << endl;
	cout << " ";
	vector<double> deltaphiVec;
	for (int k = 0; k < m; k++)
		for (int l=0; l<m; m++)
			for (int p=0; p<m; p++)
				for (int q=0; q<m; q++)
					if (k==p && l==q)
						double deltaphi = mat[k][l]-mat[p][q];
						if ( deltaphi <0)
	// Set ROOT drawing styles
	// create canvas for hSin
	TCanvas* c1 = new TCanvas("c1", "normal canvas", 900, 600);
  	// create 1d function that we will use to fit our generated data to ensure
  	// that the generation works
  	TF1* fitFunc = new TF1("fitFunc", "[0]*100", 0, 2*TMath::Pi());
  	fitFunc->SetParameter(0, 10);
  	// Save the canvas as a picture


(I know the nested for loops are horrible) and when I run it using .x test3.C(100) it generates an error with this traceback:

 *** Break *** segmentation violation
           Generating stack trace...
 0x00007f9af46a8848 in <unknown> from /snap/root-framework/910/usr/local/lib/
 0x00007f9af3e5f090 in <unknown> from /lib/x86_64-linux-gnu/
 0x00007f9ad59fec77 in <unknown function>
 0x00007f9aef75ac9f in <unknown> from /snap/root-framework/910/usr/local/lib/
 0x00007f9aef6cf035 in <unknown> from /snap/root-framework/910/usr/local/lib/
 0x00007f9aef6d1f85 in <unknown> from /snap/root-framework/910/usr/local/lib/
 0x00007f9aef7c5f49 in <unknown> from /snap/root-framework/910/usr/local/lib/
 0x00007f9aef7d45f5 in <unknown> from /snap/root-framework/910/usr/local/lib/
 0x00007f9aef7d5ea8 in <unknown> from /snap/root-framework/910/usr/local/lib/
 0x00007f9aef7bf300 in <unknown> from /snap/root-framework/910/usr/local/lib/
 0x00007f9aef4dc459 in <unknown> from /snap/root-framework/910/usr/local/lib/
 0x00007f9aef4dceef in TCling::ProcessLine(char const*, TInterpreter::EErrorCode*) + 0x8e3 from /snap/root-framework/910/usr/local/lib/
 0x00007f9aef4e0a82 in TCling::ProcessLineSynch(char const*, TInterpreter::EErrorCode*) + 0xa6 from /snap/root-framework/910/usr/local/lib/
 0x00007f9af4510a62 in TApplication::ExecuteFile(char const*, int*, bool) at TApplication.cxx:? from /snap/root-framework/910/usr/local/lib/
 0x00007f9af45101c0 in TApplication::ProcessFile(char const*, int*, bool) + 0x34 from /snap/root-framework/910/usr/local/lib/
 0x00007f9af450ffab in TApplication::ProcessLine(char const*, bool, int*) + 0xda9 from /snap/root-framework/910/usr/local/lib/
 0x00007f9af48d33d7 in TRint::ProcessLineNr(char const*, char const*, int*) + 0x315 from /snap/root-framework/910/usr/local/lib/
 0x00007f9af48d2a36 in TRint::HandleTermInput() + 0x344 from /snap/root-framework/910/usr/local/lib/
 0x00007f9af48cff59 in TTermInputHandler::Notify() + 0x35 from /snap/root-framework/910/usr/local/lib/
 0x00007f9af48d4db1 in TTermInputHandler::ReadNotify() + 0x29 from /snap/root-framework/910/usr/local/lib/
 0x00007f9af46b26df in TUnixSystem::CheckDescriptors() + 0x121 from /snap/root-framework/910/usr/local/lib/
 0x00007f9af46b193c in TUnixSystem::DispatchOneEvent(bool) + 0x178 from /snap/root-framework/910/usr/local/lib/
 0x00007f9af458b577 in TSystem::InnerLoop() + 0x45 from /snap/root-framework/910/usr/local/lib/
 0x00007f9af458b2e8 in TSystem::Run() + 0xf4 from /snap/root-framework/910/usr/local/lib/
 0x00007f9af4510c33 in TApplication::Run(bool) + 0x61 from /snap/root-framework/910/usr/local/lib/
 0x00007f9af48d1d46 in TRint::Run(bool) + 0x8da from /snap/root-framework/910/usr/local/lib/
 0x00005618eb8ec5dd in main + 0x87 from /snap/root-framework/910/usr/local/bin/root.exe
 0x00007f9af3e40083 in __libc_start_main + 0xf3 from /lib/x86_64-linux-gnu/
 0x00005618eb8ec24e in _start + 0x2e from /snap/root-framework/910/usr/local/bin/root.exe

I am not sure what this means. Plus, every time this happens, I cannot run other macros that generally work more than once. i have to quit and reenter root every time i want to run another macro, until i restart the terminal or the computer several times.

Thank you in advance,

ROOT Version: 6.26/06
Platform: Ubuntu 20.04
installed from source code

Hi @bizarre,

I cannot reason about the correctness of your code, as I don’t know what you are trying to do and your code seems “a bit confusing”.

However, here’s is what seems to be causing the issue

To me, this should read (i.e. you should be incrementing the loop index instead):

		for (int l=0; l<m; l++)


This was most definetely the issue. Thank you! I have not seen this during the entire 10 hours ive looked at it. Thanks a lot!