BmnRoot 26.10.0-210-gc52d34b0
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
expandS.C
Go to the documentation of this file.
1/*
2 BM@N alignment routine
3 BM@N experiment at NICA complex, JINR, 2025
4
5 Department: Math & Soft Group of HEP lab
6 Author: Igor Polev, polev@jinr.int
7
8 Solution expansion macro
9 Re-encodes an alignment solution from a coarse detector model into a finer one
10 that distinguishes more detector elements. Every fine element inherits the
11 correction of the coarse element it belongs to.
12*/
13
14// headers below are for IDE syntax to work and they will be
15// turned off during execution in ROOT macro mode
16#ifndef __CLING__
17#include "BmnAlignConfig.h"
18#include "BmnAlignDefines.h"
19#include "BmnAlignModels.h" // IWYU pragma: keep
20#endif
21
22#include "fairlogger/Logger.h"
23
24#include <fstream>
25#include <iomanip>
26#include <nlohmann/json.hpp>
27#include <vector>
28
29// ---- expansion configuration ------------------------------------------------
30using inDetModel = BmnRun8cgDM; // source (coarse) model
31using outDetModel = BmnRun8DM; // output (fine) model
32// -----------------------------------------------------------------------------
33
34/**
35 * @brief Re-encode a solution from a coarse detector model into a finer one.
36 *
37 * Every element of the fine model inherits the correction of the coarse element
38 * it belongs to, so a solution obtained on half-stations can seed a run that
39 * aligns individual modules. The two models are named by the inDetModel and
40 * outDetModel aliases at the top of the file.
41 *
42 * @param inSolutionPath .json solution written for the coarse model
43 * @param outSolutionPath .json file to write the expanded solution into
44 *
45 * @return 0 on success, -1 if the solution could not be read or written
46 */
47int expandS(const char* inSolutionPath = "/home/igor/bmnroot/align_new/cfg/run8/slALL.e11.00.json",
48 const char* outSolutionPath = "/home/igor/bmnroot/align_new/cfg/run8/slALL.e76.00.json")
49{
50 fair::Logger::SetConsoleColor(true);
51 fair::Logger::SetConsoleSeverity("info");
52 LOG(state) << " > expandS.C : solution expansion started";
53
54 // Read the input solution through the file-format layer alone: expanding a
55 // solution needs neither the solver nor a reconstruction task. The flat
56 // parametrization is all that is required to address the file, since the
57 // per-element values it lists are the *effective* corrections - whatever
58 // hierarchy produced them, its levels are already summed into these.
59 inDetModel inputModel;
60 inputModel.Init();
61 BmnAlignHierarchy inputLayout;
62 if (!inputLayout.BuildFlat(inputModel)) {
63 LOG(error) << " <!> failed to build the input model layout";
64 return -1;
65 }
67 if (!BmnAlignConfig::ReadSolution(inSolutionPath, inputLayout, solution)) {
68 LOG(error) << " <!> failed to load solution " << inSolutionPath;
69 return -1;
70 }
71 // corrections by coarse element index, so the loop below can look one up
72 VecSVectGL_t corrections(inputModel.Alignables());
73 for (const BmnAlignConfig::NodeValues& leaf : solution.leaves)
74 corrections[leaf.node] = leaf.values;
75 if (solution.leaves.empty()) {
76 LOG(error) << " <!> " << inSolutionPath << " holds no detector elements";
77 return -1;
78 }
79
80 outDetModel outputModel;
81 outputModel.Init();
82 const Int_t outputCount{outputModel.Alignables()};
83
84 // --- build the expanded solution -----------------------------------------
85 using nlohmann::json;
86 json js;
87 js["CorrectionValues"] = BmnParamLayout::ParamNames();
88 js["DetectorElements"] = outputCount;
89 js["CorrectionsPerDetector"] = json::array();
90
91 Int_t outputCnt{0};
92 for (Int_t i = 0; i < outputCount; i++) {
93 const Int_t outputID{outputModel.EncodedID(i)};
94 json element;
95 element["detector_id"] = outputID;
96 element["values"] = json::array();
97 element["errors"] = json::array();
98
99 // coarse element this fine element belongs to (per the old model)
100 const Int_t hitID{inputModel.IDfromHit(outputID)};
101 if (inputModel.UnknownID(hitID)) {
102 LOG(error) << " <!> element " << outputID << " maps to unknown id " << hitID << " in input model, aborting";
103 return -1;
104 }
105
106 const SVectGL& A{corrections[inputModel.Idx(hitID)]};
107 for (Int_t j = 0; j < BMN_GLOBAL_PARAMS_PD; j++) {
108 element["values"].push_back(A(j));
109 element["errors"].push_back(0.0); // errors are discarded on load
110 }
111 js["CorrectionsPerDetector"].push_back(element);
112 outputCnt++;
113 }
114
115 // --- write the expanded solution -----------------------------------------
116 std::ofstream ofs(outSolutionPath);
117 if (!ofs.good()) {
118 LOG(error) << " <!> failed to open output file " << outSolutionPath;
119 return -1;
120 }
121 try {
122 ofs << std::setw(4) << js << std::endl;
123 } catch (std::exception& e) {
124 LOG(error) << " <!> failed to write JSON data\n" << e.what();
125 return -1;
126 }
127
128 LOG(info) << " " << outputCnt << " elements written to " << outSolutionPath;
129 LOG(state) << " < expandS.C : solution expansion completed";
130 return 0;
131}
std::vector< SVectGL > VecSVectGL_t
RM::SVector< Double_t, BMN_GLOBAL_PARAMS_PD > SVectGL
#define BMN_GLOBAL_PARAMS_PD
int i
Definition P4_F32vec4.h:22
int Int_t
static Bool_t ReadSolution(const char *jsonPath, const BmnAlignHierarchy &hierarchy, Solution &solution)
Bool_t BuildFlat(const BmnDetectorModel &detModel)
Int_t EncodedID(Int_t idx) const
Bool_t Init(const char *targetGeoFile=nullptr)
Bool_t UnknownID(Int_t hitID) const
Int_t Idx(Int_t ID) const
static const std::vector< std::string > & ParamNames()
virtual Int_t Alignables() const noexcept override
Definition BmnRun8DM.h:139
virtual Int_t IDfromHit(Int_t CbmDetectorID) const noexcept override
Definition BmnRun8cgDM.h:72
BmnRun8DM outDetModel
Definition expandS.C:31
BmnRun8cgDM inDetModel
Definition expandS.C:30
int expandS(const char *inSolutionPath="/home/igor/bmnroot/align_new/cfg/run8/slALL.e11.00.json", const char *outSolutionPath="/home/igor/bmnroot/align_new/cfg/run8/slALL.e76.00.json")
Re-encode a solution from a coarse detector model into a finer one.
Definition expandS.C:47
std::vector< NodeValues > leaves