Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-08 08:29:43

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 Par04SensitiveDetector.cc
0027 /// \brief Implementation of the Par04SensitiveDetector class
0028 
0029 #include "Par04SensitiveDetector.hh"
0030 
0031 #include "Par04EventInformation.hh"  // for Par04EventInformation
0032 #include "Par04Hit.hh"  // for Par04Hit, Par04HitsCollection
0033 
0034 #include "G4Event.hh"  // for G4Event
0035 #include "G4EventManager.hh"  // for G4EventManager
0036 #include "G4HCofThisEvent.hh"  // for G4HCofThisEvent
0037 #include "G4SDManager.hh"  // for G4SDManager
0038 #include "G4Step.hh"  // for G4Step
0039 #include "G4Track.hh"  // for G4Track
0040 
0041 #include <CLHEP/Vector/Rotation.h>  // for HepRotation
0042 #include <CLHEP/Vector/ThreeVector.h>  // for Hep3Vector
0043 #include <G4CollectionNameVector.hh>  // for G4CollectionNameVector
0044 #include <G4FastHit.hh>  // for G4FastHit
0045 #include <G4FastTrack.hh>  // for G4FastTrack
0046 #include <G4RotationMatrix.hh>  // for G4RotationMatrix
0047 #include <G4StepPoint.hh>  // for G4StepPoint
0048 #include <G4THitsCollection.hh>  // for G4THitsCollection
0049 #include <G4ThreeVector.hh>  // for G4ThreeVector
0050 #include <G4VSensitiveDetector.hh>  // for G4VSensitiveDetector
0051 #include <G4VUserEventInformation.hh>  // for G4VUserEventInformation
0052 #include <cmath>  // for floor
0053 #include <cstddef>  // for size_t
0054 #include <vector>  // for vector
0055 
0056 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0057 
0058 Par04SensitiveDetector::Par04SensitiveDetector(G4String aName) : G4VSensitiveDetector(aName)
0059 {
0060   collectionName.insert("hits");
0061 }
0062 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0063 
0064 Par04SensitiveDetector::Par04SensitiveDetector(G4String aName, G4ThreeVector aNb,
0065                                                G4ThreeVector aSize)
0066   : G4VSensitiveDetector(aName), fMeshNbOfCells(aNb), fMeshSizeOfCells(aSize)
0067 {
0068   collectionName.insert("hits");
0069 }
0070 
0071 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0072 
0073 Par04SensitiveDetector::~Par04SensitiveDetector() = default;
0074 
0075 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0076 
0077 void Par04SensitiveDetector::Initialize(G4HCofThisEvent* aHCE)
0078 {
0079   fHitsCollection = new Par04HitsCollection(SensitiveDetectorName, collectionName[0]);
0080   if (fHitCollectionID < 0) {
0081     fHitCollectionID = G4SDManager::GetSDMpointer()->GetCollectionID(fHitsCollection);
0082   }
0083   aHCE->AddHitsCollection(fHitCollectionID, fHitsCollection);
0084 
0085   // reset entrance position
0086   fEntrancePosition.set(-1, -1, -1);
0087   fEntranceDirection.set(-1, -1, -1);
0088 }
0089 
0090 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0091 
0092 G4bool Par04SensitiveDetector::ProcessHits(G4Step* aStep, G4TouchableHistory*)
0093 {
0094   G4double edep = aStep->GetTotalEnergyDeposit();
0095   if (edep == 0.) return true;
0096 
0097   auto hit = RetrieveAndSetupHit(aStep->GetPostStepPoint()->GetPosition());
0098   if (hit == nullptr) return true;
0099 
0100   // Add energy deposit from G4Step
0101   hit->AddEdep(edep);
0102   // Increment the counter
0103   hit->AddNdep(1);
0104 
0105   // Fill time information from G4Step
0106   // If it's already filled, choose hit with earliest global time
0107   if (hit->GetTime() == -1 || hit->GetTime() > aStep->GetTrack()->GetGlobalTime())
0108     hit->SetTime(aStep->GetTrack()->GetGlobalTime());
0109 
0110   // Set hit type to full simulation (only if hit is not already marked as fast
0111   // sim)
0112   if (hit->GetType() != 1) hit->SetType(0);
0113 
0114   return true;
0115 }
0116 
0117 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0118 
0119 G4bool Par04SensitiveDetector::ProcessHits(const G4FastHit* aHit, const G4FastTrack* aTrack,
0120                                            G4TouchableHistory*)
0121 {
0122   G4double edep = aHit->GetEnergy();
0123   if (edep == 0.) return true;
0124 
0125   auto hit = RetrieveAndSetupHit(aHit->GetPosition());
0126   if (hit == nullptr) return true;
0127 
0128   // Add energy deposit from G4FastHit
0129   hit->AddEdep(edep);
0130   // Increment the counter
0131   hit->AddNdep(1);
0132 
0133   // Fill time information from G4FastTrack
0134   // If it's already filled, choose hit with earliest global time
0135   if (hit->GetTime() == -1 || hit->GetTime() > aTrack->GetPrimaryTrack()->GetGlobalTime()) {
0136     hit->SetTime(aTrack->GetPrimaryTrack()->GetGlobalTime());
0137   }
0138 
0139   // Set hit type to fast simulation (even if hit was already marked as full
0140   // sim, overwrite it)
0141   hit->SetType(1);
0142 
0143   return true;
0144 }
0145 
0146 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0147 
0148 Par04Hit* Par04SensitiveDetector::RetrieveAndSetupHit(G4ThreeVector aGlobalPosition)
0149 {
0150   if (fEntrancePosition.x() == -1) {
0151     auto info = dynamic_cast<Par04EventInformation*>(
0152       G4EventManager::GetEventManager()->GetConstCurrentEvent()->GetUserInformation());
0153     if (info == nullptr) return nullptr;
0154     fEntrancePosition = info->GetPosition();
0155     fEntranceDirection = info->GetDirection();
0156   }
0157 
0158   auto delta = aGlobalPosition - fEntrancePosition;
0159 
0160   // Calculate rotation matrix along the particle momentum direction
0161   // It will rotate the shower axes to match the incoming particle direction
0162   G4RotationMatrix rotMatrix = G4RotationMatrix();
0163   double particleTheta = fEntranceDirection.theta();
0164   double particlePhi = fEntranceDirection.phi();
0165   rotMatrix.rotateZ(-particlePhi);
0166   rotMatrix.rotateY(-particleTheta);
0167   G4RotationMatrix rotMatrixInv = CLHEP::inverseOf(rotMatrix);
0168 
0169   delta = rotMatrix * delta;
0170 
0171   G4int rhoNo = std::floor(delta.perp() / fMeshSizeOfCells.x());
0172   G4int phiNo = std::floor((CLHEP::pi + delta.phi()) / fMeshSizeOfCells.y());
0173   G4int zNo = std::floor(delta.z() / fMeshSizeOfCells.z());
0174 
0175   std::size_t hitID =
0176     fMeshNbOfCells.x() * fMeshNbOfCells.z() * phiNo + fMeshNbOfCells.z() * rhoNo + zNo;
0177 
0178   if (zNo >= fMeshNbOfCells.z() || rhoNo >= fMeshNbOfCells.x() || zNo < 0) {
0179     return nullptr;
0180   }
0181 
0182   auto hit = fHitsMap[hitID].get();
0183   if (hit == nullptr) {
0184     fHitsMap[hitID] = std::unique_ptr<Par04Hit>(new Par04Hit());
0185     hit = fHitsMap[hitID].get();
0186     hit->SetPhiId(phiNo);
0187     hit->SetRhoId(rhoNo);
0188     hit->SetZid(zNo);
0189     hit->SetRot(rotMatrixInv);
0190     hit->SetPos(fEntrancePosition
0191                 + rotMatrixInv * G4ThreeVector(0, 0, (zNo + 0.5) * fMeshSizeOfCells.z()));
0192   }
0193   return hit;
0194 }
0195 
0196 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0197 
0198 void Par04SensitiveDetector::EndOfEvent(G4HCofThisEvent*)
0199 {
0200   for (const auto& hits : fHitsMap) {
0201     fHitsCollection->insert(new Par04Hit(*hits.second.get()));
0202   }
0203   fHitsMap.clear();
0204 }