Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-31 09:22:18

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
0024 // ********************************************************************
0025 //
0026 // This is the *BASIC* version of Hadrontherapy, a Geant4-based application
0027 // See more at: http://g4advancedexamples.lngs.infn.it/Examples/hadrontherapy
0028 //
0029 // Visit the Hadrontherapy web site (http://www.lns.infn.it/link/Hadrontherapy) to request 
0030 // the *COMPLETE* version of this program, together with its documentation;
0031 // Hadrontherapy (both basic and full version) are supported by the Italian INFN
0032 // Institute in the framework of the MC-INFN Group
0033 //
0034 
0035 #include <fstream>
0036 
0037 #include "globals.hh"
0038 #include "G4SystemOfUnits.hh"
0039 #include "G4Material.hh"
0040 #include "G4Tubs.hh"
0041 #include "G4Box.hh"
0042 #include "G4LogicalVolume.hh"
0043 #include "G4VPhysicalVolume.hh"
0044 #include "G4ThreeVector.hh"
0045 #include "G4PVPlacement.hh"
0046 #include "G4Transform3D.hh"
0047 #include "G4RotationMatrix.hh"
0048 #include "G4VisAttributes.hh"
0049 #include "G4Colour.hh"
0050 #include "HadrontherapyModulator.hh"
0051 #include "G4Transform3D.hh"
0052 #include "G4ios.hh"
0053 #include "G4RunManager.hh"
0054 #include "G4NistManager.hh"
0055 #include <iostream>
0056 
0057 HadrontherapyModulator::HadrontherapyModulator(): physiMotherMod(0),
0058                          solidMod1(0),         logicMod1(0),          physiMod1(0),
0059                          solidMod2(0),         logicMod2(0),          physiMod2(0),
0060                          solidMod3(0),         logicMod3(0),          physiMod3(0),
0061                          solidMod4(0),         logicMod4(0),          physiMod4(0),
0062                          FileName("Modulators/Modulator010.txt")
0063 { 
0064    pi=4*std::atan(1.);
0065    StepNumbers=22;
0066    Weight=new G4double[StepNumbers];
0067    StepThickness=new G4double[StepNumbers];
0068    StartingAngle=new G4double[StepNumbers];
0069    SpanningAngle=new G4double[StepNumbers];
0070    PositionMod=new G4ThreeVector[StepNumbers];
0071 
0072 
0073    solidMod=new G4Tubs *[StepNumbers];
0074    logicMod=new G4LogicalVolume *[StepNumbers];
0075    physiMod=new G4VPhysicalVolume *[(4*(StepNumbers-1)+1)];
0076      
0077    for (G4int i=0;i<StepNumbers;i++)
0078   {
0079     Weight[i]=0;
0080     StepThickness[i]=0;
0081     StartingAngle[i]=0;
0082     SpanningAngle[i]=0;
0083     PositionMod[i]=G4ThreeVector(0,0,0);
0084     solidMod[i]=0;
0085     logicMod[i]=0;  
0086       
0087   }
0088   
0089   for (G4int i=0;i<4*(StepNumbers-1)+1;i++)
0090   {
0091   physiMod[i]=0;      
0092   }
0093     
0094   
0095   ModulatorMessenger = new  HadrontherapyModulatorMessenger(this);  
0096   ModulatorDefaultProperties();
0097   rm = new G4RotationMatrix(); 
0098   G4double phi = 270. *deg;     
0099   rm -> rotateY(phi); 
0100 }
0101 /////////////////////////////////////////////////////////////////////////////
0102 HadrontherapyModulator::~HadrontherapyModulator() 
0103 {
0104   delete rm;
0105   delete [] Weight;
0106   delete [] StepThickness;
0107   delete [] StartingAngle;
0108   delete [] SpanningAngle;
0109   delete [] PositionMod;
0110   delete [] solidMod;
0111   delete [] logicMod;  
0112   delete []    physiMod;
0113   delete ModulatorMessenger;
0114 }
0115 
0116 /////////////////////////////////////////////////////////////////////////////
0117 void HadrontherapyModulator::ModulatorDefaultProperties()
0118 {
0119 /* Here we initialize the step properties of Modulator wheel, you can create your
0120 specific modulator by changing the values in this class or writing them in an external
0121 file and activate reading from file via a macrofile.    */  
0122 
0123  StepThickness[0]=0; Weight[0]=.14445;  
0124  StepThickness[1]=.8; Weight[1]=.05665; 
0125  StepThickness[2]=1.6; Weight[2]=.05049;
0126  StepThickness[3]=2.4; Weight[3]=.04239;
0127  StepThickness[4]=3.2; Weight[4]=.04313;
0128  StepThickness[5]=4.0; Weight[5]=.03879;
0129  StepThickness[6]=4.8; Weight[6]=.04182;
0130  StepThickness[7]=5.6; Weight[7]=.03422;
0131  StepThickness[8]=6.4; Weight[8]=.03469;
0132  StepThickness[9]=7.2; Weight[9]=.03589;
0133  StepThickness[10]=8.0; Weight[10]=.03633;
0134  StepThickness[11]=8.8; Weight[11]=.03842;
0135  StepThickness[12]=9.6; Weight[12]=.03688;
0136  StepThickness[13]=10.4; Weight[13]=.03705;
0137  StepThickness[14]=11.2; Weight[14]=.03773;
0138  StepThickness[15]=12.0; Weight[15]=.03968;
0139  StepThickness[16]=12.8; Weight[16]=.04058;
0140  StepThickness[17]=13.6; Weight[17]=.03903;
0141  StepThickness[18]=14.4; Weight[18]=.04370;
0142  StepThickness[19]=15.2; Weight[19]=.03981;
0143  StepThickness[20]=16.0; Weight[20]=.05226;
0144  StepThickness[21]=16.8; Weight[21]=.03603;
0145  GetStepInformation();  
0146  
0147 } 
0148 /////////////////////////////////////////////////////////////////////////////
0149 void HadrontherapyModulator:: ModulatorPropertiesFromFile(G4String Name)
0150 {
0151   delete [] Weight;
0152   delete [] StepThickness;
0153   delete [] StartingAngle;
0154   delete [] SpanningAngle;
0155   delete [] PositionMod;
0156   delete [] solidMod;
0157   delete [] logicMod;  
0158   delete []    physiMod;
0159   delete    solidMod1;
0160   delete    logicMod1;
0161   delete    physiMod1;
0162   delete    solidMod2;       
0163   delete    logicMod2;          
0164   delete    physiMod2;
0165   delete    solidMod3;         
0166   delete    logicMod3;       
0167   delete    physiMod3;
0168   delete    solidMod4;       
0169   delete    logicMod4;       
0170   delete    physiMod4;
0171 // The Modulator wheel properties is getting form an external file "ModoulWeight.txt" 
0172   File.open(Name,  std::ios::in);
0173   if(!File.is_open())
0174   {
0175   G4cout<<" WARNING: The File with name of "<<Name<<
0176  " doesn't exist to get modulator step properties. please modify it and try again"<<G4endl;
0177  
0178  G4Exception("HadrontherapyModulator::ModulatorPropertiesFromFile( )", "Hadrontherapy0009"
0179  , FatalException, "Error: No available external file for reading from");
0180     }     
0181 
0182   G4String string;
0183   File >>string>> StepNumbers;
0184   File >>string>>string>>string;
0185   
0186   
0187    Weight=new G4double[StepNumbers];
0188    StepThickness=new G4double[StepNumbers];
0189    StartingAngle=new G4double[StepNumbers];
0190    SpanningAngle=new G4double[StepNumbers];
0191    PositionMod=new G4ThreeVector[StepNumbers];
0192 
0193 
0194    solidMod=new G4Tubs *[StepNumbers];
0195    logicMod=new G4LogicalVolume *[StepNumbers];
0196    physiMod=new G4VPhysicalVolume *[(4*(StepNumbers-1)+1)];
0197  
0198   for(G4int i=0;i<StepNumbers;i++)
0199    {
0200      G4String stringX;
0201      File>>stringX>> StepThickness[i]>>Weight[i];  
0202    }    
0203    
0204    GetStepInformation();
0205    BuildSteps();
0206    
0207 
0208 
0209 }
0210 ////////////////////////////////////////////////////////////////////////////////
0211 void HadrontherapyModulator::GetStepInformation()
0212 {
0213 
0214  G4double TotalWeight=0;
0215  // convert the absolute weight values to relative ones 
0216  for(G4int i=0;i<StepNumbers;i++)
0217   {
0218      TotalWeight+=Weight[i];
0219   }
0220  
0221  for(G4int i=0;i<StepNumbers;i++)
0222  {
0223    Weight[i]=Weight[i]/TotalWeight;
0224  }
0225  
0226  // To build the RMW step layers will be put one after another  
0227  
0228   StartingAngle[0]=0 *deg;
0229   SpanningAngle[0]=90 *deg;
0230   G4double PositionModx;
0231   G4double WholeStartingAngle=0 *deg;
0232   G4double WholeThickness=0;
0233   for(G4int i=1;i<StepNumbers;i++)
0234   {
0235       StartingAngle[i]=WholeStartingAngle+(Weight[i-1]*(2*pi))/8;
0236       SpanningAngle[i]=90* deg -2*StartingAngle[i];
0237       StepThickness[i]=StepThickness[i]-WholeThickness;
0238       PositionModx=WholeThickness+StepThickness[i]/2.;
0239       PositionMod[i]=G4ThreeVector(0,0,PositionModx);
0240       WholeThickness+=StepThickness[i];
0241       WholeStartingAngle=StartingAngle[i];
0242   } 
0243     
0244     
0245 }
0246 /////////////////////////////////////////////////////////////////////////////////
0247 void HadrontherapyModulator::BuildModulator(G4VPhysicalVolume* motherVolume)
0248 {
0249   G4bool isotopes = false;
0250   G4Material* airNist =  G4NistManager::Instance()->FindOrBuildMaterial("G4_AIR", isotopes);
0251 
0252 
0253    Mod0Mater = airNist;
0254    ModMater = airNist; // You have to change modulator material via a macrofile (default is air)
0255  
0256   innerRadiusOfTheTube = 2.5 *cm;
0257   outerRadiusOfTheTube = 9.5 *cm;
0258 
0259   // Mother of the modulator wheel  
0260   G4ThreeVector positionMotherMod = G4ThreeVector(-2160.50 *mm, 30 *mm, 50 *mm);
0261  
0262   G4Box* solidMotherMod = new G4Box("MotherMod", 12 *cm, 12 *cm, 12 *cm);
0263  
0264   logicMotherMod = new G4LogicalVolume(solidMotherMod, Mod0Mater,"MotherMod",0,0,0);
0265 
0266   physiMotherMod = new G4PVPlacement(rm,positionMotherMod,  "MotherMod", 
0267                      logicMotherMod,                      
0268                      motherVolume,      
0269                      false,           
0270                      0); 
0271     BuildSteps();                 
0272                      
0273                      
0274                      
0275                  }            
0276  ///////////////////////////////////////////////////////////////////////////////////////
0277  void HadrontherapyModulator::BuildSteps()
0278  {
0279   //----------------------------------------------------------
0280   // Mother volume of first quarter of the modulator
0281   //----------------------------------------------------------
0282 
0283   G4double hightOfTheTube0 = 10.0 *cm;
0284   G4double startAngleOfTheTube0 = 0 *deg;
0285   G4double spanningAngleOfTheTube0 = 90 *deg;
0286   
0287   G4RotationMatrix rm1;
0288   rm1.rotateZ(0 *deg);
0289  
0290   G4ThreeVector positionMod1 = G4ThreeVector(0*cm,0*cm,0*cm);
0291   
0292   solidMod1 = new G4Tubs("Mod1",
0293              innerRadiusOfTheTube, 
0294              outerRadiusOfTheTube,
0295              hightOfTheTube0/2.,
0296              startAngleOfTheTube0, 
0297              spanningAngleOfTheTube0);
0298   
0299   logicMod1 = new G4LogicalVolume(solidMod1, Mod0Mater, "Mod1",0,0,0);
0300   
0301   physiMod1 = new G4PVPlacement(G4Transform3D(rm1, positionMod1), 
0302                 logicMod1,    
0303                 "Mod1",       
0304                 logicMotherMod,  
0305                 false,         
0306                 0);            
0307   
0308  
0309   //----------------------------------------------------------
0310   //  modulator steps
0311   //----------------------------------------------------------
0312   for (G4int i=1;i<StepNumbers;i++)
0313   {
0314 
0315   solidMod[i] = new G4Tubs("Modstep",
0316              innerRadiusOfTheTube, 
0317              outerRadiusOfTheTube,
0318              StepThickness[i]/2.,
0319              StartingAngle[i], 
0320              SpanningAngle[i]);
0321                    
0322   logicMod[i] = new G4LogicalVolume(solidMod[i], 
0323                                    ModMater, "Modstep",0,0,0);
0324   
0325   physiMod[i] = new G4PVPlacement(0,               
0326                 PositionMod[i],  
0327                 logicMod[i],     
0328                 "Modstep",        
0329                 logicMod1,     
0330                 false,         
0331                 0);     
0332       
0333       
0334   }
0335  
0336  /////////////////////////////////////////////////////////////////////////////////////////////////
0337   //----------------------------------------------------------
0338   // Mother volume of the second modulator quarter
0339   //----------------------------------------------------------
0340   
0341     
0342   G4RotationMatrix rm2;
0343   rm2.rotateZ(90 *deg);
0344   
0345   G4ThreeVector positionMod2 = G4ThreeVector(0*cm,0*cm,0*cm);
0346   
0347   solidMod2 = new G4Tubs("Mod2",
0348               innerRadiusOfTheTube, 
0349               outerRadiusOfTheTube,
0350               hightOfTheTube0/2.,
0351               startAngleOfTheTube0, 
0352               spanningAngleOfTheTube0);
0353   
0354   logicMod2 = new G4LogicalVolume(solidMod2,
0355                                   Mod0Mater, "Mod2",0,0,0);
0356   
0357   
0358   physiMod2 = new G4PVPlacement(G4Transform3D(rm2, positionMod2), 
0359                  logicMod2,    
0360                  "Mod2",       
0361                  logicMotherMod, 
0362                  false,         
0363                  0);            
0364  
0365 
0366     for (G4int i=1;i<StepNumbers;i++)
0367   {
0368     
0369   physiMod[StepNumbers+i-1] = new G4PVPlacement(0,               
0370                 PositionMod[i],  
0371                 logicMod[i],     
0372                 "Modstep",        
0373                 logicMod2,     
0374                 false,         
0375                 0);  
0376                  
0377             } 
0378 
0379  
0380 
0381   //----------------------------------------------------------
0382   // Mother volume of the third modulator quarter
0383   //----------------------------------------------------------
0384   
0385     
0386   G4RotationMatrix rm3;
0387   rm3.rotateZ(180 *deg);
0388   
0389   G4ThreeVector positionMod3 = G4ThreeVector(0*cm,0*cm,0*cm);
0390   
0391   solidMod3 = new G4Tubs("Mod3",
0392               innerRadiusOfTheTube, 
0393               outerRadiusOfTheTube,
0394               hightOfTheTube0,
0395               startAngleOfTheTube0/2., 
0396               spanningAngleOfTheTube0);
0397   
0398   logicMod3 = new G4LogicalVolume(solidMod3, 
0399                                   Mod0Mater, "Mod3",0,0,0);
0400   
0401   
0402   physiMod3 = new G4PVPlacement(G4Transform3D(rm3, positionMod3), 
0403                  logicMod3,    // its logical volume              
0404                  "Mod3",        // its name
0405                  logicMotherMod,  // its mother  volume
0406                  false,         // no boolean operations
0407                  0);            // no particular field
0408   
0409 
0410 
0411 
0412  for (G4int i=1;i<StepNumbers;i++)
0413   {
0414 
0415   physiMod[2*(StepNumbers-1)+i] = new G4PVPlacement(0,               
0416                 PositionMod[i],  
0417                 logicMod[i],     
0418                 "Modstep",        
0419                 logicMod3,     
0420                 false,         
0421                 0);  
0422             
0423   }
0424 
0425   //----------------------------------------------------------
0426   // Mother volume of the fourth modulator quarter
0427   //----------------------------------------------------------
0428   
0429     
0430   G4RotationMatrix rm4;
0431   rm4.rotateZ(270 *deg);
0432   
0433   G4ThreeVector positionMod4 = G4ThreeVector(0*cm,0*cm,0*cm);
0434   
0435   solidMod4 = new G4Tubs("Mod4",
0436               innerRadiusOfTheTube, 
0437               outerRadiusOfTheTube,
0438               hightOfTheTube0,
0439               startAngleOfTheTube0/2., 
0440               spanningAngleOfTheTube0);
0441   
0442   logicMod4 = new G4LogicalVolume(solidMod4, 
0443                                   Mod0Mater, "Mod4",0,0,0);
0444   
0445   
0446   physiMod4 = new G4PVPlacement(G4Transform3D(rm4, positionMod4), 
0447                  logicMod4,               
0448                  "Mod4",        
0449                  logicMotherMod,  
0450                  false,         
0451                  0);           
0452   
0453 
0454 for (G4int i=1;i<StepNumbers;i++)
0455   {
0456   physiMod[3*(StepNumbers-1)+i] = new G4PVPlacement(0,               
0457                 PositionMod[i],  
0458                 logicMod[i],     
0459                 "Modstep",        
0460                 logicMod4,     
0461                 false,         
0462                 0);   
0463   }
0464   // Inform the kernel about the new geometry
0465     G4RunManager::GetRunManager() -> GeometryHasBeenModified();
0466     G4RunManager::GetRunManager() -> PhysicsHasBeenModified();
0467   G4VisAttributes * red = new G4VisAttributes( G4Colour(1. ,0. ,0.));
0468   red-> SetVisibility(true);
0469   red-> SetForceSolid(true);
0470   logicMotherMod -> SetVisAttributes(G4VisAttributes::GetInvisible());
0471  
0472   logicMod1 ->SetVisAttributes(G4VisAttributes::GetInvisible());
0473   logicMod2 ->SetVisAttributes(G4VisAttributes::GetInvisible());
0474   logicMod3 ->SetVisAttributes(G4VisAttributes::GetInvisible());
0475   logicMod4 ->SetVisAttributes(G4VisAttributes::GetInvisible());
0476   
0477   for (G4int i=1;i<StepNumbers;i++)
0478   {
0479      logicMod[i] -> SetVisAttributes(red); 
0480   }
0481   
0482  }
0483 
0484 /////////////////////////////////////////////////////////////////////////////
0485 // Messenger values
0486 /////////////////////////////////////////////////////////////////////////////
0487 void HadrontherapyModulator::SetModulatorAngle(G4double angle)
0488 {
0489   G4double rotationAngle = angle;
0490   rm -> rotateZ(rotationAngle);
0491   physiMotherMod -> SetRotation(rm);  
0492   G4cout << "MODULATOR HAS BEEN ROTATED OF " << rotationAngle/deg 
0493      << " deg" << G4endl;
0494   G4RunManager::GetRunManager() -> GeometryHasBeenModified(); 
0495 }
0496 /////////////////////////////////////////////////////////////////////////
0497 // Change modulator material
0498 void HadrontherapyModulator::SetModulatorMaterial(G4String Material)
0499 {
0500     if (G4Material* NewMaterial = G4NistManager::Instance()->FindOrBuildMaterial(Material, false) )
0501     {
0502     if (NewMaterial) 
0503     {
0504         for(G4int i=1;i<StepNumbers;i++)
0505         {
0506         logicMod[i] -> SetMaterial(NewMaterial);  
0507       //  G4RunManager::GetRunManager() -> PhysicsHasBeenModified();
0508         G4RunManager::GetRunManager() -> GeometryHasBeenModified();
0509         
0510       //  G4cout<<(logicMod[i]->GetMaterial()->GetName())<<G4endl;
0511     }
0512     G4cout << "The material of the Modulator wheel has been changed to " << Material << G4endl;
0513     }
0514     }
0515     else
0516     {
0517     G4cout << "WARNING: material \"" << Material << "\" doesn't exist in NIST elements/materials"
0518         " table [located in $G4INSTALL/source/materials/src/G4NistMaterialBuilder.cc]" << G4endl; 
0519     G4cout << "Use command \"/parameter/nist\" to see full materials list!" << G4endl; 
0520     
0521     
0522     }
0523 }
0524 
0525 ////////////////////////////////////////////////////////////////////////////////
0526 // Change modulator position in the beam line
0527 void HadrontherapyModulator::SetModulatorPosition(G4ThreeVector Pos)
0528 {
0529   G4ThreeVector NewModulatorPos=Pos; 
0530   physiMotherMod -> SetTranslation( NewModulatorPos); 
0531   G4RunManager::GetRunManager() -> GeometryHasBeenModified();
0532   G4cout << "The modulator wheel is translated to"<<  NewModulatorPos/mm <<"mm " <<G4endl;
0533     
0534 }
0535 /////////////////////////////////////////////////////////////////////////////////
0536 //change modulator inner raduis
0537 void HadrontherapyModulator::SetModulatorInnerRadius(G4double newvalue)
0538 {
0539 solidMod1 -> SetInnerRadius(newvalue);
0540 solidMod2 -> SetInnerRadius(newvalue);
0541 solidMod3 -> SetInnerRadius(newvalue);
0542 solidMod4 -> SetInnerRadius(newvalue);
0543  for(G4int i=1;i<StepNumbers;i++)
0544  {
0545  solidMod[i] -> SetInnerRadius(newvalue);}
0546    G4RunManager::GetRunManager() -> GeometryHasBeenModified(); 
0547   G4cout << "InnerRadius of the Modulator Wheel has been changed to :"
0548      << newvalue/mm<<" mm"<< G4endl;        
0549 }
0550 /////////////////////////////////////////////////////////////////////////////////
0551 //change modulator outer raduis
0552 void HadrontherapyModulator::SetModulatorOuterRadius(G4double newvalue)
0553 {
0554 solidMod1 -> SetOuterRadius(newvalue);
0555 solidMod2 -> SetOuterRadius(newvalue);
0556 solidMod3 -> SetOuterRadius(newvalue);
0557 solidMod4 -> SetOuterRadius(newvalue);
0558  for(G4int i=1;i<StepNumbers;i++)
0559  {
0560  solidMod[i] -> SetOuterRadius(newvalue);}
0561    G4RunManager::GetRunManager() -> GeometryHasBeenModified(); 
0562   G4cout << "OuterRadius of the Modulator Wheel has been changed to :"
0563      << newvalue/mm<<" mm"<<G4endl; 
0564 }
0565 /////////////////////////////////////////////////////////////////////////////////
0566 void HadrontherapyModulator:: GetDataFromFile(G4String value)
0567 
0568 {
0569 G4String Name=value;
0570 if(value=="default" )   
0571 {
0572 Name=FileName;
0573 }
0574 G4cout<<" Step properties of modulator will be get out from the external file "
0575  <<Name<<G4endl;
0576 ModulatorPropertiesFromFile(Name);
0577 }