Segmentation violation after histogram draw

Hi, I’m just beginning to learn ROOT and I’m having a lot of trouble with my second root macro. The code looks like this:

[code]#include “Riostream.h”
#include ""
using namespace std;

void make_hist() {

//Enumerate the data files
string files[] = {"ddx"}; //ddx", "dxd","uux", "uxu"};
int n_files = (sizeof(files)/sizeof(*files));
string infile, outfile;

// // process the text files by converting them to TTrees
// cout << "Processing " << n_files << " files..." << endl;
// for (int i=0;i<n_files;i++) {
// 	infile = "weights_" + files[i];
// 	outfile = files[i] + ".root";
// 	make_tree(infile.c_str(), outfile.c_str());
// }

//Start building a histogram

TH1D *h1 = new TH1D("h1", "Title", 170, 0, 1000);
h1->GetYaxis()->SetTitle("a. u.");
h1->SetMarkerColor(kBlack + 1);
h1->GetYaxis()->SetLabelFont(43); // Absolute font size in pixel (precision 3)

//Loop through each of the TTrees and add it's data to the histogram

TFile *f; TTree *T_p4Hmumu; TTree *T_weight0; TLorentzVector *p4Hmumul; Double_t weight0;
cout << "Finished building TTrees. Getting 4vectors..." << endl;
for (int j=0;j<n_files;j++) {
	outfile = files[j] + ".root";
	cout << "Reading from " << outfile << endl;

	f = TFile::Open(outfile.c_str(),"READ");
	p4Hmumu = new TLorentzVector();
	T_p4Hmumu->SetBranchAddress("p4Hmumu", &p4Hmumu);

	weight0 = 0;
	T_weight0->SetBranchAddress("events", &weight0);

	Int_t nevent = T_p4Hmumu->GetEntries();

	for (Int_t i=0;i<nevent;i++) {
		cout << "Mass was: " << p4Hmumu.M() << " and weight was: " << weight0 << endl;
		h1->Fill(p4Hmumu.M(), weight0);
cout << "No problem yet..." << endl;


cout << "Still no problem..." << endl;

} [/code]

When I run this code from ROOT command line the first time, it sometimes works fine. Usually by the second time, though, it fails with a Segmentation violation at the line where I’m drawing h1. I’ve worked very hard to fix it but have had no success. Am I missing something obvious?

What do you do to run the code? What is the traceback of the crash?

Sorry about that! I should have included the stack trace in my initial post. Here it is along with the commands I made in root:

root [0] .x 
Finished building TTrees. Getting 4vectors...
Reading from test.root
Warning: wrong member access operator '.'
Still in file loop...
No problem yet...
Still no problem...
root [1] .x 

 *** Break *** segmentation violation
 Generating stack trace...
 0x000000010e235592 in G__unloadfile (in + 434
 0x000000010e31bb30 in G__process_cmd (in + 12720
 0x000000010de3d3eb in TCint::ProcessLine(char const*, TInterpreter::EErrorCode*) (in + 859
 0x000000010de3d779 in TCint::ProcessLineSynch(char const*, TInterpreter::EErrorCode*) (in + 121
 0x000000010de89ec6 in TApplication::ExecuteFile(char const*, int*, bool) (in + 2310
 0x000000010ebac516 in TRint::HandleTermInput() (in + 678
 0x000000010de75ca9 in TUnixSystem::CheckDescriptors() (in + 313
 0x000000010de7e96d in TMacOSXSystem::DispatchOneEvent(bool) (in + 429
 0x000000010dee735a in TSystem::InnerLoop() (in + 26
 0x000000010dee71ab in TSystem::Run() (in + 203
 0x000000010de8a0e4 in TApplication::Run(bool) (in + 36
 0x000000010ebabe67 in TRint::Run(bool) (in + 1431
 0x000000010db0be8f in main (in root.exe) + 79
 0x00007fff82f7d5c9 in start (in libdyld.dylib) + 1
Root > !!!Dictionary position not recovered because G__unloadfile() is used in a macro!!!


We cannot try your macro is missing.

I’ve attached the two macros I wrote as well as a test datafile I’ve been using for debugging. These should allow you to reproduce my error.
weights_test.txt (1.91 KB) (1.99 KB) (3.52 KB)

With ROOT 6 I get:

In file included from /Users/couet/Downloads/
/Users/couet/Downloads/ warning: missing terminating '"' character [-Winvalid-pp-token]
/Users/couet/Downloads/ error: expected expression
/Users/couet/Downloads/ warning: missing terminating '"' character [-Winvalid-pp-token]
In file included from input_line_10:1:
/Users/couet/Downloads/ error: function definition is not allowed here
void make_hist() {
-:1:1: error: expected '}'
/Users/couet/Downloads/ note: to match this '{'
void make_tree(char * infile, char * outfile) {

you should not cut the text string on several lines I guess.

Are you compiling the files? I’m just running them by using the root command “.x”

When I do so, using root 5.34 (I’m using this version for compatibility issues), I don’t have any errors due to the code in


Yes I do the same but with ROOT 6.
ROOt 5 goes a bit further (I suggest you fixe the error anyway, try with AClic)

root [0] 
Finished building TTrees. Getting 4vectors...
Reading from test.root
Error in <TFile::TFile>: file test.root does not exist
Error: illegal pointer to class object f 0x0 5
*** Interpreter error recovered ***
root [1] 

Is the not generating a root file? It should produce a test.root file. I’m not familiar with AClic - Would you mind telling me the command I should use to compile this project?

but where/how do you execute ?

To use Aclic, simply do .x a.C+ to execute a.C

I execute it from within In the main function, I first loop through four different text files and convert their values into TLorentzVectors and Double_t values, which I store in a TTree and save to a .root file. Then, I’m trying to read each of these root files, one after the other, and use all of their values to fill a single histogram in the code for

I figured out the compilation; thanks (though now I’m getting new errors that I need to fix)

the part exciting make_tree is commented in you macro:

	// // process the text files by converting them to TTrees
	// cout << "Processing " << n_files << " files..." << endl;
	// for (int i=0;i<n_files;i++) {
	// 	infile = "weights_" + files[i];
	// 	outfile = files[i] + ".root";
	// 	make_tree(infile.c_str(), outfile.c_str());
	// }