File indexing completed on 2025-01-18 09:17:05
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #include <math.h>
0014 #include <stdint.h>
0015 #include <stdio.h>
0016 #include <string.h>
0017
0018 #include <vector>
0019
0020
0021
0022 struct RunInfo
0023 {
0024
0025 uint8_t projectionIndex;
0026 uint16_t sliceIndex;
0027 uint16_t pixelIndex;
0028 uint32_t nbParticle;
0029 };
0030
0031 struct ParticleInfo
0032 {
0033 float energy_keV;
0034 float mx;
0035 float my;
0036 float mz;
0037 };
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050 void Plot(vector<double>& energies, int bin, double eMin, double eMax)
0051 {
0052 auto mycanvas = new TCanvas("canvas", "canvas", 800, 50, 600, 600);
0053 gPad->SetLeftMargin(0.15);
0054
0055
0056 auto hist = new TH1D("hist (keV)", "Spectrum of photons", bin, eMin, eMax);
0057
0058 for (int i = 0; i < energies.size(); ++i) {
0059 hist->Fill(energies[i]);
0060 }
0061
0062 hist->Draw();
0063 hist->GetXaxis()->SetTitle("Energy (keV)");
0064 hist->GetYaxis()->SetTitle("Counts");
0065 hist->GetXaxis()->CenterTitle();
0066 hist->GetYaxis()->CenterTitle();
0067
0068 mycanvas->Print("spectrum_gamma.png");
0069 }
0070
0071 void Spectrum_gamma()
0072 {
0073 FILE* input = fopen("../build/GammaAtExit.dat", "rb");
0074
0075 if (input == NULL) {
0076 printf("error for opening the input file\n");
0077 return;
0078 }
0079
0080
0081
0082
0083
0084 const int nbProjection = 10;
0085 const int nbSlice = 1;
0086 const int nbPixel = 20;
0087
0088 int projection_index_begin = 0;
0089 int projection_index_end = 0;
0090
0091 int slice_index_begin = 0;
0092 int slice_index_end = 0;
0093
0094
0095 int bin = 100;
0096 double eMin = 0;
0097 double eMax = 0;
0098
0099
0100
0101
0102
0103 RunInfo runInfo;
0104 vector<double> energies;
0105 int runID = -1;
0106
0107 while (fread(&runInfo, sizeof(RunInfo), 1, input)) {
0108 runID++;
0109 int nbParticle = runInfo.nbParticle;
0110
0111
0112
0113
0114
0115 runInfo.projectionIndex = runID / (nbSlice * nbPixel);
0116 int remain = runID % (nbSlice * nbPixel);
0117 runInfo.sliceIndex = remain / nbPixel;
0118 runInfo.pixelIndex = remain % nbPixel;
0119
0120
0121 if (!nbParticle) continue;
0122 std::vector<ParticleInfo> particles(nbParticle);
0123 fread(&particles[0], sizeof(ParticleInfo), nbParticle, input);
0124
0125 if (runInfo.projectionIndex >= projection_index_begin
0126 && runInfo.projectionIndex <= projection_index_end)
0127 {
0128 if (runInfo.sliceIndex >= slice_index_begin && runInfo.sliceIndex <= slice_index_end) {
0129 for (int i = 0; i < nbParticle; ++i) {
0130
0131
0132 energies.push_back(particles[i].energy_keV);
0133 if (particles[i].energy_keV > eMax) eMax = particles[i].energy_keV;
0134 }
0135 }
0136 }
0137 else
0138 break;
0139 }
0140
0141 fclose(input);
0142 Plot(energies, bin, eMin, eMax + 10);
0143 }