From 26e5398807b6edd6dd71046ef745f5c6acfab1ed Mon Sep 17 00:00:00 2001 From: Stan Seibert Date: Fri, 16 Sep 2011 20:09:49 -0400 Subject: Silence annoying GEANT4 warnings and banners during operation. --- chroma/generator/g4gen.py | 17 +++++++++++++---- setup.py | 5 +++++ src/mute.cc | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 src/mute.cc 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)]) diff --git a/setup.py b/setup.py index 369979a..139ba54 100644 --- a/setup.py +++ b/setup.py @@ -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 + +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 + +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(); +} -- cgit