GGS(GenericGEANT4Simulation)Software  2.99.0
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Macros
GGSSaveTGeoAction.cpp
1 /*
2  * GGSSaveTGeoAction.cpp
3  *
4  * Created on: Jun 3, 2020
5  * Author: Valerio Formato
6  */
7 
8 // GGS headers
14 #include "utils/GGSSmartLog.h"
15 
16 // VGM headers
17 #include "Geant4GM/volumes/Factory.h"
18 #include "RootGM/volumes/Factory.h"
19 #include "TGeoManager.h"
20 
21 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
22 
24 
25 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
26 
27 GGSSaveTGeoAction::GGSSaveTGeoAction() : GGSUserAction(), _outFile(nullptr), _outBase("") {}
28 
29 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
30 
31 void GGSSaveTGeoAction::BeginOfRunAction(const G4Run *run) { // -----------------
32  const std::string routineName("GGSSaveTGeoAction::BeginOfRunAction");
33 
34  // Open the run file
35  // -----------------
36  if (GGSRootFileService::GetInstance().GetOutputMode() == GGSRootFileService::OutputMode::MULTIFILE ||
37  G4Threading::G4GetThreadId() <= 0) {
38  _outFile = GGSRootFileService::GetInstance().GetFileForThisRun(_outBase, run);
39  if (!_outFile || (_outFile && _outFile->IsZombie())) {
40  GGSCOUT(ERROR) << "Cannot open ROOT file " << _outBase << "for run" << run->GetRunID() << GGSENDL;
41  throw std::runtime_error("");
42  }
43  }
44 }
45 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
46 namespace {
47 G4Mutex mutexForSaveTGeo = G4MUTEX_INITIALIZER;
48 }
49 void GGSSaveTGeoAction::EndOfRunAction(const G4Run *run) {
50  const std::string routineName("GGSSaveTGeoAction::EndOfRunAction");
51 
52  if (GGSRootFileService::GetInstance().GetOutputMode() == GGSRootFileService::OutputMode::MULTIFILE ||
53  G4Threading::G4GetThreadId() <= 0) {
54 
55  // Acquire a lock since VGM is probably not thread-safe
56  G4AutoLock lock(mutexForSaveTGeo);
57 
59  if (!geometry) {
60  GGSCOUT(ERROR) << "Can't get the geometry. Exit." << GGSENDL;
61  throw std::runtime_error("Invalid geometry");
62  }
63 
64  auto physVolume = geometry->GetVolume();
65 
66  auto g4Factory = std::make_unique<Geant4GM::Factory>();
67  if (GGSSmartLog::verboseLevel >= GGSSmartLog::DEBUG)
68  g4Factory->SetDebug(1);
69  g4Factory->Import(physVolume);
70 
71  // Export VGM geometry to Root
72  auto rtFactory = std::make_unique<RootGM::Factory>();
73  if (GGSSmartLog::verboseLevel >= GGSSmartLog::DEBUG)
74  rtFactory->SetDebug(1);
75  g4Factory->Export(rtFactory.get());
76  gGeoManager->CloseGeometry();
77 
78  _outFile->WriteObject(gGeoManager, "GGSGeometry");
79 
81  }
82 }
83 
84 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
Abstract class needed to load GGS geometry.
TFile * GetFileForThisRun(const std::filesystem::path &baseName, const G4Run *run)
Opens a file for a given run and returns a pointer to it.
static GGSGeoPluginManager & GetInstance()
Get the singleton instance.
#define GGSENDL
Definition: GGSSmartLog.h:131
void EndOfRunAction(const G4Run *run)
Actions executed at end of run.
void CloseFileForThisRun(const std::filesystem::path &baseName, const G4Run *run)
Closes the ROOT output file.
static GGSRootFileService & GetInstance()
Get reference to GGSRootFileService unique instance.
Mother class for user actions in GGS.
Definition: GGSUserAction.h:27
GGSSaveTGeoAction()
Constructor.
GGSVGeometryConstruction * GetGeoConstruction()
Returns the geometry construction object.
#define RegisterUA(uaClassName)
Macro for registration of user actions classes.
void BeginOfRunAction(const G4Run *run)
Actions executed at beginning of run.
virtual G4VPhysicalVolume * GetVolume()=0
Get the detector VPhysicalVolume - virtual method.