Access Hit Collection from EventAction::EndOfEventAction()

Geant4 Version: 11.3.0
Operating System: Fedora 40 & Debian
Compiler/Version: g++ (GCC) 14.2.1
CMake Version: 3.28.2


In my code below I am trying to access a HitCollection populated in ProcessHits();
The collecion is populated and inserted into the HitCollection, but when I try to use it in EventAction::EventAction(RunAction* runAction) I have an empty hit collection.

I’m using multiple threads.

// SD 

class G4VHitsCollection;
namespace test
{

class SensitiveDetector : public G4VSensitiveDetector {
public:
    SensitiveDetector(const G4String& name);
    ~SensitiveDetector() override = default;
    void Initialize(G4HCofThisEvent* HCE) override;
    
    G4bool ProcessHits(G4Step* step, G4TouchableHistory* history) override;
    
    G4int GetCollectionIDPublic(const char* collectionNameChar) {
        return G4SDManager::GetSDMpointer()->GetCollectionID(collectionNameChar);  // Calls the protected method
    }
    
private:
    GasHitsCollection* fHitsCollection = nullptr;    
    static thread_local G4int fHCID;

};
} 

// SD initialize

namespace test
{

thread_local G4int SensitiveDetector::fHCID = -1;  // Initialize fHCID here
 
SensitiveDetector::SensitiveDetector(const G4String& name) : G4VSensitiveDetector(name){
	
        collectionName.insert("GasHitsCollection");
    if (collectionName.size() == 0) {
        G4cerr << "ERROR: collectionName is empty in constructor!" << G4endl;
    } else {
        G4cout << ">>> Collection name set: " << collectionName[0] << G4endl;
    }

    }

 void SensitiveDetector::Initialize(G4HCofThisEvent* hce) {
   if (fHCID < 0) {  // Only set once
        fHCID = GetCollectionID(0); // Get collection ID here
        G4cout << "=== SensitiveDetector::Initialize() ===> fHCID set to: " << fHCID << G4endl;
    }

    fHitsCollection = new GasHitsCollection(SensitiveDetectorName, collectionName[0]);
    if (!fHitsCollection) {
        G4cerr << "ERROR: Failed to create hitsCollection!" << G4endl;
        return;
    }

    if (!hce) {
        G4cerr << "ERROR: hce is NULL! Cannot add hits collection." << G4endl;
        return;
    }

    hce->AddHitsCollection(fHCID, fHitsCollection);
    G4cout << "===> Hits collection added to hce with ID: " << fHCID << G4endl;

}

// processhit()

G4bool SensitiveDetector::ProcessHits(G4Step* step, G4TouchableHistory* /*history*/) {
	
	G4double edep = step->GetTotalEnergyDeposit();

    if (edep == 0) return false;  // Skip if no energy deposition

    GasHit* newHit = new GasHit();
    newHit->SetEdep(edep);
    
    fHitsCollection->insert(newHit);

    G4cout << ">>> Hit recorded: edep = " << edep / keV << " keV, Total Hits: " 
           << fHitsCollection->entries() << G4endl;

    return true;
}


//EventAction.hh





// EventAction.cc

class G4Event;

namespace test
{

class RunAction;

/// Event action class

class EventAction : public G4UserEventAction
{
  public:
    EventAction(RunAction* runAction);
    ~EventAction() override = default;

    void BeginOfEventAction(const G4Event* event) override;
    void EndOfEventAction(const G4Event* event) override;

    void AddEdep(G4double edep){fEdep+=edep;}
    
    static thread_local G4int fHCID;  // Declare fHCID // Add this as a thread-local variable
    
  private:
    RunAction* fRunAction = nullptr;
    //G4int fHCID;
    
    
    G4int fNeutronCaptures = 0;  // Total nCapture events
    G4int fLi7Produced = 0;      // Total Li7 productions
    G4double fEdep = 0.;
};

}  // namespace B1

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......



namespace test
{
thread_local G4int EventAction::fHCID = -1; // Set default value for thread-local variable



//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

void EventAction::EndOfEventAction(const G4Event* event) {
    
    //  checking if fHCID is set correctly for the thread
    if (fHCID != -1) {
        

        G4HCofThisEvent* hce = event->GetHCofThisEvent();
        if (hce) {
           
            GasHitsCollection* hitsCollection = (GasHitsCollection*) hce->GetHC(fHCID);
            if (!hitsCollection) return;

            // Calculate total energy deposition
            
             G4cout << "Retrieved hits collection with " << hitsCollection->entries() << " entries." << G4endl;
            G4int numHits = hitsCollection->entries();
            G4cout << "Number of Hits: " << numHits << G4endl;
            
            for (G4int i = 0; i < numHits; i++) {
				
                GasHit* hit = (*hitsCollection)[i];
                //totalEdep += hit->GetEdep();
                
                // Add energy deposition to run action
                fEdep += hit->GetEdep();
                

                // Check if hit corresponds to a neutron capture event
                if (hit->IsNeutronCapture()) {
					fNeutronCaptures++;
                    
                }

                // Check if a lithium-7 nucleus was produced
                if (hit->IsLi7Production()) {
                    fLi7Produced++;
                }
            }
        }
    }

     fRunAction->AddEdep(fEdep);
     fRunAction->AddNeutronCapture(fNeutronCaptures);
     fRunAction->AddLi7Production(fLi7Produced);
  
 
}