Please read tips for efficient and successful posting and posting code
_ROOT Version:_ROOT 6.26/10
_Platform:_c++ (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0
Hi, I was writing a root macro to open four root data files named 1.root,3.root, 7.root and 13.root using the following script
void plothistogram()
{
// Create a TCanvas for plotting histograms
TCanvas *canvas = new TCanvas("canvas", "Histograms", 800, 600);
// Define the root file names
TString file_names[4] = {
"1.root",
"5.root",
"7.root",
"13.root"
};
// Create a histogram for each file
TH1F *histograms[4];
// Loop over the root files
for (Int_t file_index = 0; file_index < 4; ++file_index)
{
// Open the root file
TFile *root_file = new TFile(file_names[file_index], "read");
// Get the tree containing the data
TTree *tree = dynamic_cast<TTree *>(root_file->Get("B4"));
// Create a histogram for this file
histograms[file_index] = new TH1F(Form("hist%d", file_index), "Histogram", 500, 0, 5);
// Read the data from the branch "Esil"
double data;
tree->SetBranchAddress("Esil", &data);
// Loop over the entries in the tree
int num_entries = tree->GetEntries();
for (int i = 2; i < num_entries; ++i)
{
// Read the entry from the tree
tree->GetEntry(i);
// Fill the histogram with the data
histograms[file_index]->Fill(data);
}
// Close the root file
root_file->Close();
}
// Set the position on the canvas for the first histogram
canvas->cd();
// Draw the first histogram
histograms[0]->Draw();
// Overlay the histograms
for (Int_t i = 1; i < 4; ++i)
{
histograms[i]->SetLineColor(i + 1);
histograms[i]->Draw("SAME");
}
}
But when I run the code I am getting the segmentation error
root [0] .x plothistogram.c
*** Break *** segmentation violation
===========================================================
There was a crash.
This is the entire stack trace of all threads:
===========================================================
#0 0x00007fea850ea45a in __GI___wait4 (pid=12464, stat_loc=stat_loc
entry=0x7fff17291c98, options=options
entry=0, usage=usage
entry=0x0) at ../sysdeps/unix/sysv/linux/wait4.c:30
#1 0x00007fea850ea41b in __GI___waitpid (pid=<optimized out>, stat_loc=stat_loc
entry=0x7fff17291c98, options=options
entry=0) at ./posix/waitpid.c:38
#2 0x00007fea85050bcb in do_system (line=<optimized out>) at ../sysdeps/posix/system.c:171
#3 0x00007fea85b12d64 in TUnixSystem::StackTrace() () from /home/name/PhD/software/root/root-6.26.10-install/lib/libCore.so
#4 0x00007fea85b10075 in TUnixSystem::DispatchSignals(ESignals) () from /home/name/PhD/software/root/root-6.26.10-install/lib/libCore.so
#5 <signal handler called>
#6 0x00007fea8562c7e0 in ?? ()
#7 0x0000000000219ce0 in ?? ()
#8 0x00007fea810c0880 in ?? () from /home/name/PhD/software/root/root-6.26.10-install/lib/libCling.so
#9 0xffffffffffffffff in ?? ()
#10 0x000055e84a7bc100 in ?? ()
#11 0x000055e84caee950 in ?? ()
#12 0x000055e84bbce7c0 in ?? ()
#13 0x0000000000000003 in ?? ()
#14 0x0000000000000000 in ?? ()
===========================================================
The lines below might hint at the cause of the crash.
You may get help by asking at the ROOT forum https://root.cern/forum
Only if you are really convinced it is a bug in ROOT then please submit a
report at https://root.cern/bugs Please post the ENTIRE stack trace
from above as an attachment in addition to anything else
that might help us fixing this issue.
===========================================================
#6 0x00007fea8562c7e0 in ?? ()
#7 0x0000000000219ce0 in ?? ()
#8 0x00007fea810c0880 in ?? () from /home/name/PhD/software/root/root-6.26.10-install/lib/libCling.so
#9 0xffffffffffffffff in ?? ()
#10 0x000055e84a7bc100 in ?? ()
#11 0x000055e84caee950 in ?? ()
#12 0x000055e84bbce7c0 in ?? ()
#13 0x0000000000000003 in ?? ()
#14 0x0000000000000000 in ?? ()
===========================================================
I am pretty confident about the fact that the root file contains the tree named B4 and the branch is named Esil. I have successfully plotted them individually using another macro. I am pretty unsure if it is some problem with my code or some bug. Any help will be deeply appreciated.