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