23#include "fairlogger/Logger.h"
26#include <nlohmann/json.hpp>
31 const char* constraintsPath,
33 :
BmnAligner(measureModel, modelDetector, constraintsPath, nThreads)
36 LOG(state) <<
" > BmnAlignerTest : initialization started";
45 LOG(info) <<
" RAM required: " << ram_size / 1073741824.0 <<
" GB, ";
46 if (ram_size > BMN_MAX_HIT_RAM_SIZE) {
47 LOG(error) <<
"<!> required RAM size is too large for test:" << std::endl
48 <<
"<!> decrease number of tracks or detector planes" << std::endl
49 <<
"<!> max RAM allowed: " << BMN_MAX_HIT_RAM_SIZE / 1073741824.0 <<
" GB";
55 fpHitsData =
new Byte_t[ram_size];
56 fpHits =
reinterpret_cast<BmnATestHit*
>(fpHitsData);
64 LOG(state) <<
" < BmnAlignerTest : initialization completed";
76 LOG(state) <<
" > BmnAlignerTest : data preparation started";
78 LOG(error) <<
" <!> BmnAlignerTest is not initialized";
82 Bool_t loadTracks{tracksPath && std::filesystem::exists(tracksPath)};
87 LOG(error) <<
" <!> failed to load tracks from file " << tracksPath;
91 LOG(info) <<
" Generating random tracks";
96 BmnATestTrack(BMN_TEST_ALPHA1_SIGMA, BMN_TEST_ALPHA2_MIN, BMN_TEST_ALPHA2_MAX,
100 LOG(warning) <<
" failed to save tracks to file " << tracksPath;
104 SVect3 HitPoint, HitPointLocal;
106 Int_t iTrack, iHit{0};
108 fpFirstHits[iTrack] = iHit;
110 for (
Int_t iDet = 0; iDet < BMN_MODULE_COUNT; iDet++, iHit++) {
115 HitPoint = {Track.
X(Z), Track.
Y(Z), Z};
118 LOG_IF(error, HitPointLocal(2) > BMN_CLOSE_TO_ZERO)
119 <<
" <!> non-zero local z coordinate of test hit: " << HitPointLocal(2);
121 auto pHit =
new (fpHits + iHit)
122 BmnATestHit(HitPointLocal(0) + BMN_RND.Gaus(0.0, BMN_TEST_AVERAGE_DX),
123 HitPointLocal(1) + BMN_RND.Gaus(0.0, BMN_TEST_AVERAGE_DY),
125 BMN_TEST_AVERAGE_DX, BMN_TEST_AVERAGE_DY, &Detector);
130 fpFirstHits[iTrack] = iHit;
135 BmnATestHit &firstHit = fpHits[fpFirstHits[iTrack]], &lastHit = fpHits[fpFirstHits[iTrack + 1] - 1];
136 Double_t dX{lastHit.GetX() - firstHit.
GetX()}, dY{lastHit.GetY() - firstHit.
GetY()},
137 dZ{lastHit.GetZ() - firstHit.
GetZ()}, AlphaX1{dX / dZ}, AlphaY1{dY / dZ};
138 arrAlpha[iTrack] =
SVectLC(firstHit.
GetX() - AlphaX1 * firstHit.
GetZ(), AlphaX1,
139 firstHit.
GetY() - AlphaY1 * firstHit.
GetZ(), AlphaY1);
143 fOmegaScaleFactor = (BMN_TEST_AVERAGE_DX + BMN_TEST_AVERAGE_DY) * 0.5;
144 fOmegaScaleFactor *= fOmegaScaleFactor;
146 StoreZeroSolution(arrAlpha);
158 LOG(info) <<
" " <<
fTotalTracks <<
" tracks ready, total hits: " << iHit <<
", time spent: " << Timer.CpuTime()
160 LOG(state) <<
" < BmnAlignerTest : data preparation completed";
161 return LoadConstraints();
173 const SVectLC& Alpha = GetResult()->Alpha(
i);
177 LOG(info) <<
" === average local parameters convergence:";
178 LOG(info) <<
" error by X = (" << delta(
iX0) <<
", " << delta(
iX1) <<
")";
179 LOG(info) <<
" error by Y = (" << delta(
iY0) <<
", " << delta(
iY1) <<
")";
186 LOG(info) <<
" Saving tracks data to " << path;
189 std::ofstream ofs(path);
191 LOG(error) <<
" <!> failed to open JSON file " << path;
197 j[
"alphas"] = nlohmann::json::array();
200 const auto& Alpha = tr.GetAlpha();
201 j[
"alphas"].push_back({Alpha(
iX0), Alpha(
iX1), Alpha(
iY0), Alpha(
iY1)});
210 LOG(info) <<
" Loading tracks data from " << path;
211 std::ifstream ifs(path);
213 LOG(error) <<
" <!> failed to open JSON file " << path;
227 const auto& arr = j[
"alphas"];
229 const auto& a = arr[
i];
230 SVectLC Alpha(a[
iX0].get<double>(), a[
iX1].get<double>(), a[
iY0].get<double>(), a[
iY1].get<double>());
243 fpPad3D->SetTitle(
"Some random tracks");
245 for (
Int_t i = 0;
i < BMN_MODULE_COUNT;
i++)
248 std::vector<Int_t> drawn_tracks(BMN_DRAW_TRACKS_CNT, -1);
249 for (
Int_t i = 0, j;
i < BMN_DRAW_TRACKS_CNT;
i++) {
251 if (drawn_tracks.end() == std::find(drawn_tracks.begin(), drawn_tracks.end(), j) &&
fpTrackUsable->at(
i)) {
260 double xValues[BMN_GLPARAMS_TOTAL];
261 for (
auto i = 0;
i < BMN_GLPARAMS_TOTAL;
i++)
262 xValues[
i] =
static_cast<double>(
i + 1);
266 for (
Int_t i = 0, k = 0;
i < BMN_MODULE_COUNT;
i++)
268 A[k] = GetResult()->A(
i)(j);
270 fpPadG1->SetTitle(
"Solution");
271 TGraph* graphSolution =
new TGraph(BMN_GLPARAMS_TOTAL, xValues, A);
272 graphSolution->
SetTitle(
"Alignment values");
277 graphSolution->
Draw(
"AB");
279 for (
Int_t i = 0, k = 0;
i < BMN_MODULE_COUNT;
i++)
282 TGraph* graphTest =
new TGraph(BMN_GLPARAMS_TOTAL, xValues, T);
284 graphTest->
Draw(
"*SAME");
325 Double_t beforeValue{0.0}, afterValue{0.0};
326 for (
Int_t i = 0;
i < BMN_MODULE_COUNT;
i++) {
330 LOG(info) <<
" === distance to test A before/after alignment: " << std::sqrt(beforeValue) <<
" / "
331 << std::sqrt(afterValue);
336 fpResultCurrent->ResetValueMSE();
338 fpResultCurrent->ResidualsX().Reset();
339 fpResultCurrent->ResidualsY().Reset();
346 return fpResultCurrent->GetValueMSE();
RM::SVector< Double_t, 3 > SVect3
RM::SVector< Double_t, BMN_LOCAL_PARAMS_PT > SVectLC
RM::SVector< Double_t, BMN_GLOBAL_PARAMS_PD > SVectGL
#define BMN_GLOBAL_PARAMS_PD
const SVect3 & GetNormal() const noexcept
const SVect3 & GetCrdShift() const noexcept
Double_t GetPosition() const noexcept
const SMatr3x3 & GetCrdInvRotation() const noexcept
Double_t GetConst() const noexcept
const SVect3 & GetVertex() const noexcept
Double_t Y(Double_t z) const noexcept
Double_t X(Double_t z) const noexcept
void Draw(Bool_t solid=kTRUE, Double_t maxZ=BMN_TEST_TRACK_DEF_MAX_Z) const
const SVect3 & GetDirection() const noexcept
void AddHit(BmnATestHit &hit)
SVectGL & A(Int_t index) noexcept
BmnATestTrack * fpTestTracks
virtual void ReportResults() const override
virtual Double_t CalculateMSE(Bool_t withResiduals) override
virtual Bool_t PrepareData(const char *tracksPath=nullptr) override
virtual Bool_t IterateAlignment() override
virtual ~BmnAlignerTest()
virtual void Draw() override
const BmnATestDetector * fpDetectors
Bool_t LoadTracks(const char *path)
Byte_t * fpTestTracksData
Bool_t SaveTracks(const char *path) const
const BmnATestTrack & GetTestTrack(Int_t trackID) const noexcept
VecBool_t * fpTrackUsable
BmnRamIterator * fpRamIterator
BmnAligner(const BmnMeasureModel *measureModel, BmnDetectorModel *modelDetector, const char *filesListPath, const char *constraintsPath=nullptr, const char *presigmaPath=nullptr, Int_t nThreads=1, Bool_t withProgressBar=kTRUE)
Bool_t IterateAlignment()
Int_t TotalTracks() const noexcept
Double_t GetY() const noexcept
Double_t GetX() const noexcept
Double_t GetZ() const noexcept
virtual void SetFillColor(Color_t fcolor)
virtual void SetLineColor(Color_t lcolor)
virtual void SetRangeUser(Double_t ufirst, Double_t ulast)
virtual void SetMaximum(Double_t maximum=-1111)
void Draw(Option_t *chopt="") override
void SetTitle(const char *title="") override
virtual void SetMinimum(Double_t minimum=-1111)
virtual Double_t Uniform(Double_t x1, Double_t x2)
T Mag2(const SVector< T, D > &rhs)
T Dot(const SVector< T, D > &lhs, const SVector< T, D > &rhs)
Expr< UnaryOp< Fabs< T >, Expr< A, T, D, D2, R >, T >, T, D, D2, R > fabs(const Expr< A, T, D, D2, R > &rhs)