LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
MCRecoPart.cxx
Go to the documentation of this file.
1 //
3 // MCRecoPart source
4 //
6 
9 
13 
14 #include "MCRecoPart.h"
15 
16 namespace sim {
17 
18  //--------------------------------------------------------------------------------------------
20  : _x_max{std::numeric_limits<double>::min()}
21  , _x_min{std::numeric_limits<double>::max()}
22  , _y_max{std::numeric_limits<double>::min()}
23  , _y_min{std::numeric_limits<double>::max()}
24  , _z_max{std::numeric_limits<double>::min()}
25  , _z_min{std::numeric_limits<double>::max()}
26  , _trackIDOffsets{pset.get<std::vector<unsigned int>>("TrackIDOffsets", {0})}
27  {
28  this->clear();
29  _track_index.clear();
30  _pdg_list.clear();
31  for (auto const& id : pset.get<std::vector<int>>("SavePathPDGList"))
32 
33  _pdg_list.insert(id);
34 
36  // Build "Fiducial" Volume Definition:
37  //
38  // Iterate over all TPC's to get bounding box that covers volumes of each individual TPC in the detector
39  for (auto const& tpc : geo->Iterate<geo::TPCGeo>()) {
40  _x_max = std::max(_x_max, tpc.BoundingBox().MaxX());
41  _x_min = std::min(_x_min, tpc.BoundingBox().MinX());
42  _y_max = std::max(_y_max, tpc.BoundingBox().MaxY());
43  _y_min = std::min(_y_min, tpc.BoundingBox().MinY());
44  _z_max = std::max(_z_max, tpc.BoundingBox().MaxZ());
45  _z_min = std::min(_z_min, tpc.BoundingBox().MinZ());
46  }
47  }
48 
49  //--------------------------------------------------------------------------------------------
50  unsigned int MCRecoPart::MotherTrackID(const unsigned int part_index) const
51  //--------------------------------------------------------------------------------------------
52  {
53  if (this->size() <= part_index) return ::sim::kINVALID_UINT;
54 
55  unsigned int result = this->at(part_index)._mother;
56 
57  //Is a primary particle. Account for possible track ID offsets
58  for (auto const& offset : _trackIDOffsets) {
59  if (result == offset) return this->at(part_index)._track_id;
60  }
61 
62  if (TrackToParticleIndex(result) != ::sim::kINVALID_UINT) return result;
63 
64  //std::cout<< "\033[95mWarning:\033[00m Mother particle not in the particle list!"<<std::endl;
65  // Do brute search
66  unsigned int daughter_id = this->at(part_index)._track_id;
67 
68  for (auto const& part : *this) {
69 
70  if (part.HasDaughter(daughter_id)) return part._track_id;
71  }
72  return result;
73  }
74 
75  //--------------------------------------------------------------------------------------------
76  unsigned int MCRecoPart::AncestorTrackID(const unsigned int part_index)
77  //--------------------------------------------------------------------------------------------
78  {
79  if (part_index >= this->size()) return kINVALID_UINT;
80 
81  if ((*this)[part_index]._ancestor != kINVALID_UINT) return (*this)[part_index]._ancestor;
82 
83  auto result = MotherTrackID(part_index);
84 
85  if (result == this->at(part_index)._track_id) return result;
86 
87  //Is a primary particle. Account for possible track ID offsets
88  for (auto const& offset : _trackIDOffsets) {
89  if (result == offset) return this->at(part_index)._track_id;
90  }
91 
92  auto mother_index = TrackToParticleIndex(result);
93 
94  while (1) {
95 
96  if (mother_index != kINVALID_UINT) {
97 
98  auto const new_result = MotherTrackID(mother_index);
99 
100  if (new_result == this->at(mother_index)._track_id) break;
101 
102  result = new_result;
103  }
104  else {
105 
106  // Look for a particle that has a daughter = this mother
107  auto const old_result = result;
108  for (auto const& p : *this) {
109 
110  if (p.HasDaughter(result)) {
111  result = p._track_id;
112  break;
113  }
114  }
115  if (result == old_result) break;
116  }
117 
118  mother_index = TrackToParticleIndex(result);
119  }
120 
121  (*this)[part_index]._ancestor = result;
122  return result;
123  }
124 
125  //--------------------------------------------------------------------------------------------
126  bool MCRecoPart::InDetector(const double& x, const double& y, const double& z) const
127  //--------------------------------------------------------------------------------------------
128  {
129  return !(x > _x_max || x < _x_min || z > _z_max || z < _z_min || y > _y_max || y < _y_min);
130  }
131 
132  //--------------------------------------------------------------------------------------------
133  void MCRecoPart::AddParticles(const std::vector<simb::MCParticle>& mcp_v,
134  const std::vector<simb::Origin_t>& orig_v,
135  const std::vector<simb::MCParticle>& mcmp_v)
136  //--------------------------------------------------------------------------------------------
137  {
138  if (orig_v.size() != mcp_v.size())
139  throw cet::exception(__FUNCTION__) << "MCParticle and Origin_t vector size not same!";
140 
141  this->clear();
142  _track_index.clear();
143 
144  for (size_t i = 0; i < mcp_v.size(); ++i) {
145 
146  auto const& mcp = mcp_v[i];
147 
148  //std::cout<<" Track ID : "<<mcp.TrackId()<<" ... Index : " <<this->size()<<std::endl;
149 
150  _track_index.insert(std::make_pair((size_t)(mcp.TrackId()), (size_t)(this->size())));
151 
152  // Change units to LArSoft (MeV, cm, us)
153  // (done inside constructor of MCMiniPart)
154  this->push_back(MCMiniPart(mcp));
155 
156  auto& mini_mcp = (*this->rbegin());
157 
158  for (size_t i = 0; i < (size_t)(mcp.NumberDaughters()); ++i) {
159  mini_mcp.AddDaughter(mcp.Daughter(i));
160  }
161  mini_mcp._origin = orig_v[i];
162 
163  if (_pdg_list.find(mcp.PdgCode()) != _pdg_list.end()) {
164 
165  std::set<size_t> det_path_index;
166 
167  for (size_t i = 0; i < mcp.NumberTrajectoryPoints(); ++i) {
168 
169  if (InDetector(mcp.Vx(i), mcp.Vy(i), mcp.Vz(i))) det_path_index.insert(i);
170  }
171 
172  if (det_path_index.size()) {
173  if ((*det_path_index.begin())) det_path_index.insert((*det_path_index.begin()) - 1);
174  if (det_path_index.size() > 1) {
175  if (((*det_path_index.rbegin()) + 1) < mcp.NumberTrajectoryPoints())
176  det_path_index.insert((*det_path_index.rbegin()) + 1);
177  }
178  std::vector<std::pair<TLorentzVector, TLorentzVector>> det_path;
179  det_path.reserve(det_path_index.size());
180  for (auto const& index : det_path_index) {
181 
182  TLorentzVector vec(mcp.Momentum(index));
183  for (size_t i = 0; i < 4; ++i)
184  vec[i] *= 1.e3;
185 
186  det_path.emplace_back(mcp.Position(index), vec);
187  }
188  mini_mcp._det_path = std::move(det_path);
189  }
190  } // end if in _pdg_list
191  } // end for loop over mcp_v
192 
193  // Now loop over dropped particles
194  for (auto const& mcmp : mcmp_v) {
195 
196  _track_index.try_emplace(mcmp.TrackId(), this->size());
197 
198  this->push_back(sim::MCMiniPart(mcmp));
199 
200  } // end for loop over mcmp_v
201  } // end AddParticles
202 }
Float_t x
Definition: compare.C:6
details::range_type< T > Iterate() const
Initializes the specified ID with the ID of the first cryostat.
Definition: GeometryCore.h:541
double _z_max
z-max of volume box used to determine whether to save track information
Definition: MCRecoPart.h:134
Float_t y
Definition: compare.C:6
Double_t z
Definition: plot.C:276
Geometry information for a single TPC.
Definition: TPCGeo.h:36
unsigned int MotherTrackID(const unsigned int part_index) const
Definition: MCRecoPart.cxx:50
double _y_max
y-max of volume box used to determine whether to save track information
Definition: MCRecoPart.h:132
double _y_min
y-min of volume box used to determine whether to save track information
Definition: MCRecoPart.h:133
std::map< unsigned int, unsigned int > _track_index
Track ID => Index Map.
Definition: MCRecoPart.h:123
Particle class.
void AddParticles(const std::vector< simb::MCParticle > &mcp_v, const std::vector< simb::Origin_t > &orig_v, const std::vector< simb::MCParticle > &mcmp_v={})
Definition: MCRecoPart.cxx:133
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:101
Access the description of detector geometry.
TString part[npart]
Definition: Style.C:32
double _z_min
z-min of volume box used to determine whether to save track information
Definition: MCRecoPart.h:135
std::vector< unsigned int > _trackIDOffsets
Track ID offsets for different MCTruths.
Definition: MCRecoPart.h:136
unsigned int AncestorTrackID(const unsigned int part_index)
Definition: MCRecoPart.cxx:76
MCRecoPart(fhicl::ParameterSet const &pset)
Default constructor with fhicl parameters.
Definition: MCRecoPart.cxx:19
Monte Carlo Simulation.
double _x_max
x-max of volume box used to determine whether to save track information
Definition: MCRecoPart.h:130
double _x_min
x-min of volume box used to determine whether to save track information
Definition: MCRecoPart.h:131
std::set< int > _pdg_list
PDG code list for which particle&#39;s trajectory within the detector is saved.
Definition: MCRecoPart.h:127
const unsigned int kINVALID_UINT
Definition: MCLimits.h:14
Namespace collecting geometry-related classes utilities.
bool InDetector(const double &x, const double &y, const double &z) const
Definition: MCRecoPart.cxx:126
unsigned int TrackToParticleIndex(const unsigned int track_id) const
Definition: MCRecoPart.h:112
art framework interface to geometry description
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
vec_iX clear()