-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathspectrum.cc
469 lines (384 loc) · 14.6 KB
/
spectrum.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
/**
* @file spectrum.cc
* @author Luca Maccione
* @email [email protected]
* @brief In this file all the classes related to the CR injection spectrum are implemented.
*/
#include "spectrum.h"
#include "grid.h"
#include "input.h"
#include "utilities.h"
#ifdef HAVE_ROOT
#include "TFile.h"
#include "TTree.h"
#endif
#include "errorcode.h"
#include <cstdlib>
/**
* @fn extern "C" double dmspec_(double&, int&, double&, double[], int&, double[], int&)
* @brief Call to fortran interface to DarkSUSY
*/
extern "C" double dmspec_(double&, int&, double&, double[], int&, double[], int&);
using namespace std;
TSpectrum::TSpectrum(TGrid* coord) {
spectrum = vector<double>(coord->GetDimE(), 0.0);
}
/*
//The old TSpectrum constructor without multiple breaks (before Simon's changes)
TSpectrum::TSpectrum(TGrid* Coord, Input* in, double inj, double lowinj, bool El) {
vector<double> pp;
if (El) pp = Coord->GetMomentumEl();
else pp = Coord->GetMomentum();
if (El) {
for (int i = 0; i < pp.size(); ++i) {
if (in->Spectrum == PowerLawBreak) {
if (pp[i] < in->sp_ref_rig_break_el) spectrum.push_back(pow(pp[i]/in->sp_ref_rig_break_el,-in->spect_ind_el_low));
else spectrum.push_back(pow(pp[i]/in->sp_ref_rig_break_el,-in->spect_ind_el)*exp(-pp[i]/in->cutoff_rig));
}
else spectrum.push_back(exp(-pow(in->sp_ref_rig_exp_el/pp[i],in->exp_cut_index_el))*pow(pp[i]/in->sp_ref_rig_break_el,-in->spect_ind_el)*exp(-pp[i]/in->cutoff_rig));
}
}
else {
for (int i = 0; i < pp.size(); ++i) {
if (in->Spectrum == PowerLawBreak) {
if (pp[i] < in->sp_ref_rig) spectrum.push_back(pow(pp[i]/in->sp_ref_rig,-lowinj));
else spectrum.push_back(pow(pp[i]/in->sp_ref_rig,-inj));
}
else {
//if (pp[i] < in->sp_ref_rig) spectrum.push_back(pow(pp[i]/in->sp_ref_rig,-lowinj));
//else spectrum.push_back(pow(pp[i]/in->sp_ref_rig,-inj));
spectrum.push_back(exp(-pow(in->sp_ref_rig_exp/pp[i],1.))*pow(pp[i]/in->sp_ref_rig,-inj));
}
}
}
}
//MW130621: see above, this HAS to be made consistent.
//DG130902: this is the new TSpectrum constructor, in my opinion this has to be called only when DMCMC block is used
//
//
*/
TSpectrum::TSpectrum(TGrid* Coord, Input* in, const vector<double>& break_positions, const vector<double>& slopes, const double CutoffRig, bool El, bool ExtraComponent) {
if (in->feedback >0) cout << "Spectrum constructor " << endl;
//DG: add possibility of exp cutoff again as in the old version!!
vector<double> pp = (El) ? Coord->GetMomentumEl() : Coord->GetMomentum();
vector<double> Ek = Coord->GetEk();
if (El) {
// DG30.09.2013 NEW implementation of injection slopes for nuclei and electrons with arbitrary number of breaks
if (El) cout << "Electrons" << endl;
if (ExtraComponent) cout << "Extra component" << endl;
vector<double> el_breaks;
vector<double> el_injections;
double el_cutoff ;
if (ExtraComponent) {
el_breaks = in->inp_break_extra_positions;
el_injections = in->inp_inj_extra_indexes;
el_cutoff = in->cutoff_rig_extra; cout << "Extra component cutoff: " << el_cutoff << endl;
}
else {
el_breaks = in->inp_break_el_positions;
el_injections = in->inp_inj_el_indexes;
el_cutoff = in->cutoff_rig_el; cout << "Electron cutoff: " << el_cutoff << endl;
}
int n_breaks = el_breaks.size();
if (in->feedback >0)
cout << "Building ELECTRON spectrum with " << n_breaks << " breaks " << endl;
for (int i = 0; i < pp.size(); ++i) {
int slope_number = 0;
if (n_breaks > 0) {
for (int j = 0; j < n_breaks; j++) {
if (el_breaks[j] < pp[i])
slope_number++;
}
}
if (n_breaks == 0)
spectrum.push_back( pow(pp[i]/pp[0], -el_injections[0] ) * exp(-Ek[i]/el_cutoff) ) ;
else {
if (slope_number == 0)
spectrum.push_back( pow(pp[i]/el_breaks[0], -el_injections[0] )*exp(-Ek[i]/el_cutoff) );
else if (slope_number == 1)
spectrum.push_back( pow(pp[i]/el_breaks[0], -el_injections[1] )*exp(-Ek[i]/el_cutoff) );
else {
double number = pow(pp[i]/el_breaks[slope_number-1], -el_injections[slope_number] );
for (int j=1; j<slope_number; j++)
number *= pow(el_breaks[j]/el_breaks[j-1], -el_injections[j]) ;
spectrum.push_back(number*exp(-Ek[i]/el_cutoff));
}
}
if (in->feedback >0)
cout << "energy = " << pp[i] << "; alpha = " << -el_injections[slope_number] << "; spectrum = " << spectrum.back() << endl;
}
cout << endl;
}
else {
// DG30.09.2013 NEW implementation of injection slopes for nuclei and electrons with arbitrary number of breaks
int n_breaks = break_positions.size();
if (in->feedback >0) {
cout << "Building NUCLEAR spectrum with " << n_breaks << " breaks " << endl;
cout << "Number of slopes: " << slopes.size() << endl;
}
for (int i = 0; i < pp.size(); ++i) {
int slope_number = 0;
if (n_breaks > 0) {
for (int j = 0; j < n_breaks; j++) {
if (break_positions[j] < pp[i])
slope_number++;
}
}
if (n_breaks == 0)
spectrum.push_back( pow(pp[i]/pp[0], -slopes[0] ) ) ;
else {
if (slope_number == 0)
spectrum.push_back( pow(pp[i]/break_positions[0], -slopes[0] ) ) ;
else if (slope_number == 1)
spectrum.push_back( pow(pp[i]/break_positions[0], -slopes[1] ) ) ;
else {
//cout<<i<<endl;
double number = pow(pp[i]/break_positions[slope_number-1], -slopes[slope_number] );
for (int j=1; j<slope_number; j++)
number *= pow( break_positions[j]/break_positions[j-1], -slopes[j] );
spectrum.push_back(number);
}
}
if (CutoffRig > 0){ spectrum[i] *= exp(-pp[i]/CutoffRig);
std::cout << "Cutoff: " << CutoffRig << std::endl; }
if (in->feedback >0)
cout << "energy = " << pp[i] << "; alpha = " << -slopes[slope_number] << "; spectrum = " << spectrum.back() << endl;
}
}
// Spectrum integral over energy: Ek*dN/dEk
double integral = 0.;
for ( size_t i = 0; i < Ek.size(); ++i )
integral += Ek[i]*Ek[i]*spectrum[i];
integral *= Coord->GetDeltaE();
if (in->feedback >0)
cout << "Spectrum has been integrated over energy. I = " << integral << endl;
// DG30.09.2013 NEW implementation of injection slopes for nuclei and electrons with arbitrary number of breaks
/*
int n_breaks = break_positions.size();
if (in->feedback >0){
cout << "Building ";
if (El==true) cout << "ELECTRON ";
else cout << "NUCLEI ";
cout << "spectrum with " << n_breaks << " breaks " << endl;
}
for (int i = 0; i < pp.size(); ++i) {
int slope_number = 0;
for (int j = 0; j < n_breaks; j++) {
if (break_positions[j] < pp[i])
slope_number++;
}
if (in->feedback >0) cout << "energy = " << pp[i] << " alpha = " << -slopes[slope_number] << endl;
if (slope_number == 0)
spectrum.push_back( pow(pp[i]/break_positions[0], -slopes[0] ) ) ;
else if (slope_number == 1)
spectrum.push_back( pow(pp[i]/break_positions[0], -slopes[1] ) ) ;
else {
double number = pow(pp[i]/break_positions[slope_number-1], -slopes[slope_number] );
for (int j=1; j<slope_number; j++)
number *= pow( break_positions[j]/break_positions[j-1], -slopes[j] );
spectrum.push_back(number);
}
}
if (CutoffRig > 0) for (int i = 0; i < pp.size(); ++i) spectrum[i] *= exp(-pp[i]/CutoffRig);
// Spectrum integral over energy: Ek*dN/dEk
double integral = 0.;
for ( size_t i = 0; i < Ek.size(); ++i )
integral += Ek[i]*Ek[i]*spectrum[i];
integral *= Coord->GetDeltaE();
if (in->feedback >0)
cout << "Spectrum has been integrated over energy. I = " << integral << endl;
*/
return ;
}
TSpectrum::TSpectrum(TGrid* Coord, Input* in, int yieldk) {
vector<double> Ek = Coord->GetEk();
if (in->MOVING_CLUMP == true) {
for (int i = 0; i < Ek.size(); ++i) {
double clump_sp = in->clump_norm * pow(Ek[i],in->clump_inj) * exp(-Ek[i]/in->clump_cutoff);
if (in->DMs == Delta) {
if (Ek[i] <= in->EkDelta && Ek[i+1] > in->EkDelta) spectrum.push_back(1.0/(Ek[i+1]-Ek[i]));
else spectrum.push_back(0.0);
}
else
spectrum.push_back(clump_sp);
}
}
else {
if (in->DMs == Delta) {
for (int i = 0; i < Ek.size(); ++i) {
if (Ek[i] <= in->EkDelta && Ek[i+1] > in->EkDelta) spectrum.push_back(1.0/(Ek[i+1]-Ek[i]));
else spectrum.push_back(0.0);
}
}
else {
int nE = Ek.size();
double DSsp[nE];
double Etot[nE];
for (int i = 0; i < nE; ++i) Etot[i] = Ek[i];
dmspec_(in->mx, in->dmmode, in->sigmav, Etot, yieldk, DSsp, nE);
//cout << "Injected spectrum" << endl;
for (int i = 0; i < nE; ++i) {
spectrum.push_back(DSsp[i]);
//cout << Ek[i] << " " << DSsp[i] << endl;
}
if (in->feedback >0) cout << "Used DS spectrum" << endl;
}
}
}
TSpectrum::TSpectrum(TGrid* coord, string filename, int yieldk) {
//DG17.10.2013
//revised implementation
/*if (in->feedback >0)*/ cout << "TSpectrum constructor " << endl;
spectrum = vector<double>(coord->GetDimE(), 0.0);
vector<double> Ek = coord->GetEk();
double logEk[Ek.size()];
for (size_t i = 0; i < Ek.size(); ++i) {
//cout << i << " " << Ek[i] << endl;
logEk[i] = log10(Ek[i]);
}
ifstream infile(filename.c_str(), ios::in);
cout << "Reading data file " << filename.c_str() << endl;
vector<double> logE_from_file, logsp_from_file;
double x,y;
while (infile >> x >> y) {
logE_from_file.push_back(log10(x));
logsp_from_file.push_back(log10(y));
cout << x << " " << y << " " << log10(x) << " " << log10(y) <<endl;
}
cout << "Read "<< logE_from_file.size() << " points from data file" << endl;
cout << "Interpolation to DRAGON energy grid " << endl;
for (size_t i = 0; i < Ek.size(); ++i) {
if ( (logEk[i] > logE_from_file.back()+0.0001) || (logEk[i] < logE_from_file.front()) ) {
spectrum[i] = 0.0;
cout << Ek[i] << " - this energy is outside range provided by the user: spectrum here will set to zero. " << endl;
//cout << logEk[i] << " " << logE_from_file.back() << " " << logE_from_file.front() << endl;
}
else {
int j = 0;
while (logE_from_file[j] < logEk[i]) j++;
j = 0;
for (int jj=0; jj<logE_from_file.size(); jj++) {
if(logEk[i] >= logE_from_file[jj] ) {
j ++;
}
}
cout << i << " " << Ek[i] << " " << j << " " << logsp_from_file[j-1] << " " << logsp_from_file[j] << endl;
if(j>=logsp_from_file.size()) {
cout << "Warning: j above the size of logsp_from_file vector " << endl;
j = logsp_from_file.size()-1;
}
double r1 = (logE_from_file[j]-logEk[i])/(logE_from_file[j] - logE_from_file[j-1]);
spectrum[i] = pow(10, logsp_from_file[j-1]*r1 + (1.-r1)*logsp_from_file[j]);
cout << Ek[i] << " " << " interpolated spectrum: "<< spectrum[i] << endl;
}
}
infile.close();
cout << "Spectrum was correctly interpolated" << endl;
return ;
}
TSpectrum::TSpectrum(TGrid* Coord, int mass, int dmmode, int yieldk) {
#ifdef HAVE_ROOT
cout << "Mass = " << mass << endl;
vector<double> Ek = Coord->GetEk();
const char* possible_channels[29] = {"eL", "eR", "e", "MuL", "MuR", "Mu", "TauL", "TauR", "Tau", "q", "c", "b", "t", "WL", "WT", "W", "ZL", "ZT", "Z", "g", "Gamma", "h", "Nue", "NuMu", "NuTau", "Ve", "VMu", "VTau"};
string channel = find_channel(dmmode);
bool found = false;
for (int i = 0; i < 29 && !found; ++i) found = !strcmp(channel.c_str(), possible_channels[i]);
if (!found) {
cerr << "Channel " << channel << " is not present in our database." << endl;
exit(CHANNEL_NOT_PRESENT);
}
if (mass < 5 || mass > int(1e5)) {
cerr << "DM mass out of range." << endl;
exit(MASS_OUT_OF_RANGE);
}
TFile* rootfileDM = new TFile(EWdatafile.c_str(), "READONLY");
TTree* tr;
if (yieldk == 154) rootfileDM->GetObject("antiprotons", tr);
else if (yieldk == 151) rootfileDM->GetObject("positrons", tr);
tr->SetBranchStatus("*", 0);
tr->SetBranchStatus("mDM", 1);
tr->SetBranchStatus("log10E_over_mDM", 1);
tr->SetBranchStatus(channel.c_str(), 1);
ostringstream drawstr;
drawstr << channel << ":log10E_over_mDM";
ostringstream constrstr;
constrstr << "mDM == " << mass;
int Nap = tr->Draw(drawstr.str().c_str(), constrstr.str().c_str(), "goff");
if (Nap == 0) {
cerr << "No particles are available for this mass. Choose an integer value." << endl;
exit(NO_MASS);
}
vector<double> energy;
vector<double> orig_spectrum;
for (int i = 0; i < Nap; ++i) {
energy.push_back(double(mass)*pow(10, tr->GetV2()[i]));
orig_spectrum.push_back(tr->GetV1()[i]/log(10.)/energy[i]);
//cout << energy.back() << " " << orig_spectrum.back() << endl;
}
rootfileDM->Close();
//cout << "Injected spectrum" << endl;
for (size_t i = 0; i < Ek.size(); ++i) {
if (Ek[i] > mass) spectrum.push_back(0);
else spectrum.push_back(InterpSpectrum(Ek[i], energy, orig_spectrum));
//cout << Ek[i] << " " << spectrum.back() << endl;
}
cout << "Used Ciafaloni spectrum" << endl;
#endif
return ;
}
std::string TSpectrum::find_channel(int dmmode) {
std::string channel;
switch(dmmode) {
case 3:
channel = "e";
break;
case 13:
channel = "W";
break;
case 12:
channel="Z";
break;
case 17:
channel="Mu";
std::cerr << "Warning: did you mean VMu ??" << std::endl;
break;
case 19:
channel="Tau";
std::cerr << "Warning: did you mean VTau ??" << std::endl;
break;
case 22:
channel = "c";
break;
case 24:
channel = "t";
break;
case 25:
channel = "b";
break;
case 26:
channel = "g";
break;
default:
std::cerr << "The mode you requested was not implemented. You requested mode " << dmmode << std::endl;
exit(INVALIDDMMODE);
break;
}
return channel;
}
double TSpectrum::InterpSpectrum(const double& en, const std::vector<double>& energy, const std::vector<double>& orig_spectrum) const {
int i = 0;
while (energy[i] < en) ++i;
if (i > energy.size()) std::cout << "Index out of range: Ek = " << en << std::endl;
double r1 = log10(energy[i]/en)/log10(energy[i]/energy[i-1]);
return pow(10., r1*log10(orig_spectrum[i-1]) + (1.0-r1)*log10(orig_spectrum[i]));
}
/*
TSpectrumExtra::TSpectrumExtra(TGrid* grid, Input* in) : TSpectrum(grid) {
vector<double> pp = grid->GetMomentumEl();
for (int i = 0; i < pp.size(); ++i) {
if (pp[i] < in->sp_ref_rig_break_el_extra)
spectrum[i] = (pow(pp[i]/in->sp_ref_rig_break_el_extra,-in->spect_ind_el_low_extra));
else spectrum[i] = (pow(pp[i]/in->sp_ref_rig_break_el_extra,-in->spect_ind_el_extra)*exp(-pp[i]/in->cutoff_rig_extra));
}}
*/