Belle II Software  release-05-02-19
SetupGenfitExtrapolationModule.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2010 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Tobias Schlüter, Tadeas Bilka *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 //
12 // Common setup for everything that uses genfit's extrapolation code.
13 //
14 
15 #include <tracking/modules/genfitUtilities/SetupGenfitExtrapolationModule.h>
16 #include <tracking/modules/genfitUtilities/Geant4MaterialInterface.h>
17 
18 #include <geometry/GeometryManager.h>
19 
20 #include <tracking/gfbfield/GFGeant4Field.h>
21 #include <genfit/FieldManager.h>
22 #include <genfit/MaterialEffects.h>
23 #include <genfit/TGeoMaterialInterface.h>
24 #include <genfit/IO.h>
25 
26 #include <boost/iostreams/stream_buffer.hpp>
27 #include <boost/iostreams/concepts.hpp>
28 
29 #include <TGeoManager.h>
30 
31 using namespace Belle2;
32 
33 REG_MODULE(SetupGenfitExtrapolation)
34 
35 namespace {
38  template<size_t T_level>
39  class genfitSink : public boost::iostreams::sink {
40  public:
42  std::streamsize write(const char* s, std::streamsize n)
43  {
44  B2DEBUG(T_level, s);
45  return n;
46  }
47  };
48 
50  genfitSink<200> debugSink;
52  boost::iostreams::stream_buffer<genfitSink<200> > debugStreamBuf(debugSink);
54  genfitSink<100> errorSink;
56  boost::iostreams::stream_buffer<genfitSink<100> > errorStreamBuf(errorSink);
58  genfitSink<150> printSink;
60  boost::iostreams::stream_buffer<genfitSink<150> > printStreamBuf(printSink);
61 
63  void setupGenfitStreams()
64  {
65  genfit::debugOut.rdbuf(&debugStreamBuf);
66  genfit::errorOut.rdbuf(&errorStreamBuf);
67  genfit::printOut.rdbuf(&printStreamBuf);
68  }
69 }
70 
72  Module(), m_vxdAlignment()
73 {
74 
75  setDescription("Sets up material handling for genfit extrapolation. Also setups up I/O streams for"
76  " genfit in order to integrate it into basf2 logging system. Also sets up update of VXDAlignment from DB (temporary).");
78 
79  addParam("ignoreIfPresent", m_ignoreIfPresent,
80  "If true this module will silently ignore if the geometry is already "
81  "present and do nothing in that case. If false a B2FATAL will be "
82  "if the geometry was already created before", m_ignoreIfPresent);
83 
84  //input
85  addParam("whichGeometry", m_geometry,
86  "Which geometry should be used, either 'TGeo' or 'Geant4'", m_geometry);
87 
88  // Energy loss, multiple scattering configuration.
89  addParam("energyLossBetheBloch", m_energyLossBetheBloch,
90  "activate the material effect: EnergyLossBetheBloch", m_energyLossBetheBloch);
91  addParam("noiseBetheBloch", m_noiseBetheBloch,
92  "activate the material effect: NoiseBetheBloch", m_noiseBetheBloch);
93  addParam("noiseCoulomb", m_noiseCoulomb,
94  "activate the material effect: NoiseCoulomb", m_noiseCoulomb);
95  addParam("energyLossBrems", m_energyLossBrems,
96  "activate the material effect: EnergyLossBrems", m_energyLossBrems);
97  addParam("noiseBrems", m_noiseBrems,
98  "activate the material effect: NoiseBrems", m_noiseBrems);
99  addParam("noEffects", m_noEffects,
100  "switch off all material effects in Genfit. This overwrites all "
101  "individual material effects switches", m_noEffects);
102  addParam("MSCModel", m_mscModel,
103  "Multiple scattering model", m_mscModel);
104  addParam("useVXDAlignment", m_useVXDAlignment,
105  "Use VXD alignment from database?", m_useVXDAlignment);
106 }
107 
109 {
110  if (genfit::FieldManager::getInstance()->isInitialized() or genfit::MaterialEffects::getInstance()->isInitialized()) {
111  if (m_ignoreIfPresent) {
112  B2DEBUG(50, "Magnetic field or material handling already initialized. Not touching settings.");
113  return;
114  } else {
115  B2FATAL("Magnetic field or material handling already initialized. Not touching settings.");
116  }
117  }
118 
119  setupGenfitStreams();
120 
123 
124  if (!geometry::GeometryManager::getInstance().getTopVolume()) {
125  B2FATAL("No geometry set up so far. Load the geometry module.");
126  }
127 
128  if (m_geometry == "TGeo") {
129  if (!gGeoManager) {
130  B2INFO("Building TGeo representation.");
132  geoManager.createTGeoRepresentation();
133  }
134  genfit::MaterialEffects::getInstance()->init(new genfit::TGeoMaterialInterface());
135  } else if (m_geometry == "Geant4") {
136  genfit::MaterialEffects::getInstance()->init(new Geant4MaterialInterface());
137  } else {
138  B2FATAL("Invalid choice of geometry interface. Please use 'TGeo' or 'Geant4'.");
139  }
140 
141  // activate / deactivate material effects in genfit
142  if (m_noEffects) {
143  genfit::MaterialEffects::getInstance()->setNoEffects(true);
144  } else {
145  genfit::MaterialEffects::getInstance()->setEnergyLossBetheBloch(m_energyLossBetheBloch);
146  genfit::MaterialEffects::getInstance()->setNoiseBetheBloch(m_noiseBetheBloch);
147  genfit::MaterialEffects::getInstance()->setNoiseCoulomb(m_noiseCoulomb);
148  genfit::MaterialEffects::getInstance()->setEnergyLossBrems(m_energyLossBrems);
149  genfit::MaterialEffects::getInstance()->setNoiseBrems(m_noiseBrems);
150  genfit::MaterialEffects::getInstance()->setMscModel(m_mscModel);
151  }
152 }
genfit::MaterialEffects::init
void init(AbsMaterialInterface *matIfc)
set the material interface here. Material interface classes must be derived from AbsMaterialInterface...
Definition: MaterialEffects.cc:87
genfit::errorOut
std::ostream errorOut
Default stream for error output.
genfit::TGeoMaterialInterface
AbsMaterialInterface implementation for use with ROOT's TGeoManager.
Definition: TGeoMaterialInterface.h:35
Belle2::SetupGenfitExtrapolationModule::m_energyLossBrems
bool m_energyLossBrems
Determines if calculation of bremsstrahlung energy loss is on/off in Genfit.
Definition: SetupGenfitExtrapolationModule.h:63
Belle2::Module::setDescription
void setDescription(const std::string &description)
Sets the description of the module.
Definition: Module.cc:216
Belle2::SetupGenfitExtrapolationModule::SetupGenfitExtrapolationModule
SetupGenfitExtrapolationModule()
Constructor.
Definition: SetupGenfitExtrapolationModule.cc:71
Belle2::SetupGenfitExtrapolationModule::m_noEffects
bool m_noEffects
switch on/off ALL material effects in Genfit. "true" overwrites "true" flags for the individual effec...
Definition: SetupGenfitExtrapolationModule.h:55
REG_MODULE
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:652
Belle2::Module::c_ParallelProcessingCertified
@ 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:82
Belle2::SetupGenfitExtrapolationModule::m_energyLossBetheBloch
bool m_energyLossBetheBloch
Determines if calculation of energy loss is on/off in Genfit.
Definition: SetupGenfitExtrapolationModule.h:57
Belle2::SetupGenfitExtrapolationModule::m_mscModel
std::string m_mscModel
Multiple scattering model.
Definition: SetupGenfitExtrapolationModule.h:67
genfit::printOut
std::ostream printOut
Default stream for output of Print calls.
Belle2::SetupGenfitExtrapolationModule::m_noiseBetheBloch
bool m_noiseBetheBloch
Determines if calculation of energy loss variance is on/off in Genfit.
Definition: SetupGenfitExtrapolationModule.h:59
Belle2::geometry::GeometryManager::getInstance
static GeometryManager & getInstance()
Return a reference to the instance.
Definition: GeometryManager.cc:98
Belle2::geometry::GeometryManager::createTGeoRepresentation
void createTGeoRepresentation()
Create a TGeo representation of the native geometry description.
Definition: GeometryManager.cc:351
Belle2::SetupGenfitExtrapolationModule::m_useVXDAlignment
bool m_useVXDAlignment
Use VXD alignment from database?
Definition: SetupGenfitExtrapolationModule.h:69
Belle2::geometry::GeometryManager
Class to manage the creation and conversion of the geometry.
Definition: GeometryManager.h:50
Belle2::Module
Base class for Modules.
Definition: Module.h:74
Belle2::Module::setPropertyFlags
void setPropertyFlags(unsigned int propertyFlags)
Sets the flags for the module properties.
Definition: Module.cc:210
Belle2::SetupGenfitExtrapolationModule::m_noiseCoulomb
bool m_noiseCoulomb
Determines if calculation of multiple scattering covariance matrix on/off in Genfit.
Definition: SetupGenfitExtrapolationModule.h:61
Belle2::SetupGenfitExtrapolationModule::m_ignoreIfPresent
bool m_ignoreIfPresent
Whether or not this module will raise an error if the geometry is already present.
Definition: SetupGenfitExtrapolationModule.h:49
genfit::FieldManager::init
void init(AbsBField *b)
set the magnetic field here. Magnetic field classes must be derived from AbsBField.
Definition: FieldManager.h:78
Belle2::Geant4MaterialInterface
AbsMaterialInterface implementation for use with Geant4's navigator.
Definition: Geant4MaterialInterface.h:28
Belle2::SetupGenfitExtrapolationModule::initialize
void initialize() override
Initialize the Module.
Definition: SetupGenfitExtrapolationModule.cc:108
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::Module::addParam
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:562
Belle2::SetupGenfitExtrapolationModule::m_noiseBrems
bool m_noiseBrems
Determines if calculation of bremsstrahlung energy loss variance is on/off in Genfit.
Definition: SetupGenfitExtrapolationModule.h:65
genfit::FieldManager::getInstance
static FieldManager * getInstance()
Get singleton instance.
Definition: FieldManager.h:119
genfit::FieldManager::useCache
void useCache(bool opt=true, unsigned int nBuckets=8)
Cache last lookup positions, and use stored field values if a lookup at (almost) the same position is...
GFGeant4Field
Interface of the Belle II B-field with GenFit.
Definition: GFGeant4Field.h:17
genfit::MaterialEffects::setMscModel
void setMscModel(const std::string &modelName)
Select the multiple scattering model that will be used during track fit.
Definition: MaterialEffects.cc:98
Belle2::SetupGenfitExtrapolationModule::m_geometry
std::string m_geometry
choice of geometry representation: 'TGeo' or 'Geant4'.
Definition: SetupGenfitExtrapolationModule.h:52
genfit::debugOut
std::ostream debugOut
Default stream for debug output.