NAIA  1.0.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
MCTruthFill.cpp
Go to the documentation of this file.
1 #include "Containers/MCTruth.h"
2 
3 #include "TDatabasePDG.h"
4 
5 #include <algorithm>
6 
7 namespace NAIA {
8 
9 static float amu = 0.93146;
10 
11 MCParticle GetMCParticle(const MCEventgR &mcp) {
12  MCParticle particle{TDatabasePDG::Instance()->ConvertGeant3ToPdg(mcp.Particle),
13  static_cast<int>(mcp.Charge),
14  TMath::Nint(mcp.Mass / amu),
15  {mcp.Momentum * TVector3{mcp.Dir[0], mcp.Dir[1], mcp.Dir[2]}},
16  {TVector3{mcp.Coo[0], mcp.Coo[1], mcp.Coo[2]}},
17  mcp.trkID,
18  mcp.parentID,
19  mcp.Nskip >> 24,
20  mcp.Nskip & 0xFFFFFF};
21 
22  return particle;
23 }
24 
25 unsigned int TkIDToLayerJ(int tkID) {
26  tkID = abs(tkID);
27 
28  unsigned int layerOld = tkID / 100;
29  switch (layerOld) {
30  case 8:
31  return 1;
32  break;
33  case 9:
34  return 9;
35  default:
36  return layerOld + 1;
37  }
38 }
39 
40 MCTruthPlusData::TrMCHit GetTrMCHit(TrTrackR *pTrTrack, unsigned int jLayer, double match_radius = 0.1) {
41  AMSEventR *event = AMSEventR::Head();
42  if (!event)
43  throw std::runtime_error("Invalid pointer for AMSEventR::Head");
44  if (event->nMCEventgC() == 0)
45  throw std::runtime_error("No MCEventg information in this event");
46 
48 
49  if (!pTrTrack->TestHitLayerJ(jLayer))
50  return mcHit;
51 
52  AMSPlaneM plm = pTrTrack->GetHitCooLJN(jLayer);
53  TVector3 mglob = plm.getMGlobal();
54  AMSPoint point = AMSPoint(mglob.x(), mglob.y(), mglob.z());
55 
56  auto mcclusterIt =
57  std::min_element(begin(event->TrMCCluster()), end(event->TrMCCluster()),
58  [&point](auto &cl1, auto &cl2) { return cl1.GetXgl().dist(point) < cl2.GetXgl().dist(point); });
59 
60  if (mcclusterIt == end(event->TrMCCluster()))
61  return mcHit;
62 
63  // Fail if minimal distance is not in match_radius
64  auto rmin = mcclusterIt->GetXgl().dist(point);
65  if (rmin > match_radius)
66  return mcHit;
67 
68  mcHit.pID = mcclusterIt->GetPart();
69  for (unsigned int ic = 0; ic < 3; ic++) {
70  mcHit.Coo[ic] = mcclusterIt->GetXgl()[ic];
71  mcHit.Mom[ic] = mcclusterIt->GetMom()[ic];
72  }
73 
74  return mcHit;
75 }
76 
77 auto particleSorter = [](const MCEventgR &a, const MCEventgR &b) -> bool {
78  if (a.parentID < b.parentID)
79  return true;
80  else if (a.parentID > b.parentID)
81  return false;
82  else {
83  if (a.Coo[2] > b.Coo[2])
84  return true;
85  else if (a.Coo[2] < b.Coo[2])
86  return false;
87  else
88  return a.Momentum > b.Momentum;
89  }
90 };
91 
92 bool MCTruthBaseData::Fill(AMSEventR *evPtr) {
93 
94  // sort all MC particles by interaction point/momentum
95  std::sort(begin(evPtr->MCEventg()), end(evPtr->MCEventg()), particleSorter);
96 
97  auto primIt = std::find_if(begin(evPtr->MCEventg()), end(evPtr->MCEventg()),
98  [](const MCEventgR &mcp) { return mcp.Coo[2] > 160.0; });
99 
100  if (primIt == end(evPtr->MCEventg())) {
101  throw std::runtime_error("No generation point found for primary particle (with CooZ > 160 cm)");
102  }
103 
104  constexpr float z_tolerance = 0.35f;
105 
106  Primary = GetMCParticle(*primIt);
107  for (const auto &mcp : evPtr->MCEventg()) {
108  if (mcp.parentID != 0)
109  break;
110 
111  auto hIt = std::find_if(std::next(begin(MCTruth::MCHeightsZ)), end(MCTruth::MCHeightsZ),
112  [&mcp](float height) { return fabs(mcp.Coo[2] - height) < z_tolerance; });
113  if (hIt != end(MCTruth::MCHeightsZ)) {
114  auto position = std::distance(begin(MCTruth::MCHeightsZ), hIt);
115  Primary.Momentum.resize(position + 1);
116  Primary.Momentum[position] = mcp.Momentum * TVector3{mcp.Dir[0], mcp.Dir[1], mcp.Dir[2]};
117 
118  Primary.Position.resize(position + 1);
119  Primary.Position[position] = TVector3{mcp.Coo[0], mcp.Coo[1], mcp.Coo[2]};
120  }
121  }
122 
123  return true;
124 }
125 
126 bool MCTruthPlusData::Fill(AMSEventR *evPtr) {
127 
128  // this should already be sorted, but you never know...
129  if (!std::is_sorted(begin(evPtr->MCEventg()), end(evPtr->MCEventg()), particleSorter)) {
130  // sort all MC particles by interaction point/momentum
131  std::sort(begin(evPtr->MCEventg()), end(evPtr->MCEventg()), particleSorter);
132  }
133 
134  const MCEventgR &firstPrim = evPtr->MCEventg()[0];
135 
136  const auto lastPrimIt = std::find_if(rbegin(evPtr->MCEventg()), rend(evPtr->MCEventg()),
137  [](const MCEventgR &mcp) { return mcp.parentID == 0; });
138 
139  if (lastPrimIt == rend(evPtr->MCEventg()))
140  return false;
141 
142  for (MCEventgR &mcp : evPtr->MCEventg()) {
143  if (mcp.Momentum / firstPrim.Momentum > 0.01 && mcp.Coo[2] <= lastPrimIt->Coo[2]) {
144  Secondaries.push_back(GetMCParticle(mcp));
145  }
146  }
147 
148  return true;
149 }
150 
151 bool MCTruthPlusData::FillTrMCHit(TrTrackR *pTrTrack) {
152  for (unsigned int ij = 0; ij < 9; ij++) {
153  auto mcHit = GetTrMCHit(pTrTrack, ij + 1);
154  if (mcHit.pID > 0)
155  TrackMCHits[ij] = mcHit;
156  }
157  return true;
158 }
159 } // namespace NAIA
std::vector< TVector3 > Position
particle position at different z-heights. See MCTruth::MCHeights for a description of the possible he...
Definition: MCTruth.h:37
MCTruthPlusData::TrMCHit GetTrMCHit(TrTrackR *pTrTrack, unsigned int jLayer, double match_radius=0.1)
Definition: MCTruthFill.cpp:40
std::vector< TVector3 > Momentum
particle momentum at different z-heights. See MCTruth::MCHeights for a description of the possible he...
Definition: MCTruth.h:36
auto particleSorter
Definition: MCTruthFill.cpp:77
static float amu
Definition: MCTruthFill.cpp:9
MCParticle GetMCParticle(const MCEventgR &mcp)
Definition: MCTruthFill.cpp:11
constexpr std::array< float, numMCHeights > MCHeightsZ
Definition: Utils.h:391
NAIAChain::EventItr begin(NAIAChain &chain)
Definition: NAIAChain.h:297
std::array< float, 3 > Coo
hit position
Definition: MCTruth.h:112
Simple struct for MC tracker hits.
Definition: MCTruth.h:110
MCParticle Primary
primary particle
Definition: MCTruth.h:91
unsigned int TkIDToLayerJ(int tkID)
Definition: MCTruthFill.cpp:25
std::vector< MCParticle > Secondaries
list of particles created by interaction of the primary particle
Definition: MCTruth.h:134
MCTruth container class description.
Simple struct to describe a MC particle.
Definition: MCTruth.h:31
NAIAChain::EventItr end(NAIAChain &chain)
Definition: NAIAChain.h:298
std::array< float, 3 > Mom
hit momentum
Definition: MCTruth.h:113
LayerVariable< TrMCHit > TrackMCHits
A collection of the closest MCHit to the TrTrack cluster on each layer.
Definition: MCTruth.h:135
unsigned int pID
particle PID
Definition: MCTruth.h:111