EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
spectrum.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file spectrum.cpp
1 
2 /*
3  <one line to give the program's name and a brief idea of what it does.>
4  Copyright (C) <year> <name of author>
5 
6 p This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 
19 */
20 
21 #include "spectrum.h"
22 #include <cmath>
23 #include "beambeamsystem.h"
24 #include <randomgenerator.h>
25 #include <iostream>
26 
28  _bMin(5.0)
29  ,_bMax(128000.0)
30  ,_nBbins(6400)
31  ,_probOfBreakup(_nBbins)
32  ,_beamBeamSystem(bbs)
33  ,_nK(10000)
34  ,_fnSingle(_nK)
35  ,_fnDouble(_nK)
36  ,_fnSingleCumulative(_nK+1)
37  ,_fnDoubleCumulative(_nK+1)
38  ,_fnDoubleInt(_nK)
39  ,_fnDoubleIntCumulative(_nK+1)
40  ,_eGamma(_nK+1)
41  ,_eGammaMin(6.0)
42  ,_eGammaMax(600000.0)
43  ,_zTarget(82)
44  ,_aTarget(278)
45  ,_hadBreakProbCalculated(false)
46  ,_randy(randy)
47 {
48  _eGamma.resize(_nK+1);
49  _probOfBreakup.resize(_nBbins);
50 }
51 
53 {
54 
55  _fnSingle.resize(_nK);
56  _fnSingleCumulative.resize(_nK+1);
57 
58  double eg_inc = exp(log(_eGammaMax/_eGammaMin)/(double)_nK);
59 
60  double egamma = _eGammaMin;
61  for (int i = 0; i < _nK+1; i++)
62  {
63  _eGamma[i] = egamma;
64  egamma = egamma * eg_inc;
65  }
66  egamma = _eGammaMin;
67 
68  double fnorm = 0;
69 
70 
71  if (_hadBreakProbCalculated == false)
72  {
74  }
75  double binc = exp((log(_bMax/_bMin))/(double)_nBbins);
76 
77  for (int i = 0; i < _nK; i++)
78  {
79  double b = _bMin;
80 
81  double bint = 0.0;
82 
83  double f1 = 0;
84  double f2 = 0;
85 
86  for (int j = 0; j < _nBbins - 1; j++)
87  {
88  double bold = b;
89  if (j == 0)
90  {
91  f1 = getTransformedNofe(egamma, b)*getSigma(egamma)*_probOfBreakup[j]*b;
92  //std::cout << fProbOfBreakup[j] << std::endl;
93  }
94  else
95  {
96  f1 = f2;
97  }
98  b = b*binc;
99  f2 = getTransformedNofe(egamma, b)*getSigma(egamma)*_probOfBreakup[j+1]*b;;
100  bint = bint + 0.5*(f1+f2)*(b-bold);
101  }
102  bint = 2.0*starlightConstants::pi*bint;
103  if (i == 0)
104  {
105  fnorm = 1.0/bint;
106  }
107  _fnSingle[i] = bint*(_eGamma[i+1]-_eGamma[i]);
108 
109  egamma = egamma*eg_inc;
110  }
111 
112  _fnSingleCumulative[0] = 0.00;
113  for (int i = 0; i < _nK; i++)
114  {
116  }
117 
118  double fnormfactor = 1.00/_fnSingleCumulative[_nK];
119  for (int i = 0; i < _nK; i++)
120  {
121  _fnSingleCumulative[i+1] = fnormfactor*_fnSingleCumulative[i+1];
122  }
123 
124  return 0;
125 
126 }
127 
129 {
130  //Quick fix for now TODO: Fix it!
131  _nK = 100;
132 
133  _fnDouble.resize(_nK);
134  _fnDoubleInt.resize(_nK);
135  _fnDoubleIntCumulative.resize(_nK+1);
136  _fnDoubleCumulative.resize(_nK+1);
137  for (int i = 0; i < _nK; i++)
138  {
139  _fnDouble[i].resize(_nK);
140  _fnDoubleCumulative[i].resize(_nK+1);
141  }
142  _fnDoubleCumulative[_nK].resize(_nK+1);
143 
144  double eg_inc = exp(log(_eGammaMax/_eGammaMin)/(double)_nK);
145  double egamma1 = _eGammaMin;
146  double egamma2 = _eGammaMin;
147 
148  for (int i = 0; i < _nK+1; i++)
149  {
150  _eGamma[i] = egamma1;
151  egamma1 = egamma1 * eg_inc;
152  }
153  egamma1 = _eGammaMin;
154 
155  double fnorm = 0;
156 
157  if (_hadBreakProbCalculated == false)
158  {
160  }
161 
162  double binc = exp((log(_bMax/_bMin))/(double)_nBbins);
163 
164  int nbbins = _nBbins;
165 
166  for (int i = 0; i < _nK; i++)
167  {
168 
169  egamma2 = _eGammaMin;
170 
171  for (int j = 0; j < _nK; j++)
172  {
173  double bint = 0.0;
174  double b = _bMin;
175  double f1 = 0;
176  double f2 = 0;
177 
178  for (int k = 0; k < nbbins - 1; k++)
179  {
180  double bold = b;
181 
182  if (k == 0)
183  {
184  f1 = getTransformedNofe(egamma1, b) * getTransformedNofe(egamma2, b)
185  * getSigma(egamma1) * getSigma(egamma2) *_probOfBreakup[k]*b; }
186  else
187  {
188  f1 = f2;
189  }
190  b = b*binc;
191  f2 = getTransformedNofe(egamma1, b) * getTransformedNofe(egamma2, b)
192  * getSigma(egamma1) * getSigma(egamma2) *_probOfBreakup[k+1]*b;
193  bint = bint + 0.5*(f1+f2)*(b-bold);
194  }
195  bint = 2.0*starlightConstants::pi*bint;
196  _fnDouble[i][j] = bint * (_eGamma[i+1] - _eGamma[i]) * (_eGamma[j+1] - _eGamma[j]);
197  egamma2 = egamma2 * eg_inc;
198  }
199  egamma1 = egamma1 * eg_inc;
200  }
201 
202  for (int i = 0; i < _nK; i++)
203  {
204  _fnDoubleInt[i] = 0.0;
205  for (int j = 0; j < _nK; j++)
206  {
207  _fnDoubleInt[i] = _fnDoubleInt[i] + _fnDouble[i][j];
208  }
209  }
210 
211  _fnDoubleIntCumulative[0] = 0.0;
212  for (int i = 1; i < _nK+1; i++)
213  {
215  }
216 
217  fnorm = 1.0/_fnDoubleIntCumulative[_nK];
218  for (int i = 0; i < _nK+1; i++)
219  {
221  }
222 
223  return 0;
224 }
225 
227 {
228  double xtest = 0;
229  int itest = 0;
230  double egamma = 0.0;
231 
232  xtest = _randy.Rndom();
233  while (xtest > _fnSingleCumulative[itest])
234  {
235  itest++;
236  }
237  itest = itest - 1;
238 
239  if (itest >= _nK || itest < 0)
240  {
241  std::cerr << "ERROR: itest: " << itest << std::endl;
242 
243  }
244 
245  double delta_f = xtest - _fnSingleCumulative[itest];
246  if (delta_f <= 0.0)
247  {
248  std::cout << "WARNING: delta_f: " << delta_f << std::endl;
249  return -1;
250  }
251  double dE = _eGamma[itest+1] - _eGamma[itest];
252  double dF = _fnSingleCumulative[itest+1] - _fnSingleCumulative[itest];
253 
254  double delta_e = delta_f*dE/dF;
255 
256  if (delta_e > (_eGamma[itest+1] - _eGamma[itest]))
257  {
258  std::cerr << "ERROR: delta_E: " << delta_e << std::endl;
259  }
260 
261  egamma = _eGamma[itest] + delta_e;
262  return egamma;
263 }
264 
265 void spectrum::drawKdouble(float& egamma1, float& egamma2)
266 {
267  double xtest1 = 0.0;
268  double xtest2 = 0.0;
269  int itest1 = 0;
270  int itest2 = 0;
271 
272  xtest1 = _randy.Rndom();
273 
274  while (xtest1 > _fnDoubleIntCumulative[itest1])
275  {
276  itest1++;
277  }
278  itest1 = itest1 - 1;
279 
280  if (itest1 >= _nK || itest1 < 0)
281  {
282  std::cerr << "ERROR: itest1: " << itest1 << std::endl;
283  }
284  double delta_f = xtest1 - _fnDoubleIntCumulative[itest1];
285 
286  if (delta_f <= 0.0)
287  {
288  std::cout << "WARNING: delta_f: " << delta_f << std::endl;
289  }
290 
291  double dE = _eGamma[itest1+1] - _eGamma[itest1];
292  double dF = _fnDoubleIntCumulative[itest1+1] - _fnDoubleIntCumulative[itest1];
293 
294  double delta_e = delta_f*dE/dF;
295 
296  if (delta_e > (_eGamma[itest1+1] - _eGamma[itest1]))
297  {
298  std::cerr << "ERROR: delta_E: " << delta_e << std::endl;
299  }
300 
301  egamma1 = _eGamma[itest1] + delta_e;
302 
303  // Second gamma
304 
305  std::vector<double> fn_second_cumulative(_nK+1);
306 
307  fn_second_cumulative[0] = 0.0;
308  for(int i = 1; i < _nK+1; i++)
309  {
310  fn_second_cumulative[i] = fn_second_cumulative[i-1] + _fnDouble[itest1][i-1];
311  }
312 
313  double norm_factor = 1.0/fn_second_cumulative[_nK];
314  for(int i = 0; i < _nK+1; i++)
315  {
316  fn_second_cumulative[i] = norm_factor*fn_second_cumulative[i];
317  }
318 
319  xtest2 = _randy.Rndom();
320 
321  while (xtest2 > fn_second_cumulative[itest2])
322  {
323  itest2++;
324  }
325  itest2 = itest2 - 1;
326 
327  if (itest2 >= _nK || itest2 < 0)
328  {
329  std::cerr << "ERROR: itest2: " << itest2 << std::endl;
330  }
331  delta_f = xtest2 - fn_second_cumulative[itest2];
332 
333  if (delta_f <= 0.0)
334  {
335  std::cout << "WARNING: delta_f: " << delta_f << std::endl;
336  }
337 
338  dE = _eGamma[itest2+1] - _eGamma[itest2];
339  dF = fn_second_cumulative[itest2+1] - fn_second_cumulative[itest2];
340 
341  delta_e = delta_f*dE/dF;
342 
343  if (delta_e > (_eGamma[itest2+1] - _eGamma[itest2]))
344  {
345  std::cerr << "ERROR: delta_E: " << delta_e << std::endl;
346  }
347 
348  egamma2 = _eGamma[itest2] + delta_e;
349 
350  return;
351 }
352 
353 
355 {
356 
357  int nbbins = _nBbins;
358 
359  double b_min = _bMin;
360  double binc = exp((log(_bMax/_bMin))/(double)_nBbins);
361 
362  double b = b_min;
363 
364  _probOfBreakup.resize(nbbins);
365 
366  for (int i = 0; i < nbbins; i++)
367  {
368  double bimp = b;
369  double rhad = 0;
371  _probOfBreakup[i] = exp(-rhad);
372  b = b*binc;
373  }
374  return true;
375 }
376 
377 double spectrum::getFnSingle(double egamma) const
378 {
379  double eginc = exp(log(_eGammaMax/_eGammaMin)/static_cast<double>(_nK));
380  int i1 = log(egamma/_eGammaMin)/log(eginc);
381  int i2 = i1 + 1;
382  double fnSingle = 0.0;
383 
384  if (i1 < 0 || i2 > _nK)
385  {
386  std::cout << "I1, I2 out of bounds. Egamma = " << egamma << std::endl;
387  std::cout << "I1, I2 = " << i1 << ", " << i2 << std::endl;
388  fnSingle = 0.0;
389  }
390  else
391  {
392  double dE = _eGamma[i2] - _eGamma[i1];
393  double eFrac = (egamma - _eGamma[i1])/dE;
394 
395  if (eFrac < 0.0 || eFrac > 1.0)
396  {
397  std::cout << "WARNING: Efrac = " << eFrac << std::endl;
398  }
399  fnSingle = (1.0 - eFrac)*_fnSingle[i1] + eFrac*_fnSingle[i2];
400  }
401  return fnSingle;
402 }
403 
404 double spectrum::getFnDouble(double egamma1, double egamma2) const
405 {
406  double eginc = exp(log(_eGammaMax/_eGammaMin)/static_cast<double>(_nK));
407  int i1 = log(egamma1/_eGammaMin)/log(eginc);
408  int i2 = i1 + 1;
409  double fnDouble = 0.0;
410 
411  if (i1 < 0 || i2 > _nK)
412  {
413  std::cout << "I1, I2 out of bounds. Egamma1 = " << egamma1 << std::endl;
414  std::cout << "I1, I2 = " << i1 << ", " << i2 << std::endl;
415  fnDouble = 0.0;
416  return fnDouble;
417  }
418 
419  int j1 = log(egamma2/_eGammaMin)/log(eginc);
420  int j2 = j1 + 1;
421 
422  if (j1 < 0 || j2 > _nK)
423  {
424  std::cout << "J1, J2 out of bounds. Egamma2 = " << egamma2 << std::endl;
425  std::cout << "J1, J2 = " << j1 << ", " << j2 << std::endl;
426  fnDouble = 0.0;
427  return fnDouble;
428  }
429 
430  double dE1 = _eGamma[i2] - _eGamma[i1];
431  double eFrac1 = (egamma1 - _eGamma[i1])/dE1;
432 
433  if (eFrac1 < 0.0 || eFrac1 > 1.0)
434  {
435  std::cout << "WARNING: Efrac1 = " << eFrac1 << std::endl;
436  }
437 
438  double ptemp1 = (1.0 - eFrac1)*_fnDouble[i1][j1] + eFrac1*_fnDouble[i2][j1];
439  double ptemp2 = (1.0 - eFrac1)*_fnDouble[i1][j2] + eFrac1*_fnDouble[i2][j2];
440 
441  double dE2 = _eGamma[j2] - _eGamma[j1];
442  double eFrac2 = (egamma2 - _eGamma[j1])/dE2;
443 
444  if (eFrac2 < 0.0 || eFrac2 > 1.0)
445  {
446  std::cout << "WARNING: Efrac2 = " << eFrac2 << std::endl;
447  }
448 
449  fnDouble = (1.0 - eFrac2)*ptemp1 + eFrac2*ptemp2;
450 
451  return fnDouble;
452 
453 }
454 
455 double spectrum::getTransformedNofe(double egamma, double b)
456 {
457  double factor = 1.0/(2.0*_beamBeamSystem->beamLorentzGamma());
458  double res = factor * _beamBeamSystem->beam1().photonDensity(b, egamma*factor);
459 
460  return res;
461 }
462 
463 
464 
465