Files
minicalosim/src/DetectorConstruction.cc
T
Jan Kieseler 51001d1b59 renamed
2023-10-07 11:14:45 +02:00

321 lines
11 KiB
C++

//
// ********************************************************************
// * License and Disclaimer *
// * *
// * The Geant4 software is copyright of the Copyright Holders of *
// * the Geant4 Collaboration. It is provided under the terms and *
// * conditions of the Geant4 Software License, included in the file *
// * LICENSE and available at http://cern.ch/geant4/license . These *
// * include a list of copyright holders. *
// * *
// * Neither the authors of this software system, nor their employing *
// * institutes,nor the agencies providing financial support for this *
// * work make any representation or warranty, express or implied, *
// * regarding this software system or assume any liability for its *
// * use. Please see the license in the file LICENSE and URL above *
// * for the full disclaimer and the limitation of liability. *
// * *
// * This code implementation is the result of the scientific and *
// * technical work of the GEANT4 collaboration. *
// * By using, copying, modifying or distributing the software (or *
// * any work based on the software) you agree to acknowledge its *
// * use in resulting scientific publications, and indicate your *
// * acceptance of all terms of the Geant4 Software license. *
// ********************************************************************
//
//
/// \file B4/B4a/src/DetectorConstruction.cc
/// \brief Implementation of the B4::DetectorConstruction class
#include "DetectorConstruction.hh"
#include "GeometryDescriptor.hh"
#include "G4Material.hh"
#include "G4NistManager.hh"
#include "G4Box.hh"
#include "G4LogicalVolume.hh"
#include "G4PVPlacement.hh"
#include "G4PVReplica.hh"
#include "G4GlobalMagFieldMessenger.hh"
#include "G4AutoDelete.hh"
#include "G4GeometryManager.hh"
#include "G4PhysicalVolumeStore.hh"
#include "G4LogicalVolumeStore.hh"
#include "G4SolidStore.hh"
#include "G4VisAttributes.hh"
#include "G4Colour.hh"
#include "G4PhysicalConstants.hh"
#include "G4SystemOfUnits.hh"
#include "G4PVParameterised.hh"
#include "G4VPVParameterisation.hh"
#include "G4GeometryManager.hh"
#include "G4PhysicalVolumeStore.hh"
#include "G4LogicalVolumeStore.hh"
#include "G4SolidStore.hh"
namespace B4
{
DetectorConstruction* DetectorConstruction::_global_detector_construction=nullptr;
//helper
class LayerParametrisation: public G4VPVParameterisation{
public:
LayerParametrisation(Layer layer, G4double position = 0): G4VPVParameterisation(), layer(layer), position(position){
}
~LayerParametrisation() = default;
void ComputeTransformation(const G4int copyNo, G4VPhysicalVolume* physVol) const{
G4double x = 0;
G4double y = 0;
G4double z = 0;
if(layer.nx > 1){
x = (copyNo % layer.nx) * layer.sens_xwidth*cm;
}
if(layer.ny > 1){
y = (copyNo / layer.nx) * layer.sens_ywidth*cm;
}
G4ThreeVector origin(x, y, z);
origin -= G4ThreeVector(layer.sens_xwidth * ((float)layer.nx-1) / 2.*cm, layer.sens_ywidth * ((float)layer.ny-1) / 2.*cm, 0);
origin += G4ThreeVector(0., 0., position);
physVol->SetTranslation(origin);
}
void ComputeDimensions(G4Box& box, const G4int copyNo, const G4VPhysicalVolume* physVol) const{
box.SetXHalfLength(layer.sens_xwidth/2*cm);
box.SetYHalfLength(layer.sens_ywidth/2*cm);
box.SetZHalfLength(layer.thickness/2*cm);
}
private:
Layer layer;
G4double position;
};
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
G4ThreadLocal
G4GlobalMagFieldMessenger* DetectorConstruction::fMagFieldMessenger = nullptr;
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
G4VPhysicalVolume* DetectorConstruction::Construct()
{
fCheckOverlaps=true;
// Define materials
DefineMaterials();
// Define volumes
return DefineVolumes();
}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
void DetectorConstruction::DefineMaterials()
{
// Lead material defined using NIST Manager
auto nistManager = G4NistManager::Instance();
nistManager->FindOrBuildMaterial("G4_AIR");
auto cwLayers = cw->getLayers();
for(auto layer : cwLayers){
nistManager->FindOrBuildMaterial(layer.material);
}
// Print materials
G4cout << *(G4Material::GetMaterialTable()) << G4endl;
}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
G4VPhysicalVolume* DetectorConstruction::DefineVolumes()
{
G4GeometryManager::GetInstance()->OpenGeometry();
G4PhysicalVolumeStore::GetInstance()->Clean();
G4LogicalVolumeStore::GetInstance()->Clean();
G4SolidStore::GetInstance()->Clean();
// Geometry parameters
auto & cwLayers = cw->getLayers();
G4int nofLayers = cwLayers.size();
G4double caloLength = 0;
for(auto layer : cwLayers){
caloLength += layer.thickness * cm;
}
G4double calorSizeXY = cw->getXYWidth() * cm;
auto worldSizeXY = 1.2 * calorSizeXY;
auto worldSizeZ = 1.2 * caloLength;
// Get materials
auto defaultMaterial = G4Material::GetMaterial("G4_AIR");
//
// World
//
auto worldS
= new G4Box("World", // its name
worldSizeXY/2, worldSizeXY/2, worldSizeZ/2); // its size
auto worldLV
= new G4LogicalVolume(
worldS, // its solid
defaultMaterial, // its material
"World"); // its name
auto worldPV = new G4PVPlacement(nullptr, // no rotation
G4ThreeVector(), // at (0,0,0)
worldLV, // its logical volume
"World", // its name
nullptr, // its mother volume
false, // no boolean operation
0, // copy number
fCheckOverlaps); // checking overlaps
//
// Calorimeter
//
auto calorimeterS
= new G4Box("Calorimeter", // its name
calorSizeXY/2, calorSizeXY/2, caloLength/2); // its size
auto calorLV
= new G4LogicalVolume(
calorimeterS, // its solid
defaultMaterial, // its material
"Calorimeter"); // its name
new G4PVPlacement(nullptr, // no rotation
G4ThreeVector(0,0,0), // at (0,0,0)
calorLV, // its logical volume
"Calorimeter", // its name
worldLV, // its mother volume
false, // no boolean operation
0, // copy number
fCheckOverlaps); // checking overlaps
//
// construct layers here; this is where the layers are added to the calorimeter
// they will be flagged active or inactive based on the isActive flag later in
// the ActionInitialization by passing the ConstructioWrapper to the EventAction class.
//
G4double position = -caloLength/2;
int layerNumber = 0;
for(auto& layer : cwLayers){
layer.name = "Layer_"+std::to_string(layerNumber);
layerNumber++;
G4cout << "building layer " << layer.name << G4endl;
position += layer.thickness / 2 *cm;
auto layerS
= new G4Box(layer.name, // its name
calorSizeXY/2, calorSizeXY/2, layer.thickness/2 *cm); // its size
auto layerLV
= new G4LogicalVolume(
layerS, // its solid
defaultMaterial, //G4Material::GetMaterial(layer.material), // its material
layer.name); // its name
//set layerLV to be visible and green if active and transparent otherwise
if(layer.isActive){
auto layerVisAtt = new G4VisAttributes(G4Colour(0.0,1.0,0.0));
layerVisAtt->SetVisibility(true);
layerVisAtt->SetForceSolid(true);
layerLV->SetVisAttributes(layerVisAtt);
} //else set the absorbers to be transparent
else{
auto layerVisAtt = new G4VisAttributes(G4Colour(1.0,1.0,1.0,0.1));
layerVisAtt->SetVisibility(true);
layerVisAtt->SetForceSolid(true);
layerLV->SetVisAttributes(layerVisAtt);
}
auto sensorLV = new G4LogicalVolume(
new G4Box("sensor", layer.sens_xwidth/2*cm, layer.sens_ywidth/2*cm, layer.thickness/2*cm),
G4Material::GetMaterial(layer.material),
"sensor");
//needs RepeatPlacement for xy granularity
//use G4PVParameterised to create a grid of sensitive detectors
auto ppv = new G4PVParameterised(layer.name,
sensorLV,
layerLV, kUndefined,
layer.nx*layer.ny, new LayerParametrisation(layer,0.));
auto pv = new G4PVPlacement(nullptr, // no rotation
G4ThreeVector(0,0,position), // at (0,0,0)
layerLV, // its logical volume
layer.name, // its name
calorLV, // its mother volume
false, // no boolean operation
0, // copy number
fCheckOverlaps); // checking overlaps
layer.assignPhysicalVolume(ppv); //maybe this needs to be ppv - check
// assign sensors to layer by copyNumber; access the copyNumber of the volumes
if(layer.isActive){
for(int i = 0; i < layer.nx*layer.ny; i++){
Sensor sensor;
sensor.position = G4ThreeVector(layer.sens_xwidth * (i % layer.nx) - layer.sens_xwidth * ((float)layer.nx-1) / 2.*cm,
layer.sens_ywidth * (i / layer.nx) - layer.sens_ywidth * ((float)layer.ny-1) / 2.*cm, position);
sensor.size = G4ThreeVector(layer.sens_xwidth, layer.sens_ywidth, layer.thickness);
sensor.energy = 0;
layer.sensors.push_back(sensor);//this should now be aligned with copy number
}
}
position += layer.thickness / 2 *cm; //assign the physical volume to the layer
}
//
// Visualization attributes
//
worldLV->SetVisAttributes (G4VisAttributes::GetInvisible());
auto simpleBoxVisAtt= new G4VisAttributes(G4Colour(1.0,1.0,1.0,0.));
simpleBoxVisAtt->SetVisibility(true);
calorLV->SetVisAttributes(simpleBoxVisAtt);
//
// Always return the physical World
//
return worldPV;
}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
void DetectorConstruction::ConstructSDandField()
{
// Create global magnetic field messenger.
// Uniform magnetic field is then created automatically if
// the field value is not zero.
G4ThreeVector fieldValue;
fMagFieldMessenger = new G4GlobalMagFieldMessenger(fieldValue);
fMagFieldMessenger->SetVerboseLevel(1);
// Register the field messenger for deleting
G4AutoDelete::Register(fMagFieldMessenger);
}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
}