LArSoft  v06_85_00
Liquid Argon Software toolkit - http://larsoft.org/
DetectorConstruction.cxx
Go to the documentation of this file.
1 
10 #include "cetlib_except/exception.h"
11 
14 
15 #include "Geant4/G4VPhysicalVolume.hh"
16 #include "Geant4/G4GDMLParser.hh"
17 #include "Geant4/G4LogicalVolumeStore.hh"
18 #include "Geant4/G4Material.hh"
19 #include "Geant4/G4UniformMagField.hh"
20 #include "Geant4/G4FieldManager.hh"
21 
22 namespace g4b{
23 
24  // Allocate static variables.
25  G4VPhysicalVolume* DetectorConstruction::fWorld = nullptr;
26  G4FieldManager* DetectorConstruction::fFieldMgr = nullptr;
27 
28  //---------------------------------------------------
29  // Constructor
30  DetectorConstruction::DetectorConstruction(std::string const& gdmlFile,
31  bool const& overlapCheck,
32  bool const& validateSchema)
33  {
34  if ( gdmlFile.empty() ) {
35  throw cet::exception("DetectorConstruction") << "Supplied GDML filename is empty\n"
36  << __FILE__ << ":" << __LINE__ << "\n";
37  }
38  // Get the path to the GDML file from the Geometry interface.
39  const G4String GDMLfile = static_cast<const G4String>( gdmlFile );
40 
41  // Use Geant4's GDML parser to convert the geometry to Geant4 format.
42  G4GDMLParser parser;
43  parser.SetOverlapCheck(overlapCheck);
44  parser.Read(GDMLfile,validateSchema);
45 
46  // Fetch the world physical volume from the parser. This contains
47  // the entire detector, not just the outline of the experimental
48  // hall.
49  fWorld = parser.GetWorldVolume();
50 
51  }
52 
53  //---------------------------------------------------
54  // Destructor.
56  {
57  }
58 
59  //---------------------------------------------------
60  G4VPhysicalVolume* DetectorConstruction::Construct()
61  {
62  // Setup the magnetic field situation
64 
65  // loop over the possible fields
66  for(auto fd : bField->Fields()){
67  switch (fd.fMode) {
68  case mag::kNoBFieldMode:
69  /* NOP */
70  break;
72  // Attach this to the magnetized volume only, so get that volume
73  G4LogicalVolume *bvol = G4LogicalVolumeStore::GetInstance()->GetVolume(fd.fVolume);
74 
75  // Define the basic field, using p we should get the uniform field
76  G4UniformMagField* magField = new G4UniformMagField( fd.fField * CLHEP::tesla );
77  fFieldMgr = new G4FieldManager();
78  fFieldMgr->SetDetectorField(magField);
79  fFieldMgr->CreateChordFinder(magField);
80 
81  LOG_INFO("DetectorConstruction")
82  << "Setting uniform magnetic field to be "
83  << magField->GetConstantFieldValue().x() << " "
84  << magField->GetConstantFieldValue().y() << " "
85  << magField->GetConstantFieldValue().z() << " "
86  << " in " << bvol->GetName();
87 
88  // Reset the chord finding accuracy
89  // fFieldMgr->GetChordFinder()->SetDeltaChord(1.0 * cm);
90 
91  // the boolean tells the field manager to use local volume
92  bvol->SetFieldManager(fFieldMgr, true);
93 
94  break;
95  } // case mag::kConstantBFieldMode
96  default: // Complain if the user asks for something not handled
97  LOG_ERROR("DetectorConstruction")
98  << "Unknown or illegal Magneticfield "
99  << "mode specified: "
100  << fd.fMode
101  << ". Note that AutomaticBFieldMode is reserved.";
102  break;
103  } // end switch cases
104 
105  } // end loop over fields
106 
107  return fWorld;
108  }
109 
110 }// namespace
Build Geant4 geometry from GDML.
#define LOG_INFO(category)
std::vector< MagneticFieldDescription > const & Fields() const
Definition: MagneticField.h:54
Describe the magnetic field structure of a detector.
#define LOG_ERROR(category)
DetectorConstruction(std::string const &gdmlFile, bool const &overlapCheck=false, bool const &validateSchema=true)
Standard constructor and destructor.
CommandLineParser * parser(0)
basic interface to Geant4 for ART-based software
static G4FieldManager * fFieldMgr
pointer to the field manager
static G4VPhysicalVolume * fWorld
pointer to the world volume
G4VPhysicalVolume * Construct()
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33