MCRecoPart.cxx
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // MCRecoPart source
4 //
5 ////////////////////////////////////////////////////////////////////////
6 
9 
13 
14 #include "MCRecoPart.h"
15 
16 namespace sim {
17 
18  //--------------------------------------------------------------------------------------------
20  //--------------------------------------------------------------------------------------------
21  {
22  this->clear();
23  _track_index.clear();
24  _pdg_list.clear();
25  for(auto const& id : pset.get<std::vector<int> >("SavePathPDGList"))
26 
27  _pdg_list.insert(id);
28 
30  // Build "Fiducial" Volume Definition:
31  //
32  // Iterate over all TPC's to get bounding box that covers volumes of each individual TPC in the detector
33  _x_min = std::min_element(geo->begin_TPC(), geo->end_TPC(), [](auto const &lhs, auto const &rhs){ return lhs.BoundingBox().MinX() < rhs.BoundingBox().MinX();})->MinX();
34  _y_min = std::min_element(geo->begin_TPC(), geo->end_TPC(), [](auto const &lhs, auto const &rhs){ return lhs.BoundingBox().MinY() < rhs.BoundingBox().MinY();})->MinY();
35  _z_min = std::min_element(geo->begin_TPC(), geo->end_TPC(), [](auto const &lhs, auto const &rhs){ return lhs.BoundingBox().MinZ() < rhs.BoundingBox().MinZ();})->MinZ();
36  _x_max = std::max_element(geo->begin_TPC(), geo->end_TPC(), [](auto const &lhs, auto const &rhs){ return lhs.BoundingBox().MaxX() < rhs.BoundingBox().MaxX();})->MaxX();
37  _y_max = std::max_element(geo->begin_TPC(), geo->end_TPC(), [](auto const &lhs, auto const &rhs){ return lhs.BoundingBox().MaxY() < rhs.BoundingBox().MaxY();})->MaxY();
38  _z_max = std::max_element(geo->begin_TPC(), geo->end_TPC(), [](auto const &lhs, auto const &rhs){ return lhs.BoundingBox().MaxZ() < rhs.BoundingBox().MaxZ();})->MaxZ();
39  }
40 
41  //--------------------------------------------------------------------------------------------
42  unsigned int MCRecoPart::MotherTrackID(const unsigned int part_index) const
43  //--------------------------------------------------------------------------------------------
44  {
45  if(this->size() <= part_index) return ::sim::kINVALID_UINT;
46 
47  unsigned int result = this->at(part_index)._mother;
48 
49  if(!result) return this->at(part_index)._track_id;
50 
51  if(TrackToParticleIndex(result) != ::sim::kINVALID_UINT) return result;
52 
53  //std::cout<< "\033[95mWarning:\033[00m Mother particle not in the particle list!"<<std::endl;
54  // Do brute search
55  unsigned int daughter_id = this->at(part_index)._track_id;
56 
57  for(auto const& part : *this) {
58 
59  if(part._daughters.find(daughter_id) != part._daughters.end())
60 
61  return part._track_id;
62 
63  }
64  return result;
65  }
66 
67  //--------------------------------------------------------------------------------------------
68  unsigned int MCRecoPart::AncestorTrackID(const unsigned int part_index)
69  //--------------------------------------------------------------------------------------------
70  {
71  if(part_index >= this->size()) return kINVALID_UINT;
72 
73  if((*this)[part_index]._ancestor != kINVALID_UINT) return (*this)[part_index]._ancestor;
74 
75  unsigned int result = MotherTrackID(part_index);
76 
77  if(result == this->at(part_index)._track_id) return result;
78 
79  if(!result) return this->at(part_index)._track_id;
80 
81  auto mother_index = TrackToParticleIndex(result);
82 
83  while(1) {
84 
85  if(mother_index != kINVALID_UINT) {
86 
87  auto const new_result = MotherTrackID(mother_index);
88 
89  if(new_result == this->at(mother_index)._track_id) break;
90 
91  result = new_result;
92 
93  }else{
94 
95  // Look for a particle that has a daughter = this mother
96  auto const old_result = result;
97  for(auto const& p : *this) {
98 
99  if(p._daughters.find(result) != p._daughters.end()) {
100  result = p._track_id;
101  break;
102  }
103  }
104  if(result == old_result)
105  break;
106  }
107 
108  mother_index = TrackToParticleIndex(result);
109 
110  }
111 
112  (*this)[part_index]._ancestor = result;
113  return result;
114  }
115 
116  //--------------------------------------------------------------------------------------------
117  bool MCRecoPart::InDetector(const double& x,
118  const double& y,
119  const double& z) const
120  //--------------------------------------------------------------------------------------------
121  {
122  return !( x > _x_max || x < _x_min ||
123  z > _z_max || z < _z_min ||
124  y > _y_max || y < _y_min );
125  }
126 
127  //--------------------------------------------------------------------------------------------
128  void MCRecoPart::AddParticles(const std::vector<simb::MCParticle>& mcp_v,
129  const std::vector<simb::Origin_t>& orig_v)
130  //--------------------------------------------------------------------------------------------
131  {
132  if(orig_v.size() != mcp_v.size()) throw cet::exception(__FUNCTION__) << "MCParticle and Origin_t vector size not same!";
133 
134  this->clear();
135  _track_index.clear();
136 
137  for(size_t i=0; i < mcp_v.size(); ++i) {
138 
139  auto const& mcp = mcp_v[i];
140 
141  //std::cout<<" Track ID : "<<mcp.TrackId()<<" ... Index : " <<this->size()<<std::endl;
142 
143  _track_index.insert(std::make_pair((size_t)(mcp.TrackId()),(size_t)(this->size())));
144 
145  this->push_back(MCMiniPart());
146 
147  auto& mini_mcp = (*this->rbegin());
148 
149  for(size_t i=0; i<(size_t)(mcp.NumberDaughters()); ++i)
150  mini_mcp._daughters.insert(mcp.Daughter(i));
151 
152  mini_mcp._track_id = mcp.TrackId();
153  mini_mcp._pdgcode = mcp.PdgCode();
154  mini_mcp._mother = mcp.Mother();
155  mini_mcp._process = mcp.Process();
156  mini_mcp._start_vtx = mcp.Position();
157  mini_mcp._start_mom = mcp.Momentum();
158  mini_mcp._end_vtx = mcp.EndPosition();
159  mini_mcp._end_mom = mcp.EndMomentum();
160  mini_mcp._origin = orig_v[i];
161 
162  // Change units to LArSoft (MeV, cm, us)
163  for(size_t i=0; i<4; ++i) {
164  mini_mcp._start_mom[i] *= 1.e3;
165  mini_mcp._end_mom[i] *= 1.e3;
166  }
167  /*
168  for(size_t i=0; i<3; ++i) {
169  mini_mcp._start_vtx[i] /= 10.;
170  mini_mcp._end_vtx[i] /= 10.;
171  }
172  mini_mcp.start_vtx[3] /= 1.e-3;
173  mini_mcp.end_vtx[3] /= 1.e-3;
174  */
175 
176  if(_pdg_list.find(mcp.PdgCode()) != _pdg_list.end()) {
177 
178  std::set<size_t> det_path_index;
179 
180  for(size_t i=0; i<mcp.NumberTrajectoryPoints(); ++i) {
181 
182  if(InDetector(mcp.Vx(i),mcp.Vy(i),mcp.Vz(i)))
183 
184  det_path_index.insert(i);
185 
186  }
187 
188  if(det_path_index.size()) {
189  if( (*det_path_index.begin()) )
190  det_path_index.insert( (*det_path_index.begin())-1 );
191  if( det_path_index.size()>1 ) {
192  if( ((*det_path_index.rbegin())+1) < mcp.NumberTrajectoryPoints() )
193  det_path_index.insert( (*det_path_index.rbegin())+1 );
194  }
195  mini_mcp._det_path.reserve(det_path_index.size());
196  for(auto const& index : det_path_index) {
197 
198  TLorentzVector vec(mcp.Momentum(index));
199  for(size_t i=0; i<4; ++i) vec[i] *= 1.e3;
200 
201  mini_mcp._det_path.push_back(std::make_pair(mcp.Position(index),vec));
202 
203  }
204  }
205  }
206  }
207  }
208 }
double _z_max
z-max of volume box used to determine whether to save track information
Definition: MCRecoPart.h:108
static QCString result
unsigned int MotherTrackID(const unsigned int part_index) const
Definition: MCRecoPart.cxx:42
double _y_max
y-max of volume box used to determine whether to save track information
Definition: MCRecoPart.h:106
double _y_min
y-min of volume box used to determine whether to save track information
Definition: MCRecoPart.h:107
std::map< unsigned int, unsigned int > _track_index
Track ID => Index Map.
Definition: MCRecoPart.h:96
Particle class.
art framework interface to geometry description
TPC_iterator begin_TPC() const
Returns an iterator pointing to the first TPC in the detector.
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:92
double _z_min
z-min of volume box used to determine whether to save track information
Definition: MCRecoPart.h:109
fInnerVessel push_back(Point(-578.400000, 0.000000, 0.000000))
unsigned int AncestorTrackID(const unsigned int part_index)
Definition: MCRecoPart.cxx:68
MCRecoPart(fhicl::ParameterSet const &pset)
Default constructor with fhicl parameters.
Definition: MCRecoPart.cxx:19
void AddParticles(const std::vector< simb::MCParticle > &mcp_v, const std::vector< simb::Origin_t > &orig_v)
Definition: MCRecoPart.cxx:128
T get(std::string const &key) const
Definition: ParameterSet.h:271
p
Definition: test.py:223
Code to link reconstructed objects back to the MC truth information.
double _x_max
x-max of volume box used to determine whether to save track information
Definition: MCRecoPart.h:104
double _x_min
x-min of volume box used to determine whether to save track information
Definition: MCRecoPart.h:105
std::set< int > _pdg_list
PDG code list for which particle&#39;s trajectory within the detector is saved.
Definition: MCRecoPart.h:100
const unsigned int kINVALID_UINT
Definition: MCLimits.h:14
Access the description of detector geometry.
list x
Definition: train.py:276
vector< vector< double > > clear
LArSoft geometry interface.
Definition: ChannelGeo.h:16
bool InDetector(const double &x, const double &y, const double &z) const
Definition: MCRecoPart.cxx:117
unsigned int TrackToParticleIndex(const unsigned int track_id) const
Definition: MCRecoPart.h:82
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
TPC_iterator end_TPC() const
Returns an iterator pointing after the last TPC in the detector.