33 greaterThan1(PlnLen p1, PlnLen p2)
35 return p1.length > p2.length;
43 fSPTAlg = pset.
get<
int>(
"SPTAlg", 0);
44 fTrajOnly = pset.
get<
bool>(
"TrajOnly",
false);
45 ftmatch = pset.
get<
double>(
"TMatch");
46 fsmatch = pset.
get<
double>(
"SMatch");
62 if (fSPTAlg == 0) { TrackTrajectory(detProp, fHits); }
63 else if (fSPTAlg == 1) {
64 Track3D(clockData, detProp, fHits);
68 <<
"Unknown SPTAlg " << fSPTAlg <<
", needs to be 0 or 1";
71 if (!fTrajOnly && trajPos.size())
72 MakeSPT(clockData, detProp, fHits);
85 std::array<std::vector<geo::WireID>, 3> trkWID;
86 std::array<std::vector<double>, 3> trkX;
87 std::array<std::vector<double>, 3> trkXErr;
89 std::array<std::vector<art::Ptr<recob::Hit>>, 3> trajHits;
91 for (
size_t i = 0; i < fHits.size(); ++i) {
92 trajHits[fHits[i]->WireID().Plane].push_back(fHits[i]);
95 std::vector<PlnLen> spl;
97 for (
unsigned int ipl = 0; ipl < 3; ++ipl) {
99 plnlen.length = trajHits[ipl].size();
100 spl.push_back(plnlen);
102 std::sort(spl.begin(), spl.end(), greaterThan1);
105 for (
size_t ipl = 0; ipl < 3; ++ipl) {
106 if (!trajHits[ipl].
size())
continue;
118 trajHits[ipl][0]->
WireID().Cryostat);
119 double xend1 = detProp.
ConvertTicksToX(trajHits[ipl].back()->PeakTime(),
122 trajHits[ipl].back()->
WireID().Cryostat);
133 for (
size_t ipl = 0; ipl < 3; ++ipl) {
135 trkWID[ipl].resize(trajHits[ipl].
size());
136 trkX[ipl].resize(trajHits[ipl].
size());
137 trkXErr[ipl].resize(trajHits[ipl].
size());
138 for (
size_t iht = 0; iht < trajHits[ipl].size(); ++iht) {
139 trkWID[ipl][iht] = trajHits[ipl][iht]->WireID();
140 trkX[ipl][iht] = detProp.
ConvertTicksToX(trajHits[ipl][iht]->PeakTime(),
143 trajHits[ipl][iht]->
WireID().Cryostat);
144 trkXErr[ipl][iht] = 0.2 * trajHits[ipl][iht]->RMS() * trajHits[ipl][iht]->Multiplicity();
147 fTrackTrajectoryAlg.TrackTrajectory(trkWID, trkX, trkXErr, trajPos, trajDir);
148 if (!trajPos.size() || !trajDir.size()) {
149 mf::LogWarning(
"CosmicTrackerAlg") <<
"Failed to reconstruct trajectory points.";
153 for (
auto ipos = trajPos.begin(), idir = trajDir.begin(); ipos != trajPos.end() - 1;) {
154 auto ipos1 = ipos + 1;
155 if (*ipos1 == *ipos) {
156 ipos = trajPos.erase(ipos);
157 idir = trajDir.erase(idir);
167 vw.resize(geom->Ncryostats());
168 vt.resize(geom->Ncryostats());
169 vtraj.resize(geom->Ncryostats());
170 for (
size_t cstat = 0; cstat < geom->Ncryostats(); ++cstat) {
171 vw[cstat].resize(geom->Cryostat(cstat).NTPC());
172 vt[cstat].resize(geom->Cryostat(cstat).NTPC());
173 vtraj[cstat].resize(geom->Cryostat(cstat).NTPC());
174 for (
size_t tpc = 0; tpc < geom->Cryostat(cstat).NTPC(); ++tpc) {
175 const geo::TPCGeo& tpcgeom = geom->Cryostat(cstat).TPC(tpc);
176 vw[cstat][tpc].resize(tpcgeom.
Nplanes());
177 vt[cstat][tpc].resize(tpcgeom.
Nplanes());
178 vtraj[cstat][tpc].resize(tpcgeom.
Nplanes());
179 for (
size_t plane = 0; plane < tpcgeom.
Nplanes(); ++plane) {
180 for (
size_t i = 0; i < trajPos.size(); ++i) {
182 geom->WireCoordinate(trajPos[i].
Y(), trajPos[i].
Z(), plane, tpc, cstat);
184 vw[cstat][tpc][plane].push_back(wirecord);
185 vt[cstat][tpc][plane].push_back(tick);
186 vtraj[cstat][tpc][plane].push_back(i);
199 larprop = lar::providerFrom<detinfo::LArPropertiesService>();
202 std::vector<std::map<int, double>> vtimemap(3);
203 std::vector<std::map<int, art::Ptr<recob::Hit>>> vhitmap(3);
206 for (
size_t ihit = 0; ihit < fHits.size(); ++ihit) {
208 unsigned int w = hitWireID.
Wire;
209 unsigned int pl = hitWireID.
Plane;
210 double time = fHits[ihit]->PeakTime();
213 vtimemap[pl][
w] = time;
214 vhitmap[pl][
w] = fHits[ihit];
221 unsigned maxnumhits0 = 0;
222 unsigned maxnumhits1 = 0;
224 std::vector<double> tmin(vtimemap.size());
225 std::vector<double> tmax(vtimemap.size());
226 for (
size_t iclu = 0; iclu < vtimemap.size(); ++iclu) {
231 for (
size_t iclu = 0; iclu < vtimemap.size(); ++iclu) {
232 for (
auto itime = vtimemap[iclu].
begin(); itime != vtimemap[iclu].end(); ++itime) {
233 if (itime->second > tmax[iclu]) { tmax[iclu] = itime->second; }
234 if (itime->second < tmin[iclu]) { tmin[iclu] = itime->second; }
236 if (vtimemap[iclu].
size() > maxnumhits0) {
239 maxnumhits1 = maxnumhits0;
242 maxnumhits0 = vtimemap[iclu].size();
244 else if (vtimemap[iclu].
size() > maxnumhits1) {
246 maxnumhits1 = vtimemap[iclu].size();
253 for (
int iclu = 0; iclu < (
int)vtimemap.size(); ++iclu) {
254 if (iclu != iclu1 && iclu != iclu2) iclu3 = iclu;
257 if (iclu1 != -1 && iclu2 != -1) {
259 auto ihit = vhitmap[iclu1].begin();
260 auto itime = vtimemap[iclu1].begin();
261 while (itime != vtimemap[iclu1].
end()) {
262 if (itime->second <
std::max(tmin[iclu1], tmin[iclu2]) - ftmatch ||
263 itime->second >
std::min(tmax[iclu1], tmax[iclu2]) + ftmatch) {
264 vtimemap[iclu1].erase(itime++);
265 vhitmap[iclu1].erase(ihit++);
273 ihit = vhitmap[iclu2].begin();
274 itime = vtimemap[iclu2].begin();
275 while (itime != vtimemap[iclu2].
end()) {
276 if (itime->second <
std::max(tmin[iclu1], tmin[iclu2]) - ftmatch ||
277 itime->second >
std::min(tmax[iclu1], tmax[iclu2]) + ftmatch) {
278 vtimemap[iclu2].erase(itime++);
279 vhitmap[iclu2].erase(ihit++);
288 if (!vtimemap[iclu1].
size()) {
289 if (iclu3 != -1) {
std::swap(iclu3, iclu1); }
291 if (!vtimemap[iclu2].
size()) {
297 if ((!vtimemap[iclu1].
size()) || (!vtimemap[iclu2].
size()))
return;
300 auto times1 = vtimemap[iclu1].begin();
301 auto timee1 = vtimemap[iclu1].end();
303 auto times2 = vtimemap[iclu2].begin();
304 auto timee2 = vtimemap[iclu2].end();
307 double ts1 = times1->second;
308 double te1 = timee1->second;
309 double ts2 = times2->second;
310 double te2 = timee2->second;
318 double Efield_drift = detProp.
Efield(0);
322 Efield_drift, Temperature);
323 double timepitch = driftvelocity * timetick;
326 geom->WirePitch(vhitmap[0].
begin()->
second->WireID().Plane,
327 vhitmap[0].begin()->second->WireID().TPC,
328 vhitmap[0].begin()->second->WireID().Cryostat);
331 std::vector<double> vtracklength;
332 for (
size_t iclu = 0; iclu < vtimemap.size(); ++iclu) {
334 double tracklength = 0;
335 if (vtimemap[iclu].
size() == 1) { tracklength = wire_pitch; }
336 else if (!vtimemap[iclu].
empty()) {
338 wend = vtimemap[iclu].cend();
339 double t0 = iw->first, w0 = iw->second;
340 while (++iw != wend) {
341 tracklength +=
std::hypot((iw->first - w0) * wire_pitch, (iw->second - t0) * timepitch);
346 vtracklength.push_back(tracklength);
349 std::map<int, int> maxhitsMatch;
351 auto ihit1 = vhitmap[iclu1].begin();
352 for (
auto itime1 = vtimemap[iclu1].
begin(); itime1 != vtimemap[iclu1].end();
354 std::vector<art::Ptr<recob::Hit>> sp_hits;
355 sp_hits.push_back(ihit1->second);
356 double hitcoord[3] = {0., 0., 0.};
358 if (vtimemap[iclu1].
size() == 1) { length1 = wire_pitch; }
360 for (
auto iw1 = vtimemap[iclu1].
begin(); iw1 != itime1; ++iw1) {
363 length1 +=
std::hypot((iw1->first - iw2->first) * wire_pitch,
364 (iw1->second - iw2->second) * timepitch);
367 double difference = 1e10;
368 auto matchedtime = vtimemap[iclu2].end();
369 auto matchedhit = vhitmap[iclu2].end();
371 auto ihit2 = vhitmap[iclu2].begin();
372 for (
auto itime2 = vtimemap[iclu2].
begin(); itime2 != vtimemap[iclu2].end();
374 if (maxhitsMatch[itime2->first])
continue;
376 if (vtimemap[iclu2].
size() == 1) { length2 = wire_pitch; }
378 for (
auto iw1 = vtimemap[iclu2].
begin(); iw1 != itime2; ++iw1) {
381 length2 +=
std::hypot((iw1->first - iw2->first) * wire_pitch,
382 (iw1->second - iw2->second) * timepitch);
385 if (rev) length2 = vtracklength[iclu2] - length2;
386 length2 = vtracklength[iclu1] / vtracklength[iclu2] * length2;
387 bool timematch =
std::abs(itime1->second - itime2->second) < ftmatch;
388 if (timematch &&
std::abs(length2 - length1) < difference) {
389 difference =
std::abs(length2 - length1);
390 matchedtime = itime2;
394 if (difference < fsmatch) {
396 matchedtime->second + detProp.
GetXTicksOffset((matchedhit->second)->WireID().Plane,
397 (matchedhit->second)->
WireID().TPC,
398 (matchedhit->second)->
WireID().Cryostat),
399 (matchedhit->second)->WireID().Plane,
400 (matchedhit->second)->
WireID().TPC,
401 (matchedhit->second)->
WireID().Cryostat);
412 bool sameTpcOrNot = geom->WireIDsIntersect(c1, c2, tmpWIDI);
415 hitcoord[1] = tmpWIDI.
y;
416 hitcoord[2] = tmpWIDI.
z;
419 if (hitcoord[1] > -1e9 && hitcoord[2] > -1e9) {
420 maxhitsMatch[matchedtime->first] = 1;
421 sp_hits.push_back(matchedhit->second);
424 if (sp_hits.size() > 1) {
425 trajPos.push_back(TVector3(hitcoord));
426 trajHit.push_back(sp_hits);
438 larprop = lar::providerFrom<detinfo::LArPropertiesService>();
441 double Efield_drift = detProp.
Efield(0);
443 double driftvelocity =
445 double time_pitch = driftvelocity * timetick;
447 for (
size_t i = 0; i < fHits.size(); ++i) {
454 double mindis1 = 1e9;
455 double mindis2 = 1e9;
456 double mindis2p = 1e9;
458 unsigned int wire = wireid.
Wire;
459 unsigned int plane = wireid.
Plane;
460 unsigned int tpc = wireid.
TPC;
461 unsigned int cstat = wireid.
Cryostat;
462 double wire_pitch = geom->WirePitch(plane, tpc, cstat);
465 for (
size_t j = 0; j < vw[cstat][tpc][plane].size(); ++j) {
466 double dis =
std::hypot(wire_pitch * (wire - vw[cstat][tpc][plane][j]),
467 time_pitch * (fHits[i]->PeakTime() - vt[cstat][tpc][plane][j]));
470 ip1 = vtraj[cstat][tpc][plane][j];
475 for (
size_t j = 0; j < vw[cstat][tpc][plane].size(); ++j) {
476 if (
int(j) == ih1)
continue;
477 double dis =
std::hypot(wire_pitch * (wire - vw[cstat][tpc][plane][j]),
478 time_pitch * (fHits[i]->PeakTime() - vt[cstat][tpc][plane][j]));
481 ip2 = vtraj[cstat][tpc][plane][j];
484 TVector3 v1(wire_pitch * (wire - vw[cstat][tpc][plane][ih1]),
485 time_pitch * (fHits[i]->PeakTime() - vt[cstat][tpc][plane][j]),
487 TVector3 v2(wire_pitch * (wire - vw[cstat][tpc][plane][j]),
488 time_pitch * (fHits[i]->PeakTime() - vt[cstat][tpc][plane][j]),
490 if (v1.Dot(v2) < 0) {
491 if (dis < mindis2p) {
493 ip2p = vtraj[cstat][tpc][plane][j];
502 if (ip1 == -1 || ip2 == -1) {
504 throw cet::exception(
"CosmicTrackerAlg") <<
"Cannot find two nearest trajectory points.";
506 TVector3 v1(wire_pitch * (wire - vw[cstat][tpc][plane][ih1]),
507 time_pitch * (fHits[i]->PeakTime() - vt[cstat][tpc][plane][ih1]),
509 TVector3 v2(wire_pitch * (vw[cstat][tpc][plane][ih2] - vw[cstat][tpc][plane][ih1]),
510 time_pitch * (vt[cstat][tpc][plane][ih2] - vt[cstat][tpc][plane][ih1]),
513 throw cet::exception(
"CosmicTrackerAlg") <<
"Two identical trajectory points.";
515 double ratio = v1.Dot(v2) / v2.Mag2();
516 TVector3
pos = trajPos[ip1] + ratio * (trajPos[ip2] - trajPos[ip1]);
517 trkPos.push_back(pos);
518 if (trajDir[ip1].Mag()) { trkDir.push_back(trajDir[ip1]); }
519 else if (trajDir[ip2].Mag()) {
520 trkDir.push_back(trajDir[ip2]);
523 trkDir.push_back((trajPos[ip2] - trajPos[ip1]).Unit());
end
while True: pbar.update(maxval-len(onlies[E][S])) #print iS, "/", len(onlies[E][S]) found = False for...
code to link reconstructed objects back to the MC truth information
double z
z position of intersection
Encapsulate the construction of a single cyostat.
double GetXTicksOffset(int p, int t, int c) const
unsigned int Nplanes() const
Number of planes in this tpc.
Geometry information for a single TPC.
double Temperature() const
In kelvin.
CryostatID_t Cryostat
Index of cryostat.
WireID_t Wire
Index of the wire within its plane.
art framework interface to geometry description
std::enable_if_t< std::is_arithmetic_v< T >, T > hypot(T x, T y)
double Efield(unsigned int planegap=0) const
kV/cm
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
IDparameter< geo::WireID > WireID
Member type of validated geo::WireID parameter.
void swap(Handle< T > &a, Handle< T > &b)
T get(std::string const &key) const
tick_as<> tick
Tick number, represented by std::ptrdiff_t.
double ConvertXToTicks(double X, int p, int t, int c) const
static int max(int a, int b)
double DriftVelocity(double efield=0., double temperature=0.) const
cm/us
PlaneID_t Plane
Index of the plane within its TPC.
Definition of data types for geometry description.
Class defining a plane for tracking. It provides various functionalities to convert track parameters ...
double ConvertTicksToX(double ticks, int p, int t, int c) const
Declaration of signal hit object.
T min(sqlite3 *const db, std::string const &table_name, std::string const &column_name)
Contains all timing reference information for the detector.
double y
y position of intersection
static unsigned int reverse(QString &chars, unsigned char *level, unsigned int a, unsigned int b)
MaybeLogger_< ELseverityLevel::ELsev_warning, false > LogWarning
constexpr WireID()=default
Default constructor: an invalid TPC ID.
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
TPCID_t TPC
Index of the TPC within its cryostat.
second_as<> second
Type of time stored in seconds, in double precision.
double sampling_rate(DetectorClocksData const &data)
Returns the period of the TPC readout electronics clock.
cet::coded_exception< error, detail::translate > exception
decltype(auto) constexpr empty(T &&obj)
ADL-aware version of std::empty.
Encapsulate the construction of a single detector plane.