Issue with multithreading and sensitive detector

Hi,

I’m having an issue with multithreading, specifically as it relates to sensitive detector and detector-based UI commands that I’m hoping someone can help me with. When in single thread mode, everything seems to run fine. But when I run in MT mode, I get this exception when the geometry is re-initialized during start up:
-------- WWWW ------- G4Exception-START -------- WWWW -------
*** G4Exception : Run0054
issued by : G4VUserDetectorConstruction::SetSensitiveDetector
Attempting to add multiple times the same sensitive detector (“SD”) is not allowed, skipping.
*** This is just a warning message. ***
-------- WWWW -------- G4Exception-END --------- WWWW -------

If I try to use a UI command in MT mode the simulation segfaults immediately.

To give a bit of background:

The way I have my code set up is that in my Detector Construction class, before the constructor at the very start of the class I define:
G4ThreadLocal SensitiveDetector* DetectorConstruction::SD = nullptr ;

Then in the constructor I set some initial parameters for the detector (materials and etc), an example collector_materialName = “steel”;

Then within my G4VPhysicalVolume* DetectorConstruction::ConstructVolumes() method I actually set the material with:
SetCollectorMaterial(collector_materialName);

This method is defined at the bottom of the class:
void DetectorConstruction::SetCollectorMaterial(G4String materialChoice)
{
collector_materialName = materialChoice;
collector_newMat = G4Material::GetMaterial(materialChoice);
if(collector_newMat)
{
collector_material = collector_newMat;
if(collector_logic) { collector_logic->SetMaterial(collector_material); }
#ifdef G4MULTITHREADED
G4MTRunManager::GetRunManager()->ReinitializeGeometry();
G4MTRunManager::GetRunManager()->PhysicsHasBeenModified();
#else
G4RunManager::GetRunManager()->ReinitializeGeometry();
G4RunManager::GetRunManager()->PhysicsHasBeenModified();
#endif
G4cout << "\n–> Collector Material Successfully Found: "
<< collector_material->GetName() << G4endl;
}
else
{
G4cout << “\n–> warning from DetectorConstruction::SetMaterial : "
<< materialChoice << " not found” << G4endl;
}

G4cout << "\n--> Collector Material Set to: "
<< collector_material->GetName() << G4endl;

}

I do this so I can set materials via UI commands defined in my DetectorMessenger class. I then assign all of my detector volumed to the sensitive detector pointer within the DetectorConstruction class with the following method:
void DetectorConstruction::ConstructSDandField()
{
//need to check and see if the sensitive detector has already been delcared (for when re-initialize geometry)
if (SD == nullptr)
{
SD = new SensitiveDetector(“SD”, “DetectorHitsCollection”);// analysis);
G4SDManager::GetSDMpointer()->AddNewDetector(SD);
}
SetSensitiveDetector(“collector”, SD);
SetSensitiveDetector(“detectorPart2”, SD);
SetSensitiveDetector(“detectorPart3”, SD);
SetSensitiveDetector(“detectorPart4”, SD);
SetSensitiveDetector(“World”, SD);
}

The issues with the UI commands and MT mode started after I added sensitive detector to my code, so I suspect that the root of the issue is there. Does anyone have any suggestions? Any help is greatly appreciated!

Given you change just the material of G4LogicalVolume object without deleting/constructing a new G4LogicalVolume object, you do not need to set again the sensitive detectors.
This should fix your issue.

void DetectorConstruction::ConstructSDandField()
{
//need to check and see if the sensitive detector has already been delcared (for when re-initialize geometry)
if (SD == nullptr)
{
SD = new SensitiveDetector(“SD”, “DetectorHitsCollection”);// analysis);
G4SDManager::GetSDMpointer()->AddNewDetector(SD);
SetSensitiveDetector(“collector”, SD);
SetSensitiveDetector(“detectorPart2”, SD);
SetSensitiveDetector(“detectorPart3”, SD);
SetSensitiveDetector(“detectorPart4”, SD);
SetSensitiveDetector(“World”, SD);
}
}

That worked! Thank you! I’m still experiencing issues with my custom UI commands causing seg faults in MT mode but that’ll be a second post. Thank you so much again!