-
Notifications
You must be signed in to change notification settings - Fork 0
/
HiRACsICalibration.cpp
454 lines (372 loc) · 13.5 KB
/
HiRACsICalibration.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#include "include/HiRACsICalibration.h"
EnergyLossModule gLISEModule;
//______________________________________________
HiRACsICalibrationManager::HiRACsICalibrationManager() :
fPulserLoaded(false)
{
for(int i=0; i<CSICALIB_NUM_TEL; i++) {
for(int j=0; j<CSICALIB_NUM_CSI_TEL; j++) {
fCsIChToVInterpolated [i*CSICALIB_NUM_CSI_TEL+j]=0;
fCsIChToVExtrapolated [i*CSICALIB_NUM_CSI_TEL+j]=0;
}
}
for(int i=0; i<Z_MAX; i++) {
for(int j=0; j<A_MAX; j++) {
for(int k=0; k<CSICALIB_NUM_TEL*CSICALIB_NUM_CSI_TEL; k++) {
fCalib[i][j][k]=0;
}
}
}
}
//______________________________________________
HiRACsICalibrationManager::~HiRACsICalibrationManager()
{
for(int i=0; i<CSICALIB_NUM_TEL; i++) {
for(int j=0; j<CSICALIB_NUM_CSI_TEL; j++) {
if(fCsIChToVInterpolated [i*CSICALIB_NUM_CSI_TEL+j]) {
delete fCsIChToVInterpolated [i*CSICALIB_NUM_CSI_TEL+j];
delete fCsIChToVExtrapolated [i*CSICALIB_NUM_CSI_TEL+j];
}
}
}
for(int i=0; i<Z_MAX; i++) {
for(int j=0; j<A_MAX; j++) {
for(int k=0; k<CSICALIB_NUM_TEL*CSICALIB_NUM_CSI_TEL; k++) {
if (fCalib[i][j][k]) delete fCalib[i][j][k];
}
}
}
}
//______________________________________________
void HiRACsICalibrationManager::Clear()
{
for(int i=0; i<CSICALIB_NUM_TEL; i++) {
for(int j=0; j<CSICALIB_NUM_CSI_TEL; j++) {
if(fChValues[i*CSICALIB_NUM_CSI_TEL+j].size()) {
fChValues [i*CSICALIB_NUM_CSI_TEL+j].clear();
fVoltageValues [i*CSICALIB_NUM_CSI_TEL+j].clear();
}
if(fCsIChToVInterpolated [i*CSICALIB_NUM_CSI_TEL+j]) {
delete fCsIChToVInterpolated [i*CSICALIB_NUM_CSI_TEL+j];
delete fCsIChToVExtrapolated [i*CSICALIB_NUM_CSI_TEL+j];
}
}
}
fPulserLoaded=false;
}
//______________________________________________
int HiRACsICalibrationManager::LoadPulserInfo(const char * file_name)
{
Clear();
std::ifstream FileIn(file_name);
if(!FileIn.is_open()) {
printf("Error: error while reading pulser 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);
int numtel;
int numcsi;
int peaknum;
double voltage;
double channel;
LineStream >> numtel >> numcsi >> peaknum >> voltage >> channel;
if(channel==0) continue;
fChValues [numtel*CSICALIB_NUM_CSI_TEL+numcsi].push_back(channel);
fVoltageValues [numtel*CSICALIB_NUM_CSI_TEL+numcsi].push_back(voltage);
NRead++;
}
for(int i=0; i<CSICALIB_NUM_TEL; i++) {
for(int j=0; j<CSICALIB_NUM_CSI_TEL; j++) {
fCsIChToVExtrapolated [i*CSICALIB_NUM_CSI_TEL+j] = new TGraph(fChValues[i*CSICALIB_NUM_CSI_TEL+j].size(), fChValues[i*CSICALIB_NUM_CSI_TEL+j].data(), fVoltageValues[i*CSICALIB_NUM_CSI_TEL+j].data());
fCsIChToVInterpolated [i*CSICALIB_NUM_CSI_TEL+j] = new TSpline3(Form("CsIChToVInterpolated%02d_%02d", i, j), fCsIChToVExtrapolated [i*CSICALIB_NUM_CSI_TEL+j]);
//fCsIChToVInterpolated [i*CSICALIB_NUM_CSI_TEL+j]->SetBit(TGraph::kIsSortedX);
}
}
fPulserLoaded=true;
return NRead;
}
//______________________________________________
double HiRACsICalibrationManager::GetVoltageValue(double ch, int numtel, int numcsi) const
{
if(fCsIChToVInterpolated[numtel*CSICALIB_NUM_CSI_TEL+numcsi]==0) return -1;
if(ch>=fCsIChToVInterpolated[numtel*CSICALIB_NUM_CSI_TEL+numcsi]->GetXmin() && ch<=fCsIChToVInterpolated[numtel*CSICALIB_NUM_CSI_TEL+numcsi]->GetXmax()) {
return fCsIChToVInterpolated[numtel*CSICALIB_NUM_CSI_TEL+numcsi]->Eval(ch);
} else {
return fCsIChToVExtrapolated[numtel*CSICALIB_NUM_CSI_TEL+numcsi]->Eval(ch,0,"");
}
}
//______________________________________________
double HiRACsICalibrationManager::GetEnergyValue(double ch, int numtel, int numcsi, int Z, int A) const
{
if(fCalib[Z][A][numtel*CSICALIB_NUM_CSI_TEL+numcsi]==0 || !fPulserLoaded) return -1;
return fCalib[Z][A][numtel*CSICALIB_NUM_CSI_TEL+numcsi]->GetEnergy(GetVoltageValue(ch,numtel,numcsi));
}
//______________________________________________
void HiRACsICalibrationManager::DrawChVoltage(int numtel, int numcsi) const
{
if(!fPulserLoaded) {
printf("No pulser loaded\n");
return;
}
const int NPoints = fChValues [numtel*CSICALIB_NUM_CSI_TEL+numcsi].size();
if(NPoints==0) {
printf("No pulser available for this crystal\n");
return;
}
const double *CsChPoints = fChValues [numtel*CSICALIB_NUM_CSI_TEL+numcsi].data();
const double *CsVoltagePoints = fVoltageValues [numtel*CSICALIB_NUM_CSI_TEL+numcsi].data();
const int NPointsInterpolation = NPoints*1000;
double CsChPointsInterpolation[NPointsInterpolation];
double CsVoltagePointsInterpolation[NPointsInterpolation];
for(int i=0; i<NPointsInterpolation; i++) {
CsChPointsInterpolation[i]=double(i*4096)/NPointsInterpolation;
CsVoltagePointsInterpolation[i]=fCsIChToVExtrapolated[numtel*CSICALIB_NUM_CSI_TEL+numcsi]->Eval(CsChPointsInterpolation[i],0,"");
}
TGraph *PulserGraph = new TGraph(NPoints,CsChPoints,CsVoltagePoints);
TGraph *PulserGraphInterpolation = new TGraph(NPointsInterpolation,CsChPointsInterpolation,CsVoltagePointsInterpolation);
//TCanvas c1 ("c1", "", 800, 600);
PulserGraphInterpolation->Draw("AL");
PulserGraph->Draw("same *");
PulserGraphInterpolation->SetLineColor(kRed);
PulserGraph->SetMarkerColor(kBlue);
PulserGraphInterpolation->SetTitle(Form("HiRA_CsI_Pulser_VvsCh_%02d_%02d",numtel,numcsi));
//c1.Print(Form("pictures/HiRA_CsI_Pulser_VvsCh_%02d_%02d.png",numtel,numcsi));
return;
}
//______________________________________________
int HiRACsICalibrationManager::LoadEnergyCalibration(const char * file_name, int Z, int A)
{
std::ifstream FileIn(file_name);
if(!FileIn.is_open()) {
printf("Error: error while opening CsI calibration 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);
int numtel;
int numcsi;
double normalization;
double gradient;
LineStream >> numtel >> numcsi >> normalization >> gradient;
fCalib[Z][A][numtel*CSICALIB_NUM_CSI_TEL+numcsi]= new HiRACsICalibration(Z, A);
fCalib[Z][A][numtel*CSICALIB_NUM_CSI_TEL+numcsi]->SetNumParameters(2);
fCalib[Z][A][numtel*CSICALIB_NUM_CSI_TEL+numcsi]->SetParameter(0,normalization);
fCalib[Z][A][numtel*CSICALIB_NUM_CSI_TEL+numcsi]->SetParameter(1,gradient);
fCalib[Z][A][numtel*CSICALIB_NUM_CSI_TEL+numcsi]->InitCalibration();
NRead++;
}
return NRead;
}
//______________________________________________
HiRACsICalibration * HiRACsICalibrationManager::GetCalibration(int numtel, int numcsi, int Z, int A) const
{
return fCalib[Z][A][numtel*CSICALIB_NUM_CSI_TEL+numcsi];
}
//______________________________________________
HiRACsICalibration::HiRACsICalibration(int Z, int A) :
fSimulationModule(0),
fNumParameters(0),
fParameters(0),
fCalibrationFunc(0),
fCalibrationInitialized(false),
fZ(Z),
fA(A)
{
fSimulationModule= new HiRACsISimulation();
}
//______________________________________________
HiRACsICalibration::~HiRACsICalibration()
{
delete fSimulationModule;
}
//______________________________________________
void HiRACsICalibration::SetNumParameters(int num_parameters)
{
fNumParameters=num_parameters;
fParameters = new double[fNumParameters];
return;
}
//______________________________________________
void HiRACsICalibration::SetParameter(int par_to_set, double value)
{
fParameters[par_to_set]=value;
}
//______________________________________________
void HiRACsICalibration::InitCalibration()
{
//protons, deuterons or tritons
if((fZ==1 && fA==1) || (fZ==1 && fA==2) || (fZ==1 && fA==3)) {
int NPoints=100;
for(int i=1; i<NPoints; i++)
{
double range=(i)*CsILength/NPoints;
double energy=gLISEModule.GetEnergyFromRange(fZ,fA,range,"CsI");
fLISEEnergyMeV.push_back(energy);
TF1 * light_response = new TF1("light_response","1-[0]*x", 0, 200000);
light_response->SetParameter(0, fParameters[1]);
double Light=fParameters[0]*fSimulationModule->GetSimulatedLightFromEnergy(fZ,fA,energy,light_response,CsILength);
fCsIRawV.push_back(Light);
delete light_response;
}
fCalibrationInitialized=true;
fVtoEExtrapolated= new TGraph(fCsIRawV.size(), fCsIRawV.data(), fLISEEnergyMeV.data());
fVtoEInterpolated = new TSpline3("VtoEInterpolated", fVtoEExtrapolated);
} else
{
fCalibrationInitialized=false;
}
}
//______________________________________________
double HiRACsICalibration::GetEnergy(double V) const
{
if(!fCalibrationInitialized) {
return -1;
}
if(V>=fVtoEInterpolated->GetXmin() && V<=fVtoEInterpolated->GetXmax()) {
return fVtoEInterpolated->Eval(V);
} else {
return fVtoEExtrapolated->Eval(V,0,"");
}
}
//______________________________________________
void HiRACsICalibration::CheckCalibrationValidity(const char * file_name, int tel, int csi)
{
//TCanvas * c1 = new TCanvas("check","Check on Calibrations", 800, 600);
std::ifstream FileIn(file_name);
if(!FileIn.is_open()) {
printf("Error: file %s not found\n", file_name);
return;
}
int npoints=0;
double csivoltage[500];
double err_csivoltage[500];
double energy[500];
double err_energy[500];
while (!FileIn.eof()) {
std::string LineRead;
std::getline(FileIn, LineRead);
if(LineRead.empty()) continue;
if(LineRead.find('*')==0) continue;
std::istringstream LineStream(LineRead);
int numtel;
int numcsi;
LineStream >> numtel>> numcsi;
if(numtel!=tel || numcsi!=csi) continue;
LineStream >> csivoltage[npoints] >> err_csivoltage[npoints] >> energy[npoints] >> err_energy[npoints] ;
if(err_energy[npoints]==0) continue;
npoints++;
}
const int NPointsInterpolation = npoints*1000;
double CsIVoltagePointsInterpolation[NPointsInterpolation];
double CsIEnergyPointsInterpolation[NPointsInterpolation];
for(int i=0; i<NPointsInterpolation; i++) {
CsIVoltagePointsInterpolation[i]=double(i*2)/NPointsInterpolation;
CsIEnergyPointsInterpolation[i]=fVtoEExtrapolated->Eval(CsIVoltagePointsInterpolation[i],0,"");
}
TGraph * GraphCalibration = new TGraph(NPointsInterpolation,CsIVoltagePointsInterpolation,CsIEnergyPointsInterpolation);
TGraphErrors * GraphPoints = new TGraphErrors(npoints, csivoltage, energy, err_csivoltage, err_energy);
GraphCalibration->Draw("AL");
GraphPoints->Draw("same *");
GraphCalibration->SetLineColor(kRed);
GraphPoints->SetMarkerColor(kBlue);
GraphCalibration->SetTitle(Form("HiRA_CsI_EvsV_%02d_%02d",tel,csi));
}
//______________________________________________
int HiRACsISimulation::LoadEnergyLossFile(const char * file_name)
{
ClearEnergyLossInfo();
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_ELOSS; 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_ELOSS; i++) {
SplineInterpolator[i].SetData(ParticleEnergy,LiseELoss[i]);
}
return NRead;
}
//______________________________________________
void HiRACsISimulation::ClearEnergyLossInfo()
{
ParticleEnergy.clear();
for(int i=0; i<NUM_MODELS_ELOSS; i++) {
if(LiseELoss[i].size()) {
LiseELoss[i].clear();
}
}
}
//______________________________________________
HiRACsISimulation::HiRACsISimulation() :
NucData(0)
{
NucData=new nuclear_masses("Nuclear_Masses/masses.conf");
}
//______________________________________________
HiRACsISimulation::~HiRACsISimulation()
{
delete NucData;
}
//______________________________________________
double HiRACsISimulation::GetSimulatedLightFromEnergy(int Z, int A, double Einc, TF1 * LightResponse, double CsIthickness_um, int model)
{
double Precision=0.0001;
double dThicknessMin=CsIthickness_um*1E-4;
double IntegrateThickness=0;
double dThickness=dThicknessMin;
double Eresidual=Einc;
double ELoss=0;
double SimulatedLight=0;
double mass_uma=NucData->get_mass_Z_A_uma(Z,A);
char material[]="CsI";
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<CsIthickness_um; IntegrateThickness+=dThickness)
{
if(Eresidual<=Emin*mass_uma) { //the particle stopped in the material
ELoss=Einc;
return SimulatedLight+Eresidual*( LightResponse ? LightResponse->Eval(IntegrateThickness) : 1 );
}
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);
SimulatedLight+=( LightResponse ? LightResponse->Eval(IntegrateThickness) : 1 )*ELossStep;
ELoss+=ELossStep;
Eresidual-=ELossStep;
}
return SimulatedLight;
}