Hello everyone,
I’m currently working with a map of 128 pairs of vectors, which I use to create 128 scatter plots. My current approach is shown in the code below:
void save_scatter_plot_with_tgraph(const std::vector<double>& vector1, const std::vector<double>& vector2, const std::string& file_name) {
// Check if the input vectors have the same size
if (vector1.size() != vector2.size()) {
throw std::runtime_error("Input vectors must have the same size");
}
// Create a TGraph object
TGraph scatter(vector1.size(), vector1.data(), vector2.data());
// Set visual styles for the scatter plot
scatter.SetLineColor(1);
scatter.SetMarkerSize(1);
scatter.SetMarkerStyle(8);
// Set the axis titles
scatter.GetXaxis()->SetTitle("Ring Energy");
scatter.GetYaxis()->SetTitle("Wedge Energy");
// Create a TCanvas object
TCanvas canvas("canvas", "Scatter Plot", 800, 800);
// Draw the TMultiGraph on the canvas
scatter.Draw("a");
// Modify, update, and set grid for the canvas
canvas.Modified();
canvas.Update();
canvas.SetGrid();
// Save the canvas as an image
canvas.SaveAs(file_name.c_str());
}
for (int ringchan = 0; ringchan < 16; ++ringchan) {
for (int wedgechan = 0; wedgechan < 8; ++wedgechan) {
std::pair<int, int> key = std::make_pair(ringchan, wedgechan);
std::pair<std::vector<double>, std::vector<double>> energy = filteredEnergyData[key];
// Call the save_scatter_plot function with the data and output file name
std::string file_name = output_directory + "/scatter_plot_" + std::to_string(ringchan) + "_" + std::to_string(wedgechan) + ".png";
save_scatter_plot_with_tgraph(energy.first, energy.second, file_name);
}
}
Unfortunately, the performance of this code is not as fast as I would like it to be. I attempted to optimize it by using a nested for loop with OpenMP, as shown below:
#pragma omp parallel for collapse(2) num_threads(num_threads) shared(filteredEnergyData, output_directory)
for (int ringchan = 0; ringchan < 16; ++ringchan) {
for (int wedgechan = 0; wedgechan < 8; ++wedgechan) {
std::pair<int, int> key = std::make_pair(ringchan, wedgechan);
std::pair<std::vector<double>, std::vector<double>> energy = filteredEnergyData[key];
// Call the save_scatter_plot function with the data and output file name
std::string file_name = output_directory + "/scatter_plot_" + std::to_string(ringchan) + "_" + std::to_string(wedgechan) + ".png";
#pragma omp critical
save_scatter_plot_with_tgraph(energy.first, energy.second, file_name);
}
}
However, without the #pragma omp critical
line, I encounter segmentation fault errors. I’m looking for suggestions on how to improve the performance of this code. Are there any alternative methods that would help speed up the scatter plot generation process? Additionally, is there a “lighter weight” version of TGraph or another library that I could use to achieve the same goal?
Any advice or insights would be greatly appreciated. Thank you in advance for your help!