source: PSPA/Interface_Web/trunk/pspaWT/sources/controler/src/particleBeam.cc @ 324

Last change on this file since 324 was 324, checked in by touze, 11 years ago

menu analyse graphique + histo

File size: 12.7 KB
Line 
1#include "particleBeam.h"
2#include "mathematicalConstants.h"
3#include "PhysicalConstants.h"
4#include "mathematicalTools.h"
5//#include <string>
6#include <stdio.h>
7#include <algorithm>
8#include <sstream>
9//#include "environmentVariables.h"
10#include <Wt/WApplication>
11
12using namespace std;
13
14particleBeam::particleBeam()  {
15  // rij_transportMoments_.resize(6);
16  // unsigned dim=0;
17  // unsigned k;
18  // for ( k=0; k < 6; k++){
19  //   rij_transportMoments_.at(k).resize(++dim);
20  // }
21  P0Transport_ = 0.0;
22  particleRepresentationOk_ = false;
23  momentRepresentationOk_ = false;
24}
25
26void particleBeam::clear() {
27  goodPartic_.clear();
28  rij_.raz();
29  P0Transport_ = 0.0;
30  particleRepresentationOk_ = false;
31  momentRepresentationOk_ = false;
32}
33
34int particleBeam::getNbParticles() const {
35  return goodPartic_.size();
36}
37
38const beam2Moments&   particleBeam::getTransportMoments() const  { 
39    return rij_;
40}
41
42double particleBeam::getSigmaTransportij(unsigned i, unsigned j)  {
43  if ( i < 1 || i > 6 || j < 1 || j > 6 ) {
44    cerr << " particleBeam::getSigmaTransportij() indices out of range  " << endl;
45    return 0.0;
46  }
47  if ( !momentRepresentationOk_ ) {
48    cerr << " particleBeam::getSigmaTransportij() beam is not in moment representation " << endl;
49    return 0.0;
50  }
51
52  i--;
53  j--;
54  if ( j > i ) {
55    unsigned aux = i;
56    i = j;
57    j = aux;
58  }
59  return ( rij_.getMatrix().at(i) ).at(j);
60}
61
62
63double particleBeam::getUnnormalizedEmittanceX() {
64  double r = getSigmaTransportij(2,1);
65  double rac = (1 - r*r);
66  if ( rac <= 0.0 ) {
67    return 0.0;
68  }
69  rac = sqrt(1 - r*r);
70  return  10.*getSigmaTransportij(1,1) * getSigmaTransportij(2,2) * rac; // en pi.mm.mrad
71}
72
73double particleBeam::getP0Transport() const { 
74  return P0Transport_;
75}
76
77double particleBeam::referenceKineticEnergyMeV() const {
78  if ( particleRepresentationOk_ ) {
79    return (referenceParticle_.getGamma() -1.) * ERESTMeV;
80  } else {
81    double P0Norm = 1000.0 * P0Transport_ / ERESTMeV;
82    double gamma = sqrt(1.0 +  P0Norm * P0Norm);
83    return (gamma - 1.0) * ERESTMeV;
84  }
85}
86
87
88
89void particleBeam::set2Moments(beam2Moments& moments) {
90  rij_ = moments;
91  momentRepresentationOk_ = true;
92}
93
94void particleBeam::setWithParticles(vector<double>& centroid, bareParticle& referencePart, vector<bareParticle>& particles) {
95  cout << " particleBeam::setWithParticles taille vect. part. " << particles.size() << endl;
96  centroid_.clear();
97  centroid_ = centroid;
98  referenceParticle_ = referencePart;
99  goodPartic_.clear();
100  goodPartic_ = particles;
101  cout << " particleBeam::setWithParticles taille vect. part. ENREGISTRE " << goodPartic_.size() << endl;
102  //  printAllXYZ();
103  particleRepresentationOk_ = true;
104}
105
106
107bool particleBeam::particleRepresentationOk() const {
108  return particleRepresentationOk_;
109}
110bool particleBeam::momentRepresentationOk() const {
111  return momentRepresentationOk_;
112}
113
114void  particleBeam::addParticle( bareParticle p)
115{
116  goodPartic_.push_back(p);
117}
118
119const vector<bareParticle>& particleBeam::getParticleVector() const
120{
121  return goodPartic_;
122}
123vector<bareParticle>& particleBeam::getParticleVector() 
124{
125  return goodPartic_;
126}
127
128
129
130
131void particleBeam::getVariance(double& varx, double& vary, double& varz) const {
132  unsigned int k;
133  double x,y,z;
134  double xav = 0.;
135  double yav = 0.;
136  double zav = 0.;
137  double xavsq = 0.;
138  double yavsq = 0.;
139  double zavsq = 0.;
140
141  TRIDVECTOR pos;
142
143
144  for ( k = 0 ; k < goodPartic_.size(); k++) {
145    pos = goodPartic_.at(k).getPosition();
146    pos.getComponents(x,y,z);
147    //      partic_[k].getXYZ(x,y,z);
148    xav += x;
149    xavsq += x*x;
150    yav += y;
151    yavsq += y*y;
152    zav += z;
153    zavsq += z*z;
154  }
155
156  double aginv = double (goodPartic_.size());
157  aginv = 1.0/aginv;
158
159  varx =  aginv * ( xavsq - xav*xav*aginv );
160  vary =  aginv * ( yavsq - yav*yav*aginv );
161  varz =  aginv * ( zavsq - zav*zav*aginv );
162}
163
164
165void particleBeam::printAllXYZ() const {
166  cout << " dump du faisceau : " << endl;
167  cout <<  goodPartic_.size() << " particules " << endl;
168  unsigned int k;
169  for ( k = 0 ; k < goodPartic_.size(); k++)
170    {
171      double xx,yy,zz;
172      goodPartic_.at(k).getPosition().getComponents(xx,yy,zz);
173      double betgamx, betgamy, betgamz;
174      goodPartic_.at(k).getBetaGamma().getComponents(betgamx, betgamy, betgamz);
175      cout << " part. numero " << k << "  x= " << xx << " y= " << yy  << " z= " << zz << "  betgamx= " << betgamx << " betgamy= " << betgamy  << " betgamz= " << betgamz << endl;
176    }
177}
178
179
180
181void particleBeam::Zrange(double& zmin, double& zmax) const {
182  double z;
183  zmin = GRAND;
184  zmax = -zmin;
185
186  unsigned int k;
187  for ( k = 0 ; k < goodPartic_.size(); k++)
188    {
189      z = goodPartic_.at(k).getZ();
190      if ( z < zmin ) zmin = z;
191      else if ( z > zmax) zmax = z;         
192    }
193}
194
195
196
197string particleBeam::FileOutputFlow() const {
198  ostringstream sortie;
199  unsigned int k;
200  for ( k = 0 ; k < goodPartic_.size(); k++)
201    {
202      sortie << goodPartic_.at(k).FileOutputFlow() << endl;
203    }
204  sortie << endl;
205  return sortie.str();
206}
207
208bool particleBeam::FileInput( ifstream& ifs) {
209  bool test = true;
210  string dum1, dum2;
211  double dummy;
212  if ( !( ifs >> dum1 >> dum2 >> dummy) ) return false;
213 
214  bareParticle pp;
215  while ( pp.FileInput(ifs) )
216    {
217      addParticle( pp);
218    }
219  return test;
220}
221
222void particleBeam::buildMomentRepresentation() {
223
224  unsigned k,j,m;
225  double auxj, auxm;
226  if ( !particleRepresentationOk_)
227    {
228      cerr << " particleBeam::buildMomentRepresentation() vecteur de particules invalide" << endl;
229      return;
230    }
231
232  cout << " buildMomentRepresentation " << endl;
233  //  printAllXYZ();
234
235  double gref = referenceParticle_.getGamma() - 1.0;
236  double P_reference_MeV_sur_c = sqrt( gref*(gref+2) );
237
238  cout << " gref = " << gref << " P_reference_MeV_sur_c = " << P_reference_MeV_sur_c << endl;
239
240
241  // initialisation des moments
242  razDesMoments();
243
244  // accumulation
245  TRIDVECTOR pos;
246  TRIDVECTOR begam;
247  double gamma;
248  double begamz;
249  double g;
250  double PMeVsc;
251  double del;
252  vector<double> part(6, 0.0);
253
254    vector< vector<double> >& matrice = rij_.getMatrix();
255
256
257  for (k=0; k < goodPartic_.size(); k++) {
258    gamma = goodPartic_.at(k).getGamma();
259    pos = goodPartic_.at(k).getPosition();
260    begam= goodPartic_.at(k).getBetaGamma();
261    begamz = begam.getComponent(2);
262    g = gamma -1.0;
263    PMeVsc = sqrt( g*(g+2) );
264    del = 100.0 * ( PMeVsc -  P_reference_MeV_sur_c ) / P_reference_MeV_sur_c ; // en %
265
266    part[0] = pos.getComponent(0);
267    part[1] = begam.getComponent(0)/begamz;
268    part[2] = pos.getComponent(1);
269    part[3] = begam.getComponent(1)/begamz;
270    part[4] = pos.getComponent(2);
271    part[5] = del;
272
273    for ( j = 0; j < 6; j++) {
274      auxj = part.at(j) - centroid_.at(j);
275      for (m=0; m <= j; m++) 
276        {
277          auxm = part.at(m) - centroid_.at(m);
278
279          ( matrice.at(j) ).at(m) += auxj*auxm;
280          //      ( rij_transportMoments_.at(j) ).at(m) += auxj*auxm;
281
282
283          //          cout << " j= " << j << " m= " << m << " rjm= " << ( rij_transportMoments_.at(j) ).at(m) << endl;
284        }
285    }
286  }
287
288
289  // moyenne
290  double facmoy = 1.0/double( goodPartic_.size() );
291  for ( j = 0; j < 6; j++) {
292        ( matrice.at(j) ).at(j) = sqrt(( matrice.at(j) ).at(j) * facmoy );
293  }
294
295  for ( j = 0; j < 6; j++) {
296    auxj =  ( matrice.at(j) ).at(j);
297    for (m=0; m < j; m++) {
298      auxm = ( matrice.at(m) ).at(m);
299      (  matrice.at(j) ).at(m) *= facmoy/(auxj * auxm);
300    }
301  }
302   
303  ////////////////// si C21 = 1 , transport plante ! a voir //////////
304cout << " valeur initiale de  C21: " << ( matrice.at(1) ).at(0) << endl;
305  if ( ( matrice.at(1) ).at(0) >0.999999  ) {
306    ( matrice.at(1) ).at(0) = 0.999999;
307    cout << " j'ai fait la correction C21: " << ( matrice.at(1) ).at(0) << endl;
308  }
309 
310
311  // les longueurs sont en cm
312  // les angles en radians, on passe en mrad;
313
314  double uniteAngle = 1.0e+3;
315  ( matrice.at(1) ).at(1)  *= uniteAngle;
316  ( matrice.at(3) ).at(3)  *= uniteAngle;
317  P0Transport_ = 1.0e-3*ERESTMeV*P_reference_MeV_sur_c;
318
319  //  cout << " buildmomentrepresentation impression des moments " << endl;
320  //  impressionDesMoments();
321
322  momentRepresentationOk_ = true;
323}
324
325void particleBeam::impressionDesMoments() const {
326  rij_.impression();
327}
328
329void particleBeam::razDesMoments() {
330  rij_.raz();
331}
332
333
334// void particleBeam::readTransportMoments(ifstream& inp) {
335// rij_.readFromTransportOutput(inp);
336// }
337
338// void particleBeam::readTransportMoments(stringstream& inp) {
339// rij_.readFromTransportOutput(inp);
340// }
341
342double particleBeam::getXmaxRms() {
343  if ( !momentRepresentationOk_ ) buildMomentRepresentation();
344  return ( rij_.getMatrix().at(0) ).at(0);
345  // return ( rij_transportMoments_.at(0) ).at(0);
346}
347
348void particleBeam::donneesDessinEllipseXxp(vector<double>& xcor, vector<double>& ycor) {
349  int k;
350  double x,y;
351
352  if ( !momentRepresentationOk_ ) return;
353
354  xcor.clear();
355  ycor.clear();
356
357  double xm = ( rij_.getMatrix().at(0) ).at(0);
358  double ym = ( rij_.getMatrix().at(1) ).at(1);
359  double r  = ( rij_.getMatrix().at(1) ).at(0);
360
361  cout <<  " racs11= " << xm << " racs22= " << ym << " r12= " << r << endl;
362
363
364  int nbintv = 50;
365  if ( xm == 0.0 ) return;
366  double pas = 2.0 * xm / nbintv;
367
368  //  cout << " r= " << r << endl;
369  double rac = (1 - r*r);
370  if ( rac > 0.0 ) 
371    {
372      cout << " cas rac > " << endl;
373      rac = sqrt(1 - r*r);
374      double alpha = -r / rac;
375      double beta = xm / ( ym * rac);
376      //  double gamma = ym / ( xm * rac );
377      double epsil = xm * ym * rac;
378      double fac1 = -1.0 / ( beta * beta);
379      double fac2 = epsil/beta;
380      double fac3 = -alpha/beta;
381      double aux;
382      for ( k=0; k < nbintv; k++)
383        {
384          x = -xm + k*pas;
385          aux = fac1 * x * x + fac2;
386          //     cout << " aux2= " << aux << endl;
387          if ( aux <= 0.0 )
388            {
389              aux = 0.0;
390            }
391          else aux = sqrt(aux);
392     
393          //        y = fac3*x;
394          y = fac3*x + aux;
395          xcor.push_back(x);
396          ycor.push_back(y);
397        }
398
399      for ( k=0; k <= nbintv; k++)
400        {
401          x = xm - k*pas;
402          aux =  fac1 * x * x + fac2;
403          if ( aux <= 0.0 ) 
404            {
405              aux = 0.0;
406            }
407          else aux = sqrt(aux);
408          //   y = fac3*x;
409          y = fac3*x - aux;
410          xcor.push_back(x);
411          ycor.push_back(y);
412        }
413    }
414  else
415    // cas degenere
416    {
417      cout << " cas degenere " << endl;
418      double fac = ym/xm;
419      for ( k=0; k < nbintv; k++)
420        {
421          x = -xm + k*pas;
422          y = fac*x;
423          xcor.push_back(x);
424          ycor.push_back(y);
425        }
426       
427    }
428}
429
430void particleBeam::histogramme(vector<double>&xcor,vector<int>& hist,int& cnts,double out[3])
431{
432  // sortie pour la legende: out[0]= entries, out[1]= mean, out[2]= rms
433
434  double gammin= GRAND;
435  double gammax= -gammin;
436  double Emoy= 0.0;
437  double ecatyp= 0.0;
438
439  for (unsigned int k = 0; k < goodPartic_.size(); k++)
440    {
441      double gamma = goodPartic_.at(k).getGamma();
442      double EMev = (gamma-1.0)*ERESTMeV;
443      if (gamma < gammin) gammin = gamma;
444      else if (gamma > gammax) gammax = gamma;
445      Emoy += EMev;
446      ecatyp += EMev*EMev;
447    }
448
449  double sum= (float)goodPartic_.size();
450  out[0]= sum;
451  Emoy /= sum;
452  out[1]= Emoy; //MeV
453  ecatyp /= sum;
454  out[2]= 1000.0*sqrt(ecatyp); //KeV
455  ecatyp = sqrt(abs(ecatyp-Emoy*Emoy));
456
457  double Emin = (gammin-1.0)*ERESTMeV;
458  double Emax = (gammax-1.0)*ERESTMeV;
459  cout << "energie cinetique -moyenne " << Emoy << " Mev " << "-mini " << Emin << " Mev " << "-maxi " << Emax << " Mev " << "ecart type " << ecatyp*1000.0 << " Kev" << endl;
460
461  vector<double> Eshf;
462  for (unsigned int k = 0; k < goodPartic_.size(); k++)
463    {
464      double gamma = goodPartic_.at(k).getGamma();
465      double EMev = (gamma-1.0)*ERESTMeV;
466      Eshf.push_back(EMev-Emoy);
467    }
468
469  //////////////////////////////////////////////////////////////////////////////
470 
471  // demi fenetre en energie, et pas de l'histogramme
472  //  double hfene= max(3.*ecatyp-Emoy,Emoy-3.*ecatyp);
473  double hfene= 3.*ecatyp;
474  double hpas = hfene/25.;
475
476  cout << "demi fenetre " << hfene << ", hpas= " << hpas << endl;
477
478  double vmin = -hfene;
479  double dfen = 2.*hfene;
480  int ihist = dfen/hpas;
481  double phist = ihist*hpas;
482  double dpas = hpas-(dfen-phist);
483  if(dpas <= hpas*1.e-03) {
484    ihist++;
485    phist= ihist*hpas;
486  }
487  double vmax= vmin+hpas*ihist;
488 
489  cout << "'xAxisNumberOfBins= " << ihist <<", xAxisMinimum= " << vmin << ", xAxisMaximum= " << vmax << ", NParticules= " << Eshf.size() << ", phist " << phist << endl;
490
491  xcor= vector<double>(ihist);
492  for (int i = 0; i < ihist; ++i) {
493
494    // on gradue l'abscisse en pourcents
495    xcor[i]= 100.*( vmin+i*hpas );
496  }
497
498  hist= vector<int>(ihist,0);
499  for (unsigned i = 0; i < Eshf.size(); ++i) {
500    double var= Eshf[i]-vmin;
501    if(var < 0 || var >= phist) cout<<"out of range "<<var<<", ("<< i<<")"<< endl;
502    int k= var/hpas;
503    int kk= (int)floor(var/hpas);
504    //if(i%20 == 0) cout<<"v("<<i<<")= " <<var<<" ["<<k<<"-"<<kk<<"], "<<endl;
505    hist[kk]++;
506  }
507
508  cnts= 0;
509  for (int i = 0; i < ihist; ++i) {
510    if(hist.at(i) > 0) cnts++;
511    //cout<<"("<<xcor.at(i)<<","<<hist.at(i)<<") ";
512  }
513  cout<< " ... cnts=  " << cnts << endl;
514}
Note: See TracBrowser for help on using the repository browser.