All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TrackCheater_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // TrackCheater module
4 //
5 // brebel@fnal.gov
6 //
7 ////////////////////////////////////////////////////////////////////////
8 // Framework includes
9 #include "art/Framework/Core/ModuleMacros.h"
10 
11 #include <string>
12 
13 // ROOT includes
14 #include "TVector3.h"
15 
16 // LArSoft includes
26 #include "nusimdata/SimulationBase/MCParticle.h"
27 
28 // Framework includes
29 #include "art/Framework/Core/EDProducer.h"
30 #include "art/Framework/Principal/Event.h"
31 #include "art/Framework/Principal/Handle.h"
32 #include "art/Framework/Services/Registry/ServiceHandle.h"
33 #include "canvas/Persistency/Common/FindManyP.h"
34 #include "fhiclcpp/ParameterSet.h"
35 #include "messagefacility/MessageLogger/MessageLogger.h"
36 
37 namespace trkf {
38  class TrackCheater : public art::EDProducer {
39  public:
40  explicit TrackCheater(fhicl::ParameterSet const& pset);
41 
42  private:
43  void produce(art::Event& evt) override;
44 
45  std::string const fCheatedClusterLabel; ///< label for module creating
46  ///< recob::Cluster objects
47  std::string const fG4ModuleLabel; ///< label for module running G4 and making particles, etc
48  };
49 }
50 
51 namespace trkf {
52 
53  //--------------------------------------------------------------------
54  TrackCheater::TrackCheater(fhicl::ParameterSet const& pset)
55  : EDProducer{pset}
56  , fCheatedClusterLabel{pset.get<std::string>("CheatedClusterLabel", "cluster")}
57  , fG4ModuleLabel{pset.get<std::string>("G4ModuleLabel", "largeant")}
58  {
59  produces<std::vector<recob::Track>>();
60  produces<std::vector<recob::SpacePoint>>();
61  produces<art::Assns<recob::Track, recob::Cluster>>();
62  produces<art::Assns<recob::Track, recob::SpacePoint>>();
63  produces<art::Assns<recob::Track, recob::Hit>>();
64  }
65 
66  //--------------------------------------------------------------------
67  void
69  {
70  art::ServiceHandle<cheat::ParticleInventoryService const> pi_serv;
71  art::ServiceHandle<cheat::BackTrackerService const> bt_serv;
72  art::ServiceHandle<geo::Geometry const> geo;
73 
74  // grab the clusters that have been reconstructed
75  art::Handle<std::vector<recob::Cluster>> clustercol;
76  evt.getByLabel(fCheatedClusterLabel, clustercol);
77 
78  art::FindManyP<recob::Hit> fmh(clustercol, evt, fCheatedClusterLabel);
79 
80  // make a vector of them - we aren't writing anything out to a file
81  // so no need for a art::PtrVector here
82  std::vector<art::Ptr<recob::Cluster>> clusters;
83  art::fill_ptr_vector(clusters, clustercol);
84 
85  // loop over the clusters and figure out which particle contributed to each one
86  std::vector<art::Ptr<recob::Cluster>>::iterator itr = clusters.begin();
87 
88  // make a map of vectors of art::Ptrs keyed by eveID values
89  std::map<int, std::vector<std::pair<size_t, art::Ptr<recob::Cluster>>>> eveClusterMap;
90 
91  // loop over all clusters and fill in the map
92  for (size_t c = 0; c < clusters.size(); ++c) {
93 
94  std::pair<size_t, art::Ptr<recob::Cluster>> idxPtr(c, clusters[c]);
95 
96  // in the ClusterCheater module we set the cluster ID to be
97  // the eve particle track ID*1000 + plane*100 + tpc*10 + cryostat number. The
98  // floor function on the cluster ID / 1000 will give us
99  // the eve track ID
100  int eveID = floor((*itr)->ID() / 1000.);
101 
102  eveClusterMap[eveID].push_back(idxPtr);
103  ++itr;
104  } // end loop over clusters
105 
106  // loop over the map and make prongs
107  std::unique_ptr<std::vector<recob::Track>> trackcol(new std::vector<recob::Track>);
108  std::unique_ptr<std::vector<recob::SpacePoint>> spcol(new std::vector<recob::SpacePoint>);
109  std::unique_ptr<art::Assns<recob::Track, recob::SpacePoint>> tspassn(
110  new art::Assns<recob::Track, recob::SpacePoint>);
111  std::unique_ptr<art::Assns<recob::Track, recob::Cluster>> tcassn(
112  new art::Assns<recob::Track, recob::Cluster>);
113  std::unique_ptr<art::Assns<recob::Track, recob::Hit>> thassn(
114  new art::Assns<recob::Track, recob::Hit>);
115 
116  auto const clockData = art::ServiceHandle<detinfo::DetectorClocksService const>()->DataFor(evt);
117 
118  for (auto const& clusterMapItr : eveClusterMap) {
119 
120  // separate out the hits for each particle into the different views
121  std::vector<std::pair<size_t, art::Ptr<recob::Cluster>>> const& eveClusters =
122  clusterMapItr.second;
123 
124  simb::MCParticle* part = pi_serv->ParticleList()[clusterMapItr.first];
125 
126  // is this prong electro-magnetic in nature or
127  // hadronic/muonic? EM --> shower, everything else is a track
128  if (abs(part->PdgCode()) != 11 && abs(part->PdgCode()) != 22 && abs(part->PdgCode()) != 111) {
129 
130  // vectors to hold the positions and directions of the track
131  std::vector<TVector3> points;
132  std::vector<TVector3> moms;
133 
134  mf::LogInfo("TrackCheater")
135  << "G4 id " << clusterMapItr.first << " is a track with pdg code " << part->PdgCode();
136 
137  art::PtrVector<recob::Cluster> ptrvs;
138  std::vector<size_t> idxs;
139  for (auto const& idxPtr : eveClusters) {
140  ptrvs.push_back(idxPtr.second);
141  idxs.push_back(idxPtr.first);
142  }
143 
144  // grab the hits associated with the collection plane
145  std::vector<art::Ptr<recob::Hit>> hits;
146  for (size_t p = 0; p < ptrvs.size(); ++p) {
147  std::vector<art::Ptr<recob::Hit>> chits = fmh.at(idxs[p]);
148  if (!chits.size()) continue;
149  if (chits[0]->SignalType() != geo::kCollection) continue;
150  hits.insert(hits.end(), chits.begin(), chits.end());
151  }
152 
153  // need at least 2 hits to make a track
154  if (hits.size() < 2) continue;
155 
156  // loop over the hits to get the positions and directions
157  size_t spStart = spcol->size();
158  for (size_t t = 0; t < hits.size(); ++t) {
159  std::vector<double> xyz = bt_serv->HitToXYZ(clockData, hits[t]);
160  TVector3 point(xyz[0], xyz[1], xyz[2]);
161  points.push_back(point);
162 
163  std::vector<double> xyz1;
164  double dx = 0.;
165  double sign = 1.;
166 
167  if (t < hits.size() - 1) { xyz1 = bt_serv->HitToXYZ(clockData, hits[t + 1]); }
168  else {
169  xyz1 = bt_serv->HitToXYZ(clockData, hits[t - 1]);
170  sign = -1.;
171  }
172 
173  // dx is always positive
174  dx = std::sqrt(std::pow(xyz1[0] - xyz[0], 2) + std::pow(xyz1[1] - xyz[1], 2) +
175  std::pow(xyz1[2] - xyz[2], 2));
176 
177  // figure out momentum
178  double mom = 0;
179  double drmin = std::numeric_limits<double>::max();
180  for (unsigned int itp = 0; itp < part->NumberTrajectoryPoints(); itp++) {
181  TVector3 p(part->Vx(itp), part->Vy(itp), part->Vz(itp));
182  double dr = (p - point).Mag();
183  if (dr < drmin) {
184  mom = part->P(itp);
185  drmin = dr;
186  }
187  }
188 
189  // direction is always from the previous point along track to
190  // the next point, that is why sign is there
191  moms.push_back(TVector3(mom * sign * (xyz1[0] - xyz[0]) / dx,
192  mom * sign * (xyz1[1] - xyz[1]) / dx,
193  mom * sign * (xyz1[2] - xyz[2]) / dx));
194 
195  /*************************************************************/
196  /* WARNING */
197  /*************************************************************/
198  /* The dQdx information in recob::Track has been deprecated */
199  /* since 2016 and in 11/2018 the recob::Track interface was */
200  /* changed and DQdxAtPoint and NumberdQdx were removed. */
201  /* Therefore the code below is now commented out */
202  /* (note that it was most likely not functional anyways). */
203  /* For any issue please contact: larsoft-team@fnal.gov */
204  /*************************************************************/
205  /*
206  dQdx[0].push_back(charge/dx);
207  dQdx[1].push_back(charge/dx);
208  dQdx[2].push_back(charge/dx);
209  */
210  /*************************************************************/
211 
212  // make the space point and set its ID and XYZ
213  double xyzerr[6] = {1.e-3};
214  double chisqr = 0.9;
215  recob::SpacePoint sp(&xyz[0], xyzerr, chisqr, clusterMapItr.first * 10000 + t);
216  spcol->push_back(sp);
217  }
218 
219  size_t spEnd = spcol->size();
220 
221  // add a track to the collection. Make the track
222  // ID be the same as the track ID for the eve particle
223  trackcol->push_back(
226  recob::Track::Flags_t(points.size()),
227  true),
228  0,
229  -1.,
230  0,
233  clusterMapItr.first));
234 
235  // associate the track with its clusters
236  util::CreateAssn(*this, evt, *trackcol, ptrvs, *tcassn);
237 
238  // assume the input tracks were previously associated with hits
239  hits.clear();
240  for (size_t p = 0; p < ptrvs.size(); ++p) {
241  hits = fmh.at(idxs[p]);
242  util::CreateAssn(*this, evt, *trackcol, hits, *thassn);
243  }
244 
245  // associate the track to the space points
246  util::CreateAssn(*this, evt, *trackcol, *spcol, *tspassn, spStart, spEnd);
247 
248  mf::LogInfo("TrackCheater") << "adding track: \n" << trackcol->back() << "\nto collection.";
249 
250  } //end if this is a track
251 
252  } // end loop over the map
253 
254  evt.put(std::move(trackcol));
255  evt.put(std::move(spcol));
256  evt.put(std::move(tcassn));
257  evt.put(std::move(thassn));
258  evt.put(std::move(tspassn));
259 
260  return;
261 
262  } // end produce
263 
264 } // end namespace
265 
266 namespace trkf {
267 
268  DEFINE_ART_MODULE(TrackCheater)
269 
270 }
std::string const fCheatedClusterLabel
ROOT::Math::SMatrix< Double32_t, 5, 5, ROOT::Math::MatRepSym< Double32_t, 5 > > SMatrixSym55
Definition: TrackingTypes.h:85
std::vector< Vector_t > convertCollToVector(std::vector< Vector > const &coll)
Definition: TrackingTypes.h:58
Declaration of signal hit object.
pdgs p
Definition: selectors.fcl:22
TrackTrajectory::Flags_t Flags_t
TrackCheater(fhicl::ParameterSet const &pset)
T abs(T value)
A trajectory in space reconstructed from hits.
std::vector< Point_t > convertCollToPoint(std::vector< Point > const &coll)
Definition: TrackingTypes.h:55
Declaration of cluster object.
Provides recob::Track data product.
bool CreateAssn(art::Event &evt, std::vector< T > const &a, art::Ptr< U > const &b, art::Assns< U, T > &assn, std::string a_instance, size_t index=UINT_MAX)
Creates a single one-to-one association.
int sign(double val)
Definition: UtilFunc.cxx:104
TCEvent evt
Definition: DataStructs.cxx:8
void produce(art::Event &evt) override
std::string const fG4ModuleLabel
label for module running G4 and making particles, etc
art framework interface to geometry description
Track from a non-cascading particle.A recob::Track consists of a recob::TrackTrajectory, plus additional members relevant for a &quot;fitted&quot; track:
Signal from collection planes.
Definition: geo_types.h:146