All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MCRecoPart.cxx
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // MCRecoPart source
4 //
5 ////////////////////////////////////////////////////////////////////////
6 
7 #include "art/Framework/Services/Registry/ServiceHandle.h"
8 #include "fhiclcpp/ParameterSet.h"
9 
12 #include "nusimdata/SimulationBase/MCParticle.h"
13 
14 #include "MCRecoPart.h"
15 
16 namespace sim {
17 
18  //--------------------------------------------------------------------------------------------
19  MCRecoPart::MCRecoPart(fhicl::ParameterSet const& pset)
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 
29  art::ServiceHandle<geo::Geometry const> geo;
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.HasDaughter(daughter_id) )
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  auto 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.HasDaughter(result)) {
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  const std::vector<sim::MCParticleLite>& mcmp_v)
131  //--------------------------------------------------------------------------------------------
132  {
133  if(orig_v.size() != mcp_v.size()) throw cet::exception(__FUNCTION__) << "MCParticle and Origin_t vector size not same!";
134 
135  this->clear();
136  _track_index.clear();
137 
138  for(size_t i=0; i < mcp_v.size(); ++i) {
139 
140  auto const& mcp = mcp_v[i];
141 
142  //std::cout<<" Track ID : "<<mcp.TrackId()<<" ... Index : " <<this->size()<<std::endl;
143 
144  _track_index.insert(std::make_pair((size_t)(mcp.TrackId()),(size_t)(this->size())));
145 
146  // Change units to LArSoft (MeV, cm, us)
147  // (done inside constructor of MCMiniPart)
148  this->push_back(MCMiniPart(mcp));
149 
150  auto& mini_mcp = (*this->rbegin());
151 
152  for(size_t i=0; i<(size_t)(mcp.NumberDaughters()); ++i) {
153  mini_mcp.AddDaughter( mcp.Daughter(i) );
154  }
155  mini_mcp._origin = orig_v[i];
156 
157  if(_pdg_list.find(mcp.PdgCode()) != _pdg_list.end()) {
158 
159  std::set<size_t> det_path_index;
160 
161  for(size_t i=0; i<mcp.NumberTrajectoryPoints(); ++i) {
162 
163  if(InDetector(mcp.Vx(i),mcp.Vy(i),mcp.Vz(i)))
164 
165  det_path_index.insert(i);
166 
167  }
168 
169  if(det_path_index.size()) {
170  if( (*det_path_index.begin()) )
171  det_path_index.insert( (*det_path_index.begin())-1 );
172  if( det_path_index.size()>1 ) {
173  if( ((*det_path_index.rbegin())+1) < mcp.NumberTrajectoryPoints() )
174  det_path_index.insert( (*det_path_index.rbegin())+1 );
175  }
176  std::vector<std::pair<TLorentzVector,TLorentzVector>> det_path;
177  det_path.reserve(det_path_index.size());
178  for(auto const& index : det_path_index) {
179 
180  TLorentzVector vec(mcp.Momentum(index));
181  for(size_t i=0; i<4; ++i) vec[i] *= 1.e3;
182 
183  det_path.emplace_back(mcp.Position(index), vec);
184 
185  }
186  mini_mcp._det_path = std::move(det_path);
187  }
188  } // end if in _pdg_list
189  } // end for loop over mcp_v
190 
191  // Now loop over dropped particles
192  for(auto const& mcmp : mcmp_v) {
193 
194  _track_index.try_emplace(mcmp.TrackID(), this->size());
195 
196  this->push_back(sim::MCMiniPart(mcmp));
197 
198  } // end for loop over mcmp_v
199  } // end AddParticles
200 }
process_name opflash particleana ie ie ie z
double _z_max
z-max of volume box used to determine whether to save track information
Definition: MCRecoPart.h:156
process_name opflash particleana ie x
void AddParticles(const std::vector< simb::MCParticle > &mcp_v, const std::vector< simb::Origin_t > &orig_v, const std::vector< sim::MCParticleLite > &mcmp_v={})
Definition: MCRecoPart.cxx:128
pdgs p
Definition: selectors.fcl:22
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:154
double _y_min
y-min of volume box used to determine whether to save track information
Definition: MCRecoPart.h:155
std::size_t size(FixedBins< T, C > const &) noexcept
Definition: FixedBins.h:561
std::map< unsigned int, unsigned int > _track_index
Track ID =&gt; Index Map.
Definition: MCRecoPart.h:144
Access the description of detector geometry.
double _z_min
z-min of volume box used to determine whether to save track information
Definition: MCRecoPart.h:157
unsigned int AncestorTrackID(const unsigned int part_index)
Definition: MCRecoPart.cxx:68
process_name opflash particleana ie ie y
MCRecoPart(fhicl::ParameterSet const &pset)
Default constructor with fhicl parameters.
Definition: MCRecoPart.cxx:19
double _x_max
x-max of volume box used to determine whether to save track information
Definition: MCRecoPart.h:152
double _x_min
x-min of volume box used to determine whether to save track information
Definition: MCRecoPart.h:153
std::set< int > _pdg_list
PDG code list for which particle&#39;s trajectory within the detector is saved.
Definition: MCRecoPart.h:148
const unsigned int kINVALID_UINT
Definition: MCLimits.h:14
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:130
art framework interface to geometry description