Commit 1af19424 authored by Sam Wood's avatar Sam Wood Committed by Simon Spannagel
Browse files

Add G4 class to load B-fields from geometry manager regardless of type

parent 9ca63005
Loading
Loading
Loading
Loading
+17 −12
Original line number Diff line number Diff line
@@ -31,7 +31,6 @@

#include "G4FieldManager.hh"
#include "G4TransportationManager.hh"
#include "G4UniformMagField.hh"

#include "core/config/exceptions.h"
#include "core/geometry/GeometryManager.hpp"
@@ -469,17 +468,10 @@ void DepositionGeant4Module::finalizeThread() {

void DepositionGeant4Module::construct_sensitive_detectors_and_fields() {
    if(geo_manager_->hasMagneticField()) {
        MagneticFieldType magnetic_field_type_ = geo_manager_->getMagneticFieldType();

        if(magnetic_field_type_ == MagneticFieldType::CONSTANT) {
            ROOT::Math::XYZVector b_field = geo_manager_->getMagneticField(ROOT::Math::XYZPoint(0., 0., 0.));
            G4MagneticField* magField = new G4UniformMagField(G4ThreeVector(b_field.x(), b_field.y(), b_field.z()));
        G4MagneticField* magField = new MagFieldG4(geo_manager_);
        G4FieldManager* globalFieldMgr = G4TransportationManager::GetTransportationManager()->GetFieldManager();
        globalFieldMgr->SetDetectorField(magField);
        globalFieldMgr->CreateChordFinder(magField);
        } else {
            throw ModuleError("Magnetic field enabled, but not constant. This can't be handled by this module yet.");
        }
    }

    // Loop through all detectors and set the sensitive detector action that handles the particle passage
@@ -612,3 +604,16 @@ void DepositionGeant4Module::record_module_statistics() {
        total_charges_ += sensor->getTotalDepositedCharge();
    }
}

MagFieldG4::MagFieldG4(GeometryManager* gm) { gm_ = gm; }

MagFieldG4::~MagFieldG4(){};

void MagFieldG4::GetFieldValue(const double Point[4], double* Bfield) const {
    G4cout << "Getting mag field from gm" << G4endl;
    ROOT::Math::XYZVector bfield_vector = gm_->getMagneticField(ROOT::Math::XYZPoint(Point[0], Point[1], Point[2]));
    G4cout << bfield_vector << G4endl;
    Bfield[0] = bfield_vector.x();
    Bfield[1] = bfield_vector.y();
    Bfield[2] = bfield_vector.z();
}
+12 −0
Original line number Diff line number Diff line
@@ -18,6 +18,8 @@

#include <G4UserLimits.hh>

#include "G4MagneticField.hh"

#include "core/config/Configuration.hpp"
#include "core/geometry/GeometryManager.hpp"
#include "core/messenger/Messenger.hpp"
@@ -133,6 +135,16 @@ namespace allpix {
        // Mutex used for the construction of histograms
        std::mutex histogram_mutex_;
    };

    class MagFieldG4 : public G4MagneticField {
    protected:
        GeometryManager* gm_;

    public:
        MagFieldG4(GeometryManager* gm);
        ~MagFieldG4();
        virtual void GetFieldValue(const double Point[4], double* Bfield) const;
    };
} // namespace allpix

#endif /* ALLPIX_SIMPLE_DEPOSITION_MODULE_H */