Menu ▾ ▴

#35 Retrieve the average energy of the spectrum

2.1.1
open
None
2026-09-30
2026-09-30
No

Retrieve the average energy of the spectrum as:

    // Compute the average energy of a photon
    std::vector<double> energy_bins = getEnergyBins("keV");
    std::vector<double> photon_counts = getPhotonCountsPerCm2At1m(true);

    double total_energy_polychromatic_beam = 0.0;
    double number_of_photons_polychromatic_beam = 0;

    // C++ 23
    // for (auto [energy, count]: std::views::zip(energy_bins, photon_counts)) {
    //     total_energy_polychromatic_beam += energy * count;
    //     number_of_photons_polychromatic_beam += count;
    // }
    for (size_t i = 0; i < photon_counts.size(); ++i) {
        auto count = photon_counts[i];
        total_energy_polychromatic_beam += energy_bins[i] * count;
        number_of_photons_polychromatic_beam += count;
    }

    double photon_energy_in_keV = total_energy_polychromatic_beam / number_of_photons_polychromatic_beam;

Discussion


Log in to post a comment.