File indexing completed on 2025-01-18 09:17:04
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 StimEvent
0023 {
0024 uint16_t energy_keV;
0025 uint16_t pixelIndex;
0026 uint16_t sliceIndex;
0027 uint8_t projectionIndex;
0028 };
0029
0030 struct ParticleInfo
0031 {
0032 float energy_keV;
0033 float mx;
0034 float my;
0035 float mz;
0036 };
0037 struct RunInfo
0038 {
0039
0040 uint8_t projectionIndex;
0041 uint16_t sliceIndex;
0042 uint16_t pixelIndex;
0043 uint32_t nbParticle;
0044 };
0045 struct Point
0046 {
0047 double m_x;
0048 double m_y;
0049 double m_z;
0050 };
0051
0052 bool IsDetected(Point poi1, Point poi2, double theta)
0053 {
0054 double a = (poi1.m_x * poi2.m_x + poi1.m_y * poi2.m_y + poi1.m_z * poi2.m_z)
0055 / sqrt(poi1.m_x * poi1.m_x + poi1.m_y * poi1.m_y + poi1.m_z * poi1.m_z)
0056 / sqrt(poi2.m_x * poi2.m_x + poi2.m_y * poi2.m_y + poi2.m_z * poi2.m_z);
0057 if (a > 1.0) a = 1;
0058 if (a < -1.0) a = -1;
0059 double r = acos(a);
0060 if (r > theta)
0061 return false;
0062 else
0063 return true;
0064 }
0065 void BinToStd_ProtonAtExit()
0066 {
0067
0068
0069
0070
0071 const int nbProjection = 10;
0072 const int nbSlice = 1;
0073 const int nbPixel = 20;
0074 double totalAngleSpan = 180.;
0075
0076
0077
0078 double angleOfDetector = 0.;
0079 double distanceObjectDetector = 22.;
0080 double radiusOfDetector = 5.;
0081
0082
0083 double theta = 10.2 * TMath::DegToRad();
0084
0085
0086
0087
0088
0089 FILE* input = fopen("../build/ProtonAtExit.dat", "rb");
0090 FILE* out = fopen("../build/StimEvent_std.DAT", "wb");
0091
0092 if (input == NULL) {
0093 printf("error for opening the input ProtonAtExit.dat file\n");
0094 return;
0095 }
0096
0097 RunInfo runInfo;
0098 StimEvent stimEvent;
0099 Point centerOfDetector;
0100 Point protonMomentum;
0101 long long count = 0;
0102 int runID = -1;
0103
0104
0105 while (fread(&runInfo, sizeof(RunInfo), 1, input)) {
0106 runID++;
0107 int nbParticle = runInfo.nbParticle;
0108
0109
0110
0111
0112
0113 runInfo.projectionIndex = runID / (nbSlice * nbPixel);
0114 int remain = runID % (nbSlice * nbPixel);
0115 runInfo.sliceIndex = remain / nbPixel;
0116 runInfo.pixelIndex = remain % nbPixel;
0117
0118
0119
0120
0121
0122
0123 printf("---------RunID=%d: ProjectionIndex=%d, SliceIndex=%d, PixelIndex=%d, nbParticle = %d\n",
0124 runID, runInfo.projectionIndex, runInfo.sliceIndex, runInfo.pixelIndex, nbParticle);
0125
0126
0127
0128
0129
0130 if (!nbParticle) continue;
0131 std::vector<ParticleInfo> protonAtExit(nbParticle);
0132 fread(&protonAtExit[0], sizeof(ParticleInfo), nbParticle, input);
0133
0134
0135
0136 double ra = TMath::DegToRad()
0137 * (angleOfDetector + totalAngleSpan / nbProjection * runInfo.projectionIndex);
0138 centerOfDetector.m_x = distanceObjectDetector * cos(ra);
0139 centerOfDetector.m_y = distanceObjectDetector * sin(ra);
0140 centerOfDetector.m_z = 0;
0141
0142 for (int i = 0; i < nbParticle; ++i) {
0143
0144 if (protonAtExit[i].energy_keV >= 4095) continue;
0145
0146 protonMomentum.m_x = protonAtExit[i].mx;
0147 protonMomentum.m_y = protonAtExit[i].my;
0148 protonMomentum.m_z = protonAtExit[i].mz;
0149
0150 if (!IsDetected(centerOfDetector, protonMomentum, theta))
0151 continue;
0152 else {
0153 stimEvent.energy_keV = floor(protonAtExit[i].energy_keV + 0.5);
0154 stimEvent.projectionIndex = runInfo.projectionIndex;
0155 stimEvent.sliceIndex = runInfo.sliceIndex;
0156 stimEvent.pixelIndex = runInfo.pixelIndex;
0157 fwrite(&stimEvent, 7, 1, out);
0158 count++;
0159
0160 }
0161 }
0162 }
0163 printf("---------------Number of StimEvent in total: %lld------------------------\n", count);
0164 fclose(input);
0165 fclose(out);
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182
0183
0184 }