10 #include "art/Framework/Core/EDProducer.h"
11 #include "art/Framework/Core/ModuleMacros.h"
12 #include "art/Framework/Principal/Event.h"
13 #include "art/Framework/Principal/Handle.h"
14 #include "art/Framework/Services/Registry/ServiceHandle.h"
15 #include "canvas/Utilities/InputTag.h"
16 #include "fhiclcpp/ParameterSet.h"
51 void produce(art::Event&
e)
override;
67 , fHitModuleLabel(
p.get<art::InputTag>(
"HitModuleLabel"))
68 , fSpacePointModuleLabel(
p.get<art::InputTag>(
"SpacePointModuleLabel"))
69 , fSPHitAssnLabel(
p.get<art::InputTag>(
"SPHitAssnLabel"))
70 , fDBScan(
p.get<fhicl::ParameterSet>(
"DBScan3DAlg"))
71 , fMinHitDis(
p.get<
double>(
"MinHitDis"))
73 produces<std::vector<recob::Slice>>();
74 produces<art::Assns<recob::Slice, recob::Hit>>();
75 produces<art::Assns<recob::Slice, recob::SpacePoint>>();
77 fGeom = art::ServiceHandle<geo::Geometry const>().
get();
78 auto const clock_data = art::ServiceHandle<detinfo::DetectorClocksService const>()->DataForJob();
80 art::ServiceHandle<detinfo::DetectorPropertiesService const>()->DataForJob(clock_data);
82 tickToDist = det_prop.DriftVelocity(det_prop.Efield(), det_prop.Temperature());
84 fMinHitDis *= fMinHitDis;
90 auto scol = std::make_unique<std::vector<recob::Slice>>();
91 auto slc_hit_assn = std::make_unique<art::Assns<recob::Slice, recob::Hit>>();
92 auto slc_sps_assn = std::make_unique<art::Assns<recob::Slice, recob::SpacePoint>>();
94 auto hitsHandle = evt.getValidHandle<std::vector<recob::Hit>>(fHitModuleLabel);
95 auto spsHandle = evt.getValidHandle<std::vector<recob::SpacePoint>>(fSpacePointModuleLabel);
98 std::vector<art::Ptr<recob::Hit>> hits;
99 art::fill_ptr_vector(hits, hitsHandle);
102 std::vector<art::Ptr<recob::SpacePoint>> sps;
103 art::fill_ptr_vector(sps, spsHandle);
105 art::FindManyP<recob::SpacePoint> spFromHit(hitsHandle, evt, fSPHitAssnLabel);
106 if (!spFromHit.isValid()) {
111 static bool first =
true;
113 bool success =
false;
114 bool foundsps =
false;
115 for (
auto&
hit : hits) {
116 auto& sps = spFromHit.at(
hit.key());
117 if (sps.empty())
continue;
118 success = (sps[0].id() == spsHandle.id());
122 if ((!success) && foundsps)
123 throw cet::exception(
"DBCluster3D")
124 <<
"HitModuleLabel, SpacePointModuleLabel and SPHitAssnLabel are inconsistent\n";
128 art::FindManyP<recob::Hit> hitFromSp(spsHandle, evt, fSPHitAssnLabel);
129 if (!hitFromSp.isValid()) {
133 fDBScan.init(sps, hitFromSp);
138 for (
size_t i = 0; i < fDBScan.points.size(); ++i) {
139 if (fDBScan.points[i].cluster_id > maxid) maxid = fDBScan.points[i].cluster_id;
142 if (maxid >= 0) nslc = maxid + 1;
145 std::vector<std::vector<art::Ptr<recob::Hit>>> slcHits(nslc);
147 std::map<geo::PlaneID, std::vector<std::pair<art::Ptr<recob::Hit>,
unsigned int>>> hitmap;
148 for (
auto&
hit : hits) {
149 auto& sps = spFromHit.at(
hit.key());
151 if (fDBScan.points[sps[0].key()].cluster_id >= 0) {
152 slcHits[fDBScan.points[sps[0].key()].cluster_id].push_back(
hit);
154 std::make_pair(
hit, fDBScan.points[sps[0].key()].cluster_id));
160 for (
auto&
hit : hits) {
162 for (
size_t i = 0; i < slcHits.size(); ++i) {
163 if (std::find(slcHits[i].
begin(), slcHits[i].
end(),
hit) != slcHits[i].end()) {
169 double wirePitch = fGeom->WirePitch(
hit->WireID());
170 double UnitsPerTick = tickToDist / wirePitch;
171 double x0 =
hit->WireID().Wire;
172 double y0 =
hit->PeakTime() * UnitsPerTick;
173 double mindis = DBL_MAX;
174 unsigned slcIndex = UINT_MAX;
176 double dx = hit2.first->WireID().Wire - x0;
177 double dy = hit2.first->PeakTime() * UnitsPerTick - y0;
178 double dis = dx * dx + dy * dy;
181 slcIndex = hit2.second;
184 if (slcIndex != UINT_MAX && mindis < fMinHitDis) { slcHits[slcIndex].push_back(
hit); }
189 std::vector<std::vector<art::Ptr<recob::SpacePoint>>> sps_in_slc(nslc);
190 for (
size_t i = 0; i < fDBScan.points.size(); ++i) {
191 if (fDBScan.points[i].cluster_id >= 0) {
192 sps_in_slc[fDBScan.points[i].cluster_id].push_back(sps[i]);
197 for (
size_t isl = 0; isl < nslc; ++isl) {
198 double sum = sps_in_slc[isl].size();
200 double center[3] = {0.};
202 float charge = slcHits[isl].size();
203 for (
auto& spt : sps_in_slc[isl]) {
204 for (
unsigned short xyz = 0; xyz < 3; ++xyz)
205 center[xyz] += spt->XYZ()[xyz];
207 for (
unsigned short xyz = 0; xyz < 3; ++xyz)
210 double sumx = 0, sumy = 0., sumz = 0., sumx2 = 0, sumy2 = 0, sumz2 = 0;
211 double sumxy = 0, sumxz = 0;
212 for (
auto& spt : sps_in_slc[isl]) {
213 double xx = spt->XYZ()[0] - center[0];
214 double yy = spt->XYZ()[1] - center[1];
215 double zz = spt->XYZ()[2] - center[2];
225 double delta = sum * sumx2 - sumx * sumx;
226 if (delta <= 0)
continue;
228 double dydx = (sumxy * sum - sumx * sumy) / delta;
229 double dzdx = (sumxz * sum - sumx * sumz) / delta;
232 double norm = std::sqrt(1 + dydx * dydx + dzdx * dzdx);
233 direction[0] = 1 /
norm;
234 direction[1] = dydx /
norm;
235 direction[2] = dzdx /
norm;
237 unsigned int imax = 0, imin = 0;
238 float minAlong = 1E6, maxAlong = -1E6;
240 for (
unsigned int ipt = 0; ipt < sps_in_slc[isl].size(); ++ipt) {
241 auto& spt = sps_in_slc[isl][ipt];
242 for (
unsigned short xyz = 0; xyz < 3; ++xyz)
243 tmpVec[xyz] = spt->XYZ()[xyz] - center[xyz];
245 for (
unsigned short xyz = 0; xyz < 3; ++xyz)
246 dotp += tmpVec[xyz] * direction[xyz];
247 if (dotp < minAlong) {
251 if (dotp > maxAlong) {
258 float aspectRatio = 0;
259 double arg = sum * sumy2 - sumy * sumy;
261 aspectRatio +=
std::abs(sum * sumxy - sumx * sumy) / sqrt(delta * arg);
264 arg = sum * sumz2 - sumz * sumz;
266 aspectRatio +=
std::abs(sum * sumxz - sumx * sumz) / sqrt(delta * arg);
269 if (cnt > 1) aspectRatio /= cnt;
271 Point_t ctr(center[0], center[1], center[2]);
272 Vector_t dir(direction[0], direction[1], direction[2]);
273 auto pos0 = sps_in_slc[isl][imin]->XYZ();
274 Point_t ep0(pos0[0], pos0[1], pos0[2]);
275 auto pos1 = sps_in_slc[isl][imax]->XYZ();
276 Point_t ep1(pos1[0], pos1[1], pos1[2]);
277 scol->emplace_back(
id, ctr, dir, ep0, ep1, aspectRatio, charge);
282 evt.put(std::move(scol));
283 evt.put(std::move(slc_hit_assn));
284 evt.put(std::move(slc_sps_assn));
geo::GeometryCore const * fGeom
DBCluster3D & operator=(DBCluster3D const &)=delete
Declaration of signal hit object.
The data type to uniquely identify a Plane.
const art::InputTag fSpacePointModuleLabel
const art::InputTag fHitModuleLabel
const art::InputTag fSPHitAssnLabel
IDparameter< geo::PlaneID > PlaneID
Member type of validated geo::PlaneID parameter.
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 ...
recob::tracking::Vector_t Vector_t
DBCluster3D(fhicl::ParameterSet const &p)
void produce(art::Event &e) override
auto end(FixedBins< T, C > const &) noexcept
Description of geometry of one entire detector.
recob::tracking::Point_t Point_t
auto norm(Vector const &v)
Return norm of the specified vector.
auto begin(FixedBins< T, C > const &) noexcept
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.
double sampling_rate(DetectorClocksData const &data)
Returns the period of the TPC readout electronics clock.
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...
art framework interface to geometry description
BEGIN_PROLOG could also be cout