3 <one line to give the program's name and a brief idea of what it does.>
4 Copyright (C) <year> <name of author>
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.
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.
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/>.
23 #include "beambeamsystem.h"
24 #include <randomgenerator.h>
27 spectrum::spectrum(beamBeamSystem *bbs) :
31 ,_probOfBreakup(_nBbins)
36 ,_fnSingleCumulative(_nK+1)
37 ,_fnDoubleCumulative(_nK+1)
39 ,_fnDoubleIntCumulative(_nK+1)
45 ,_hadBreakProbCalculated(false)
47 _eGamma.resize(_nK+1);
48 _probOfBreakup.resize(_nBbins);
51 int spectrum::generateKsingle()
54 _fnSingle.resize(_nK);
55 _fnSingleCumulative.resize(_nK+1);
57 double eg_inc = exp(log(_eGammaMax/_eGammaMin)/(double)_nK);
59 double egamma = _eGammaMin;
60 for (int i = 0; i < _nK+1; i++)
63 egamma = egamma * eg_inc;
70 if (_hadBreakProbCalculated == false)
72 _hadBreakProbCalculated = generateBreakupProbabilities();
74 double binc = exp((log(_bMax/_bMin))/(double)_nBbins);
76 for (int i = 0; i < _nK; i++)
85 for (int j = 0; j < _nBbins - 1; j++)
90 //f1 = fBeamBeamSystem->getBeam1().nofe(egamma, b)*GetSigma(egamma)*fProbOfBreakup[j]*b;
91 f1 = getTransformedNofe(egamma, b)*getSigma(egamma)*_probOfBreakup[j]*b;
92 //std::cout << fProbOfBreakup[j] << std::endl;
99 // f2 = fBeamBeamSystem->getBeam1().nofe(egamma, b)*GetSigma(egamma)*fProbOfBreakup[j+1]*b;;
100 f2 = getTransformedNofe(egamma, b)*getSigma(egamma)*_probOfBreakup[j+1]*b;;
101 bint = bint + 0.5*(f1+f2)*(b-bold);
103 bint = 2.0*starlightConstants::pi*bint;
108 _fnSingle[i] = bint*(_eGamma[i+1]-_eGamma[i]);
110 egamma = egamma*eg_inc;
113 _fnSingleCumulative[0] = 0.00;
114 for (int i = 0; i < _nK; i++)
116 _fnSingleCumulative[i+1] = _fnSingleCumulative[i]+_fnSingle[i];
119 double fnormfactor = 1.00/_fnSingleCumulative[_nK];
120 for (int i = 0; i < _nK; i++)
122 _fnSingleCumulative[i+1] = fnormfactor*_fnSingleCumulative[i+1];
129 int spectrum::generateKdouble()
131 //Quick fix for now TODO: Fix it!
134 _fnDouble.resize(_nK);
135 _fnDoubleInt.resize(_nK);
136 _fnDoubleIntCumulative.resize(_nK+1);
137 _fnDoubleCumulative.resize(_nK+1);
138 for (int i = 0; i < _nK; i++)
140 _fnDouble[i].resize(_nK);
141 _fnDoubleCumulative[i].resize(_nK+1);
143 _fnDoubleCumulative[_nK].resize(_nK+1);
145 double eg_inc = exp(log(_eGammaMax/_eGammaMin)/(double)_nK);
146 double egamma1 = _eGammaMin;
147 double egamma2 = _eGammaMin;
149 for (int i = 0; i < _nK+1; i++)
151 _eGamma[i] = egamma1;
152 egamma1 = egamma1 * eg_inc;
154 egamma1 = _eGammaMin;
158 if (_hadBreakProbCalculated == false)
160 _hadBreakProbCalculated = generateBreakupProbabilities();
163 double binc = exp((log(_bMax/_bMin))/(double)_nBbins);
165 int nbbins = _nBbins;
167 //double b_min = _bMin;
168 //double b_max = _bMax;
170 for (int i = 0; i < _nK; i++)
173 egamma2 = _eGammaMin;
174 //double sum_over_k = 0.0;
176 for (int j = 0; j < _nK; j++)
183 for (int k = 0; k < nbbins - 1; k++)
189 // f1 = fBeamBeamSystem->getBeam1().nofe(egamma1, b) * fBeamBeamSystem->getBeam2().nofe(egamma2, b)
190 // * GetSigma(egamma1) * GetSigma(egamma2) *fProbOfBreakup[j]*b;
191 f1 = getTransformedNofe(egamma1, b) * getTransformedNofe(egamma2, b)
192 * getSigma(egamma1) * getSigma(egamma2) *_probOfBreakup[k]*b; }
198 // f2 = fBeamBeamSystem->getBeam1().nofe(egamma1, b) * fBeamBeamSystem->getBeam2().nofe(egamma2, b)
199 // * GetSigma(egamma1) * GetSigma(egamma2) *fProbOfBreakup[j+1]*b;
200 f2 = getTransformedNofe(egamma1, b) * getTransformedNofe(egamma2, b)
201 * getSigma(egamma1) * getSigma(egamma2) *_probOfBreakup[k+1]*b;
202 bint = bint + 0.5*(f1+f2)*(b-bold);
204 bint = 2.0*starlightConstants::pi*bint;
205 _fnDouble[i][j] = bint * (_eGamma[i+1] - _eGamma[i]) * (_eGamma[j+1] - _eGamma[j]);
206 egamma2 = egamma2 * eg_inc;
208 egamma1 = egamma1 * eg_inc;
211 for (int i = 0; i < _nK; i++)
213 _fnDoubleInt[i] = 0.0;
214 for (int j = 0; j < _nK; j++)
216 _fnDoubleInt[i] = _fnDoubleInt[i] + _fnDouble[i][j];
220 _fnDoubleIntCumulative[0] = 0.0;
221 for (int i = 1; i < _nK+1; i++)
223 _fnDoubleIntCumulative[i] = _fnDoubleIntCumulative[i-1] + _fnDoubleInt[i-1];
226 fnorm = 1.0/_fnDoubleIntCumulative[_nK];
227 for (int i = 0; i < _nK+1; i++)
229 _fnDoubleIntCumulative[i] = fnorm * _fnDoubleIntCumulative[i];
235 double spectrum::drawKsingle()
241 xtest = randyInstance.Rndom();
242 while (xtest > _fnSingleCumulative[itest])
248 if (itest >= _nK || itest < 0)
250 std::cerr << "ERROR: itest: " << itest << std::endl;
254 double delta_f = xtest - _fnSingleCumulative[itest];
257 std::cout << "WARNING: delta_f: " << delta_f << std::endl;
260 double dE = _eGamma[itest+1] - _eGamma[itest];
261 double dF = _fnSingleCumulative[itest+1] - _fnSingleCumulative[itest];
263 double delta_e = delta_f*dE/dF;
265 if (delta_e > (_eGamma[itest+1] - _eGamma[itest]))
267 std::cerr << "ERROR: delta_E: " << delta_e << std::endl;
270 egamma = _eGamma[itest] + delta_e;
274 void spectrum::drawKdouble(float& egamma1, float& egamma2)
281 xtest1 = randyInstance.Rndom();
283 while (xtest1 > _fnDoubleIntCumulative[itest1])
289 if (itest1 >= _nK || itest1 < 0)
291 std::cerr << "ERROR: itest1: " << itest1 << std::endl;
293 double delta_f = xtest1 - _fnDoubleIntCumulative[itest1];
297 std::cout << "WARNING: delta_f: " << delta_f << std::endl;
300 double dE = _eGamma[itest1+1] - _eGamma[itest1];
301 double dF = _fnDoubleIntCumulative[itest1+1] - _fnDoubleIntCumulative[itest1];
303 double delta_e = delta_f*dE/dF;
305 if (delta_e > (_eGamma[itest1+1] - _eGamma[itest1]))
307 std::cerr << "ERROR: delta_E: " << delta_e << std::endl;
310 egamma1 = _eGamma[itest1] + delta_e;
314 // double reldw = delta_e/(fEGamma[itest1+1] - fEGamma[itest1]);
315 // std::vector<double> fn_second(fNK);
316 std::vector<double> fn_second_cumulative(_nK+1);
318 // for(int i = 0; i < fNK; i++)
320 // fn_second[i] = fFnDouble[itest1][i] + (fFnDouble[itest1+1][i] - fFnDouble[itest1][i])*reldw;
323 fn_second_cumulative[0] = 0.0;
324 for(int i = 1; i < _nK+1; i++)
326 // fn_second_cumulative[i] = fn_second_cumulative[i-1] + fn_second[i-1]; //TODO:check indexing
327 fn_second_cumulative[i] = fn_second_cumulative[i-1] + _fnDouble[itest1][i-1];
330 double norm_factor = 1.0/fn_second_cumulative[_nK];
331 for(int i = 0; i < _nK+1; i++)
333 fn_second_cumulative[i] = norm_factor*fn_second_cumulative[i];
336 xtest2 = randyInstance.Rndom();
338 while (xtest2 > fn_second_cumulative[itest2])
344 if (itest2 >= _nK || itest2 < 0)
346 std::cerr << "ERROR: itest2: " << itest2 << std::endl;
348 delta_f = xtest2 - fn_second_cumulative[itest2];
352 std::cout << "WARNING: delta_f: " << delta_f << std::endl;
355 dE = _eGamma[itest2+1] - _eGamma[itest2];
356 dF = fn_second_cumulative[itest2+1] - fn_second_cumulative[itest2];
358 delta_e = delta_f*dE/dF;
360 if (delta_e > (_eGamma[itest2+1] - _eGamma[itest2]))
362 std::cerr << "ERROR: delta_E: " << delta_e << std::endl;
365 egamma2 = _eGamma[itest2] + delta_e;
371 bool spectrum::generateBreakupProbabilities()
374 int nbbins = _nBbins;
376 double b_min = _bMin;
377 //double b_max = _bMax;
379 // double binc = (log(b_max/b_min))/(double)nbbins;
380 double binc = exp((log(_bMax/_bMin))/(double)_nBbins);
384 _probOfBreakup.resize(nbbins);
386 for (int i = 0; i < nbbins; i++)
390 rhad = _beamBeamSystem->probabilityOfBreakup(bimp);
391 _probOfBreakup[i] = exp(-rhad);
397 double spectrum::getFnSingle(double egamma) const
399 double eginc = exp(log(_eGammaMax/_eGammaMin)/static_cast<double>(_nK));
400 int i1 = log(egamma/_eGammaMin)/log(eginc);
402 double fnSingle = 0.0;
404 if (i1 < 0 || i2 > _nK)
406 std::cout << "I1, I2 out of bounds. Egamma = " << egamma << std::endl;
407 std::cout << "I1, I2 = " << i1 << ", " << i2 << std::endl;
412 double dE = _eGamma[i2] - _eGamma[i1];
413 double eFrac = (egamma - _eGamma[i1])/dE;
415 if (eFrac < 0.0 || eFrac > 1.0)
417 std::cout << "WARNING: Efrac = " << eFrac << std::endl;
419 fnSingle = (1.0 - eFrac)*_fnSingle[i1] + eFrac*_fnSingle[i2];
424 double spectrum::getFnDouble(double egamma1, double egamma2) const
426 double eginc = exp(log(_eGammaMax/_eGammaMin)/static_cast<double>(_nK));
427 int i1 = log(egamma1/_eGammaMin)/log(eginc);
429 double fnDouble = 0.0;
431 if (i1 < 0 || i2 > _nK)
433 std::cout << "I1, I2 out of bounds. Egamma1 = " << egamma1 << std::endl;
434 std::cout << "I1, I2 = " << i1 << ", " << i2 << std::endl;
439 int j1 = log(egamma2/_eGammaMin)/log(eginc);
442 if (j1 < 0 || j2 > _nK)
444 std::cout << "J1, J2 out of bounds. Egamma2 = " << egamma2 << std::endl;
445 std::cout << "J1, J2 = " << j1 << ", " << j2 << std::endl;
450 double dE1 = _eGamma[i2] - _eGamma[i1];
451 double eFrac1 = (egamma1 - _eGamma[i1])/dE1;
453 if (eFrac1 < 0.0 || eFrac1 > 1.0)
455 std::cout << "WARNING: Efrac1 = " << eFrac1 << std::endl;
458 double ptemp1 = (1.0 - eFrac1)*_fnDouble[i1][j1] + eFrac1*_fnDouble[i2][j1];
459 double ptemp2 = (1.0 - eFrac1)*_fnDouble[i1][j2] + eFrac1*_fnDouble[i2][j2];
461 double dE2 = _eGamma[j2] - _eGamma[j1];
462 double eFrac2 = (egamma2 - _eGamma[j1])/dE2;
464 if (eFrac2 < 0.0 || eFrac2 > 1.0)
466 std::cout << "WARNING: Efrac2 = " << eFrac2 << std::endl;
469 fnDouble = (1.0 - eFrac2)*ptemp1 + eFrac2*ptemp2;
475 double spectrum::getTransformedNofe(double egamma, double b)
477 double factor = 1.0/(2.0*_beamBeamSystem->beamLorentzGamma());
478 double res = factor * _beamBeamSystem->beam1().photonFlux(b, egamma*factor);