Geant4 Cross Reference |
1 //*********************************************************************************************************** 2 // TomoSpectrum.C 3 // Root command file 4 // Type: root TomoSpectrum.C 5 // 6 // It visualizes the spectrum of X-rays and plots a graph by reading PixeEvent data. 7 // 8 // More information is available in UserGuide 9 // Created by Z.LI LP2i Bordeaux 2022 10 //*********************************************************************************************************** 11 12 #include <math.h> 13 #include <stdint.h> 14 #include <stdio.h> 15 #include <string.h> 16 17 #include <vector> 18 // using namespace std; 19 20 struct PixeEvent 21 { 22 uint16_t energy_10eV; 23 uint16_t pixelIndex; 24 uint16_t sliceIndex; 25 uint8_t projectionIndex; 26 }; 27 28 void Plot(int nbChannels, vector<int>& X, vector<int>& Y) 29 { 30 gROOT->Reset(); 31 32 auto mycanvas = new TCanvas("canvas", "canvas", 800, 50, 600, 600); 33 mycanvas->ToggleEventStatus(); 34 35 gPad->SetLeftMargin(0.15); 36 37 auto graph = new TGraph(nbChannels, X.data(), Y.data()); 38 graph->SetLineColor(8); 39 40 graph->Draw("AL"); 41 42 graph->SetLineColor(8); 43 graph->SetTitle("TOMO Energy Spectrum"); 44 graph->GetXaxis()->SetTitle("ADC channels"); 45 graph->GetYaxis()->SetTitle("Nb events"); 46 graph->GetXaxis()->CenterTitle(); 47 graph->GetYaxis()->CenterTitle(); 48 49 mycanvas->Print("TomoSpectrum.png"); 50 } 51 52 void TomoSpectrum() 53 { 54 FILE* input = fopen("../build/PixeEvent_std_AtExit_Detector135_Aperture70.DAT", "rb"); 55 56 if (input == NULL) { 57 printf("----------error for opening the input file--------------\n"); 58 return; 59 } 60 61 //*********************************************************************** 62 //**************************Selection parameters (begin)***************** 63 //*********************************************************************** 64 const int nbProjection = 10; 65 const int nbSlice = 1; 66 const int nbPixel = 20; 67 68 int projection_index_begin = 0; // starter of the projection selected 69 int projection_index_end = 0; // end of the projection selected 70 71 int slice_index_begin = 0; // starter of the slice selected 72 int slice_index_end = 0; // end of the slice selected 73 74 //*********************************************************************** 75 //**************************Selection parameters (end)******************* 76 //*********************************************************************** 77 78 int nbChannels = 4096; 79 vector<int> X(nbChannels); // save channels 1-4096, index X: 0-4095 80 vector<int> Y(nbChannels); // save event counts for channel 1-4096, index Y: 0-4095 81 PixeEvent p; 82 83 while (fread(&p, 7, 1, input)) { 84 if (p.projectionIndex >= projection_index_begin && p.projectionIndex <= projection_index_end) { 85 if (p.sliceIndex >= slice_index_begin && p.sliceIndex <= slice_index_end) { 86 // printf("%d %d %d\n",p.projectionIndex, p.sliceIndex, p.energy_10eV); 87 Y[p.energy_10eV - 1] = Y[p.energy_10eV - 1] + 1; 88 } 89 } 90 } 91 fclose(input); 92 for (int i = 0; i < nbChannels; ++i) { 93 X[i] = 1 + i; 94 } 95 96 FILE* out = fopen("Spectrum.txt", "wb"); 97 for (int i = 0; i < nbChannels; ++i) { 98 fprintf(out, "%d\t%d\n", X[i], Y[i]); 99 } 100 101 fclose(out); 102 Plot(nbChannels, X, Y); 103 } 104