src/ggmad.cc

00001 #include "BDSGlobalConstants.hh" 
00002 #include "BDSElement.hh"
00003 #include "BDSDebug.hh"
00004 #include "BDSAcceleratorComponent.hh"
00005 #include "ggmad.hh"
00006 
00007 #include "G4Box.hh"
00008 #include "G4Tubs.hh"
00009 #include "G4Cons.hh"
00010 #include "G4Trd.hh"
00011 #include "G4VisAttributes.hh"
00012 #include "G4LogicalVolume.hh"
00013 #include "G4VPhysicalVolume.hh"
00014 #include "G4PVPlacement.hh"
00015 #include "G4UserLimits.hh"
00016 
00017 
00018 GGmadDriver::GGmadDriver(G4String file)
00019 {
00020   G4cout << __METHOD_NAME__ << "> reading file : " << file << G4endl;
00021   inputf.open(file);
00022   if(!inputf.good()) {G4cerr<<"ggmad driver: error  opening input file "<<file<<G4endl; exit(1);}
00023 }
00024 
00025 void GGmadDriver::Construct(G4LogicalVolume *marker)
00026 {
00027   
00028   G4String token;
00029   G4String token1;
00030 
00031   G4double x0=0, y0=0, z0=0, x=0, y=0, z=0; // box parameters
00032   G4double rmin=0, rmax=0, rmin2=0, rmax2=0, phi0=0, dphi=0; // cylindrical parameters
00033   G4double x1=0, x2=0, y1=0, y2=0;
00034   G4double phi=0, theta=0, psi=0; // Euler angles - for rotation of components
00035   G4String material;
00036   // G4double FieldX, FieldY, FieldZ;
00037   // FieldX = FieldY = FieldZ = 0.0;
00038 
00039   G4Material *theMaterial;
00040   G4Box *aBox;
00041   G4Tubs *aTubs;
00042   G4Cons *aCons;
00043   //G4Trap *aTrap;
00044   G4Trd *aTrd;
00045 
00046   G4LogicalVolume *lvol;
00047 
00048   G4VisAttributes *visAttr = new G4VisAttributes(true, G4Colour(0.2,0.2,0.2));
00049   visAttr->SetForceSolid(true);
00050                                                  
00051 
00052   G4int count = 0;
00053 
00054   while (inputf.good())
00055     {
00056       token = getWord();
00057  
00058       if(token=="Box") {
00059         if(getWord() == "{")
00060           {
00061             
00062             while((token = getWord()))
00063               {
00064                 if (token == "}") break;
00065                 
00066                 getParameter(x0,"x0",token); // origin
00067                 getParameter(y0,"y0",token);
00068                 getParameter(z0,"z0",token);
00069                 getParameter(x,"x",token); //half lengths
00070                 getParameter(y,"y",token);
00071                 getParameter(z,"z",token);
00072                 getParameter(phi,"phi",token);
00073                 getParameter(theta,"theta",token);
00074                 getParameter(psi,"psi",token);
00075                 getParameter(material,"material",token);
00076               }
00077 
00078             //create Box
00079 
00080             theMaterial = BDSMaterials::Instance()->GetMaterial(material);
00081             
00082           
00083             G4cout<<"creating box : "<<x0<<"  "<<y0<<" "<<z0<<G4endl;
00084 
00085 
00086             aBox = new G4Box("aBox" + G4String(count),
00087                              x,   // half x
00088                              y, // half y
00089                              z ); // half z
00090             
00091             lvol = new G4LogicalVolume(aBox,
00092                         theMaterial,
00093                         "_bmp_logical");
00094 
00095 
00096             // place into mother volume
00097 
00098             G4RotationMatrix *rot = new G4RotationMatrix;
00099             rot->rotateX(2*pi*phi/360.);
00100             rot->rotateY(2*pi*theta/360.);
00101             rot->rotateZ(2*pi*psi/360.);
00102 
00103             // G4VPhysicalVolume* PhysiComp =
00104             new G4PVPlacement(
00105               rot,                     // rotation
00106               G4ThreeVector(x0,y0,z0), // at (x0,y0,z0)
00107               lvol,                    // its logical volume
00108               "vol_"+G4String(count),  // its name
00109               marker,                  // its mother  volume
00110               false,                   // no boolean operation
00111               0, BDSGlobalConstants::Instance()->GetCheckOverlaps());
00112             count++;
00113 
00114           } else error();
00115         
00116       }// G4Box
00117 
00118       if(token=="Tubs") {
00119         if(getWord() == "{")
00120           {
00121             
00122             while((token = getWord()))
00123               {
00124                 if (token == "}") break;
00125                 
00126                 getParameter(x0,"x0",token); // origin
00127                 getParameter(y0,"y0",token);
00128                 getParameter(z0,"z0",token);
00129                 getParameter(rmin,"rmin",token); //half lengths
00130                 getParameter(rmax,"rmax",token);
00131                 getParameter(z,"z",token);
00132                 getParameter(phi0,"phi0",token);
00133                 getParameter(dphi,"dphi",token);
00134                 getParameter(phi,"phi",token);
00135                 getParameter(theta,"theta",token);
00136                 getParameter(psi,"psi",token);
00137                 getParameter(material,"material",token);
00138               }
00139             
00140             //create Box
00141             
00142             theMaterial = BDSMaterials::Instance()->GetMaterial(material);
00143             
00144             
00145             G4cout<<"creating tubs : "<<x0<<"  "<<y0<<" "<<z0<<G4endl;
00146             
00147             // default - tube
00148             phi0 = 0;
00149             dphi = 360; // degrees
00150             
00151             aTubs = new G4Tubs("aTubs" + G4String(count),
00152                                rmin,   // inner R
00153                                rmax, // outer R
00154                                z, //z
00155                                phi0,//phi 0 
00156                                2 * pi * dphi / 360  ); //delta phi
00157             
00158             lvol = new G4LogicalVolume(aTubs,
00159                                        theMaterial,
00160                                        "_bmp_logical");
00161             
00162             
00163             // place into mother volume
00164             
00165             G4RotationMatrix *rot = new G4RotationMatrix;
00166             rot->rotateX(2*pi*phi/360.);
00167             rot->rotateY(2*pi*theta/360.);
00168             rot->rotateZ(2*pi*psi/360.);
00169 
00170             // G4VPhysicalVolume* PhysiComp =
00171             new G4PVPlacement(
00172                               rot,                           // rotation
00173                               G4ThreeVector(x0,y0,z0),     // at (0,0,0)
00174                               lvol,  // its logical volume
00175                               "vol_"+G4String(count),        // its name
00176                               marker,     // its mother  volume
00177                               false,                 // no boolean operation
00178                               0, BDSGlobalConstants::Instance()->GetCheckOverlaps());
00179             
00180             count++;
00181             
00182           } else error();
00183       }
00184 
00185       if(token=="Cons") {
00186         if(getWord() == "{")
00187           {
00188             
00189             while((token = getWord()))
00190               {
00191                 if (token == "}") break;
00192                 
00193                 getParameter(x0,"x0",token); // origin
00194                 getParameter(y0,"y0",token);
00195                 getParameter(z0,"z0",token);
00196                 getParameter(rmin,"rmin1",token); //half lengths
00197                 getParameter(rmax,"rmax1",token);
00198                 getParameter(rmin2,"rmin2",token); //half lengths
00199                 getParameter(rmax2,"rmax2",token);
00200                 getParameter(z,"z",token);
00201                 getParameter(phi0,"phi0",token);
00202                 getParameter(dphi,"dphi",token);
00203                 getParameter(phi,"phi",token);
00204                 getParameter(theta,"theta",token);
00205                 getParameter(psi,"psi",token);
00206                 getParameter(material,"material",token);
00207               }
00208             
00209             //create Box
00210             
00211             theMaterial = BDSMaterials::Instance()->GetMaterial(material);
00212             
00213             
00214             G4cout<<"creating cons : "<<x0<<"  "<<y0<<" "<<z0<<G4endl;
00215             G4cout<<"rotation : "<<phi<<"  "<<theta<<" "<<psi<<G4endl;
00216             
00217             // default - tube
00218             phi0 = 0;
00219             dphi = 360; // degrees
00220             
00221             aCons = new G4Cons("aCons" + G4String(count),
00222                                rmin,   // inner R
00223                                rmax,
00224                                rmin2,
00225                                rmax2,
00226                                z, //z
00227                                phi0,//phi 0 
00228                                2 * pi * dphi / 360  ); //delta phi
00229             
00230             lvol = new G4LogicalVolume(aCons,
00231                                        theMaterial,
00232                                        "_bmp_logical");
00233 
00234             lvol->SetVisAttributes( visAttr );
00235             
00236             
00237             // place into mother volume
00238 
00239             G4RotationMatrix *rot = new G4RotationMatrix;
00240             rot->rotateX(2*pi*phi/360.);
00241             rot->rotateY(2*pi*theta/360.);
00242             rot->rotateZ(2*pi*psi/360.);
00243 
00244             // G4VPhysicalVolume* PhysiComp =
00245             new G4PVPlacement(
00246                               rot,                           // rotation
00247                               G4ThreeVector(x0,y0,z0),     // at (0,0,0)
00248                               lvol,  // its logical volume
00249                               "vol_"+G4String(count),        // its name
00250                               marker,     // its mother  volume
00251                               false,                 // no boolean operation
00252                               0, BDSGlobalConstants::Instance()->GetCheckOverlaps());
00253             
00254             count++;
00255             
00256           } else error();
00257       }
00258       if(token=="Trd") {   // trapezoid
00259         if(getWord() == "{")
00260           {
00261             
00262             while((token = getWord()))
00263               {
00264                 if (token == "}") break;
00265                 
00266                 getParameter(x0,"x0",token); // origin
00267                 getParameter(y0,"y0",token);
00268                 getParameter(z0,"z0",token);
00269                 getParameter(x1,"x1",token); //half length at wider side
00270                 getParameter(x2,"x2",token); //half length at narrow side
00271                 getParameter(y1,"y1",token); //half lengths
00272                 getParameter(y2,"y2",token); //half lengths
00273                 getParameter(z,"z",token);
00274                 getParameter(phi,"phi",token);
00275                 getParameter(theta,"theta",token);
00276                 getParameter(psi,"psi",token);
00277                 getParameter(material,"material",token);
00278               }
00279             
00280             //create Box
00281             
00282             theMaterial = BDSMaterials::Instance()->GetMaterial(material);
00283             
00284             
00285             G4cout<<"creating trd : "<<x0<<"  "<<y0<<" "<<z0<<
00286                   z<<" "<<y<<" "<<x1<<" "<<x2<<G4endl;
00287             
00288             
00289             aTrd = new G4Trd("aTrd" + G4String(count),
00290                              x1,x2,   // inner R
00291                              y1,y2,
00292                              z);
00293             
00294             lvol = new G4LogicalVolume(aTrd,
00295                                        theMaterial,
00296                                        "_bmp_logical");
00297 
00298             // place into mother volume
00299 
00300             G4RotationMatrix *rot = new G4RotationMatrix;
00301             rot->rotateX(2*pi*phi/360.);
00302             rot->rotateY(2*pi*theta/360.);
00303             rot->rotateZ(2*pi*psi/360.);
00304             
00305             
00306             // G4VPhysicalVolume* PhysiComp =
00307             new G4PVPlacement(
00308                               rot,                           // rotation
00309                               G4ThreeVector(x0,y0,z0),     // at (0,0,0)
00310                               lvol,  // its logical volume
00311                               "vol_"+G4String(count),        // its name
00312                               marker,     // its mother  volume
00313                               false,                 // no boolean operation
00314                               0, BDSGlobalConstants::Instance()->GetCheckOverlaps());
00315             
00316             count++;
00317             
00318           } else error();
00319       }
00320     }
00321 
00322 
00323   return;
00324 }
00325 
00326 
00327 G4String GGmadDriver::getWord()
00328 {
00329 
00330   G4String str="";
00331   char c = 'a';
00332 
00333   while (inputf.good())     // loop while extraction from file is possible
00334   {
00335     inputf.get(c);       // get character from file
00336 
00337     // return char tokens 
00338     if(c=='=') return G4String(c);
00339 
00340     // skip whitespace
00341     if( (c != ' ' ) && (c != '\t' )&& (c != ',' ) && (c != '\n' )) break;
00342   }
00343 
00344   while (inputf.good())     // loop while extraction from file is possible
00345   {
00346     str += c;
00347     inputf.get(c);       // get character from file
00348     if( (c == ' ' ) || (c == '\t' )|| (c == ',' )|| (c == '\n' )|| (c == '=' ) ) 
00349       {
00350         inputf.putback(c);
00351         break;
00352       }
00353   }
00354 
00355   return str;
00356 
00357 }
00358 
00359 void GGmadDriver::getParameter(G4double& x, G4String name, G4String lastToken)
00360 {
00361   G4String token;
00362 
00363   if(lastToken == name)
00364     {
00365       token = getWord();
00366       if(token == "=")
00367         {
00368           token = getWord();
00369           x = strtod(token.c_str(),NULL);
00370         }
00371     }
00372 }
00373 
00374 void GGmadDriver::getParameter(G4String& lval, G4String name, G4String lastToken)
00375 {
00376   G4String token;
00377 
00378   if(lastToken == name)
00379     {
00380       token = getWord();
00381       if(token == "=")
00382         {
00383           token = getWord();
00384           lval = token;
00385         }
00386     }
00387 }

Generated on 27 Aug 2013 for BDSIM by  doxygen 1.4.7