How do I move an object / detector?

I would like to move an object at a defined velocity. This object contains the detectors.

How do I do that? Is there any example (in general, in specific)?

1 Like

You don’t. The geometry is “frozen,” otherwise Geant4 would not be able to generate a voxelized mapping of coordinates in the world to physical volumes, and it would have to traverse the geometry tree on every step of every track.

There are ways to reposition geometry elements between events within the loop, and there are examples which show this. But that’s not the same as having a continuously moving geometry.

1 Like

The geometry is certainly frozen for each individual event. But you may parameterize the location of an object as a function of the event ID. Then, as long as the speed of the object is much slower than the speed of particles, you may “imitate” the motion of the object. To make this trick work,
1] You must not voxelize the mother volume of the moving object, and
2] It is your responsibility to make sure the moving object not protrude from the mother volume.

1 Like

Hello! @BenjaminW , may I ask for your mail?
I have some problem to discuss with you.

Hi Sheldon

Please use the User Forum.


How exactly? Are here no pms possible?

@BenjaminW I want to have a look in your code where you placed multiple detector at different theta,phi and to distinguish them for readout. This would help me a lot .

I guess it’s better when I try to explain instead of simply posting raw code.

First, in your geometry, you have to position different detectors or the same detector several times, depends on your goal. In my case it is:

G4Box *detector_case = new G4Box(detector_case, 52/2*mm, 9/2*mm, 22/2*mm);|
G4LogicalVolume *logDetector_case = new G4LogicalVolume(detector_case, ABS, logDetector_case);|

G4VPhysicalVolume *physDetector_case1 = new G4PVPlacement(rotM_case, G4ThreeVector((-0.45 + 0.052/2)*m, 0*mm, 0.*mm), logDetector_case, "logDetector_case1", box_detectors_in, false, 200, true);	
G4VPhysicalVolume *physDetector_case2 = new G4PVPlacement(rotM_case, G4ThreeVector(0.*mm, 0*mm, 0.*mm), logDetector_case, "logDetector_case2", box_detectors_in, false, 210, true);

`G4VPhysicalVolume *physDetector1 = new G4PVPlacement(0, G4ThreeVector(0*mm, 0*mm, 0*mm), logicDetector, "physDetector", logDetector_case, false, 500, true);

You can see, that I created logDetector_case which I placed twice via physDetector_case1 and physDetector_case2. Afterwards, I finally positioned a detector inside logDetector_case via physDetector1. When I remember correctly, I didn’t place the detector e.g. inside physDetector_case1 due to the ease of readout aspects.

For distinguishing the detectors which both have as ID = 500, I use the ID of the physDetector_case to know whether a particle went through 210 or 220: if(touchable->GetReplicaNumber(1) == 210){man->FillNtupleDColumn(1, 0, energy); man->AddNtupleRow(1);} or adequately if(touchable->GetReplicaNumber(1) == 220){man->FillNtupleDColumn(1, 0, energy); man->AddNtupleRow(2);}
AddNtupleRow(2) corresponds to a according histogram in the end while (2) would be one for detector 2 and (1) for detector 1.

Does this help?

I forgot to thank you and @makotoasai makotoas for your help!
Could you please elaborate a bit? It would help me already if you could me point to an easy example.

Thanks for the reply. Actually I want to place multiple volume at different theta phi . I tried this way

G4double phi = 0deg, theta = 45deg;
G4ThreeVector rotAxis =
G4ThreeVector(std::sin(theta-90deg), 0., std::cos(theta-90deg));
G4RotationMatrix rotm1 = G4RotationMatrix();
rotm1.rotate (phi, rotAxis);
G4ThreeVector w = G4ThreeVector( std::sin(theta)*std::cos(phi),
std::sin(phi), std::cos(theta)*std::cos(phi));

G4ThreeVector position1 =CeBr3_PosZ2 *w;
G4Transform3D transform1(rotm1,position1);
new G4PVPlacement(transform1,

Here, one detector is placed at 45 deg angle with respect to another. But for placement of multiple detector at different theta and phi i have difficulty. How to place " logic_det" at different theta phi using loop?

G4Transform3D was implemented to correspond the theory of transformations. An object of this class is a 4x3 matrix. A complex transformation can be represented as a sequence of simple transformations (shift, rotation around axes, …). The resulting transformation can be found as a right-to-left product of these simple transformations.

The attached is an example of how to place a number of boxes on the surface of a sphere. You can try it with example B1. (3.7 KB)

This is a very smart way . Here boxes are placed on the surface of a sphere but when I place my source at origin the results must be different because of the sphere.

I’ve just provided an example how to place a logical volume at different theta phi using loop. Sphere does not matter, it can be removed.


Could you tell me where I would do this? E.g. I have a class for the geometry itself (G4VPhysicalVolume *MyDetectorConstruction::Construct()), another one for the particle guns (void MyPrimaryGenerator::GeneratePrimaries(G4Event *anEvent)) and another one where I manage the readout of events (G4bool MySensitiveDetector::ProcessHits(G4Step *aStep, G4TouchableHistory *ROhist)).

I’m having a bit trouble with this as the objects I want to move are inside the geometry class but the events are read out in another class.