Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-18 08:32:53

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 Par03EventAction.cc
0027 /// \brief Implementation of the Par03EventAction class
0028 
0029 #include "Par03EventAction.hh"
0030 
0031 #include "Par03DetectorConstruction.hh"
0032 #include "Par03Hit.hh"
0033 
0034 #include "G4AnalysisManager.hh"
0035 #include "G4Event.hh"
0036 #include "G4EventManager.hh"
0037 #include "G4HCofThisEvent.hh"
0038 #include "G4SDManager.hh"
0039 
0040 Par03EventAction::Par03EventAction(Par03DetectorConstruction* aDetector)
0041   : G4UserEventAction(), fHitCollectionID(-1), fTimer(), fDetector(aDetector)
0042 {}
0043 
0044 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0045 
0046 Par03EventAction::~Par03EventAction() = default;
0047 
0048 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0049 
0050 void Par03EventAction::BeginOfEventAction(const G4Event*)
0051 {
0052   fTimer.Start();
0053 }
0054 
0055 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0056 
0057 void Par03EventAction::EndOfEventAction(const G4Event* aEvent)
0058 {
0059   fTimer.Stop();
0060   // Get hits collection ID (only once)
0061   if (fHitCollectionID == -1) {
0062     fHitCollectionID = G4SDManager::GetSDMpointer()->GetCollectionID("hits");
0063   }
0064   // Get hits collection
0065   auto hitsCollection =
0066     static_cast<Par03HitsCollection*>(aEvent->GetHCofThisEvent()->GetHC(fHitCollectionID));
0067 
0068   if (hitsCollection == nullptr) {
0069     G4ExceptionDescription msg;
0070     msg << "Cannot access hitsCollection ID " << fHitCollectionID;
0071     G4Exception("Par03EventAction::GetHitsCollection()", "MyCode0001", FatalException, msg);
0072   }
0073   // Get analysis manager
0074   auto analysisManager = G4AnalysisManager::Instance();
0075   // Retrieve only once detector dimensions
0076   if (fCellSizeZ == 0) {
0077     fCellSizeZ = fDetector->GetLength() / fDetector->GetNbOfLayers();
0078     fCellSizeRho = fDetector->GetRadius() / fDetector->GetNbOfRhoCells();
0079   }
0080 
0081   // Retrieve information from primary vertex and primary particle
0082   // To calculate shower axis and entry point to the detector
0083   auto primaryVertex =
0084     G4EventManager::GetEventManager()->GetConstCurrentEvent()->GetPrimaryVertex();
0085   auto primaryParticle = primaryVertex->GetPrimary(0);
0086   G4double primaryEnergy = primaryParticle->GetTotalEnergy();
0087   // Estimate from vertex and particle direction the entry point to the detector
0088   // Calculate entrance point to the detector located at z = 0
0089   auto primaryDirection = primaryParticle->GetMomentumDirection();
0090   auto primaryEntrance =
0091     primaryVertex->GetPosition() - primaryVertex->GetPosition().z() * primaryDirection;
0092   G4double cosDirection = std::cos(primaryDirection.theta());
0093   G4double sinDirection = std::sin(primaryDirection.theta());
0094 
0095   // Fill histograms
0096   Par03Hit* hit = nullptr;
0097   G4double hitEn = 0;
0098   G4double totalEnergy = 0;
0099   G4int hitZ = -1;
0100   G4int hitRho = -1;
0101   G4int hitType = -1;
0102   G4double tDistance = 0., rDistance = 0.;
0103   G4double tFirstMoment = 0., tSecondMoment = 0.;
0104   G4double rFirstMoment = 0., rSecondMoment = 0.;
0105   for (size_t iHit = 0; iHit < hitsCollection->entries(); iHit++) {
0106     hit = static_cast<Par03Hit*>(hitsCollection->GetHit(iHit));
0107     hitZ = hit->GetZid();
0108     hitRho = hit->GetRhoId();
0109     hitEn = hit->GetEdep();
0110     hitType = hit->GetType();
0111     if (hitEn > 0) {
0112       totalEnergy += hitEn;
0113       tDistance = hitZ * fCellSizeZ * cosDirection
0114                   + (hitRho * fCellSizeRho - primaryEntrance.perp()) * sinDirection;
0115       rDistance = hitZ * fCellSizeZ * (-sinDirection)
0116                   + (hitRho * fCellSizeRho - primaryEntrance.perp()) * cosDirection;
0117       tFirstMoment += hitEn * tDistance;
0118       rFirstMoment += hitEn * rDistance;
0119       analysisManager->FillH1(4, tDistance, hitEn);
0120       analysisManager->FillH1(5, rDistance, hitEn);
0121       analysisManager->FillH1(10, hitType);
0122     }
0123   }
0124   tFirstMoment /= totalEnergy;
0125   rFirstMoment /= totalEnergy;
0126   analysisManager->FillH1(0, primaryEnergy / GeV);
0127   analysisManager->FillH1(1, totalEnergy / GeV);
0128   analysisManager->FillH1(2, totalEnergy / primaryEnergy);
0129   analysisManager->FillH1(3, fTimer.GetRealElapsed());
0130   analysisManager->FillH1(6, tFirstMoment);
0131   analysisManager->FillH1(7, rFirstMoment);
0132 
0133   // Second loop over hits to calculate second moments
0134   for (size_t iHit = 0; iHit < hitsCollection->entries(); iHit++) {
0135     hit = static_cast<Par03Hit*>(hitsCollection->GetHit(iHit));
0136     hitEn = hit->GetEdep();
0137     hitZ = hit->GetZid();
0138     hitRho = hit->GetRhoId();
0139     if (hitEn > 0) {
0140       tDistance = hitZ * fCellSizeZ * cosDirection
0141                   + (hitRho * fCellSizeRho - primaryEntrance.r()) * sinDirection;
0142       rDistance = hitZ * fCellSizeZ * (-sinDirection)
0143                   + (hitRho * fCellSizeRho - primaryEntrance.r()) * cosDirection;
0144       tSecondMoment += hitEn * std::pow(tDistance - tFirstMoment, 2);
0145       rSecondMoment += hitEn * std::pow(rDistance - rFirstMoment, 2);
0146     }
0147   }
0148   tSecondMoment /= totalEnergy;
0149   rSecondMoment /= totalEnergy;
0150   analysisManager->FillH1(8, tSecondMoment);
0151   analysisManager->FillH1(9, rSecondMoment);
0152 }