GGS(GenericGEANT4Simulation)Software  2.7.0
 All Data Structures Namespaces Files Functions Variables Typedefs 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 
28 
29 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
30 
31 void GGSSaveTGeoAction::BeginOfRunAction(const G4Run *run) { // -----------------
32  static const std::string routineName("GGSSaveTGeoAction::BeginOfRunAction");
33 
34  // Open the run file
35  // -----------------
36  _outFile = GGSRootFileService::GetInstance().GetFileForThisRun(_outBase, run);
37  if (!_outFile || (_outFile && _outFile->IsZombie())) {
38  COUT(ERROR) << "Cannot open ROOT file " << _outBase << "for run" << run->GetRunID() << ENDL;
39  throw std::runtime_error("");
40  }
41 }
42 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
43 
44 void GGSSaveTGeoAction::EndOfRunAction(const G4Run *) { // -----------------
45  static const std::string routineName("GGSSaveTGeoAction::EndOfRunAction");
46 
48  if (!geometry) {
49  COUT(ERROR) << "Can't get the geometry. Exit." << ENDL;
50  throw std::runtime_error("Invalid geometry");
51  }
52 
53  auto physVolume = geometry->GetVolume();
54 
55  auto g4Factory = std::make_unique<Geant4GM::Factory>();
56  if (GGSSmartLog::verboseLevel >= GGSSmartLog::DEBUG)
57  g4Factory->SetDebug(1);
58  g4Factory->Import(physVolume);
59 
60  // Export VGM geometry to Root
61  auto rtFactory = std::make_unique<RootGM::Factory>();
62  if (GGSSmartLog::verboseLevel >= GGSSmartLog::DEBUG)
63  rtFactory->SetDebug(1);
64  g4Factory->Export(rtFactory.get());
65  gGeoManager->CloseGeometry();
66 
67  _outFile->cd();
68  gGeoManager->Write("GGSGeometry");
69 
71 }
72 
73 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
TFile * GetFileForThisRun(const path &baseName, const G4Run *run)
Opens a file for a given run and returns a pointer to it.
Abstract class needed to load GGS geometry.
static GGSGeoPluginManager & GetInstance()
Get the singleton instance.
void CloseFileForThisRun(const path &baseName)
Closes the ROOT output file.
#define ENDL
Definition: GGSSmartLog.h:105
#define COUT(level)
Smart log macro. It writes on stdout only if the specified verbosity level is lesser than the maximum...
Definition: GGSSmartLog.h:76
void EndOfRunAction(const G4Run *run)
Actions executed at end of run.
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.