ATLAS Offline Software
MuonSpacePointMakerAlg.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
5 
6 #include "StoreGate/ReadHandle.h"
8 
9 
10 namespace MuonR4 {
11 
12 
13 MuonSpacePointMakerAlg::MuonSpacePointMakerAlg(const std::string& name, ISvcLocator* pSvcLocator):
14  AthReentrantAlgorithm{name, pSvcLocator}{}
15 
16 
19  ATH_CHECK(m_mdtKey.initialize(!m_mdtKey.empty()));
20  ATH_CHECK(m_rpcKey.initialize(!m_rpcKey.empty()));
21  ATH_CHECK(m_tgcKey.initialize(!m_tgcKey.empty()));
22  ATH_CHECK(m_mmKey.initialize(!m_mmKey.empty()));
23  ATH_CHECK(m_stgcKey.initialize(!m_stgcKey.empty()));
24  ATH_CHECK(m_idHelperSvc.retrieve());
25  ATH_CHECK(m_writeKey.initialize());
26  return StatusCode::SUCCESS;
27 }
28 
29 template <class ContType>StatusCode MuonSpacePointMakerAlg::loadContainerAndSort(const EventContext& ctx,
32  if (key.empty()) {
33  ATH_MSG_DEBUG("Key "<<typeid(ContType).name()<<" not set. Do not fill anything");
34  return StatusCode::SUCCESS;
35  }
36  SG::ReadHandle<ContType> readHandle{key, ctx};
37  ATH_CHECK(readHandle.isPresent());
38 
40  ATH_CHECK(gctx.isPresent());
41 
42  using PrdType = typename ContType::const_value_type;
43  using PrdVec = std::vector<PrdType>;
47  for (const PrdType prd : *readHandle) {
48  fillContainer[prd->readoutElement()->getChamber()].etaHits.emplace_back(*gctx, prd, nullptr);
49  }
50  } else {
52  using EtaPhiHits = std::pair<PrdVec, PrdVec>;
57  using EtaPhiHitsPerChamber = std::array<EtaPhiHits, 6>;
58  std::map<const MuonGMR4::MuonReadoutElement*, EtaPhiHitsPerChamber> collectedPrds{};
59  for (const PrdType prd : *readHandle) {
60  EtaPhiHitsPerChamber& hitsPerChamb = collectedPrds[prd->readoutElement()];
62  unsigned int gapIdx = prd->gasGap() -1;
65  gapIdx = 2*gapIdx + (prd->doubletPhi() - 1);
66  }
67  bool measPhi{false};
70  if (prd->channelType() == sTgcIdHelper::sTgcChannelTypes::Pad) {
71  fillContainer[prd->readoutElement()->getChamber()].etaHits.emplace_back(*gctx, prd, nullptr);
72  continue;
73  }
74  measPhi = prd->channelType() == sTgcIdHelper::sTgcChannelTypes::Wire;
75  } else {
76  measPhi = prd->measuresPhi();
77  }
78  EtaPhiHits& hitsPerLayer = hitsPerChamb[gapIdx];
79  if (!measPhi) {
80  hitsPerLayer.first.push_back(prd);
81  } else {
82  hitsPerLayer.second.push_back(prd);
83  }
84  }
86  for (auto& [reEle, hitsPerChamb] : collectedPrds) {
87  spacePointsPerChamber& fillInto {fillContainer[reEle->getChamber()]};
88  ATH_MSG_VERBOSE("Fill collected measurements for "<<m_idHelperSvc->toStringDetEl(reEle->identify()));
89  for (auto& [etaHits, phiHits]: hitsPerChamb) {
91  if (etaHits.empty() || phiHits.empty()) {
92  fillInto.etaHits.reserve(fillInto.etaHits.size() + etaHits.size());
93  fillInto.phiHits.reserve(fillInto.phiHits.size() + phiHits.size());
94  for (const PrdType etaPrd : etaHits) {
95  fillInto.etaHits.emplace_back(*gctx, etaPrd);
96  }
97  for (const PrdType phiPrd : phiHits) {
98  fillInto.phiHits.emplace_back(*gctx, phiPrd);
99  }
100  continue;
101  }
103  fillInto.etaHits.reserve(fillInto.etaHits.size() + etaHits.size() * phiHits.size());
104  for (const PrdType etaPrd : etaHits) {
105  for (const PrdType phiPrd: phiHits) {
106  fillInto.etaHits.emplace_back(*gctx, etaPrd, phiPrd);
107  }
108  }
109  }
110  }
111  }
112  return StatusCode::SUCCESS;
113 }
114 
115 
116 StatusCode MuonSpacePointMakerAlg::execute(const EventContext& ctx) const {
117  PreSortedSpacePointMap preSortedContainer{};
118  ATH_CHECK(loadContainerAndSort(ctx, m_mdtKey, preSortedContainer));
119  ATH_CHECK(loadContainerAndSort(ctx, m_rpcKey, preSortedContainer));
120  ATH_CHECK(loadContainerAndSort(ctx, m_tgcKey, preSortedContainer));
121  ATH_CHECK(loadContainerAndSort(ctx, m_mmKey, preSortedContainer));
122  ATH_CHECK(loadContainerAndSort(ctx, m_stgcKey, preSortedContainer));
123  std::unique_ptr<MuonSpacePointContainer> outContainer = std::make_unique<MuonSpacePointContainer>();
124 
125  for (auto &[chamber, hitsPerChamber] : preSortedContainer){
126  ATH_MSG_VERBOSE("Fill space points for chamber "<<chamber);
127  distributePointsAndStore(ctx, std::move(hitsPerChamber), *outContainer);
128  }
130  ATH_CHECK(writeHandle.record(std::move(outContainer)));
131  return StatusCode::SUCCESS;
132 }
133 
135  spacePointsPerChamber&& hitsPerChamber,
136  MuonSpacePointContainer& finalContainer) const {
137  SpacePointBucketVec splittedHits{};
138  splittedHits.emplace_back();
139  distributePointsAndStore(ctx, std::move(hitsPerChamber.etaHits), splittedHits);
140  distributePointsAndStore(ctx, std::move(hitsPerChamber.phiHits), splittedHits);
141 
142  for (MuonSpacePointBucket& bucket : splittedHits) {
143  if (bucket.size() > 1)
144  finalContainer.push_back(std::make_unique<MuonSpacePointBucket>(std::move(bucket)));
145  }
146 
147 }
149  std::vector<MuonSpacePoint>&& spacePoints,
150  SpacePointBucketVec& splittedHits) const {
151 
152  if (spacePoints.empty()) return;
153 
154  const bool defineBuckets = splittedHits[0].empty();
155  const bool hasEtaMeas{spacePoints[0].measuresEta()};
156 
157  auto pointPos = [hasEtaMeas, defineBuckets] (const MuonSpacePoint& p) {
158  return hasEtaMeas || !defineBuckets ? p.positionInChamber().y() : p.positionInChamber().x();
159  };
161 
162  auto channelDir = [hasEtaMeas, defineBuckets, &gctx](const MuonSpacePoint & p) {
163  const Amg::Vector3D d = xAOD::channelDirInChamber(*gctx, p.primaryMeasurement());
164  return std::abs(hasEtaMeas || !defineBuckets ? d.y() : d.z());
165  };
166 
167  std::sort(spacePoints.begin(), spacePoints.end(),
168  [&pointPos] (const MuonSpacePoint& a, const MuonSpacePoint& b) {
169  return pointPos(a) < pointPos(b);
170  });
171 
172 
173  double lastPoint = pointPos(spacePoints[0]);
174 
175  auto newBucket = [this, &lastPoint, &splittedHits, &pointPos, &channelDir] (const double currPos) {
176  splittedHits.emplace_back();
177  splittedHits.back().setBucketId(splittedHits.size() -1);
178  MuonSpacePointBucket& overlap{splittedHits[splittedHits.size() - 2]};
179  MuonSpacePointBucket& newContainer{splittedHits[splittedHits.size() - 1]};
180 
181  for (const std::shared_ptr<MuonSpacePoint>& pointInBucket : overlap) {
182  const double overlapPos = pointPos(*pointInBucket) + pointInBucket->uncertainty()[1] * channelDir(*pointInBucket);
183  if (std::abs(overlapPos - currPos) < m_spacePointOverlap) {
184  newContainer.push_back(pointInBucket);
185  }
186  }
187  lastPoint = newContainer.empty() ? currPos : pointPos(**newContainer.begin());
188  overlap.setCoveredRange(pointPos(**overlap.begin()), pointPos(**overlap.rbegin()));
189  };
190 
191  for (MuonSpacePoint& toSort : spacePoints) {
192  const double currPoint = pointPos(toSort);
194  if (!defineBuckets) {
195  std::shared_ptr<MuonSpacePoint> madePoint = std::make_shared<MuonSpacePoint>(std::move(toSort));
196  for (MuonSpacePointBucket& bucket : splittedHits) {
197  const double measDir = channelDir(toSort);
198  const double posMin = currPoint - toSort.uncertainty()[1] * measDir;
199  const double posMax = currPoint + toSort.uncertainty()[1] * measDir;
200 
201  if (posMax >= bucket.coveredMin() && bucket.coveredMax() >= posMin) {
202  bucket.push_back(madePoint);
203  }
204  }
205  continue;
206  }
208  if (currPoint - lastPoint > m_spacePointWindow) {
209  newBucket(currPoint);
210  }
211  std::shared_ptr<MuonSpacePoint> spacePoint = std::make_shared<MuonSpacePoint>(std::move(toSort));
212  splittedHits.back().emplace_back(spacePoint);
213  if (splittedHits.size() > 1) {
214  MuonSpacePointBucket& overlap{splittedHits[splittedHits.size() - 2]};
215  const double overlapPos = currPoint - spacePoint->uncertainty()[1] * channelDir(*spacePoint);
216  if (overlapPos - overlap.coveredMax() < m_spacePointOverlap) {
217  overlap.push_back(spacePoint);
218  }
219  }
220  }
221  MuonSpacePointBucket& lastBucket{splittedHits[splittedHits.size() - 1]};
222  newBucket(pointPos(*lastBucket.back()));
224  splittedHits.pop_back();
225 
226 }
227 
228 }
python.PerfMonSerializer.p
def p
Definition: PerfMonSerializer.py:743
MuonR4::MuonSpacePointMakerAlg::m_writeKey
SG::WriteHandleKey< MuonSpacePointContainer > m_writeKey
Definition: MuonSpacePointMakerAlg.h:76
calibdata.chamber
chamber
Definition: calibdata.py:32
MuonR4::MuonSpacePointMakerAlg::m_tgcKey
SG::ReadHandleKey< xAOD::TgcStripContainer > m_tgcKey
Definition: MuonSpacePointMakerAlg.h:63
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
hist_file_dump.d
d
Definition: hist_file_dump.py:137
athena.value
value
Definition: athena.py:122
MuonR4::MuonSpacePointMakerAlg::m_mdtKey
SG::ReadHandleKey< xAOD::MdtDriftCircleContainer > m_mdtKey
Definition: MuonSpacePointMakerAlg.h:57
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
SG::ReadHandleKey
Property holding a SG store/key/clid from which a ReadHandle is made.
Definition: StoreGate/StoreGate/ReadHandleKey.h:39
MuonR4::MuonSpacePointMakerAlg::initialize
StatusCode initialize() override
Definition: MuonSpacePointMakerAlg.cxx:17
MuonSpacePointMakerAlg.h
xAOD::channelDirInChamber
Amg::Vector3D channelDirInChamber(const ActsGeometryContext &gctx, const UncalibratedMeasurement *meas)
Definition: MuonSpectrometer/MuonPhaseII/Event/xAOD/xAODMuonPrepData/Root/UtilFunctions.cxx:88
AthReentrantAlgorithm
An algorithm that can be simultaneously executed in multiple threads.
Definition: AthReentrantAlgorithm.h:83
MuonR4::MuonSpacePointMakerAlg::MuonSpacePointMakerAlg
MuonSpacePointMakerAlg(const std::string &name, ISvcLocator *pSvcLocator)
Definition: MuonSpacePointMakerAlg.cxx:13
WriteHandle.h
Handle class for recording to StoreGate.
MuonR4::MuonSpacePointBucket
: The muon space point bucket represents a collection of points that will bre processed together in t...
Definition: MuonSpacePointContainer.h:22
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
MuonR4::MuonSpacePointMakerAlg::loadContainerAndSort
StatusCode loadContainerAndSort(const EventContext &ctx, const SG::ReadHandleKey< ContType > &key, PreSortedSpacePointMap &fillContainer) const
Definition: MuonSpacePointMakerAlg.cxx:29
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
SG::VarHandleKey::initialize
StatusCode initialize(bool used=true)
If this object is used as a property, then this should be called during the initialize phase.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:103
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
MuonR4::MuonSpacePointMakerAlg::m_mmKey
SG::ReadHandleKey< xAOD::MMClusterContainer > m_mmKey
Definition: MuonSpacePointMakerAlg.h:66
MuonR4::MuonSpacePoint
The muon space point is the combination of two uncalibrated measurements one of them measures the eta...
Definition: MuonSpacePoint.h:18
NSWL1::PadTriggerAdapter::fillContainer
StatusCode fillContainer(const std::unique_ptr< Muon::NSW_PadTriggerDataContainer > &out, const std::vector< std::unique_ptr< NSWL1::PadTrigger >> &triggers, const uint32_t l1id)
Definition: PadTriggerAdapter.cxx:17
MuonR4::MuonSpacePointMakerAlg::execute
StatusCode execute(const EventContext &ctx) const override
Definition: MuonSpacePointMakerAlg.cxx:116
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
DataVector::push_back
value_type push_back(value_type pElem)
Add an element to the end of the collection.
Amg::Vector3D
Eigen::Matrix< double, 3, 1 > Vector3D
Definition: GeoPrimitives.h:47
MuonR4
The CsvMuonSimHitDumper reads a Simulation Hit container for muons and dumps information to csv files...
Definition: MuonSpacePoint.h:11
SG::WriteHandle
Definition: StoreGate/StoreGate/WriteHandle.h:76
a
TList * a
Definition: liststreamerinfos.cxx:10
MuonR4::MuonSpacePointMakerAlg::distributePointsAndStore
void distributePointsAndStore(const EventContext &ctx, spacePointsPerChamber &&hitsPerChamber, MuonSpacePointContainer &finalContainer) const
Definition: MuonSpacePointMakerAlg.cxx:134
MuonR4::MuonSpacePointMakerAlg::m_idHelperSvc
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
Definition: MuonSpacePointMakerAlg.h:74
MuonR4::MuonSpacePointMakerAlg::m_stgcKey
SG::ReadHandleKey< xAOD::sTgcMeasContainer > m_stgcKey
Definition: MuonSpacePointMakerAlg.h:69
ReadHandle.h
Handle class for reading from StoreGate.
MuonR4::MuonSpacePointMakerAlg::spacePointsPerChamber
Helper struct to collect all space points per chamber.
Definition: MuonSpacePointMakerAlg.h:35
MuonR4::MuonSpacePointMakerAlg::m_spacePointWindow
Gaudi::Property< double > m_spacePointWindow
Definition: MuonSpacePointMakerAlg.h:78
MuonR4::MuonSpacePointMakerAlg::m_spacePointOverlap
Gaudi::Property< double > m_spacePointOverlap
Definition: MuonSpacePointMakerAlg.h:81
MuonR4::MuonSpacePointMakerAlg::m_rpcKey
SG::ReadHandleKey< xAOD::RpcMeasurementContainer > m_rpcKey
Definition: MuonSpacePointMakerAlg.h:60
MuonR4::MuonSpacePointMakerAlg::SpacePointBucketVec
std::vector< MuonSpacePointBucket > SpacePointBucketVec
Definition: MuonSpacePointMakerAlg.h:42
MuonR4::MuonSpacePointMakerAlg::PreSortedSpacePointMap
std::map< const MuonGMR4::MuonChamber *, spacePointsPerChamber, ChamberSorter > PreSortedSpacePointMap
Definition: MuonSpacePointMakerAlg.h:41
MuonR4::MuonSpacePointMakerAlg::m_geoCtxKey
SG::ReadHandleKey< ActsGeometryContext > m_geoCtxKey
Definition: MuonSpacePointMakerAlg.h:72
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37