Dear ROOT community,
I’m encountering an issue when running a ROOT script called “Analyse3.cpp”. The script is designed to analyze data from four different detectors and plot the evolution of the average number of prompt gammas and the solid angle for each detector.
When I run the script, I get the following error message:
root [0] .X/Analyse3.Cpp
*** Break *** segmentation violation
Generating stack trace...
The code is as follows:
#include <iostream>
#include <TFile.h>
#include <TTree.h>
#include <TString.h>
#include <TGraphErrors.h>
#include <TCanvas.h>
#include <cmath>
#include <cstring>
#include <vector>
#include <TMath.h>
using namespace std;
// Function to calculate the angle theta for detectors 2 and 4
double calculthetad2(double lecture) {
double theta(0);
theta = asin(sin(M_PI/6)*lecture/22.75);
return theta;
}
int Analyse3() {
// Vectors to store the data
vector<double> gamma;
vector<double> omega;
vector<double> Lecture;
vector<double> gammaerror;
// Loop through the 4 detectors
for (int i = 1; i <= 4; i++) {
// Loop through the 11 positions (0 to 10 cm)
for (int j = 0; j <= 10; j++) {
double lecture = j*1.0;
Lecture.push_back(lecture);
// Construct the file path
TString f = Form("~/StageM2/simulation/output/detecteur%d_%d.0cm.root", i, j);
TFile *tf = new TFile(f, "READ");
TTree *tree = (TTree *)tf->Get("PhaseSpace;2");
int Ngamma = 0;
Char_t CreatorProcess, ParticleName, p, n, g;
p = 'p';
g = 'g';
n = 'n';
// Set the branch addresses
tree->SetBranchAddress("ParticleName", &ParticleName);
tree->SetBranchAddress("CreatorProcess", &CreatorProcess);
// Loop through the entries in the tree
for (int index = 0; index < tree->GetEntries(); index++) {
if (tree->GetEntry(index) < 0) {
// Handle the error
continue;
}
// Count the number of gamma particles from proton or neutron inelastic interactions
if (ParticleName == g && (CreatorProcess == p || CreatorProcess == n)) {
Ngamma++;
}
}
gamma.push_back(Ngamma);
gammaerror.push_back(sqrt(Ngamma));
double x, r, z, c;
c = 2.54 * 2.54;
// Calculate the solid angle for each detector
switch (i) {
case 1:
z = 30 - lecture;
omega.push_back(c / (z * z));
break;
case 2:
z = 27.75 - lecture;
x = 12.5;
r = sqrt((z * z) + (x * x));
omega.push_back((c * cos(calculthetad2(lecture))) / (r * r));
break;
case 3:
z = 50 - lecture;
x = 25;
r = sqrt((z * z) + (x * x));
omega.push_back((c * x) / (r * r * r));
break;
case 4:
z = 17.75 + lecture;
x = 12.5;
r = sqrt((z * z) + (x * x));
omega.push_back((c * cos(calculthetad2(lecture))) / (r * r));
break;
default:
cout << "There's a bug somewhere: " << endl;
break;
}
tf->Close();
}
TString title = Form("Detector %d", i);
// Plot the curves
TCanvas *c1 = new TCanvas(title, title, 800, 600);
c1->Divide(1, 2);
c1->cd(1);
TGraphErrors *gr_gamma = new TGraphErrors(gamma.size(), &Lecture[0], &gamma[0], nullptr, &gammaerror[0]);
gr_gamma->SetTitle(title);
gr_gamma->GetXaxis()->SetTitle("Detector position in mm along the z-axis");
gr_gamma->GetYaxis()->SetTitle("Average number of prompt gammas");
gr_gamma->Draw("AP");
c1->cd(2);
TGraphErrors *gr_omega = new TGraphErrors(omega.size(), &Lecture[0], omega.data(), nullptr, nullptr);
gr_omega->SetTitle(title);
gr_omega->GetXaxis()->SetTitle("Detector position in mm along the z-axis");
gr_omega->GetYaxis()->SetTitle("Solid angle in str");
gr_omega->Draw("AP");
// Set the y-axis ranges for each detector
switch(i){
case 1:
c1->cd(1);
gr_gamma->GetYaxis()->SetRangeUser(400,1400);
c1->cd(2);
gr_omega->GetYaxis()->SetRangeUser(0.04, 0.14);
break;
case 2:
c1->cd(1);
gr_gamma->GetYaxis()->SetRangeUser(500,1100);
c1->cd(2);
gr_omega->GetYaxis()->SetRangeUser(0.005, 0.011);
break;
case 3:
c1->cd(1);
gr_gamma->GetYaxis()->SetRangeUser(700,840);
c1->cd(2);
gr_omega->GetYaxis()->SetRangeUser(0.007, 0.014);
break;
case 4:
c1->cd(1);
gr_gamma->GetYaxis()->SetRangeUser(800, 1600);
c1->cd(2);
gr_omega->GetYaxis()->SetRangeUser(0.008, 0.016);
break;
default:
cout<<"There must be a bug somewhere!"<<endl;
break;
}
c1->SaveAs(Form("detecteur_%d_evolution.png", i));
Lecture.clear();
omega.clear();
gamma.clear();
gammaerror.clear();
}
return 0;
}
I’ve tried to debug the code, but I’m not sure where the issue is coming from. The script seems to be accessing the branches of the TTree correctly, but the segmentation violation error suggests that there might be an issue with the memory access.
Could you please help me identify the problem and provide any suggestions on how to resolve it? I would greatly appreciate your assistance.
Thank you in advance for your help.
Best regards,
Houssem