-
Notifications
You must be signed in to change notification settings - Fork 1
/
EnergyLossModule.cpp
256 lines (201 loc) · 7.48 KB
/
EnergyLossModule.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include "include/EnergyLossModule.h"
//______________________________________________
EnergyLossModule::EnergyLossModule()
{
NucData=new nuclear_masses("Nuclear_Masses/masses.conf");
}
//______________________________________________
EnergyLossModule::~EnergyLossModule()
{
delete NucData;
}
//______________________________________________
void EnergyLossModule::Clear()
{
ParticleEnergy.clear();
for(int i=0; i<NUM_MODELS; i++) {
if(LiseELoss[i].size()) {
LiseELoss[i].clear();
}
}
for(int i=0; i<NUM_RANGE_MODELS; i++) {
if(LiseRange[i].size()) {
LiseRange[i].clear();
}
}
}
//______________________________________________
int EnergyLossModule::LoadEnergyLossFile(const char * file_name)
{
Clear();
std::ifstream FileIn(file_name);
if(!FileIn.is_open()) {
printf("Error: error while reading energy loss file\n");
return -1;
}
int NRead=0;
while (!FileIn.eof())
{
std::string LineRead;
std::getline(FileIn, LineRead);
if(LineRead.empty()) continue;
if(LineRead.find('*')==0) continue;
std::istringstream LineStream(LineRead);
double energy;
double eloss;
for(int i=0; i<NUM_MODELS; i++) {
LineStream >> energy >> eloss;
LiseELoss[i].push_back(eloss);
}
ParticleEnergy.push_back(energy);
NRead++;
}
Emin=ParticleEnergy[0];
Emax=ParticleEnergy[ParticleEnergy.size()-1];
for(int i=0; i<NUM_MODELS; i++) {
SplineInterpolator[i].SetData(ParticleEnergy,LiseELoss[i]);
}
return NRead;
}
//______________________________________________
int EnergyLossModule::LoadRangeFile(const char * file_name)
{
Clear();
std::ifstream FileIn(file_name);
if(!FileIn.is_open()) {
printf("Error: error while reading range file\n");
return -1;
}
int NRead=0;
while (!FileIn.eof())
{
std::string LineRead;
std::getline(FileIn, LineRead);
if(LineRead.empty()) continue;
if(LineRead.find('*')==0) continue;
std::istringstream LineStream(LineRead);
double energy;
double range;
for(int i=0; i<NUM_RANGE_MODELS; i++) {
LineStream >> energy >> range;
LiseRange[i].push_back(range);
}
ParticleEnergyRange.push_back(energy);
NRead++;
}
ERangeMin=ParticleEnergyRange[0];
ERangeMax=ParticleEnergyRange[ParticleEnergyRange.size()-1];
for(int i=0; i<NUM_RANGE_MODELS; i++) {
RangeSplineInterpolator[i].SetData(ParticleEnergyRange,LiseRange[i]);
EnergyFromRangeSplineInterpolator[i].SetData(LiseRange[i],ParticleEnergyRange);
RangeMin[i]=LiseRange[i][0];
RangeMax[i]=LiseRange[i][LiseRange[i].size()-1];
}
return NRead;
}
//______________________________________________
double EnergyLossModule::GetEnergyLoss(int Z, int A, double Einc, const char * material, double thickness_um, int model)
{
double Precision=0.0001;
double dThicknessMin=thickness_um*1E-3;
double IntegrateThickness=0;
double dThickness=dThicknessMin;
double Eresidual=Einc;
double ELoss=0;
double mass_uma=NucData->get_mass_Z_A_uma(Z,A);
if(LoadEnergyLossFile(Form("input/LISE_ELoss_Z%02d_A%02d_%s.dat", Z, A, material))<=0) {
printf("Error: information not present for Z=%d A=%d material=%s\n", Z, A, material);
return -100;
}
for(;IntegrateThickness<thickness_um; IntegrateThickness+=dThickness)
{
if(Eresidual<=Emin*mass_uma) { //the particle stopped in the material
ELoss=Einc;
return ELoss;
}
if(SplineInterpolator[model].Deriv(Eresidual/mass_uma)!=0) {
dThickness=fmin(dThicknessMin,std::abs(Precision/(SplineInterpolator[model].Eval(Eresidual/mass_uma)*SplineInterpolator[model].Deriv(Eresidual/mass_uma)))); //variable integration step with fixed precision
}
double ELossStep=dThickness*SplineInterpolator[model].Eval(Eresidual/mass_uma);
ELoss+=ELossStep;
Eresidual-=ELossStep;
}
return ELoss;
}
//______________________________________________
double EnergyLossModule::GetResidualEnergy(int Z, int A, double Eloss, const char * material, double thickness_um, int model)
{
double Einc=GetIncidentEnergy(Z,A,Eloss,material,thickness_um, model);
if(Einc<0) return -1; //the particle cannot lose this energy (energy greater than punch through energy)
return Einc-Eloss;
}
//______________________________________________
double EnergyLossModule::GetIncidentEnergy(int Z, int A, double Eloss, const char * material, double thickness_um, int model)
{
double EincStep=Eloss;
double ElossStep;
double dE=1.;
ElossStep=GetEnergyLoss(Z,A,EincStep,material,thickness_um, model);
if(ElossStep<Eloss) return -1; //the particle cannot lose this energy (energy greater than punch through energy)
for(;;EincStep+=dE)
{
ElossStep=GetEnergyLoss(Z,A,EincStep,material,thickness_um, model);
if(ElossStep<Eloss) {
dE=-std::abs(dE)/2;
}
if(ElossStep>Eloss && dE<0) {
dE=std::abs(dE);
}
if(std::abs(dE)<0.00001) break;
}
return EincStep;
}
//______________________________________________
double EnergyLossModule::GetRangeFromEnergy(int Z, int A, double Einc, const char * material, int model)
{
double mass_uma=NucData->get_mass_Z_A_uma(Z,A);
if(LoadRangeFile(Form("input/LISE_Range_Z%02d_A%02d_%s.dat", Z, A, material))<=0) {
printf("Error: information not present for Z=%d A=%d material=%s\n", Z, A, material);
return -100;
}
if(Einc/mass_uma>=ERangeMin && Einc/mass_uma<=ERangeMax) {
return RangeSplineInterpolator[model].Eval(Einc/mass_uma);
} else return -1;
}
//______________________________________________
double EnergyLossModule::GetEnergyFromRange(int Z, int A, double range, const char * material, int model)
{
double mass_uma=NucData->get_mass_Z_A_uma(Z,A);
if(LoadRangeFile(Form("input/LISE_Range_Z%02d_A%02d_%s.dat", Z, A, material))<=0) {
printf("Error: information not present for Z=%d A=%d material=%s\n", Z, A, material);
return -100;
}
if(range>=RangeMin[model] && range<=RangeMax[model]) {
return mass_uma*EnergyFromRangeSplineInterpolator[model].Eval(range);
} else return -1;
}
//______________________________________________
void EnergyLossModule::DrawdEdx(int Z, int A, const char * material, int model)
{
if(LoadEnergyLossFile(Form("input/LISE_ELoss_Z%02d_A%02d_%s.dat", Z, A, material))<=0) return;
const int NPoints = ParticleEnergy.size();
double E_LISE_Values[NPoints];
double LISE_Values[NPoints];
const int NPointsInterpolation = NPoints*1000;
double E_LISE_ValuesInterpolation[NPointsInterpolation];
double LISE_ValuesInterpolation[NPointsInterpolation];
for(int i=0; i<NPoints; i++) {
E_LISE_Values[i]=ParticleEnergy[i];
LISE_Values[i]=LiseELoss[model][i];
}
for(int i=0; i<NPointsInterpolation; i++) {
E_LISE_ValuesInterpolation[i]=i*(Emax-Emin)/NPointsInterpolation;
LISE_ValuesInterpolation[i]=SplineInterpolator[model].Eval(E_LISE_ValuesInterpolation[i]);
}
TGraph *LISEGraph = new TGraph(NPoints,E_LISE_Values,LISE_Values);
TGraph *LISEGraphInterpolation = new TGraph(NPointsInterpolation,E_LISE_ValuesInterpolation,LISE_ValuesInterpolation);
LISEGraph->Draw("A*");
LISEGraphInterpolation->Draw("same L");
LISEGraphInterpolation->SetLineColor(kRed);
return;
}