Geant4 Cross Reference |
1 // 2 // ******************************************************************** 3 // * License and Disclaimer * 4 // * * 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. * 10 // * * 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitation of liability. * 17 // * * 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************************************** 25 // 26 #include "Par03EventAction.hh" 27 28 #include "Par03DetectorConstruction.hh" 29 #include "Par03Hit.hh" 30 31 #include "G4AnalysisManager.hh" 32 #include "G4Event.hh" 33 #include "G4EventManager.hh" 34 #include "G4HCofThisEvent.hh" 35 #include "G4SDManager.hh" 36 37 Par03EventAction::Par03EventAction(Par03DetectorConstruction* aDetector) 38 : G4UserEventAction(), fHitCollectionID(-1), fTimer(), fDetector(aDetector) 39 {} 40 41 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 42 43 Par03EventAction::~Par03EventAction() = default; 44 45 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 46 47 void Par03EventAction::BeginOfEventAction(const G4Event*) 48 { 49 fTimer.Start(); 50 } 51 52 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 53 54 void Par03EventAction::EndOfEventAction(const G4Event* aEvent) 55 { 56 fTimer.Stop(); 57 // Get hits collection ID (only once) 58 if (fHitCollectionID == -1) { 59 fHitCollectionID = G4SDManager::GetSDMpointer()->GetCollectionID("hits"); 60 } 61 // Get hits collection 62 auto hitsCollection = 63 static_cast<Par03HitsCollection*>(aEvent->GetHCofThisEvent()->GetHC(fHitCollectionID)); 64 65 if (hitsCollection == nullptr) { 66 G4ExceptionDescription msg; 67 msg << "Cannot access hitsCollection ID " << fHitCollectionID; 68 G4Exception("Par03EventAction::GetHitsCollection()", "MyCode0001", FatalException, msg); 69 } 70 // Get analysis manager 71 auto analysisManager = G4AnalysisManager::Instance(); 72 // Retrieve only once detector dimensions 73 if (fCellSizeZ == 0) { 74 fCellSizeZ = fDetector->GetLength() / fDetector->GetNbOfLayers(); 75 fCellSizeRho = fDetector->GetRadius() / fDetector->GetNbOfRhoCells(); 76 } 77 78 // Retrieve information from primary vertex and primary particle 79 // To calculate shower axis and entry point to the detector 80 auto primaryVertex = 81 G4EventManager::GetEventManager()->GetConstCurrentEvent()->GetPrimaryVertex(); 82 auto primaryParticle = primaryVertex->GetPrimary(0); 83 G4double primaryEnergy = primaryParticle->GetTotalEnergy(); 84 // Estimate from vertex and particle direction the entry point to the detector 85 // Calculate entrance point to the detector located at z = 0 86 auto primaryDirection = primaryParticle->GetMomentumDirection(); 87 auto primaryEntrance = 88 primaryVertex->GetPosition() - primaryVertex->GetPosition().z() * primaryDirection; 89 G4double cosDirection = std::cos(primaryDirection.theta()); 90 G4double sinDirection = std::sin(primaryDirection.theta()); 91 92 // Fill histograms 93 Par03Hit* hit = nullptr; 94 G4double hitEn = 0; 95 G4double totalEnergy = 0; 96 G4int hitZ = -1; 97 G4int hitRho = -1; 98 G4int hitType = -1; 99 G4double tDistance = 0., rDistance = 0.; 100 G4double tFirstMoment = 0., tSecondMoment = 0.; 101 G4double rFirstMoment = 0., rSecondMoment = 0.; 102 for (size_t iHit = 0; iHit < hitsCollection->entries(); iHit++) { 103 hit = static_cast<Par03Hit*>(hitsCollection->GetHit(iHit)); 104 hitZ = hit->GetZid(); 105 hitRho = hit->GetRhoId(); 106 hitEn = hit->GetEdep(); 107 hitType = hit->GetType(); 108 if (hitEn > 0) { 109 totalEnergy += hitEn; 110 tDistance = hitZ * fCellSizeZ * cosDirection 111 + (hitRho * fCellSizeRho - primaryEntrance.perp()) * sinDirection; 112 rDistance = hitZ * fCellSizeZ * (-sinDirection) 113 + (hitRho * fCellSizeRho - primaryEntrance.perp()) * cosDirection; 114 tFirstMoment += hitEn * tDistance; 115 rFirstMoment += hitEn * rDistance; 116 analysisManager->FillH1(4, tDistance, hitEn); 117 analysisManager->FillH1(5, rDistance, hitEn); 118 analysisManager->FillH1(10, hitType); 119 } 120 } 121 tFirstMoment /= totalEnergy; 122 rFirstMoment /= totalEnergy; 123 analysisManager->FillH1(0, primaryEnergy / GeV); 124 analysisManager->FillH1(1, totalEnergy / GeV); 125 analysisManager->FillH1(2, totalEnergy / primaryEnergy); 126 analysisManager->FillH1(3, fTimer.GetRealElapsed()); 127 analysisManager->FillH1(6, tFirstMoment); 128 analysisManager->FillH1(7, rFirstMoment); 129 130 // Second loop over hits to calculate second moments 131 for (size_t iHit = 0; iHit < hitsCollection->entries(); iHit++) { 132 hit = static_cast<Par03Hit*>(hitsCollection->GetHit(iHit)); 133 hitEn = hit->GetEdep(); 134 hitZ = hit->GetZid(); 135 hitRho = hit->GetRhoId(); 136 if (hitEn > 0) { 137 tDistance = hitZ * fCellSizeZ * cosDirection 138 + (hitRho * fCellSizeRho - primaryEntrance.r()) * sinDirection; 139 rDistance = hitZ * fCellSizeZ * (-sinDirection) 140 + (hitRho * fCellSizeRho - primaryEntrance.r()) * cosDirection; 141 tSecondMoment += hitEn * std::pow(tDistance - tFirstMoment, 2); 142 rSecondMoment += hitEn * std::pow(rDistance - rFirstMoment, 2); 143 } 144 } 145 tSecondMoment /= totalEnergy; 146 rSecondMoment /= totalEnergy; 147 analysisManager->FillH1(8, tSecondMoment); 148 analysisManager->FillH1(9, rSecondMoment); 149 }