#include "SensitiveDetector.hh" SensitiveDetector::SensitiveDetector(G4String name) : G4VSensitiveDetector(name) { fTotalEnergyDeposited = 0.; } SensitiveDetector::~SensitiveDetector() { } void SensitiveDetector::Initialize(G4HCofThisEvent *) { fTotalEnergyDeposited = 0.; G4AnalysisManager *analysisManager = G4AnalysisManager::Instance(); if (!analysisManager || !analysisManager->IsOpenFile()) { G4cerr << "Error: AnalysisManager is NULL or file is not open in Initialize!" << G4endl; } } void SensitiveDetector::EndOfEvent(G4HCofThisEvent *) { G4AnalysisManager *analysisManager = G4AnalysisManager::Instance(); if (!analysisManager || !analysisManager->IsOpenFile()) { G4cerr << "Error: AnalysisManager is NULL or file is not open in EndOfEvent!" << G4endl; return; } G4AutoLock lock(&myMutex); // Ensure thread safety analysisManager->FillNtupleDColumn(1, fTotalEnergyDeposited); analysisManager->AddNtupleRow(); G4cout << "Deposited Energy at EndOfEvent: " << fTotalEnergyDeposited << " keV" << G4endl; } G4bool SensitiveDetector::ProcessHits(G4Step *aStep, G4TouchableHistory *) { // Ensure step has a valid energy deposition G4double energyDeposited = aStep->GetTotalEnergyDeposit(); // Only process nonzero energy deposits if (energyDeposited > 0) { fTotalEnergyDeposited += energyDeposited; // Accumulate total deposit G4cout << "Energy Deposited: " << energyDeposited / keV << " keV" << G4endl; } // Ensure G4AnalysisManager is correctly used G4AnalysisManager *analysisManager = G4AnalysisManager::Instance(); if (!analysisManager || !analysisManager->IsOpenFile()) { G4cerr << "Error: AnalysisManager is NULL or file is not open in ProcessHits! Skipping event." << G4endl; return false; } G4AutoLock lock(&myMutex); // Thread-safe writing // Retrieve event ID and step details G4int eventID = (G4RunManager::GetRunManager()->GetCurrentEvent()->GetEventID()); G4RunManager::GetRunManager()->GetCurrentEvent()->GetEventID(); G4StepPoint *preStepPoint = aStep->GetPreStepPoint(); G4double globalTime = preStepPoint->GetGlobalTime(); // Store data in the Ntuple analysisManager->FillNtupleIColumn(0, eventID); analysisManager->FillNtupleDColumn(1, energyDeposited); analysisManager->AddNtupleRow(); // Accumulate total energy deposition fTotalEnergyDeposited += energyDeposited; return true; }