Changeset 1365 in svn for trunk/external/Hector/H_AbstractBeamLine.cc
- Timestamp:
- Apr 16, 2014, 3:56:14 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/external/Hector/H_AbstractBeamLine.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 */ 11 18 12 19 13 /// \file H_AbstractBeamLine.cc … … 27 21 28 22 // ROOT #includes 29 #include "TPaveLabel.h"30 #include "TLine.h"31 #include "TGaxis.h"32 #include "TLegend.h"33 #include "TF1.h"34 #include "TROOT.h"23 //#include "TPaveLabel.h" 24 //#include "TLine.h" 25 //#include "TGaxis.h" 26 //#include "TLegend.h" 27 //#include "TF1.h" 28 //#include "TROOT.h" 35 29 36 30 // local #includes … … 39 33 #include "H_Drift.h" 40 34 #include "H_AbstractBeamLine.h" 35 #include "H_BeamParticle.h" 41 36 #include "H_RomanPot.h" 42 37 using namespace std; 43 38 44 39 void H_AbstractBeamLine::init(const float length) { 45 beam_mat.ResizeTo(MDIM,MDIM); 46 beam_mat = driftmat(length); 40 beam_mat = new TMatrix(MDIM,MDIM); 47 41 beam_length = length; 48 42 H_Drift * drift0 = new H_Drift("Drift0",0.,length); … … 51 45 } 52 46 53 H_AbstractBeamLine::H_AbstractBeamLine(const H_AbstractBeamLine& beamline) : 54 matrices(beamline.matrices) { 55 //elements = beamline.elements; //<-- bad ! the new vector contains the same pointers as the previous one 56 cloneElements(beamline); 57 beam_mat.ResizeTo(MDIM,MDIM); 58 beam_mat = beamline.beam_mat; 47 H_AbstractBeamLine::H_AbstractBeamLine(const H_AbstractBeamLine& beamline) { 48 elements = beamline.elements; 49 matrices = beamline.matrices; 50 beam_mat = new TMatrix(*(beamline.beam_mat)); 59 51 beam_length = beamline.beam_length; 60 52 } … … 62 54 H_AbstractBeamLine& H_AbstractBeamLine::operator=(const H_AbstractBeamLine& beamline) { 63 55 if(this== &beamline) return *this; 64 //elements = beamline.elements; //<-- bad ! the new vector contains the same pointers as the previous one 65 cloneElements(beamline); 56 elements = beamline.elements; 66 57 matrices = beamline.matrices; 67 beam_mat = beamline.beam_mat;58 beam_mat = new TMatrix(*(beamline.beam_mat)); 68 59 beam_length = beamline.beam_length; 69 60 return *this; 70 }71 72 H_AbstractBeamLine* H_AbstractBeamLine::clone() const {73 H_AbstractBeamLine* temp_beam = new H_AbstractBeamLine(beam_length);74 vector<H_OpticalElement*>::const_iterator element_i;75 for (element_i = elements.begin(); element_i<elements.end(); element_i++) {76 if((*element_i)->getType()!=DRIFT) {77 H_OpticalElement* temp_el = (*element_i)->clone();78 temp_beam->add(temp_el);79 }80 }81 temp_beam->beam_mat = beam_mat;82 temp_beam->matrices = matrices;83 return temp_beam;84 61 } 85 62 … … 91 68 elements.clear(); 92 69 matrices.clear(); 93 } 94 95 void H_AbstractBeamLine::cloneElements(const H_AbstractBeamLine& beam) { 96 vector<H_OpticalElement*>::const_iterator element_i; 97 for (element_i = beam.elements.begin(); element_i< beam.elements.end(); element_i++) { 98 H_OpticalElement* temp_el = (*element_i)->clone(); 99 elements.push_back(temp_el); 100 } 70 delete beam_mat; 101 71 } 102 72 … … 110 80 if (a > beam_length) { 111 81 beam_length = a; 112 if(VERBOSE) cout<<" <H_AbstractBeamLine>WARNING : element ("<< newElement->getName()<<") too far away. The beam length has been extended to "<< beam_length << ". "<<endl;82 if(VERBOSE) cout<<"WARNING : element ("<< newElement->getName()<<") too far away. The beam length has been extended to "<< beam_length << ". "<<endl; 113 83 } 114 84 calcSequence(); 115 85 calcMatrix(); 116 return; 117 } 118 119 const TMatrix H_AbstractBeamLine::getBeamMatrix() const { 120 return beam_mat; 121 } 122 123 const TMatrix H_AbstractBeamLine::getBeamMatrix(const float eloss,const float p_mass, const float p_charge) { 86 } 87 88 void H_AbstractBeamLine::add(H_OpticalElement & newElement) { 89 /// @param newElement is added to the beamline 90 // H_OpticalElement * el = new H_OpticalElement(newElement); 91 // elements.push_back(el); 92 elements.push_back(&newElement); 93 float a = newElement.getS()+newElement.getLength(); 94 if (a > beam_length) { 95 beam_length = a; 96 if(VERBOSE) cout<<"WARNING : element ("<< newElement.getName()<<") too far away. The beam length has been extended to "<< beam_length << ". "<<endl; 97 } 98 calcSequence(); 99 calcMatrix(); 100 } 101 102 const TMatrix * H_AbstractBeamLine::getBeamMatrix() const { 103 TMatrix * mat = new TMatrix(*beam_mat); 104 return mat; 105 } 106 107 const TMatrix * H_AbstractBeamLine::getBeamMatrix(const float eloss,const float p_mass, const float p_charge) { 124 108 125 109 vector<H_OpticalElement*>::iterator element_i; … … 134 118 calc_mat *= (*element_i)->getMatrix(eloss,p_mass,p_charge); 135 119 } 136 return calc_mat; 137 } 138 139 const TMatrix H_AbstractBeamLine::getPartialMatrix(const string& elname, const float eloss, const float p_mass, const float p_charge) { 120 const TMatrix* bmat = new TMatrix(calc_mat); 121 return bmat; 122 } 123 124 const TMatrix * H_AbstractBeamLine::getPartialMatrix(const string elname, const float eloss, const float p_mass, const float p_charge) { 140 125 141 126 vector<H_OpticalElement*>::iterator element_i; … … 147 132 calc_mat *= (*element_i)->getMatrix(eloss,p_mass,p_charge); 148 133 if(elname==(*element_i)->getName()) { 149 return calc_mat; 150 } 151 } 152 cout<<"<H_AbstractBeamLine> Element "<<elname<<" desn't exist. Returning full beam matrix"<<endl; 153 return calc_mat; 154 } 155 156 const TMatrix H_AbstractBeamLine::getPartialMatrix(const unsigned int element_position) const { 134 const TMatrix* bmat = new TMatrix(calc_mat); 135 return bmat; 136 } 137 } 138 cout<<"Element "<<elname<<" desn't exist. Returning full beam matrix"<<endl; 139 const TMatrix* bmat = new TMatrix(calc_mat); 140 return bmat; 141 } 142 143 const TMatrix * H_AbstractBeamLine::getPartialMatrix(const unsigned int element_position) const { 157 144 //const int N = (element_position<0)?0:(( (element_position)>elements.size()-1)?elements.size()-1:element_position); 158 145 const int N = (element_position>elements.size()-1)?elements.size()-1:element_position; 159 return *(matrices.begin()+N); // //for optimization of the code :same as return &matrices[N];160 } 161 162 const TMatrix H_AbstractBeamLine::getPartialMatrix(const H_OpticalElement * element) const{146 return &(*(matrices.begin()+N)); // //for optimization of the code :same as return &matrices[N]; 147 } 148 149 const TMatrix * H_AbstractBeamLine::getPartialMatrix(const H_OpticalElement * element) const{ 163 150 // returns the transport matrix to transport until the end of the specified element 164 151 // !!! 2 elements should never have the same name in "elements" !!! … … 166 153 vector<H_OpticalElement*>::const_iterator element_i; 167 154 vector<TMatrix>::const_iterator matrix_i; 168 TMatrix calc_mat(MDIM,MDIM);155 TMatrix * calc_mat = new TMatrix(MDIM,MDIM); 169 156 170 157 // parses the list of optical elements and find the searched one … … 172 159 if(element->getName() == (*element_i)->getName()) { 173 160 // element has been found 174 calc_mat = *matrix_i;161 calc_mat = const_cast<TMatrix*>( &(*matrix_i)); 175 162 } 176 163 } 177 return calc_mat;164 return (const TMatrix*) calc_mat; 178 165 } 179 166 … … 183 170 } 184 171 185 H_OpticalElement * H_AbstractBeamLine::getElement(const unsigned int element_position) const {172 const H_OpticalElement * H_AbstractBeamLine::getElement(const unsigned int element_position) const { 186 173 const unsigned int N = (element_position>elements.size())?elements.size():element_position; 187 174 return *(elements.begin()+N);//for optimization of the code :same as return &elements[N]; … … 189 176 190 177 191 H_OpticalElement * H_AbstractBeamLine::getElement(const string &el_name) {178 H_OpticalElement * H_AbstractBeamLine::getElement(const string el_name) { 192 179 for(unsigned int i=0; i < elements.size(); i++) { 193 180 if( (*(elements.begin()+i))->getName() == el_name ) 194 181 return *(elements.begin()+i); 195 182 } // if found -> return ; else : not found at all ! 196 cout<<" <H_AbstractBeamLine>Element "<<el_name<<" not found"<<endl;183 cout<<"Element "<<el_name<<" not found"<<endl; 197 184 return *(elements.begin()+1); 198 185 } 199 186 200 H_OpticalElement * H_AbstractBeamLine::getElement(const string&el_name) const {187 const H_OpticalElement * H_AbstractBeamLine::getElement(const string el_name) const { 201 188 for(unsigned int i=0; i < elements.size(); i++) { 202 189 if( (*(elements.begin()+i))->getName() == el_name) 203 190 return *(elements.begin()+i); 204 191 } // if found -> return ; else : not found at all ! 205 cout<<" <H_AbstractBeamLine>Element "<<el_name<<" not found"<<endl;192 cout<<"Element "<<el_name<<" not found"<<endl; 206 193 return *(elements.begin()+1); 207 194 } 208 195 209 std::ostream& operator<< (std::ostream& os, const H_AbstractBeamLine& be) { 210 vector<H_OpticalElement*>::const_iterator element_i; 211 os << "Beamline content" << endl; 212 for (element_i = be.elements.begin(); element_i < be.elements.end(); element_i++) { 213 os << (int)(element_i - be.elements.begin()) << "\t" << (*element_i)->getName() << "\t" << (*element_i)->getS() << endl; 214 } 215 return os; 216 } 217 218 void H_AbstractBeamLine::printProperties() const { cout << *this; return; } 196 void H_AbstractBeamLine::printProperties() const { 197 vector<H_OpticalElement*>::const_iterator element_i; 198 cout << "Pointeurs des elements du faisceau" << endl; 199 for (element_i = elements.begin(); element_i < elements.end(); element_i++) { 200 cout << (int)(element_i-elements.begin()) << "\t" << (*element_i)->getName() << "\t" << (*element_i)->getS() << endl; 201 } 202 return; 203 } 219 204 220 205 void H_AbstractBeamLine::showElements() const{ … … 254 239 temp = *matrix_i; 255 240 cout << "Matrix for transport until s=" << (*element_i)->getS() + (*element_i)->getLength() << "m (" << (*element_i)->getName() << "). " << endl; 256 printMatrix( temp);241 printMatrix(&temp); 257 242 cout << endl; 258 243 } … … 271 256 // getting rid of drifts before calculating 272 257 for(element_i = elements.begin(); element_i < elements.end(); element_i++) { 273 if((*element_i)->getType() == DRIFT) { delete (*element_i);elements.erase(element_i); }258 if((*element_i)->getType() == DRIFT) {elements.erase(element_i); } 274 259 } 275 260 … … 296 281 temp_elements.push_back(dr); 297 282 } 298 299 // cleaning : avoid some memory leaks300 283 elements.clear(); 301 302 284 for(element_i=temp_elements.begin(); element_i < temp_elements.end(); element_i++) { 303 285 elements.push_back(*element_i); … … 321 303 } 322 304 323 beam_mat.ResizeTo(MDIM,MDIM); 324 beam_mat = calc_mat; 305 *beam_mat = calc_mat; 325 306 return; 326 307 } … … 338 319 } 339 320 340 void H_AbstractBeamLine::draw( const float xmin, const float ymin, const float xmax, const float ymax) const{341 gROOT->SetStyle("Plain");342 TLegend* leg = new TLegend( xmin,ymin,xmax,ymax,"");321 void H_AbstractBeamLine::draw() const{ 322 /* gROOT->SetStyle("Plain"); 323 TLegend* leg = new TLegend(0.85,0.50,1,1,"Legend"); 343 324 leg->SetBorderSize(1); 344 leg->SetFillColor(kWhite);345 325 TBox* b1 = new TBox(); 346 326 TBox* b2 = new TBox(0,0,10,10); … … 365 345 leg->AddEntry(b7,"RCollimator"); 366 346 leg->Draw(); 347 */ 367 348 /* TLine* l1 = new TLine(0.05,0.5,0.95,0.5); 368 349 TLine* l2 = new TLine(0.1,0.1,0.1,0.9); … … 413 394 } 414 395 415 void H_AbstractBeamLine::drawX(const float a_min, const float a_max , const float scale) const{396 void H_AbstractBeamLine::drawX(const float a_min, const float a_max) const{ 416 397 /// @param a_min defines the size of the drawing 417 398 /// @param a_max defines the size of the drawing 418 /// @param scale allows to multiply the drawing, i.e. changing the units419 399 const int N = getNumberOfElements(); 420 400 for(int i=0;i<N;i++) { … … 422 402 float meight = fabs(a_min); 423 403 float size = (height>meight)?meight:height; 424 float middle = getElement(i)->getX()*URAD *scale;404 float middle = getElement(i)->getX()*URAD; 425 405 if(getElement(i)->getType()!=DRIFT) getElement(i)->draw(middle+size/2.,middle-size/2.); 426 406 } … … 440 420 } 441 421 442 void H_AbstractBeamLine::moveElement(const string &name, const float new_s) {422 void H_AbstractBeamLine::moveElement(const string name, const float new_s) { 443 423 /// @param name identifies the element to move 444 424 /// @param new_s is where to put it … … 453 433 } 454 434 455 void H_AbstractBeamLine::alignElement(const string &name, const float disp_x, const float disp_y) {435 void H_AbstractBeamLine::alignElement(const string name, const float disp_x, const float disp_y) { 456 436 /// @param name identifies the element to move 457 437 /// @param disp_x identifies the displacement to add in x [\f$ \mu m \f$] … … 465 445 } 466 446 } 467 cout<<"<H_AbstractBeamLine> WARNING : Element "<<name<<" not found."<<endl; 447 cout<<"Element "<<name<<" not found."<<endl; 448 if(VERBOSE) cout<<"Element "<<name<<" not found."<<endl; 468 449 return; 469 450 } 470 451 471 void H_AbstractBeamLine::tiltElement(const string &name, const float ang_x, const float ang_y) {452 void H_AbstractBeamLine::tiltElement(const string name, const float ang_x, const float ang_y) { 472 453 /// @param name identifies the element to move 473 454 /// @param ang_x identifies the angle to add in x … … 481 462 } 482 463 } 483 cout<<"<H_AbstractBeamLine> WARNING : Element "<<name<<" not found."<<endl;464 if(VERBOSE) cout<<"Element "<<name<<" not found."<<endl; 484 465 return; 485 466 } … … 500 481 } 501 482 483 /* 502 484 TGraph * H_AbstractBeamLine::getBetaX() const{ 503 485 const int N = elements.size(); … … 648 630 return rely; 649 631 } 650 632 */
Note:
See TracChangeset
for help on using the changeset viewer.