Belle II Software development
SetupGenfitExtrapolationModule.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#include <tracking/modules/genfitUtilities/SetupGenfitExtrapolationModule.h>
10#include <tracking/modules/genfitUtilities/Geant4MaterialInterface.h>
11
12#include <geometry/GeometryManager.h>
13
14#include <tracking/gfbfield/GFGeant4Field.h>
15#include <genfit/FieldManager.h>
16#include <genfit/MaterialEffects.h>
17#include <genfit/TGeoMaterialInterface.h>
18#include <genfit/IO.h>
19
20#include <boost/iostreams/stream_buffer.hpp>
21#include <boost/iostreams/concepts.hpp>
22
23#include <TGeoManager.h>
24
25using namespace Belle2;
26
27REG_MODULE(SetupGenfitExtrapolation)
28
29namespace {
30
33 template<size_t T_level>
34 class genfitSink : public boost::iostreams::sink {
35 public:
37 std::streamsize write(const char* s, std::streamsize n)
38 {
39 B2DEBUG(T_level, s);
40 return n;
41 }
42 };
43
45 genfitSink<200> debugSink;
47 boost::iostreams::stream_buffer<genfitSink<200> > debugStreamBuf(debugSink);
49 genfitSink<100> errorSink;
51 boost::iostreams::stream_buffer<genfitSink<100> > errorStreamBuf(errorSink);
53 genfitSink<150> printSink;
55 boost::iostreams::stream_buffer<genfitSink<150> > printStreamBuf(printSink);
56
58 void setupGenfitStreams()
59 {
60 genfit::debugOut.rdbuf(&debugStreamBuf);
61 genfit::errorOut.rdbuf(&errorStreamBuf);
62 genfit::printOut.rdbuf(&printStreamBuf);
63 }
64}
65
67 Module()
68{
69
70 setDescription("Sets up material handling for genfit extrapolation. Also setups up I/O streams for"
71 " genfit in order to integrate it into basf2 logging system.");
73
74 addParam("ignoreIfPresent", m_ignoreIfPresent,
75 "If true this module will silently ignore if the geometry is already "
76 "present and do nothing in that case. If false a B2FATAL will be "
77 "if the geometry was already created before", m_ignoreIfPresent);
78
79 //input
80 addParam("whichGeometry", m_geometry,
81 "Which geometry should be used, either 'TGeo' or 'Geant4'", m_geometry);
82
83 // Energy loss, multiple scattering configuration.
84 addParam("energyLossBetheBloch", m_energyLossBetheBloch,
85 "activate the material effect: EnergyLossBetheBloch", m_energyLossBetheBloch);
86 addParam("noiseBetheBloch", m_noiseBetheBloch,
87 "activate the material effect: NoiseBetheBloch", m_noiseBetheBloch);
88 addParam("noiseCoulomb", m_noiseCoulomb,
89 "activate the material effect: NoiseCoulomb", m_noiseCoulomb);
90 addParam("energyLossBrems", m_energyLossBrems,
91 "activate the material effect: EnergyLossBrems", m_energyLossBrems);
92 addParam("noiseBrems", m_noiseBrems,
93 "activate the material effect: NoiseBrems", m_noiseBrems);
94 addParam("noEffects", m_noEffects,
95 "switch off all material effects in Genfit. This overwrites all "
96 "individual material effects switches", m_noEffects);
97 addParam("multipleScatteringModel", m_mscModel,
98 "Multiple scattering model", m_mscModel);
99 addParam("useBFieldCache", m_useBFieldCache, "activate the usage of the magnetic field cache",
101}
102
104{
105 if (genfit::FieldManager::getInstance()->isInitialized() or genfit::MaterialEffects::getInstance()->isInitialized()) {
106 if (m_ignoreIfPresent) {
107 B2DEBUG(29, "Magnetic field or material handling already initialized. Not touching settings.");
108 return;
109 } else {
110 B2FATAL("Magnetic field or material handling already initialized. Not touching settings.");
111 }
112 }
113
114 setupGenfitStreams();
115
116 genfit::FieldManager::getInstance()->init(new GFGeant4Field());
117 // The usage of the magnetic field cache in Genfit leads to irreproducible results.
118 // A parameter is added here to switch off the usage of the cache until the problem
119 // is fixed upstream.
120 genfit::FieldManager::getInstance()->useCache(m_useBFieldCache);
121
122 if (!geometry::GeometryManager::getInstance().getTopVolume()) {
123 B2FATAL("No geometry set up so far. Load the geometry module.");
124 }
125
126 if (m_geometry == "TGeo") {
127 if (!gGeoManager) {
128 B2INFO("Building TGeo representation.");
130 geoManager.createTGeoRepresentation();
131 }
132 genfit::MaterialEffects::getInstance()->init(new genfit::TGeoMaterialInterface());
133 } else if (m_geometry == "Geant4") {
134 genfit::MaterialEffects::getInstance()->init(new Geant4MaterialInterface());
135 } else {
136 B2FATAL("Invalid choice of geometry interface. Please use 'TGeo' or 'Geant4'.");
137 }
138
139 // activate / deactivate material effects in genfit
140 if (m_noEffects) {
141 genfit::MaterialEffects::getInstance()->setNoEffects(true);
142 } else {
143 genfit::MaterialEffects::getInstance()->setEnergyLossBetheBloch(m_energyLossBetheBloch);
144 genfit::MaterialEffects::getInstance()->setNoiseBetheBloch(m_noiseBetheBloch);
145 genfit::MaterialEffects::getInstance()->setNoiseCoulomb(m_noiseCoulomb);
146 genfit::MaterialEffects::getInstance()->setEnergyLossBrems(m_energyLossBrems);
147 genfit::MaterialEffects::getInstance()->setNoiseBrems(m_noiseBrems);
148 genfit::MaterialEffects::getInstance()->setMscModel(m_mscModel);
149 }
150}
AbsMaterialInterface implementation for use with Geant4's navigator.
void setDescription(const std::string &description)
Sets the description of the module.
Definition Module.cc:214
void setPropertyFlags(unsigned int propertyFlags)
Sets the flags for the module properties.
Definition Module.cc:208
Module()
Constructor.
Definition Module.cc:30
@ c_ParallelProcessingCertified
This module can be run in parallel processing mode safely (All I/O must be done through the data stor...
Definition Module.h:80
std::string m_mscModel
Multiple scattering model.
std::string m_geometry
choice of geometry representation: 'TGeo' or 'Geant4'.
bool m_noEffects
Switch on/off ALL material effects in Genfit. "true" overwrites "true" flags for the individual effec...
void initialize() override
Initialize the Module.
bool m_noiseBetheBloch
Determines if calculation of energy loss variance is on/off in Genfit.
bool m_noiseBrems
Determines if calculation of bremsstrahlung energy loss variance is on/off in Genfit.
bool m_useBFieldCache
Determines if the magnetic field cache is on/off in Genfit.
bool m_energyLossBrems
Determines if calculation of bremsstrahlung energy loss is on/off in Genfit.
bool m_noiseCoulomb
Determines if calculation of multiple scattering covariance matrix on/off in Genfit.
bool m_energyLossBetheBloch
Determines if calculation of energy loss is on/off in Genfit.
bool m_ignoreIfPresent
Whether or not this module will raise an error if the geometry is already present.
Class to manage the creation and conversion of the geometry.
static GeometryManager & getInstance()
Return a reference to the instance.
void createTGeoRepresentation()
Create a TGeo representation of the native geometry description.
Interface of the Belle II B-field with GenFit.
void addParam(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
Definition Module.h:559
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition Module.h:649
Abstract base class for different kinds of events.