#include "DetectorConstruction.h"
#include "G4BasePhantomBuilder.h"
#include "G4CustomFemaleBuilder.h"
#include "G4FemaleBuilder.h"
#include "G4HumanPhantomMaterial.h"
#include "G4HumanPhantomMessenger.h"
#include "G4HumanPhantomSD.h"
#include "G4MaleBuilder.h"
#include "Material.h"

#include "G4Box.hh"
#include "G4Cons.hh"
#include "G4IntersectionSolid.hh"
#include "G4LogicalVolume.hh"
#include "G4PVPlacement.hh"
#include "G4SDManager.hh"
#include "G4Sphere.hh"
#include "G4SubtractionSolid.hh"
#include "G4SystemOfUnits.hh"
#include "G4Trd.hh"
#include "G4Tubs.hh"
#include "G4UnionSolid.hh"

DetectorConstruction::DetectorConstruction() {
    material = new G4HumanPhantomMaterial();
    messenger = new G4HumanPhantomMessenger(this);
}

DetectorConstruction::~DetectorConstruction() {
    delete material;
    delete messenger;
}

void DetectorConstruction::ConstructSectionSphere(G4LogicalVolume* fMotherLogical, G4double zBias) {
    G4double pRadius = 2.8 * m / 2;
    G4double pRadius2 = 1.2 * m;
    G4double pRadiusMid = pRadius - 10 * cm;
    G4ThreeVector pos0 = G4ThreeVector();
    G4ThreeVector pos1 = G4ThreeVector(0, 0, zBias + 0.91 * m);

    // 大环境
    G4Sphere* solidWorld = new G4Sphere("NodeWorld", 0, pRadius, 0, 360 * deg, 0, 180 * deg);
    G4LogicalVolume* logicWorld = new G4LogicalVolume(solidWorld, G4Material::GetMaterial("Vacuum"), "NodeWorld");
    new G4PVPlacement(0, pos1, logicWorld, "NodeWorld", fMotherLogical, false, 0, true);

    // 外壳
    G4Sphere* solidShell = new G4Sphere("NodeShell", 0, pRadius, 0, 360 * deg, 0, 139.5 * deg);
    G4LogicalVolume* logicShell = new G4LogicalVolume(solidShell, G4Material::GetMaterial("Vacuum"), "NodeShell");
    new G4PVPlacement(0, pos0, logicShell, "NodeShell", logicWorld, false, 0, true);

    G4Sphere* solidShellAl1 = new G4Sphere("NodeShellAl1", pRadius - 2, pRadius, 0, 360 * deg, 0, 139.5 * deg);
    G4LogicalVolume* logicShellAl1 =
        new G4LogicalVolume(solidShellAl1, G4Material::GetMaterial("AluminumAlloySeries5"), "NodeShellAl1");
    new G4PVPlacement(0, pos0, logicShellAl1, "NodeShellAl1", logicShell, false, 0, true);

    G4Sphere* solidShellTap = new G4Sphere("NodeShellTap", pRadius - 55, pRadius - 45, 0, 360 * deg, 0, 139.5 * deg);
    G4LogicalVolume* logicShellTap =
        new G4LogicalVolume(solidShellTap, G4Material::GetMaterial("Taparan"), "NodeShellTap");
    new G4PVPlacement(0, pos0, logicShellTap, "NodeShellTap", logicShell, false, 0, true);

    G4Sphere* solidShellAl2 = new G4Sphere("NodeShellAl2", pRadiusMid, pRadiusMid + 5, 0, 360 * deg, 0, 139.5 * deg);
    G4LogicalVolume* logicShellAl2 =
        new G4LogicalVolume(solidShellAl2, G4Material::GetMaterial("AluminumAlloySeries5"), "NodeShellAl2");
    new G4PVPlacement(0, pos0, logicShellAl2, "NodeShellAl2", logicShell, false, 0, true);

    // 填充
    G4Sphere* solidMid = new G4Sphere("NodeMid", pRadius2, pRadius, 0, 360 * deg, 0, 139.5 * deg);
    G4LogicalVolume* logicMid = new G4LogicalVolume(solidMid, G4Material::GetMaterial("midMaterial"), "NodeMid");
    new G4PVPlacement(0, pos0, logicMid, "NodeMid", logicWorld, false, 0, true);

    // 空气
    G4Sphere* solidAir = new G4Sphere("NodeAir", 0, pRadius2, 0, 360 * deg, 0, 180 * deg);
    G4LogicalVolume* logicAir = new G4LogicalVolume(solidAir, G4Material::GetMaterial("G4_AIR"), "NodeAir");
    new G4PVPlacement(0, pos0, logicAir, "NodeAir", logicWorld, false, 0, true);
}

void DetectorConstruction::ConstructSectionCons(G4String name, G4LogicalVolume* fMotherLogical, G4double zBias,
                                                G4double pRmax1, G4double pRmin1, G4double pRmax2, G4double pRmin2,
                                                G4double hz) {
    G4double pRminMid = pRmin1 - 10 * cm;
    G4double pRmaxMid = pRmax1 - 10 * cm;
    G4ThreeVector pos0 = G4ThreeVector();
    G4ThreeVector pos1 = G4ThreeVector(0, 0, zBias);

    // 大环境
    G4Tubs* solidWorld = new G4Tubs(name + "World", 0, pRmax1, hz, 0, 360);
    G4LogicalVolume* logicWorld = new G4LogicalVolume(solidWorld, G4Material::GetMaterial("Vacuum"), name + "World");
    new G4PVPlacement(0, pos1, logicWorld, name + "World", fMotherLogical, false, 0, true);

    // 外壳
    G4Cons* solidShell = new G4Cons(name + "Shell", 0, pRmax1, 0, pRmin1, hz, 0, 360);
    G4LogicalVolume* logicShell = new G4LogicalVolume(solidShell, G4Material::GetMaterial("Vacuum"), name + "Shell");
    new G4PVPlacement(0, pos0, logicShell, name + "Shell", logicWorld, false, 0, true);

    G4Cons* solidShellAl1 = new G4Cons(name + "ShellAl1", pRmax1 - 2, pRmax1, pRmin1 - 2, pRmin1, hz, 0, 360);
    G4LogicalVolume* logicShellAl1 =
        new G4LogicalVolume(solidShellAl1, G4Material::GetMaterial("AluminumAlloySeries5"), name + "ShellAl1");
    new G4PVPlacement(0, pos0, logicShellAl1, name + "ShellAl1", logicShell, false, 0, true);

    G4Cons* solidShellTap =
        new G4Cons(name + "ShellTap", pRmax1 - 55, pRmax1 - 45, pRmin1 - 55, pRmin1 - 45, hz, 0, 360);
    G4LogicalVolume* logicShellTap =
        new G4LogicalVolume(solidShellTap, G4Material::GetMaterial("Taparan"), name + "ShellTap");
    new G4PVPlacement(0, pos0, logicShellTap, name + "ShellTap", logicShell, false, 0, true);

    G4Cons* solidShellAl2 = new G4Cons(name + "ShellAl2", pRmaxMid, pRmaxMid + 5, pRminMid, pRminMid + 5, hz, 0, 360);
    G4LogicalVolume* logicShellAl2 =
        new G4LogicalVolume(solidShellAl2, G4Material::GetMaterial("AluminumAlloySeries5"), name + "ShellAl2");
    new G4PVPlacement(0, pos0, logicShellAl2, name + "ShellAl2", logicShell, false, 0, true);

    // 填充
    G4Cons* solidMidCons = new G4Cons(name + "MidCons", 0, pRmax1, 0, pRmin1, hz, 0, 360);
    G4Trd* solidMidTrd = new G4Trd(name + "MidTrd", pRmax2, pRmin2, pRmax2, pRmin2, hz);
    G4SubtractionSolid* solidMid = new G4SubtractionSolid(name + "Mid", solidMidCons, solidMidTrd);
    G4LogicalVolume* logicMid = new G4LogicalVolume(solidMid, G4Material::GetMaterial("midMaterial"), name + "Mid");
    new G4PVPlacement(0, pos0, logicMid, name + "Mid", logicWorld, false, 0, true);

    // 空气
    G4Trd* solidAir = new G4Trd(name + "Air", pRmax2, pRmin2, pRmax2, pRmin2, hz);
    G4LogicalVolume* logicAir = new G4LogicalVolume(solidAir, G4Material::GetMaterial("G4_AIR"), name + "Air");
    new G4PVPlacement(0, pos0, logicAir, name + "Air", logicWorld, false, 0, true);
}

void DetectorConstruction::ConstructSectionSmall(G4LogicalVolume* fMotherLogical, G4double zBias) {
    G4double pRmax = 2.8 / 2 * m;
    G4double hz = 5.18 / 2 * m;
    G4double hxBox = 1.5 / 2 * m;
    G4double pRmaxMid = pRmax - 10 * cm;
    G4ThreeVector pos0 = G4ThreeVector();
    G4ThreeVector pos1 = G4ThreeVector(0, 0, 8.5);

    // 大环境
    G4Tubs* solidWorld = new G4Tubs("SmallWorld", 0, pRmax, hz, 0, 360);
    G4LogicalVolume* logicWorld = new G4LogicalVolume(solidWorld, G4Material::GetMaterial("Vacuum"), "SmallWorld");
    new G4PVPlacement(0, G4ThreeVector(0, 0, zBias + hz), logicWorld, "SmallWorld", fMotherLogical, false, 0, true);

    // 外壳 2mm铝 + 10mm芳纶 + 5mm铝
    G4Tubs* solidShell = new G4Tubs("SmallShell", pRmaxMid, pRmax, hz, 0, 360);
    G4LogicalVolume* logicShell = new G4LogicalVolume(solidShell, G4Material::GetMaterial("Vacuum"), "SmallShell");
    new G4PVPlacement(0, pos1, logicShell, "SmallShell", logicWorld, false, 0, true);

    G4Tubs* solidShellAl1 = new G4Tubs("SmallShellAl1", pRmax - 2, pRmax, hz, 0, 360);
    G4LogicalVolume* logicShellAl1 =
        new G4LogicalVolume(solidShellAl1, G4Material::GetMaterial("AluminumAlloySeries5"), "SmallShellAl1");
    new G4PVPlacement(0, pos0, logicShellAl1, "SmallShellAl1", logicShell, false, 0, true);

    G4Tubs* solidShellTap = new G4Tubs("SmallShellTap", pRmax - 55, pRmax - 45, hz, 0, 360);
    G4LogicalVolume* logicShellTap =
        new G4LogicalVolume(solidShellTap, G4Material::GetMaterial("Taparan"), "SmallShellTap");
    new G4PVPlacement(0, pos0, logicShellTap, "SmallShellTap", logicShell, false, 0, true);

    G4Tubs* solidShellAl2 = new G4Tubs("SmallShellAl2", pRmaxMid, pRmaxMid + 5, hz, 0, 360);
    G4LogicalVolume* logicShellAl2 =
        new G4LogicalVolume(solidShellAl2, G4Material::GetMaterial("AluminumAlloySeries5"), "SmallShellAl2");
    new G4PVPlacement(0, pos0, logicShellAl2, "SmallShellAl2", logicShell, false, 0, true);

    // 填充
    G4Tubs* solidMidTubs = new G4Tubs("SmallMidTubs", 0, pRmaxMid, hz, 0, 360);
    G4Box* solidMidBox = new G4Box("SmallMidBox", hxBox, hxBox, hz);
    G4SubtractionSolid* solidMid = new G4SubtractionSolid("SmallMid", solidMidTubs, solidMidBox);
    G4LogicalVolume* logicMid = new G4LogicalVolume(solidMid, G4Material::GetMaterial("midMaterial"), "SmallMid");
    new G4PVPlacement(0, pos1, logicMid, "SmallMid", logicWorld, false, 0, true);

    // 空气
    G4Box* solidAir = new G4Box("SmallAirBox", hxBox, hxBox, hz);
    G4LogicalVolume* logicAir = new G4LogicalVolume(solidAir, G4Material::GetMaterial("G4_AIR"), "SmallAir");
    new G4PVPlacement(0, pos1, logicAir, "SmallAir", logicWorld, false, 0, true);
}

void DetectorConstruction::ConstructSectionBig(G4LogicalVolume* fMotherLogical, G4double zBias) {
    G4double pRmax = 4.2 / 2 * m;
    G4double hz = 6.72 / 2 * m;
    G4double hxBox = 2.4 / 2 * m;
    G4double pRmaxMid = pRmax - 10 * cm;
    G4ThreeVector pos0 = G4ThreeVector();
    G4ThreeVector pos1 = G4ThreeVector(0, 0, 8.5);

    // 大环境
    G4Tubs* solidWorld = new G4Tubs("BigWorld", 0, pRmax, hz + 8.5, 0, 360);
    G4LogicalVolume* logicWorld = new G4LogicalVolume(solidWorld, G4Material::GetMaterial("Vacuum"), "BigWorld");
    new G4PVPlacement(0, G4ThreeVector(0, 0, -(zBias + hz + 8.5)), logicWorld, "BigWorld", fMotherLogical, false, 0,
                      true);

    // 外壳 2mm铝 + 10mm芳纶 + 5mm铝
    G4Tubs* solidShell = new G4Tubs("BigShell", pRmaxMid, pRmax, hz, 0, 360);
    G4LogicalVolume* logicShell = new G4LogicalVolume(solidShell, G4Material::GetMaterial("Vacuum"), "BigShell");
    new G4PVPlacement(0, pos1, logicShell, "BigShell", logicWorld, false, 0, true);

    G4Tubs* solidShellAl1 = new G4Tubs("BigShellAl1", pRmax - 2, pRmax, hz, 0, 360);
    G4LogicalVolume* logicShellAl1 =
        new G4LogicalVolume(solidShellAl1, G4Material::GetMaterial("AluminumAlloySeries5"), "BigShellAl1");
    new G4PVPlacement(0, pos0, logicShellAl1, "BigShellAl1", logicShell, false, 0, true);

    G4Tubs* solidShellTap = new G4Tubs("BigShellTap", pRmax - 55, pRmax - 45, hz, 0, 360);
    G4LogicalVolume* logicShellTap =
        new G4LogicalVolume(solidShellTap, G4Material::GetMaterial("Taparan"), "BigShellTap");
    new G4PVPlacement(0, pos0, logicShellTap, "BigShellTap", logicShell, false, 0, true);

    G4Tubs* solidShellAl2 = new G4Tubs("BigShellAl2", pRmaxMid, pRmaxMid + 5, hz, 0, 360);
    G4LogicalVolume* logicShellAl2 =
        new G4LogicalVolume(solidShellAl2, G4Material::GetMaterial("AluminumAlloySeries5"), "BigShellAl2");
    new G4PVPlacement(0, pos0, logicShellAl2, "BigShellAl2", logicShell, false, 0, true);

    // 填充
    G4Tubs* solidMidTubs = new G4Tubs("BigMidTubs", 0, pRmaxMid, hz, 0, 360);
    G4Box* solidMidBox = new G4Box("BigMidBox", hxBox, hxBox, hz);
    G4SubtractionSolid* solidMid = new G4SubtractionSolid("BigMid", solidMidTubs, solidMidBox);
    G4LogicalVolume* logicMid = new G4LogicalVolume(solidMid, G4Material::GetMaterial("midMaterial"), "BigMid");
    new G4PVPlacement(0, pos1, logicMid, "BigMid", logicWorld, false, 0, true);

    // 空气
    G4Box* solidAir = new G4Box("BigAirBox", hxBox, hxBox, hz);
    G4LogicalVolume* logicAir = new G4LogicalVolume(solidAir, G4Material::GetMaterial("G4_AIR"), "BigAir");
    new G4PVPlacement(0, pos1, logicAir, "BigAir", logicWorld, false, 0, true);

    // 尾部 5mm铝 + 10mm芳纶 + 2mm铝
    G4Tubs* solidTail = new G4Tubs("BigTail", 0, pRmax, 8.5, 0, 360);
    G4LogicalVolume* logicTail = new G4LogicalVolume(solidTail, G4Material::GetMaterial("Vacuum"), "BigTail");
    new G4PVPlacement(0, G4ThreeVector(0, 0, -(hz + 8.5)), logicTail, "BigTail", logicWorld, false, 0, true);

    G4Tubs* solidTailAl1 = new G4Tubs("BigTailAl1", 0, pRmax, 2.5, 0, 360);
    G4LogicalVolume* logicTailAl1 =
        new G4LogicalVolume(solidTailAl1, G4Material::GetMaterial("AluminumAlloySeries5"), "BigTailAl1");
    new G4PVPlacement(0, G4ThreeVector(0, 0, 6), logicTailAl1, "BigTailAl1", logicTail, false, 0, true);

    G4Tubs* solidTailTap = new G4Tubs("BigTailTap", 0, pRmax, 5, 0, 360);
    G4LogicalVolume* logicTailTap = new G4LogicalVolume(solidTailTap, G4Material::GetMaterial("Taparan"), "BigTailTap");
    new G4PVPlacement(0, G4ThreeVector(0, 0, -1.5), logicTailTap, "BigTailTap", logicTail, false, 0, true);

    G4Tubs* solidTailAl2 = new G4Tubs("BigTailAl2", 0, pRmax, 1, 0, 360);
    G4LogicalVolume* logicTailAl2 =
        new G4LogicalVolume(solidTailAl2, G4Material::GetMaterial("AluminumAlloySeries5"), "BigTailAl2");
    new G4PVPlacement(0, G4ThreeVector(0, 0, -7.5), logicTailAl2, "BigTailAl2", logicTail, false, 0, true);
}

void DetectorConstruction::ConstructHumanPhantom(G4VPhysicalVolume* fMotherPhysics) {
    material->DefineMaterials();
    G4BasePhantomBuilder* builder = 0;

    if (sex == "Female")
        builder = new G4FemaleBuilder;
    else
        builder = new G4MaleBuilder;

    builder->SetModel(model);
    builder->SetMotherVolume(fMotherPhysics);

    // the argument indicates the sensitivity of the volume
    builder->BuildHead("blue", false, sensitivities["Head"]);
    builder->BuildSkull("orange", false, sensitivities["Skull"]);
    builder->BuildBrain("yellow", true, sensitivities["Brain"]);

    if (model != "MIRDHead") {
        builder->BuildTrunk("yellow", false, sensitivities["Trunk"]);

        builder->BuildLeftLeg("yellow", false, sensitivities["LeftLeg"]);
        builder->BuildRightLeg("yellow", false, sensitivities["RightLeg"]);

        builder->BuildLeftArmBone("grey", true, sensitivities["LeftArmBone"]);
        builder->BuildRightArmBone("grey", true, sensitivities["RightArmBone"]);

        builder->BuildLeftLegBone("grey", true, sensitivities["LeftLegBone"]);
        builder->BuildRightLegBone("grey", true, sensitivities["RightLegBone"]);

        builder->BuildUpperSpine("yellow", true, sensitivities["UpperSpine"]);

        builder->BuildLeftScapula("grey", true, sensitivities["LeftScapula"]);
        builder->BuildRightScapula("grey", true, sensitivities["RightScapula"]);
        builder->BuildLeftAdrenal("yellow", true, sensitivities["LeftAdrenal"]);
        builder->BuildRightAdrenal("yellow", true, sensitivities["RightAdrenal"]);
        builder->BuildThymus("orange", true, sensitivities["Thymus"]);
        builder->BuildLeftClavicle("grey", true, sensitivities["LeftClavicle"]);
        builder->BuildRightClavicle("grey", true, sensitivities["RightClavicle"]);
        builder->BuildSmallIntestine("orange", true, sensitivities["SmallIntestine"]);
        builder->BuildRibCage("grey", true, sensitivities["RibCage"]);

        builder->BuildMiddleLowerSpine("yellow", true, sensitivities["MiddleLowerSpine"]);

        builder->BuildPelvis("grey", true, sensitivities["Pelvis"]);

        builder->BuildStomach("orange", true, sensitivities["Stomach"]);
        builder->BuildUpperLargeIntestine("lightBlue", true, sensitivities["UpperLargeIntestine"]);
        builder->BuildLowerLargeIntestine("lightBlue", true, sensitivities["LowerLargeIntestine"]);

        builder->BuildSpleen("green", true, sensitivities["Spleen"]);
        builder->BuildPancreas("purple", true, sensitivities["Pancreas"]);
        builder->BuildLiver("orange", true, sensitivities["Liver"]);

        builder->BuildLeftKidney("green", true, sensitivities["LeftKidney"]);
        builder->BuildRightKidney("green", true, sensitivities["RightKidney"]);
        builder->BuildUrinaryBladder("green", true, sensitivities["UrinaryBladder"]);

        builder->BuildHeart("red", true, sensitivities["Hearth"]);  // to do MIRD
        builder->BuildLeftLung("blue", true, sensitivities["LeftLung"]);
        builder->BuildRightLung("blue", true, sensitivities["RightLung"]);
        builder->BuildThyroid("orange", true, sensitivities["Thyroid"]);

        if (sex == "Female") {
            builder->BuildLeftOvary("purple", true, sensitivities["LeftOvary"]);
            builder->BuildRightOvary("purple", true, sensitivities["RightOvary"]);
            builder->BuildUterus("purple", true, sensitivities["Uterus"]);

            if (model == "MIRD") {
                builder->BuildLeftBreast("purple", true, sensitivities["LeftBreast"]);
                builder->BuildRightBreast("purple", true, sensitivities["RightBreast"]);
            } else {
                builder->BuildVoxelLeftBreast("purple", false, sensitivities["LeftBreast"]);
                builder->BuildVoxelRightBreast("purple", false, sensitivities["RightBreast"]);
            }
        }

        if (sex == "Male") {
            builder->BuildMaleGenitalia("yellow", false, sensitivities["MaleGenitalia"]);
            builder->BuildLeftTeste("purple", true, sensitivities["LeftTeste"]);
            builder->BuildRightTeste("purple", true, sensitivities["RightTeste"]);
        }
    }

    delete builder;
}

G4VPhysicalVolume* DetectorConstruction::Construct() {
    // Define materials
    DefineMaterials();

    // World
    G4Sphere* solidWorld = new G4Sphere("World", 0, 15. * m, 0, 360 * deg, 0, 180 * deg);
    G4LogicalVolume* logicWorld = new G4LogicalVolume(solidWorld, G4Material::GetMaterial("Vacuum"), "World");
    G4VPhysicalVolume* physicsWorld = new G4PVPlacement(0, G4ThreeVector(), logicWorld, "World", 0, false, 0, true);

    // 节点舱
    ConstructSectionSphere(logicWorld, (5.18 + 0.33) * m);
    // 过渡段 1
    ConstructSectionCons("Trans1", logicWorld, 5.18 * m + 0.33 * m / 2, 2.8 * m / 2, 2.13 * m / 2, 1.5 * m / 2,
                         1.22 * m / 2, 0.33 * m / 2);
    // 小柱段
    ConstructSectionSmall(logicWorld, 0);
    // 过渡段 2
    ConstructSectionCons("Trans2", logicWorld, -0.815 * m / 2, 4.2 * m / 2, 2.8 * m / 2, 2.4 * m / 2, 1.5 * m / 2,
                         0.815 * m / 2);
    // 大柱段
    ConstructSectionBig(logicWorld, 0.815 * m);
    // 体模
    G4RotationMatrix* rotationMatrix = new G4RotationMatrix();
    rotationMatrix->rotateX(90. * deg);

    G4Box* solidPhantom = new G4Box("Phantom", 2. * m, 1. * m, 1. * m);
    G4LogicalVolume* logicPhantom = new G4LogicalVolume(solidPhantom, G4Material::GetMaterial("Vacuum"), "Phantom");
    G4VPhysicalVolume* physicsPhantom = new G4PVPlacement(rotationMatrix, G4ThreeVector(0, 0, -1 * m), logicPhantom,
                                                          "Phantom", logicWorld, false, 0, true);
    ConstructHumanPhantom(physicsPhantom);

    return physicsWorld;
}

void DetectorConstruction::SetBodyPartSensitivity(G4String, G4bool) {
    G4cout << "This method is not currently working !!!!" << G4endl;
}

void DetectorConstruction::SetPhantomSex(G4String newSex) {
    sex = newSex;

    if (sex == "Male") G4cout << ">> Male Phantom will be built." << G4endl;
    if (sex == "Female") G4cout << ">> Female Phantom will be built." << G4endl;
    if ((sex != "Female") && (sex != "Male")) G4cout << sex << " can not be defined!" << G4endl;
}

void DetectorConstruction::SetPhantomModel(G4String newModel) {
    model = newModel;

    if (model == "MIRD") G4cout << " >> Phantom " << model << " will be built." << G4endl;
    if (model == "MIRDHead") G4cout << " >> Phantom " << model << " will be built." << G4endl;
    if ((model != "MIRD") && (model != "MIRDHead")) G4cout << model << " can not be defined!" << G4endl;
}

void DetectorConstruction::ConstructSDandField() {
    G4HumanPhantomSD* SD = new G4HumanPhantomSD("SD", "HumanPhantomCollection");
    G4SDManager::GetSDMpointer()->AddNewDetector(SD);

    SetSensitiveDetector("logicalHead", SD);
    SetSensitiveDetector("logicalSkull", SD);
    SetSensitiveDetector("logicalBrain", SD);

    if (model != "MIRDHead") {
        SetSensitiveDetector("logicalTrunk", SD);
        SetSensitiveDetector("logicalLeftLeg", SD);
        SetSensitiveDetector("logicalRightLeg", SD);
        SetSensitiveDetector("logicalLeftArmBone", SD);
        SetSensitiveDetector("logicalRightArmBone", SD);
        SetSensitiveDetector("logicalLeftLegBone", SD);
        SetSensitiveDetector("logicalRightLegBone", SD);
        SetSensitiveDetector("logicalUpperSpine", SD);
        SetSensitiveDetector("logicalLeftScapula", SD);
        SetSensitiveDetector("logicalRightScapula", SD);
        SetSensitiveDetector("logicalLeftAdrenal", SD);
        SetSensitiveDetector("logicalRightAdrenal", SD);
        SetSensitiveDetector("logicalThymus", SD);
        SetSensitiveDetector("logicalLeftClavicle", SD);
        SetSensitiveDetector("logicalRightClavicle", SD);
        SetSensitiveDetector("logicalSmallIntestine", SD);
        SetSensitiveDetector("logicalRibCage", SD);
        SetSensitiveDetector("logicalMiddleLowerSpine", SD);
        SetSensitiveDetector("logicalStomach", SD);
        SetSensitiveDetector("logicalUpperLargeIntestine", SD);
        SetSensitiveDetector("logicalLowerLargeIntestine", SD);
        SetSensitiveDetector("logicalSpleen", SD);
        SetSensitiveDetector("logicalPancreas", SD);
        SetSensitiveDetector("logicalLeftKidney", SD);
        SetSensitiveDetector("logicalRightKidney", SD);
        SetSensitiveDetector("logicalUrinaryBladder", SD);

        if (sex == "Female") {
            SetSensitiveDetector("logicalLeftOvary", SD);
            SetSensitiveDetector("logicalRightOvary", SD);
            SetSensitiveDetector("logicalUterus", SD);
            SetSensitiveDetector("logicalLeftBreast", SD);
            SetSensitiveDetector("logicalRightBreast", SD);
        }

        if (sex == "Male") {
            SetSensitiveDetector("logicalMaleGenitalia", SD);
            SetSensitiveDetector("logicalLeftTeste", SD);
            SetSensitiveDetector("logicalRightTeste", SD);
        }
    }
}
