# Making logical volumes in loops into Sensitive Detectors

I am making 10 layers of 100x100 Silicon arrays of varying sizes from the centre.

``````G4double halfsize_si_mm  = {0.25, 0.50, 0.75, 1.0, 1.25, 1.50 , 1.75, 2.0 , 2.25, 2.50}; //size of each Si cube in mm
G4double edge  = {2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20, 22.5, 25};// the edge of each layer where 1st cube is built
G4double distance  = {5, 10, 15, 20, 25, 30, 35, 40, 45, 50}; //distance of each layer from center in cm
G4double length = {5. , 10., 15., 20., 25., 30., 35., 40., 45., 50. };// size of each layer in cm

for (G4int ly = 0 ; ly<10; ++ly){

G4Box *targetSolid = new G4Box("solid-Target", halfsize_si_mm[ly]*mm, 0.15*mm, halfsize_si_mm[ly]*mm);
G4LogicalVolume* targetLogical = new G4LogicalVolume(targetSolid, targetMaterial, "logic-Target");
// create an array of sensitive detectors
for (G4int i =0 ; i< 100; i++){
for(G4int j =0 ; j<100; j++){
fTargetPhysical = new G4PVPlacement(0,G4ThreeVector(-1*edge[ly]*cm + (i*length[ly])*cm/100, distance[ly]*cm, -1*edge[ly]*cm + (j*length[ly])*cm/100 ),targetLogical, "Target", worldLogical, false, j+i*100, false);
}
}
}
``````

I want to make 10 layers of the Sensitive Detectors, but I realised that the Logical Volume definition is in the loop, hence the Logical Volume definition is destroyed after the loop is over. The only way I can figure out to fix this is to write separate definitions 10 times, each with a different name. Then use the SetSensitiveDetector 10 times at the ConstructSD. But are there better ways of doing this?