CbmRoot
Loading...
Searching...
No Matches
ATKFParticleFinder.cxx
Go to the documentation of this file.
1/* Copyright (C) 2020-2021 GSI, IKF-UFra
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Oleksii Lubynets [committer] */
4
6
7#include "AnalysisTree/Matching.hpp"
8#include "KFParticleTopoReconstructor.h"
9
10void ATKFParticleFinder::InitInput(const std::string& file_name, const std::string& tree_name)
11{
12 std::cout << "ATKFParticleFinder::InitInput()\n";
13
14 in_chain_ = new AnalysisTree::Chain(file_name.c_str(), tree_name.c_str());
15
16 if (in_chain_->CheckBranchExistence("VtxTracks") == 1) { in_chain_->SetBranchAddress("VtxTracks", &kf_tracks_); }
17 else if (in_chain_->CheckBranchExistence("VtxTracks") == 2) {
18 in_chain_->SetBranchAddress("VtxTracks.", &kf_tracks_);
19 }
20 if (in_chain_->CheckBranchExistence("RecEventHeader") == 1) {
21 in_chain_->SetBranchAddress("RecEventHeader", &rec_event_header_);
22 }
23 else if (in_chain_->CheckBranchExistence("RecEventHeader") == 2) {
24 in_chain_->SetBranchAddress("RecEventHeader.", &rec_event_header_);
25 }
26 if (in_chain_->CheckBranchExistence("SimParticles") == 1) {
27 in_chain_->SetBranchAddress("SimParticles", &sim_particles_);
28 }
29 else if (in_chain_->CheckBranchExistence("SimParticles") == 2) {
30 in_chain_->SetBranchAddress("SimParticles.", &sim_particles_);
31 }
32 std::string kf2sim_tracks_name = in_chain_->GetConfiguration()->GetMatchName("VtxTracks", "SimParticles");
33 if (in_chain_->CheckBranchExistence(kf2sim_tracks_name) == 1) {
34 in_chain_->SetBranchAddress(kf2sim_tracks_name.c_str(), &kf2sim_tracks_);
35 }
36 else if (in_chain_->CheckBranchExistence(kf2sim_tracks_name) == 2) {
37 in_chain_->SetBranchAddress((kf2sim_tracks_name + ".").c_str(), &kf2sim_tracks_);
38 }
39
40 auto branch_conf_kftr = in_chain_->GetConfiguration()->GetBranchConfig("VtxTracks");
41 q_field_id_ = branch_conf_kftr.GetFieldId("q");
42
43 par_field_id_ = branch_conf_kftr.GetFieldId("x"); // par0
44 mf_field_id_ = branch_conf_kftr.GetFieldId("cx0"); // magnetic field par0
45 cov_field_id_ = branch_conf_kftr.GetFieldId("cov1"); // cov matrix 0
46
47 passcuts_field_id_ = branch_conf_kftr.GetFieldId("pass_cuts");
48 nhits_field_id_ = branch_conf_kftr.GetFieldId("nhits");
49 nhits_mvd_field_id_ = branch_conf_kftr.GetFieldId("nhits_mvd");
50 vtx_chi2_field_id_ = branch_conf_kftr.GetFieldId("vtx_chi2");
51
52 topo_reconstructor_ = new KFParticleTopoReconstructor;
53 // cuts setting
54 topo_reconstructor_->GetKFParticleFinder()->SetChiPrimaryCut2D(cuts_.GetCutChi2Prim());
55 topo_reconstructor_->GetKFParticleFinder()->SetMaxDistanceBetweenParticlesCut(cuts_.GetCutDistance());
56 topo_reconstructor_->GetKFParticleFinder()->SetChi2Cut2D(cuts_.GetCutChi2Geo());
57 topo_reconstructor_->GetKFParticleFinder()->SetLCut(cuts_.GetCutLDown());
58 topo_reconstructor_->GetKFParticleFinder()->SetLdLCut2D(cuts_.GetCutLdL());
59}
60
61void ATKFParticleFinder::InitOutput(const std::string& file_name)
62{
63 std::cout << "ATKFParticleFinder::InitOutput()\n";
64
65 out_file_ = TFile::Open(file_name.c_str(), "recreate");
66
67 AnalysisTree::BranchConfig ParticlesRecoBranch("ParticlesReconstructed", AnalysisTree::DetType::kParticle);
68
69 ParticlesRecoBranch.AddField<int>("daughter1id");
70 ParticlesRecoBranch.AddField<int>("daughter2id");
71
72 out_config_.AddBranchConfig(ParticlesRecoBranch);
73 particles_reco_ = new AnalysisTree::Particles(out_config_.GetBranchConfig("ParticlesReconstructed").GetId());
74
75 out_tree_ = new TTree("aTree", "AnalysisTree ParticlesReco");
76 out_tree_->Branch("ParticlesReconstructed", "AnalysisTree::Particles", &particles_reco_);
77 out_tree_->SetAutoSave(0);
78 out_config_.Write("Configuration");
79
80 daughter1_id_field_id_ = out_config_.GetBranchConfig(particles_reco_->GetId()).GetFieldId("daughter1id");
81 daughter2_id_field_id_ = out_config_.GetBranchConfig(particles_reco_->GetId()).GetFieldId("daughter2id");
82}
83
85{
86 std::cout << "ATKFParticleFinder::Finish()\n";
87
89 out_tree_->Write();
90 out_file_->Close();
91}
92
93void ATKFParticleFinder::Run(int n_events)
94{
95 if (n_events < 0 || n_events > in_chain_->GetEntries()) n_events = in_chain_->GetEntries();
96
97 std::cout << "ATKFParticleFinder::Run() " << n_events << " events\n";
98
99 for (int i_event = 0; i_event < n_events; i_event++) {
100 std::cout << "eveNo = " << i_event << "\n";
101 in_chain_->GetEntry(i_event);
103
104 // const KFPTrackVector* tv = eventTopoReconstructor->GetTracks();
105 // KFPTrackVector tvv = *tv;
106 // tvv.Print();
107
108 topo_reconstructor_->SortTracks();
109 topo_reconstructor_->ReconstructParticles();
110
112 }
113 Finish();
114}
115
117{
118 //
119 // Initializes KFParticleTopoReconstructor
120 // with all necessary input information (tracks and PV) of the current events
121 // in order to perform particle selection using
122 // non-simplified "standard" KFParticle algorithm.
123 //
124
125 topo_reconstructor_->Clear();
126
127 int n_good_tracks = 0;
128
129 for (unsigned int i_track = 0; i_track < kf_tracks_->GetNumberOfChannels(); i_track++) {
130 const AnalysisTree::Track& rec_track = kf_tracks_->GetChannel(i_track);
131 if (rec_track.GetField<bool>(passcuts_field_id_) == 0) continue;
132 n_good_tracks++;
133 }
134
135 KFPTrackVector track_vector1, track_vector2;
136 track_vector1.Resize(n_good_tracks);
137
138 int j_track = 0;
139
140 for (unsigned int i_track = 0; i_track < kf_tracks_->GetNumberOfChannels(); i_track++) {
141 const AnalysisTree::Track& rec_track = kf_tracks_->GetChannel(i_track);
142
143 if (rec_track.GetField<bool>(passcuts_field_id_) == 0) continue;
144
145 for (Int_t iP = 0; iP < 3; iP++)
146 track_vector1.SetParameter(rec_track.GetField<float>(par_field_id_ + iP), iP, j_track);
147 track_vector1.SetParameter(rec_track.GetPx(), 3, j_track);
148 track_vector1.SetParameter(rec_track.GetPy(), 4, j_track);
149 track_vector1.SetParameter(rec_track.GetPz(), 5, j_track);
150 auto cov_matrix = GetCovMatrixCbm(rec_track);
151 for (Int_t iC = 0; iC < 21; iC++)
152 track_vector1.SetCovariance(cov_matrix.at(iC), iC, j_track);
153 for (Int_t iF = 0; iF < 10; iF++)
154 track_vector1.SetFieldCoefficient(rec_track.GetField<float>(mf_field_id_ + iF), iF, j_track);
155 if (pid_mode_ == 0) track_vector1.SetPDG(-1, j_track);
156 else if (pid_mode_ == 1) {
157 int pdg {-999};
158 const int sim_id = kf2sim_tracks_->GetMatch(rec_track.GetId());
159 if (sim_id > 0) pdg = sim_particles_->GetChannel(sim_id).GetPid();
160 track_vector1.SetPDG(pdg, j_track);
161 }
162 track_vector1.SetQ(rec_track.GetField<int>(q_field_id_), j_track);
163 if (rec_track.GetField<float>(vtx_chi2_field_id_) < 3.) track_vector1.SetPVIndex(0, j_track);
164 else
165 track_vector1.SetPVIndex(-1, j_track);
166 track_vector1.SetNPixelHits(rec_track.GetField<int>(nhits_mvd_field_id_), j_track);
167 track_vector1.SetId(rec_track.GetId(), j_track);
168 j_track++;
169 }
170 topo_reconstructor_->Init(track_vector1, track_vector2);
171
172 KFPVertex primVtx_tmp;
173 primVtx_tmp.SetXYZ(rec_event_header_->GetVertexX(), rec_event_header_->GetVertexY(), rec_event_header_->GetVertexZ());
174 primVtx_tmp.SetCovarianceMatrix(0, 0, 0, 0, 0, 0);
175 primVtx_tmp.SetNContributors(0);
176 primVtx_tmp.SetChi2(-100);
177 std::vector<int> pvTrackIds;
178 KFVertex pv(primVtx_tmp);
179 topo_reconstructor_->AddPV(pv, pvTrackIds);
180
181 std::cout << track_vector1.Size() << "\n";
182
183 // return TR;
184}
185
186void ATKFParticleFinder::WriteCandidates(const KFParticleTopoReconstructor* eventTR)
187{
188 particles_reco_->ClearChannels();
189
190 for (const auto& particle : eventTR->GetParticles()) {
191 auto* particlerec = particles_reco_->AddChannel();
192 particlerec->Init(out_config_.GetBranchConfig(particles_reco_->GetId()));
193
194 float mass, masserr;
195 particle.GetMass(mass, masserr);
196 particlerec->SetMass(mass);
197 particlerec->SetField(particle.DaughterIds()[0], daughter1_id_field_id_);
198 particlerec->SetField(particle.DaughterIds()[1], daughter2_id_field_id_);
199 particlerec->SetMomentum(particle.GetPx(), particle.GetPy(), particle.GetPz());
200 particlerec->SetPid(particle.GetPDG());
201 }
202 out_tree_->Fill();
203}
204
205std::vector<float> ATKFParticleFinder::GetCovMatrixCbm(const AnalysisTree::Track& track) const
206{
207 const double tx = track.GetField<float>(par_field_id_ + 3);
208 const double ty = track.GetField<float>(par_field_id_ + 4);
209 const double qp = track.GetField<float>(par_field_id_ + 5);
210 const Int_t q = track.GetField<int>(q_field_id_);
211
212 //calculate covariance matrix
213 const double t = sqrt(1.f + tx * tx + ty * ty);
214 const double t3 = t * t * t;
215 const double dpxdtx = q / qp * (1.f + ty * ty) / t3;
216 const double dpxdty = -q / qp * tx * ty / t3;
217 const double dpxdqp = -q / (qp * qp) * tx / t;
218 const double dpydtx = -q / qp * tx * ty / t3;
219 const double dpydty = q / qp * (1.f + tx * tx) / t3;
220 const double dpydqp = -q / (qp * qp) * ty / t;
221 const double dpzdtx = -q / qp * tx / t3;
222 const double dpzdty = -q / qp * ty / t3;
223 const double dpzdqp = -q / (qp * qp * t3);
224
225 const double F[6][5] = {{1.f, 0.f, 0.f, 0.f, 0.f}, {0.f, 1.f, 0.f, 0.f, 0.f},
226 {0.f, 0.f, 0.f, 0.f, 0.f}, {0.f, 0.f, dpxdtx, dpxdty, dpxdqp},
227 {0.f, 0.f, dpydtx, dpydty, dpydqp}, {0.f, 0.f, dpzdtx, dpzdty, dpzdqp}};
228
229 double VFT[5][6];
230 for (int i = 0; i < 5; i++)
231 for (int j = 0; j < 6; j++) {
232 VFT[i][j] = 0;
233 for (int k = 0; k < 5; k++) {
234 if (k <= i)
235 VFT[i][j] += track.GetField<float>(cov_field_id_ + k + i * (i + 1) / 2)
236 * F[j][k]; //parameters->GetCovariance(i,k) * F[j][k];
237 else
238 VFT[i][j] += track.GetField<float>(cov_field_id_ + i + k * (k + 1) / 2)
239 * F[j][k]; //parameters->GetCovariance(i,k) * F[j][k];
240 }
241 }
242
243
244 std::vector<float> cov(21, 0);
245 for (int i = 0, l = 0; i < 6; i++)
246 for (int j = 0; j <= i; j++, l++) {
247 cov[l] = 0;
248 for (int k = 0; k < 5; k++) {
249 cov[l] += F[i][k] * VFT[k][j];
250 }
251 }
252 return cov;
253}
friend fvec sqrt(const fvec &a)
void Run(int n_events=-1)
AnalysisTree::Particles * sim_particles_
AnalysisTree::TrackDetector * kf_tracks_
AnalysisTree::Configuration out_config_
std::vector< float > GetCovMatrixCbm(const AnalysisTree::Track &track) const
void InitInput(const std::string &file_name, const std::string &tree_name)
AnalysisTree::Chain * in_chain_
void InitOutput(const std::string &file_name="KFPF_output.root")
AnalysisTree::EventHeader * rec_event_header_
AnalysisTree::Matching * kf2sim_tracks_
KFParticleTopoReconstructor * topo_reconstructor_
AnalysisTree::Particles * particles_reco_
void WriteCandidates(const KFParticleTopoReconstructor *TR)
float GetCutChi2Geo() const
float GetCutLdL() const
float GetCutChi2Prim() const
float GetCutLDown() const
float GetCutDistance() const