LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
DBCluster3D_module.cc
Go to the documentation of this file.
1 // Class: DBCluster3D
3 // Plugin Type: producer (art v2_11_02)
4 // File: DBCluster3D_module.cc
5 //
6 // Generated at Sat Jun 16 07:43:32 2018 by Tingjun Yang using cetskelgen
7 // from cetlib version v3_03_01.
9 
17 #include "fhiclcpp/ParameterSet.h"
19 
29 
30 #include <memory>
31 
32 namespace cluster {
33  class DBCluster3D;
34 }
35 
36 
38 
41 
42 public:
43  explicit DBCluster3D(fhicl::ParameterSet const & p);
44  // The compiler-generated destructor is fine for non-base
45  // classes without bare pointers or other resource use.
46 
47  // Plugins should not be copied or assigned.
48  DBCluster3D(DBCluster3D const &) = delete;
49  DBCluster3D(DBCluster3D &&) = delete;
50  DBCluster3D & operator = (DBCluster3D const &) = delete;
51  DBCluster3D & operator = (DBCluster3D &&) = delete;
52 
53  // Required functions.
54  void produce(art::Event & e) override;
55 
56 private:
57 
61 
63 
66 
67  double tickToDist;
68  double fMinHitDis;
69 
70 };
71 
72 
74  : fHitModuleLabel(p.get< art::InputTag >("HitModuleLabel"))
75  , fSpacePointModuleLabel(p.get< art::InputTag >("SpacePointModuleLabel"))
76  , fSPHitAssnLabel(p.get< art::InputTag >("SPHitAssnLabel"))
77  , fDBScan(p.get< fhicl::ParameterSet >("DBScan3DAlg"))
78  , fMinHitDis(p.get< double >("MinHitDis"))
79 {
80  produces< std::vector<recob::Slice> >();
81  produces< art::Assns<recob::Slice, recob::Hit> >();
82  produces< art::Assns<recob::Slice, recob::SpacePoint> >();
83 
85  fDetProp = lar::providerFrom<detinfo::DetectorPropertiesService>();
86 
88  tickToDist *= 1.e-3 * fDetProp->SamplingRate(); // 1e-3 is conversion of 1/us to 1/ns
90 }
91 
93 {
94 
95  std::vector<recob::Slice> slcCol;
96 
97  std::unique_ptr<art::Assns<recob::Slice, recob::Hit>>
98  slc_hit_assn(new art::Assns<recob::Slice, recob::Hit>);
99 
100  std::unique_ptr<art::Assns<recob::Slice, recob::SpacePoint>>
102 
103  auto hitsHandle = evt.getValidHandle< std::vector<recob::Hit> >(fHitModuleLabel);
104  auto spsHandle = evt.getValidHandle< std::vector<recob::SpacePoint> >(fSpacePointModuleLabel);
105 
106  // all hits in the collection
107  std::vector< art::Ptr<recob::Hit> > hits;
108  art::fill_ptr_vector(hits, hitsHandle);
109 
110  // all space points in the collection
111  std::vector< art::Ptr<recob::SpacePoint> > sps;
112  art::fill_ptr_vector(sps, spsHandle);
113 
114  art::FindManyP< recob::SpacePoint > spFromHit(hitsHandle, evt, fSPHitAssnLabel);
115  if(!spFromHit.isValid()) {
116  std::cout<<"spFromHit is invalid\n";
117  return;
118  }
119  // Find the first Hit - SpacePoint assn and check consistency on the first event
120  static bool first = true;
121  if(first) {
122  bool success = false;
123  for(auto &hit : hits) {
124  auto &sps = spFromHit.at(hit.key());
125  if(sps.empty()) continue;
126  success = (sps[0].id() == spsHandle.id());
127  break;
128  } // hit
129  if(!success) throw cet::exception("DBCluster3D")<<"HitModuleLabel, SpacePointModuleLabel and SPHitAssnLabel are inconsistent\n";
130  first = false;
131  } // first
132 
133  art::FindManyP< recob::Hit > hitFromSp(spsHandle, evt, fSPHitAssnLabel);
134  if(!hitFromSp.isValid()) {
135  std::cout<<"hitFromSp is invalid\n";
136  return;
137  }
138  fDBScan.init(sps, hitFromSp);
139  fDBScan.dbscan();
140 
141  //Find number of slices
142  int maxid = INT_MIN;
143  for (size_t i = 0; i<fDBScan.points.size(); ++i){
144 // std::cout<<"Space point index "<<i<<" "<<fDBScan.points[i].sp.key()<<" "<<fDBScan.points[i].cluster_id<<std::endl;
145  if (fDBScan.points[i].cluster_id > maxid) maxid = fDBScan.points[i].cluster_id;
146  }
147  size_t nslc = 0;
148  if (maxid>=0) nslc = maxid + 1;
149 
150  //Save hits associated with each slice
151  std::vector<std::vector<art::Ptr<recob::Hit>>> slcHits(nslc);
152  //Save hits on each PlaneID with pfparticle index
153  std::map<geo::PlaneID, std::vector<std::pair<art::Ptr<recob::Hit>, unsigned int>>> hitmap;
154  for (auto &hit : hits){
155  auto &sps = spFromHit.at(hit.key());
156  if (sps.size()){//found associated space point
157  if (fDBScan.points[sps[0].key()].cluster_id>=0){
158  slcHits[fDBScan.points[sps[0].key()].cluster_id].push_back(hit);
159  hitmap[geo::PlaneID(hit->WireID())].push_back(std::make_pair(hit, fDBScan.points[sps[0].key()].cluster_id));
160  }
161  } // sps.size()
162  } // hit
163 
164  //Save hits not associated with any spacepoints
165  for (auto &hit : hits){
166  bool found = false;
167  for (size_t i = 0; i<slcHits.size(); ++i){
168  if (std::find(slcHits[i].begin(), slcHits[i].end(), hit) != slcHits[i].end()){
169  found = true;
170  break;
171  }
172  }
173  if (!found){
174  double wirePitch = fGeom->WirePitch(hit->WireID());
175  double UnitsPerTick = tickToDist / wirePitch;
176  double x0 = hit->WireID().Wire;
177  double y0 = hit->PeakTime() * UnitsPerTick;
178  double mindis = DBL_MAX;
179  unsigned slcIndex = UINT_MAX;
180  for (auto &hit2 : hitmap[geo::PlaneID(hit->WireID())]) {
181  double dx = hit2.first->WireID().Wire - x0;
182  double dy = hit2.first->PeakTime() * UnitsPerTick - y0;
183  double dis = dx*dx + dy*dy;
184  if (dis<mindis){
185  mindis = dis;
186  slcIndex = hit2.second;
187  }
188  }
189  if (slcIndex != UINT_MAX && mindis < fMinHitDis){
190  slcHits[slcIndex].push_back(hit);
191  }
192  }
193  }
194 
195  //Save spacepoints for each slice
196  std::vector<std::vector<art::Ptr<recob::SpacePoint>>> sps_in_slc(nslc);
197  for (size_t i = 0; i<fDBScan.points.size(); ++i){
198  if (fDBScan.points[i].cluster_id>=0){
199  sps_in_slc[fDBScan.points[i].cluster_id].push_back(sps[i]);
200  }
201  } // i
202 
203  // calculate the properties of the slice
204  for(size_t isl = 0; isl < nslc; ++isl) {
205  double sum = sps_in_slc[isl].size();
206  // find the center
207  double center[3] = {0.};
208  // TODO: calculate the charge using recob::PointCharge instead of just counting hits
209  float charge = slcHits[isl].size();
210  for(auto& spt : sps_in_slc[isl]) {
211  for(unsigned short xyz = 0; xyz < 3; ++xyz) center[xyz] += spt->XYZ()[xyz];
212  } // spt
213  for(unsigned short xyz = 0; xyz < 3; ++xyz) center[xyz] /= sum;
214  // do a linear fit
215  double sumx = 0, sumy = 0., sumz = 0., sumx2 = 0, sumy2 = 0, sumz2 = 0;
216  double sumxy = 0, sumxz = 0;
217  for(auto& spt : sps_in_slc[isl]) {
218  double xx = spt->XYZ()[0] - center[0];
219  double yy = spt->XYZ()[1] - center[1];
220  double zz = spt->XYZ()[2] - center[2];
221  sumx += xx;
222  sumy += yy;
223  sumz += zz;
224  sumx2 += xx * xx;
225  sumy2 += yy * yy;
226  sumz2 += zz * zz;
227  sumxy += xx * yy;
228  sumxz += xx * zz;
229  } // spt
230  double delta = sum * sumx2 - sumx * sumx;
231  if(delta <= 0) continue;
232  // calculate the slopes
233  double dydx = (sumxy * sum - sumx * sumy) / delta;
234  double dzdx = (sumxz * sum - sumx * sumz) / delta;
235  // and convert to direction vector
236  double direction[3];
237  double norm = std::sqrt(1 + dydx * dydx + dzdx * dzdx);
238  direction[0] = 1 / norm;
239  direction[1] = dydx / norm;
240  direction[2] = dzdx / norm;
241  // Find the points that are farthest away from the center along the slice axis
242  unsigned int imax = 0, imin = 0;
243  float minAlong = 1E6, maxAlong = -1E6;
244  double tmpVec[3];
245  for(unsigned int ipt = 0; ipt < sps_in_slc[isl].size(); ++ipt) {
246  auto& spt = sps_in_slc[isl][ipt];
247  for(unsigned short xyz = 0; xyz < 3; ++xyz) tmpVec[xyz] = spt->XYZ()[xyz] - center[xyz];
248  double dotp = 0;
249  for(unsigned short xyz = 0; xyz < 3; ++xyz) dotp += tmpVec[xyz] * direction[xyz];
250  if(dotp < minAlong) { minAlong = dotp; imin = ipt; }
251  if(dotp > maxAlong) { maxAlong = dotp; imax = ipt; }
252  } // spt
253  // Find the aspect ratio
254  float cnt = 0.;
255  float aspectRatio = 0;
256  double arg = sum * sumy2 - sumy * sumy;
257  if(arg > 0) {
258  aspectRatio += std::abs(sum * sumxy - sumx * sumy) / sqrt(delta * arg);
259  ++cnt;
260  }
261  arg = sum * sumz2 - sumz * sumz;
262  if(arg > 0) {
263  aspectRatio += std::abs(sum * sumxz - sumx * sumz) / sqrt(delta * arg);
264  ++cnt;
265  }
266  if(cnt > 1) aspectRatio /= cnt;
267  int id = isl + 1;
268  Point_t ctr(center[0], center[1], center[2]);
269  Vector_t dir(direction[0], direction[1], direction[2]);
270  auto pos0 = sps_in_slc[isl][imin]->XYZ();
271  Point_t ep0(pos0[0], pos0[1], pos0[2]);
272  auto pos1 = sps_in_slc[isl][imax]->XYZ();
273  Point_t ep1(pos1[0], pos1[1], pos1[2]);
274  slcCol.emplace_back(id, ctr, dir, ep0, ep1, aspectRatio, charge);
275  util::CreateAssn(*this, evt, slcCol, slcHits[isl], *slc_hit_assn);
276  util::CreateAssn(*this, evt, slcCol, sps_in_slc[isl], *slc_sps_assn);
277  } // isl
278 
279  std::unique_ptr<std::vector<recob::Slice> > scol(new std::vector<recob::Slice>(std::move(slcCol)));
280  evt.put(std::move(scol));
281  evt.put(std::move(slc_hit_assn));
282  evt.put(std::move(slc_sps_assn));
283 }
284 
geo::GeometryCore const * fGeom
DBCluster3D & operator=(DBCluster3D const &)=delete
Double_t xx
Definition: macro.C:12
Declaration of signal hit object.
The data type to uniquely identify a Plane.
Definition: geo_types.h:250
virtual double SamplingRate() const =0
Returns the period of the TPC readout electronics clock.
Double_t zz
Definition: plot.C:279
const art::InputTag fSpacePointModuleLabel
Cluster finding and building.
const art::InputTag fHitModuleLabel
const art::InputTag fSPHitAssnLabel
geo::Length_t WirePitch(geo::PlaneID const &planeid) const
Returns the distance between two consecutive wires.
ProductID put(std::unique_ptr< PROD > &&product)
Definition: Event.h:102
ROOT::Math::DisplacementVector3D< ROOT::Math::Cartesian3D< Coord_t >, ROOT::Math::GlobalCoordinateSystemTag > Vector_t
Type for representation of momenta in 3D space. See recob::tracking::Coord_t for more details on the ...
Definition: TrackingTypes.h:29
recob::tracking::Vector_t Vector_t
void hits()
Definition: readHits.C:15
DBCluster3D(fhicl::ParameterSet const &p)
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:42
void produce(art::Event &e) override
void init(const std::vector< art::Ptr< recob::SpacePoint >> &sps, art::FindManyP< recob::Hit > &hitFromSp)
Definition: DBScan3DAlg.cxx:29
parameter set interface
virtual double Temperature() const =0
std::vector< evd::details::RawDigitInfo_t >::const_iterator begin(RawDigitCacheDataClass const &cache)
bool CreateAssn(PRODUCER const &prod, art::Event &evt, std::vector< T > const &a, art::Ptr< U > const &b, art::Assns< U, T > &assn, std::string a_instance, size_t indx=UINT_MAX)
Creates a single one-to-one association.
detinfo::DetectorProperties const * fDetProp
Description of geometry of one entire detector.
Declaration of cluster object.
recob::tracking::Point_t Point_t
Detector simulation of raw signals on wires.
Utility object to perform functions of association.
Float_t norm
TDirectory * dir
Definition: macro.C:5
virtual double DriftVelocity(double efield=0., double temperature=0.) const =0
HLT enums.
std::vector< evd::details::RawDigitInfo_t >::const_iterator end(RawDigitCacheDataClass const &cache)
std::vector< point_t > points
Definition: DBScan3DAlg.h:82
virtual double Efield(unsigned int planegap=0) const =0
Returns the nominal electric field in the specified volume.
TCEvent evt
Definition: DataStructs.cxx:5
ValidHandle< PROD > getValidHandle(InputTag const &tag) const
void fill_ptr_vector(std::vector< Ptr< T >> &ptrs, H const &h)
Definition: Ptr.h:464
Float_t e
Definition: plot.C:34
ROOT::Math::PositionVector3D< ROOT::Math::Cartesian3D< Coord_t >, ROOT::Math::GlobalCoordinateSystemTag > Point_t
Type for representation of position in physical 3D space. See recob::tracking::Coord_t for more detai...
Definition: TrackingTypes.h:26
art framework interface to geometry description
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33