CbmRoot
Loading...
Searching...
No Matches
CbmBbaAlignTask.h
Go to the documentation of this file.
1/* Copyright (C) 2025 Johann Wolfgang Goethe-Universitaet Frankfurt, Frankfurt am Main
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Nora Bluhme [committer], Sergey Gorbunov */
4
9
10#ifndef CbmBbaAlignTask_H
11#define CbmBbaAlignTask_H
12
13#if __has_include(<omp.h>)
14#define HAVE_OMP
15#endif
16
17#include "CbmBbaAlignmentBody.h"
18#include "CbmKfTrackFitter.h"
19#include "FairTask.h"
20#include "TClonesArray.h"
21#include "TFile.h"
22#include "TGeoPhysicalNode.h"
23#include "TH1F.h"
24#include "TString.h"
25#include "bba/Parameter.h"
26
27#include <TMatrixD.h>
28
29#include <cstddef>
30#include <string>
31
32namespace
33{
34 constexpr size_t kNdfBody = cbm::bba::AlignmentBody::NofParameters; // degrees of freedom per alignment body
35}
36
37class CbmBbaAlignTask : public FairTask {
38 public:
39 CbmBbaAlignTask(const char* name = "CbmBbaAlignTask", Int_t iVerbose = 0,
40 TString histoFileName = "./CbmBbaAlignmentHisto.root", size_t nMaxTracks = 1000);
41
42 InitStatus Init();
43 void Exec(Option_t* opt);
44 void Finish();
45
46 // clang-format off
48 {
49 station, // one alignment body per tracking station
50 sensor // one alignment body per sensor
51 };
52 // clang-format on
53
54 class Trajectory : public CbmKfTrackFitter<cbm::algo::kf::DoFitTime::N>::Trajectory {
55 public:
58
59 static int GetSensorId(const Node& node) { return node.userReferences[3]; }
60 static void SetSensorId(Node& node, int sensorId) { node.userReferences[3] = sensorId; }
61 void SetSensorId(size_t inode, int sensorId) { SetSensorId(fNodes[inode], sensorId); }
62
63 static int GetAlignmentBodyId(const Node& node) { return node.userReferences[4]; }
64 static void SetAlignmentBodyId(Node& node, int bodyId) { node.userReferences[4] = bodyId; }
65 void SetAlignmentBodyId(size_t inode, int bodyId) { SetAlignmentBodyId(fNodes[inode], bodyId); }
66 };
67
73 int fNmvdHits{0}; // number of MVD hits
74 int fNstsHits{0}; // number of STS hits
75 int fNmuchHits{0}; // number of MUCH hits
76 int fNtrd1dHits{0}; // number of TRD hits
77 int fNtrd2dHits{0}; // number of TRD hits
78 int fNtofHits{0}; // number of TOF hits
79 bool fIsActive{1}; // is the track active
80 std::vector<std::array<double, 3>> fOriginalHitPositions; // original hit positions
82 void MakeConsistent();
83 };
84
85 struct Sensor { // TGeoNode to be aligned
89 std::string fNodePath{""}; // full path to the node in the root geometry
90
91 bool operator<(const Sensor& other) const
92 {
93 if (fSystemId < other.fSystemId) return true;
94 if (fSystemId > other.fSystemId) return false;
95 if (fTrackingStation < other.fTrackingStation) return true;
96 if (fTrackingStation > other.fTrackingStation) return false;
97 return (fNodePath < other.fNodePath);
98 };
99
100 bool operator==(const Sensor& other) const { return fNodePath == other.fNodePath; };
101 };
102
103 struct Histograms {
104 std::vector<std::unique_ptr<TH1F>> fResidualsX; // residuals of x-coordinates of the hits
105 std::vector<std::unique_ptr<TH1F>> fResidualsY; // residuals of y-coordinates of the hits
106 std::vector<std::unique_ptr<TH1F>> fPullsX; // pulls of x-coordinates of the hits
107 std::vector<std::unique_ptr<TH1F>> fPullsY; // pulls of y-coordinates of the hits
108 };
109
112 void SetRandomSeed(int seed) { fRandomSeed = seed; }
113 int GetRandomSeed() const { return fRandomSeed; }
114 void SetMaxTracks(size_t nMaxTracks) { fNmaxTracks = nMaxTracks; }
115 size_t GetMaxTracks() const { return fNmaxTracks; }
116
117 private:
118 unsigned SelectTracks(TClonesArray* inputTracks, std::vector<TrackContainer>& tracks);
119 std::vector<Sensor> SensorsFromTracks(const std::vector<TrackContainer>& tracks) const;
120 std::vector<cbm::bba::AlignmentBody> CreateAndSetAlignmentBodies(AlignmentMode mode,
121 std::vector<Sensor>& sensors) const;
122 void SetReferences(const std::vector<Sensor>& sensors, std::vector<TrackContainer>& tracks) const;
123
124 std::vector<bba::Parameter> CreateAlignmentParameters() const;
125 std::vector<double> InvertParameters(std::vector<double> par) const;
126 std::vector<double> DiffParameters(const std::vector<double>& par1, const std::vector<double>& par2) const;
127 std::string PrintParameters(const std::vector<double>& par) const;
128 std::string PrintRMSE(const std::vector<bba::Parameter>& par, const std::vector<double>& parMC,
129 const std::vector<double>& parValues) const;
130
131 std::vector<double> SimulateMisalignment(const std::vector<bba::Parameter>& par, std::vector<TrackContainer>& tracks,
132 std::vector<cbm::bba::AlignmentBody>& AlignmentBodies) const;
133
135 void FillHistograms(const std::vector<TrackContainer>& tracks, Histograms& histo);
136 void WriteObjectsCurFile(TObject* obj);
137
138 void SetAlignment(const std::vector<double>& par, std::vector<cbm::bba::AlignmentBody>& AlignmentBodies) const;
139 void ApplyAlignment(const std::vector<double>& par, std::vector<TrackContainer>& tracks,
140 std::vector<cbm::bba::AlignmentBody>& AlignmentBodies, bool invert) const;
141
142 double CostFunction(const std::vector<double>& par, std::vector<TrackContainer>& tracks);
143 double CostFunction(const std::vector<double>& par);
144
145 // Output
146 std::pair<std::string, TGeoHMatrix> CreateAlignmentNode(std::string path, double shiftX, double shiftY, double shiftZ,
147 double rotX, double rotY, double rotZ) const;
148 std::map<std::string, TGeoHMatrix> CreateAlignmentMatrices(const std::vector<double>& par) const;
149 TString fMatrixOutFileName{"AlignmentMatrices.root"};
150
151 std::vector<TrackContainer> fTracks;
152 std::vector<Sensor> fSensors;
153 std::vector<cbm::bba::AlignmentBody> fAlignmentBodies;
154
155 TClonesArray* fInputStsTracks{nullptr};
157
158 int fNtrackingStations{0}; // number of tracking stations
159 size_t fNevents{0}; // number of processed events
160 size_t fNmaxTracks{0}; // maximum number of tracks in the event
161 size_t fNcallsCost{};
162
163 // Simulation settings
165 int fRandomSeed{1}; // random seed for misalignment
166 std::vector<double> fParMC;
167 std::vector<double> fParStart;
168
169 // paramaters for alignment
171
172 double fCostIdeal{1.e10};
173 double fCostInitial{0.};
174
175 // inputs/outputs of the cost function
176 int fNthreads{1};
177 long fFixedNdf{-1};
178
179 // histograms
182
183 TString fHistoFileName{"CbmBbaAlignmentHisto.root"};
184 TFile* fHistoFile{nullptr};
185 TDirectory* fHistoDir{nullptr};
186
187 // make root happy
189};
190#endif
TClonesArray * tracks
Alignment body class for the BBA alignment.
ECbmModuleId
Enumerator for module Identifiers.
Definition CbmDefs.h:45
int Int_t
static int GetAlignmentBodyId(const Node &node)
void SetAlignmentBodyId(size_t inode, int bodyId)
void SetSensorId(size_t inode, int sensorId)
static void SetAlignmentBodyId(Node &node, int bodyId)
static int GetSensorId(const Node &node)
static void SetSensorId(Node &node, int sensorId)
std::vector< double > fParStart
std::vector< cbm::bba::AlignmentBody > CreateAndSetAlignmentBodies(AlignmentMode mode, std::vector< Sensor > &sensors) const
TClonesArray * fInputStsTracks
void ApplyAlignment(const std::vector< double > &par, std::vector< TrackContainer > &tracks, std::vector< cbm::bba::AlignmentBody > &AlignmentBodies, bool invert) const
double CostFunction(const std::vector< double > &par, std::vector< TrackContainer > &tracks)
std::vector< bba::Parameter > CreateAlignmentParameters() const
std::vector< double > fParMC
std::vector< Sensor > fSensors
double GetSimulatedMisalignmentRange() const
int GetRandomSeed() const
unsigned SelectTracks(TClonesArray *inputTracks, std::vector< TrackContainer > &tracks)
void SetAlignment(const std::vector< double > &par, std::vector< cbm::bba::AlignmentBody > &AlignmentBodies) const
std::vector< double > InvertParameters(std::vector< double > par) const
void Exec(Option_t *opt)
AlignmentMode fAlignmentMode
std::vector< double > DiffParameters(const std::vector< double > &par1, const std::vector< double > &par2) const
Histograms fHistoBeforeAlignment
std::string PrintParameters(const std::vector< double > &par) const
void SetReferences(const std::vector< Sensor > &sensors, std::vector< TrackContainer > &tracks) const
ClassDef(CbmBbaAlignTask, 0)
std::vector< double > SimulateMisalignment(const std::vector< bba::Parameter > &par, std::vector< TrackContainer > &tracks, std::vector< cbm::bba::AlignmentBody > &AlignmentBodies) const
void SetRandomSeed(int seed)
std::vector< TrackContainer > fTracks
void SetSimulatedMisalignmentRange(double range)
std::vector< cbm::bba::AlignmentBody > fAlignmentBodies
std::pair< std::string, TGeoHMatrix > CreateAlignmentNode(std::string path, double shiftX, double shiftY, double shiftZ, double rotX, double rotY, double rotZ) const
TDirectory * fHistoDir
CbmKfTrackFitter< cbm::algo::kf::DoFitTime::N > fFitter
double fSimulatedMisalignmentRange
size_t GetMaxTracks() const
CbmBbaAlignTask(const char *name="CbmBbaAlignTask", Int_t iVerbose=0, TString histoFileName="./CbmBbaAlignmentHisto.root", size_t nMaxTracks=1000)
void WriteObjectsCurFile(TObject *obj)
void SetMaxTracks(size_t nMaxTracks)
void FillHistograms(const std::vector< TrackContainer > &tracks, Histograms &histo)
std::map< std::string, TGeoHMatrix > CreateAlignmentMatrices(const std::vector< double > &par) const
Histograms fHistoAfterAlignment
std::string PrintRMSE(const std::vector< bba::Parameter > &par, const std::vector< double > &parMC, const std::vector< double > &parValues) const
std::vector< Sensor > SensorsFromTracks(const std::vector< TrackContainer > &tracks) const
static constexpr int NofParameters
number of alignment parameters
TrackParam< double > TrackParamD
@ N
Do not fit the time component.
Definition KfDefs.h:133
std::vector< std::unique_ptr< TH1F > > fResidualsY
std::vector< std::unique_ptr< TH1F > > fPullsY
std::vector< std::unique_ptr< TH1F > > fResidualsX
std::vector< std::unique_ptr< TH1F > > fPullsX
bool operator<(const Sensor &other) const
bool operator==(const Sensor &other) const
cbm::algo::kf::TrackParamD fFittedTrackParam
std::vector< std::array< double, 3 > > fOriginalHitPositions