Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-25 07:40:11

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
0024 // ********************************************************************
0025 //
0026 /// \file VG01DetectorConstruction.cc
0027 /// \brief Implementation of the VG01DetectorConstruction class
0028 
0029 //  Authors:   J. Apostolakis & S. Wenzel (CERN)   2018-2021
0030 //
0031 //  Derived from FullCMS code by Mihaly Novak  (2017-18)
0032 
0033 #include "VG01DetectorConstruction.hh"
0034 
0035 #include "G4VecGeomConverter.h"
0036 #include "VG01DetectorMessenger.hh"
0037 
0038 #include "G4FieldManager.hh"
0039 #include "G4PropagatorInField.hh"
0040 #include "G4RunManager.hh"
0041 #include "G4SystemOfUnits.hh"
0042 #include "G4TransportationManager.hh"
0043 #include "G4UniformMagField.hh"
0044 #include "G4VPhysicalVolume.hh"
0045 #include "G4VisAttributes.hh"
0046 #include "G4ios.hh"
0047 #include "globals.hh"
0048 
0049 // NOTE: VecGeom versioning was introduced in 1.2.4, the minimum required to
0050 // build this file
0051 #include <VecGeom/base/Version.h>
0052 #include <VecGeom/management/GeoManager.h>
0053 #include <VecGeom/management/LoggerTypes.h>
0054 
0055 #if VECGEOM_VERSION >= 0x010208
0056 // NOTE: Logger was introduced in 1.2.8; earlier version log messages will go
0057 // to stdout via printf/cout
0058 #  include <VecGeom/management/Logger.h>
0059 #  define VECGEOM_HAS_LOGGER 1
0060 #endif
0061 
0062 #ifdef VECGEOM_HAS_LOGGER
0063 namespace
0064 {
0065 using vecgeom::LogLevel;
0066 using vecgeom::Provenance;
0067 
0068 // This logger adapts VecGeom messages, warnings, and errors to Geant4
0069 // io/exceptions.
0070 void G4VecGeomLogger(Provenance prov, LogLevel lev, std::string str)
0071 {
0072   std::ostringstream where;
0073   if (!prov.file.empty()) {
0074     // Only print out the last path component of the originating file
0075     auto pos = prov.file.rfind('/') + 1;
0076     where << prov.file.substr(pos);
0077   }
0078   if (prov.line != 0) {
0079     where << ':' << prov.line;
0080   }
0081 
0082   if (lev < LogLevel::warning) {
0083     G4cout << "VecGeom: " << where.str() << ": " << vecgeom::to_cstring(lev)
0084            << ": " << str << std::endl;
0085     return;
0086   }
0087 
0088   G4Exception(where.str().c_str(), "VecGeom",
0089               lev == LogLevel::warning ? JustWarning : FatalException,
0090               str.c_str());
0091 }
0092 }  // namespace
0093 #endif
0094 
0095 G4double VG01DetectorConstruction::fglobFieldValue = 0.0;
0096 
0097 VG01DetectorConstruction::VG01DetectorConstruction()
0098   : G4VUserDetectorConstruction(),
0099     fWorld(nullptr),
0100     fFieldMgr(nullptr),
0101     fUniformMagField(nullptr),
0102     fDetectorMessenger(nullptr)
0103 {
0104   fGDMLFileName = "TestNTST.gdml";
0105   fDetectorMessenger = new VG01DetectorMessenger(this);
0106 #ifdef VECGEOM_HAS_LOGGER
0107   // Have the global VecGeom logger redirect to G4cout rather than the
0108   // default std:clog
0109   auto orig_level = vecgeom::logger().level();
0110   vecgeom::Logger new_logger(&G4VecGeomLogger);
0111   new_logger.level(orig_level);
0112   vecgeom::logger() = std::move(new_logger);
0113 #endif
0114 }
0115 
0116 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0117 
0118 VG01DetectorConstruction::~VG01DetectorConstruction()
0119 {
0120   delete fDetectorMessenger;
0121   if (fUniformMagField) {
0122     delete fUniformMagField;
0123   }
0124 }
0125 
0126 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0127 
0128 G4VPhysicalVolume* VG01DetectorConstruction::Construct()
0129 {
0130   //  parser.SetOverlapCheck(true);
0131   G4TransportationManager* trMgr =
0132     G4TransportationManager::GetTransportationManager();
0133   assert(trMgr);
0134 
0135   fParser.Read(fGDMLFileName, false);
0136   fWorld = (G4VPhysicalVolume*)fParser.GetWorldVolume();
0137 
0138   // enabling 'check' mode in G4 Navigator
0139   G4Navigator* nav = trMgr->GetNavigatorForTracking();
0140   assert(nav);
0141   nav->CheckMode(true);
0142   // nav->SetVerboseLevel(1);
0143   std::cout << "Enabled Check mode in G4Navigator";
0144 
0145   // write back
0146   // fParser.Write("out.gdml", fWorld);
0147 
0148   fFieldMgr = trMgr->GetFieldManager();
0149   fWorld->GetLogicalVolume()->SetVisAttributes(G4VisAttributes::GetInvisible());
0150   if (fWorld == nullptr) {
0151     G4ExceptionDescription ed;
0152     ed << "World volume not set properly check your setup selection criteria"
0153        << "or GDML input!" << G4endl;
0154     G4Exception("VG01DetectorConstruction::Construct()",
0155                 "G4VecGeomNavExtExample_0001", FatalException, ed);
0156   }
0157   CreateMagFieldAndIntegrator();
0158 
0159   if (fUseVecGeom) {
0160     // This is converting the geometry to VecGeom and implicitely also setting
0161     // the navigator; We should pull this out really
0162     G4VecGeomConverter::Instance().SetVerbose(1);
0163     G4VecGeomConverter::Instance().ConvertG4Geometry(fWorld);
0164     G4cout << vecgeom::GeoManager::Instance().getMaxDepth() << "\n";
0165   }
0166   return fWorld;
0167 }
0168 
0169 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0170 
0171 void VG01DetectorConstruction::CreateMagFieldAndIntegrator()
0172 {
0173   delete fUniformMagField;
0174 
0175   if (std::abs(fglobFieldValue) > 0.0) {
0176     // Apply a global uniform magnetic field along the Z axis.
0177     // Note:  only when the magnetic field (pointer) is NOT zero,
0178     //        does the Geant4 transportion in field get activated.
0179     fUniformMagField = new G4UniformMagField(G4ThreeVector(0.0, 0.0,
0180                                              fglobFieldValue));
0181     fFieldMgr->SetDetectorField(fUniformMagField);
0182     fFieldMgr->CreateChordFinder(fUniformMagField);
0183     G4cout << G4endl
0184            << " *** SETTING MAGNETIC FIELD in Z direction : fieldValue = "
0185            << fglobFieldValue / tesla << " tesla *** " << G4endl << G4endl;
0186   }
0187   else {
0188     fFieldMgr->SetDetectorField(nullptr);
0189     G4cout << G4endl << " *** NO MAGNETIC FIELD SET  *** " << G4endl << G4endl;
0190   }
0191 }