G4MaterialScanner.cc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 //
00027 // $Id$
00028 //
00029 //
00030 //
00031 
00032 #include "G4MaterialScanner.hh"
00033 
00034 #include "G4SystemOfUnits.hh"
00035 #include "G4EventManager.hh"
00036 #include "G4MatScanMessenger.hh"
00037 #include "G4RayShooter.hh"
00038 #include "G4MSSteppingAction.hh"
00039 #include "G4GeometryManager.hh"
00040 #include "G4StateManager.hh"
00041 #include "G4Event.hh"
00042 #include "G4TransportationManager.hh"
00043 #include "G4RunManagerKernel.hh"
00044 #include "G4Region.hh"
00045 #include "G4RegionStore.hh"
00046 #include "G4ProcessManager.hh"
00047 #include "G4ProcessVector.hh"
00048 #include "G4SDManager.hh"
00049 
00050 
00051 G4MaterialScanner::G4MaterialScanner()
00052 {
00053   theRayShooter = new G4RayShooter();
00054   theMessenger = new G4MatScanMessenger(this);
00055   theEventManager = G4EventManager::GetEventManager();
00056 
00057   theUserEventAction = 0;
00058   theUserStackingAction = 0;
00059   theUserTrackingAction = 0;
00060   theUserSteppingAction = 0;
00061 
00062   theMatScannerEventAction = 0;
00063   theMatScannerStackingAction = 0;
00064   theMatScannerTrackingAction = 0;
00065   theMatScannerSteppingAction = 0;
00066 
00067   eyePosition = G4ThreeVector(0.,0.,0.);
00068   nTheta = 91;
00069   thetaMin = 0.*deg;
00070   thetaSpan = 90.*deg;
00071   nPhi = 37;
00072   phiMin = 0.*deg;
00073   phiSpan = 360.*deg;
00074 
00075   regionSensitive = false;
00076   regionName = "notDefined";
00077   theRegion = 0;
00078 }
00079 
00080 G4MaterialScanner::~G4MaterialScanner()
00081 {
00082   delete theRayShooter;
00083   delete theMatScannerSteppingAction;
00084   delete theMessenger;
00085 }
00086 
00087 void G4MaterialScanner::Scan()
00088 {
00089   G4StateManager* theStateMan = G4StateManager::GetStateManager();
00090   G4ApplicationState currentState = theStateMan->GetCurrentState();
00091   if(currentState!=G4State_Idle)
00092   {
00093     G4cerr << "Illegal application state - Scan() ignored." << G4endl;
00094     return;
00095   }
00096 
00097   if(!theMatScannerSteppingAction)
00098   { theMatScannerSteppingAction = new G4MSSteppingAction(); }
00099   StoreUserActions();
00100   DoScan();
00101   RestoreUserActions();
00102 }
00103 
00104 void G4MaterialScanner::StoreUserActions()
00105 { 
00106   theUserEventAction = theEventManager->GetUserEventAction();
00107   theUserStackingAction = theEventManager->GetUserStackingAction();
00108   theUserTrackingAction = theEventManager->GetUserTrackingAction();
00109   theUserSteppingAction = theEventManager->GetUserSteppingAction();
00110 
00111   theEventManager->SetUserAction(theMatScannerEventAction);
00112   theEventManager->SetUserAction(theMatScannerStackingAction);
00113   theEventManager->SetUserAction(theMatScannerTrackingAction);
00114   theEventManager->SetUserAction(theMatScannerSteppingAction);
00115 
00116   G4SDManager* theSDMan = G4SDManager::GetSDMpointerIfExist();
00117   if(theSDMan)
00118   { theSDMan->Activate("/",false); }
00119 
00120   G4GeometryManager* theGeomMan = G4GeometryManager::GetInstance();
00121   theGeomMan->OpenGeometry();
00122   theGeomMan->CloseGeometry(true);
00123 }
00124 
00125 void G4MaterialScanner::RestoreUserActions()
00126 {
00127   theEventManager->SetUserAction(theUserEventAction);
00128   theEventManager->SetUserAction(theUserStackingAction);
00129   theEventManager->SetUserAction(theUserTrackingAction);
00130   theEventManager->SetUserAction(theUserSteppingAction);
00131 
00132   G4SDManager* theSDMan = G4SDManager::GetSDMpointerIfExist();
00133   if(theSDMan)
00134   { theSDMan->Activate("/",true); }
00135 }
00136 
00137 void G4MaterialScanner::DoScan()
00138 {
00139 // Confirm material table is updated
00140   G4RunManagerKernel::GetRunManagerKernel()->UpdateRegion();
00141 
00148 
00149 // Close geometry and set the application state
00150   G4GeometryManager* geomManager = G4GeometryManager::GetInstance();
00151   geomManager->OpenGeometry();
00152   geomManager->CloseGeometry(1,0);
00153   
00154   G4ThreeVector center(0,0,0);
00155   G4Navigator* navigator =
00156       G4TransportationManager::GetTransportationManager()->GetNavigatorForTracking();
00157   navigator->LocateGlobalPointAndSetup(center,0,false);
00158 
00159   G4StateManager* theStateMan = G4StateManager::GetStateManager();
00160   theStateMan->SetNewState(G4State_GeomClosed); 
00161 
00162 // Event loop
00163   G4int iEvent = 0;
00164   for(G4int iTheta=0;iTheta<nTheta;iTheta++)
00165   {
00166    G4double theta = thetaMin;
00167    if(iTheta>0) theta += G4double(iTheta)*thetaSpan/G4double(nTheta-1);
00168    G4double aveLength = 0.;
00169    G4double aveX0 = 0.;
00170    G4double aveLambda = 0.;
00171    G4cout << G4endl;
00172    G4cout << "         Theta(deg)    Phi(deg)  Length(mm)          x0     lambda0" << G4endl;
00173    G4cout << G4endl;
00174    for(G4int iPhi=0;iPhi<nPhi;iPhi++)
00175    {
00176     G4Event* anEvent = new G4Event(iEvent++);
00177     G4double phi = phiMin;
00178     if(iPhi>0) phi += G4double(iPhi)*phiSpan/G4double(nPhi-1);
00179     eyeDirection = G4ThreeVector(std::cos(theta)*std::cos(phi),
00180                                  std::cos(theta)*std::sin(phi),
00181                                  std::sin(theta));
00182     theRayShooter->Shoot(anEvent,eyePosition,eyeDirection);
00183     theMatScannerSteppingAction->Initialize(regionSensitive,theRegion);
00184     theEventManager->ProcessOneEvent(anEvent);
00185     G4double length = theMatScannerSteppingAction->GetTotalStepLength();
00186     G4double x0 = theMatScannerSteppingAction->GetX0();
00187     G4double lambda = theMatScannerSteppingAction->GetLambda0();
00188 
00189     G4cout << "        "
00190            << std::setw(11) << theta/deg << " "
00191            << std::setw(11) << phi/deg << " "
00192            << std::setw(11) << length/mm << " "
00193            << std::setw(11) << x0 << " "
00194            << std::setw(11) << lambda << G4endl;
00195     aveLength += length/mm;
00196     aveX0 += x0;
00197     aveLambda += lambda;
00198    }
00199    if(nPhi>1)
00200    {
00201     G4cout << G4endl;
00202     G4cout << " ave. for theta = " << std::setw(11) << theta/deg << " : "
00203            << std::setw(11) << aveLength/nPhi << " "
00204            << std::setw(11) << aveX0/nPhi << " "
00205            << std::setw(11) << aveLambda/nPhi << G4endl;
00206    }
00207   }
00208 
00209   theStateMan->SetNewState(G4State_Idle); 
00210   return;
00211 }
00212 
00213 G4bool G4MaterialScanner::SetRegionName(const G4String& val)
00214 {
00215   G4Region* aRegion = G4RegionStore::GetInstance()->GetRegion(val);
00216   if(aRegion)
00217   {
00218     theRegion = aRegion;
00219     regionName = val;
00220     return true;
00221   }
00222   else
00223   {
00224     G4cerr << "Region <" << val << "> not found. Command ignored." << G4endl;
00225     G4cerr << "Defined regions are : " << G4endl;
00226     for(size_t i=0;i<G4RegionStore::GetInstance()->size();i++)
00227     { G4cerr << " " << (*(G4RegionStore::GetInstance()))[i]->GetName(); }
00228     G4cerr << G4endl;
00229     return false;
00230   }
00231 }

Generated on Mon May 27 17:48:51 2013 for Geant4 by  doxygen 1.4.7