/home/cern/BDSIM_new/src/BDSSteppingAction.cc

00001 /* BDSIM code.    Version 1.0
00002    Author: Grahame A. Blair, Royal Holloway, Univ. of London.
00003    Last modified 25.12.2003
00004    Copyright (c) 2003 by G.A.Blair.  ALL RIGHTS RESERVED. 
00005  
00006    Modified 22.03.05 by J.C.Carter, Royal Holloway, Univ. of London.
00007    Added code for GABs BeamGasPlug
00008    Changed Killer to kill just electrons/positrons if called KILLERE
00009 
00010    Modified 10.05.05 IA
00011    Removed Killer element, added cuts per element check
00012    Can have killer functionality if cuts set to 0
00013 
00014 */
00015 
00016 
00017 //====================================================
00018 //  Class description here ...
00019 //
00020 //====================================================
00021 
00022 #include "BDSGlobalConstants.hh" // must be first in include list
00023 
00024 #include "BDSSteppingAction.hh"
00025 
00026 #include <iostream>
00027 #include "G4Track.hh"
00028 
00029 #include"G4TransportationManager.hh"
00030 #include"G4FieldManager.hh"
00031 
00032 #include"G4Navigator.hh"
00033 #include"G4StepPoint.hh"
00034 #include "G4RotationMatrix.hh"
00035 #include "G4AffineTransform.hh"
00036 
00037 #include "BDSMaterials.hh"
00038 #include "G4Material.hh"
00039 #include "G4VSensitiveDetector.hh"
00040 #include "Randomize.hh"
00041 
00042 #include "G4UserLimits.hh"
00043 
00044 #include "BDSNeutronTrackInfo.hh"
00045 
00046 #include "G4VUserTrackInformation.hh"
00047 
00048 #include "G4VProcess.hh"
00049 
00050 #include "G4MagneticField.hh"
00051 #include "G4EventManager.hh"
00052 #include "G4StackManager.hh"
00053 #include "G4ChordFinder.hh"
00054 #include "G4MagIntegratorDriver.hh"
00055 #include "G4Region.hh"
00056 #include "BDSAcceleratorComponent.hh"
00057 
00058 #include "BDSQuadStepper.hh"
00059 #include "BDSSextStepper.hh"
00060 #include "BDSOctStepper.hh"
00061 #include "myQuadStepper.hh"
00062 
00063 extern BDSMaterials* theMaterials;
00064 
00065 typedef list<BDSAcceleratorComponent*> BDSBeamline;
00066 extern BDSBeamline theBeamline;
00067 
00068 extern G4double BDS_Threshold_Energy;
00069 extern G4double BDSLocalRadiusOfCurvature;
00070 
00071 extern G4int event_number;
00072 
00073 extern G4double initial_x,initial_xp,initial_y,initial_yp,initial_z,initial_E;
00074 
00075 extern G4bool verbose;      // run options
00076 extern G4bool verboseStep;
00077 extern G4bool verboseEvent;
00078 extern G4int verboseEventNumber;
00079 extern G4bool isBatch;
00080 
00081 extern G4int nptwiss;
00082 
00083 //static G4LogicalVolume* LastLogVol;
00084 //====================================================
00085 
00086 BDSSteppingAction::BDSSteppingAction()
00087 { 
00088   //  itsZposTolerance=1.e-11*m;
00089   itsZposTolerance=1.e-4*m;
00090   itsPosKick=1.e-11*m;
00091   itsNmax=10000;
00092   postponedEnergy=0;
00093 }
00094 
00095 //====================================================
00096 
00097 BDSSteppingAction::~BDSSteppingAction()
00098 { }
00099 
00100 
00101 //====================================================
00102 
00103 extern G4double htot;
00104 
00105 void BDSSteppingAction::UserSteppingAction(const G4Step* ThisStep)
00106 { 
00107   // G4cout<<"user stepping action called"<<G4endl;
00108 
00109   G4Track* ThisTrack=ThisStep->GetTrack();
00110   
00111   //G4int TrackID=ThisTrack->GetTrackID();
00112   
00113   G4String pName=ThisTrack->GetDefinition()->GetParticleName();
00114 
00115   if(ThisTrack->GetProperTime() > 1e-4*second)
00116     {
00117       G4cout << "WARNING: ProperTime > 1.e-4 second!" << G4endl;
00118       G4cout<<" Killing the particle"<<G4endl;
00119       ThisTrack->SetTrackStatus(fStopAndKill);
00120     }
00121   
00122   // Check for a problem at the boundaries, where an infinite loop can
00123   // apparently sometimes occur
00124   
00125 
00126   // leave Geant4 to take care of it 
00127 
00128  //  if(TrackID!=itsLastTrackID)
00129 //     {
00130 //       itsLastTrackID=TrackID;
00131 //       itsNtry=0;
00132 //     }
00133 //   else 
00134 //     { 
00135 //       if(fabs(ThisTrack->GetPosition().z()-itsLastZpos)<itsZposTolerance)
00136 //      {
00137 //        itsNtry++;
00138 //        if(itsNtry>itsNmax)
00139 //          {
00140 //            G4cout<<" Killing the particle"<<G4endl;
00141 //            ThisTrack->SetTrackStatus(fStopAndKill);
00142 //            /* ThisTrack->
00143 //              SetPosition(
00144 //                          ThisTrack->GetPosition()+
00145 //                          ThisTrack->GetMomentumDirection()*itsPosKick
00146 //                          ); */
00147 //            //              itsNtry=0;
00148 //          }
00149 //      }
00150 //       else   
00151 //      itsNtry=0;
00152 //     }
00153 
00154 
00155 //  itsLastZpos=ThisTrack->GetPosition().z();
00156   
00157 
00158   // check that there actually is a next volume as it may be the end of the optics line
00159   if(BDSGlobals->DoTwiss() && ThisTrack->GetNextVolume()) 
00160     {
00161 
00162       G4cout <<" ***"<< ThisTrack->GetVolume()->GetName()<<G4endl;
00163       
00164       G4int curr_delim = ThisTrack->GetVolume()->GetName().find("_");
00165       G4String curr_gmad_name = ThisTrack->GetVolume()->GetName().substr(0,curr_delim);
00166       G4int delim = ThisTrack->GetNextVolume()->GetName().find("_");
00167       G4String gmad_name = ThisTrack->GetNextVolume()->GetName().substr(0,delim);
00168       if (curr_gmad_name != gmad_name && gmad_name!="World")
00169       //      if(ThisTrack->GetVolume()->GetName().contains("samp"))
00170         { 
00171           G4cout << curr_gmad_name << " " << gmad_name << G4endl;
00172           G4ThreeVector pos = ThisTrack->GetPosition();
00173           postponedEnergy+=ThisTrack->GetTotalEnergy();
00174                   
00175           G4StackManager* SM = G4EventManager::GetEventManager()->GetStackManager();
00176 
00177           if(SM->GetNPostponedTrack()!= nptwiss-1 ) { 
00178             // postpone track and save its coordinates for twiss fit
00179             ThisTrack->SetTrackStatus(fPostponeToNextEvent);
00180 
00181 //          G4ThreeVector pos=ThisTrack->GetPosition();
00182 //          G4ThreeVector momDir=ThisTrack->GetMomentumDirection();
00183 
00184             // Get Translation and Rotation of Sampler Volume w.r.t the World Volume
00185          //    G4AffineTransform tf(ThisTrack->GetTouchable()->GetHistory()->GetTopTransform().Inverse());
00186 //          const G4RotationMatrix Rot=tf.NetRotation();
00187 //          const G4ThreeVector Trans=-tf.NetTranslation();
00188             
00189 //          G4ThreeVector LocalPosition=pos+Trans; 
00190 //          G4ThreeVector LocalDirection=Rot*momDir; 
00191 
00192 
00193             //subtract the energy end position chnge due to SR when rescaling
00194 
00195             //G4cout<<"changing track coordinates according to sr"<<G4endl;
00196 
00197 
00198           //   // get the field for the local element (assuming 
00199 //          // it doesn't chnage much when the particle travles through it
00200 
00201 
00202 //          G4FieldManager* TheFieldManager= ThisTrack->GetVolume()->GetLogicalVolume()->GetFieldManager();
00203 
00204 //          G4double dE = 0*GeV;
00205 //          G4double dx = 0*mm;
00206 
00207 //          if(TheFieldManager)
00208 //            {
00209 //              const G4Field* pField = TheFieldManager->GetDetectorField() ;
00210 //              G4ThreeVector  globPosition = track.GetPosition() ;
00211 //              G4double  globPosVec[3], FieldValueVec[3] ;
00212 //              globPosVec[0] = globPosition.x() ;
00213 //              globPosVec[1] = globPosition.y() ;
00214 //              globPosVec[2] = globPosition.z() ;
00215                 
00216 //              if(pField)
00217 //                pField->GetFieldValue( globPosVec, FieldValueVec ) ; 
00218                 
00219 //              G4double Blocal=
00220 //                Blocal=sqrt(FieldValueVec[0]*FieldValueVec[0] + 
00221 //                            FieldValueVec[1]*FieldValueVec[1] +
00222 //                            FieldValueVec[2]*FieldValueVec[2] );
00223 
00224 //              G4cout<<"Blocal = "<<Blocal/tesla<<" tesla"<<G4endl;
00225 
00226 //              G4ThreeVector InitMag=track.GetMomentum();
00227                 
00228 //              // transform to the local coordinate frame
00229                 
00230 //              G4AffineTransform tf(track.GetTouchable()->GetHistory()->GetTopTransform().Inverse());
00231 //              const G4RotationMatrix Rot=tf.NetRotation();
00232 //              const G4ThreeVector Trans=-tf.NetTranslation();
00233 //              InitMag=Rot*InitMag; 
00234                 
00235 //              G4double Rlocal=(InitMag.z()/GeV)/(0.299792458 * Blocal/tesla) *m;
00236 
00237 //              G4cout<<"Rlocal = "<<Rlocal/m<<" m"<<G4endl;
00238 
00239 //              dE = - 
00240 
00241 //            };
00242 
00243 
00244           
00245 //          LocalPosition += G4ThreeVector(dx,0,0);
00246 //          pos = LocalPosition - Trans;
00247 
00248 //          ThisTrack->SetPosition(pos);
00249 //          ThisTrack->SetKineticEnergy( ThisTrack->GetKineticEnergy() + dE );
00250 
00251           //   G4cout<<"postponed track: "<<SM->GetNPostponedTrack()<<G4endl;
00252 //          G4cout<<"r: "<<LocalPosition<<G4endl;
00253 //          G4cout<<"rp: "<<LocalDirection<<G4endl;
00254             //x.push_back(
00255           }
00256 
00257           // all tracks arrived - do rescaling
00258           if(SM->GetNPostponedTrack()== nptwiss-1)
00259             {
00260               SM->TransferStackedTracks(fPostpone, fUrgent);
00261               if(verbose) G4cout << "\nMean Energy: " << (postponedEnergy/nptwiss)/GeV << G4endl;
00262 
00263               list<BDSAcceleratorComponent*>::const_iterator iBeam;
00264               G4String type="";       
00265               for(iBeam=theBeamline.begin();iBeam!=theBeamline.end();iBeam++)
00266                 { 
00267                   if( (*iBeam)->GetName() == gmad_name)
00268                     {
00269                       type = (*iBeam)->GetType();
00270                       if(verbose) G4cout << "Next Element is: " << (*iBeam)->GetName() << G4endl;
00271                       if(verbose) G4cout << "Element Type: " << type << G4endl;
00272                       G4double old_P0 = BDSGlobals->GetBeamTotalEnergy();
00273                       G4double old_brho = 
00274                         sqrt(pow(old_P0,2)- pow(electron_mass_c2,2))/(0.299792458 * (GeV/(tesla*m)));
00275                       G4double new_P0 = postponedEnergy/nptwiss;
00276                       G4double new_brho = 
00277                         sqrt(pow(new_P0,2)- pow(electron_mass_c2,2))/(0.299792458 * (GeV/(tesla*m)));
00278                       
00279                       if(BDSGlobals->GetSynchRescale()) 
00280                         {
00281                           (*iBeam)->SynchRescale(new_brho/old_brho);
00282                           
00283                           if(verbose) G4cout << "Rescaling by: " << new_brho/old_brho << G4endl;
00284                           G4cout << "*";
00285                           G4cout.flush();
00286                         }
00287                       break;
00288                     }
00289                 }
00290               postponedEnergy=0;
00291             }
00292           return;
00293         }
00294     }
00295 
00296   
00297   // ------------  output in case of verbose step ---------------------
00298 
00299 
00300   if(verboseStep && (!BDSGlobals->GetSynchRescale()) )
00301     {
00302 
00303         int ID=ThisTrack->GetTrackID();
00304 
00305 
00306         G4cout.precision(10);
00307         G4cout<<"This volume="<< ThisTrack->GetVolume()->GetName()<<G4endl;
00308         
00309         G4LogicalVolume* LogVol=ThisTrack->GetVolume()->GetLogicalVolume();
00310         G4cout<<"This log volume="<<LogVol->GetName() <<G4endl;
00311         
00312         G4cout<<"ID="<<ID<<" part="<<
00313           ThisTrack->GetDefinition()->GetParticleName()<<
00314           "Energy="<<ThisTrack->GetTotalEnergy()/GeV<<
00315           " mom Px="
00316               <<ThisTrack->GetMomentum()[0]/GeV<<
00317           " Py="<<ThisTrack->GetMomentum()[1]/GeV<<
00318           " Pz="<<ThisTrack->GetMomentum()[2]/GeV<<" vol="<<
00319           ThisTrack->GetVolume()->GetName()<<G4endl;
00320         
00321         G4cout<<" Global Position="<<ThisTrack->GetPosition()<<G4endl;
00322         
00323         
00324         if(ThisTrack->GetMaterial()->GetName() !="LCVacuum")
00325           G4cout<<"material="<<ThisTrack->GetMaterial()->GetName()<<G4endl;
00326         //      G4Exception(" wrong material");
00327         
00328 
00329           G4VProcess* proc=(G4VProcess*)(ThisStep->GetPostStepPoint()->
00330             GetProcessDefinedStep() );
00331 
00332           if(proc)G4cout<<" post-step process="<<proc->GetProcessName()<<G4endl<<G4endl;
00333 
00334 
00335           proc=(G4VProcess*)(ThisStep->GetPreStepPoint()->
00336             GetProcessDefinedStep() );
00337 
00338           if(proc)G4cout<<" pre-step process="<<proc->GetProcessName()<<G4endl<<G4endl;
00339 
00340         
00341 
00342       }
00343 
00344 
00345   // -------------  kill tracks according to cuts -------------------
00346   
00347 
00348   // this cuts apply to default region
00349 
00350   //G4LogicalVolume* LogVol=ThisTrack->GetVolume()->GetLogicalVolume();
00351   //G4Region* reg = LogVol->GetRegion();
00352 
00353   //G4cout<<"I am in "<<reg->GetName()<<G4endl;
00354 
00355   //if(reg->GetName() != "precision")
00356     {
00357       
00358       
00359       if(pName=="gamma")
00360         {
00361           G4double photonCut = BDSGlobals->GetThresholdCutPhotons();
00362           
00363           if(ThisTrack->GetTotalEnergy()<photonCut)
00364             {
00365              
00366               ThisTrack->SetTrackStatus(fStopAndKill);
00367              
00368             }
00369         }
00370       
00371       if(pName=="e-"||pName=="e+")
00372         {
00373           if(ThisTrack->GetTotalEnergy()<BDSGlobals->GetThresholdCutCharged())
00374             {
00375         
00376               ThisTrack->SetTrackStatus(fStopAndKill);
00377         
00378             }
00379         }
00380       
00381     }
00382   
00383    
00384         
00385 }
00386 
00387 //====================================================
00388 
00389 
00390 

Generated on Wed Mar 5 17:25:23 2008 for BDSIM by  doxygen 1.5.3