Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-01 07:50:52

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 /// \file SteppingAction.cc
0027 /// \brief Implementation of the SteppingAction class
0028 
0029 #include "SteppingAction.hh"
0030 
0031 #include "Run.hh"
0032 
0033 #include "G4IonTable.hh"
0034 #include "G4LossTableManager.hh"
0035 #include "G4ParticleDefinition.hh"
0036 #include "G4ParticleTypes.hh"
0037 #include "G4Step.hh"
0038 #include "G4StepPoint.hh"
0039 #include "G4SystemOfUnits.hh"
0040 #include "G4TouchableHistory.hh"
0041 #include "G4Track.hh"
0042 #include "G4VPhysicalVolume.hh"
0043 #include "G4VSolid.hh"
0044 #include "G4VTouchable.hh"
0045 
0046 const std::array<G4String, SteppingAction::fkNumberScoringShells>
0047   SteppingAction::fkArrayScoringShellNames = {"tracker", "emCalo", "hadCalo"};
0048 
0049 const std::array<G4String, SteppingAction::fkNumberKinematicRegions>
0050   SteppingAction::fkArrayKinematicRegionNames = {"", "below 20 MeV", "above 20 MeV"};
0051 
0052 const std::array<G4String, SteppingAction::fkNumberScoringPositions>
0053   SteppingAction::fkArrayScoringPositionNames = {"forward", "backward"};
0054 
0055 const std::array<G4String, SteppingAction::fkNumberParticleTypes>
0056   SteppingAction::fkArrayParticleTypeNames = {"all",      "electron",   "gamma",      "muon",
0057                                               "neutrino", "pion",       "neutron",    "proton",
0058                                               "ion",      "otherMeson", "otherBaryon"};
0059 
0060 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0061 
0062 G4int SteppingAction::GetIndex(const G4int iScoringShell, const G4int iKinematicRegion,
0063                                const G4int iScoringPosition, const G4int iParticleType)
0064 {
0065   G4int index = -1;
0066   if (iScoringShell >= 0 && iScoringShell < fkNumberScoringShells && iKinematicRegion >= 0
0067       && iKinematicRegion < fkNumberKinematicRegions && iScoringPosition >= 0
0068       && iScoringPosition < fkNumberScoringPositions && iParticleType >= 0
0069       && iParticleType < fkNumberParticleTypes)
0070   {
0071     index =
0072       iScoringShell * fkNumberKinematicRegions * fkNumberScoringPositions * fkNumberParticleTypes
0073       + iKinematicRegion * fkNumberScoringPositions * fkNumberParticleTypes
0074       + iScoringPosition * fkNumberParticleTypes + iParticleType;
0075   }
0076   if (index < 0 || index >= fkNumberCombinations) {
0077     G4cerr << "SteppingAction::GetIndex : WRONG index=" << index << "  set it to 0 !" << G4endl;
0078     index = 0;
0079   }
0080   return index;
0081 }
0082 
0083 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0084 
0085 SteppingAction::SteppingAction() : G4UserSteppingAction()
0086 {
0087   Initialize();
0088 }
0089 
0090 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0091 
0092 void SteppingAction::Initialize()
0093 {
0094   // Initialization needed at the beginning of each Run
0095   fPrimaryParticleId = 0;
0096   fPrimaryParticleEnergy = 0.0;
0097   fPrimaryParticleDirection = G4ThreeVector(0.0, 0.0, 1.0);
0098   fTrackerMaterialName = fEmCaloMaterialName = fHadCaloMaterialName = "";
0099   fIsFirstStepOfTheEvent = true;
0100   fIsFirstStepInTracker = fIsFirstStepInEmCalo = fIsFirstStepInHadCalo = true;
0101   fIsFirstStepInScoringTrackerShell = fIsFirstStepInScoringEmCaloShell =
0102     fIsFirstStepInScoringHadCaloShell = true;
0103   fCubicVolumeScoringTrackerShell = fCubicVolumeScoringEmCaloShell =
0104     fCubicVolumeScoringHadCaloShell = 1.0;
0105   for (G4int i = 0; i < fkNumberCombinations; ++i) {
0106     fArraySumStepLengths[i] = 0.0;
0107   }
0108   /*
0109   for ( G4int i = 0; i < fkNumberCombinations; ++i ) fArraySumStepLengths[i] = 999.9;
0110   G4cout << " fkNumberCombinations=" << fkNumberCombinations << G4endl;
0111   for ( G4int i = 0; i < fkNumberScoringShells; ++i ) {
0112     for ( G4int j = 0; j < fkNumberKinematicRegions; ++j ) {
0113       for ( G4int k = 0; k < fkNumberScoringPositions; ++k ) {
0114         for ( G4int ll = 0; ll < fkNumberParticleTypes; ++ll ) {
0115           G4int index = GetIndex( i, j, k, ll );
0116           G4cout << "(i, j, k, ll)=(" << i << ", " << j << ", " << k << ", "
0117                  << ll << ")  ->" << index;
0118           if ( fArraySumStepLengths[ index ] < 1.0 ) G4cout << " <=== REPEATED!";
0119           else                                       fArraySumStepLengths[ index ] = 0.0;
0120           G4cout << G4endl;
0121         }
0122       }
0123     }
0124   }
0125   for ( G4int i = 0; i < fkNumberCombinations; ++i ) {
0126     if ( fArraySumStepLengths[i] > 999.0 ) G4cout << " i=" << i << " NOT COVERED !" << G4endl;
0127   }
0128   */
0129 }
0130 
0131 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0132 
0133 void SteppingAction::UserSteppingAction(const G4Step* theStep)
0134 {
0135   // Get information on the primary particle
0136   if (fIsFirstStepOfTheEvent) {
0137     if (theStep->GetTrack()->GetParentID() == 0) {
0138       fPrimaryParticleId = theStep->GetTrack()->GetDefinition()->GetPDGEncoding();
0139       fPrimaryParticleEnergy = theStep->GetPreStepPoint()->GetKineticEnergy();
0140       fPrimaryParticleDirection = theStep->GetPreStepPoint()->GetMomentumDirection();
0141       if (fRunPtr) {
0142         fRunPtr->SetPrimaryParticleId(fPrimaryParticleId);
0143         fRunPtr->SetPrimaryParticleEnergy(fPrimaryParticleEnergy);
0144         fRunPtr->SetPrimaryParticleDirection(fPrimaryParticleDirection);
0145       }
0146       fIsFirstStepOfTheEvent = false;
0147     }
0148   }
0149   // Get information on the materials
0150   if (fIsFirstStepInTracker
0151       && theStep->GetPreStepPoint()->GetPhysicalVolume()->GetName() == "physiTrackerShell")
0152   {
0153     fTrackerMaterialName = theStep->GetPreStepPoint()->GetMaterial()->GetName();
0154     if (fRunPtr) fRunPtr->SetTrackerMaterialName(fTrackerMaterialName);
0155     fIsFirstStepInTracker = false;
0156   }
0157   if (fIsFirstStepInEmCalo
0158       && theStep->GetPreStepPoint()->GetPhysicalVolume()->GetName() == "physiEmCaloShell")
0159   {
0160     fEmCaloMaterialName = theStep->GetPreStepPoint()->GetMaterial()->GetName();
0161     if (fRunPtr) fRunPtr->SetEmCaloMaterialName(fEmCaloMaterialName);
0162     fIsFirstStepInEmCalo = false;
0163   }
0164   if (fIsFirstStepInHadCalo
0165       && theStep->GetPreStepPoint()->GetPhysicalVolume()->GetName() == "physiHadCaloShell")
0166   {
0167     fHadCaloMaterialName = theStep->GetPreStepPoint()->GetMaterial()->GetName();
0168     if (fRunPtr) fRunPtr->SetHadCaloMaterialName(fHadCaloMaterialName);
0169     fIsFirstStepInHadCalo = false;
0170   }
0171   // Get information on step lengths in the scoring shells
0172   G4int iScoringShell = -1;
0173   if (theStep->GetPreStepPoint()->GetPhysicalVolume()->GetName() == "physiScoringTrackerShell") {
0174     iScoringShell = 0;
0175     if (fIsFirstStepInScoringTrackerShell) {
0176       fCubicVolumeScoringTrackerShell =
0177         theStep->GetTrack()->GetVolume()->GetLogicalVolume()->GetSolid()->GetCubicVolume();
0178       if (fRunPtr) fRunPtr->SetCubicVolumeScoringTrackerShell(fCubicVolumeScoringTrackerShell);
0179       fIsFirstStepInScoringTrackerShell = false;
0180     }
0181   }
0182   else if (theStep->GetPreStepPoint()->GetPhysicalVolume()->GetName() == "physiScoringEmCaloShell")
0183   {
0184     iScoringShell = 1;
0185     if (fIsFirstStepInScoringEmCaloShell) {
0186       fCubicVolumeScoringEmCaloShell =
0187         theStep->GetTrack()->GetVolume()->GetLogicalVolume()->GetSolid()->GetCubicVolume();
0188       if (fRunPtr) fRunPtr->SetCubicVolumeScoringEmCaloShell(fCubicVolumeScoringEmCaloShell);
0189       fIsFirstStepInScoringEmCaloShell = false;
0190     }
0191   }
0192   else if (theStep->GetPreStepPoint()->GetPhysicalVolume()->GetName() == "physiScoringHadCaloShell")
0193   {
0194     iScoringShell = 2;
0195     if (fIsFirstStepInScoringHadCaloShell) {
0196       fCubicVolumeScoringHadCaloShell =
0197         theStep->GetTrack()->GetVolume()->GetLogicalVolume()->GetSolid()->GetCubicVolume();
0198       if (fRunPtr) fRunPtr->SetCubicVolumeScoringHadCaloShell(fCubicVolumeScoringHadCaloShell);
0199       fIsFirstStepInScoringHadCaloShell = false;
0200     }
0201   }
0202   if (iScoringShell >= 0) {
0203     G4double stepLength = theStep->GetTrack()->GetStepLength() * theStep->GetTrack()->GetWeight();
0204     G4int absPdg = theStep->GetTrack()->GetDefinition() == nullptr
0205                      ? 0
0206                      : std::abs(theStep->GetTrack()->GetDefinition()->GetPDGEncoding());
0207     /*
0208     G4cout << theStep->GetTrack()->GetDefinition()->GetParticleName() << "  absPdg=" << absPdg
0209            << "  Ekin[MeV]=" << theStep->GetPreStepPoint()->GetKineticEnergy()
0210            << "  r[mm]=" << theStep->GetTrack()->GetPosition().mag()
0211            << "  z[mm]=" << theStep->GetTrack()->GetPosition().z()
0212            << "  " << theStep->GetTrack()->GetVolume()->GetName()
0213            << "  " << theStep->GetTrack()->GetMaterial()->GetName()
0214            << "  L[mm]=" << stepLength << "  "
0215            << ( fPrimaryParticleDirection.dot( theStep->GetTrack()->GetPosition().unit() ) > 0.0
0216                 ? "forward" : "backward" ) << G4endl;
0217     */
0218     // Three kinematical regions:  [0] : any value ;  [1] : below 20 MeV ;  [2] : above 20 MeV
0219     G4int iKinematicRegion = theStep->GetPreStepPoint()->GetKineticEnergy() < 20.0 ? 1 : 2;
0220     // Two scoring positions:  [0] : forward hemisphere ;  [1] : backward hemisphere
0221     // (with respect to the primary particle initial direction)
0222     G4int iScoringPosition =
0223       fPrimaryParticleDirection.dot(theStep->GetTrack()->GetPosition().unit()) > 0.0 ? 0 : 1;
0224     G4int iParticleType = -1;
0225     if (absPdg == 11)
0226       iParticleType = 1;  // electron (and positron)
0227     else if (absPdg == 22)
0228       iParticleType = 2;  // gamma
0229     else if (absPdg == 13)
0230       iParticleType = 3;  // muons (mu- and mu+)
0231     else if (absPdg == 12 || absPdg == 14 || absPdg == 16)
0232       iParticleType = 4;  // neutrinos
0233     // (and anti-neutrinos), all flavors
0234     else if (absPdg == 111 || absPdg == 211)
0235       iParticleType = 5;  // (charged) pions
0236     else if (absPdg == 2112)
0237       iParticleType = 6;  // neutron (and anti-neutron)
0238     else if (absPdg == 2212)
0239       iParticleType = 7;  // proton  (and anti-proton)
0240     else if (G4IonTable::IsIon(theStep->GetTrack()->GetDefinition()) ||  // ions (and anti-ions)
0241              G4IonTable::IsAntiIon(theStep->GetTrack()->GetDefinition()))
0242       iParticleType = 8;
0243     else if (absPdg < 1000)
0244       iParticleType = 9;  // other mesons (e.g. kaons) (Note: this works
0245                           // in most cases, but not always!)
0246     else if (absPdg > 1000)
0247       iParticleType = 10;  // other baryons (e.g. hyperons, anti-hyperons,
0248                            // etc.)
0249     // Consider the specific case : scoring shell, kinematic region, scoring position, and
0250     // particle type
0251     G4int index = GetIndex(iScoringShell, iKinematicRegion, iScoringPosition, iParticleType);
0252     fArraySumStepLengths[index] += stepLength;
0253     // Consider the "all" particle case, with the same scoring shell, kinematic region and
0254     // scoring position
0255     index = GetIndex(iScoringShell, iKinematicRegion, iScoringPosition, 0);
0256     fArraySumStepLengths[index] += stepLength;
0257     // Consider the "any" kinematic region case, with the same scoring shell, scoring position
0258     // and particle type
0259     index = GetIndex(iScoringShell, 0, iScoringPosition, iParticleType);
0260     fArraySumStepLengths[index] += stepLength;
0261     // Consider the "any" kinematic region and "all" particle, with the same scoring shell and
0262     // scoring position
0263     index = GetIndex(iScoringShell, 0, iScoringPosition, 0);
0264     fArraySumStepLengths[index] += stepLength;
0265     if (fRunPtr) fRunPtr->SetSteppingArray(fArraySumStepLengths);
0266   }
0267 }
0268 
0269 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......