Hello everyone !
I am facing difficulty in running my detector after introducing sensitive detector in it. Here is the code
void DetectorConstruction::ConstructSDandField()
{
auto sdManager = G4SDManager::GetSDMpointer();
G4String SDname;
MySensitiveDetector* sensDet = new MySensitiveDetector(SDname="/sensDet");
sdManager->AddNewDetector(sensDet);
logicDetector->SetSensitiveDetector(sensDet);
}
The last line is creating some issue because when I comment it out, it runs but I am not able to implement sensitive detector part in it. Kindly help !
Where is logicDetector filled? Is it a data member of your DetectorConstruction class? If it is not properly filled with a valid G4LogicalVolume pointer, then you’re dereferencing an invalid pointer and of course your job crashes.
Also, you probably don’t want to instantiate and register a new SD object every time. If you ever run a job with multiple run, that will cause errors. What you should do instead is initialize SDname with the name string first, then query the sdManager to get a pointer to the SD. If that get request fails, then create a new SD and register it into sdManager.
G4Tubs* solidDetector = new G4Tubs("Detector",0.*cm,5*cm, 0.5*cm, 0*deg, 360*deg);
logicDetector = new G4LogicalVolume(solidDetector, worldMat, "logicDetector");
and you are good to go. Otherwise you create a local “logicDetector” that shadows the class member logicDetector (should be one of the compiler warnings btw).
No. Do not do this. ConstructSDandField() is called separately by the worker threads. If you do that in the master thread, you will have problems. The correct thing to do, as @weller wrote, is to make sure you’re filling the data members, and not creating (and then discarding) a local variable with the same name.
Hi, John. The reason for doing this is so that the created volume pointers can be referenced later, when the worker threads call ConstructSDandField(). Otherwise, the user would need to hardcode the volume names and search through the geometry store to get the pointers. We do the same thing in our simulation.
The issue is resolved. Thank you so much everyone for your responses. I carefully checked the code and called define materials function in detection constructions class. The fault vanished.