-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcalculate_GCR_chisq.cc
executable file
·815 lines (793 loc) · 37.5 KB
/
calculate_GCR_chisq.cc
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
#include "galprop_classes.h"
#include "galprop_internal.h"
#include <vector>
#include <map>
#include <cstring>
#include <cstdlib>
static bool outputErrorMessage = true;
//Convenience functions
void retrieveGCRData(vector<double> &eMean, vector<double> &flux, vector<double> &errMinus, vector<double> &errPlus, const GCR_data &gcr_data, vector<int> Z_denom, vector<int> A_denom, vector<int> Z_numer, vector<int> A_numer, char *type, const vector<string> &references = vector<string>(0) ) {
for (int i = 0; i < gcr_data.n; ++i){
//Apply all the requirements
bool include = strcmp(gcr_data.Y_name[i], type) == NULL;
//The size matters
size_t ds=0;
while (ds < 3 && gcr_data.Z_denominator[i][ds] != 0) ++ds;
size_t ns=0;
while (ns < 3 && gcr_data.Z_numerator[i][ns] != 0) ++ns;
include &= Z_denom.size() == ds;
include &= Z_numer.size() == ns;
//The order matters
for (int j = 0; j < Z_denom.size(); ++j) include &= Z_denom[j] == gcr_data.Z_denominator[i][j];
for (int j = 0; j < A_denom.size(); ++j) include &= A_denom[j] == gcr_data.A_denominator[i][j];
for (int j = 0; j < Z_numer.size(); ++j) include &= Z_numer[j] == gcr_data.Z_numerator[i][j];
for (int j = 0; j < A_numer.size(); ++j) include &= A_numer[j] == gcr_data.A_numerator[i][j];
//All the references should be included
bool instr = references.size() == 0;
for (int j = 0; j < references.size(); ++j) instr |= references[j] == gcr_data.reference[i];
include &= instr;
if (include && gcr_data.value[i] != 0){
//Calculate the average E_mean if it does not exist
if (gcr_data.E_mean[i] == 0) {
eMean.push_back(sqrt(gcr_data.E_low[i]*gcr_data.E_high[i]));
} else {
eMean.push_back(gcr_data.E_mean[i]);
}
flux.push_back(gcr_data.value[i]);
//Handle unsigned errors in either minus or plus or both
if (gcr_data.err_minus[i] == 0) {
if (gcr_data.err_plus[i] == 0) {
errMinus.push_back(0.1*gcr_data.value[i]);
errPlus.push_back(0.1*gcr_data.value[i]);
}else{
errMinus.push_back(gcr_data.err_plus[i]);
errPlus.push_back(gcr_data.err_plus[i]);
}
}else{
errMinus.push_back(gcr_data.err_minus[i]);
if (gcr_data.err_plus[i] == 0){
errPlus.push_back(gcr_data.err_minus[i]);
}else{
errPlus.push_back(gcr_data.err_plus[i]);
}
}
}
}
}
std::vector<double> interpolateAndModulate(vector<double> spectra, const double *energy, const vector<double> &dataEmean, int Z, int A, double phi){
//Constants needed for modulation
const double eMass = 0.5109990615; //Electron mass in MeV;
const double pMass = 939.; //Proton mass in MeV
vector<double> output(dataEmean.size(),0.0);
for (int i = 0; i < dataEmean.size(); ++i){
double en;
if ( A == 0) {
en = dataEmean[i] + fabs(Z)*phi;
} else {
en = dataEmean[i] + fabs(Z)*phi/float(A);
}
//Do interpolation in energy, but no extrapolation
if (en >= energy[0] && en <= energy[spectra.size()-1]) {
int j(0);
while (en > energy[j]) ++j;
if (j == 0) ++j; //If en is excactly the lower limit
//Power law interpolation if both values exist, otherwise linear
if (spectra[j-1] > 0 && spectra[j] > 0) {
double sl = log(spectra[j]/spectra[j-1])/log(energy[j]/energy[j-1]);
output[i] = spectra[j]*pow(en/energy[j], sl);
}else if (spectra[j-1] > 0) {
output[i] = (energy[j] - en)*spectra[j-1]/(energy[j]-energy[j-1]);
}else if (spectra[j] > 0) {
output[i] = (en - energy[j-1])*spectra[j]/(energy[j]-energy[j-1]);
}
if ( A == 0) {
output[i] *= dataEmean[i]*(dataEmean[i]+2*eMass)/(en*(en+2*eMass));
} else {
output[i] *= dataEmean[i]*(dataEmean[i]+2*pMass)/(en*(en+2*pMass));
}
} else {
output[i] = -1;
if (outputErrorMessage){
cerr<<"Energy range of model is too small for data"<<endl;
cerr<<en<<" is not within range "<<energy[0]<<" - "<<energy[spectra.size()-1]<<endl;
cerr<<"This message will not be repeated"<<endl;
outputErrorMessage = false;
}
}
}
return output;
}
std::vector<double> readNuclei(const Particle *gcr, int n_species, int Z, int A, int iz1, int iz2, int ir1, int ir2, int ix1, int ix2, int iy1, int iy2, double fz1, double fz2, double fr1, double fr2, double fx1, double fx2, double fy1, double fy2){
vector<double> output(0);
for (int i = 0; i < n_species; ++i){
if (gcr[i].Z == Z && gcr[i].A == A){
if (output.size() == 0) output.resize(gcr[0].n_pgrid, 0.0);
for (int j = 0; j < gcr[0].n_pgrid;++j) {
//Do interpolation in z,r,x,y
if (gcr[i].n_spatial_dimensions==2){
output[j]+=fr1*fz1*gcr[i].cr_density.d2[ir1][iz1].s[j]+fr2*fz1*gcr[i].cr_density.d2[ir2][iz1].s[j]+
fr1*fz2*gcr[i].cr_density.d2[ir1][iz2].s[j]+fr2*fz2*gcr[i].cr_density.d2[ir2][iz2].s[j];
}else if (gcr[i].n_spatial_dimensions==3){
output[j]+=fx1*fy1*fz1*gcr[i].cr_density.d3[ix1][iy1][iz1].s[j]+fx2*fy1*fz1*gcr[i].cr_density.d3[ix2][iy1][iz1].s[j]+
fx1*fy2*fz1*gcr[i].cr_density.d3[ix1][iy2][iz1].s[j]+fx2*fy2*fz1*gcr[i].cr_density.d3[ix2][iy2][iz1].s[j]+
fx1*fy1*fz2*gcr[i].cr_density.d3[ix1][iy1][iz2].s[j]+fx2*fy1*fz2*gcr[i].cr_density.d3[ix2][iy1][iz2].s[j]+
fx1*fy2*fz2*gcr[i].cr_density.d3[ix1][iy2][iz2].s[j]+fx2*fy2*fz2*gcr[i].cr_density.d3[ix2][iy2][iz2].s[j];
}
}
}
}
return output;
}
//Finds the value of il and iu such that arr[il] < value < arr[iu]. Size of
//array is size, the step size in arr is da, and it should be monotomically
//increasing.
//If value lies outside of the boundaries, an error is printed and the closest
//value is chosen. il is in this case put to the closest pixel, iu to -1, fl to
//1 and fu to 0
//If the value falls within 0.1% of a arr element, it is chosen as il, fl set
//to 1, fu to 0 and iu to 0
void findInterpolationBounds(const double *arr, double da, int size, int &il, int &iu, double &fl, double &fu, double value, const std::string & type) {
il=(int)((value-arr[0])/da);
iu = 0;
fl = 1;
fu = 0;
if (il >= 0 && il < size) { //Should always be the case
//Do proper interpolation if needed
if ( fabs(value-arr[il])/da > 1e-3 ) {
if (value-arr[il] < 0) { //Need to have a lower value for ir
if (il == 0) {
std::cerr<<type<<" grid not wide enough to include the sun, using closest grid point"<<std::endl;
} else {
iu = il;
--il;
fl = (value-arr[iu])/da;
fu = 1-fl;
}
} else {
if (il == size-1) {
std::cerr<<type<<" grid not wide enough to include the sun, using closest grid point"<<std::endl;
} else {
iu = il+1;
fu = (value-arr[il])/da;
fl = 1-fu;
}
}
}
} else {
//Just post an error and use the last grid point
std::cerr<<type<<" grid not wide enough to include the sun, using closest grid point"<<std::endl;
if (il < 0) {
il = 0;
} else {
il = size-1;
}
}
}
// The bool sets determine which datasets to include. The available ones are in
// order:
// 0: e- (HEAT, SANRIKU98, AMS )
// 1: e+
// 2: H (BESS[Wa02, Ha04, Sa00], ATIC2, AMS, JACEE)
// 3: pbar (BESS[Or00], PAMELA[Ad10]
// 4: He (BESS[Wa02, Ha04, Sa00], ATIC2, AMS, JACEE)
// 5: O (ACE, HEAO3)
// 6: C (ACE, HEAO3)
// 7: B/C ratio (ACE (Da00, No01), HEAO3, ATIC2, CREAM)
// 8: Be10/Be9 ratio (ACE)
// The names in parentheses indicate the experiments included. Modulation for
// those must be set.
//
// The bool expmnts can be used to exclude/include experiments (references) in the
// calculation, in order
// 0: HEAO3 (En90)
// 1: ACE (ACEW, Da00)
// 2: ATIC2 (Pa06, Pa07)
// 3: CREAM (Ahn8)
// 4: AMS (Ag02)
// 5: BESS (Ha04)
// 6: BESS (Wa02)
// 7: JACEE (As98)
// 8: HEAT (Du01)
// 9: SANRIKU98 (Ko99)
// 10: Fermi-LAT (Ab09, Abnn)
// 11: HESS (Ah08)
// 12: HESS09 (Ah09)
// 13: BESS (Sa00)
// 14: BESS (Or00)
// 15: PAMELA (Ad10)
// Note that no check is made for datasets with 0 data points included. What
// happens then is currently unknown
//
// The double array modulation gives the modulation potential for each
// reference/experiment (same order as expmnts). Suggested values are in
// parentheses
// 0: HEAO3 (600 +- 50)
// 1: ACE (325 +- 50)
// 2: ATIC2 (Not important since data at high energy, but needed as a parameter, 0
// should be OK)
// 3: CREAM (Not very important since data at high energy, but needed as a
// parameter, 600 +- 50 should be a safe estimate)
// 4: AMS (650 +- 40 according to ag02)
// 5: BESS (1100 +- 100)
// 6: BESS (1300 +- 100)
// 7: JACEE (Not important since data at high energy, but needed as a
// parameter, 0 should be OK)
// 8: HEAT (700 +- 50)
// 9: SANRIKU98 (Not important since data at high energy, but needed as a
// parameter, 0 should be OK)
// 10: Fermi-LAT (300 should be OK)
// 11: HESS (Not important since data at high energy, but needed as a
// parameter, 0 should be OK)
// 12: HESS09 (Not important since data at high energy, but needed as a
// parameter, 0 should be OK)
// 13: BESS (400 +- 100 [solar minimum])
// 14: BESS (450 +- 100)
// 15: PAMELA (400 +- 100 [solar minimum])
//
// The double array tau sets a scaling factor for the error of each experiment (precision)
// such that new sigma_i^2 = sigma_i^2/tau. The array should be set to the log10
// value of tau. One parameter for each reference/experiment, same order as
// expmnts
// RT: modified chisquare to be -2loglike (including prefactor of log(tau)
// 0: HEAO3
// 1: ACE
// 2: ATIC2
// 3: CREAM
// 4: AMS
// 5: BESS
// 6: BESS
// 7: JACEE
// 8: HEAT
// 9: SANRIKU98
// 10: Fermi-LAT
// 11: HESS
// 12: HESS09
// 13: BESS
// 14: BESS
// 15: PAMELS
//
// Double array energyScale sets the energy scale for the experiments. Same
// order as arrays above.
double Galprop::calculate_GCR_chisq(bool sets[], bool expmnts[], double modulation[], double tau[], double energyScale[], double chisqa[]) {
double prt=0;
//Read in the data if necessary
if (gcr_data.n == 0) {
const string fullFilename = configure.fGlobalDataPath + "/" + galdef.GCR_data_filename;//"GCR_data_1.dat";
std::cout<<"Filename for GCR database: "<<fullFilename<<std::endl;
gcr_data.read(fullFilename.c_str(), "cm2", "MeV");
}
//Find the iz value for z = 0;
int iz1, iz2, ir1, ir2, ix1, ix2, iy1, iy2;
double fz1,fz2,fr1,fr2,fx1,fx2,fy1,fy2;
findInterpolationBounds(galaxy.z, galdef.dz, galaxy.n_zgrid, iz1, iz2, fz1, fz2, 0.0, "z");
//3D and 2D are different
if (gcr[0].n_spatial_dimensions==2) {
findInterpolationBounds(galaxy.r, galdef.dr, galaxy.n_rgrid, ir1, ir2, fr1, fr2, Rsun, "r");
} else if (gcr[0].n_spatial_dimensions==3){
findInterpolationBounds(galaxy.x, galdef.dx, galaxy.n_xgrid, ix1, ix2, fx1, fx2, Rsun, "x");
findInterpolationBounds(galaxy.y, galdef.dy, galaxy.n_ygrid, iy1, iy2, fy1, fy2, 0.0, "y");
}
//std::cout<<"Interpolation values \n z: ("<<galaxy.z[iz1]<<"["<<iz1<<"], "<<fz1<<"), ("<<galaxy.z[iz2]<<"["<<iz2<<"], "<<fz2<<")\n r: ("<<galaxy.r[ir1]<<"["<<ir1<<"], "<<fr1<<"), ("<<galaxy.r[ir2]<<"["<<ir2<<"], "<<fr2<<")\n";
double chisq=0;
//Check the sets
if (sets[0]) { //electrons
//Data storage for the electron spectrum
vector<double> electrons = readNuclei(gcr,n_species,-1,0,iz1,iz2,ir1,ir2,ix1,ix2,iy1,iy2,fz1,fz2,fr1,fr2,fx1,fx2,fy1,fy2);
if (electrons.size() == 0){
std::cerr<<"No electrons found in nuclei data, not using those in chisq"<<std::endl;
} else {
//Select the appropriate data
std::vector<double> dataEmean, dataFlux, dataErrMinus, dataErrPlus;
vector<int> Z_d, A_d, Z_n, A_n;
vector<string> references(1); //Storage to select references
map<string,int> refs; //The actual references and mapping to modulation
if (expmnts[4]) refs["Ag02"] = 4;
if (expmnts[8]) refs["Du01"] = 8;
if (expmnts[9]) refs["Ko99"] = 9;
if (expmnts[10]) {
refs["Ab09"] = 10;
refs["Abnn"] = 10;
}
if (expmnts[11]) refs["Ah08"] = 11;
if (expmnts[12]) refs["Ah09"] = 12;
Z_d.push_back(-1);
A_d.push_back(0);
map<string,int>::iterator it = refs.begin();
for ( ; it != refs.end(); ++it) {
vector<double> dataEmean, dataFlux, dataErrMinus, dataErrPlus;
references[0] = it->first;
retrieveGCRData(dataEmean,dataFlux,dataErrMinus,dataErrPlus,gcr_data,Z_d,A_d,Z_n,A_n,"flux",references);
//Correct for energy scaling
for (int i = 0; i < dataEmean.size(); ++i)
dataEmean[i] *= energyScale[it->second];
//Interpolate and modulate the model
vector<double> flux = interpolateAndModulate(electrons, gcr[0].Ekin, dataEmean, -1, 0, modulation[it->second]);
//Now we can calculate the chisq, including modulation
if (galdef.verbose == -701) cout<<"#Electron data "<<it->first<<":"<<endl;
//cout<<"Modulation:"<<modulation[it->second]<<endl;
for (int i = 0; i < dataEmean.size(); ++i){
if (flux[i] >= 0) {
if (flux[i] < dataFlux[i]) {
chisq += pow((flux[i]-dataFlux[i])/dataErrMinus[i], 2)*pow(10,tau[it->second]) - log(pow(10,tau[it->second]));
if (galdef.verbose == -701) cout<<dataEmean[i]<<" "<<flux[i]<<" "<<dataFlux[i]<<" "<<(dataErrMinus[i]/pow(10,tau[it->second]))<<" "<<chisq-prt<<endl;
} else {
chisq += pow((flux[i]-dataFlux[i])/dataErrPlus[i], 2)*pow(10,tau[it->second]) - log(pow(10,tau[it->second]));
if (galdef.verbose == -701) cout<<dataEmean[i]<<" "<<flux[i]<<" "<<dataFlux[i]<<" "<<(dataErrPlus[i]/pow(10,tau[it->second]))<<" "<<chisq-prt<<endl;
}
}
}
if (galdef.verbose == -701) cout<<endl<<endl;
}
chisqa[0] = chisq;
prt = chisqa[0];
}
}
if (sets[1]) { //positrons
//Data storage for the electron spectrum
vector<double> positrons = readNuclei(gcr,n_species,1,0,iz1,iz2,ir1,ir2,ix1,ix2,iy1,iy2,fz1,fz2,fr1,fr2,fx1,fx2,fy1,fy2);
if (positrons.size() == 0){
std::cerr<<"No positrons found in nuclei data, not using those in chisq"<<std::endl;
} else {
//Select the appropriate data
std::vector<double> dataEmean, dataFlux, dataErrMinus, dataErrPlus;
vector<int> Z_d, A_d, Z_n, A_n;
vector<string> references(1); //Storage to select references
map<string,int> refs; //The actual references and mapping to modulation
if (expmnts[4]) refs["Ag02"] = 4;
if (expmnts[8]) refs["Du01"] = 8;
Z_d.push_back(1);
A_d.push_back(0);
map<string,int>::iterator it = refs.begin();
for ( ; it != refs.end(); ++it) {
vector<double> dataEmean, dataFlux, dataErrMinus, dataErrPlus;
references[0] = it->first;
retrieveGCRData(dataEmean,dataFlux,dataErrMinus,dataErrPlus,gcr_data,Z_d,A_d,Z_n,A_n,"flux",references);
//Correct for energy scaling
for (int i = 0; i < dataEmean.size(); ++i)
dataEmean[i] *= energyScale[it->second];
//Interpolate and modulate the model
vector<double> flux = interpolateAndModulate(positrons, gcr[0].Ekin, dataEmean, -1, 0, modulation[it->second]);
//Now we can calculate the chisq, including modulation
if (galdef.verbose == -701) cout<<"#Positron data "<<it->first<<":"<<endl;
//cout<<"Modulation:"<<modulation[it->second]<<endl;
for (int i = 0; i < dataEmean.size(); ++i){
if (flux[i] >= 0) {
if (flux[i] < dataFlux[i]) {
chisq += pow((flux[i]-dataFlux[i])/dataErrMinus[i], 2)*pow(10,tau[it->second]) - log(pow(10,tau[it->second]));
if (galdef.verbose == -701) cout<<dataEmean[i]<<" "<<flux[i]<<" "<<dataFlux[i]<<" "<<(dataErrMinus[i]/pow(10,tau[it->second]))<<" "<<chisq-prt<<endl;
} else {
chisq += pow((flux[i]-dataFlux[i])/dataErrPlus[i], 2)*pow(10,tau[it->second]) - log(pow(10,tau[it->second]));
if (galdef.verbose == -701) cout<<dataEmean[i]<<" "<<flux[i]<<" "<<dataFlux[i]<<" "<<(dataErrPlus[i]/pow(10,tau[it->second]))<<" "<<chisq-prt<<endl;
}
}
}
if (galdef.verbose == -701) cout<<endl<<endl;
}
chisqa[1] = chisq;
prt = chisqa[1];
}
}
if (sets[2]) { //protons
//Data storage for the proton spectrum
vector<double> protons = readNuclei(gcr,n_species,1,1,iz1,iz2,ir1,ir2,ix1,ix2,iy1,iy2,fz1,fz2,fr1,fr2,fx1,fx2,fy1,fy2);
if (protons.size() == 0){
std::cerr<<"No protons found in nuclei data, not using those in chisq"<<std::endl;
} else {
//Select the appropriate data
std::vector<double> dataEmean, dataFlux, dataErrMinus, dataErrPlus;
vector<int> Z_d, A_d, Z_n, A_n;
vector<string> references(1); //Storage to select references
map<string,int> refs; //The actual references and mapping to modulation
if (expmnts[2]) refs["Pa06"] = 2;
if (expmnts[4]) refs["Ag02"] = 4;
if (expmnts[5]) refs["Ha04"] = 5;
if (expmnts[6]) refs["Wa02"] = 6;
if (expmnts[7]) refs["As98"] = 7;
if (expmnts[13]) refs["Sa00"] = 13;
Z_d.push_back(1);
A_d.push_back(1);
map<string,int>::iterator it = refs.begin();
for ( ; it != refs.end(); ++it) {
vector<double> dataEmean, dataFlux, dataErrMinus, dataErrPlus;
references[0] = it->first;
retrieveGCRData(dataEmean,dataFlux,dataErrMinus,dataErrPlus,gcr_data,Z_d,A_d,Z_n,A_n,"flux",references);
//Correct for energy scaling
for (int i = 0; i < dataEmean.size(); ++i)
dataEmean[i] *= energyScale[it->second];
//Interpolate and modulate the model
vector<double> flux = interpolateAndModulate(protons, gcr[0].Ekin, dataEmean, 1, 1, modulation[it->second]);
//Now we can calculate the chisq, including modulation
if (galdef.verbose == -701) cout<<"#Proton data "<<it->first<<":"<<endl;
//cout<<"Modulation:"<<modulation[it->second]<<endl;
for (int i = 0; i < dataEmean.size(); ++i){
if (flux[i] >= 0) {
if (flux[i] < dataFlux[i]) {
chisq += pow((flux[i]-dataFlux[i])/dataErrMinus[i], 2)*pow(10,tau[it->second]) - log(pow(10,tau[it->second]));
if (galdef.verbose == -701) cout<<dataEmean[i]<<" "<<flux[i]<<" "<<dataFlux[i]<<" "<<(dataErrMinus[i]/pow(10,tau[it->second]))<<" "<<chisq-prt<<endl;
} else {
chisq += pow((flux[i]-dataFlux[i])/dataErrPlus[i], 2)*pow(10,tau[it->second]) - log(pow(10,tau[it->second]));
if (galdef.verbose == -701) cout<<dataEmean[i]<<" "<<flux[i]<<" "<<dataFlux[i]<<" "<<(dataErrPlus[i]/pow(10,tau[it->second]))<<" "<<chisq-prt<<endl;
}
}
}
if (galdef.verbose == -701) cout<<endl<<endl;
}
chisqa[2] = chisq - prt;
prt = prt + chisqa[2];
}
}
if (sets[3]) { //antiprotons
//Data storage for the proton spectrum
vector<double> antiprotons = readNuclei(gcr,n_species,-1,1,iz1,iz2,ir1,ir2,ix1,ix2,iy1,iy2,fz1,fz2,fr1,fr2,fx1,fx2,fy1,fy2);
if (antiprotons.size() == 0){
std::cerr<<"No anti-protons found in nuclei data, not using those in chisq"<<std::endl;
} else {
//Select the appropriate data
std::vector<double> dataEmean, dataFlux, dataErrMinus, dataErrPlus;
vector<int> Z_d, A_d, Z_n, A_n;
vector<string> references(1); //Storage to select references
map<string,int> refs; //The actual references and mapping to modulation
if (expmnts[14]) refs["Or00"] = 14;
if (expmnts[15]) refs["Ad10"] = 15;
Z_d.push_back(-1);
A_d.push_back(1);
map<string,int>::iterator it = refs.begin();
for ( ; it != refs.end(); ++it) {
vector<double> dataEmean, dataFlux, dataErrMinus, dataErrPlus;
references[0] = it->first;
retrieveGCRData(dataEmean,dataFlux,dataErrMinus,dataErrPlus,gcr_data,Z_d,A_d,Z_n,A_n,"flux",references);
//Correct for energy scaling
for (int i = 0; i < dataEmean.size(); ++i)
dataEmean[i] *= energyScale[it->second];
//Interpolate and modulate the model
vector<double> flux = interpolateAndModulate(antiprotons, gcr[0].Ekin, dataEmean, -1, 1, modulation[it->second]);
//Now we can calculate the chisq, including modulation
if (galdef.verbose == -701) cout<<"#anti-proton data "<<it->first<<":"<<endl;
//cout<<"Modulation:"<<modulation[it->second]<<endl;
for (int i = 0; i < dataEmean.size(); ++i){
if (flux[i] >= 0) {
if (flux[i] < dataFlux[i]) {
chisq += pow((flux[i]-dataFlux[i])/dataErrMinus[i], 2)*pow(10,tau[it->second]) - log(pow(10,tau[it->second]));
if (galdef.verbose == -701) cout<<dataEmean[i]<<" "<<flux[i]<<" "<<dataFlux[i]<<" "<<(dataErrMinus[i]/pow(10,tau[it->second]))<<" "<<chisq-prt<<endl;
} else {
chisq += pow((flux[i]-dataFlux[i])/dataErrPlus[i], 2)*pow(10,tau[it->second]) - log(pow(10,tau[it->second]));
if (galdef.verbose == -701) cout<<dataEmean[i]<<" "<<flux[i]<<" "<<dataFlux[i]<<" "<<(dataErrPlus[i]/pow(10,tau[it->second]))<<" "<<chisq-prt<<endl;
}
}
}
if (galdef.verbose == -701) cout<<endl<<endl;
}
chisqa[3] = chisq - prt;
prt = prt + chisqa[3];
}
}
if (sets[4]) { //Helium
//Data storage for the Helium spectrum of the model
vector<double> He4 = readNuclei(gcr,n_species,2,4,iz1,iz2,ir1,ir2,ix1,ix2,iy1,iy2,fz1,fz2,fr1,fr2,fx1,fx2,fy1,fy2);
if (He4.size()) {
//Select the appropriate data
std::vector<double> dataEmean, dataFlux, dataErrMinus, dataErrPlus;
vector<int> Z_d, A_d, Z_n, A_n;
vector<string> references(1); //Storage to select references
map<string,int> refs; //The actual references and mapping to modulation
if (expmnts[2]) refs["Pa06"] = 2;
if (expmnts[4]) refs["Ag02"] = 4;
if (expmnts[7]) refs["As98"] = 7;
if (expmnts[13]) refs["Sa00"] = 13;
Z_d.push_back(2);
A_d.push_back(4);
map<string,int>::iterator it = refs.begin();
for ( ; it != refs.end(); ++it) {
vector<double> dataEmean, dataFlux, dataErrMinus, dataErrPlus;
references[0] = it->first;
retrieveGCRData(dataEmean,dataFlux,dataErrMinus,dataErrPlus,gcr_data,Z_d,A_d,Z_n,A_n,"flux",references);
//Correct for energy scaling
for (int i = 0; i < dataEmean.size(); ++i)
dataEmean[i] *= energyScale[it->second];
//Interpolate and modulate the model
vector<double> flux = interpolateAndModulate(He4, gcr[0].Ekin, dataEmean, 2, 4, modulation[it->second]);
for (int i = 0; i < flux.size(); ++i) flux[i] *= 4;
//Now we can calculate the chisq, including modulation
if (galdef.verbose == -701) cout<<"#He4 data "<<it->first<<":"<<endl;
//cout<<"Modulation:"<<modulation[it->second]<<endl;
for (int i = 0; i < dataEmean.size(); ++i){
if (flux[i] >= 0) {
if (flux[i] < dataFlux[i]) {
chisq += pow((flux[i]-dataFlux[i])/dataErrMinus[i], 2)*pow(10,tau[it->second]) - log(pow(10,tau[it->second]));
if (galdef.verbose == -701) cout<<dataEmean[i]<<" "<<flux[i]<<" "<<dataFlux[i]<<" "<<(dataErrMinus[i]/pow(10,tau[it->second]))<<" "<<chisq-prt<<endl;
} else {
chisq += pow((flux[i]-dataFlux[i])/dataErrPlus[i], 2)*pow(10,tau[it->second]) - log(pow(10,tau[it->second]));
if (galdef.verbose == -701) cout<<dataEmean[i]<<" "<<flux[i]<<" "<<dataFlux[i]<<" "<<(dataErrPlus[i]/pow(10,tau[it->second]))<<" "<<chisq-prt<<endl;
}
}
}
if (galdef.verbose == -701) cout<<endl<<endl;
}
chisqa[4] = chisq - prt;
prt = prt + chisqa[4];
} else {
cerr<<"No He4 found in the model, not using that in chisq"<<endl;
}
}
if (sets[5]) { //Oxygen
//Data storage for the Oxygen spectrum of the model
vector<double> O16 = readNuclei(gcr,n_species,8,16,iz1,iz2,ir1,ir2,ix1,ix2,iy1,iy2,fz1,fz2,fr1,fr2,fx1,fx2,fy1,fy2);
vector<double> O17 = readNuclei(gcr,n_species,8,17,iz1,iz2,ir1,ir2,ix1,ix2,iy1,iy2,fz1,fz2,fr1,fr2,fx1,fx2,fy1,fy2);
vector<double> O18 = readNuclei(gcr,n_species,8,18,iz1,iz2,ir1,ir2,ix1,ix2,iy1,iy2,fz1,fz2,fr1,fr2,fx1,fx2,fy1,fy2);
//Need at least one of them
if (O16.size() || O17.size() || O18.size() ) {
//Get the data
vector<int> Z_d, A_d, Z_n, A_n;
vector<string> references(1); //Storage to select references
map<string,int> refs; //The actual references and mapping to modulation
if (expmnts[0]) refs["En90"] = 0;
if (expmnts[1]) refs["ACEW"] = 1;
Z_d.push_back(8);
A_d.push_back(-1);
map<string,int>::iterator it = refs.begin();
for ( ; it != refs.end(); ++it) {
vector<double> dataEmean, dataFlux, dataErrMinus, dataErrPlus;
references[0] = it->first;
retrieveGCRData(dataEmean,dataFlux,dataErrMinus,dataErrPlus,gcr_data,Z_d,A_d,Z_n,A_n,"flux",references);
//Correct for energy scaling
for (int i = 0; i < dataEmean.size(); ++i)
dataEmean[i] *= energyScale[it->second];
//Interpolate and modulate the available data
vector<double> flux(dataEmean.size(),0.0);
if (O16.size()) {
vector<double> flux16 = interpolateAndModulate(O16, gcr[0].Ekin, dataEmean, 8, 16, modulation[it->second]);
for (int i = 0; i < flux16.size(); ++i) flux16[i] *= 16;
for (int i = 0; i < flux16.size(); ++i) flux[i] += flux16[i];
} else {
cerr<<"O16 not found in the model"<<endl;
}
if (O17.size()) {
vector<double> flux17 = interpolateAndModulate(O17, gcr[0].Ekin, dataEmean, 8, 17, modulation[it->second]);
for (int i = 0; i < flux17.size(); ++i) flux17[i] *= 17;
for (int i = 0; i < flux17.size(); ++i) flux[i] += flux17[i];
} else {
cerr<<"O17 not found in the model"<<endl;
}
if (O18.size()) {
vector<double> flux18 = interpolateAndModulate(O18, gcr[0].Ekin, dataEmean, 8, 18, modulation[it->second]);
for (int i = 0; i < flux18.size(); ++i) flux18[i] *= 18;
for (int i = 0; i < flux18.size(); ++i) flux[i] += flux18[i];
} else {
cerr<<"O18 not found in the model"<<endl;
}
//Now we can calculate the chisq, including modulation
if (galdef.verbose == -701) cout<<"#Oxygen data "<<it->first<<":"<<endl;
//cout<<"Modulation:"<<modulation[it->second]<<endl;
for (int i = 0; i < dataEmean.size(); ++i){
if (flux[i] >= 0) {
if (flux[i] < dataFlux[i]) {
chisq += pow((flux[i]-dataFlux[i])/dataErrMinus[i], 2)*pow(10,tau[it->second]) - log(pow(10,tau[it->second]));
if (galdef.verbose == -701) cout<<dataEmean[i]<<" "<<flux[i]<<" "<<dataFlux[i]<<" "<<(dataErrMinus[i]/pow(10,tau[it->second]))<<" "<<chisq-prt<<endl;
} else {
chisq += pow((flux[i]-dataFlux[i])/dataErrPlus[i], 2)*pow(10,tau[it->second]) - log(pow(10,tau[it->second]));
if (galdef.verbose == -701) cout<<dataEmean[i]<<" "<<flux[i]<<" "<<dataFlux[i]<<" "<<(dataErrPlus[i]/pow(10,tau[it->second]))<<" "<<chisq-prt<<endl;
}
}
}
if (galdef.verbose == -701) cout<<endl<<endl;
}
chisqa[5] = chisq - prt;
prt = prt + chisqa[5];
} else {
cerr<<"No O found in the model, not using in chisq"<<endl;
}
}
if (sets[6]) { //Carbon
//Data storage for the Carbon spectrum of the model
vector<double> C12 = readNuclei(gcr,n_species,6,12,iz1,iz2,ir1,ir2,ix1,ix2,iy1,iy2,fz1,fz2,fr1,fr2,fx1,fx2,fy1,fy2);
vector<double> C13 = readNuclei(gcr,n_species,6,13,iz1,iz2,ir1,ir2,ix1,ix2,iy1,iy2,fz1,fz2,fr1,fr2,fx1,fx2,fy1,fy2);
if (C12.size() || C13.size()) {
vector<int> Z_d, A_d, Z_n, A_n;
vector<string> references(1); //Storage to select references
map<string,int> refs; //The actual references and mapping to modulation
if (expmnts[0]) refs["En90"] = 0;
if (expmnts[1]) refs["ACEW"] = 1;
Z_d.push_back(6);
A_d.push_back(-1);
map<string,int>::iterator it = refs.begin();
for ( ; it != refs.end(); ++it) {
vector<double> dataEmean, dataFlux, dataErrMinus, dataErrPlus;
references[0] = it->first;
retrieveGCRData(dataEmean,dataFlux,dataErrMinus,dataErrPlus,gcr_data,Z_d,A_d,Z_n,A_n,"flux",references);
//Correct for energy scaling
for (int i = 0; i < dataEmean.size(); ++i)
dataEmean[i] *= energyScale[it->second];
//Interpolate and modulate the available data
vector<double> flux(dataEmean.size(),0.0);
if (C12.size()) {
vector<double> flux12 = interpolateAndModulate(C12, gcr[0].Ekin, dataEmean, 6, 12, modulation[it->second]);
for (int i = 0; i < flux12.size(); ++i) flux12[i] *= 12;
for (int i = 0; i < flux12.size(); ++i) flux[i] += flux12[i];
} else {
cerr<<"C12 not found in the model"<<endl;
}
if (C13.size()) {
vector<double> flux13 = interpolateAndModulate(C13, gcr[0].Ekin, dataEmean, 6, 13, modulation[it->second]);
for (int i = 0; i < flux13.size(); ++i) flux13[i] *= 13;
for (int i = 0; i < flux13.size(); ++i) flux[i] += flux13[i];
} else {
cerr<<"C13 not found in the model"<<endl;
}
//Now we can calculate the chisq, including modulation
if (galdef.verbose == -701) cout<<"#Carbon data "<<it->first<<":"<<endl;
//cout<<"Modulation:"<<modulation[it->second]<<endl;
//cout<<C12[0]<<", "<<C13[0];
for (int i = 0; i < dataEmean.size(); ++i){
if (flux[i] >= 0) {
if (flux[i] < dataFlux[i]) {
chisq += pow((flux[i]-dataFlux[i])/dataErrMinus[i], 2)*pow(10,tau[it->second]) - log(pow(10,tau[it->second]));
if (galdef.verbose == -701) cout<<dataEmean[i]<<" "<<flux[i]<<" "<<dataFlux[i]<<" "<<(dataErrMinus[i]/pow(10,tau[it->second]))<<" "<<chisq-prt<<endl;
} else {
chisq += pow((flux[i]-dataFlux[i])/dataErrPlus[i], 2)*pow(10,tau[it->second]) - log(pow(10,tau[it->second]));
if (galdef.verbose == -701) cout<<dataEmean[i]<<" "<<flux[i]<<" "<<dataFlux[i]<<" "<<(dataErrPlus[i]/pow(10,tau[it->second]))<<" "<<chisq-prt<<endl;
}
}
}
if (galdef.verbose == -701) cout<<endl<<endl;
}
chisqa[6] = chisq - prt;
prt = prt + chisqa[6];
} else {
cerr<<"No C found in the model, not using in chisq"<<endl;
}
}
if (sets[7]) { //B/C ratio
//Data storage for the Carbon spectrum of the model
vector<double> C12 = readNuclei(gcr,n_species,6,12,iz1,iz2,ir1,ir2,ix1,ix2,iy1,iy2,fz1,fz2,fr1,fr2,fx1,fx2,fy1,fy2);
vector<double> C13 = readNuclei(gcr,n_species,6,13,iz1,iz2,ir1,ir2,ix1,ix2,iy1,iy2,fz1,fz2,fr1,fr2,fx1,fx2,fy1,fy2);
//Data storage for the Boron spectrum of the model
vector<double> B10 = readNuclei(gcr,n_species,5,10,iz1,iz2,ir1,ir2,ix1,ix2,iy1,iy2,fz1,fz2,fr1,fr2,fx1,fx2,fy1,fy2);
vector<double> B11 = readNuclei(gcr,n_species,5,11,iz1,iz2,ir1,ir2,ix1,ix2,iy1,iy2,fz1,fz2,fr1,fr2,fx1,fx2,fy1,fy2);
//We must have one of each at leas
if ( (C12.size() || C13.size() ) && (B10.size() || B11.size())) {
//Get the data
vector<int> Z_d, A_d, Z_n, A_n;
vector<string> references(1); //Storage to select references
map<string,int> refs; //The actual references and mapping to modulation
if (expmnts[0]) refs["En90"] = 0;
if (expmnts[1]) {
refs["Da00"] = 1;
//refs["No01"] = 1; /These are really bad datapoints, completely
//incompatible with the rest
}
if (expmnts[2]) refs["Pa07"] = 2;
if (expmnts[3]) refs["Ahn8"] = 3;
Z_d.push_back(6);
A_d.push_back(12);
Z_d.push_back(6);
A_d.push_back(13);
Z_n.push_back(5);
A_n.push_back(10);
Z_n.push_back(5);
A_n.push_back(11);
map<string,int>::iterator it = refs.begin();
for ( ; it != refs.end(); ++it) {
vector<double> dataEmean, dataFlux, dataErrMinus, dataErrPlus;
references[0] = it->first;
retrieveGCRData(dataEmean,dataFlux,dataErrMinus,dataErrPlus,gcr_data,Z_d,A_d,Z_n,A_n,"ratio",references);
//Correct for energy scaling
for (int i = 0; i < dataEmean.size(); ++i)
dataEmean[i] *= energyScale[it->second];
//Interpolate and modulate the data
vector<double> flux(dataEmean.size(),0.0), fluxC(dataEmean.size(), 0.0);
if (C12.size()){
vector<double> flux12 = interpolateAndModulate(C12, gcr[0].Ekin, dataEmean, 6, 12, modulation[it->second]);
for (int i = 0; i < flux12.size(); ++i) flux12[i] *= 12;
for (int i = 0; i < flux12.size(); ++i) fluxC[i] += flux12[i];
} else {
cerr<<"C12 not found, not included in B/C chisq ratio"<<endl;
}
if (C13.size()){
vector<double> flux13 = interpolateAndModulate(C13, gcr[0].Ekin, dataEmean, 6, 13, modulation[it->second]);
for (int i = 0; i < flux13.size(); ++i) flux13[i] *= 13;
for (int i = 0; i < flux13.size(); ++i) fluxC[i] += flux13[i];
} else {
cerr<<"C13 not found, not included in B/C chisq ratio"<<endl;
}
if (B10.size()){
vector<double> flux10 = interpolateAndModulate(B10, gcr[0].Ekin, dataEmean, 5, 10, modulation[it->second]);
for (int i = 0; i < flux10.size(); ++i) flux10[i] *= 10;
for (int i = 0; i < flux10.size(); ++i) flux[i] += flux10[i];
} else {
cerr<<"B10 not found, not included in B/C chisq ratio"<<endl;
}
if (B11.size()){
vector<double> flux11 = interpolateAndModulate(B11, gcr[0].Ekin, dataEmean, 5, 11, modulation[it->second]);
for (int i = 0; i < flux11.size(); ++i) flux11[i] *= 11;
for (int i = 0; i < flux11.size(); ++i) flux[i] += flux11[i];
} else {
cerr<<"B11 not found, not included in B/C chisq ratio"<<endl;
}
//Calculate the ratio and chisq
for (int i = 0; i < flux.size(); ++i) {
if (fluxC[i] > 0 && flux[i] >= 0) {
flux[i] /= fluxC[i];
}else {
flux[i] = -1;
}
}
if (galdef.verbose == -701) cout<<"#B/C ratio "<<it->first<<":"<<endl;
for (int i = 0; i < dataEmean.size(); ++i){
if (flux[i] >= 0) {
if (flux[i] < dataFlux[i]) {
chisq += pow((flux[i]-dataFlux[i])/dataErrMinus[i], 2)*pow(10,tau[it->second]) - log(pow(10,tau[it->second]));
if (galdef.verbose == -701) cout<<dataEmean[i]<<" "<<flux[i]<<" "<<dataFlux[i]<<" "<<(dataErrMinus[i]/pow(10,tau[it->second]))<<" "<<chisq-prt<<endl;
} else {
chisq += pow((flux[i]-dataFlux[i])/dataErrPlus[i], 2)*pow(10,tau[it->second]) - log(pow(10,tau[it->second]));
if (galdef.verbose == -701) cout<<dataEmean[i]<<" "<<flux[i]<<" "<<dataFlux[i]<<" "<<(dataErrPlus[i]/pow(10,tau[it->second]))<<" "<<chisq-prt<<endl;
}
}
}
if (galdef.verbose == -701) cout<<endl<<endl;
}
chisqa[7] = chisq - prt;
prt = prt + chisqa[7];
} else {
cerr<<"We must have both B and C to calculate B/C ratio, not included in chisq"<<endl;
}
}
if (sets[8]) { //Be10/Be9 ratio
//Data storage for the Berillium spectrum of the model
vector<double> Be10 = readNuclei(gcr,n_species,4,10,iz1,iz2,ir1,ir2,ix1,ix2,iy1,iy2,fz1,fz2,fr1,fr2,fx1,fx2,fy1,fy2);
vector<double> Be9 = readNuclei(gcr,n_species,4,9,iz1,iz2,ir1,ir2,ix1,ix2,iy1,iy2,fz1,fz2,fr1,fr2,fx1,fx2,fy1,fy2);
//We must have both
if ( Be10.size() && Be9.size() ) {
//Get the data
vector<int> Z_d, A_d, Z_n, A_n;
vector<string> references(1); //Storage to select references
map<string,int> refs; //The actual references and mapping to modulation
if (expmnts[1]) refs["Ya01"] = 1;
Z_d.push_back(4);
A_d.push_back(9);
Z_n.push_back(4);
A_n.push_back(10);
map<string,int>::iterator it = refs.begin();
for ( ; it != refs.end(); ++it) {
vector<double> dataEmean, dataFlux, dataErrMinus, dataErrPlus;
references[0] = it->first;
retrieveGCRData(dataEmean,dataFlux,dataErrMinus,dataErrPlus,gcr_data,Z_d,A_d,Z_n,A_n,"ratio",references);
//std::cout<<"Reading first "<<dataEmean.size()<<" points"<<std::endl;
//Correct for energy scaling
for (int i = 0; i < dataEmean.size(); ++i)
dataEmean[i] *= energyScale[it->second];
//Interpolate and modulate the data
vector<double> flux = interpolateAndModulate(Be10,gcr[0].Ekin, dataEmean, 4, 10, modulation[it->second]);
vector<double> fluxD = interpolateAndModulate(Be9,gcr[0].Ekin, dataEmean, 4, 9, modulation[it->second]);
for (int i = 0; i < dataEmean.size(); ++i){
flux[i] *= 10;
fluxD[i] *= 9;
}
//Calculate the ratio and chisq
for (int i = 0; i < flux.size(); ++i) {
if(fluxD[i] > 0 && flux[i] >= 0) {
flux[i] /= fluxD[i];
} else {
flux[i] == -1;
}
}
if (galdef.verbose == -701) cout<<"#BE 10/9 ratio "<<it->first<<":"<<endl;
for (int i = 0; i < dataEmean.size(); ++i){
if (flux[i] >= 0) {
if (flux[i] < dataFlux[i]) {
chisq += pow((flux[i]-dataFlux[i])/dataErrMinus[i], 2)*pow(10,tau[it->second]) - log(pow(10,tau[it->second]));
if (galdef.verbose == -701) cout<<dataEmean[i]<<" "<<flux[i]<<" "<<dataFlux[i]<<" "<<(dataErrMinus[i]/pow(10,tau[it->second]))<<" "<<chisq-prt<<endl;
} else {
chisq += pow((flux[i]-dataFlux[i])/dataErrPlus[i], 2)*pow(10,tau[it->second]) - log(pow(10,tau[it->second]));
if (galdef.verbose == -701) cout<<dataEmean[i]<<" "<<flux[i]<<" "<<dataFlux[i]<<" "<<(dataErrPlus[i]/pow(10,tau[it->second]))<<" "<<chisq-prt<<endl;
}
}
}
if (galdef.verbose == -701) cout<<endl<<endl;
}
chisqa[8] = chisq - prt;
} else {
cerr<<"We must have both Be10 and Be9 to calculate Be10/Be9 ratio, not included in chisq"<<endl;
}
}
return chisq;
}