8 #include "TClonesArray.h"
9 #include "TGeoManager.h"
12 #include "FairRootManager.h"
14 #include "FairRuntimeDb.h"
20 #include "TStopwatch.h"
26 : FairTask(
"LMD Track Finding Task (Cellular Automation)") {
44 int innSensPP,
int innP,
47 "LMD Track Finding Task (Cellular Automation) with/without <<missing "
48 "planes>> algoritm") {
70 InitStatus stat = kERROR;
77 std::vector<std::vector<std::pair<Int_t, bool> > > &hitsd,
82 for (Int_t iHit = 0; iHit < nStripHits; iHit++) {
86 Int_t planeid = floor(
88 hitsd.at(planeid).push_back(make_pair(iHit,
false));
91 for (Int_t iPlane = 0; iPlane <
nP; iPlane++) {
92 if (hitsd.at(iPlane).size() > 0) nPlanes++;
97 cout <<
"Hits: " << nStripHits <<
" in " << nPlanes <<
" plane(s)." << endl;
98 for (Int_t idet = 0; idet <
nP; idet++)
99 cout <<
"Plane: " << idet <<
" DiscHits: " << hitsd.at(idet).size()
103 if (nPlanes > 2)
return true;
111 std::vector<std::vector<Int_t> > &hitsd, Int_t nStripHits) {
115 for (Int_t iHit = 0; iHit < nStripHits; iHit++) {
118 Int_t planeid = floor(
120 hitsd.at(planeid).push_back(iHit);
123 for (Int_t iPlane = 0; iPlane <
nP; iPlane++) {
124 if (hitsd.at(iPlane).size() > 0) nPlanes++;
128 cout <<
"Hits: " << nStripHits <<
" in " << nPlanes <<
" plane(s)." << endl;
129 for (Int_t idet = 0; idet <
nP; idet++)
130 cout <<
"Plane: " << idet <<
" DiscHits: " << hitsd.at(idet).size()
134 if (nPlanes > 2)
return true;
142 std::vector<std::vector<Int_t> > &hitsd, Int_t nStripHits) {
147 for (Int_t iHit = 0; iHit < nStripHits; iHit++) {
152 lmd_helper.getHitLocationInfo(sensid);
154 int virtplane = hit_info.
plane;
157 hitsd.at(virtplane).push_back(iHit);
160 for (Int_t iPlane = 0; iPlane <
nP; iPlane++) {
161 if (hitsd.at(iPlane).size() > 0) nPlanes++;
165 cout <<
"Hits: " << nStripHits <<
" in " << nPlanes <<
" plane(s)." << endl;
166 for (Int_t idet = 0; idet <
nP; idet++)
167 cout <<
"Plane: " << idet <<
" DiscHits: " << hitsd.at(idet).size()
171 if (nPlanes > 2)
return true;
179 std::vector<std::vector<std::pair<Int_t, bool> > > &hitsd,
181 std::vector<Double_t> detZ;
184 for (Int_t iHit = 0; iHit < nStripHits; iHit++) {
187 for (
unsigned int idet = 0; idet < detZ.size(); idet++) {
188 if (
fabs(tmp - detZ.at(idet)) <
198 for (
int idet = detZ.size() - 1; idet >= 0; idet--) {
199 if (tmp < detZ.at(idet)) pos = idet;
205 for (
unsigned int i = pos + 1;
i < detZ.size();
i++) {
215 for (Int_t iHit = 0; iHit < nStripHits; iHit++) {
219 for (
unsigned int idet = 0; idet < detZ.size(); idet++) {
221 if (
fabs(z - detZ.at(idet)) < 9.) {
222 hitsd.at(idet).push_back(make_pair(iHit,
false));
228 cout <<
"Hits: " << nStripHits <<
" in " << detZ.size() <<
" plane(s)."
230 for (
unsigned int idet = 0; idet < detZ.size(); idet++)
231 cout <<
"Plane: " << idet <<
" DiscHits: " << hitsd.at(idet).size()
235 if (detZ.size() > 3)
return true;
242 FairRootManager *ioman = FairRootManager::Instance();
245 std::cout <<
"-E- PndLmdTrackFinderCATask::Init: "
246 <<
"RootManager not instantiated!" << std::endl;
253 std::cout <<
"-W- PndLmdTrackFinderCATask::Init: "
254 <<
"No fStripHitArray!" << std::endl;
263 std::cout <<
"-I- PndLmdTrackFinderCATask: Initialisation successfull"
266 std::cout <<
"-I- PndLmdTrackFinderCATask: missing plane(s) algorithm will "
284 TVector3 A(hit01->GetX() - hit00->GetX(), hit01->GetY() - hit00->GetY(),
285 hit01->GetZ() - hit00->GetZ());
286 TVector3 B(hit11->GetX() - hit01->GetX(), hit11->GetY() - hit01->GetY(),
287 hit11->GetZ() - hit01->GetZ());
288 double ScalAB = A.Dot(B);
289 double cosPsi = ScalAB / (A.Mag() * B.Mag());
290 if ((1 - cosPsi) <
rule_max)
return true;
305 TVector3 A(hit01->GetX() - hit00->GetX(), hit01->GetY() - hit00->GetY(),
306 hit01->GetZ() - hit00->GetZ());
307 TVector3 B(hit11->GetX() - hit01->GetX(), hit11->GetY() - hit01->GetY(),
308 hit11->GetZ() - hit01->GetZ());
309 double cosPsi = (A.Dot(B)) / (A.Mag() * B.Mag());
310 if ((1 - cosPsi) <
rule_max)
return true;
322 int &pv0,
int &pv1,
int &pv0_n,
int &pv1_n,
332 if (
fVerbose > 2) cout <<
"pv 0 = " << pv0_n <<
" pv 1 = " << pv1_n << endl;
337 TClonesArray *tCellArray,
int niter) {
340 std::vector<int> pv_new;
341 Int_t nCells = tCellArray->GetEntriesFast();
342 for (
int icv = 0; icv < nCells; icv++) pv_new.push_back(-1);
343 bool stop_itter =
true;
346 TStopwatch *timer_neighbors_itter =
new TStopwatch();
347 for (
int itt = 0; itt <
niter;
353 if (
fVerbose > 0) timer_neighbors_itter->Start();
354 nCells = tCellArray->GetEntriesFast();
356 cout <<
" === ITTER === " << itt <<
" with " << nCells <<
" cells"
358 for (
int ic = 0; ic < nCells; ic++) {
360 for (
int jc = ic + 1; jc < nCells; jc++) {
363 bool isNeighbor =
Neighbor(cell0, cell1);
370 if ((cell1->
GetPV()) < 0) pv_new[ic] = 0;
371 if ((cell1->
GetPV()) > -1) pv_new[ic] = cell0->
GetPV();
372 pv_new[jc] = cell1->
GetPV() + 1;
375 cout <<
"pv 0 = " << pv_new[ic] <<
" pv 1 = " << pv_new[jc]
388 for (
int icv = 0; icv < tCellArray->GetEntriesFast(); icv++) {
389 if (pv_new[icv] < 0) {
390 tCellArray->RemoveAt(icv);
391 tCellArray->Compress();
392 pv_new.erase(pv_new.begin() + icv);
397 cout <<
"cell with hitDw = " << cell->
GetHitDw()
398 <<
" and hitUp = " << cell->
GetHitUp()
399 <<
" gets new PV = " << pv_new[icv] << endl;
400 cell->
SetPV(pv_new[icv]);
406 cout <<
"One itter with " << nCells <<
" cells: " << endl;
407 timer_neighbors_itter->Stop();
408 timer_neighbors_itter->Print();
411 cout <<
"-- CA made " << itt <<
" itterations --" << endl;
412 if (stop_itter)
break;
414 delete timer_neighbors_itter;
422 std::vector<std::vector<Int_t> > hitsd) {
423 TClonesArray *tCellArray =
new TClonesArray(
"PndSdsCell");
427 cout <<
"Start cell contruction from " << nPixelHits <<
" hits" << endl;
429 for (
int pl0 = 0; pl0 < 3; pl0++) {
431 tCellArray =
CookCells(hitsd, pl0, pl1, tCellArray);
434 tCellArray =
CookCells(hitsd, pl0, pl1, tCellArray);
442 std::vector<std::vector<Int_t> > hitsd,
int &pl0,
int &pl1,
443 TClonesArray *tCellArray) {
444 int ncells = tCellArray->GetEntriesFast();
447 cout <<
"Start cell contruction from " << nPixelHits <<
" hits" << endl;
449 for (
unsigned int i = 0;
i < hitsd.at(pl0).size();
i++) {
450 for (
unsigned int j = 0; j < hitsd.at(pl1).size(); j++) {
452 cout <<
"new cell with hits #" << hitsd.at(pl0).at(
i) <<
" and "
453 << hitsd.at(pl1).at(j) << endl;
458 new ((*tCellArray)[ncells])
PndSdsCell(hitsd.at(pl1).at(j),
459 hitsd.at(pl0).at(
i));
468 TStopwatch *timer_array =
new TStopwatch();
470 if (
fVerbose > 0) timer_array->Start();
477 if (nPixelHits < 2) {
479 cout <<
"Evt finsihed: too less hits-----" << endl << endl;
483 std::vector<std::vector<Int_t> > hitsd(
492 std::cout <<
"Algorithm depend on sensor type! Please, set it via "
493 "SetSensStripFlag(bool fS) or SetSensPixelFlag(bool fS)"
500 cout <<
"Evt finsihed: too less planes-----" << endl << endl;
505 TVector3 start, tmp,
vec, dstart, dvec;
507 for (Int_t iPlane = 0; iPlane <
nP; iPlane++) {
508 if (hitsd.at(iPlane).size() > 0) {
509 for (
unsigned int i = 0;
i < hitsd.at(iPlane).size();
i++) {
513 cout <<
" Hit#" << hitsd.at(iPlane).at(
i) <<
" Plane" << iPlane
514 <<
" Hit=(" << hit->GetX() <<
", " << hit->GetY() <<
", "
515 << hit->GetZ() <<
") with err=(" << hit->GetDx() <<
", "
516 << hit->GetDy() <<
", " << hit->GetDz() <<
")"
517 <<
", MChitID = " << hit->GetRefIndex() << endl;
523 cout <<
"array preparation: " << endl;
525 timer_array->
Print();
532 TStopwatch *timer_cook_cells =
new TStopwatch();
533 if (
fVerbose > 0) timer_cook_cells->Start();
544 for (
int pl0 = 0; pl0 < 2; pl0++) {
559 for (
int icell0 = 0; icell0 <
fCellArray->GetEntries(); icell0++) {
572 for (
int pl0 = 1; pl0 < 3; pl0++) {
578 for (
int icell0 = 0; icell0 <
fCellArray->GetEntries(); icell0++) {
610 for (
int icell0 = 0; icell0 <
fCellArray->GetEntries(); icell0++) {
616 cout <<
"Cells cooking:" << endl;
617 timer_cook_cells->Stop();
618 timer_cook_cells->Print();
621 TStopwatch *timer_neighbors_cells =
new TStopwatch();
622 if (
fVerbose > 0) timer_cook_cells->Start();
630 TStopwatch *timer_build_trk_combinations =
new TStopwatch();
632 if (
fVerbose > 0) timer_build_trk_combinations->Start();
636 for (
int cid = 1; cid < nCells; cid++) {
638 int tag_cur = cell->
GetPV();
639 if (tag_cur > pcmax) pcmax = tag_cur;
641 if (
fVerbose > 4) cout <<
"track can contain " << pcmax <<
"+1 cells" << endl;
642 const unsigned int trk_arr_size = pcmax + 1;
643 std::vector<std::vector<int> > trk_cells(trk_arr_size);
646 for (
unsigned int newpcmax = pcmax; newpcmax > 0;
648 unsigned int cur_max_tag = newpcmax;
650 cout <<
"Now we are looking for trk with max " << cur_max_tag + 1
651 <<
" cells among " << nCells <<
" cells" << endl;
653 for (
int icell0 = 0; icell0 < nCells; icell0++) {
655 if (cell0->
GetPV() ==
657 for (
int icell1 = 0; icell1 < nCells; icell1++) {
662 bool isNeighbor =
Neighbor(cell1, cell0);
665 cout <<
"trk #" << trk_count <<
" " << cell0->
GetPV() <<
", "
666 << cell1->
GetPV() <<
"; ";
667 if (cell1->
GetPV() > 0) {
671 for (
int icell2 = 0; icell2 < nCells; icell2++) {
676 bool isNeighbor2 =
Neighbor(cell2, cell1);
690 if ((cell0->
GetPV() + 1) == pcmax) {
695 if (
fVerbose > 2) cout <<
"" << endl;
703 timer_build_trk_combinations->Stop();
704 cout <<
"Build trks combinations: " << endl;
705 timer_build_trk_combinations->Print();
708 cout <<
"Before filtering number of trk-cands = " << trk_cells.at(0).size()
712 TStopwatch *timer_filter_trk_combinations =
new TStopwatch();
714 if (
fVerbose > 0) timer_filter_trk_combinations->Start();
716 if (
fVerbose > 4) cout <<
"--- filter trk-cand array: " << endl;
718 cout <<
" Attention each trk candidate with repeated cells, but smaller "
719 "cells number will be deleted!"
721 vector<unsigned int> cell_parts;
722 vector<bool> trk_accept;
723 for (
unsigned int itrk = 0; itrk < trk_cells.at(0).size(); itrk++) {
724 int maxcellnum = trk_arr_size;
725 int curr_arr = trk_arr_size;
726 while (curr_arr > 0) {
729 cout <<
" we have: trk_cells.at(" << curr_arr <<
").at(" << itrk
730 <<
")=" << trk_cells.at(curr_arr).at(itrk) << endl;
731 if (trk_cells.at(curr_arr).at(itrk) < 0) maxcellnum--;
733 cell_parts.push_back(maxcellnum);
735 trk_accept.push_back(
true);
737 trk_accept.push_back(
false);
738 if (
fVerbose > 4) cout <<
" with:" << maxcellnum <<
" cells" << endl;
741 for (
int itrc = (cell_parts.size() - 1); itrc >= 0; itrc--) {
742 if (!trk_accept[itrc])
continue;
744 for (
int itrc2 = cup; itrc2 >= 0; itrc2--) {
745 if (!trk_accept[itrc])
break;
746 if (!trk_accept[itrc2])
continue;
749 if (cell_parts[itrc] < cell_parts[itrc2]) {
750 unsigned int count_re = 0;
751 unsigned int count_hits = 0;
753 int curr_arr = trk_arr_size;
754 while (curr_arr > 0) {
756 int cellnum1 = trk_cells.at(curr_arr).at(itrc);
757 int cellnum2 = trk_cells.at(curr_arr).at(itrc2);
759 if (cellnum1 < 0 || cellnum2 < 0)
continue;
760 if (cellnum1 == cellnum2) count_re++;
768 if (hit0_0 == hit0_1) count_hits++;
769 if (hit1_0 == hit1_1) count_hits++;
770 if (hit0_0 == hit1_1) count_hits++;
771 if (hit1_0 == hit0_1) count_hits++;
774 cout <<
"Cand.No" << itrc
775 <<
": Number of repeating cells = " << count_re
776 <<
" and cells in tot = " << cell_parts[itrc]
777 <<
" Number of repeating hits = " << count_hits << endl;
778 if (count_re > 0.55 * cell_parts[itrc] || count_hits > 2) {
779 trk_accept[itrc] =
false;
781 cout <<
"Delete: trk-cand#" << itrc <<
" because it contains ("
782 << count_re <<
") more then 55% cells from trk-cand#" << itrc2
791 timer_filter_trk_combinations->Stop();
792 cout <<
"Filter: " << endl;
793 timer_filter_trk_combinations->Print();
798 for (
unsigned int itrk = 0; itrk < trk_accept.size(); itrk++) {
799 if (!trk_accept[itrk])
continue;
800 bool firstHit =
true;
803 if (
fVerbose > 2) cout <<
"Trk-cand contains hits:";
804 for (
unsigned int icell = 0; icell < trk_arr_size; icell++) {
805 int cellNum = trk_cells.at(icell).at(itrk);
806 if (cellNum < 0)
continue;
816 dir *= 1. / dir.Mag();
824 if (
fVerbose > 2) cout <<
" " << endl;
826 new ((*fTrackCandArray)[NtrkRec++])
829 if (
fVerbose > 3) cout <<
"Ntrk No. " << NtrkRec << endl;
833 hitsd.erase(hitsd.begin(), hitsd.end());
836 cout <<
"Number of Trk-Cands is " << ntcandFin << endl;
837 cout <<
"Evt finsihed--------------" << endl << endl;
844 delete timer_cook_cells;
845 delete timer_neighbors_cells;
846 delete timer_build_trk_combinations;
847 delete timer_filter_trk_combinations;
854 return (2 /
TMath::Sqrt(p.Px() * p.Px() + p.Py() * p.Py()));
860 return (p.Mag() /
TMath::Sqrt(p.Px() * p.Px() + p.Py() * p.Py()));
bool SortHitsByDet(std::vector< std::vector< std::pair< Int_t, bool > > > &hitsd, Int_t nStripHits)
void Evolution(int &pv0, int &pv1, int &pv0_n, int &pv1_n, bool isch)
virtual void Print(const Option_t *opt=0) const
Double_t GetTrackDip(PndMCTrack *myTrack)
TVector3 GetPosition() const
TClonesArray * fCellArray
virtual InitStatus Init()
static T Sqrt(const T &x)
labels push_back("electron")
bool SortHitsByDetSimple(std::vector< std::vector< Int_t > > &hitsd, Int_t nStripHits)
virtual InitStatus ReInit()
virtual void SetParContainers()
static PndLmdGeometryHelper & getInstance()
TVector3 GetMomentum() const
TClonesArray * CookAllCells(std::vector< std::vector< Int_t > > hitsd)
bool SortHitsByZ(std::vector< std::vector< std::pair< Int_t, bool > > > &hitsd, Int_t nStripHits)
unsigned char module_side
bool Neighbor(int &icell0, int &icell1)
TClonesArray * ForwardEvolution(TClonesArray *fCellArray, int niter=100)
void AddHit(UInt_t detId, UInt_t hitId, Double_t rho)
virtual void Exec(Option_t *opt)
TClonesArray * fTrackCandArray
friend F32vec4 fabs(const F32vec4 &a)
int hit(Int_t nEvents=0, TString inFile="sim.root", TString parFile="par.root", TString inDigi="digi.root", TString outFile="hit.root", Int_t timeBased=0)
virtual ~PndLmdTrackFinderCATask()
TClonesArray * fStripHitArray
Double_t GetTrackCurvature(PndMCTrack *myTrack)
PndLmdTrackFinderCATask()
Int_t GetSensorID() const
bool SortHitsByDetSimple2(std::vector< std::vector< Int_t > > &hitsd, Int_t nStripHits)
TClonesArray * CookCells(std::vector< std::vector< Int_t > > hitsd, int &pl0, int &pl1, TClonesArray *tCellArray)