Dear Experts
I need some help to plot x y z distributions using the hepmc file on this link:
I have the following code which is suppose to plot x y z distributions but my output histograms are empty. Please help to make these distributions form this file.
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <TCanvas.h>
#include <TH1F.h>
int main() {
// Open the input file
std::ifstream inputFile("bremge_1keV_eic.hepmc");
// Create histograms to store the positions
TH1F* hX = new TH1F("hX", "Position X Distribution", 100, -10.0, 10.0);
TH1F* hY = new TH1F("hY", "Position Y Distribution", 100, -10.0, 10.0);
TH1F* hZ = new TH1F("hZ", "Position Z Distribution", 100, -10.0, 10.0);
std::string line;
while (std::getline(inputFile, line)) {
// Skip the header lines starting with "H"
if (line[0] == 'H')
continue;
std::istringstream iss(line);
int id;
double px, py, pz, energy, x, y, z;
// Read the particle ID, 4-vector components, and position
if (iss >> id >> px >> py >> pz >> energy >> x >> y >> z) {
// Fill the histograms with the positions
hX->Fill(x);
hY->Fill(y);
hZ->Fill(z);
} else {
std::cerr << "Error reading line: " << line << std::endl;
}
}
// Close the input file
inputFile.close();
// Create a canvas and draw the histograms
TCanvas* canvas = new TCanvas("canvas", "Position Distributions", 1200, 600);
canvas->Divide(3, 2);
canvas->cd(1);
hX->Draw();
canvas->cd(2);
hY->Draw();
canvas->cd(3);
hZ->Draw();
// Save the histograms as images
canvas->SaveAs("p1.png");
// Clean up
delete hX;
delete hY;
delete hZ;
delete canvas;
return 0;
}
Please read tips for efficient and successful posting and posting code
emphasized text
ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided