Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-23 09:22:35

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