Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-10-26 08:01:21

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 //
0027 /// \file VG01DetectorConstruction.cc
0028 /// \brief Implementation of the VG01DetectorConstruction class
0029 
0030 //  Authors:   J. Apostolakis & S. Wenzel (CERN)   2018-2021
0031 //
0032 //  Derived from FullCMS code by Mihaly Novak  (2017-18)
0033 
0034 #include "VG01DetectorConstruction.hh"
0035 
0036 #include "G4VecGeomConverter.h"
0037 #include "VG01DetectorMessenger.hh"
0038 
0039 #include "G4FieldManager.hh"
0040 #include "G4PropagatorInField.hh"
0041 #include "G4RunManager.hh"
0042 #include "G4SystemOfUnits.hh"
0043 #include "G4TransportationManager.hh"
0044 #include "G4UniformMagField.hh"
0045 #include "G4VPhysicalVolume.hh"
0046 #include "G4VisAttributes.hh"
0047 #include "G4ios.hh"
0048 #include "globals.hh"
0049 
0050 // NOTE: VecGeom versioning was introduced in 1.2.4, the minimum required to
0051 // build this file
0052 #include <VecGeom/base/Version.h>
0053 #include <VecGeom/management/GeoManager.h>
0054 #include <VecGeom/management/LoggerTypes.h>
0055 
0056 #if VECGEOM_VERSION >= 0x010208
0057 // NOTE: Logger was introduced in 1.2.8; earlier version log messages will go
0058 // to stdout via printf/cout
0059 #  include <VecGeom/management/Logger.h>
0060 #  define VECGEOM_HAS_LOGGER 1
0061 #endif
0062 
0063 #ifdef VECGEOM_HAS_LOGGER
0064 namespace
0065 {
0066 using vecgeom::LogLevel;
0067 using vecgeom::Provenance;
0068 
0069 // This logger adapts VecGeom messages, warnings, and errors to Geant4 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) << ": " << str << std::endl;
0084     return;
0085   }
0086 
0087   G4Exception(where.str().c_str(), "VecGeom",
0088               lev == LogLevel::warning ? JustWarning : FatalException, str.c_str());
0089 }
0090 }  // namespace
0091 #endif
0092 
0093 G4double VG01DetectorConstruction::fglobFieldValue = 0.0;
0094 
0095 VG01DetectorConstruction::VG01DetectorConstruction()
0096   : G4VUserDetectorConstruction(),
0097     fWorld(nullptr),
0098     fFieldMgr(nullptr),
0099     fUniformMagField(nullptr),
0100     fDetectorMessenger(nullptr)
0101 {
0102   fGDMLFileName = "TestNTST.gdml";
0103   fDetectorMessenger = new VG01DetectorMessenger(this);
0104 #ifdef VECGEOM_HAS_LOGGER
0105   // Have the global VecGeom logger redirect to G4cout rather than the default std:clog
0106   auto orig_level = vecgeom::logger().level();
0107   vecgeom::Logger new_logger(&G4VecGeomLogger);
0108   new_logger.level(orig_level);
0109   vecgeom::logger() = std::move(new_logger);
0110 #endif
0111 }
0112 
0113 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0114 
0115 VG01DetectorConstruction::~VG01DetectorConstruction()
0116 {
0117   delete fDetectorMessenger;
0118   if (fUniformMagField) {
0119     delete fUniformMagField;
0120   }
0121 }
0122 
0123 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0124 
0125 G4VPhysicalVolume* VG01DetectorConstruction::Construct()
0126 {
0127   //  parser.SetOverlapCheck(true);
0128   G4TransportationManager* trMgr = G4TransportationManager::GetTransportationManager();
0129   assert(trMgr);
0130 
0131   fParser.Read(fGDMLFileName, false);
0132   fWorld = (G4VPhysicalVolume*)fParser.GetWorldVolume();
0133 
0134   // enabling 'check' mode in G4 Navigator
0135   G4Navigator* nav = trMgr->GetNavigatorForTracking();
0136   assert(nav);
0137   nav->CheckMode(true);
0138   // nav->SetVerboseLevel(1);
0139   std::cout << "Enabled Check mode in G4Navigator";
0140 
0141   // write back
0142   // fParser.Write("out.gdml", fWorld);
0143 
0144   fFieldMgr = trMgr->GetFieldManager();
0145   fWorld->GetLogicalVolume()->SetVisAttributes(G4VisAttributes::GetInvisible());
0146   if (fWorld == nullptr) {
0147     G4ExceptionDescription ed;
0148     ed << "World volume not set properly check your setup selection criteria"
0149        << "or GDML input!" << G4endl;
0150     G4Exception("VG01DetectorConstruction::Construct()", "G4VecGeomNavExtExample_0001",
0151                 FatalException, ed);
0152   }
0153   CreateMagFieldAndIntegrator();
0154 
0155   if (fUseVecGeom) {
0156     // This is converting the geometry to VecGeom and implicitely also setting
0157     // the navigator; We should pull this out really
0158     G4VecGeomConverter::Instance().SetVerbose(1);
0159     G4VecGeomConverter::Instance().ConvertG4Geometry(fWorld);
0160     G4cout << vecgeom::GeoManager::Instance().getMaxDepth() << "\n";
0161   }
0162   return fWorld;
0163 }
0164 
0165 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0166 
0167 void VG01DetectorConstruction::CreateMagFieldAndIntegrator()
0168 {
0169   delete fUniformMagField;
0170 
0171   if (std::abs(fglobFieldValue) > 0.0) {
0172     // Apply a global uniform magnetic field along the Z axis.
0173     // Note:  only when the magnetic field (pointer) is NOT zero,
0174     //        does the Geant4 transportion in field get activated.
0175     fUniformMagField = new G4UniformMagField(G4ThreeVector(0.0, 0.0, fglobFieldValue));
0176     fFieldMgr->SetDetectorField(fUniformMagField);
0177     fFieldMgr->CreateChordFinder(fUniformMagField);
0178     G4cout << G4endl << " *** SETTING MAGNETIC FIELD in Z direction : fieldValue = "
0179            << fglobFieldValue / tesla << " tesla *** " << G4endl << G4endl;
0180   }
0181   else {
0182     fFieldMgr->SetDetectorField(nullptr);
0183     G4cout << G4endl << " *** NO MAGNETIC FIELD SET  *** " << G4endl << G4endl;
0184   }
0185 }