47 bool simplify,
bool blend,
48 double blendMassLimit)
const {
50 const std::string name = gv->getLogVol()->getName();
54 gv->getLogVol()->getShape(), ident)};
68 double volumeFromGeoShape =
76 double volume = volumeFromGeoShape >= 0 ? volumeFromGeoShape : -1.;
77 if ((simplify || blend) && volumeFromGeoShape < 0) {
92 if (constituents.size() == 1 && !constituents[0].second) {
93 return std::make_unique<TrackingVolume>(*volGeo, mat,
nullptr,
nullptr,
97 std::unique_ptr<Volume> envelope{};
98 std::string envName = name;
100 std::unique_ptr<TrackingVolume> trEnv{};
102 bool blended =
false;
104 if (constituents.size() == 1) {
106 envelope = std::make_unique<Volume>(*(constituents.front().first),
107 volGeo->transform());
110 if (blend && volume > 0 && volEnv > 0 &&
111 volume * mat.rho < blendMassLimit)
114 if ((simplify || blended) && volume > 0 && volEnv > 0) {
116 double fraction = volume / volEnv;
117 Material matScaled(mat.X0 / fraction, mat.L0 / fraction, mat.A,
118 mat.Z, fraction * mat.rho);
119 if (blend && !blended)
120 envName = envName +
"_PERM";
121 trEnv = std::make_unique<TrackingVolume>(*envelope, matScaled,
122 nullptr,
nullptr, envName);
124 auto confinedVols =std::make_unique<std::vector<TrackingVolume*>>();
125 confinedVols->push_back(std::make_unique<TrackingVolume>(*volGeo, mat,
nullptr,
nullptr, name).
release());
126 envName = name +
"_envelope";
127 trEnv = std::make_unique<TrackingVolume>(*envelope, dummyMaterial, std::move(confinedVols), envName);
135 std::unique_ptr<VolumeSpan> span =
139 for (
const auto& fv : constituents) {
142 &(fv.first->volumeBounds()));
150 <<
"envelope estimate: object contains cylinder:"
151 << name <<
":" << isCyl);
153 <<
"complex volume span for envelope:" << name
154 <<
":x range:" << (*span).xMin <<
","
157 <<
"complex volume span for envelope:" << name
158 <<
":y range:" << (*span).yMin <<
","
161 <<
"complex volume span for envelope:" << name
162 <<
":z range:" << (*span).zMin <<
","
165 <<
"complex volume span for envelope:" << name
166 <<
":R range:" << (*span).rMin <<
","
169 <<
"complex volume span for envelope:" << name
170 <<
":phi range:" << (*span).phiMin <<
","
176 0.5 * ((*span).yMin + (*span).yMax),
177 0.5 * ((*span).zMin + (*span).zMax)}};
179 std::shared_ptr<VolumeBounds> bounds =
180 std::make_shared<CuboidVolumeBounds>(
181 0.5 * ((*span).xMax - (*span).xMin),
182 0.5 * ((*span).yMax - (*span).yMin),
183 0.5 * ((*span).zMax - (*span).zMin));
184 envelope = std::make_unique<Volume>(
185 makeTransform(cylTrf), std::move(bounds));
187 double dPhi = (*span).phiMin > (*span).phiMax
188 ? (*span).phiMax - (*span).phiMin + 2 *
M_PI
189 : (*span).phiMax - (*span).phiMin;
190 std::shared_ptr<VolumeBounds> cylBounds{};
192 if (dPhi < 2 *
M_PI) {
193 double aPhi = 0.5 * ((*span).phiMax + (*span).phiMin);
194 cylBounds = std::make_shared<CylinderVolumeBounds>(
195 (*span).rMin, (*span).rMax, 0.5 * dPhi,
196 0.5 * ((*span).zMax - (*span).zMin));
199 cylBounds = std::make_shared<CylinderVolumeBounds>(
200 (*span).rMin, (*span).rMax,
201 0.5 * ((*span).zMax - (*span).zMin));
203 envelope = std::make_unique<Volume>(
204 makeTransform(cylTrf), std::move(cylBounds));
209 if (blend && volume > 0 && volEnv > 0 && volume * mat.rho < blendMassLimit)
212 if ((simplify || blended) && volume > 0 && volEnv > 0) {
213 double fraction = volume / volEnv;
214 Material matScaled(mat.X0 / fraction, mat.L0 / fraction, mat.A, mat.Z,
216 if (blend && !blended)
217 envName = envName +
"_PERM";
218 trEnv = std::make_unique<TrackingVolume>(*envelope, mat,
nullptr,
221 auto confinedVols = std::make_unique<std::vector<TrackingVolume*>>();
222 confinedVols->push_back( std::make_unique<TrackingVolume>(*volGeo, mat,
nullptr,
nullptr, name).
release());
223 envName = envName +
"_envelope";
224 trEnv = std::make_unique<TrackingVolume>(*envelope, dummyMaterial, std::move(confinedVols), envName);
235 inputVol.
parts.push_back(std::make_unique<Volume>(trVol));
236 constituents.push_back(std::move(inputVol));
237 VolumePartVec::iterator sIter = constituents.begin();
241 while (sIter != constituents.end()) {
243 for (
unsigned int ii = 0; ii < (*sIter).parts.size(); ++ii) {
244 const VolumeBounds& bounds{((*sIter).parts[ii]->volumeBounds())};
250 (*sIter).parts[ii].reset(comb->
first()->
clone());
252 constituents.push_back(vp);
253 constituents.back().parts[ii].reset(comb->
second()->
clone());
254 constituents.push_back(vp);
255 constituents.back().parts.emplace_back(comb->
second()->
clone());
256 constituents.back().sign = -1. * constituents.back().sign;
260 (*sIter).parts[ii].reset(sub->
outer()->
clone());
264 volume += -1. * (*sIter).sign * volSub;
266 constituents.emplace_back(*sIter);
267 constituents.back().parts.emplace_back(
269 constituents.back().sign = -1. * constituents.back().sign;
277 sIter=constituents.erase(sIter);
284 sIter = constituents.begin();
285 else if ((*sIter).parts.size() == 1) {
287 volume += (*sIter).sign * volSingle;
288 sIter=constituents.erase(sIter);
290 std::vector<std::shared_ptr<Volume>>
::iterator tit =
291 (*sIter).parts.begin();
292 bool noovrlp =
false;
293 while (tit + 1 != (*sIter).parts.end()) {
294 std::pair<bool, std::unique_ptr<Volume>> overlap =
296 if (overlap.first && !overlap.second) {
297 sIter=constituents.erase(sIter);
301 else if (overlap.first && overlap.second) {
302 (*sIter).parts.erase(tit, tit + 2);
303 (*sIter).parts.push_back(std::move(overlap.second));
304 tit = (*sIter).parts.begin();
307 sIter=constituents.erase(sIter);
312 sIter=constituents.erase(sIter);
316 std::pair<bool, std::unique_ptr<Volume>> overlap =
320 if (overlap.second) {
321 (*sIter).parts.erase(tit, tit + 2);
322 (*sIter).parts.push_back(std::move(overlap.second));
323 tit = (*sIter).parts.begin();
325 sIter=constituents.erase(sIter);
334 }
else if ((*sIter).parts.size() == 1) {
336 volume += (*sIter).sign * volSingle;
337 sIter=constituents.erase(sIter);
344 if (!constituents.empty()) {
346 << constituents.size() <<
" items "
347 <<
":volume estimate:" << volume);
356 constituents.emplace_back(std::make_unique<Volume>(trVol),
nullptr);
357 VolumePairVec::iterator sIter = constituents.begin();
358 std::shared_ptr<VolumeBounds> newBounds{};
359 while (sIter != constituents.end()) {
363 &((*sIter).first->volumeBounds()));
366 &((*sIter).first->volumeBounds()));
369 std::shared_ptr<Volume> subVol = (*sIter).second;
370 sIter = constituents.erase(sIter);
371 std::shared_ptr<Volume> combFirst{comb->
first()->
clone()};
372 std::shared_ptr<Volume> combSecond{comb->
second()->
clone()};
374 newBounds = std::make_shared<Trk::SubtractedVolumeBounds>(
375 std::unique_ptr<Trk::Volume>(combFirst->clone()), std::unique_ptr<Trk::Volume>(combSecond->clone()));
376 std::unique_ptr<Trk::Volume> newSubVol =
377 std::make_unique<Volume>(
nullptr, std::move(newBounds));
379 newBounds = std::make_shared<CombinedVolumeBounds>(
380 std::unique_ptr<Trk::Volume>(subVol->clone()), std::move(newSubVol),
false);
381 std::shared_ptr<Volume> newCSubVol =
382 std::make_unique<Volume>(
nullptr, std::move(newBounds));
383 constituents.insert(sIter,
384 std::make_pair(combFirst, newCSubVol));
387 sIter, std::make_pair(combFirst, std::move(newSubVol)));
390 constituents.insert(sIter, std::make_pair(combFirst, subVol));
392 newBounds = std::make_shared<CombinedVolumeBounds>(
393 std::unique_ptr<Trk::Volume>(subVol->clone()),
394 std::unique_ptr<Trk::Volume>(combFirst->clone()),
false);
395 std::unique_ptr<Trk::Volume> newSubVol =
396 std::make_unique<Volume>(
nullptr, std::move(newBounds));
399 std::make_pair(combSecond, std::move(newSubVol)));
401 constituents.insert(sIter,
402 std::make_pair(combSecond, combFirst));
405 sIter = constituents.begin();
407 std::shared_ptr<Volume> subVol = (*sIter).second;
408 sIter = constituents.erase(sIter);
409 std::shared_ptr<Volume> innerVol{sub->
inner()->
clone()};
410 std::shared_ptr<Volume> outerVol{sub->
outer()->
clone()};
412 newBounds = std::make_shared<CombinedVolumeBounds>(
413 std::unique_ptr<Trk::Volume>(subVol->clone()),
414 std::unique_ptr<Trk::Volume>(innerVol->clone()),
false);
415 std::unique_ptr<Volume> newSubVol =
416 std::make_unique<Trk::Volume>(
nullptr, newBounds);
418 sIter, std::make_pair(outerVol, std::move(newSubVol)));
420 constituents.insert(sIter, std::make_pair(outerVol, innerVol));
422 sIter = constituents.begin();
432 double zTol,
double phiTol)
const {
470 scomb.
rMin = std::min((*s1).rMin, (*s2).rMin);
471 scomb.
rMax = std::max((*s1).rMax, (*s2).rMax);
472 scomb.
xMin = std::min((*s1).xMin, (*s2).xMin);
473 scomb.
xMax = std::max((*s1).xMax, (*s2).xMax);
474 scomb.
yMin = std::min((*s1).yMin, (*s2).yMin);
475 scomb.
yMax = std::max((*s1).yMax, (*s2).yMax);
476 scomb.
zMin = std::min((*s1).zMin, (*s2).zMin);
477 scomb.
zMax = std::max((*s1).zMax, (*s2).zMax);
478 if ((*s1).phiMin < (*s1).phiMax && (*s2).phiMin < (*s2).phiMax) {
479 scomb.
phiMin = std::min((*s1).phiMin, (*s2).phiMin);
480 scomb.
phiMax = std::max((*s1).phiMax, (*s2).phiMax);
481 }
else if ((*s1).phiMin < (*s1).phiMax && (*s2).phiMin > (*s2).phiMax) {
482 if ((*s1).phiMin > (*s2).phiMax) {
483 scomb.
phiMin = std::min((*s1).phiMin, (*s2).phiMin);
484 scomb.
phiMax = (*s2).phiMax;
485 }
else if ((*s1).phiMax < (*s2).phiMin) {
486 scomb.
phiMin = (*s2).phiMin;
487 scomb.
phiMax = std::max((*s1).phiMax, (*s2).phiMax);
492 }
else if ((*s1).phiMin > (*s1).phiMax && (*s2).phiMin < (*s2).phiMax) {
493 if ((*s2).phiMin > (*s1).phiMax) {
494 scomb.
phiMin = std::min((*s1).phiMin, (*s2).phiMin);
495 scomb.
phiMax = (*s1).phiMax;
496 }
else if ((*s2).phiMax < (*s1).phiMin) {
497 scomb.
phiMin = (*s1).phiMin;
498 scomb.
phiMax = std::max((*s1).phiMax, (*s2).phiMax);
504 scomb.
phiMin = std::min((*s1).phiMin, (*s2).phiMin);
505 scomb.
phiMax = std::max((*s1).phiMax, (*s2).phiMax);
507 return std::make_unique<VolumeSpan>(scomb);
513 double minPhi{2 *
M_PI};
523 std::vector<Amg::Vector3D> vtx;
524 std::vector<std::pair<int, int>> edges;
544 edges.emplace_back(0, 1);
545 edges.emplace_back(0, 2);
546 edges.emplace_back(1, 3);
547 edges.emplace_back(2, 3);
548 edges.emplace_back(4, 5);
549 edges.emplace_back(4, 6);
550 edges.emplace_back(5, 7);
551 edges.emplace_back(6, 7);
552 edges.emplace_back(0, 4);
553 edges.emplace_back(1, 5);
554 edges.emplace_back(2, 6);
555 edges.emplace_back(3, 7);
574 edges.emplace_back(0, 1);
575 edges.emplace_back(0, 2);
576 edges.emplace_back(1, 3);
577 edges.emplace_back(2, 3);
578 edges.emplace_back(4, 5);
579 edges.emplace_back(4, 6);
580 edges.emplace_back(5, 7);
581 edges.emplace_back(6, 7);
582 edges.emplace_back(0, 4);
583 edges.emplace_back(1, 5);
584 edges.emplace_back(2, 6);
585 edges.emplace_back(3, 7);
608 edges.emplace_back(0, 1);
609 edges.emplace_back(0, 2);
610 edges.emplace_back(1, 3);
611 edges.emplace_back(2, 4);
612 edges.emplace_back(3, 5);
613 edges.emplace_back(4, 5);
614 edges.emplace_back(6, 7);
615 edges.emplace_back(6, 8);
616 edges.emplace_back(7, 9);
617 edges.emplace_back(8, 10);
618 edges.emplace_back(9, 11);
619 edges.emplace_back(10, 11);
620 edges.emplace_back(0, 6);
621 edges.emplace_back(1, 7);
622 edges.emplace_back(2, 8);
623 edges.emplace_back(3, 9);
624 edges.emplace_back(4, 10);
625 edges.emplace_back(5, 11);
631 edges.emplace_back(0, 1);
633 const double cosDphi = std::cos(dPhi);
634 const double sinDphi = std::sin(dPhi);
662 edges.emplace_back(2, 3);
663 edges.emplace_back(4, 5);
664 edges.emplace_back(6, 7);
665 edges.emplace_back(8, 9);
666 if (bcyl->
type() == 1 || bcyl->
type() == 3) {
667 edges.emplace_back(3, 5);
668 edges.emplace_back(7, 9);
670 if (bcyl->
type() == 2 || bcyl->
type() == 3) {
671 edges.emplace_back(2, 4);
672 edges.emplace_back(6, 8);
680 edges.emplace_back(0, 1);
682 const double cosDphi = std::cos(dPhi);
683 const double sinDphi = std::sin(dPhi);
705 edges.emplace_back(2, 3);
706 edges.emplace_back(4, 5);
707 edges.emplace_back(6, 7);
708 edges.emplace_back(8, 9);
713 const std::vector<std::pair<double, double>> vtcs = spb->
xyVertices();
714 for (
const auto& vtc : vtcs) {
715 vtx.emplace_back(vtc.first, vtc.second, spb->
halflengthZ());
716 vtx.emplace_back(vtc.first, vtc.second, -spb->
halflengthZ());
717 edges.emplace_back(vtx.size() - 2, vtx.size() - 1);
718 if (vtx.size() > 2) {
720 vtx.size() - 4, vtx.size() - 2);
722 vtx.size() - 3, vtx.size() - 1);
724 if (vtx.size() > 4) {
725 edges.emplace_back(vtx.size() - 2, 1);
726 edges.emplace_back(vtx.size() - 1, 0);
729 edges.emplace_back(0, vtx.size() - 2);
730 edges.emplace_back(1, vtx.size() - 1);
734 const std::vector<std::pair<double, double>> vtcs = prism->
xyVertices();
735 for (
const auto& vtc : vtcs) {
736 vtx.emplace_back(vtc.first, vtc.second, prism->
halflengthZ());
737 vtx.emplace_back(vtc.first, vtc.second, -prism->
halflengthZ());
738 edges.emplace_back(vtx.size() - 2, vtx.size() - 1);
739 if (vtx.size() > 2) {
741 vtx.size() - 4, vtx.size() - 2);
743 vtx.size() - 3, vtx.size() - 1);
746 edges.emplace_back(0, vtx.size() - 2);
747 edges.emplace_back(1, vtx.size() - 1);
750 std::vector<Amg::Vector3D> vtxt;
752 for (
unsigned int ie = 0; ie < vtx.size(); ie++) {
757 double rad = gp.perp();
760 minX = std::min(minX, gp[0]);
761 maxX = std::max(maxX, gp[0]);
762 minY = std::min(minY, gp[1]);
763 maxY = std::max(maxY, gp[1]);
764 minZ = std::min(minZ, gp[2]);
765 maxZ = std::max(maxZ, gp[2]);
766 minR = std::min(minR, rad);
767 maxR = std::max(maxR, rad);
768 maxPhi = std::max(maxPhi,
phi);
769 minPhi = std::min(minPhi,
phi);
778 (vtxt[edges[0].first] - vtxt[edges[0].second]).
unit();
779 maxZ += ro * sin(dir.theta());
780 minZ += -ro * sin(dir.theta());
786 double le = (vtxt[0] - vtxt[1]).norm();
787 if ((closest.
position - vtxt[0]).norm() < le &&
788 (closest.
position - vtxt[1]).norm() < le) {
789 if (minR > closest.
position.perp() - ro)
790 minR = std::max(0., closest.
position.perp() - ro);
793 if (phiClosest < minPhi || phiClosest > maxPhi) {
794 double phiTmp = minPhi;
799 minR = std::max(0., minR - ro * std::abs(dir.z()));
801 const double aTan = std::atan2(ro, minR);
806 if (maxPhi > 2 *
M_PI)
809 maxR += ro * std::abs(cos(dir.theta()));
812 double rAx = std::max(vtxt[0].
perp(), vtxt[1].
perp());
816 minR = std::max(0., minR - ro * std::abs(cos(dir.theta())));
820 for (
unsigned int ie = 0; ie < edges.size(); ie++) {
822 (vtxt[edges[ie].first] - vtxt[edges[ie].second]).
unit();
826 (vtxt[edges[ie].first] - vtxt[edges[ie].second]).norm();
827 if ((closest.
position - vtxt[edges[ie].first]).norm() < le &&
828 (closest.
position - vtxt[edges[ie].second]).norm() < le)
833 if (vtxt.size() > 10) {
836 double phiSecLmin = std::min(
839 double phiSecLmax = std::max(
844 double phiSecUmin = std::min(
847 double phiSecUmax = std::max(
850 minPhi = std::min(std::min(phiSecLmin, phiSecLmax),
851 std::min(phiSecUmin, phiSecUmax));
852 maxPhi = std::max(std::max(phiSecLmin, phiSecLmax),
853 std::max(phiSecUmin, phiSecUmax));
854 if (vtxt[10].
phi() +
M_PI < minPhi ||
855 vtxt[10].
phi() +
M_PI > maxPhi) {
859 for (
unsigned int iv = 2; iv < vtxt.size(); iv++) {
860 phiTmp = vtxt[iv].phi() +
M_PI;
863 minPhi = phiTmp < minPhi ? phiTmp : minPhi;
864 maxPhi = phiTmp > maxPhi ? phiTmp : maxPhi;
866 if (minPhi > 2 *
M_PI)
868 if (maxPhi > 2 *
M_PI)
874 maxR += ro * std::abs(std::cos(dir.theta()));
876 if (minPhi >= maxPhi && (minPhi - maxPhi) <
M_PI) {
886 for (
unsigned int ie = 0; ie < edges.size(); ie++) {
888 (vtxt[edges[ie].first] - vtxt[edges[ie].second]).
unit();
891 double le = (vtxt[edges[ie].first] - vtxt[edges[ie].second]).norm();
892 if ((closest.
position - vtxt[edges[ie].first]).norm() < le &&
893 (closest.
position - vtxt[edges[ie].second]).norm() < le)
898 if (std::abs(maxPhi - minPhi) >
M_PI) {
899 double phiTmp = minPhi;
902 for (
unsigned int iv = 0; iv < vtxt.size(); iv++) {
903 phiTmp = vtxt[iv].phi() +
M_PI;
906 minPhi = phiTmp < minPhi ? phiTmp : minPhi;
907 maxPhi = phiTmp > maxPhi ? phiTmp : maxPhi;
909 if (minPhi > 2 *
M_PI)
911 if (maxPhi > 2 *
M_PI)
913 if (minPhi >= maxPhi && (minPhi - maxPhi) <
M_PI) {
920 if (cyl || bcyl || box || trd || dtrd || spb || prism) {
921 span.zMin = minZ - zTol;
922 span.zMax = maxZ - +zTol;
923 minPhi = (minPhi - phiTol) < 0 ? minPhi - phiTol + 2 *
M_PI
925 span.phiMin = minPhi;
926 maxPhi = (maxPhi + phiTol) > 2 *
M_PI ? maxPhi + phiTol - 2 *
M_PI
928 span.phiMax = maxPhi;
929 span.rMin = std::max(70.001, minR - zTol);
930 span.rMax = maxR + zTol;
931 span.xMin = minX - zTol;
932 span.xMax = maxX - +zTol;
933 span.yMin = minY - zTol;
934 span.yMax = maxY - +zTol;
938 return std::make_unique<VolumeSpan>(span);