22 #include <unordered_map>
57 int TRDVertexStatus = -99;
66 double PrimaryParticleL1HitDistance = -9;
69 int nFittedPrimaryMCTRDtrkInEvent = 0;
70 int nPrimaryParticleL1Hit = 0;
80 MCEventgR * mc0 = pev->GetPrimaryMC();
81 const int mc0_PID = mc0->Particle;
82 int mc0_trkID = mc0->trkID;
83 double mc0_mom = mc0->Momentum;
84 int mc0_Nskip = mc0->Nskip;
85 double mc0_x = mc0->Coo[0];
86 double mc0_y = mc0->Coo[1];
87 double mc0_z = mc0->Coo[2];
92 std::cerr<<std::string(NLENGTH,
'_')<<std::endl;
93 printf(
"\n\n\nFor primary MC particle in this AMS event:\n");
94 printf(
"g3 PID = %10d, trkID = %10d, Momentum = %7.2f, Nskip = %12d, (MCCooX, MCCooY, MCCooZ) = %8.2f %8.2f %8.2f\n",
95 mc0_PID, mc0_trkID, mc0_mom, mc0_Nskip, mc0_x, mc0_y, mc0_z);
105 int nmceventg = pev->nMCEventg();
106 std::vector<int> vMCtrkIDs = {};
107 std::vector<Point::Point2DwID> vMCEventgClustersXY = {};
111 printf(
"\nFound %d MCEventgR's in this AMS event.\n", nmceventg);
113 for(
int imc = 0; imc < nmceventg; imc++)
115 MCEventgR mc = pev->MCEventg(imc);
116 vMCtrkIDs.push_back(mc.trkID);
124 vMCEventgClustersXY.emplace_back(mc.Coo[0], mc.Coo[1], mc.trkID, mc.Particle, -1);
129 printf(
"g3 PID = %10d, trkID = %10d, Momentum = %7.2f, Nskip = %12d, (MCCooX, MCCooY, MCCooZ) = %8.2f %8.2f %8.2f\n",
130 mc.Particle, mc.trkID, mc.Momentum, mc.Nskip, mc.Coo[0], mc.Coo[1], mc.Coo[2]);
139 printf(
"Unique trkIDs found: %zu.\nDetails of trkIDs with nClusters > 1:\n", MCEventgResultMap.size());
140 for(
const auto& pair : MCEventgResultMap)
143 printf(
"trkID = %10d, nClusters = %10d\n", pair.first, pair.second);
160 std::cerr<<std::string(NLENGTH,
'_')<<std::endl;
161 printf(
"\n\nNow analyzing MC TRD track clusters in this event...");
164 int ntrdmccluster = pev->nTrdMCCluster();
165 std::vector<int> vTRDMCtrkIDs = {};
168 printf(
"\nFound %d TrdMCClusterR's in this AMS event.\n", ntrdmccluster);
170 if (ntrdmccluster == 0)
172 TRDVertexStatus = -29;
173 return std::make_pair(TRDVertexStatus, PrimaryParticleL1HitDistance);
176 for(
int iTRDMCCluster = 0; iTRDMCCluster < ntrdmccluster; iTRDMCCluster++)
178 TrdMCClusterR trdCltr = pev->TrdMCCluster(iTRDMCCluster);
179 vTRDMCtrkIDs.push_back(trdCltr.GtrkID);
183 printf(
"g3 PID = %10d, GtrkID = %10d, Ekin = %7.2f, ProcID = %12d, MCCooX = %8.2f, MCCooY = %8.2f, MCCooZ = %8.2f\n",
184 trdCltr.ParticleNo,trdCltr.GtrkID, trdCltr.Ekin,trdCltr.ProcID, trdCltr.Xgl[0], trdCltr.Xgl[1], trdCltr.Xgl[2]);
190 printf(
"\n\nUnique TRD trkIDs found: %zu.", TRDMCresultMap.size());
195 unsigned int size_TRDMCresultMap = TRDMCresultMap.size();
200 printf(
"\nGood TRD trkIDs found : %zu.\nDetails of good TRD trkIDs with nClusters > 1:\n", TRDMCresultMap.size());
201 for(
const auto& pair : TRDMCresultMap)
206 printf(
"TRD trkID = %10d, nClusters = %10d\n", pair.first, pair.second);
220 std::cerr<<std::string(NLENGTH,
'_')<<std::endl;
221 printf(
"\n\nNow grouping clusters under separate AMSTrdMCTrack::Tracks...");
224 std::vector<AMSTrdMCTrack::Track> vMCTracksInEvent = {};
226 for(
const auto& pair : TRDMCresultMap)
232 track.
trkID = pair.first;
234 std::vector<int> vClusterG3PID;
236 for(
int iTRDMCCluster = 0; iTRDMCCluster < ntrdmccluster; iTRDMCCluster++)
238 TrdMCClusterR trdCltr = pev->TrdMCCluster(iTRDMCCluster);
240 if (pair.first != trdCltr.GtrkID)
243 vClusterG3PID.push_back(trdCltr.ParticleNo);
246 cluster.
x = trdCltr.Xgl[0];
247 cluster.
y = trdCltr.Xgl[1];
248 cluster.
z = trdCltr.Xgl[2];
256 track.
g3PID = vClusterG3PID[0];
257 vMCTracksInEvent.push_back(track);
262 printf(
"\n\nWARNING: In TRD trkID = %10d, nClusters = %10d, one vClusterG3PID element is different from the rest. Ignoring this element and moving on...\n", pair.first, pair.second);
264 vMCTracksInEvent.push_back(track);
269 --size_TRDMCresultMap;
272 printf(
"\n\n\n\nERROR! Track clusters do not have the same g3 PID!\n");
273 printf(
"vClusterG3PID.size() = %zu\n", vClusterG3PID.size());
274 printf(
"vClusterG3PID elements:\n");
276 printf(
"These clusters won't be used!\n\n\n\n");
280 vClusterG3PID.clear(); vClusterG3PID.resize(0);
284 if (vMCTracksInEvent.size() != size_TRDMCresultMap)
285 printf(
"\n\n\n\nERROR! Number of tracks saved does not equal unique tracks found!\n\nPlease confirm logic!\n\n\n\n");
289 printf(
"\n\nSaved %zu tracks in this AMS event for fitting:", vMCTracksInEvent.size());
291 if (vMCTracksInEvent.size() == 0)
293 TRDVertexStatus = -29;
294 return std::make_pair(TRDVertexStatus, PrimaryParticleL1HitDistance);
304 std::cerr<<std::string(NLENGTH,
'_')<<std::endl;
305 printf(
"\n\nNow performing track fits...");
308 std::vector<std::vector<Line::FitLine2D>> vvFittedTracks = {};
312 printf(
"\n\ntrkID = %d, G3PID = %d, nClusters = %zu\n", track.trkID, track.g3PID, track.clusters.size());
317 printf(
"---> Cluster: %9.3f %9.3f %9.3f\n", cluster.
x, cluster.
y, cluster.
z);
329 if (vFitted_Lines2D.size() == 3)
330 vvFittedTracks.push_back(vFitted_Lines2D);
336 unsigned int nFittedTracks = vvFittedTracks.size();
337 if (nFittedTracks == 0)
339 TRDVertexStatus = -29;
340 return std::make_pair(TRDVertexStatus, PrimaryParticleL1HitDistance);
346 printf(
"\n\n%d fitted AMSTrdMCTrack::Track found in event.\nVerifying track fit prameters:\n", nFittedTracks);
348 for (
unsigned int iFittedTrack = 0; iFittedTrack < nFittedTracks; iFittedTrack++)
352 for (
unsigned int iPlane = 0; iPlane < 3; iPlane++)
354 printf(
"Track#: %3d (%5d, %5d) = Fit plane: %s, M = %14.7f, C = %14.7f\n",
355 iFittedTrack, vvFittedTracks[iFittedTrack][iPlane].trkID, vvFittedTracks[iFittedTrack][iPlane].g3PID,
356 vFitPlanes[iPlane].c_str(), vvFittedTracks[iFittedTrack][iPlane].m, vvFittedTracks[iFittedTrack][iPlane].c);
362 if (vvFittedTracks[iFittedTrack][0].trkID == 1 && vvFittedTracks[iFittedTrack][0].g3PID == mc0_PID)
363 ++nFittedPrimaryMCTRDtrkInEvent;
365 if (nFittedPrimaryMCTRDtrkInEvent < 1)
369 TRDVertexStatus = -9;
370 return std::make_pair(TRDVertexStatus, PrimaryParticleL1HitDistance);
388 std::cerr<<std::string(NLENGTH,
'_')<<std::endl;
389 printf(
"\n\nNow finding 3D line from fitted plane projections...");
393 for (
unsigned int iFittedTrack = 0; iFittedTrack < nFittedTracks; iFittedTrack++)
395 const double m1 = vvFittedTracks[iFittedTrack][0].m;
const double b1 = vvFittedTracks[iFittedTrack][0].c;
396 const double m2 = vvFittedTracks[iFittedTrack][1].m;
const double b2 = vvFittedTracks[iFittedTrack][1].c;
397 const double m3 = vvFittedTracks[iFittedTrack][2].m;
const double b3 = vvFittedTracks[iFittedTrack][2].c;
398 const int fit_trkID = vvFittedTracks[iFittedTrack][0].trkID;
399 const int fit_g3PID = vvFittedTracks[iFittedTrack][0].g3PID;
403 printf(
"\n\n----> For fitted track# %d, (trkID = %d, g3PID = %d), the 2D projections provided are:\n", iFittedTrack, fit_trkID, fit_g3PID);
404 printf(
"XZ: m1, b1 = (%9.4f, %9.4f)\n", m1, b1);
405 printf(
"YZ: m2, b2 = (%9.4f, %9.4f)\n", m2, b2);
406 printf(
"XY: m3, b3 = (%9.4f, %9.4f)\n", m3, b3);
427 std::cout<<
"\nInvalid input or no solution exists for the given projections.\n";
439 if (closestPoint.second ==
false)
442 std::cout<<
"\nNo L1 hit found for this fitted track.\n\n";
448 std::cout<<
"\nFitted track potentially intersects with L1. Matching with MCEventgCluster hits...";
451 if (closestPoint.first.trkID == fit_trkID && closestPoint.first.PID == fit_g3PID)
454 std::cout<<
"\nThe closest L1 hit found: ("<<closestPoint.first.x<<
", "<<closestPoint.first.y<<
") at "<<closestPoint.first.closedis<<
" cm.\n";
457 if (fit_trkID == mc0_trkID && fit_g3PID == mc0_PID)
459 ++nPrimaryParticleL1Hit;
460 PrimaryParticleL1HitDistance = closestPoint.first.closedis;
467 std::cout<<
"\nBut no matched L1 MCEventgCluster hit found for this fitted track.\n\n";
472 if (nPrimaryParticleL1Hit != 1)
473 PrimaryParticleL1HitDistance = -5;
510 std::cerr<<std::string(NLENGTH,
'_')<<std::endl;
511 printf(
"\n\nFinding vertices in fitted intersecting tracks...");
512 printf(
"\n\nFor %d fitted tracks in this event.\n", nFittedTracks);
513 printf(
"nMAX_POSSIBLE_PAIRS = %d\n", nMAX_POSSIBLE_PAIRS);
514 printf(
"nNEXTTOMAX_POSSIBLE_PAIRS = %d\n", nNEXTTOMAX_POSSIBLE_PAIRS);
524 printf(
"\nnIntersecting 3D lines found = %d\n", nIntersecting_3Dlines);
530 if (nIntersecting_3Dlines == 0)
532 if (nFittedTracks == 1)
538 else if (nIntersecting_3Dlines == nMAX_POSSIBLE_PAIRS && nFittedTracks > 1)
542 if ((nFittedTracks == 2) &&
543 (vvFittedTracks[0][0].g3PID == -3 || vvFittedTracks[1][0].g3PID == -3) &&
544 (vvFittedTracks[0][0].g3PID * vvFittedTracks[1][0].g3PID < 0))
552 TRDVertexStatus = -19;
555 else if (nIntersecting_3Dlines == nNEXTTOMAX_POSSIBLE_PAIRS && nFittedTracks > 2)
568 else if (nIntersecting_3Dlines > nNEXTTOMAX_POSSIBLE_PAIRS && nIntersecting_3Dlines < nMAX_POSSIBLE_PAIRS)
573 TRDVertexStatus = -2;
576 else if (nIntersecting_3Dlines > 0 && nIntersecting_3Dlines < nNEXTTOMAX_POSSIBLE_PAIRS && nFittedTracks > 3)
582 TRDVertexStatus = -1;
587 TRDVertexStatus = -39;
589 printf(
"\n\n\n\nWARNING: Unknown logic: unable to assign the TRDVertexStatus!\n\n\n\n");
594 printf(
"\nEvent has been classified as TRDVertexStatus = %d\n", TRDVertexStatus);
601 vMCtrkIDs.clear(); vMCtrkIDs.resize(0);
602 vMCEventgClustersXY.clear(); vMCEventgClustersXY.resize(0);
603 vTRDMCtrkIDs.clear(); vTRDMCtrkIDs.resize(0);
604 vMCTracksInEvent.clear(); vMCTracksInEvent.resize(0);
605 vvFittedTracks.clear(); vvFittedTracks.resize(0);
607 return std::make_pair(TRDVertexStatus, PrimaryParticleL1HitDistance);
std::pair< int, double > GetTRDVertexClassifierOutput(AMSEventR *pev, TH2D *h_TrackerL1_XY, bool debugMode=false)
Definition: AMSMCEventClassifier.h:36
const double TrkL1_ZPos
Definition: AMSTracker.h:12
const double TrkL1_ZPosCutoff
cm
Definition: AMSTracker.h:13
const std::vector< std::string > vFitPlanes
Strings for fit planes and components.
Definition: TRDConstants.h:23
const int nMIN_CLUSTERS_FOR_FIT
cm
Definition: TRDConstants.h:19
const double fitChi2perNDF_threshold
fit threshold for ClusterFitter::GetClusterProjectionLinearFits()
Definition: TRDConstants.h:38
@ Z
Definition: AMSTrdMCTrack.h:21
@ X
Definition: AMSTrdMCTrack.h:19
@ Y
Definition: AMSTrdMCTrack.h:20
std::vector< double > GetTrkPosValues(const Track &track, Coordinate coord)
Definition: AMSTrdMCTrack.h:211
std::vector< Line::FitLine2D > GetClusterProjectionLinearFits(const std::vector< double > &TrkClusters_x, const std::vector< double > &TrkClusters_y, const std::vector< double > &TrkClusters_z, const int &trkID, const int &g3PID, const bool &debug=false, const double threshold=1000)
Definition: ClusterFitter.h:19
int GetNIntersectingLinePairs(const std::vector< std::vector< Line::FitLine2D >> &vvFittedTracks, const bool &debugMode=false)
Definition: Intersection3D.h:171
Line3DParametricForm Getline3DParametricForm(const double &m1, const double &b1, const double &m2, const double &b2, const double &m3, const double &b3, const int &trkID, const int &g3PID, bool debugMode)
Definition: Line.h:76
bool HasL1XYHit(const TH2D *hTrackerL1_XY, const double &x, const double &y, const bool &debug=false)
Definition: AMSTracker.h:20
std::pair< Point::Point2DwID, bool > FindClosestTrkL1ClusterFrom3DLine(const TH2D *hTrackerL1_XY, const Line::Line3DParametricForm &line, const std::vector< Point::Point2DwID > &gMCClustersXY, const bool &debug=false)
Definition: AMSTracker.h:34
int CalculatePairs(int n)
Definition: Utilities.h:30
bool areAllElementsEqual(const std::vector< T > &vec)
Definition: Utilities.h:113
void filterAndShrinkMap(std::map< int, int > &frequencyMap, int threshold=2)
Definition: Utilities.h:97
int getMajorityElement(const std::vector< int > &vec)
Definition: Utilities.h:122
std::map< int, int > GetUniqueElementsAndFrequency(const std::vector< int > &vec)
Definition: Utilities.h:83
void printVector(const std::vector< T > &v)
Definition: Utilities.h:58
int countDifferentFromMajority(const std::vector< int > &vec)
Definition: Utilities.h:146
Definition: AMSTrdMCTrack.h:27
double y
Definition: AMSTrdMCTrack.h:29
double z
Definition: AMSTrdMCTrack.h:30
double x
Definition: AMSTrdMCTrack.h:28
Definition: AMSTrdMCTrack.h:49
int trkID
Definition: AMSTrdMCTrack.h:50
std::vector< Cluster > clusters
Definition: AMSTrdMCTrack.h:52
int g3PID
Definition: AMSTrdMCTrack.h:51