diff options
-rw-r--r-- | chroma/generator/g4gen.py | 17 | ||||
-rw-r--r-- | setup.py | 5 | ||||
-rw-r--r-- | src/mute.cc | 41 |
3 files changed, 59 insertions, 4 deletions
diff --git a/chroma/generator/g4gen.py b/chroma/generator/g4gen.py index 7f55e56..0ed5433 100644 --- a/chroma/generator/g4gen.py +++ b/chroma/generator/g4gen.py @@ -1,12 +1,20 @@ +from chroma.generator.mute import * + +import pyublas +import numpy as np +from chroma.event import Photons, Vertex + +g4mute() from Geant4 import * +g4unmute() import g4py.ezgeom import g4py.NISTmaterials import g4py.ParticleGun -import pyublas -import numpy as np -from chroma.event import Photons, Vertex from chroma.generator import _g4chroma +#cppmute() +#cppunmute() + class G4Generator(object): def __init__(self, material, seed=None): """Create generator to produce photons inside the specified material. @@ -40,8 +48,9 @@ class G4Generator(object): self.tracking_action = _g4chroma.PhotonTrackingAction() gRunManager.SetUserAction(self.tracking_action) + g4mute() gRunManager.Initialize() - + g4unmute() # preinitialize the process by running a simple event self.generate_photons([Vertex('e-', (0,0,0), (1,0,0), 0, 1.0)]) @@ -21,6 +21,11 @@ setup( extra_link_args=geant4_libs, libraries=['boost_python'], ), + Extension('chroma.generator.mute', + ['src/mute.cc'], + extra_compile_args=geant4_cflags, + extra_link_args=geant4_libs, + libraries=['boost_python']), ], install_requires = ['uncertainties','pyzmq-static','h5py','spnav', 'pycuda', diff --git a/src/mute.cc b/src/mute.cc new file mode 100644 index 0000000..e13814f --- /dev/null +++ b/src/mute.cc @@ -0,0 +1,41 @@ +#include <geant4/G4ios.hh> + +class discard_streambuf : public std::streambuf { +public: + discard_streambuf() { }; + + virtual int_type overflow(int_type c) { + // Do nothing with this character + return c; + }; +}; + +discard_streambuf discard; +std::streambuf *g4cout_orig = G4cout.rdbuf(); +std::streambuf *g4cerr_orig = G4cerr.rdbuf(); + +void mute_g4mute() { + G4cout.rdbuf(&discard); + G4cerr.rdbuf(&discard); +} + +void mute_g4unmute() { + G4cout.rdbuf(g4cout_orig); + G4cerr.rdbuf(g4cerr_orig); +} + + +#include <boost/python.hpp> + +using namespace boost::python; + +void export_mute() +{ + def("g4mute", mute_g4mute, default_call_policies(), "Silence all GEANT4 output"); + def("g4unmute", mute_g4unmute, default_call_policies(), "Re-enable GEANT4 output after calling ``g4mute()``."); +} + +BOOST_PYTHON_MODULE(mute) +{ + export_mute(); +} |