CbmRoot
Loading...
Searching...
No Matches
CbmStsTracksConverter.cxx
Go to the documentation of this file.
1/* Copyright (C) 2020-2021 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Viktor Klochkov [committer] */
4
6
7#include "CbmDefs.h"
8#include "CbmEvent.h"
9#include "CbmL1PFFitter.h"
10#include "CbmMCDataManager.h"
11#include "CbmMCTrack.h"
12#include "CbmStsTrack.h"
13#include "CbmTrackMatchNew.h"
14#include "CbmVertex.h"
16
17#include "FairRootManager.h"
18
19#include "TClonesArray.h"
20
21#include <AnalysisTree/TaskManager.hpp>
22#include <cassert>
23
24#include <cmath>
25
26#include "AnalysisTree/Matching.hpp"
27
28
30
32{
33 assert(cbm_sts_tracks_ != nullptr);
34
35 vtx_tracks_2_sim_->Clear();
36 out_indexes_map_.clear();
37
38 ReadVertexTracks(event);
39 MapTracks(event);
40}
41
47
49{
50 auto* ioman = FairRootManager::Instance();
51
52 cbm_sts_tracks_ = (TClonesArray*) ioman->GetObject("StsTrack");
53 cbm_sts_match_ = (TClonesArray*) ioman->GetObject("StsTrackMatch");
54
55 cbm_mc_manager_ = dynamic_cast<CbmMCDataManager*>(ioman->GetObject("MCDataManager"));
56}
57
59{
60 InitInput();
61
62 AnalysisTree::BranchConfig vtx_tracks_config(out_branch_, AnalysisTree::DetType::kTrack);
63 vtx_tracks_config.AddField<float>("chi2", "spatial chi2 of the track fit");
64 vtx_tracks_config.AddField<float>("chi2_time", "time chi2 of the track fit");
65 vtx_tracks_config.AddField<float>("vtx_chi2", "chi2 to to the primary vertex");
66 vtx_tracks_config.AddFields<float>({"dcax", "dcay", "dcaz"},
67 "not actuall Distance of Closest Approach, but extrapolated to z=z_vtx");
68 vtx_tracks_config.AddField<int>("ndf", "spatial number degrees of freedom");
69 vtx_tracks_config.AddField<int>("ndf_time", "time number degrees of freedom");
70 vtx_tracks_config.AddField<int>("q", "charge");
71 vtx_tracks_config.AddField<int>("nhits", "number of hits (total MVD+STS)");
72 vtx_tracks_config.AddField<int>("nhits_mvd", "number of hits in MVD");
73 vtx_tracks_config.AddField<float>("match_weight", "true over all hits ratio for a matched MC-track");
74 vtx_tracks_config.AddField<float>("dE_over_dx");
75
76 iq_ = vtx_tracks_config.GetFieldId("q");
77 indf_ = vtx_tracks_config.GetFieldId("ndf");
78 indf_time_ = vtx_tracks_config.GetFieldId("ndf_time");
79 ichi2_ = vtx_tracks_config.GetFieldId("chi2");
80 ichi2_time_ = vtx_tracks_config.GetFieldId("chi2_time");
81 inhits_ = vtx_tracks_config.GetFieldId("nhits");
82 inhits_mvd_ = vtx_tracks_config.GetFieldId("nhits_mvd");
83 idcax_ = vtx_tracks_config.GetFieldId("dcax");
84 ivtx_chi2_ = vtx_tracks_config.GetFieldId("vtx_chi2");
85 ide_dx_ = vtx_tracks_config.GetFieldId("dE_over_dx");
86 imatch_weight_ = vtx_tracks_config.GetFieldId("match_weight");
87
88 if (is_write_kfinfo_) {
89 vtx_tracks_config.AddFields<float>({"x", "y", "z", "tx", "ty", "qp"}, "track parameters");
90 vtx_tracks_config.AddFields<float>({"cx0", "cx1", "cx2", "cy0", "cy1", "cy2", "cz0", "cz1", "cz2", "z0"},
91 "magnetic field approximation");
92 vtx_tracks_config.AddFields<float>({"cov1", "cov2", "cov3", "cov4", "cov5", "cov6", "cov7", "cov8", "cov9", "cov10",
93 "cov11", "cov12", "cov13", "cov14", "cov15"},
94 "covarience matrix");
95
96 vtx_tracks_config.AddField<int>("mother_pdg", "PDG code of mother particle");
97 vtx_tracks_config.AddField<bool>("pass_cuts", "ask Oleksii");
98
99 ipar_ = vtx_tracks_config.GetFieldId("x");
100 imf_ = vtx_tracks_config.GetFieldId("cx0");
101 icov_ = vtx_tracks_config.GetFieldId("cov1");
102 imother_pdg_ = vtx_tracks_config.GetFieldId("mother_pdg");
103 ipasscuts_ = vtx_tracks_config.GetFieldId("pass_cuts");
104 }
105 auto* man = AnalysisTree::TaskManager::GetInstance();
106 man->AddBranch(vtx_tracks_, vtx_tracks_config);
107 man->AddMatching(out_branch_, match_to_, vtx_tracks_2_sim_);
108}
109
110// TODO misleading name, move field filling somewhere else?
111float CbmStsTracksConverter::ExtrapolateToVertex(CbmStsTrack* sts_track, AnalysisTree::Track& track, int pdg)
112{
113 std::vector<CbmStsTrack> tracks = {*sts_track};
114 CbmL1PFFitter fitter;
115 std::vector<float> chi2_to_vtx;
116 std::vector<CbmL1PFFitter::PFFieldRegion> field;
119 std::vector<int> pdgVector = {pdg};
120 fitter.Fit(tracks, pdgVector);
121 }
122 fitter.GetChiToVertex(tracks, field, chi2_to_vtx, kfVertex, 3.);
123 *sts_track = tracks[0];
124
125 if (is_write_kfinfo_) {
126 for (int i = 0; i < 10; i++) {
127 track.SetField(field[0].fP[i], imf_ + i);
128 }
129 }
130
131 return chi2_to_vtx[0];
132}
133
135{
136 if (event) {
137 cbm_prim_vertex_ = event->GetVertex();
138 }
140
141 vtx_tracks_->ClearChannels();
142 auto* out_config_ = AnalysisTree::TaskManager::GetInstance()->GetConfig();
143 const auto& branch = out_config_->GetBranchConfig(out_branch_);
144
145 const int n_sts_tracks = event ? event->GetNofStsTracks() : cbm_sts_tracks_->GetEntries();
146 if (n_sts_tracks <= 0) {
147 LOG(warn) << "No STS tracks!";
148 return;
149 }
150 vtx_tracks_->Reserve(n_sts_tracks);
151
152 for (short i_track = 0; i_track < n_sts_tracks; ++i_track) {
153 const int track_index = event ? event->GetStsTrackIndex(i_track) : i_track;
154 auto* sts_track = (CbmStsTrack*) cbm_sts_tracks_->At(track_index);
155 if (!sts_track) { throw std::runtime_error("empty track!"); }
156
157 auto& track = vtx_tracks_->AddChannel(branch);
158
159 // int pdg = GetMcPid((CbmTrackMatchNew*) cbm_sts_match_->At(track_index), track);
160 bool is_good_track = IsGoodCovMatrix(sts_track);
161
162 float chi2_vertex = ExtrapolateToVertex(sts_track, track, 211);
163 const FairTrackParam* trackParamFirst = sts_track->GetParamFirst();
164 TVector3 momRec;
165 trackParamFirst->Momentum(momRec);
166 const Int_t q = trackParamFirst->GetQp() > 0 ? 1 : -1;
167
168 track.SetMomentum3(momRec);
169 track.SetField(int(q), iq_);
170 track.SetField(int(sts_track->GetNDF()), indf_);
171 track.SetField(int(sts_track->GetNdfTime()), indf_time_);
172 track.SetField(float(sts_track->GetChiSq()), ichi2_);
173 track.SetField(float(sts_track->GetChiSqTime()), ichi2_time_);
174 track.SetField(int(sts_track->GetTotalNofHits()), inhits_);
175 track.SetField(float(trackParamFirst->GetX() - cbm_prim_vertex_->GetX()), idcax_);
176 track.SetField(float(trackParamFirst->GetY() - cbm_prim_vertex_->GetY()), idcax_ + 1);
177 track.SetField(float(trackParamFirst->GetZ() - cbm_prim_vertex_->GetZ()), idcax_ + 2);
178 track.SetField(int(sts_track->GetNofMvdHits()), inhits_mvd_);
179 track.SetField(float(chi2_vertex), ivtx_chi2_);
180 track.SetField(float(sts_track->GetELoss()), ide_dx_);
181
182 out_indexes_map_.insert(std::make_pair(track_index, track.GetId()));
183
184 if (is_write_kfinfo_) { WriteKFInfo(track, sts_track, is_good_track); }
185 }
186}
187
188void CbmStsTracksConverter::WriteKFInfo(AnalysisTree::Track& track, const CbmStsTrack* sts_track,
189 bool is_good_track) const
190{
191 assert(sts_track);
192 const FairTrackParam* trackParamFirst = sts_track->GetParamFirst();
193
194 track.SetField(float(trackParamFirst->GetX()), ipar_);
195 track.SetField(float(trackParamFirst->GetY()), ipar_ + 1);
196 track.SetField(float(trackParamFirst->GetZ()), ipar_ + 2);
197 track.SetField(float(trackParamFirst->GetTx()), ipar_ + 3);
198 track.SetField(float(trackParamFirst->GetTy()), ipar_ + 4);
199 track.SetField(float(trackParamFirst->GetQp()), ipar_ + 5);
200
201 for (Int_t i = 0, iCov = 0; i < 5; i++) {
202 for (Int_t j = 0; j <= i; j++, iCov++) {
203 track.SetField(float(trackParamFirst->GetCovariance(i, j)), icov_ + iCov);
204 }
205 }
206 track.SetField(is_good_track, ipasscuts_);
207}
208
210{
211 if (!is_reproduce_cbmkfpf_) return true;
212
213 assert(sts_track);
214 const FairTrackParam* trackParamFirst = sts_track->GetParamFirst();
215
216 Double_t cov_matrix[15] = {0.f};
217 for (Int_t i = 0, iCov = 0; i < 5; i++) {
218 for (Int_t j = 0; j <= i; j++, iCov++) {
219 cov_matrix[iCov] = trackParamFirst->GetCovariance(i, j);
220 }
221 }
222 // Cuts, coded in MZ's CbmKFParticleFinder.cxx
223 bool ok = true;
224 ok = ok && std::isfinite(sts_track->GetParamFirst()->GetX());
225 ok = ok && std::isfinite(sts_track->GetParamFirst()->GetY());
226 ok = ok && std::isfinite(sts_track->GetParamFirst()->GetZ());
227 ok = ok && std::isfinite(sts_track->GetParamFirst()->GetTx());
228 ok = ok && std::isfinite(sts_track->GetParamFirst()->GetTy());
229 ok = ok && std::isfinite(sts_track->GetParamFirst()->GetQp());
230
231 for (auto element : cov_matrix) {
232 ok = ok && std::isfinite(element);
233 }
234 ok = ok && (cov_matrix[0] < 1. && cov_matrix[0] > 0.) && (cov_matrix[2] < 1. && cov_matrix[2] > 0.)
235 && (cov_matrix[5] < 1. && cov_matrix[5] > 0.) && (cov_matrix[9] < 1. && cov_matrix[9] > 0.)
236 && (cov_matrix[14] < 1. && cov_matrix[14] > 0.);
237
238 ok = ok && sts_track->GetChiSq() < 10. * sts_track->GetNDF();
239
240 return ok;
241}
242
244{
245 const auto it = indexes_map_->find(match_to_);
246 if (it == indexes_map_->end()) { throw std::runtime_error(match_to_ + " is not found to match with vertex tracks"); }
247 auto sim_tracks_map = it->second;
248
249 if (out_indexes_map_.empty() || sim_tracks_map.empty()) return;
250 CbmTrackMatchNew* match {nullptr};
251
252 int file_id {0}, event_id {0};
253 if (event && event->GetMatch() && event->GetMatch()->GetNofLinks() > 0) {
254 file_id = event->GetMatch()->GetMatchedLink().GetFile();
255 event_id = event->GetMatch()->GetMatchedLink().GetEntry();
256 }
257 else {
258 event_id = FairRootManager::Instance()->GetEntryNr();
259 }
260
261 for (const auto& track_id : out_indexes_map_) {
262 const int cbm_id = track_id.first;
263 const int out_id = track_id.second;
264 auto& track = vtx_tracks_->Channel(out_id);
265
266 match = (CbmTrackMatchNew*) cbm_sts_match_->At(cbm_id);
267 if (match->GetNofLinks() > 0) { // there is at least one matched MC-track
268
269 const auto& link = match->GetMatchedLink();
270
271 if (link.GetFile() != file_id || link.GetEntry() != event_id) { // match from different event
272 LOG(warn) << "MC track from different event";
273 continue;
274 }
275
276 const int mc_track_id = link.GetIndex();
277
278 auto p = sim_tracks_map.find(mc_track_id);
279 if (p == sim_tracks_map.end()) // match is not found
280 continue;
281
282 track.SetField(float(match->GetTrueOverAllHitsRatio()), imatch_weight_);
283 vtx_tracks_2_sim_->AddMatch(out_id, p->second);
284 }
285 }
286}
TClonesArray * tracks
Data class for STS tracks.
ClassImp(CbmStsTracksConverter)
MapType out_indexes_map_
CbmRoot to AnalysisTree indexes map for output branch.
std::string match_to_
AT branch to match.
std::string out_branch_
std::map< std::string, MapType > * indexes_map_
from other tasks
Class characterising one event by a collection of links (indices) to data objects,...
Definition CbmEvent.h:34
CbmMatch * GetMatch() const
Definition CbmEvent.h:98
void GetChiToVertex(std::vector< CbmStsTrack > &Tracks, std::vector< PFFieldRegion > &field, std::vector< float > &chiToVtx, CbmKFVertex &primVtx, float chiPrim=-1)
void Fit(std::vector< CbmStsTrack > &Tracks, const std::vector< CbmMvdHit > &vMvdHits, const std::vector< CbmStsHit > &vStsHits, const std::vector< int > &pidHypo)
Task class creating and managing CbmMCDataArray objects.
int32_t GetNofLinks() const
Definition CbmMatch.h:42
void ProcessData(CbmEvent *event) final
TClonesArray * cbm_sts_match_
non-owning pointer
bool IsGoodCovMatrix(const CbmStsTrack *sts_track) const
void WriteKFInfo(AnalysisTree::Track &track, const CbmStsTrack *sts_track, bool is_good_track) const
AnalysisTree::Matching * vtx_tracks_2_sim_
raw pointers are needed for TTree::Branch
CbmVertex * cbm_prim_vertex_
non-owning pointer
void MapTracks(CbmEvent *event)
AnalysisTree::TrackDetector * vtx_tracks_
raw pointers are needed for TTree::Branch
TClonesArray * cbm_sts_tracks_
non-owning pointer
void ReadVertexTracks(CbmEvent *event)
CbmMCDataManager * cbm_mc_manager_
non-owning pointer
float ExtrapolateToVertex(CbmStsTrack *sts_track, AnalysisTree::Track &track, int pdg)
int32_t GetNDF() const
Definition CbmTrack.h:64
const FairTrackParam * GetParamFirst() const
Definition CbmTrack.h:68
double GetChiSq() const
Definition CbmTrack.h:63
double GetZ() const
Definition CbmVertex.h:69
double GetY() const
Definition CbmVertex.h:68
double GetX() const
Definition CbmVertex.h:67