Changeset 1365 in svn for trunk/external/Hector/H_BeamParticle.cc
- Timestamp:
- Apr 16, 2014, 3:56:14 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/external/Hector/H_BeamParticle.cc
r1360 r1365 1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * 2 * * 3 * --<--<-- A fast simulator --<--<-- * 4 * / --<--<-- of particle --<--<-- * 5 * ----HECTOR----< * 6 * \ -->-->-- transport through -->-->-- * 7 * -->-->-- generic beamlines -->-->-- * 8 * * 9 * JINST 2:P09005 (2007) * 10 * X Rouby, J de Favereau, K Piotrzkowski (CP3) * 11 * http://www.fynu.ucl.ac.be/hector.html * 12 * * 13 * Center for Cosmology, Particle Physics and Phenomenology * 14 * Universite catholique de Louvain * 15 * Louvain-la-Neuve, Belgium * 16 * * 17 * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 1 /* 2 ---- Hector the simulator ---- 3 A fast simulator of particles through generic beamlines. 4 J. de Favereau, X. Rouby ~~~ hector_devel@cp3.phys.ucl.ac.be 5 6 http://www.fynu.ucl.ac.be/hector.html 7 8 Centre de Physique des Particules et de Phénoménologie (CP3) 9 Université Catholique de Louvain (UCL) 10 */ 18 11 19 12 /// \file H_BeamParticle.cc … … 26 19 // c++ #includes 27 20 #include <iostream> 28 #include <fstream>29 21 #include <iomanip> 30 22 #include <vector> … … 35 27 #include "H_Parameters.h" 36 28 #include "TRandom.h" 37 #include "TVectorD.h" 29 //#include "TView.h" 30 //#include "TPolyLine3D.h" 38 31 #ifdef _include_pythia_ 39 32 #include "TPythia6.h" 33 #include "TRandom.h" 40 34 #endif 41 42 35 // local #includes 43 36 #include "H_OpticalElement.h" … … 47 40 using namespace std; 48 41 49 void H_BeamParticle::init() { // for more efficiency, put the objects away from init!42 void H_BeamParticle::init() { 50 43 mp = MP; 51 44 qp = QP; … … 65 58 } 66 59 67 H_BeamParticle::H_BeamParticle() { 60 H_BeamParticle::H_BeamParticle() { 68 61 init(); 69 62 } 70 63 71 H_BeamParticle::H_BeamParticle(const H_BeamParticle& p): 72 mp(p.mp), qp(p.qp), fs(p.fs), fx(p.fx), fy(p.fy), thx(p.thx), thy(p.thy), 73 energy(p.energy), hasstopped(p.hasstopped), hasemitted(p.hasemitted), 74 isphysical(p.isphysical), stop_position(new TVectorD(*(p.stop_position))), 75 stop_element(0), positions(p.positions) { 64 H_BeamParticle::H_BeamParticle(const H_BeamParticle& p) { 65 mp = p.mp; 66 qp = p.qp; 67 fx = p.fx; 68 fy = p.fy; 69 thx = p.thx; 70 thy = p.thy; 71 fs = p.fs; 72 energy = p.energy; 73 hasstopped = p.hasstopped; 74 hasemitted = p.hasemitted; 75 isphysical = p.isphysical; 76 stop_position = new TVectorD(*(p.stop_position)); 76 77 if(p.hasstopped) stop_element = new H_OpticalElement(*(p.stop_element)); 78 positions = p.positions; 77 79 } 78 80 … … 99 101 stop_position = new TVectorD(*(p.stop_position)); 100 102 if(p.hasstopped) stop_element = new H_OpticalElement(*(p.stop_element)); 101 else stop_element = 0;102 103 positions = p.positions; 103 104 return *this; 104 105 } 105 106 106 constbool H_BeamParticle::stopped(const H_AbstractBeamLine * beamline) {107 bool H_BeamParticle::stopped(const H_AbstractBeamLine * beamline) { 107 108 vector<TVectorD>::const_iterator position_i; 108 109 for(position_i = positions.begin(); position_i < positions.end()-1; position_i++) { … … 110 111 if(beamline->getElement(pos)->getAperture()->getType()!=NONE) { 111 112 bool has_passed_entrance = beamline->getElement(pos)->isInside((*position_i)[INDEX_X],(*position_i)[INDEX_Y]); 112 bool has_passed_exit = beamline->getElement(pos)->isInside((*(position_i+1))[INDEX_X],(*(position_i+1))[INDEX_Y]); 113 // here we should distinguish between particles passing the input or not. 114 // - particles not passing the input are logically stopped at the input position. period. 115 // - particles passing the input but not the output are stopped somewhere in the element 116 // - particles passing both the input and the output could have been stopped somewhere inside too (less likely) 117 if(!has_passed_entrance) { 118 if(VERBOSE) cout<<"Stopped at the entrance of "<<beamline->getElement(pos)->getName(); 119 hasstopped=true; 120 stop_element = const_cast<H_OpticalElement*>(beamline->getElement(pos)); 121 *stop_position = *position_i; 122 if(VERBOSE) cout<<" at s = "<<(*stop_position)[4]<<endl; 123 return hasstopped; 124 } else if (!has_passed_exit) { 125 if(VERBOSE) cout<<"Stopped inside "<<beamline->getElement(pos)->getName(); 126 hasstopped=true; 127 stop_element = const_cast<H_OpticalElement*>(beamline->getElement(pos)); 128 // this should be computed using the element-based method "H_OpticalElement::getHitPosition" (caution : always nonlinear). 129 *stop_position = beamline->getElement(pos)->getHitPosition(*position_i,BE-energy,mp,qp); 130 if(VERBOSE) cout<<" at s = "<<(*stop_position)[4]<<endl; 131 return hasstopped; 132 } 133 /* 113 bool has_passed_exit = beamline->getElement(pos)->isInside((*(position_i+1))[INDEX_X],(*(position_i+1))[INDEX_Y]); 134 114 if(!(has_passed_entrance && has_passed_exit)) { 115 // cout << "p =" << (*position_i)[INDEX_X] << "; el=" << beamline->getElement(pos)->getX()<<endl; 116 // beamline->getElement(position_i-positions.begin())->printProperties(); 135 117 if(VERBOSE) cout<<"particle stopped at "<<(beamline->getElement(pos))->getName(); 136 118 if(VERBOSE) cout<<" (s = "<<(*position_i)[4] << ")" << endl; 119 if(VERBOSE && !has_passed_exit) cout << "Particle stopped inside the element" << endl; 137 120 hasstopped=true; 138 121 stop_element = const_cast<H_OpticalElement*>(beamline->getElement(pos)); … … 140 123 return hasstopped; 141 124 } // if 142 */ 125 // else cout << "outside aperture " << endl; 143 126 } // if 144 127 } // for … … 159 142 } 160 143 161 162 163 144 void H_BeamParticle::smearPos(const double dx,const double dy, TRandom* r) { 164 145 // the beam is centered on (fx,fy) at IP … … 191 172 } 192 173 193 194 174 void H_BeamParticle::set4Momentum(const double px, const double py, const double pz, const double ene) { 195 175 /// @param px, py, pz, ene is \f$ (\vec p , E) [GeV]\f$ … … 207 187 addPosition(fx,thx,fy,thy,fs); 208 188 return; 209 }210 211 void H_BeamParticle::set4Momentum(const TLorentzVector& pmu) {212 /// @param pmu is the particle 4-momentum \f$ p^\mu \f$213 ///214 /// Clears the H_BeamParticle::positions vector.215 set4Momentum(pmu.Px(), pmu.Py(), pmu.Pz(), pmu.E());216 189 } 217 190 … … 242 215 } 243 216 217 void H_BeamParticle::emitGamma(const double gee, const double gq2) { 218 emitGamma(gee,gq2,0,2*PI); 219 return; 220 } 221 244 222 void H_BeamParticle::emitGamma(const double gee, const double gq2, const double phimin, const double phimax) { 245 223 /// @param gee = \f$ E_{\gamma} \f$ is the photon energy … … 271 249 272 250 if( (gq2>q2max) || (gq2<q2min)) { 273 if(VERBOSE) cout<<" <H_BeamParticle> WARNING :Non physical particle ! Q2 (" << q2 << ") and E ("<<gee << ") are not compatible." << endl;251 if(VERBOSE) cout<<"Non physical particle ! Q2 (" << q2 << ") and E ("<<gee << ") are not compatible." << endl; 274 252 isphysical = false; 275 253 } 276 254 277 if(hasemitted) { cout<<" <H_BeamParticle> WARNING :particle has already emitted at least one gamma !"<<endl;}255 if(hasemitted) { cout<<"particle has already emitted at least one gamma !"<<endl;} 278 256 hasemitted = true; 279 257 energy = energy - gee; … … 330 308 } 331 309 332 std::ostream& operator<< (std::ostream& os, const H_BeamParticle& p){333 os << " M = " << p.getM() << "GeV ";334 os << " Q = " << p.getQ() << "e";335 os << " fx = " << p.getX() << "m ";336 os << " fy = " << p.getY() << "m ";337 os << " thx = " << p.getTX() << "rad ";338 os << " thy = " << p.getTY() << "rad ";339 os<< endl;340 return os;310 void H_BeamParticle::printProperties() const { 311 cout << " M = " << getM() << "GeV "; 312 cout << " Q = " << getQ() << "e"; 313 cout << " fx = " << getX() << "m "; 314 cout << " fy = " << getY() << "m "; 315 cout << " thx = " << getTX() << "rad "; 316 cout << " thy = " << getTY() << "rad "; 317 cout << endl; 318 return; 341 319 } 342 320 … … 378 356 if((*position_i)[INDEX_S]>=position) { 379 357 if(position_i==positions.begin()) { 380 if(VERBOSE) cout<<" <H_BeamParticle>ERROR : non reachable value"<<endl;358 if(VERBOSE) cout<<"ERROR : non reachable value"<<endl; 381 359 return; 382 360 } 383 361 l = (*position_i)[INDEX_S] - (*(position_i-1))[INDEX_S]; 384 362 if(l==0) { 385 if(VERBOSE) cout<<" <H_BeamParticle>WARNING : no luck in choosing position, no propagation done"<<endl;363 if(VERBOSE) cout<<"WARNING : no luck in choosing position, no propagation done"<<endl; 386 364 return; 387 365 } … … 396 374 position_i = positions.begin(); 397 375 cout << "Desired position is : " << position << " & positions.begin() is " << (*position_i)[INDEX_S] << endl; 398 cout<<" <H_BeamParticle>ERROR : position not reachable"<<endl;376 cout<<"ERROR : position not reachable"<<endl; 399 377 return; 400 378 } 401 379 } 402 380 403 /// Caution : do not use this method (obsolete)!!!381 /// Caution : do not use this method !!! 404 382 void H_BeamParticle::propagate(const H_AbstractBeamLine * beam, const H_OpticalElement * element) { 405 383 TMatrixD X(*getV()); 406 X *= beam->getPartialMatrix(element);384 X *= *(beam->getPartialMatrix(element)); 407 385 fx = URAD*(X.GetMatrixArray())[0]; 408 386 thx = URAD*atan((X.GetMatrixArray())[1]); … … 412 390 } 413 391 414 void H_BeamParticle::propagate(const H_AbstractBeamLine * beam, const string &el_name) {415 propagate(beam->getElement(el_name)->getS() +beam->getElement(el_name)->getLength());392 void H_BeamParticle::propagate(const H_AbstractBeamLine * beam, const string el_name) { 393 propagate(beam->getElement(el_name)->getS()); 416 394 return; 417 395 } … … 419 397 void H_BeamParticle::propagate(const H_AbstractBeamLine * beam) { 420 398 TMatrixD X(*getV()); 421 X *= beam->getBeamMatrix();399 X *= *(beam->getBeamMatrix()); 422 400 fx = URAD*(X.GetMatrixArray())[0]; 423 401 thx = URAD*atan((X.GetMatrixArray())[1]); … … 436 414 return ; 437 415 } 438 416 /* 439 417 TGraph * H_BeamParticle::getPath(const int x_or_y, const int color) const{ 440 418 /// @param x_or_y = 0(1) draws the x(y) component; 441 419 442 420 const int N = (int) positions.size(); 443 444 if(N<2) cout<<" <H_BeamParticle> WARNING :particle positions not calculated : please run computePath"<<endl;421 int mycolor = color; 422 if(N<2) cout<<"particle positions not calculated : please run computePath"<<endl; 445 423 double * s = new double[N], * graph = new double[N]; 446 424 … … 461 439 } 462 440 463 void H_BeamParticle::getPath(const int x_or_y, const string& filename) const{ 464 /// @param x_or_y = 0(1) draws the x(y) component; 465 ofstream outfile(filename.c_str()); 466 467 int index; 468 if(x_or_y==0) {index = INDEX_X;} else {index = INDEX_Y;} 441 TPolyLine3D * H_BeamParticle::getPath3D(const H_AbstractBeamLine * beam, const bool isfirst, const int color, const int side) const{ 442 const int N = (int) positions.size(); 443 int mycolor = color; 444 if(N<2) cout<<"WARNING : particle positions not calculated. Run computePath"<<endl; 445 double * s = new double[N], * graphx = new double[N], * graphy = new double[N]; 446 int direction = (side<0)?-1:1; 447 469 448 470 449 vector<TVectorD>::const_iterator position_i; 471 450 for(position_i = positions.begin(); position_i < positions.end(); position_i++) { 472 outfile << (*position_i)[index] << "\t" << (*position_i)[INDEX_S] << endl; 451 graphx[(int)(position_i-positions.begin())] = (*position_i)[INDEX_X]; 452 graphy[(int)(position_i-positions.begin())] = (*position_i)[INDEX_Y]; 453 s[(int)(position_i-positions.begin())] = (*position_i)[INDEX_S]*1000*direction; 473 454 } 474 outfile.close(); 475 } 476 455 456 float coi[3] = {beam->getLength()*(-1000),-10000,-5000}; 457 float cof[3] = {beam->getLength()*1000,10000,5000}; 458 TView *view = TView::CreateView(11); 459 view->SetRange(coi[0],coi[1],coi[2],cof[0],cof[1],cof[2]); 460 TPolyLine3D* ppath = new TPolyLine3D(N,s,graphx,graphy); 461 ppath->SetLineColor(mycolor); 462 463 if(isfirst) { 464 ppath->Draw(); 465 view->ShowAxis(); 466 } else { 467 ppath->Draw("same"); 468 } 469 470 delete [] s; 471 delete [] graphx; 472 delete [] graphy; 473 return ppath; 474 } // getPath3D 475 */ 476 void H_BeamParticle::computePath(const H_AbstractBeamLine * beam) { 477 computePath(beam,true); 478 } 477 479 478 480 // should be removed later, to keep only computePath(const H_AbstractBeamLine & , const bool) … … 505 507 for (int i=0; i<N; i++) { 506 508 const unsigned pos = i; 507 if(fs < beam->getElement(pos)->getS() && fs > beam->getElement(pos-1)->getS()) { 508 if(beam->getElement(pos-1)->getType()!=DRIFT) { 509 cout<<"Path starts inside element "<<beam->getElement(pos-1)->getName()<<endl; 510 } else { 511 cout<<"Path starts inside unnamed drift "<<endl; 512 } 513 H_OpticalElement* temp_el = beam->getElement(pos-1)->clone(); 514 temp_el->setS(fs); 515 temp_el->setLength(beam->getElement(pos)->getS() - fs); 516 mat[0][0] = mat[0][0] - temp_el->getX(); 517 mat[0][1] = mat[0][1] - tan(temp_el->getTX()); 518 mat[0][2] = mat[0][2] - temp_el->getY(); 519 mat[0][3] = mat[0][3] - tan(temp_el->getTY()); 520 mat *= temp_el->getMatrix(energy_loss,mp,qp); 521 mat[0][0] = mat[0][0] + temp_el->getX(); 522 mat[0][1] = mat[0][1] + tan(temp_el->getTX()); 523 mat[0][2] = mat[0][2] + temp_el->getY(); 524 mat[0][3] = mat[0][3] + tan(temp_el->getTY()); 525 xys[0] = mat.GetMatrixArray()[0]*URAD; 526 xys[1] = atan(mat.GetMatrixArray()[1])*URAD; 527 xys[2] = mat.GetMatrixArray()[2]*URAD; 528 xys[3] = atan(mat.GetMatrixArray()[3])*URAD; 529 xys[4] = temp_el->getS()+temp_el->getLength(); 530 addPosition(xys[0],xys[1],xys[2],xys[3],xys[4]); 531 if (temp_el) delete temp_el; 532 } 533 if(fs <= beam->getElement(pos)->getS()) { 534 mat[0][0] = mat[0][0] - beam->getElement(pos)->getX(); 535 mat[0][1] = mat[0][1] - tan(beam->getElement(pos)->getTX()/URAD)*URAD; 536 mat[0][2] = mat[0][2] - beam->getElement(pos)->getY(); 537 mat[0][3] = mat[0][3] - tan(beam->getElement(pos)->getTY()/URAD)*URAD; 538 mat *= beam->getElement(pos)->getMatrix(energy_loss,mp,qp); 539 mat[0][0] = mat[0][0] + beam->getElement(pos)->getX(); 540 mat[0][1] = mat[0][1] + tan(beam->getElement(pos)->getTX()/URAD)*URAD; 541 mat[0][2] = mat[0][2] + beam->getElement(pos)->getY(); 542 mat[0][3] = mat[0][3] + tan(beam->getElement(pos)->getTY()/URAD)*URAD; 543 xys[0] = mat.GetMatrixArray()[0]*URAD; 544 xys[1] = atan(mat.GetMatrixArray()[1])*URAD; 545 xys[2] = mat.GetMatrixArray()[2]*URAD; 546 xys[3] = atan(mat.GetMatrixArray()[3])*URAD; 547 xys[4] = beam->getElement(pos)->getS()+beam->getElement(pos)->getLength(); 548 addPosition(xys[0],xys[1],xys[2],xys[3],xys[4]); 549 } 550 } 551 } 552 553 // part about non-ip particle is not ready yet. use the above method in the meantime 509 mat[0][0] = mat[0][0] - beam->getElement(pos)->getX(); 510 mat[0][1] = mat[0][1] - tan(beam->getElement(pos)->getTX()/URAD)*URAD; 511 mat[0][2] = mat[0][2] - beam->getElement(pos)->getY(); 512 mat[0][3] = mat[0][3] - tan(beam->getElement(pos)->getTY()/URAD)*URAD; 513 mat *= beam->getElement(pos)->getMatrix(energy_loss,mp,qp); 514 mat[0][0] = mat[0][0] + beam->getElement(pos)->getX(); 515 mat[0][1] = mat[0][1] + tan(beam->getElement(pos)->getTX()/URAD)*URAD; 516 mat[0][2] = mat[0][2] + beam->getElement(pos)->getY(); 517 mat[0][3] = mat[0][3] + tan(beam->getElement(pos)->getTY()/URAD)*URAD; 518 xys[0] = mat.GetMatrixArray()[0]*URAD; 519 xys[1] = atan(mat.GetMatrixArray()[1])*URAD; 520 xys[2] = mat.GetMatrixArray()[2]*URAD; 521 xys[3] = atan(mat.GetMatrixArray()[3])*URAD; 522 xys[4] = beam->getElement(pos)->getS()+beam->getElement(pos)->getLength(); 523 addPosition(xys[0],xys[1],xys[2],xys[3],xys[4]); 524 fx = xys[0]; 525 fy = xys[2]; 526 thx = xys[1]; 527 thy = xys[3]; 528 } 529 } 530 554 531 void H_BeamParticle::computePath(const H_AbstractBeamLine & beam, const bool NonLinear) { 555 532 TMatrixD temp_mat(MDIM,MDIM); … … 578 555 double energy_loss = NonLinear?BE-energy:0; 579 556 580 // modify here to allow starting at non-IP positions581 // s is distance to IP582 // initial position is already in positions vector ?583 557 for (int i=0; i<N; i++) { 584 558 const unsigned pos = i; 585 // if we are inside an element, we should start by adding the action 586 // of the rest of this element 587 if(pos > 0 && fs < beam.getElement(pos)->getS() && fs > beam.getElement(pos-1)->getS()) { 588 cout<<"Path starts inside element "<<beam.getElement(pos-1)->getName()<<endl; 589 H_OpticalElement* temp_el = new H_OpticalElement(*(beam.getElement(pos-1))); 590 temp_el->setS(fs); 591 temp_el->setLength(beam.getElement(pos)->getS() - fs); 592 mat[0][0] = mat[0][0] - temp_el->getX(); 593 mat[0][1] = mat[0][1] - tan(temp_el->getTX()); 594 mat[0][2] = mat[0][2] - temp_el->getY(); 595 mat[0][3] = mat[0][3] - tan(temp_el->getTY()); 596 mat *= temp_el->getMatrix(energy_loss,mp,qp); 597 mat[0][0] = mat[0][0] + temp_el->getX(); 598 mat[0][1] = mat[0][1] + tan(temp_el->getTX()); 599 mat[0][2] = mat[0][2] + temp_el->getY(); 600 mat[0][3] = mat[0][3] + tan(temp_el->getTY()); 601 } else if(fs >= beam.getElement(pos)->getS()) { 602 mat[0][0] = mat[0][0] - beam.getElement(pos)->getX(); 603 mat[0][1] = mat[0][1] - tan(beam.getElement(pos)->getTX()); 604 mat[0][2] = mat[0][2] - beam.getElement(pos)->getY(); 605 mat[0][3] = mat[0][3] - tan(beam.getElement(pos)->getTY()); 606 mat *= beam.getElement(pos)->getMatrix(energy_loss,mp,qp); 607 mat[0][0] = mat[0][0] + beam.getElement(pos)->getX(); 608 mat[0][1] = mat[0][1] + tan(beam.getElement(pos)->getTX()); 609 mat[0][2] = mat[0][2] + beam.getElement(pos)->getY(); 610 mat[0][3] = mat[0][3] + tan(beam.getElement(pos)->getTY()); 611 } 612 xys[0] = mat.GetMatrixArray()[0]*URAD; 613 xys[1] = atan(mat.GetMatrixArray()[1])*URAD; 614 xys[2] = mat.GetMatrixArray()[2]*URAD; 615 xys[3] = atan(mat.GetMatrixArray()[3])*URAD; 616 xys[4] = beam.getElement(pos)->getS()+beam.getElement(pos)->getLength(); 617 addPosition(xys[0],xys[1],xys[2],xys[3],xys[4]); 618 fx = xys[0]; 619 fy = xys[2]; 620 thx = xys[1]; 621 thy = xys[3]; 622 } 559 mat[0][0] = mat[0][0] - beam.getElement(pos)->getX(); 560 mat[0][1] = mat[0][1] - tan(beam.getElement(pos)->getTX()); 561 mat[0][2] = mat[0][2] - beam.getElement(pos)->getY(); 562 mat[0][3] = mat[0][3] - tan(beam.getElement(pos)->getTY()); 563 mat *= beam.getElement(pos)->getMatrix(energy_loss,mp,qp); 564 mat[0][0] = mat[0][0] + beam.getElement(pos)->getX(); 565 mat[0][1] = mat[0][1] + tan(beam.getElement(pos)->getTX()); 566 mat[0][2] = mat[0][2] + beam.getElement(pos)->getY(); 567 mat[0][3] = mat[0][3] + tan(beam.getElement(pos)->getTY()); 568 xys[0] = mat.GetMatrixArray()[0]*URAD; 569 xys[1] = atan(mat.GetMatrixArray()[1])*URAD; 570 xys[2] = mat.GetMatrixArray()[2]*URAD; 571 xys[3] = atan(mat.GetMatrixArray()[3])*URAD; 572 xys[4] = beam.getElement(pos)->getS()+beam.getElement(pos)->getLength(); 573 addPosition(xys[0],xys[1],xys[2],xys[3],xys[4]); 574 fx = xys[0]; 575 fy = xys[2]; 576 thx = xys[1]; 577 thy = xys[3]; 578 } 623 579 } 624 580
Note:
See TracChangeset
for help on using the changeset viewer.