CbmRoot
Loading...
Searching...
No Matches
CbmGenerateMaterialMaps.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
11
12#include "CbmKfTarget.h"
13#include "CbmRecoSetupManager.h"
14#include "KfRootUtils.h"
15#include "TFile.h"
16#include "TH2F.h"
17
18#include <fmt/format.h>
19
20// ---------------------------------------------------------------------------------------------------------------------
21//
23
24// ---------------------------------------------------------------------------------------------------------------------
25//
26CbmGenerateMaterialMaps::CbmGenerateMaterialMaps(const char* name, int verbose) : FairTask(name, verbose) {}
27
28// ---------------------------------------------------------------------------------------------------------------------
29//
31
32// ---------------------------------------------------------------------------------------------------------------------
33//
35try {
36 // ----- Init configuration
37 if (!fsUserConfig.empty()) {
39 }
40 else {
41 LOG(info) << fName << ": configuration file was not provided. Using default settings";
42 }
43
45
46 fpMaterialFactory = std::make_unique<::kf::tools::MaterialMapFactory>();
47 if (!fConfig.fbParallelRays) {
48 fpMaterialFactory->SetDoRadialProjection(fTargetZ);
49 }
50 fpMaterialFactory->SetSafeMaterialInitialization(true);
51 fpMaterialFactory->SetNraysPerDim(fConfig.fNofRays);
52
53 const auto* pRecoSetupManager = cbm::RecoSetupManager::Instance();
54 if (!pRecoSetupManager->IsInitialized()) {
55 return kFATAL;
56 }
57
58 const auto& recoSetup = pRecoSetupManager->GetSetup();
59
60 if (fConfig.fbTrackingStations) {
61 std::set<MaterialSlice> mSlice;
62 // Collect information on material slices from different units
63 recoSetup.ForEachUnit([&mSlice, &fName = this->fName](const auto& unit) {
64 using Unit_t = std::remove_reference_t<std::remove_cv_t<decltype(unit)>>;
65 LOG(info) << fName << ": generating material budget maps for " << Unit_t::GetDetectorName();
66 int nSt = unit.GetNofTrackingStations();
67 for (int iSt = 0; iSt < nSt; ++iSt) {
68 const auto& vol = unit.GetFullVolume(iSt);
69 mSlice.insert(std::move(MaterialSlice{.fName = fmt::format("{}_station_{}", Unit_t::GetDetectorName(), iSt),
70 .fRefZ = vol.GetCenterZ(),
71 .fMinZ = vol.GetMinZ(),
72 .fMaxZ = vol.GetMaxZ(),
73 .fMaxXY = std::max(std::fabs(vol.GetMaxX()), std::fabs(vol.GetMaxY()))}));
74 }
75 });
76
77 double zLast = fTargetZ + 1.;
78 // Run material helper
79 for (auto it = mSlice.begin(); it != mSlice.end(); ++it) {
80 LOG(info) << "Creating material map for " << it->fName;
81 double z1 = it->fMaxZ;
82 double z2 = z1;
83 auto itNext = std::next(it, 1);
84 if (itNext != mSlice.end()) {
85 z2 = itNext->fMinZ;
86 }
87 double zRef = it->fRefZ;
88 double zNew = 0.5 * (z1 + z2);
89 double xyMax = kXYoffset * it->fMaxXY;
90 int nBins = static_cast<int>(std::ceil(2. * xyMax / fConfig.fPitch));
91 if (nBins < 1) {
92 LOG(fatal) << fName << ": selected pitch " << fConfig.fPitch << " gives wrong number of bins = " << nBins;
93 }
94 if (nBins > fConfig.fMaxNofBins) {
95 nBins = fConfig.fMaxNofBins;
96 }
97 fmMaterial[it->fName] = std::move(fpMaterialFactory->GenerateMaterialMap(zRef, zLast, zNew, xyMax, nBins));
98 zLast = zNew;
99 }
100 }
101
102 // Loop over user-defined slices
103 for (const auto& slice : fConfig.fvUserSlices) {
104 LOG(info) << "Creating material map for " << slice.fName;
105 if (fmMaterial.find(slice.fName) != fmMaterial.end()) {
106 LOG(warn) << fName << ": Material for slice " << slice.fName << " was already prepared. "
107 << "Please, check your configuration file";
108 continue;
109 }
110 double xyMax = kXYoffset * slice.fMaxXY;
111 double zMin = slice.fMinZ;
112 double zMax = slice.fMaxZ;
113 double zRef = slice.fRefZ;
114
115 int nBins = static_cast<int>(std::ceil(2. * xyMax / fConfig.fPitch));
116
117 if (nBins < 1) {
118 throw std::runtime_error(fmt::format("selected pitch {} gives wrong number of bins ({})", fConfig.fPitch, nBins));
119 }
120 if (nBins > fConfig.fMaxNofBins) {
121 nBins = fConfig.fMaxNofBins;
122 }
123 fmMaterial[slice.fName] = fpMaterialFactory->GenerateMaterialMap(zRef, zMin, zMax, xyMax, nBins);
124 }
125
127 return kSUCCESS;
128}
129catch (const std::exception& err) {
130 LOG(error) << fName << ": task initialization failed. Reason: " << err.what();
131 return kFATAL;
132}
133
134// ---------------------------------------------------------------------------------------------------------------------
135//
136InitStatus CbmGenerateMaterialMaps::ReInit() { return kSUCCESS; }
137
138// ---------------------------------------------------------------------------------------------------------------------
139//
141{
142 auto f = TFile(fsOutputFile.c_str(), "RECREATE");
143 f.cd();
144 for (const auto& [name, material] : fmMaterial) {
145 double zMin = material.GetZmin();
146 double zMax = material.GetZmax();
147 std::string title = Form("Material thickness in X_{0}, z region [%.2f,%.2f] cm (%s)", zMin, zMax, name.c_str());
148 if (fConfig.fbParallelRays) {
149 title += "; horizontal projection";
150 }
151 else {
152 title += Form("; radial projection from z=%f cm", fTargetZ);
153 }
154 auto* h = ::kf::tools::RootUtils::ToHistogram(material, name.c_str(), title.c_str());
155 h->SetDirectory(&f);
156 }
157 f.Write();
158}
A FAIR-task to generate material maps (header)
Target property initialization and access in CBM (source)
A manager for setup representation in CBM reconstruction.
Different ROOT utility functions for the KF-framework (header)
InitStatus ReInit()
Re-initializer.
Config fConfig
Offset from target to start generating mat. maps [cm].
std::map< std::string, MaterialMap > fmMaterial
Material budget maps.
CbmGenerateMaterialMaps()
Default constructor.
static constexpr double kXYoffset
std::string fsOutputFile
Output file name.
double fTargetZ
z-coordinate of the target center
std::unique_ptr<::kf::tools::MaterialMapFactory > fpMaterialFactory
Material factory instance.
InitStatus Init()
Initializer.
void WriteMaterialMaps()
Writes material budget maps to file.
std::string fsUserConfig
User configuration file.
Data class with information on a STS local track.
static RecoSetupManager * Instance()
Instance access.
static Target * Instance()
Instance access.
double GetZ() const
Gets target center z-coordinate [cm].
Definition CbmKfTarget.h:34
T ReadFromFile(fs::path path)
Definition CbmYaml.h:66
Input parameters for the material map generation.
static TH2F * ToHistogram(const cbm::algo::kf::MaterialMap &material, const TString &name, const TString &title)
Converts a material budget map into a TH2D histogram.