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 | goodPartic_.clear(); |
---|
22 | rij_.raz(); |
---|
23 | P0Transport_ = 0.0; |
---|
24 | particleRepresentationOk_ = false; |
---|
25 | momentRepresentationOk_ = false; |
---|
26 | } |
---|
27 | |
---|
28 | int particleBeam::getNbParticles() const { |
---|
29 | return goodPartic_.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 | goodPartic_.clear(); |
---|
102 | goodPartic_ = particles; |
---|
103 | cout << " particleBeam::setWithParticles taille vect. part. ENREGISTRE " << goodPartic_.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 | goodPartic_.push_back(p); |
---|
117 | } |
---|
118 | |
---|
119 | const vector<bareParticle>& particleBeam::getParticleVector() const |
---|
120 | { |
---|
121 | return goodPartic_; |
---|
122 | } |
---|
123 | |
---|
124 | vector<bareParticle>& particleBeam::getParticleVector() |
---|
125 | { |
---|
126 | return goodPartic_; |
---|
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 << goodPartic_.size() << " particules " << endl; |
---|
166 | unsigned int k; |
---|
167 | for ( k = 0 ; k < goodPartic_.size(); k++) |
---|
168 | { |
---|
169 | double xx,yy,zz; |
---|
170 | goodPartic_.at(k).getPosition().getComponents(xx,yy,zz); |
---|
171 | double betgamx, betgamy, betgamz; |
---|
172 | goodPartic_.at(k).getBetaGamma().getComponents(betgamx, betgamy, betgamz); |
---|
173 | cout << " part. numero " << k << " x= " << xx << " y= " << yy << " z= " << zz << " betgamx= " << betgamx << " betgamy= " << betgamy << " betgamz= " << betgamz << endl; |
---|
174 | } |
---|
175 | } |
---|
176 | |
---|
177 | |
---|
178 | |
---|
179 | void particleBeam::Zrange(double& zmin, double& zmax) const { |
---|
180 | double z; |
---|
181 | zmin = GRAND; |
---|
182 | zmax = -zmin; |
---|
183 | |
---|
184 | unsigned int k; |
---|
185 | for ( k = 0 ; k < goodPartic_.size(); k++) |
---|
186 | { |
---|
187 | z = goodPartic_.at(k).getZ(); |
---|
188 | if ( z < zmin ) zmin = z; |
---|
189 | else if ( z > zmax) zmax = z; |
---|
190 | } |
---|
191 | } |
---|
192 | |
---|
193 | |
---|
194 | |
---|
195 | string particleBeam::FileOutputFlow() const { |
---|
196 | ostringstream sortie; |
---|
197 | unsigned int k; |
---|
198 | for ( k = 0 ; k < goodPartic_.size(); k++) |
---|
199 | { |
---|
200 | sortie << goodPartic_.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 | |
---|
222 | unsigned k,j,m; |
---|
223 | double auxj, auxm; |
---|
224 | if ( !particleRepresentationOk_) |
---|
225 | { |
---|
226 | cerr << " particleBeam::buildMomentRepresentation() vecteur de particules invalide" << endl; |
---|
227 | return; |
---|
228 | } |
---|
229 | |
---|
230 | cout << " buildMomentRepresentation " << endl; |
---|
231 | // printAllXYZ(); |
---|
232 | |
---|
233 | double gref = referenceParticle_.getGamma() - 1.0; |
---|
234 | double P_reference_MeV_sur_c = sqrt( gref*(gref+2) ); |
---|
235 | |
---|
236 | cout << " gref = " << gref << " P_reference_MeV_sur_c = " << P_reference_MeV_sur_c << endl; |
---|
237 | |
---|
238 | |
---|
239 | // initialisation des moments |
---|
240 | razDesMoments(); |
---|
241 | |
---|
242 | // accumulation |
---|
243 | TRIDVECTOR pos; |
---|
244 | TRIDVECTOR begam; |
---|
245 | double gamma; |
---|
246 | double begamz; |
---|
247 | double g; |
---|
248 | double PMeVsc; |
---|
249 | double del; |
---|
250 | vector<double> part(6, 0.0); |
---|
251 | |
---|
252 | vector< vector<double> >& matrice = rij_.getMatrix(); |
---|
253 | |
---|
254 | |
---|
255 | for (k=0; k < goodPartic_.size(); k++) { |
---|
256 | gamma = goodPartic_.at(k).getGamma(); |
---|
257 | pos = goodPartic_.at(k).getPosition(); |
---|
258 | begam= goodPartic_.at(k).getBetaGamma(); |
---|
259 | begamz = begam.getComponent(2); |
---|
260 | g = gamma -1.0; |
---|
261 | PMeVsc = sqrt( g*(g+2) ); |
---|
262 | del = 100.0 * ( PMeVsc - P_reference_MeV_sur_c ) / P_reference_MeV_sur_c ; // en % |
---|
263 | |
---|
264 | part[0] = pos.getComponent(0); |
---|
265 | part[1] = begam.getComponent(0)/begamz; |
---|
266 | part[2] = pos.getComponent(1); |
---|
267 | part[3] = begam.getComponent(1)/begamz; |
---|
268 | part[4] = pos.getComponent(2); |
---|
269 | part[5] = del; |
---|
270 | |
---|
271 | for ( j = 0; j < 6; j++) { |
---|
272 | auxj = part.at(j) - centroid_.at(j); |
---|
273 | for (m=0; m <= j; m++) |
---|
274 | { |
---|
275 | auxm = part.at(m) - centroid_.at(m); |
---|
276 | |
---|
277 | ( matrice.at(j) ).at(m) += auxj*auxm; |
---|
278 | // ( rij_transportMoments_.at(j) ).at(m) += auxj*auxm; |
---|
279 | |
---|
280 | |
---|
281 | // cout << " j= " << j << " m= " << m << " rjm= " << ( rij_transportMoments_.at(j) ).at(m) << endl; |
---|
282 | } |
---|
283 | } |
---|
284 | } |
---|
285 | |
---|
286 | |
---|
287 | // moyenne |
---|
288 | double facmoy = 1.0/double( goodPartic_.size() ); |
---|
289 | for ( j = 0; j < 6; j++) { |
---|
290 | ( matrice.at(j) ).at(j) = sqrt(( matrice.at(j) ).at(j) * facmoy ); |
---|
291 | } |
---|
292 | |
---|
293 | for ( j = 0; j < 6; j++) { |
---|
294 | auxj = ( matrice.at(j) ).at(j); |
---|
295 | for (m=0; m < j; m++) { |
---|
296 | auxm = ( matrice.at(m) ).at(m); |
---|
297 | ( matrice.at(j) ).at(m) *= facmoy/(auxj * auxm); |
---|
298 | } |
---|
299 | } |
---|
300 | |
---|
301 | ////////////////// si C21 = 1 , transport plante ! a voir ////////// |
---|
302 | cout << " valeur initiale de C21: " << ( matrice.at(1) ).at(0) << endl; |
---|
303 | if ( ( matrice.at(1) ).at(0) >0.999999 ) { |
---|
304 | ( matrice.at(1) ).at(0) = 0.999999; |
---|
305 | cout << " j'ai fait la correction C21: " << ( matrice.at(1) ).at(0) << endl; |
---|
306 | } |
---|
307 | |
---|
308 | |
---|
309 | // les longueurs sont en cm |
---|
310 | // les angles en radians, on passe en mrad; |
---|
311 | |
---|
312 | double uniteAngle = 1.0e+3; |
---|
313 | ( matrice.at(1) ).at(1) *= uniteAngle; |
---|
314 | ( matrice.at(3) ).at(3) *= uniteAngle; |
---|
315 | P0Transport_ = 1.0e-3*EREST_MeV*P_reference_MeV_sur_c; |
---|
316 | |
---|
317 | // cout << " buildmomentrepresentation impression des moments " << endl; |
---|
318 | // impressionDesMoments(); |
---|
319 | |
---|
320 | momentRepresentationOk_ = true; |
---|
321 | } |
---|
322 | |
---|
323 | void particleBeam::impressionDesMoments() const { |
---|
324 | rij_.impression(); |
---|
325 | } |
---|
326 | |
---|
327 | void particleBeam::razDesMoments() { |
---|
328 | rij_.raz(); |
---|
329 | } |
---|
330 | |
---|
331 | |
---|
332 | // void particleBeam::readTransportMoments(ifstream& inp) { |
---|
333 | // rij_.readFromTransportOutput(inp); |
---|
334 | // } |
---|
335 | |
---|
336 | // void particleBeam::readTransportMoments(stringstream& inp) { |
---|
337 | // rij_.readFromTransportOutput(inp); |
---|
338 | // } |
---|
339 | |
---|
340 | double particleBeam::getXmaxRms() { |
---|
341 | if ( !momentRepresentationOk_ ) buildMomentRepresentation(); |
---|
342 | return ( rij_.getMatrix().at(0) ).at(0); |
---|
343 | // return ( rij_transportMoments_.at(0) ).at(0); |
---|
344 | } |
---|
345 | |
---|
346 | |
---|
347 | void particleBeam::particlesPhaseSpaceData(vector<double>& xcor, vector<double>& ycor, unsigned indexAbs, unsigned indexOrd) { |
---|
348 | particlesPhaseSpaceComponent(xcor, indexAbs); |
---|
349 | particlesPhaseSpaceComponent(ycor, indexOrd); |
---|
350 | } |
---|
351 | |
---|
352 | void particleBeam::particlesPhaseSpaceComponent(vector<double>& coord, unsigned index) { |
---|
353 | if ( !particleRepresentationOk_ ) return; |
---|
354 | coord.clear(); |
---|
355 | coord.resize(goodPartic_.size(), 0.0 ); |
---|
356 | cout << " particleBeam::particlesPhaseSpaceComponent index = " << index << endl; |
---|
357 | if ( index <= 2 ) { |
---|
358 | for (unsigned i = 0; i < goodPartic_.size(); ++i) { |
---|
359 | coord.at(i) = goodPartic_.at(i).getPosition().getComponent(index); |
---|
360 | } |
---|
361 | return; |
---|
362 | } |
---|
363 | |
---|
364 | if ( index > 2 && index < 5 ) { |
---|
365 | for (unsigned i = 0; i < goodPartic_.size(); ++i) { |
---|
366 | double begamz = goodPartic_.at(i).getBetaGamma().getComponent(2); |
---|
367 | if ( begamz != 0.0) { |
---|
368 | coord.at(i) = 1000.*goodPartic_.at(i).getBetaGamma().getComponent(index - 3)/begamz; // mimmiradians |
---|
369 | } else { |
---|
370 | coord.at(i) = 0.0; |
---|
371 | } |
---|
372 | } |
---|
373 | return; |
---|
374 | } |
---|
375 | |
---|
376 | if ( index == 5 ) { |
---|
377 | double gamma0 = referenceParticle_.getGamma(); |
---|
378 | // cout << " gamma0 = " << gamma0 << endl; |
---|
379 | if ( gamma0 == 0.0 ) return; |
---|
380 | for (unsigned i = 0; i < goodPartic_.size(); ++i) { |
---|
381 | coord.at(i) = 100.*(goodPartic_.at(i).getGamma() - gamma0)/gamma0; // en % |
---|
382 | // cout << " gamma0 = " << gamma0 << " gamma = " << goodPartic_.at(i).getGamma() << endl; |
---|
383 | } |
---|
384 | return; |
---|
385 | } |
---|
386 | } |
---|
387 | |
---|
388 | unsigned particleBeam::indexFromPspaToTransport(unsigned index) const { |
---|
389 | cout << " indexFromPspaToTransport entree : " << index << endl; |
---|
390 | switch ( index ) { |
---|
391 | case 0 : return 0; // x |
---|
392 | case 1 : return 2; // y |
---|
393 | case 2 : return 4; // z -> l |
---|
394 | case 3 : return 1; // xp |
---|
395 | case 4 : return 3; // yp |
---|
396 | case 5 : return 5; // de/E |
---|
397 | default : { |
---|
398 | cout << " particleBeam::indexFromPspaToTransport : coordinate index ERROR inital index : "<< index << endl; |
---|
399 | return 99; |
---|
400 | } |
---|
401 | } |
---|
402 | } |
---|
403 | |
---|
404 | |
---|
405 | void particleBeam::donneesDessinEllipse(vector<double>& xcor, vector<double>& ycor, vector<string>& legende, unsigned indexAbs, unsigned indexOrd) { |
---|
406 | int k; |
---|
407 | double x,y; |
---|
408 | cout << " particleBeam::donneesDessinEllipse index recus x" << indexAbs << " index y " << indexOrd << endl; |
---|
409 | |
---|
410 | if ( !momentRepresentationOk_ ) return; |
---|
411 | |
---|
412 | if ( indexAbs > 5 || indexOrd > 5 ) return; |
---|
413 | |
---|
414 | xcor.clear(); |
---|
415 | ycor.clear(); |
---|
416 | // les index sont dans l'ordre x,y,z,xp,yp, de/E |
---|
417 | // on traduit en TRANSPORT |
---|
418 | |
---|
419 | indexAbs = indexFromPspaToTransport(indexAbs); |
---|
420 | indexOrd = indexFromPspaToTransport(indexOrd); |
---|
421 | |
---|
422 | |
---|
423 | double dimensionalFactorX, dimensionalFactorY; // to mm, if necessary |
---|
424 | dimensionalFactorX = dimensionalFactorFromTransportToGraphics(indexAbs); |
---|
425 | dimensionalFactorY = dimensionalFactorFromTransportToGraphics(indexOrd); |
---|
426 | |
---|
427 | |
---|
428 | // a completer |
---|
429 | legende.clear(); |
---|
430 | // if ( indexAbs == 0 && indexOrd == 1 ) { |
---|
431 | string namx = transportVariableName(indexAbs); |
---|
432 | string namy = transportVariableName(indexOrd); |
---|
433 | double em = getUnnormalizedTranspPhaseSpaceArea(indexAbs,indexOrd) ; |
---|
434 | string emitt = mixedTools::doubleToString(getUnnormalizedTranspPhaseSpaceArea(indexAbs,indexOrd)); |
---|
435 | string xmax = namx + "max= "; |
---|
436 | string valXmax = mixedTools::doubleToString(dimensionalFactorX*getSigmaTransportij(indexAbs,indexAbs)); |
---|
437 | string ymax = namy + "max= "; |
---|
438 | string valYmax = mixedTools::doubleToString(dimensionalFactorY*getSigmaTransportij(indexOrd,indexOrd)); // mm |
---|
439 | string correl = " correlation "; |
---|
440 | string valCorrel = mixedTools::doubleToString(getSigmaTransportij(1,0)); |
---|
441 | string xunit = graphicTransportUnitName(indexAbs); |
---|
442 | string yunit = graphicTransportUnitName(indexOrd); |
---|
443 | legende.push_back( "emittance" + namx + "," + namy + ": " + emitt + " pi." + xunit + "." + yunit); |
---|
444 | legende.push_back( xmax + valXmax + xunit); |
---|
445 | legende.push_back( ymax + valYmax + yunit); |
---|
446 | // } else { |
---|
447 | // legende.push_back(" text of legend not yet programmed "); |
---|
448 | // } |
---|
449 | |
---|
450 | cout << " index x" << indexAbs << " index y " << indexOrd << endl; |
---|
451 | double xm = dimensionalFactorX*( rij_.getMatrix().at(indexAbs) ).at(indexAbs); |
---|
452 | double ym = dimensionalFactorY*( rij_.getMatrix().at(indexOrd) ).at(indexOrd); |
---|
453 | double r; |
---|
454 | if ( indexOrd > indexAbs ) { |
---|
455 | r = ( rij_.getMatrix().at(indexOrd) ).at(indexAbs); |
---|
456 | } else { |
---|
457 | r = ( rij_.getMatrix().at(indexAbs) ).at(indexOrd); |
---|
458 | } |
---|
459 | |
---|
460 | cout << " racs11= " << xm << " racs22= " << ym << " r12= " << r << endl; |
---|
461 | |
---|
462 | |
---|
463 | int nbintv = 50; |
---|
464 | if ( xm == 0.0 ) return; |
---|
465 | double pas = 2.0 * xm / nbintv; |
---|
466 | |
---|
467 | // cout << " r= " << r << endl; |
---|
468 | double rac = (1 - r*r); |
---|
469 | if ( rac > 0.0 ) |
---|
470 | { |
---|
471 | cout << " cas rac > " << endl; |
---|
472 | rac = sqrt(1 - r*r); |
---|
473 | double alpha = -r / rac; |
---|
474 | double beta = xm / ( ym * rac); |
---|
475 | // double gamma = ym / ( xm * rac ); |
---|
476 | double epsil = xm * ym * rac; |
---|
477 | double fac1 = -1.0 / ( beta * beta); |
---|
478 | double fac2 = epsil/beta; |
---|
479 | double fac3 = -alpha/beta; |
---|
480 | double aux; |
---|
481 | for ( k=0; k < nbintv; k++) |
---|
482 | { |
---|
483 | x = -xm + k*pas; |
---|
484 | aux = fac1 * x * x + fac2; |
---|
485 | // cout << " aux2= " << aux << endl; |
---|
486 | if ( aux <= 0.0 ) |
---|
487 | { |
---|
488 | aux = 0.0; |
---|
489 | } |
---|
490 | else aux = sqrt(aux); |
---|
491 | |
---|
492 | // y = fac3*x; |
---|
493 | y = fac3*x + aux; |
---|
494 | xcor.push_back(x); |
---|
495 | ycor.push_back(y); |
---|
496 | } |
---|
497 | |
---|
498 | for ( k=0; k <= nbintv; k++) |
---|
499 | { |
---|
500 | x = xm - k*pas; |
---|
501 | aux = fac1 * x * x + fac2; |
---|
502 | if ( aux <= 0.0 ) |
---|
503 | { |
---|
504 | aux = 0.0; |
---|
505 | } |
---|
506 | else aux = sqrt(aux); |
---|
507 | // y = fac3*x; |
---|
508 | y = fac3*x - aux; |
---|
509 | xcor.push_back(x); |
---|
510 | ycor.push_back(y); |
---|
511 | } |
---|
512 | } |
---|
513 | else |
---|
514 | // cas degenere |
---|
515 | { |
---|
516 | cout << " cas degenere " << endl; |
---|
517 | double fac = ym/xm; |
---|
518 | for ( k=0; k < nbintv; k++) |
---|
519 | { |
---|
520 | x = -xm + k*pas; |
---|
521 | y = fac*x; |
---|
522 | xcor.push_back(x); |
---|
523 | ycor.push_back(y); |
---|
524 | } |
---|
525 | |
---|
526 | } |
---|
527 | } |
---|
528 | |
---|
529 | void particleBeam::histogramme(int iabs,vector<double>&xcor,vector<int>& hist,int& cnts,double out[3]) |
---|
530 | { |
---|
531 | vector<double> vshf; |
---|
532 | preparationDesHistogrammes(iabs,vshf,out); |
---|
533 | |
---|
534 | double val[2]; |
---|
535 | if(iabs < 3) { |
---|
536 | val[0] = 100.0; |
---|
537 | val[1] = out[2]; |
---|
538 | } else { |
---|
539 | val[0] = out[1]; |
---|
540 | val[1] = out[2]; |
---|
541 | } |
---|
542 | remplissageDesHistogrammes(val,vshf,xcor,hist,cnts); |
---|
543 | |
---|
544 | // sortie pour la legende: out[0]= entries, out[1]= mean, out[2]= std deviation |
---|
545 | if(iabs < 3) { |
---|
546 | out[1] *= 1.0 ; // en m ? |
---|
547 | out[2] *= 1000.0; //mm |
---|
548 | } else { |
---|
549 | out[1] *= EREST_MeV ; //MeV |
---|
550 | out[2] *= EREST_keV; //KeV |
---|
551 | } |
---|
552 | } |
---|
553 | |
---|
554 | void particleBeam::preparationDesHistogrammes(int index,vector<double>& vshf,double out[3]) |
---|
555 | { |
---|
556 | double vmin= GRAND; |
---|
557 | double vmax= -vmin; |
---|
558 | double vmoy= 0.0; |
---|
559 | double ecatyp= 0.0; |
---|
560 | |
---|
561 | double sum= (float)goodPartic_.size(); |
---|
562 | if(index < 3) |
---|
563 | { |
---|
564 | // collecte d'une coordonnee x(index= 0), y(index= 1) ou z(index= 2) des particules |
---|
565 | for (unsigned int k = 0; k < goodPartic_.size(); k++) { |
---|
566 | double val = goodPartic_.at(k).getPosition().getComponent(index); |
---|
567 | if (val < vmin) vmin = val; |
---|
568 | else if (val > vmax) vmax = val; |
---|
569 | vmoy += val; |
---|
570 | ecatyp += val*val; |
---|
571 | } |
---|
572 | |
---|
573 | out[0]= sum; |
---|
574 | vmoy /= sum; |
---|
575 | out[1]= vmoy; |
---|
576 | ecatyp /= sum; |
---|
577 | ecatyp = sqrt(abs(ecatyp-vmoy*vmoy)); |
---|
578 | out[2]= ecatyp; |
---|
579 | } |
---|
580 | else |
---|
581 | { |
---|
582 | // collecte de l'energie acquise par les particules |
---|
583 | for (unsigned int k = 0; k < goodPartic_.size(); k++) { |
---|
584 | double gamma = goodPartic_.at(k).getGamma(); |
---|
585 | if (gamma < vmin) vmin = gamma; |
---|
586 | else if (gamma > vmax) vmax = gamma; |
---|
587 | double val = gamma-1.0; |
---|
588 | vmoy += val; |
---|
589 | ecatyp += val*val; |
---|
590 | } |
---|
591 | |
---|
592 | out[0]= sum; |
---|
593 | vmoy /= sum; |
---|
594 | out[1]= vmoy; |
---|
595 | ecatyp /= sum; |
---|
596 | ecatyp = sqrt(abs(ecatyp-vmoy*vmoy)); |
---|
597 | out[2]= ecatyp; |
---|
598 | } |
---|
599 | |
---|
600 | if(index == 0) { |
---|
601 | cout << "position X -moyenne " << vmoy << " m " << "-mini " << vmin << " m " << "-maxi " << vmax << " m " << "ecart type " << ecatyp*1000.0 << " mm" << endl; |
---|
602 | } else if(index == 1) { |
---|
603 | cout << "position Y -moyenne " << vmoy << " m " << "-mini " << vmin << " m " << "-maxi " << vmax << " m " << "ecart type " << ecatyp*1000.0 << " mm" << endl; |
---|
604 | } else if(index == 2) { |
---|
605 | cout << "position Z -moyenne " << vmoy << " m " << "-mini " << vmin << " m " << "-maxi " << vmax << " m " << "ecart type " << ecatyp*1000.0 << " mm" << endl; |
---|
606 | } else { |
---|
607 | double gmin = vmin-1.0; |
---|
608 | double gmax = vmax-1.0; |
---|
609 | 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; |
---|
610 | } |
---|
611 | |
---|
612 | //pouVoir: autre maniere de calculer l'ecart-type |
---|
613 | double stdev= 0.0; |
---|
614 | |
---|
615 | if(!vshf.empty()) vshf.clear(); |
---|
616 | vshf.resize(goodPartic_.size()); |
---|
617 | if(index < 3) |
---|
618 | { |
---|
619 | for (unsigned int k = 0; k < goodPartic_.size(); k++) { |
---|
620 | double val = goodPartic_.at(k).getPosition().getComponent(index); |
---|
621 | vshf[k]= val-vmoy; |
---|
622 | stdev += vshf[k]*vshf[k]; |
---|
623 | } |
---|
624 | } |
---|
625 | else |
---|
626 | { |
---|
627 | for (unsigned int k = 0; k < goodPartic_.size(); k++) { |
---|
628 | double gamma = goodPartic_.at(k).getGamma(); |
---|
629 | double val = gamma-1.0; |
---|
630 | vshf[k]= val-vmoy; |
---|
631 | stdev += vshf[k]*vshf[k]; |
---|
632 | } |
---|
633 | } |
---|
634 | |
---|
635 | stdev /= sum; |
---|
636 | stdev = sqrt(stdev); |
---|
637 | cout << "stdev " << stdev << ", ecatyp " << ecatyp << ", diff " << stdev-ecatyp << endl; |
---|
638 | } |
---|
639 | |
---|
640 | void particleBeam::remplissageDesHistogrammes(double val[2],vector<double>vshf,vector<double>&xcor,vector<int>& hist,int& cnts) |
---|
641 | { |
---|
642 | double vmoy = val[0]; |
---|
643 | double ecatyp= val[1]; |
---|
644 | |
---|
645 | // demi fenetre hfene= max(3.*ecatyp-vmoy,vmoy-3.*ecatyp); |
---|
646 | double hfene= 3.*ecatyp; |
---|
647 | // et pas de l'histogramme |
---|
648 | double hpas = hfene/25.; |
---|
649 | |
---|
650 | cout << "demi fenetre " << hfene << ", hpas= " << hpas << endl; |
---|
651 | |
---|
652 | double vmin = -hfene; |
---|
653 | double dfen = 2.*hfene; |
---|
654 | int ihist = dfen/hpas; |
---|
655 | double phist = ihist*hpas; |
---|
656 | double dpas = hpas-(dfen-phist); |
---|
657 | if(dpas <= hpas*1.e-03) { |
---|
658 | ihist++; |
---|
659 | phist= ihist*hpas; |
---|
660 | } |
---|
661 | double vmax= vmin+hpas*ihist; |
---|
662 | |
---|
663 | cout << "'xAxisNumberOfBins= " << ihist <<", xAxisMinimum= " << vmin << ", xAxisMaximum= " << vmax << ", NParticules= " << vshf.size() << ", phist " << phist << endl; |
---|
664 | |
---|
665 | if(!xcor.empty()) xcor.clear(); |
---|
666 | |
---|
667 | // pourVoir ///////////////////////////////////// |
---|
668 | // xcor.resize(ihist); |
---|
669 | // for (int i = 0; i < ihist; ++i) { |
---|
670 | // // on gradue l'abscisse en pourcents |
---|
671 | // xcor[i]= 100.*( vmin+i*hpas )/vmoy; |
---|
672 | // } |
---|
673 | |
---|
674 | xcor.resize(ihist+1); |
---|
675 | for (int i = 0; i <= ihist; ++i) { |
---|
676 | xcor[i]= 100.*( vmin+i*hpas )/vmoy; |
---|
677 | } |
---|
678 | ///////////////////////////////////// |
---|
679 | |
---|
680 | if(!hist.empty()) hist.clear(); |
---|
681 | hist.resize(ihist,0); |
---|
682 | for (unsigned i = 0; i < vshf.size(); ++i) { |
---|
683 | double var= vshf[i]-vmin; |
---|
684 | if(var < 0 ) { |
---|
685 | cout<<"lesser that the minimum "<<var<<", ("<< i<<")"<< endl; |
---|
686 | hist[0]++; |
---|
687 | } |
---|
688 | |
---|
689 | if(var >= phist) { |
---|
690 | cout<<"greater that the maximum "<<var<<", ("<< i<<")"<< endl; |
---|
691 | hist[ihist-1]++; |
---|
692 | } |
---|
693 | |
---|
694 | int kk= (int)floor(var/hpas); |
---|
695 | hist[kk]++; |
---|
696 | } |
---|
697 | |
---|
698 | cnts= 0; |
---|
699 | for (int i = 0; i < ihist; ++i) { |
---|
700 | if(hist.at(i) > 0) cnts++; |
---|
701 | } |
---|
702 | cout<< " ... cnts= " << cnts << endl; |
---|
703 | } |
---|
704 | |
---|
705 | // void particleBeam::histogramme(vector<double>&xcor,vector<int>& hist,int& cnts,double out[3]) |
---|
706 | // { |
---|
707 | // // sortie pour la legende: out[0]= entries, out[1]= mean, out[2]= rms |
---|
708 | |
---|
709 | // // on calcule avec les gammas ; les MeV c'est pour les sorties |
---|
710 | |
---|
711 | // double gammin= GRAND; |
---|
712 | // double gammax= -gammin; |
---|
713 | // // double Emoy= 0.0; |
---|
714 | // double ecatyp= 0.0; |
---|
715 | // double gmoy=0.0; |
---|
716 | // for (unsigned int k = 0; k < goodPartic_.size(); k++) |
---|
717 | // { |
---|
718 | // double gamma = goodPartic_.at(k).getGamma(); |
---|
719 | // // double EMev = (gamma-1.0)*ERESTMeV; |
---|
720 | // if (gamma < gammin) gammin = gamma; |
---|
721 | // else if (gamma > gammax) gammax = gamma; |
---|
722 | // // Emoy += EMev; |
---|
723 | // double g = gamma-1.0; |
---|
724 | // gmoy += g; |
---|
725 | // // ecatyp += EMev*EMev; |
---|
726 | // ecatyp += g*g; |
---|
727 | // } |
---|
728 | |
---|
729 | // double sum= (float)goodPartic_.size(); |
---|
730 | // out[0]= sum; |
---|
731 | // // Emoy /= sum; |
---|
732 | // gmoy /= sum; |
---|
733 | // // out[1]= Emoy; //MeV |
---|
734 | // out[1]= gmoy*EREST_MeV ; //MeV |
---|
735 | // ecatyp /= sum; |
---|
736 | // // out[2]= 1000.0*sqrt(ecatyp); //KeV |
---|
737 | // // ecatyp = sqrt(abs(ecatyp-Emoy*Emoy)); |
---|
738 | |
---|
739 | // ecatyp = sqrt(abs(ecatyp-gmoy*gmoy)); |
---|
740 | // out[2]= ecatyp*EREST_keV; //KeV |
---|
741 | |
---|
742 | // double gmin = gammin-1.0; |
---|
743 | // double gmax = gammax-1.0; |
---|
744 | // cout << "energie cinetique -moyenne " << gmoy*EREST_MeV << " Mev " << "-mini " << gmin*EREST_MeV << " Mev " << "-maxi " << gmax*EREST_MeV << " Mev " << "ecart type " << ecatyp*EREST_keV << " Kev" << endl; |
---|
745 | |
---|
746 | // vector<double> Eshf; |
---|
747 | // for (unsigned int k = 0; k < goodPartic_.size(); k++) |
---|
748 | // { |
---|
749 | // double gamma = goodPartic_.at(k).getGamma(); |
---|
750 | // // double EMev = (gamma-1.0)*ERESTMeV; |
---|
751 | // // Eshf.push_back(EMev-Emoy); |
---|
752 | // // modif glm 2/03/2013 |
---|
753 | // double g = (gamma-1.0); |
---|
754 | // Eshf.push_back( (g-gmoy) ); |
---|
755 | // } |
---|
756 | |
---|
757 | // ////////////////////////////////////////////////////////////////////////////// |
---|
758 | |
---|
759 | // // demi fenetre en energie, et pas de l'histogramme |
---|
760 | // // double hfene= max(3.*ecatyp-Emoy,Emoy-3.*ecatyp); |
---|
761 | // double hfene= 3.*ecatyp; |
---|
762 | // double hpas = hfene/25.; |
---|
763 | |
---|
764 | // cout << "demi fenetre " << hfene << ", hpas= " << hpas << endl; |
---|
765 | |
---|
766 | // double vmin = -hfene; |
---|
767 | // double dfen = 2.*hfene; |
---|
768 | // int ihist = dfen/hpas; |
---|
769 | // double phist = ihist*hpas; |
---|
770 | // double dpas = hpas-(dfen-phist); |
---|
771 | // if(dpas <= hpas*1.e-03) { |
---|
772 | // ihist++; |
---|
773 | // phist= ihist*hpas; |
---|
774 | // } |
---|
775 | // double vmax= vmin+hpas*ihist; |
---|
776 | |
---|
777 | // cout << "'xAxisNumberOfBins= " << ihist <<", xAxisMinimum= " << vmin << ", xAxisMaximum= " << vmax << ", NParticules= " << Eshf.size() << ", phist " << phist << endl; |
---|
778 | |
---|
779 | // xcor= vector<double>(ihist); |
---|
780 | // for (int i = 0; i < ihist; ++i) { |
---|
781 | |
---|
782 | // // on gradue l'abscisse en pourcents |
---|
783 | // // xcor[i]= 100.*( vmin+i*hpas ); |
---|
784 | // xcor[i]= 100.*( vmin+i*hpas )/gmoy; |
---|
785 | // } |
---|
786 | |
---|
787 | // hist= vector<int>(ihist,0); |
---|
788 | // for (unsigned i = 0; i < Eshf.size(); ++i) { |
---|
789 | // double var= Eshf[i]-vmin; |
---|
790 | // // if(var < 0 || var >= phist) cout<<"out of range "<<var<<", ("<< i<<")"<< endl; |
---|
791 | // if(var < 0 ) { |
---|
792 | // cout<<"lesser that the minimum "<<var<<", ("<< i<<")"<< endl; |
---|
793 | // hist[0]++; |
---|
794 | // } |
---|
795 | // if(var >= phist) { |
---|
796 | // cout<<"greater that the maximum "<<var<<", ("<< i<<")"<< endl; |
---|
797 | // hist[ihist-1]++; |
---|
798 | // } |
---|
799 | // // int k= var/hpas; |
---|
800 | // int kk= (int)floor(var/hpas); |
---|
801 | // //if(i%20 == 0) cout<<"v("<<i<<")= " <<var<<" ["<<k<<"-"<<kk<<"], "<<endl; |
---|
802 | // hist[kk]++; |
---|
803 | // } |
---|
804 | |
---|
805 | // cnts= 0; |
---|
806 | // for (int i = 0; i < ihist; ++i) { |
---|
807 | // if(hist.at(i) > 0) cnts++; |
---|
808 | // //cout<<"("<<xcor.at(i)<<","<<hist.at(i)<<") "; |
---|
809 | // } |
---|
810 | // cout<< " ... cnts= " << cnts << endl; |
---|
811 | // } |
---|