12#include <boost/filesystem.hpp>
18namespace fs = boost::filesystem;
25 "GeoLayer: detId={}, locId={}, zRef={:9.4}cm, z=[{:9.4}, {:9.4}]cm, xFull=[{:9.4}, {:9.4}]cm, "
26 "yFull=[{:9.4, {:9.4}}]cm, xAct=[{:9.4}, {:9.4}]cm, yAct=[{:9.4}, {:9.4}]cm, fieldType={}, timeInfo={}",
27 detId,
locId,
zRef,
zMin,
zMax,
xMinFull,
xMaxFull,
yMinFull,
yMaxFull,
xMinAct,
xMaxAct,
yMinAct,
yMaxAct,
36 auto [itAdded, wasInserted] =
fGeoLayers.insert(geoLayer);
38 throw std::logic_error(fmt::format(
39 "SetupBuilder::AddLayer: attempt of adding a duplicating geometry layer: {}\n"
40 "The next layers were already added: {}",
42 std::accumulate(
fGeoLayers.begin(),
fGeoLayers.end(), std::string{}, [](std::string s,
const auto& l) {
43 return std::move(s) +
"\n\t - " + std::move(l.ToString());
53 LOG(info) <<
"kf::SetupBuilder initialization: ...";
56 throw std::runtime_error(
"kf::SetupBuilder: no field is provided");
59 LOG(info) <<
"kf::SetupBuilder initialization: done";
65 throw std::runtime_error(
"kf::SetupBuilder: no geometry layers initialized");
68 throw std::runtime_error(
"kf::SetupBuilder: no material map factory provided");
71 throw std::runtime_error(
"kf::SetupBuilder: target properties were not set");
75 bool bMaterialLoaded{
false};
81 if (!bMaterialLoaded) {
93 double acceptanceSlope = -1.;
95 double xyMax = std::max(std::max(std::fabs(layer.xMaxFull), std::fabs(layer.xMinFull)),
96 std::max(std::fabs(layer.yMaxFull), std::fabs(layer.yMinFull)));
97 double tCurr = xyMax / (layer.zRef -
fTarget.GetZ());
99 acceptanceSlope = std::max(acceptanceSlope, tCurr);
102 if (!bMaterialLoaded) {
108 double z1 = layerIt->zMax;
110 if (std::next(layerIt) !=
fGeoLayers.cend()) {
111 z2 = std::next(layerIt)->zMin;
113 double zNew{0.5 * (z1 + z2)};
114 double xyMax{acceptanceSlope * (layerIt->zRef -
fTarget.GetZ())};
115 if (!bMaterialLoaded) {
122 double fieldXmax = std::max(std::fabs(layerIt->xMaxFull), std::fabs(layerIt->xMinFull));
123 double fieldYmax = std::max(std::fabs(layerIt->yMaxFull), std::fabs(layerIt->yMinFull));
124 fFieldBuilder.AddSliceReference(fieldXmax, fieldYmax, layerIt->zRef);
136 LOG(info) <<
"kf::SetupBuilder initialization: done";
144 "kf::SetupBuilder initialization: failed:"
145 "\n - target property set: {}"
146 "\n - geo layers added: {}"
147 "\n - field function set: {}"
148 "\n - set from setup: {}"
149 "\n - material map creator set: {}",
159 std::ifstream ifs(path.string(), std::ios::binary);
164 boost::archive::binary_iarchive ia(ifs);
169 LOG(warn) <<
"kf::SetupBuilder::LoadMaterial: reference hash from input file \"" << path.string()
171 "diverges from one, obtained from the actual detector setup geometry. Material budget will be "
172 "re-generated, and a new file will be created";
177 fTarget.SetMaterial(targetMat);
179 catch (
const std::exception& err) {
180 LOG(warn) <<
"kf::SetupBuilder::LoadMaterial: input file \"" << path.string()
181 <<
"\" has inconsistent format "
182 "or was corrupted. The material maps will be re-generated";
185 LOG(info) <<
"kf::SetupBuilder::LoadMaterial: the material maps were loaded from cache file \"" << path.string()
224 auto path = fs::absolute(fs::weakly_canonical(fileName));
225 fs::create_directories(path.parent_path());
227 std::ofstream ofs(path.string(), std::ios::binary);
229 throw std::runtime_error(
230 fmt::format(
"kf::SetupBuilder::Store: failed opening file \"{}\" to store the setup", path.string()));
232 LOG(info) <<
"kf::SetupBuilder: writing setup into file \"" << path.string() <<
'\"';
233 boost::archive::binary_oarchive oa(ofs);
242 fs::create_directories(path.parent_path());
244 std::ofstream ofs(path.string(), std::ios::binary);
246 throw std::runtime_error(
247 fmt::format(
"kf::SetupBuilder::Store: failed opening file \"{}\" to store the material cache", path.string()));
249 boost::archive::binary_oarchive oa(ofs);
A base KF-Setup initialization class (source)
A map of station thickness in units of radiation length (X0) to the specific point in XY plane.
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.
std::set< LayerProperty > fGeoLayers
Set of geometrical layers.
static constexpr double kTargetMaterialOffset
Offset of the target material [in dz].
std::string InitStatusMsg() const
Prints initialization status message.
bool fbIfFieldFunctionSet
Field function initialized.
static constexpr double kTargetMaterialTransverseSizeMargin
Margin of target transverse size.
ModuleIndexMapFactory fModuleIndexFactory
Module index factory.
Target< double > fTarget
Target properties.
FieldBuilder fFieldBuilder
Instance of field factory.
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 AddLayer(LayerProperty geoLayer)
Adds material layer.
void SetTargetProperty(double x, double y, double z, double dz, double r)
Sets target initialization properties.
static constexpr int kTargetMaterialMapNofBins
Number of bins along x- and y-axis in the target region;.
static constexpr double kTargetCenterOffset
Offset from target center [cm].
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.
std::string ToString() const
String representation of the class.
A helper structure to store geometrical information of the layers.
double xMaxAct
Upper x-coordinate boundary of active volume [cm].
bool timeInfo
If a layer stores time information.
double yMinAct
Lower x-coordinate boundary of active volume [cm].
double zMin
Lower z-coordinate boundary [cm].
int locId
Local index of the detector module.
double xMaxFull
Upper x-coordinate boundary of full volume [cm].
double yMinFull
Lower x-coordinate boundary of full volume [cm].
double zMax
Upper z-coordinate boundary [cm].
double zRef
Reference z-coordinate [cm].
EFieldType fieldType
Type of the magnetic field.
double xMinAct
Lower x-coordinate boundary of active volume [cm].
double xMinFull
Lower x-coordinate boundary of full volume [cm].
double yMaxFull
Upper x-coordinate boundary of full volume [cm].
double yMaxAct
Upper x-coordinate boundary of active volume [cm].