CbmRoot
Loading...
Searching...
No Matches
KfSetupBuilder.cxx
Go to the documentation of this file.
1/* Copyright (C) 2024 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Sergei Zharko [committer] */
4
9
10#include "KfSetupBuilder.h"
11
12#include <sstream>
13
14#include <fmt/format.h>
15
17
18// ---------------------------------------------------------------------------------------------------------------------
19//
21{
22 LOG(info) << "kf::SetupBuilder initilization: ...";
23
25 throw std::runtime_error("kf::SetupBuilder: no field is provided");
26 }
27 if (fbIfSetFromSetup) {
28 LOG(info) << "kf::SetupBuilder initilization: done";
29 fbReady = true;
30 return;
31 }
32 else {
33 if (fGeoLayers.size() == 0) {
34 throw std::runtime_error("kf::SetupBuilder: no geometry layers initialized");
35 }
36 if (!fpMaterialMapFactory.get()) {
37 throw std::runtime_error("kf::SetupBuilder: no material map factory provided");
38 }
39 if (!fbIfTargetSet) {
40 throw std::runtime_error("kf::SetupBuilder: target properties were not set");
41 }
42 }
43
44 bool bMaterialLoaded{false};
45 if (!fsMaterialCacheFile.empty()) {
46 bMaterialLoaded = this->LoadMaterial();
47 }
48
49 // Add material to the target:
50 if (!bMaterialLoaded) {
51 // Material budget
52 double zMin{fTarget.GetZ() + kTargetCenterOffset};
53 double zMax{fTarget.GetZ() + fTarget.GetDz() * kTargetMaterialOffset};
56 fpMaterialMapFactory->GenerateMaterialMap(fTarget.GetZ(), zMin, zMax, rMax, kTargetMaterialMapNofBins));
57 }
58
59 // Init geometry layers
60 {
61 // Estimate acceptance slope
62 double acceptanceSlope = -1.; // arbitrary negative value
63 for (const auto& layer : fGeoLayers) {
64 double tCurr = std::max(std::fabs(layer.fXmax), std::fabs(layer.fYmax)) / (layer.fZref - fTarget.GetZ());
65 assert(tCurr > 0);
66 acceptanceSlope = std::max(acceptanceSlope, tCurr);
67 }
68
69 if (!bMaterialLoaded) {
70 fvMaterial.clear();
71 fvMaterial.reserve(fGeoLayers.size());
72 }
73 double zLast{fTarget.GetZ() + fTarget.GetDz() * kTargetMaterialOffset};
74 for (auto layerIt = fGeoLayers.cbegin(); layerIt != fGeoLayers.cend(); ++layerIt) {
75 double z1 = layerIt->fZmax;
76 double z2 = z1;
77 if (std::next(layerIt) != fGeoLayers.cend()) {
78 z2 = std::next(layerIt)->fZmin;
79 }
80 double zNew{0.5 * (z1 + z2)};
81 double xyMax{acceptanceSlope * (layerIt->fZref - fTarget.GetZ())};
82 if (!bMaterialLoaded) {
83 auto material{fpMaterialMapFactory->GenerateMaterialMap(layerIt->fZref, zLast, zNew, xyMax, fMatMapNofBins)};
84 fvMaterial.push_back(std::move(material));
85 }
86
87 // Note: square material maps to follow the material budget map regions
88 fFieldFactory.AddSliceReference(xyMax, xyMax, layerIt->fZref);
89 fModuleIndexFactory.AddComponent<int>(layerIt->fDetID, layerIt->fLocID, layerIt->fZref);
90 zLast = zNew;
91 }
92 }
93
94 if (!bMaterialLoaded && !fsMaterialCacheFile.empty()) {
95 this->StoreMaterial();
96 }
97
98 fbIfGeoLayersInit = true;
99 fbReady = true;
100 LOG(info) << "kf::SetupBuilder initialization: done";
101}
102
103// ---------------------------------------------------------------------------------------------------------------------
104//
106{
107 std::stringstream msg;
108 msg << "kf::SetupBuilder initialization: failed:\n";
109 msg << fmt::format("\n - {<30} {}", "target property set:", fbIfTargetSet);
110 msg << fmt::format("\n - {<30} {}", "geo layers added:", fbIfGeoLayersInit);
111 msg << fmt::format("\n - {<30} {}", "field function set:", fbIfFieldFunctionSet);
112 msg << fmt::format("\n - {<30} {}", "set from setup:", fbIfSetFromSetup);
113 msg << fmt::format("\n - {<30} {}", "material map creator set:", bool(fpMaterialMapFactory.get()));
114 return msg.str();
115}
116
117// ---------------------------------------------------------------------------------------------------------------------
118//
120{
121 std::ifstream ifs(fsMaterialCacheFile, std::ios::binary);
122 if (!ifs) { // File does not exist yet
123 return false;
124 }
125 try {
126 boost::archive::binary_iarchive ia(ifs);
127 MaterialMap targetMat;
128 size_t refHash;
129 ia >> refHash;
130 if (refHash != fGeoHash) {
131 LOG(warn) << "kf::SetupBuilder::LoadMaterial: reference hash from input file \"" << fsMaterialCacheFile
132 << "\" "
133 "diverges from one, obtained from the actual detector setup geometry. Material budget will be "
134 "re-generated, and a new file will be created";
135 return false;
136 }
137 ia >> targetMat;
138 ia >> fvMaterial;
139 fTarget.SetMaterial(targetMat);
140 }
141 catch (const std::exception& err) {
142 LOG(warn) << "kf::SetupBuilder::LoadMaterial: input file \"" << fsMaterialCacheFile
143 << "\" has inconsistent format "
144 "or was corrupted. The material maps will be re-generated";
145 return false;
146 }
147 LOG(info) << "kf::SetupBuilder::LoadMaterial: the material maps were loaded from cache file \"" << fsMaterialCacheFile
148 << "\"";
149 return true;
150}
151
152// ---------------------------------------------------------------------------------------------------------------------
153//
155{
156 fbReady = false;
157 fbIfTargetSet = false;
158 fbIfGeoLayersInit = false;
159 fbIfFieldFunctionSet = false;
160 fbIfSetFromSetup = false;
163 fvMaterial.clear();
164 fGeoLayers.clear();
165 fpMaterialMapFactory = nullptr;
166}
167
168// ---------------------------------------------------------------------------------------------------------------------
169//
170void SetupBuilder::SetTargetProperty(double x, double y, double z, double dz, double r)
171{
172 fbReady = false;
173 fTarget.SetX(x);
174 fTarget.SetY(y);
175 fTarget.SetZ(z);
176 fTarget.SetDz(dz);
177 fTarget.SetR(r);
179 fbIfTargetSet = true;
180}
181
182// ---------------------------------------------------------------------------------------------------------------------
183//
184void SetupBuilder::Store(const Setup<double>& setup, const std::string& fileName)
185{
186 std::ofstream ofs(fileName, std::ios::binary);
187 if (!ofs) {
188 std::stringstream msg;
189 msg << "kf::SetupBuilder::Store: failed opening file \"" << fileName << "\" to store the setup";
190 throw std::runtime_error(msg.str());
191 }
192 boost::archive::binary_oarchive oa(ofs);
193 oa << setup;
194}
195
196// ---------------------------------------------------------------------------------------------------------------------
197//
199{
200 std::ofstream ofs(fsMaterialCacheFile, std::ios::binary);
201 if (!ofs) {
202 std::stringstream msg;
203 msg << "kf::SetupBuilder::Store: failed opening cache file \"" << fsMaterialCacheFile << "\" to store the setup";
204 throw std::runtime_error(msg.str());
205 }
206 boost::archive::binary_oarchive oa(ofs);
207 oa << fGeoHash;
208 oa << fTarget.GetMaterial();
209 oa << fvMaterial;
210}
A base KF-Setup initialization class (source)
void SetTarget(double x, double y, double z)
Sets target.
Definition KfField.h:401
void Reset()
Resets the instance.
Definition KfField.h:377
void AddSliceReference(double halfSizeX, double halfSizeY, double refZ)
Adds a slice reference.
Definition KfField.cxx:96
A map of station thickness in units of radiation length (X0) to the specific point in XY plane.
void AddComponent(EDetID detId, int locId, double z)
Adds component info.
Creates a valid initialized Setup instance.
void Reset()
Resets the instance.
int fMatMapNofBins
Number of bins in material maps.
std::string fsMaterialCacheFile
A cache file for the material.
static constexpr double kTargetMaterialOffset
std::string InitStatusMsg() const
Prints initialization status message.
bool fbIfFieldFunctionSet
Field function initialized.
static constexpr double kTargetMaterialTransverseSizeMargin
std::set< GeoLayer< int > > fGeoLayers
Set of geo layers.
FieldFactory fFieldFactory
Instance of field factory.
ModuleIndexMapFactory fModuleIndexFactory
Module index factory.
Target< double > fTarget
Target properties.
bool fbIfGeoLayersInit
Geo layers initialized.
bool LoadMaterial()
Reads material from file.
void StoreMaterial() const
Stores material to file.
void Init()
Initializes, validates and caches the parameters.
void SetTargetProperty(double x, double y, double z, double dz, double r)
Sets target initialization properties.
static constexpr int kTargetMaterialMapNofBins
static constexpr double kTargetCenterOffset
std::vector< MaterialMap > fvMaterial
Material map container.
bool fbIfTargetSet
Target initialized.
bool fbReady
Instance is ready for setup generation.
static void Store(const Setup< double > &setup, const std::string &fileName)
Stores a serialized setup to a file.
std::shared_ptr< IMaterialMapFactory > fpMaterialMapFactory
Material map creator.
size_t fGeoHash
A hash of the geometry.
KF-framework representation of the detector setup.
Definition KfSetup.h:33
void SetR(const T &r)
Sets target transverse size.
Definition KfTarget.h:104
const T & GetDz() const
Gets target half-thickness.
Definition KfTarget.h:70
const MaterialMap & GetMaterial() const
Gets material map.
Definition KfTarget.h:76
void SetDz(const T &dz)
Sets target half-thickness.
Definition KfTarget.h:100
void SetY(const T &y)
Sets y-coordinate of the nominal target center.
Definition KfTarget.h:92
void SetZ(const T &z)
Sets x-coordinate of the nominal target center.
Definition KfTarget.h:96
void SetMaterial(const MaterialMap &material)
Sets material map.
Definition KfTarget.cxx:32
const T & GetR() const
Gets transverse size of target.
Definition KfTarget.h:73
void SetX(const T &x)
Sets x-coordinate of the nominal target center.
Definition KfTarget.h:88
const T & GetZ() const
Gets z-coordinate of the nominal target center.
Definition KfTarget.h:67