Belle II Software development
MaterialScan.h
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#pragma once
10
11#include <framework/core/Module.h>
12#include <framework/gearbox/Unit.h>
13
14#include <optional>
15#include <set>
16#include <string>
17#include <vector>
18
19#include <G4UserSteppingAction.hh>
20#include <G4ThreeVector.hh>
21
22#include <TFile.h>
23class TH1D;
24class TH2D;
25
26namespace Belle2 {
33 class MaterialScanBase: public G4UserSteppingAction {
34 public:
37 MaterialScanBase(TFile* rootFile, const std::string& name, const std::string& axisLabel):
38 m_rootFile(rootFile), m_name(name), m_axisLabel(axisLabel)
39 {
40 m_rootFile->mkdir(name.c_str());
41 }
43 std::string getName() const { return m_name; }
45 virtual int getNRays() const = 0;
46
54 virtual bool createNext(G4ThreeVector& origin, G4ThreeVector& direction) = 0;
55
56 protected:
58 bool checkStep(const G4Step* step);
60 TFile* m_rootFile;
62 std::string m_name;
64 std::string m_axisLabel;
65 private:
67 static constexpr double c_zeroTolerance = 1e-6;
69 static constexpr int c_maxZeroStepsNudge = 10;
71 static constexpr int c_maxZeroStepsKill = 20;
73 int m_zeroSteps {0};
74 };
75
81 public:
83 struct ScanParams {
85 ScanParams(): nU(0), nV(0), minU(0), maxU(0), minV(0), maxV(0), maxDepth(-1), splitByMaterials(false) {}
87 int nU;
89 int nV;
91 double minU;
93 double maxU;
95 double minV;
97 double maxV;
99 double maxDepth;
101 std::vector<std::string> ignoredMaterials;
104 };
105
112 MaterialScan2D(TFile* rootFile, const std::string& name, const std::string& axisLabel, const ScanParams& params);
113
119 bool createNext(G4ThreeVector& origin, G4ThreeVector& direction) override;
120
122 int getNRays() const override { return m_params.nU * m_params.nV; }
123
125 void UserSteppingAction(const G4Step* step) override;
126 protected:
132 virtual void getRay(G4ThreeVector& origin, G4ThreeVector& direction) = 0;
133
137 TH2D* getHistogram(const std::string& name);
138
143 void fillValue(const std::string& name, double value);
144
148 double m_curU;
150 double m_stepU;
152 double m_curV;
154 double m_stepV;
158 std::map<std::string, std::unique_ptr<TH2D>> m_regions;
159 };
160
165 public:
172 MaterialScanSpherical(TFile* rootFile, const G4ThreeVector& origin, const ScanParams& params, bool doCosTheta):
173 MaterialScan2D(rootFile, "Spherical", doCosTheta ? "cos(#theta);#phi [deg]" : "#theta [deg];#phi [deg]", params), m_origin(origin),
174 m_doCosTheta(doCosTheta)
175 {
176 if (doCosTheta) {
179 if (m_params.minU > m_params.maxU) std::swap(m_params.minU, m_params.maxU);
181 m_curU = m_params.minU - m_stepU / 2.;
182 }
183 }
184 protected:
186 void getRay(G4ThreeVector& origin, G4ThreeVector& direction) override;
187
189 G4ThreeVector m_origin;
190
193 };
194
203 public:
211 MaterialScanPlanar(TFile* rootFile, const G4ThreeVector& origin, const G4ThreeVector& dirU, const G4ThreeVector& dirV,
212 const ScanParams& params):
213 MaterialScan2D(rootFile, "Planar", "u [cm];v [cm]", params), m_origin(origin), m_dirU(dirU.unit()), m_dirV(dirV.unit()),
214 m_dirW(m_dirU.cross(m_dirV))
215 {
216 }
217 protected:
219 void getRay(G4ThreeVector& origin, G4ThreeVector& direction) override;
220 protected:
222 G4ThreeVector m_origin;
224 G4ThreeVector m_dirU;
226 G4ThreeVector m_dirV;
228 G4ThreeVector m_dirW;
229 };
230
240 public:
242 MaterialScanRay(TFile* rootFile, const G4ThreeVector& origin, const G4ThreeVector& dir, double opening,
243 int count, double sampleDepth, double maxDepth, bool splitByMaterials,
244 const std::vector<std::string>& ignoredMaterials):
245 MaterialScanBase(rootFile, "Ray", "ray length [cm]; material budget [X_0/cm]"),
246 m_ignoredMaterials(ignoredMaterials.begin(), ignoredMaterials.end()),
247 m_origin(origin), m_dir(dir), m_opening(opening), m_sampleDepth(sampleDepth),
248 m_maxDepth(maxDepth), m_count(std::max(1, count)), m_splitByMaterials(splitByMaterials)
249 {
250 if (m_opening <= 0) m_count = 1;
251 }
252
254 int getNRays() const override { return m_count + 1; }
256 void UserSteppingAction(const G4Step* step) override;
258 bool createNext(G4ThreeVector& origin, G4ThreeVector& direction) override;
259 private:
263 TH1D* getHistogram(const std::string& name);
269 void fillValue(const std::string& name, double value, double steplength);
270
272 std::set<std::string> m_ignoredMaterials;
274 std::map<std::string, std::unique_ptr<TH1D>> m_regions;
276 G4ThreeVector m_origin;
278 G4ThreeVector m_dir;
280 double m_opening;
284 double m_maxDepth{0};
286 double m_curDepth{0};
292 double m_scanDepth{0};
296 int m_curRay{ -1};
299 };
300
311 class MaterialScanModule : public Module {
312
313 public:
314
315 /* The constructor of the module.
316 * Sets the description and the parameters of the module.
317 */
319
321 void initialize() override;
322
324 void beginRun() override;
325 private:
328 G4ThreeVector getAxis(char name);
329
333 std::string m_filename;
335 std::string m_planeName;
341 std::vector<double> m_customPlane;
343 std::vector<double> m_sphericalOrigin;
345 std::vector<double> m_rayOrigin{0, 0, 0};
347 std::vector<std::string> m_rayIgnoredMaterials;
349 std::optional<std::vector<double>> m_rayDirection;
351 double m_rayTheta{0};
353 double m_rayPhi{0};
355 double m_rayOpening{0};
357 double m_raySampleDepth{0.1};
359 double m_rayMaxDepth{1000};
361 unsigned int m_rayCount{0};
373 };
375}
Base class to create a Material Scan of the detector geometry.
Definition: MaterialScan.h:80
void fillValue(const std::string &name, double value)
Fill the recorded material budget into the corresponding histogram.
virtual void getRay(G4ThreeVector &origin, G4ThreeVector &direction)=0
Get the origin and direction for the next scan particle.
double m_stepU
Stepsize for the parameter u.
Definition: MaterialScan.h:150
bool createNext(G4ThreeVector &origin, G4ThreeVector &direction) override
Get the origin and direction for the next scan particle.
Definition: MaterialScan.cc:97
TH2D * getHistogram(const std::string &name)
get histogram for a given name, create if needed.
void UserSteppingAction(const G4Step *step) override
Record the material budget for each step of the particles.
std::map< std::string, std::unique_ptr< TH2D > > m_regions
Map holding pointers to all created histograms.
Definition: MaterialScan.h:158
double m_curDepth
Tracklength of the current Ray.
Definition: MaterialScan.h:156
int getNRays() const override
Return the number of rays in this scan.
Definition: MaterialScan.h:122
double m_curU
Current value of the parametetr u.
Definition: MaterialScan.h:148
double m_curV
Current value of the parametetr v.
Definition: MaterialScan.h:152
double m_stepV
Stepsize for the parameter v.
Definition: MaterialScan.h:154
ScanParams m_params
Parameters for the scan.
Definition: MaterialScan.h:146
Base class for Material Scans.
Definition: MaterialScan.h:33
virtual bool createNext(G4ThreeVector &origin, G4ThreeVector &direction)=0
Belle2::MaterialScanBase::createNext() implemention Get the origin and direction for the next scan pa...
bool checkStep(const G4Step *step)
check for stuck tracks by looking at the step length
Definition: MaterialScan.cc:44
int m_zeroSteps
Count the number of steps with (almost) zero length.
Definition: MaterialScan.h:73
static constexpr int c_maxZeroStepsNudge
maximum number of consecutive zero steps before nudging the track along
Definition: MaterialScan.h:69
virtual int getNRays() const =0
Return the number of rays necessary to perform the scan.
TFile * m_rootFile
Pointer to the root file for the histograms.
Definition: MaterialScan.h:60
std::string m_axisLabel
Labels for the coordinate axes.
Definition: MaterialScan.h:64
MaterialScanBase(TFile *rootFile, const std::string &name, const std::string &axisLabel)
This is indeed the constructor and it creates a TDirectory in the output root file and sets all varia...
Definition: MaterialScan.h:37
std::string getName() const
Return the name of the scan.
Definition: MaterialScan.h:43
static constexpr int c_maxZeroStepsKill
maximum number of consecutive zero steps before killing the track
Definition: MaterialScan.h:71
std::string m_name
Name of the scan, will be prefixed to all histogram names.
Definition: MaterialScan.h:62
static constexpr double c_zeroTolerance
maximum Step length to be considered zero
Definition: MaterialScan.h:67
The MaterialScan module.
Definition: MaterialScan.h:311
double m_rayTheta
Theta direction of the ray if custom direction is not set.
Definition: MaterialScan.h:351
G4ThreeVector getAxis(char name)
Return a vector along the axis with the given name.
double m_raySampleDepth
Sample depth of the ray: create one bin every x cm.
Definition: MaterialScan.h:357
bool m_raySplitByMaterials
If true Split by materials instead of regions when doing ray scan.
Definition: MaterialScan.h:372
double m_rayPhi
Phi direction of the ray if custom direction is not set.
Definition: MaterialScan.h:353
void initialize() override
Initialize the output file and check the parameters.
TFile * m_rootFile
Pointer to the ROOT file for the histograms.
Definition: MaterialScan.h:331
std::string m_planeName
Name of the plane to use for scanning.
Definition: MaterialScan.h:335
std::vector< std::string > m_rayIgnoredMaterials
Materials ignored when ray scanning.
Definition: MaterialScan.h:347
unsigned int m_rayCount
Number of rays to shoot (if opening angle is >0)
Definition: MaterialScan.h:361
std::optional< std::vector< double > > m_rayDirection
Direction of the ray, cannot be set at the same time as Theta and Phi.
Definition: MaterialScan.h:349
void beginRun() override
Do the actual scanning.
MaterialScan2D::ScanParams m_spherical
Scan parameters for the spherical scan.
Definition: MaterialScan.h:337
bool m_doSpherical
Wether or not to do a spherical scan.
Definition: MaterialScan.h:363
bool m_doPlanar
Wether or not to do a planar scan.
Definition: MaterialScan.h:365
bool m_doRay
Perform a material ray scan: scan along a certain direction with a given opening angle and plot mater...
Definition: MaterialScan.h:370
std::vector< double > m_sphericalOrigin
Origin for spherical scan.
Definition: MaterialScan.h:343
std::string m_filename
Filename for the ROOT output.
Definition: MaterialScan.h:333
MaterialScan2D::ScanParams m_planar
Scan parameters for the planar scan.
Definition: MaterialScan.h:339
double m_rayOpening
Opening angle of the ray.
Definition: MaterialScan.h:355
double m_rayMaxDepth
Max depth of the ray before stopping.
Definition: MaterialScan.h:359
std::vector< double > m_customPlane
Custom plane definition if m_planName is "custom".
Definition: MaterialScan.h:341
bool m_doCosTheta
Perform the spherical scan uniform in cos(theta) instead of theta.
Definition: MaterialScan.h:367
std::vector< double > m_rayOrigin
Origin of the ray if a ray scan is performed
Definition: MaterialScan.h:345
Specific implementaion of MaterialScan to scan parallel to a given plane.
Definition: MaterialScan.h:202
G4ThreeVector m_origin
Origin of the scan plane.
Definition: MaterialScan.h:222
G4ThreeVector m_dirV
v direction of the scan plane
Definition: MaterialScan.h:226
G4ThreeVector m_dirU
u direction of the scan plane
Definition: MaterialScan.h:224
MaterialScanPlanar(TFile *rootFile, const G4ThreeVector &origin, const G4ThreeVector &dirU, const G4ThreeVector &dirV, const ScanParams &params)
Create a Planar Scan object with the given parameters.
Definition: MaterialScan.h:211
G4ThreeVector m_dirW
direction perpendicluar to u and v
Definition: MaterialScan.h:228
void getRay(G4ThreeVector &origin, G4ThreeVector &direction) override
Create a ray with the current parameter values according to a planar distribution.
MaterialScan implementation to shoot one ray along a defined direction and record the Material as a f...
Definition: MaterialScan.h:239
void fillValue(const std::string &name, double value, double steplength)
Fill the recorded material budget into the corresponding histogram.
G4ThreeVector m_origin
Origin of the scan.
Definition: MaterialScan.h:276
std::map< std::string, std::unique_ptr< TH1D > > m_regions
Map holding pointers to all created histograms.
Definition: MaterialScan.h:274
MaterialScanRay(TFile *rootFile, const G4ThreeVector &origin, const G4ThreeVector &dir, double opening, int count, double sampleDepth, double maxDepth, bool splitByMaterials, const std::vector< std::string > &ignoredMaterials)
Construct a new instance and set all parameters.
Definition: MaterialScan.h:242
bool m_splitByMaterials
If true Split by materials instead of regions.
Definition: MaterialScan.h:298
double m_scanDepth
The first ray does not record any material but just checks for the maximum useful depth to not get a ...
Definition: MaterialScan.h:292
bool createNext(G4ThreeVector &origin, G4ThreeVector &direction) override
Implement shooting along the ray.
int m_curRay
Current Ray number: 0 = scan for maximum depth, 1..N = record materials.
Definition: MaterialScan.h:296
void UserSteppingAction(const G4Step *step) override
Record the material budget for each step of the particles.
G4ThreeVector m_dir
Direction of the ray.
Definition: MaterialScan.h:278
double m_curDepth
Current depth of the current ray.
Definition: MaterialScan.h:286
double m_maxDepth
Maximum depth for each ray after which it will be stopped.
Definition: MaterialScan.h:284
int getNRays() const override
Return the number of rays.
Definition: MaterialScan.h:254
double m_opening
Opening angle in radian.
Definition: MaterialScan.h:280
std::set< std::string > m_ignoredMaterials
Materials ignored when scanning.
Definition: MaterialScan.h:272
double m_sampleDepth
The ray length after which to sample.
Definition: MaterialScan.h:282
TH1D * getHistogram(const std::string &name)
get histogram for a given name, create if needed.
int m_count
Amount of rays to shoot.
Definition: MaterialScan.h:294
Specific implementation of MaterialScan to do Spherical scanning.
Definition: MaterialScan.h:164
G4ThreeVector m_origin
Origin for the spherical scan.
Definition: MaterialScan.h:189
MaterialScanSpherical(TFile *rootFile, const G4ThreeVector &origin, const ScanParams &params, bool doCosTheta)
Create a Spherical Scan object with the given parameters.
Definition: MaterialScan.h:172
void getRay(G4ThreeVector &origin, G4ThreeVector &direction) override
Create a ray with the current parameter values according to a spherical distribution.
bool m_doCosTheta
Flag to indicate if polar-angular sampling is uniform in cos(theta) rather than theta.
Definition: MaterialScan.h:192
Base class for Modules.
Definition: Module.h:72
static const double deg
degree to radians
Definition: Unit.h:109
Abstract base class for different kinds of events.
STL namespace.
Helper struct to Store Parameters of a Scan.
Definition: MaterialScan.h:83
double maxU
Maximum u value to scan.
Definition: MaterialScan.h:93
bool splitByMaterials
If true, split output by Materials (otherwise by region)
Definition: MaterialScan.h:103
ScanParams()
Default Constructor.
Definition: MaterialScan.h:85
double minV
Minimum v value to scan.
Definition: MaterialScan.h:95
double maxDepth
Maximum depth of the scan.
Definition: MaterialScan.h:99
int nU
Number of rays along u coordinate.
Definition: MaterialScan.h:87
double minU
Minimum u value to scan.
Definition: MaterialScan.h:91
double maxV
Maximum v value to scan.
Definition: MaterialScan.h:97
std::vector< std::string > ignoredMaterials
Names of ignored Materials.
Definition: MaterialScan.h:101
int nV
Number of rays along v coordinate.
Definition: MaterialScan.h:89