1 | |
---|
2 | #include "particleBeam.h" |
---|
3 | #include "mathematicalConstants.h" |
---|
4 | #include "PhysicalConstants.h" |
---|
5 | #include "mathematicalTools.h" |
---|
6 | #include "mixedTools.h" |
---|
7 | |
---|
8 | #include <stdio.h> |
---|
9 | #include <algorithm> |
---|
10 | #include <sstream> |
---|
11 | |
---|
12 | using namespace std; |
---|
13 | |
---|
14 | particleBeam::particleBeam() { |
---|
15 | P0Transport_ = 0.0; |
---|
16 | particleRepresentationOk_ = false; |
---|
17 | momentRepresentationOk_ = false; |
---|
18 | } |
---|
19 | |
---|
20 | void particleBeam::clear() { |
---|
21 | relativePartic_.clear(); |
---|
22 | rij_.raz(); |
---|
23 | P0Transport_ = 0.0; |
---|
24 | particleRepresentationOk_ = false; |
---|
25 | momentRepresentationOk_ = false; |
---|
26 | } |
---|
27 | |
---|
28 | int particleBeam::getNbParticles() const { |
---|
29 | return relativePartic_.size(); |
---|
30 | } |
---|
31 | |
---|
32 | const beam2Moments& particleBeam::getTransportMoments() const { |
---|
33 | return rij_; |
---|
34 | } |
---|
35 | |
---|
36 | double particleBeam::getSigmaTransportij(unsigned indexI, unsigned indexJ) { |
---|
37 | if ( indexI > 5 || indexJ > 5 ) { |
---|
38 | cerr << " particleBeam::getSigmaTransportij() indices out of range " << endl; |
---|
39 | return 0.0; |
---|
40 | } |
---|
41 | if ( !momentRepresentationOk_ ) { |
---|
42 | cerr << " particleBeam::getSigmaTransportij() beam is not in moment representation " << endl; |
---|
43 | return 0.0; |
---|
44 | } |
---|
45 | if ( indexI >= indexJ ) { |
---|
46 | return ( rij_.getMatrix().at(indexI) ).at(indexJ); |
---|
47 | } else { |
---|
48 | return ( rij_.getMatrix().at(indexJ) ).at(indexI); |
---|
49 | } |
---|
50 | |
---|
51 | } |
---|
52 | |
---|
53 | double particleBeam::getUnnormalizedEmittanceX() { |
---|
54 | double r = getSigmaTransportij(1,0); |
---|
55 | double rac = (1 - r*r); |
---|
56 | if ( rac <= 0.0 ) { |
---|
57 | return 0.0; |
---|
58 | } |
---|
59 | rac = sqrt(1 - r*r); |
---|
60 | return dimensionalFactorFromTransportToGraphics(0)*getSigmaTransportij(0,0) * getSigmaTransportij(1,1) * rac; // en pi.mm.mrad |
---|
61 | } |
---|
62 | |
---|
63 | double particleBeam::getUnnormalizedTranspPhaseSpaceArea(unsigned TranspIndexAbs, unsigned TranspIndexOrd) { |
---|
64 | if ( TranspIndexAbs == TranspIndexOrd ) return 0.0; |
---|
65 | double r = getSigmaTransportij(TranspIndexAbs,TranspIndexOrd); |
---|
66 | double rac = (1 - r*r); |
---|
67 | if ( rac <= 0.0 ) { |
---|
68 | return 0.0; |
---|
69 | } |
---|
70 | rac = sqrt(1 - r*r); |
---|
71 | return dimensionalFactorFromTransportToGraphics(TranspIndexAbs)*getSigmaTransportij(TranspIndexAbs,TranspIndexAbs) * |
---|
72 | dimensionalFactorFromTransportToGraphics(TranspIndexOrd)*getSigmaTransportij(TranspIndexOrd,TranspIndexOrd) * rac; // en pi.mm.mrad |
---|
73 | } |
---|
74 | |
---|
75 | |
---|
76 | |
---|
77 | double particleBeam::getP0Transport() const { |
---|
78 | return P0Transport_; |
---|
79 | } |
---|
80 | |
---|
81 | double particleBeam::referenceKineticEnergyMeV() const { |
---|
82 | if ( particleRepresentationOk_ ) { |
---|
83 | return (referenceParticle_.getGamma() -1.) * EREST_MeV; |
---|
84 | } else { |
---|
85 | double P0Norm = 1000.0 * P0Transport_ / EREST_MeV; |
---|
86 | double gamma = sqrt(1.0 + P0Norm * P0Norm); |
---|
87 | return (gamma - 1.0) * EREST_MeV; |
---|
88 | } |
---|
89 | } |
---|
90 | |
---|
91 | void particleBeam::set2Moments(beam2Moments& moments) { |
---|
92 | rij_ = moments; |
---|
93 | momentRepresentationOk_ = true; |
---|
94 | } |
---|
95 | |
---|
96 | void particleBeam::setWithParticles(vector<double>& centroid, bareParticle& referencePart, vector<bareParticle>& particles) { |
---|
97 | cout << " particleBeam::setWithParticles taille vect. part. " << particles.size() << endl; |
---|
98 | centroid_.clear(); |
---|
99 | centroid_ = centroid; |
---|
100 | referenceParticle_ = referencePart; |
---|
101 | relativePartic_.clear(); |
---|
102 | relativePartic_ = particles; |
---|
103 | cout << " particleBeam::setWithParticles taille vect. part. ENREGISTRE " << relativePartic_.size() << endl; |
---|
104 | particleRepresentationOk_ = true; |
---|
105 | } |
---|
106 | |
---|
107 | bool particleBeam::particleRepresentationOk() const { |
---|
108 | return particleRepresentationOk_; |
---|
109 | } |
---|
110 | bool particleBeam::momentRepresentationOk() const { |
---|
111 | return momentRepresentationOk_; |
---|
112 | } |
---|
113 | |
---|
114 | void particleBeam::addParticle( bareParticle p) |
---|
115 | { |
---|
116 | relativePartic_.push_back(p); |
---|
117 | } |
---|
118 | |
---|
119 | const vector<bareParticle>& particleBeam::getParticleVector() const |
---|
120 | { |
---|
121 | return relativePartic_; |
---|
122 | } |
---|
123 | |
---|
124 | vector<bareParticle>& particleBeam::getParticleVector() |
---|
125 | { |
---|
126 | return relativePartic_; |
---|
127 | } |
---|
128 | |
---|
129 | // void particleBeam::getVariance(double& varx, double& vary, double& varz) const { |
---|
130 | // unsigned int k; |
---|
131 | // double x,y,z; |
---|
132 | // double xav = 0.; |
---|
133 | // double yav = 0.; |
---|
134 | // double zav = 0.; |
---|
135 | // double xavsq = 0.; |
---|
136 | // double yavsq = 0.; |
---|
137 | // double zavsq = 0.; |
---|
138 | |
---|
139 | // TRIDVECTOR pos; |
---|
140 | |
---|
141 | |
---|
142 | // for ( k = 0 ; k < goodPartic_.size(); k++) { |
---|
143 | // pos = goodPartic_.at(k).getPosition(); |
---|
144 | // pos.getComponents(x,y,z); |
---|
145 | // // partic_[k].getXYZ(x,y,z); |
---|
146 | // xav += x; |
---|
147 | // xavsq += x*x; |
---|
148 | // yav += y; |
---|
149 | // yavsq += y*y; |
---|
150 | // zav += z; |
---|
151 | // zavsq += z*z; |
---|
152 | // } |
---|
153 | |
---|
154 | // double aginv = double (goodPartic_.size()); |
---|
155 | // aginv = 1.0/aginv; |
---|
156 | |
---|
157 | // varx = aginv * ( xavsq - xav*xav*aginv ); |
---|
158 | // vary = aginv * ( yavsq - yav*yav*aginv ); |
---|
159 | // varz = aginv * ( zavsq - zav*zav*aginv ); |
---|
160 | // } |
---|
161 | |
---|
162 | |
---|
163 | void particleBeam::printAllXYZ() const { |
---|
164 | cout << " dump du faisceau : " << endl; |
---|
165 | cout << relativePartic_.size() << " particules " << endl; |
---|
166 | unsigned int k; |
---|
167 | for ( k = 0 ; k < relativePartic_.size(); k++) |
---|
168 | { |
---|
169 | double xx,yy,zz; |
---|
170 | relativePartic_.at(k).getPosition().getComponents(xx,yy,zz); |
---|
171 | double betgamx, betgamy, betgamz; |
---|
172 | relativePartic_.at(k).getBetaGamma().getComponents(betgamx, betgamy, betgamz); |
---|
173 | cout << " part. numero " << k << " x= " << xx << " y= " << yy << " dphas (c.dt, cm) = " << zz << " betgamx= " << betgamx << " betgamy= " << betgamy << " betgamz= " << betgamz << endl; |
---|
174 | } |
---|
175 | } |
---|
176 | |
---|
177 | |
---|
178 | // extension en phase (cm) |
---|
179 | void particleBeam::ZrangeCdt(double& cdtmin, double& cdtmax) const { |
---|
180 | double z; |
---|
181 | cdtmin = GRAND; |
---|
182 | cdtmax = -cdtmin; |
---|
183 | |
---|
184 | unsigned int k; |
---|
185 | for ( k = 0 ; k < relativePartic_.size(); k++) |
---|
186 | { |
---|
187 | z = relativePartic_.at(k).getZ(); // ce z est un dephasage, c.dt, en cm |
---|
188 | if ( z < cdtmin ) cdtmin = z; |
---|
189 | else if ( z > cdtmax) cdtmax = z; |
---|
190 | } |
---|
191 | } |
---|
192 | |
---|
193 | |
---|
194 | |
---|
195 | string particleBeam::fileOutputFlow() const { |
---|
196 | ostringstream sortie; |
---|
197 | unsigned int k; |
---|
198 | for ( k = 0 ; k < relativePartic_.size(); k++) |
---|
199 | { |
---|
200 | sortie << relativePartic_.at(k).FileOutputFlow() << endl; |
---|
201 | } |
---|
202 | sortie << endl; |
---|
203 | return sortie.str(); |
---|
204 | } |
---|
205 | |
---|
206 | bool particleBeam::FileInput( ifstream& ifs) { |
---|
207 | bool test = true; |
---|
208 | string dum1, dum2; |
---|
209 | double dummy; |
---|
210 | if ( !( ifs >> dum1 >> dum2 >> dummy) ) return false; |
---|
211 | |
---|
212 | bareParticle pp; |
---|
213 | while ( pp.FileInput(ifs) ) |
---|
214 | { |
---|
215 | addParticle( pp); |
---|
216 | } |
---|
217 | return test; |
---|
218 | } |
---|
219 | |
---|
220 | void particleBeam::buildMomentRepresentation() { |
---|
221 | // le faisceau cense etre represente en un z donne (reference) sera |
---|
222 | // ici deploye spatialement (faisceau en un temps donne) |
---|
223 | unsigned k,j,m; |
---|
224 | double auxj, auxm; |
---|
225 | if ( !particleRepresentationOk_) |
---|
226 | { |
---|
227 | cerr << " particleBeam::buildMomentRepresentation() vecteur de particules invalide" << endl; |
---|
228 | return; |
---|
229 | } |
---|
230 | |
---|
231 | cout << " buildMomentRepresentation " << endl; |
---|
232 | // printAllXYZ(); |
---|
233 | |
---|
234 | double gref = referenceParticle_.getGamma() - 1.0; |
---|
235 | double P_reference_MeV_sur_c = sqrt( gref*(gref+2) ); |
---|
236 | |
---|
237 | cout << " gref = " << gref << " P_reference_MeV_sur_c = " << P_reference_MeV_sur_c << endl; |
---|
238 | |
---|
239 | |
---|
240 | // initialisation des moments |
---|
241 | razDesMoments(); |
---|
242 | |
---|
243 | // accumulation |
---|
244 | TRIDVECTOR pos; |
---|
245 | TRIDVECTOR begam; |
---|
246 | double gamma; |
---|
247 | double begamz; |
---|
248 | double g; |
---|
249 | double PMeVsc; |
---|
250 | double del; |
---|
251 | // double xp,yp,dz,cdt; |
---|
252 | vector<double> part(6, 0.0); |
---|
253 | |
---|
254 | vector< vector<double> >& matrice = rij_.getMatrix(); |
---|
255 | |
---|
256 | TRIDVECTOR positionDeployee; |
---|
257 | |
---|
258 | for (k=0; k < relativePartic_.size(); k++) { |
---|
259 | |
---|
260 | positionDeployee = coordonneesDeployees(k); |
---|
261 | gamma = relativePartic_.at(k).getGamma(); |
---|
262 | |
---|
263 | // pos = relativePartic_.at(k).getPosition(); |
---|
264 | // cdt = pos.getComponent(2); |
---|
265 | begam= relativePartic_.at(k).getBetaGamma(); |
---|
266 | begamz = begam.getComponent(2); |
---|
267 | g = gamma -1.0; |
---|
268 | PMeVsc = sqrt( g*(g+2) ); |
---|
269 | del = 100.0 * ( PMeVsc - P_reference_MeV_sur_c ) / P_reference_MeV_sur_c ; // en % |
---|
270 | |
---|
271 | // dz = begamz * cdt / gamma; |
---|
272 | // xp = begam.getComponent(0)/begamz; |
---|
273 | // yp = begam.getComponent(1)/begamz; |
---|
274 | |
---|
275 | part[0] = positionDeployee.getComponent(0); |
---|
276 | part[1] = begam.getComponent(0)/begamz; |
---|
277 | part[2] = positionDeployee.getComponent(1); |
---|
278 | part[3] = begam.getComponent(1)/begamz; |
---|
279 | part[4] = positionDeployee.getComponent(2); |
---|
280 | part[5] = del; |
---|
281 | |
---|
282 | for ( j = 0; j < 6; j++) { |
---|
283 | auxj = part.at(j) - centroid_.at(j); |
---|
284 | for (m=0; m <= j; m++) |
---|
285 | { |
---|
286 | auxm = part.at(m) - centroid_.at(m); |
---|
287 | |
---|
288 | ( matrice.at(j) ).at(m) += auxj*auxm; |
---|
289 | // ( rij_transportMoments_.at(j) ).at(m) += auxj*auxm; |
---|
290 | |
---|
291 | |
---|
292 | // cout << " j= " << j << " m= " << m << " rjm= " << ( rij_transportMoments_.at(j) ).at(m) << endl; |
---|
293 | } |
---|
294 | } |
---|
295 | } |
---|
296 | |
---|
297 | |
---|
298 | // moyenne |
---|
299 | double facmoy = 1.0/double( relativePartic_.size() ); |
---|
300 | for ( j = 0; j < 6; j++) { |
---|
301 | ( matrice.at(j) ).at(j) = sqrt(( matrice.at(j) ).at(j) * facmoy ); |
---|
302 | } |
---|
303 | |
---|
304 | for ( j = 0; j < 6; j++) { |
---|
305 | auxj = ( matrice.at(j) ).at(j); |
---|
306 | for (m=0; m < j; m++) { |
---|
307 | auxm = ( matrice.at(m) ).at(m); |
---|
308 | ( matrice.at(j) ).at(m) *= facmoy/(auxj * auxm); |
---|
309 | } |
---|
310 | } |
---|
311 | |
---|
312 | ////////////////// si C21 = 1 , transport plante ! a voir ////////// |
---|
313 | cout << " valeur initiale de C21: " << ( matrice.at(1) ).at(0) << endl; |
---|
314 | if ( ( matrice.at(1) ).at(0) >0.999999 ) { |
---|
315 | ( matrice.at(1) ).at(0) = 0.999999; |
---|
316 | cout << " j'ai fait la correction C21: " << ( matrice.at(1) ).at(0) << endl; |
---|
317 | } |
---|
318 | |
---|
319 | |
---|
320 | // les longueurs sont en cm |
---|
321 | // les angles en radians, on passe en mrad; |
---|
322 | |
---|
323 | double uniteAngle = 1.0e+3; |
---|
324 | ( matrice.at(1) ).at(1) *= uniteAngle; |
---|
325 | ( matrice.at(3) ).at(3) *= uniteAngle; |
---|
326 | P0Transport_ = 1.0e-3*EREST_MeV*P_reference_MeV_sur_c; |
---|
327 | |
---|
328 | // cout << " buildmomentrepresentation impression des moments " << endl; |
---|
329 | // impressionDesMoments(); |
---|
330 | |
---|
331 | momentRepresentationOk_ = true; |
---|
332 | } |
---|
333 | |
---|
334 | void particleBeam::impressionDesMoments() const { |
---|
335 | rij_.impression(); |
---|
336 | } |
---|
337 | |
---|
338 | void particleBeam::razDesMoments() { |
---|
339 | rij_.raz(); |
---|
340 | } |
---|
341 | |
---|
342 | |
---|
343 | // void particleBeam::readTransportMoments(ifstream& inp) { |
---|
344 | // rij_.readFromTransportOutput(inp); |
---|
345 | // } |
---|
346 | |
---|
347 | // void particleBeam::readTransportMoments(stringstream& inp) { |
---|
348 | // rij_.readFromTransportOutput(inp); |
---|
349 | // } |
---|
350 | |
---|
351 | double particleBeam::getXmaxRms() { |
---|
352 | if ( !momentRepresentationOk_ ) buildMomentRepresentation(); |
---|
353 | return ( rij_.getMatrix().at(0) ).at(0); |
---|
354 | // return ( rij_transportMoments_.at(0) ).at(0); |
---|
355 | } |
---|
356 | |
---|
357 | |
---|
358 | void particleBeam::particlesPhaseSpaceData(vector<double>& xcor, vector<double>& ycor, vector<string>& legende, string namex, string namey) { |
---|
359 | |
---|
360 | unsigned indexAbs, indexOrd; |
---|
361 | indexAbs = pspaCoorIndexFromString(namex); |
---|
362 | indexOrd = pspaCoorIndexFromString(namey); |
---|
363 | |
---|
364 | particlesPhaseSpaceComponent(xcor, indexAbs); |
---|
365 | particlesPhaseSpaceComponent(ycor, indexOrd); |
---|
366 | legende.clear(); |
---|
367 | legende.push_back( "phase space " + namex + "," + namey); |
---|
368 | legende.push_back( "particle number : " + mixedTools::intToString(getNbParticles())); |
---|
369 | } |
---|
370 | |
---|
371 | // renvoie (dans le vecteur coord) la liste des coordonnées d'index 'index' : |
---|
372 | // index = 0 , 1, 2 -> x,y,z (en coordonnees deployees) |
---|
373 | // index = 3,4 -> x' = betax/betaz , y' = betay/betaz |
---|
374 | // index = 5 -> Ec : energie cinetique |
---|
375 | void particleBeam::particlesPhaseSpaceComponent(vector<double>& coord, unsigned index) |
---|
376 | { |
---|
377 | if ( !particleRepresentationOk_ ) return; |
---|
378 | |
---|
379 | coord.clear(); |
---|
380 | coord.resize(relativePartic_.size(), 0.0 ); |
---|
381 | cout << " particleBeam::particlesPhaseSpaceComponent index = " << index << endl; |
---|
382 | |
---|
383 | if ( index <= 2 ) { |
---|
384 | |
---|
385 | for (unsigned i = 0; i < relativePartic_.size(); ++i) { |
---|
386 | |
---|
387 | coord.at(i) = 10.*coordonneesDeployees(i).getComponent(index); |
---|
388 | // coord.at(i) = 10.*relativePartic_.at(i).getPosition().getComponent(index); // en mm |
---|
389 | } |
---|
390 | return; |
---|
391 | } |
---|
392 | |
---|
393 | if ( index > 2 && index < 5 ) { |
---|
394 | cout << " particleBeam::particlesPhaseSpaceComponent traitement vitesses " << endl; |
---|
395 | for (unsigned i = 0; i < relativePartic_.size(); ++i) { |
---|
396 | double begamz = relativePartic_.at(i).getBetaGamma().getComponent(2); |
---|
397 | if ( begamz != 0.0) { |
---|
398 | coord.at(i) = 1000.*relativePartic_.at(i).getBetaGamma().getComponent(index - 3)/begamz; // milliradians |
---|
399 | } else { |
---|
400 | coord.at(i) = 0.0; |
---|
401 | } |
---|
402 | } |
---|
403 | cout << " particleBeam::particlesPhaseSpaceComponent traitement vitesses TERMINE " << endl; |
---|
404 | return; |
---|
405 | } |
---|
406 | |
---|
407 | if ( index == 5 ) { |
---|
408 | double gamma0 = referenceParticle_.getGamma(); |
---|
409 | if ( gamma0 == 0.0 ) return; |
---|
410 | for (unsigned i = 0; i < relativePartic_.size(); ++i) { |
---|
411 | coord.at(i) = 100.*(relativePartic_.at(i).getGamma() - gamma0)/gamma0; // en % |
---|
412 | } |
---|
413 | return; |
---|
414 | } |
---|
415 | } |
---|
416 | |
---|
417 | unsigned particleBeam::indexFromPspaToTransport(unsigned index) const { |
---|
418 | cout << " indexFromPspaToTransport entree : " << index << endl; |
---|
419 | switch ( index ) { |
---|
420 | case 0 : return 0; // x |
---|
421 | case 1 : return 2; // y |
---|
422 | case 2 : return 4; // z -> l |
---|
423 | case 3 : return 1; // xp |
---|
424 | case 4 : return 3; // yp |
---|
425 | case 5 : return 5; // de/E |
---|
426 | default : { |
---|
427 | cout << " particleBeam::indexFromPspaToTransport : coordinate index ERROR inital index : "<< index << endl; |
---|
428 | return 99; |
---|
429 | } |
---|
430 | } |
---|
431 | } |
---|
432 | |
---|
433 | unsigned particleBeam::pspaCoorIndexFromString(string nameCoor) const { |
---|
434 | if ( nameCoor == "x" ) { |
---|
435 | return 0; |
---|
436 | } else if ( nameCoor == "y" ) { |
---|
437 | return 1; |
---|
438 | } else if ( nameCoor == "dz" ) { |
---|
439 | return 2; |
---|
440 | } else if ( nameCoor == "xp" ) { |
---|
441 | return 3; |
---|
442 | } else if ( nameCoor == "yp" ) { |
---|
443 | return 4; |
---|
444 | } else if ( nameCoor == "dE/E" ) { |
---|
445 | return 5; |
---|
446 | } else { |
---|
447 | return 99; |
---|
448 | } |
---|
449 | } |
---|
450 | |
---|
451 | unsigned particleBeam::transportCoorIndexFromString(string nameCoor) const { |
---|
452 | if ( nameCoor == "x" ) { |
---|
453 | return 0; |
---|
454 | } else if ( nameCoor == "y" ) { |
---|
455 | return 2; |
---|
456 | } else if ( nameCoor == "dz" ) { |
---|
457 | return 4; |
---|
458 | } else if ( nameCoor == "xp" ) { |
---|
459 | return 1; |
---|
460 | } else if ( nameCoor == "yp" ) { |
---|
461 | return 3; |
---|
462 | } else if ( nameCoor == "dE/E" ) { |
---|
463 | return 5; |
---|
464 | } else { |
---|
465 | return 99; |
---|
466 | } |
---|
467 | } |
---|
468 | |
---|
469 | //void particleBeam::donneesDessinEllipse(vector<double>& xcor, vector<double>& ycor, vector<string>& legende, unsigned indexAbs, unsigned indexOrd) { |
---|
470 | |
---|
471 | void particleBeam::donneesDessinEllipse(vector<double>& xcor, vector<double>& ycor, vector<string>& legende, string namex, string namey) { |
---|
472 | int k; |
---|
473 | double x,y; |
---|
474 | if ( !momentRepresentationOk_ ) buildMomentRepresentation(); |
---|
475 | |
---|
476 | // if ( !momentRepresentationOk_ ) return; |
---|
477 | |
---|
478 | unsigned indexAbs, indexOrd; |
---|
479 | |
---|
480 | |
---|
481 | // index TRANSPORT |
---|
482 | indexAbs = transportCoorIndexFromString(namex); |
---|
483 | indexOrd = transportCoorIndexFromString(namey); |
---|
484 | cout << " namex= " << namex << " indexAbs= " << indexAbs << " namey= " << namey << " indexOrd= " << indexOrd << endl; |
---|
485 | if ( indexAbs > 5 || indexOrd > 5 ) return; |
---|
486 | |
---|
487 | xcor.clear(); |
---|
488 | ycor.clear(); |
---|
489 | |
---|
490 | |
---|
491 | |
---|
492 | double dimensionalFactorX, dimensionalFactorY; // to mm, if necessary |
---|
493 | dimensionalFactorX = dimensionalFactorFromTransportToGraphics(indexAbs); |
---|
494 | dimensionalFactorY = dimensionalFactorFromTransportToGraphics(indexOrd); |
---|
495 | |
---|
496 | |
---|
497 | |
---|
498 | legende.clear(); |
---|
499 | // if ( indexAbs == 0 && indexOrd == 1 ) { |
---|
500 | string namx = transportVariableName(indexAbs); |
---|
501 | string namy = transportVariableName(indexOrd); |
---|
502 | double em = getUnnormalizedTranspPhaseSpaceArea(indexAbs,indexOrd) ; |
---|
503 | string emitt = mixedTools::doubleToString(getUnnormalizedTranspPhaseSpaceArea(indexAbs,indexOrd)); |
---|
504 | string xmax = namx + "max= "; |
---|
505 | string valXmax = mixedTools::doubleToString(dimensionalFactorX*getSigmaTransportij(indexAbs,indexAbs)); |
---|
506 | string ymax = namy + "max= "; |
---|
507 | string valYmax = mixedTools::doubleToString(dimensionalFactorY*getSigmaTransportij(indexOrd,indexOrd)); // mm |
---|
508 | string correl = " correlation "; |
---|
509 | string valCorrel = mixedTools::doubleToString(getSigmaTransportij(1,0)); |
---|
510 | string xunit = graphicTransportUnitName(indexAbs); |
---|
511 | string yunit = graphicTransportUnitName(indexOrd); |
---|
512 | legende.push_back( "emittance" + namx + "," + namy + ": " + emitt + " pi." + xunit + "." + yunit); |
---|
513 | legende.push_back( xmax + valXmax + xunit); |
---|
514 | legende.push_back( ymax + valYmax + yunit); |
---|
515 | // } else { |
---|
516 | // legende.push_back(" text of legend not yet programmed "); |
---|
517 | // } |
---|
518 | |
---|
519 | cout << " index x" << indexAbs << " index y " << indexOrd << endl; |
---|
520 | double xm = dimensionalFactorX*( rij_.getMatrix().at(indexAbs) ).at(indexAbs); |
---|
521 | double ym = dimensionalFactorY*( rij_.getMatrix().at(indexOrd) ).at(indexOrd); |
---|
522 | double r; |
---|
523 | if ( indexOrd > indexAbs ) { |
---|
524 | r = ( rij_.getMatrix().at(indexOrd) ).at(indexAbs); |
---|
525 | } else { |
---|
526 | r = ( rij_.getMatrix().at(indexAbs) ).at(indexOrd); |
---|
527 | } |
---|
528 | |
---|
529 | cout << " racs11= " << xm << " racs22= " << ym << " r12= " << r << endl; |
---|
530 | |
---|
531 | |
---|
532 | int nbintv = 50; |
---|
533 | if ( xm == 0.0 ) return; |
---|
534 | double pas = 2.0 * xm / nbintv; |
---|
535 | |
---|
536 | // cout << " r= " << r << endl; |
---|
537 | double rac = (1 - r*r); |
---|
538 | if ( rac > 0.0 ) |
---|
539 | { |
---|
540 | cout << " cas rac > " << endl; |
---|
541 | rac = sqrt(1 - r*r); |
---|
542 | double alpha = -r / rac; |
---|
543 | double beta = xm / ( ym * rac); |
---|
544 | // double gamma = ym / ( xm * rac ); |
---|
545 | double epsil = xm * ym * rac; |
---|
546 | double fac1 = -1.0 / ( beta * beta); |
---|
547 | double fac2 = epsil/beta; |
---|
548 | double fac3 = -alpha/beta; |
---|
549 | double aux; |
---|
550 | for ( k=0; k < nbintv; k++) |
---|
551 | { |
---|
552 | x = -xm + k*pas; |
---|
553 | aux = fac1 * x * x + fac2; |
---|
554 | // cout << " aux2= " << aux << endl; |
---|
555 | if ( aux <= 0.0 ) |
---|
556 | { |
---|
557 | aux = 0.0; |
---|
558 | } |
---|
559 | else aux = sqrt(aux); |
---|
560 | |
---|
561 | // y = fac3*x; |
---|
562 | y = fac3*x + aux; |
---|
563 | xcor.push_back(x); |
---|
564 | ycor.push_back(y); |
---|
565 | } |
---|
566 | |
---|
567 | for ( k=0; k <= nbintv; k++) |
---|
568 | { |
---|
569 | x = xm - k*pas; |
---|
570 | aux = fac1 * x * x + fac2; |
---|
571 | if ( aux <= 0.0 ) |
---|
572 | { |
---|
573 | aux = 0.0; |
---|
574 | } |
---|
575 | else aux = sqrt(aux); |
---|
576 | // y = fac3*x; |
---|
577 | y = fac3*x - aux; |
---|
578 | xcor.push_back(x); |
---|
579 | ycor.push_back(y); |
---|
580 | } |
---|
581 | } |
---|
582 | else |
---|
583 | // cas degenere |
---|
584 | { |
---|
585 | cout << " cas degenere " << endl; |
---|
586 | double fac = ym/xm; |
---|
587 | for ( k=0; k < nbintv; k++) |
---|
588 | { |
---|
589 | x = -xm + k*pas; |
---|
590 | y = fac*x; |
---|
591 | xcor.push_back(x); |
---|
592 | ycor.push_back(y); |
---|
593 | } |
---|
594 | |
---|
595 | } |
---|
596 | } |
---|
597 | |
---|
598 | void particleBeam::histogramme(unsigned int iabs,vector<double>& xcor,vector<int>& hist, vector<string>& legende) |
---|
599 | { |
---|
600 | double out[3]; |
---|
601 | // out[0]= nbre de particules, out[1]= moyenne, out[2]= ecart-type |
---|
602 | vector<double> vshf; |
---|
603 | particlesPhaseSpaceComponent(vshf,iabs); |
---|
604 | histogramInitialize(iabs,vshf,out); |
---|
605 | histogramPacking(out[2],vshf,xcor,hist); |
---|
606 | |
---|
607 | if(iabs == 5) { |
---|
608 | out[1] *= EREST_MeV; // moyenne en MeV |
---|
609 | out[2] *= EREST_keV; // ecart-type en KeV |
---|
610 | } |
---|
611 | // legendes |
---|
612 | string unites[2]; |
---|
613 | if(iabs == 0 || iabs == 1 || iabs == 2) { |
---|
614 | unites[0]= unites[1]= " mm"; |
---|
615 | } |
---|
616 | if(iabs == 3 || iabs == 4) { |
---|
617 | unites[0]= unites[1]= " mrad"; |
---|
618 | } |
---|
619 | if(iabs == 5) { |
---|
620 | unites[0]= " MeV"; unites[1]= " KeV"; |
---|
621 | } |
---|
622 | |
---|
623 | legende.clear(); |
---|
624 | legende.push_back(" entries : "+ mixedTools::intToString((int)out[0]) ); |
---|
625 | legende.push_back(" mean : "+ mixedTools::doubleToString(out[1])+unites[0]); |
---|
626 | legende.push_back(" sigma rms : "+ mixedTools::doubleToString(out[2])+unites[1]); |
---|
627 | } |
---|
628 | |
---|
629 | void particleBeam::histogramInitialize(unsigned int index,vector<double>& vshf,double out[3]) |
---|
630 | { |
---|
631 | double vmin= GRAND; |
---|
632 | double vmax= -vmin; |
---|
633 | double vmoy= 0.0; |
---|
634 | double ecatyp= 0.0; |
---|
635 | |
---|
636 | for (unsigned int k = 0; k < relativePartic_.size(); k++) { |
---|
637 | if (vshf[k] < vmin) vmin = vshf[k]; |
---|
638 | else if (vshf[k] > vmax) vmax = vshf[k]; |
---|
639 | vmoy += vshf[k]; |
---|
640 | ecatyp += vshf[k]*vshf[k]; |
---|
641 | } |
---|
642 | |
---|
643 | double sum= (float)relativePartic_.size(); |
---|
644 | out[0]= sum; |
---|
645 | vmoy /= sum; |
---|
646 | out[1]= vmoy; |
---|
647 | ecatyp /= sum; |
---|
648 | ecatyp = sqrt(abs(ecatyp-vmoy*vmoy)); |
---|
649 | out[2]= ecatyp; |
---|
650 | |
---|
651 | if(index == 0) { |
---|
652 | cout << "position x -moyenne " << vmoy << " cm " << "-mini " << vmin << " cm " << "-maxi " << vmax << " cm " << "ecart type " << ecatyp << " cm" << endl; |
---|
653 | } |
---|
654 | if(index == 1) { |
---|
655 | cout << "position y -moyenne " << vmoy << " cm " << "-mini " << vmin << " cm " << "-maxi " << vmax << " cm " << "ecart type " << ecatyp << " cm" << endl; |
---|
656 | } |
---|
657 | if(index == 2) { |
---|
658 | cout << "position z -moyenne " << vmoy << " cm " << "-mini " << vmin << " cm " << "-maxi " << vmax << " cm " << "ecart type " << ecatyp << " cm" << endl; |
---|
659 | } |
---|
660 | if(index == 3) { |
---|
661 | cout << "divergence xp -moyenne " << vmoy << " mrad " << "-mini " << vmin << " mrad " << "-maxi " << vmax << " mrad " << "ecart type " << ecatyp << " mrad" << endl; |
---|
662 | } |
---|
663 | if(index == 4) { |
---|
664 | cout << "divergence yp -moyenne " << vmoy << " mrad " << "-mini " << vmin << " mrad " << "-maxi " << vmax << " mrad " << "ecart type " << ecatyp << " mrad" << endl; |
---|
665 | } |
---|
666 | if(index == 5) { |
---|
667 | double gmin = vmin-1.0; |
---|
668 | double gmax = vmax-1.0; |
---|
669 | cout << "energie cinetique -moyenne " << vmoy*EREST_MeV << " Mev " << "-mini " << gmin*EREST_MeV << " Mev " << "-maxi " << gmax*EREST_MeV << " Mev " << "ecart type " << ecatyp*EREST_MeV << " Kev" << endl; |
---|
670 | } |
---|
671 | |
---|
672 | for (unsigned int k = 0; k < relativePartic_.size(); k++) { |
---|
673 | vshf[k] -= vmoy; |
---|
674 | } |
---|
675 | } |
---|
676 | |
---|
677 | void particleBeam::histogramPacking(double ecatyp,vector<double>vshf,vector<double>&xcor,vector<int>& hist) |
---|
678 | { |
---|
679 | // demi fenetre hfene= max(5.*ecatyp-vmoy,vmoy-5.*ecatyp); |
---|
680 | double hfene= 5.*ecatyp; |
---|
681 | // et pas de l'histogramme |
---|
682 | double hpas = hfene/25.; |
---|
683 | |
---|
684 | cout << "demi fenetre " << hfene << ", hpas= " << hpas << endl; |
---|
685 | |
---|
686 | double vmin = -hfene; |
---|
687 | double dfen = 2.*hfene; |
---|
688 | int ihist = dfen/hpas; |
---|
689 | double phist = ihist*hpas; |
---|
690 | double dpas = hpas-(dfen-phist); |
---|
691 | if(dpas <= hpas*1.e-03) { |
---|
692 | ihist++; |
---|
693 | phist= ihist*hpas; |
---|
694 | } |
---|
695 | double vmax= vmin+hpas*ihist; |
---|
696 | |
---|
697 | cout << "xAxisNumberOfBins= " << ihist <<", xAxisMinimum= " << vmin << ", xAxisMaximum= " << vmax << ", NParticules= " << vshf.size() << ", phist " << phist << endl; |
---|
698 | |
---|
699 | if(!xcor.empty()) xcor.clear(); |
---|
700 | xcor.resize(ihist+1); |
---|
701 | for (int i = 0; i <= ihist; ++i) { |
---|
702 | xcor[i] = vmin+i*hpas; |
---|
703 | } |
---|
704 | |
---|
705 | ///////////////////////////////////// |
---|
706 | |
---|
707 | if(!hist.empty()) hist.clear(); |
---|
708 | hist.resize(ihist,0); |
---|
709 | for (unsigned int i = 0; i < vshf.size(); ++i) { |
---|
710 | double var= vshf[i]-vmin; |
---|
711 | if(var < 0 ) { |
---|
712 | cout<<"lesser that the minimum "<<var<<", ("<< i<<")"<< endl; |
---|
713 | hist.at(0)++; |
---|
714 | } else if(var >= phist) { |
---|
715 | cout<<"greater that the maximum "<<var<<", ("<< i<<")"<< endl; |
---|
716 | hist.at(ihist-1)++; |
---|
717 | } else { |
---|
718 | int kk= (int)floor(var/hpas); |
---|
719 | hist.at(kk)++; |
---|
720 | } |
---|
721 | } |
---|
722 | } |
---|
723 | |
---|
724 | // coordonnees d'une particule dans le faisceau deploye ( passage |
---|
725 | // d'une representation z=cte a une representation t=cte) |
---|
726 | TRIDVECTOR particleBeam::coordonneesDeployees(unsigned particleIndex, double cdtShift) { |
---|
727 | double gamma = relativePartic_.at(particleIndex).getGamma(); |
---|
728 | // TRIDVECTOR pos = relativePartic_.at(particleIndex).getPosition(); |
---|
729 | double xx = relativePartic_.at(particleIndex).getReferenceToPosition().getComponent(0); |
---|
730 | double yy = relativePartic_.at(particleIndex).getReferenceToPosition().getComponent(1); |
---|
731 | double cdt = relativePartic_.at(particleIndex).getReferenceToPosition().getComponent(2); |
---|
732 | cdt += cdtShift; |
---|
733 | // TRIDVECTOR begam= goodPartic_.at(particleIndex).getBetaGamma(); |
---|
734 | double begamz = relativePartic_.at(particleIndex).getReferenceToBetaGamma().getComponent(2); |
---|
735 | double dz = begamz * cdt / gamma; |
---|
736 | double xp = relativePartic_.at(particleIndex).getReferenceToBetaGamma().getComponent(0)/begamz; |
---|
737 | double yp = relativePartic_.at(particleIndex).getReferenceToBetaGamma().getComponent(1)/begamz; |
---|
738 | xx += xp * dz; |
---|
739 | yy += yp * dz; |
---|
740 | return TRIDVECTOR(xx, yy, dz); |
---|
741 | } |
---|