Newer
Older
Markus Frank
committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//==========================================================================
// AIDA Detector description implementation
//--------------------------------------------------------------------------
// Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
// All rights reserved.
//
// For the licensing terms see $DD4hepINSTALL/LICENSE.
// For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
//
// Author : M.Frank
//
//==========================================================================
#ifndef DD4HEP_DDG4_GEANT4GDMLWRITEACTION_H
#define DD4HEP_DDG4_GEANT4GDMLWRITEACTION_H
// Framework include files
#include "DDG4/Geant4Action.h"
/// Namespace for the AIDA detector description toolkit
namespace dd4hep {
/// Namespace for the Geant4 based simulation part of the AIDA detector description toolkit
namespace sim {
/// Class to measure the energy of escaping tracks
/** Class to dump Geant4 geometry to GDML
*
* \author M.Frank
* \version 1.0
* \ingroup DD4HEP_SIMULATION
*/
class Geant4GDMLWriteAction : public Geant4Action {
public:
/// Property: collection names to be dumped
std::string m_output;
/// Poprerty: Flag to overwrite existing files
int m_overWrite;
public:
/// Standard constructor
Geant4GDMLWriteAction(Geant4Context* context, const std::string& nam);
/// Default destructor
virtual ~Geant4GDMLWriteAction();
/// Install command control messenger if wanted
virtual void installCommandMessenger() override;
/// Write geometry to GDML
virtual void writeGDML();
};
} // End namespace sim
} // End namespace dd4hep
#endif /* DD4HEP_DDG4_GEANT4GDMLWRITEACTION_H */
//====================================================================
// AIDA Detector description implementation
//--------------------------------------------------------------------
// Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
// All rights reserved.
//
// For the licensing terms see $DD4hepINSTALL/LICENSE.
// For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
//
// Author : M.Frank
//
//====================================================================
// Framework include files
#include "DD4hep/InstanceCount.h"
#include "DD4hep/Printout.h"
#include "DD4hep/Primitives.h"
#include "DDG4/Geant4DataDump.h"
#include "DDG4/Geant4UIMessenger.h"
// Geant 4 includes
#include "G4GDMLParser.hh"
using namespace std;
using namespace dd4hep;
using namespace dd4hep::sim;
/// Standard constructor
Geant4GDMLWriteAction::Geant4GDMLWriteAction(Geant4Context* ctxt, const string& nam)
: Geant4Action(ctxt, nam)
{
m_needsControl = true;
declareProperty("Output", m_output = "");
declareProperty("OverWrite", m_overWrite = 1);
InstanceCount::increment(this);
}
/// Default destructor
Geant4GDMLWriteAction::~Geant4GDMLWriteAction() {
InstanceCount::decrement(this);
}
/// Install command control messenger if wanted
void Geant4GDMLWriteAction::installCommandMessenger() {
Callback cb = Callback(this).make(&Geant4GDMLWriteAction::writeGDML);
m_control->addCall("write", "Write geometry to GDML file",cb);
}
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
/// Write geometry to GDML
void Geant4GDMLWriteAction::writeGDML() {
struct stat buff;
if ( m_output.empty() ) {
error("+++ No GDML file name given. Please set the output file (property Output)");
return;
}
if ( 0 == ::stat(m_output.c_str(), &buff) && !m_overWrite ) {
error("+++ GDML file elready exists. Please set another output file (property Output)");
return;
}
if ( 0 == ::stat(m_output.c_str(), &buff) && m_overWrite ) {
warning("+++ GDML file %s already exists. Overwriting existing file.", m_output.c_str());
::unlink(m_output.c_str());
}
G4GDMLParser parser;
info("+++ Writing GDML file: %s", m_output.c_str());
parser.Write(m_output, context()->world());
}
#include "DDG4/Factories.h"
DECLARE_GEANT4ACTION(Geant4GDMLWriteAction)