# Reading of HepMC file and Plotting of x y z distribution

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 == '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;
}

``````

emphasized text
ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided

That doesn’t match the content of the file. if you want to read the lines like:

``````P 1 0 -11 0.0000000000000000e+00 0.0000000000000000e+00 -9.9999999869440011e+00 1.0000000000000000e+01 5.1099900095068825e-04 4
``````

You can first change this:

``````   if (line == 'H')
continue;
``````

by this:

``````    if (line != 'P')
continue;
``````

And then you have to also read the first character and the last value, something like:

``````    char P;
double px, py, pz, energy, x, y, z, w;

// Read the particle ID, 4-vector components, and position
if (iss >> P >> id >> px >> py >> pz >> energy >> x >> y >> z >> w) {
``````

But you have to make sure the values you read are the correct ones (I don’t know what the values in that file are)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.