Drawing a histogram using a TLorentzVector - Error **break** Segmentation Violation

ROOT Version: ROOT 6.26/08, “built for Mac osx arm64”
Platform: Mac OS Monterey, MacBook Air with M1 Chip
Compiler: Clang

Hello everyone, this is my first time posting here and I hope the question I am about to ask is well received. I am fairly new to ROOT so I would like you to keep this in mind when looking at my code as I most likely lack some fundamental understanding in some of the classes that I have decided to use. I am trying to create my own TLorentzVector, filling a histogram with the .CosTheta() method and then drawing the histogram to see the results of my calculation. The purpose is to practice with these classes to later perform a more complete data analysis.

Important to note that this is being done with a previously created root file from a higher up. When I run my code I get the error ** Break ** Segmentation Violation, followed by a long sequence of code which I can post if requested. I have tried debugging my code, referencing posts about the same error and I have not been able to find a solution. I also feel it is important to mention that I initially installed ROOT using homebrew, after receiving this error I tested this same code on a friends computer and it worked for him. This led me to believe that my installation of ROOT was perhaps lacking something I did not quite understand so I reinstalled and built ROOT from the source. After receiving this error once again I have come to the conclusion that the issue might be due to my compiler? Which might explain why the code works on someone else’s computer, or maybe my code is using a class in a way it was not intended to be used. Regardless, I would love some insight from a more experienced ROOT user.

The code that is being used is as follows

void calccos(){

float costh_pbar;
float sinth_pbar;
float phi_pbar; 
float sinphi_pbar; 
float Ppbar; 
float cosphi_pbar; 
double mp = 0.93827208816;

//Creating Tree via TChain, directing branches into previously stated variables and filling them.

TChain tree3("pp");
tree3.Add("pp_all_good_56519.root");
tree3.SetBranchAddress("ctpbarecor", &costh_pbar);
tree3.SetBranchAddress("phipbarecor", &phi_pbar);
tree3.SetBranchAddress("ppbarecor", &Ppbar);


Long64_t nentries = tree3.GetEntries();



//for loop here

TH1D *h1 = new TH1D("h1","h1",10, -2, 2);

	for(int i = 0; i<nentries; i++){

//	for(int i = 0; i<100; i++){
		tree3.GetEntry(i);

		TLorentzVector Pg;
		TLorentzVector Pt;
		TLorentzVector Pbar;
		TLorentzVector P1;

		sinth_pbar = TMath::Sin(TMath::ACos((costh_pbar)));
		cosphi_pbar = TMath::Cos(( phi_pbar * TMath::Pi()));
		sinphi_pbar = TMath::Sin(( phi_pbar * TMath::Pi()));
		
		Pbar.SetPxPyPzE(TMath::Sqrt(TMath::Sq(Ppbar) + TMath::Sq(mp)),
Ppbar*sinth_pbar*cosphi_pbar, Ppbar*sinth_pbar*sinphi_pbar,
		Ppbar*costh_pbar);
		
		
		h1->Fill(Pbar.CosTheta());
		
		//if (sinth_pbar > -10) {
		
		
		cout << Pbar.CosTheta() << " corresponding to entry " << i << endl;
				
		//}
		//	cout<<"i= "<<i << "Cos theta = "<<Pbar.CosTheta()<<endl;
	}
		//TCanvas *test = new TCanvas("","",1024,768);
		
		
		h1->Draw("E");
}

Thank you for your help

ROOT Version: ROOT 6.26/08, “built for Mac osx arm64”
Platform: Mac OS Monterey, MacBook Air with M1 Chip
Compiler:Clang

Hi @naahchill ,

and welcome to the ROOT forum! A few things:

  • a “segmentation violation” happens when some code accesses a memory address that it is not allowed to. this is typically due to a null pointer dereference, a use after delete, or an out-of-bounds array access. since the memory layout of the program depends on the compiler, the compiler options and many other things, it is completely possible that on one computer a program results in a segfault while on another it seems to run fine (but most probably at some point it has read or written some garbage data from a wrong address that still happened to be valid)
  • the stacktrace that ROOT prints after the “Break – segmentation violation” is actually the interesting part of the error report: it tells us where exactly the invalid memory access happened, so please do share it with us

In order to properly debug the problem, I would strongly suggest to make the macro a full, compilable C++ program: basically you will need to add a main function that invokes your macro function and add the missing includes (#include <TH1.h> etc.), like it’s done here: Creating a user application with ROOT - ROOT . Then you can compile the program and run the program under a debugger such as gdb or lldb (there are many tutorials on the internet) or you can compile it with the special flag -fsanitize=address in order to get some diagnostics about invalid memory accesses.

If everything else fails, please share a minimal reproducer of the problem (including any required input files) so that we can take a look.

As a side note, using TLorentzVector is discouraged, Math::LorentzVector being the superior alternative – see the note at ROOT: TLorentzVector Class Reference .

Cheers,
Enrico

Thanks for the quick reply, I will make changes accordingly to see if I can get things working. I am including the stack trace as follows. Thanks again for your help.

[/Users/nicocea/buildir/installdir/lib/libCore.so] TList::FindObject(char const*) const (no debug info)
[/Users/nicocea/buildir/installdir/lib/libCore.so] THashTable::FindObject(char const*) const (no debug info)
[/Users/nicocea/buildir/installdir/lib/libCore.so] TClassTable::Add(char const*, short, std::type_info const&, TClass* (*)(), int) (no debug info)
[/Users/nicocea/buildir/installdir/lib/libCore.so] ROOT::TGenericClassInfo::TGenericClassInfo(char const*, int, char const*, int, std::type_info const&, ROOT::Internal::TInitBehavior const*, TClass* (*)(), TVirtualIsAProxy*, int, int) (no debug info)
[/Users/nicocea/buildir/installdir/lib/libGui.so] ROOT::GenerateInitInstanceLocal(TGHeaderFrame const*) (no debug info)
[/Users/nicocea/buildir/installdir/lib/libGui.so] ROOT::GenerateInitInstance(TGHeaderFrame const*) (no debug info)
[/Users/nicocea/buildir/installdir/lib/libGui.so] _GLOBAL__sub_I_TGFrame.cxx (no debug info)
[/usr/lib/dyld] invocation function for block in dyld4::Loader::findAndRunAllInitializers(dyld4::RuntimeState&) const (no debug info)
[/usr/lib/dyld] invocation function for block in dyld3::MachOAnalyzer::forEachInitializer(Diagnostics&, dyld3::MachOAnalyzer::VMAddrConverter const&, void (unsigned int) block_pointer, void const*) const (no debug info)
[/usr/lib/dyld] invocation function for block in dyld3::MachOFile::forEachSection(void (dyld3::MachOFile::SectionInfo const&, bool, bool&) block_pointer) const (no debug info)
[/usr/lib/dyld] dyld3::MachOFile::forEachLoadCommand(Diagnostics&, void (load_command const*, bool&) block_pointer) const (no debug info)
[/usr/lib/dyld] dyld3::MachOFile::forEachSection(void (dyld3::MachOFile::SectionInfo const&, bool, bool&) block_pointer) const (no debug info)
[/usr/lib/dyld] dyld3::MachOAnalyzer::forEachInitializer(Diagnostics&, dyld3::MachOAnalyzer::VMAddrConverter const&, void (unsigned int) block_pointer, void const*) const (no debug info)
[/usr/lib/dyld] dyld4::Loader::findAndRunAllInitializers(dyld4::RuntimeState&) const (no debug info)
[/usr/lib/dyld] dyld4::Loader::runInitializersBottomUp(dyld4::RuntimeState&, dyld3::Array<dyld4::Loader const*>&) const (no debug info)
[/usr/lib/dyld] dyld4::Loader::runInitializersBottomUp(dyld4::RuntimeState&, dyld3::Array<dyld4::Loader const*>&) const (no debug info)
[/usr/lib/dyld] dyld4::Loader::runInitializersBottomUpPlusUpwardLinks(dyld4::RuntimeState&) const (no debug info)
[/usr/lib/dyld] dyld4::APIs::dlopen_from(char const*, int, void*) (no debug info)
[/Users/nicocea/buildir/installdir/lib/libCling.so] cling::utils::platform::DLOpen(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*) (no debug info)
[/Users/nicocea/buildir/installdir/lib/libCling.so] cling::DynamicLibraryManager::loadLibrary(llvm::StringRef, bool, bool) (no debug info)
[/Users/nicocea/buildir/installdir/lib/libCling.so] TCling::Load(char const*, bool) (no debug info)
[/Users/nicocea/buildir/installdir/lib/libCore.so] TSystem::Load(char const*, char const*, bool) (no debug info)
[/Users/nicocea/buildir/installdir/lib/libCore.so] TROOT::LoadClass(char const*, char const*, bool) (no debug info)
[/Users/nicocea/buildir/installdir/lib/libCling.so] TCling::ShallowAutoLoadImpl(char const*) (no debug info)
[/Users/nicocea/buildir/installdir/lib/libCling.so] TCling::DeepAutoLoadImpl(char const*, std::__1::unordered_set<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::hash<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, std::__1::equal_to<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > >&, bool) (no debug info)
[/Users/nicocea/buildir/installdir/lib/libCling.so] TCling::AutoLoad(char const*, bool) (no debug info)
[/Users/nicocea/buildir/installdir/lib/libCore.so] TClass::LoadClass(char const*, bool) (no debug info)
[/Users/nicocea/buildir/installdir/lib/libCore.so] TPluginHandler::LoadPlugin() (no debug info)
[/Users/nicocea/buildir/installdir/lib/libCore.so] TApplication::LoadGraphicsLibs() (no debug info)
[/Users/nicocea/buildir/installdir/lib/libCore.so] TApplication::InitializeGraphics() (no debug info)
[/Users/nicocea/buildir/installdir/lib/libGpad.so] TCanvas::Init() (no debug info)
[/Users/nicocea/buildir/installdir/lib/libGpad.so] TCanvas::Constructor(char const*, char const*, int) (no debug info)
[/Users/nicocea/buildir/installdir/lib/libGpad.so] TCanvas::TCanvas(char const*, char const*, int) (no debug info)
[/Users/nicocea/buildir/installdir/lib/libGpad.so] TCanvas::MakeDefCanvas() (no debug info)
[<unknown binary>] (no debug info)
[/Users/nicocea/buildir/installdir/lib/libCling.so] cling::IncrementalExecutor::executeWrapper(llvm::StringRef, cling::Value*) const (no debug info)
[/Users/nicocea/buildir/installdir/lib/libCling.so] cling::Interpreter::RunFunction(clang::FunctionDecl const*, cling::Value*) (no debug info)
[/Users/nicocea/buildir/installdir/lib/libCling.so] cling::Interpreter::EvaluateInternal(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, cling::CompilationOptions, cling::Value*, cling::Transaction**, unsigned long) (no debug info)
[/Users/nicocea/buildir/installdir/lib/libCling.so] cling::Interpreter::process(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, cling::Value*, cling::Transaction**, bool) (no debug info)
[/Users/nicocea/buildir/installdir/lib/libCling.so] cling::MetaProcessor::process(llvm::StringRef, cling::Interpreter::CompilationResult&, cling::Value*, bool) (no debug info)
[/Users/nicocea/buildir/installdir/lib/libCling.so] HandleInterpreterException(cling::MetaProcessor*, char const*, cling::Interpreter::CompilationResult&, cling::Value*) (no debug info)
[/Users/nicocea/buildir/installdir/lib/libCling.so] TCling::ProcessLine(char const*, TInterpreter::EErrorCode*) (no debug info)
[/Users/nicocea/buildir/installdir/lib/libCore.so] TROOT::ProcessLine(char const*, int*) (no debug info)
[/Users/nicocea/buildir/installdir/lib/libCore.so] TObject::AppendPad(char const*) (no debug info)
[/Users/nicocea/buildir/installdir/lib/libHist.so] TH1::Draw(char const*) (no debug info)
[<unknown binary>] (no debug info)
[/Users/nicocea/buildir/installdir/lib/libCling.so] cling::IncrementalExecutor::executeWrapper(llvm::StringRef, cling::Value*) const (no debug info)
[/Users/nicocea/buildir/installdir/lib/libCling.so] cling::Interpreter::RunFunction(clang::FunctionDecl const*, cling::Value*) (no debug info)
[/Users/nicocea/buildir/installdir/lib/libCling.so] cling::Interpreter::EvaluateInternal(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, cling::CompilationOptions, cling::Value*, cling::Transaction**, unsigned long) (no debug info)
[/Users/nicocea/buildir/installdir/lib/libCling.so] cling::MetaSema::actOnxCommand(llvm::StringRef, llvm::StringRef, cling::Value*) (no debug info)
[/Users/nicocea/buildir/installdir/lib/libCling.so] cling::MetaParser::isXCommand(cling::MetaSema::ActionResult&, cling::Value*) (no debug info)
[/Users/nicocea/buildir/installdir/lib/libCling.so] cling::MetaParser::isCommand(cling::MetaSema::ActionResult&, cling::Value*) (no debug info)
[/Users/nicocea/buildir/installdir/lib/libCling.so] cling::MetaProcessor::process(llvm::StringRef, cling::Interpreter::CompilationResult&, cling::Value*, bool) (no debug info)
[/Users/nicocea/buildir/installdir/lib/libCling.so] HandleInterpreterException(cling::MetaProcessor*, char const*, cling::Interpreter::CompilationResult&, cling::Value*) (no debug info)
[/Users/nicocea/buildir/installdir/lib/libCling.so] TCling::ProcessLine(char const*, TInterpreter::EErrorCode*) (no debug info)
[/Users/nicocea/buildir/installdir/lib/libCling.so] TCling::ProcessLineSynch(char const*, TInterpreter::EErrorCode*) (no debug info)
[/Users/nicocea/buildir/installdir/lib/libCore.so] TApplication::ExecuteFile(char const*, int*, bool) (no debug info)
[/Users/nicocea/buildir/installdir/lib/libRint.so] TRint::ProcessLineNr(char const*, char const*, int*) (no debug info)
[/Users/nicocea/buildir/installdir/lib/libRint.so] TRint::Run(bool) (no debug info)
[/Users/nicocea/buildir/installdir/bin/root.exe] main (no debug info)
[/usr/lib/dyld] start (no debug info)

Alright, from the stacktrace the line in your program that triggers the problem is h1->Draw(), but the actual crash comes from deep inside ROOT internals. At first glance I don’t understand what’s happening here, but it looks like we should probably take a look on our side. Feel free to share the input file so we can debug further.

Cheers,
Enrico