17#include "fairlogger/Logger.h"
38constexpr Double_t CONSTRAINT_TOL{1.0E-9};
40constexpr Int_t VERTEX_STEPS{3};
110 const char* dataPath,
111 const char* solutionPath,
117 LOG(state) <<
" > BmnAlignTestTask : preparing test task";
119 LOG(error) <<
"<!> BmnAligner is not initialized";
120 LOG(state) <<
" < BmnAlignTestTask : test task preparation terminated";
124 LOG(error) <<
" <!> BmnAlignTestTask::Make: no parameter layout, PrepareData() must run first";
127 if (sigmaScale < 0.0) {
128 LOG(error) <<
" <!> BmnAlignTestTask::Make: sigmaScale " << sigmaScale <<
" is negative";
131 if (
kUserValues == noise && (stdDevXY.
X() < 0.0 || stdDevXY.
Y() < 0.0)) {
132 LOG(error) <<
" <!> BmnAlignTestTask::Make: noise StdDev (" << stdDevXY.
X() <<
", " << stdDevXY.
Y()
141 const Bool_t withBeam{trgID >= 0};
145 TFile outFile(dataPath,
"RECREATE");
147 LOG(error) <<
" <!> BmnAlignTestTask::Make: failed to create output file " << dataPath;
152 std::unique_ptr<BmnAlignResult> pResultCopy;
153 auto RestoreResults = [&]() {
163 pResultCopy = std::make_unique<BmnAlignResult>(aligner.
ResultLast());
177 if (detID == trgID) {
185 lineX.Add(hit(
iZ), hit(
iX), itr.
HitWx());
186 lineY.Add(hit(
iZ), hit(
iY), itr.
HitWy());
201 errX[idx] += 1.0 / std::sqrt(ram.
HitWx());
202 errY[idx] += 1.0 / std::sqrt(ram.
HitWy());
206 LOG(info) <<
" mean hit errors of the elements in the source data, x and y (cm):";
223 LOG(warning) <<
" ! z0 is left at the target centre";
225 Int_t iTrack{0}, vertexTracks{0};
229 vertexTrack[iTrack] =
231 vertexTracks += vertexTrack[iTrack];
234 LOG(info) <<
Form(
" %d vertex tracks, %zu free tracks (vertex hit rejected or absent)", vertexTracks,
235 vertexTrack.size() - vertexTracks);
252 vQ(p.full) = priors.
ref[p.full] +
gRandom->Gaus(0.0, sigma);
253 vVar(p.full) = sigma * sigma;
260 auto LogRms = [&](
const TVectorD& q,
const char* stage) {
264 if (!r.IsComposite() && r.leaf == trgIdx)
266 (r.IsComposite() ? comp : leaf)(r.prm) += p(r.full) * p(r.full);
272 row +=
Form(
" %10.3e", n ? std::sqrt(sum(k) / n) : 0.0);
275 LOG(info) <<
Form(
" %-12s elements ", stage) << Row(leaf, nLeaf);
276 LOG_IF(info, nComp) <<
Form(
" %-12s composites", stage) << Row(comp, nComp);
281 LOG(info) <<
Form(
" %-12s %-10s",
"truth RMS",
"(cm, rad)") << head;
285 for (
Int_t j = 0; j < nCnstr; j++) {
287 Int_t k{-1}, terms{0};
295 vQ(k) = priors.
rhs(j) / c(k);
299 Int_t drawn{0}, fixed{0};
319 LOG(error) <<
" <!> BmnAlignTestTask::Make: SVD failed, the truth cannot be projected"
320 " onto the constraints";
327 for (
Int_t j = 0; j < nCnstr; j++)
328 vX(j) = sig(j) > sig(0) * RANK_TOL ? vX(j) / sig(j) : 0.0;
329 vX = svd.
GetV() * vX;
335 for (
Int_t j = 0; j < nCnstr; j++)
336 if (std::fabs(vViol(j)) > CONSTRAINT_TOL) {
337 LOG(error) <<
" <!> BmnAlignTestTask::Make: constraint #" << j + 1 <<
" cannot hold, violated by "
338 << vViol(j) <<
" - it rests on parameters without a truth sigma";
346 LogRms(vQ,
"constrained");
351 LOG(info) <<
" truth drawn: " << drawn <<
" parameter(s) from the truth sigmas times " << sigmaScale <<
", "
352 << fixed <<
" fixed by single-parameter constraints, all " << nCnstr <<
" constraint(s) hold";
353 LOG_IF(warning, !drawn && sigmaScale > 0.0)
354 <<
" ! no parameter carries a truth sigma: the truth is the nominal geometry";
356 const SVectGL& aTrg{truth.
A(trgIdx)};
357 LOG(info) <<
Form(
" target truth : dX %+.3e, dY %+.3e, dZ %+.3e cm", aTrg(
idX), aTrg(
idY), aTrg(
idZ));
363 TClonesArray trackData(
"CbmStsTrack", 200), hitData(
"CbmStsHit", 200 * model.
Alignables()), beamData(
"BmnHit"),
364 *tdataPtr{&trackData}, *hdataPtr{&hitData}, *bdataPtr{&beamData};
388 std::map<Int_t, std::array<Double_t, 3>> sibtErr;
389 Int_t iTrack{0}, freeWithVertex{0};
400 std::array<Double_t, 3>& err{sibtErr[hit.
GetStation()]};
401 err[0] += hit.GetDx();
402 err[1] += hit.GetDy();
413 const Bool_t hasVertex{FitFree(root, lineX, lineY, vertex, vertexW)};
414 if (lineX.
Det() <= 0.0 || lineY.
Det() <= 0.0) {
415 LOG(error) <<
" <!> BmnAlignTestTask::Make: track #" << iTrack <<
" has too few hits for a straight line";
421 if (vertexTrack[iTrack]) {
422 const SVectGL& aTrg{truth.
A(trgIdx)};
424 auto Vertex = [&](
const SVectLC& a) {
return SVect2(measure.
X(zV, aTrg, a), measure.
Y(zV, aTrg, a)); };
425 const SVect2 vB{vertex(
iX) + vertexNoiseX, vertex(
iY) + vertexNoiseY};
433 for (
Int_t step = 0; step < VERTEX_STEPS; step++) {
436 SVectLC up{alpha}, down{alpha};
439 const SVect2 d{0.5 * (Vertex(up) - Vertex(down))};
446 LOG(error) <<
" <!> BmnAlignTestTask::Make: track #" << iTrack
447 <<
" cannot be put through the vertex";
452 alpha = alphaFree - mCAt * (mM * (mA * (alphaFree - alpha) + Vertex(alpha) - vB));
454 }
else if (hasVertex)
464 const Int_t hitIdx{hitData.GetEntriesFast()}, hitDet{srcHit->GetDetectorID()},
466 const Double_t z0hit{srcHit->GetZ()},
474 measure.
X(z0hit, truth.
A(detIdx), alpha) + (dx ?
gRandom->Gaus(0, dx) : 0.0),
475 measure.
Y(z0hit, truth.
A(detIdx), alpha) + (dy ?
gRandom->Gaus(0, dy) : 0.0), z0hit},
477 new (hitData[hitIdx])
CbmStsHit(hitDet, pos, dpos, 0.0, 0, 0);
478 hitArray[arrIdx++] = hitIdx;
509 for (
const auto& [station, err] : sibtErr)
510 LOG(info) <<
Form(
" SiBT station %d : mean hit errors x %.2e, y %.2e cm", station, err[0] / err[2],
512 LOG_IF(info, withBeam) <<
" " << freeWithVertex
513 <<
" free tracks take a vertex hit in the task:"
514 " the aligner's vertex test should reject about as many";
518 LOG(info) <<
" test data successfully written to " << dataPath;
520 LOG(info) <<
" test solution successfully written to " << solutionPath;
522 LOG(error) <<
" <!> BmnAlignTestTask::Make: failed to save solution to " << solutionPath;
528 LOG(state) <<
Form(
" < BmnAlignTestTask : test task prepared in %.1fs", timer.
RealTime());
std::vector< Double_t > VecDouble_t
std::vector< Bool_t > VecBool_t
RM::SVector< Double_t, 3 > SVect3
std::vector< SVectGL > VecSVectGL_t
RM::SVector< Double_t, BMN_LOCAL_PARAMS_PT > SVectLC
RM::SVector< Double_t, 2 > SVect2
RM::SVector< Double_t, BMN_GLOBAL_PARAMS_PD > SVectGL
std::vector< Int_t > VecInt_t
RM::SMatrix< Double_t, BMN_LOCAL_PARAMS_PT, BMN_LOCAL_PARAMS_PT, RM::MatRepSym< Double_t, BMN_LOCAL_PARAMS_PT > > SMatrLC
#define BMN_LOCAL_PARAMS_PT
#define BMN_GLOBAL_PARAMS_PD
#define BMN_BEAM_BRANCH_NAME
#define BMN_TRACKS_BRANCH_NAME
#define BMN_HITS_BRANCH_NAME
TMatrixTRow_const< Double_t > TMatrixDRow_const
TMatrixT< Double_t > TMatrixD
R__EXTERN TRandom * gRandom
char * Form(const char *fmt,...)
TVectorT< Double_t > TVectorD
Int_t LeafCount() const noexcept
const TMatrixD & J() const noexcept
Int_t CompositeCount() const noexcept
Int_t AllParams() const noexcept
const BmnParamLayout & Layout() const noexcept
Int_t CompositeParams() const noexcept
Bool_t Ready() const noexcept
Int_t CompositeParamBase() const noexcept
Double_t SigmaAt(Int_t full) const noexcept
Int_t ConstCount() const noexcept
SVectGL & A(Int_t index) noexcept
static Bool_t Make(BmnAligner &aligner, const char *dataPath, const char *solutionPath, Double_t sigmaScale=1.0, ENoise noise=kHitErrors, const TVector2 &stdDevXY=TVector2())
BmnSimpleProgressBar * ProgressBar() const noexcept
const BmnDetectorModel * DetectorModel() const noexcept
Bool_t Initialized() const noexcept
const BmnAlignResult & ResultLast() const
BmnRamIterator * RamIterator() const noexcept
Results_t & Results() noexcept
const BmnMeasureModel * MeasureModel() const noexcept
BmnRootIterator * RootIterator() noexcept
Int_t TotalTracks() const noexcept
const BmnAlignPriors & Priors() const noexcept
Bool_t SaveSolution(const char *jsonPath, Bool_t withAlpha=kFALSE)
const BmnAlignHierarchy & Hierarchy() const noexcept
Bool_t FitTargetZ(Double_t &z0, Double_t &z0Err)
virtual Double_t HitX() const =0
virtual Double_t HitY() const =0
virtual Double_t HitZ() const =0
virtual Double_t HitWx() const =0
Bool_t EndOfTracks() const noexcept
virtual Int_t HitDetectorID() const =0
virtual Double_t HitWy() const =0
Bool_t EndOfHits() const noexcept
const Layer & Target() const
Int_t EncodedID(Int_t idx) const
virtual std::string DecodeID(Int_t id) const =0
virtual Int_t Alignables() const =0
virtual Int_t TargetID() const
Int_t Idx(Int_t ID) const
Int_t IdxFromHit(Int_t hitID) const
static constexpr Double_t MIN_HIT_ERROR
Short_t GetStation() const
virtual Double_t Y(Double_t z0, const SVectGL &gl, const SVectLC &lc) const noexcept=0
virtual void AlignHitApproximately(SVect3 &hit, const SVectGL &a) const noexcept=0
virtual Double_t X(Double_t z0, const SVectGL &gl, const SVectLC &lc) const noexcept=0
const Nodes_t & ByNode() const noexcept
static const char * ParamName(Int_t p)
const Range_t & All() const noexcept
virtual void NextHit() override
virtual void ResetAll() override
virtual Int_t HitDetectorID() const override
virtual Double_t HitWy() const override
virtual Double_t HitWx() const override
virtual void NextTrack() override
virtual void ResetAll() override
TChain & GetChain() const noexcept
const Hit_t * GetHit() const noexcept
Bool_t IsNewEvent() const noexcept
const Track_t * GetTrack() const noexcept
Double_t VertexVarY() const noexcept
virtual Int_t HitsInTrack() const override
Bool_t HasVertexHit() const noexcept
virtual void ResetHits() override
virtual void NextTrack() override
Double_t VertexVarX() const noexcept
virtual void NextHit() override
const TClonesArray * GetBeamHits() const
Double_t Det() const noexcept
Double_t At0() const noexcept
Double_t Cov() const noexcept
Double_t Var(Double_t z) const noexcept
Double_t Slope() const noexcept
Double_t SlopeVar() const noexcept
FairTrackParam * GetParamLast()
void SetParamLast(FairTrackParam &par)
void SetStsHits(TArrayI &hitsArr)
void SetStsEv(Int_t evNo)
void SetParamFirst(FairTrackParam &par)
void SetChi2(Double_t chi2)
FairTrackParam * GetParamFirst()
void SetPidHypo(Int_t pid)
Modifiers.
TTree * GetTree() const override
const TVectorD & GetSig()
Bool_t Decompose() override
Int_t Write(const char *name=nullptr, Int_t opt=0, Int_t bufsize=0) const override
void Close(Option_t *option="") override
TMatrixT< Element > & NormByRow(const TVectorT< Element > &v, Option_t *option="D")
TMatrixTBase< Element > & SetSub(Int_t row_lwb, Int_t col_lwb, const TMatrixTBase< Element > &source) override
const char * GetName() const override
const char * GetTitle() const override
Int_t GetEntriesFast() const
TObject * At(Int_t idx) const override
R__ALWAYS_INLINE Bool_t IsZombie() const
virtual void SetDirectory(TDirectory *dir)
virtual Int_t Branch(const char *folder, Int_t bufsize=32000, Int_t splitlevel=99)
Expr< TransposeOp< Expr< A, T, D1, D2, R >, T, D1, D2 >, T, D2, D1, typename TranspPolicy< T, D1, D2, R >::RepType > Transpose(const Expr< A, T, D1, D2, R > &rhs)
T Similarity(const Expr< A, T, D, D, R > &lhs, const SVector< T, D > &rhs)