ATLAS Offline Software
CaruanaSTgcClusterBuilderTool.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 
11 
12 using namespace Muon;
13 
14 Muon::CaruanaSTgcClusterBuilderTool::CaruanaSTgcClusterBuilderTool(const std::string& t, const std::string& n, const IInterface* p) :
15  AthAlgTool(t,n,p)
16 {
17 }
18 
20 {
21  ATH_CHECK( m_idHelperSvc.retrieve() );
22  ATH_CHECK(m_DetectorManagerKey.initialize());
23  ATH_CHECK(m_uncertCalibKey.initialize());
24  return StatusCode::SUCCESS;
25 }
26 
27 //
28 // Build the clusters given a vector of single-hit PRD
29 //
31  std::vector<Muon::sTgcPrepData>&& stripsVect,
32  std::vector<std::unique_ptr<Muon::sTgcPrepData>>& clustersVect) const
33 {
34 
35  ATH_MSG_DEBUG("Size of the input vector: " << stripsVect.size());
36 
37  if (stripsVect.empty()) {
38  ATH_MSG_DEBUG("Size of the channel vectors is zero");
39  return StatusCode::SUCCESS;
40  }
41 
42  SG::ReadCondHandle<NswErrorCalibData> errorCalibDB{m_uncertCalibKey, ctx};
43  if (!errorCalibDB.isValid()) {
44  ATH_MSG_FATAL("Failed to retrieve the parameterized errors "<<m_uncertCalibKey.fullKey());
45  return StatusCode::FAILURE;
46  }
47 
48  Muon::STgcClusterBuilderCommon stgcClusterCommon(m_idHelperSvc->stgcIdHelper(), **errorCalibDB);
49 
50  // define the identifier hash
51  Identifier chanId = stripsVect.at(0).identify();
52  IdentifierHash hash = m_idHelperSvc->moduleHash(chanId);
53 
54  int channelType = m_idHelperSvc->stgcIdHelper().channelType(stripsVect.at(0).identify());
55  double resolution = Amg::error(stripsVect.at(0).localCovariance(), Trk::locX);
56  ATH_MSG_DEBUG("ChannelType: " << channelType << " Single channel resolution: " << resolution);
57 
58  // Separate hits by layer (using index = 0 to 7) and sort them
59  std::array<std::vector<Muon::sTgcPrepData>, 8> stgcPrdsPerLayer = stgcClusterCommon.sortSTGCPrdPerLayer(std::move(stripsVect));
60 
61  SG::ReadCondHandle<MuonGM::MuonDetectorManager> detManager{m_DetectorManagerKey, ctx};
62  if(!detManager.isValid()){
63  ATH_MSG_ERROR("Null pointer to the read MuonDetectorManager conditions object");
64  return StatusCode::FAILURE;
65  }
66 
67  // Loop over the 8 layers, from 0 to 7
68  for (std::vector<sTgcPrepData>& unmerged : stgcPrdsPerLayer) {
69  // Get the strip clusters of the layer.
70  std::vector<std::vector<Muon::sTgcPrepData>> layerClusters = stgcClusterCommon.findStripCluster(std::move(unmerged),
71  m_maxHoleSize);
72 
73  // Loop over the clusters of that gap
74  for (std::vector<Muon::sTgcPrepData>& cluster: layerClusters) {
76  std::vector<Identifier> rdoList;
77  //vectors to hold the properties of the elements of a cluster
78  std::vector<int> elementsCharge;
79  std::vector<short int> elementsTime;
80  std::vector<uint16_t> elementsChannel;
81  std::vector<double> elementsLocalPositions;
82  std::vector<Identifier> elementsIdentifier;
83  double posY = (cluster.at(0)).localPosition().y();
84 
85  // loop on the strips and set the cluster weighted position and charge
86  for (const sTgcPrepData& it : cluster ){
87  rdoList.push_back(it.identify());
88  elementsCharge.push_back(it.charge());
89  elementsChannel.push_back(m_idHelperSvc->stgcIdHelper().channel(it.identify()));
90  elementsTime.push_back(it.time());
91  elementsLocalPositions.push_back(it.localPosition().x());
92  elementsIdentifier.push_back(it.identify());
93  }
94 
95  // Use the Caruana method to reconstruct clusters, determining the cluster mean position and error.
96  // If the Caruana method fails at some point, the reconstruction reverts to the weighted average method.
97  Identifier clusterId;
98  double reconstructedPosX{0};
99  double sigmaSq{0};
100  std::optional<Muon::STgcClusterPosition> optStripCluster = stgcClusterCommon.caruanaGaussianFitting(cluster,
101  m_positionStripResolution,
102  m_angularStripResolution,
103  detManager.cptr());
104  if (optStripCluster.has_value()) {
105  clusterId = (*optStripCluster).getClusterId();
106  reconstructedPosX = (*optStripCluster).getMeanPosition();
107  sigmaSq = (*optStripCluster).getErrorSquared();
109  } else {
110  ATH_MSG_DEBUG("sTGC cluster reconstruction using the Caruana method failed, reversing to the weighted average");
111 
112  // Fallback to the weighted average when the Caruana method fails
113  bool isStrip = (channelType == sTgcIdHelper::sTgcChannelTypes::Strip);
114  // Calculate the cluster position and error using the weighted average
115  std::optional<Muon::STgcClusterPosition> optClusterWA = stgcClusterCommon.weightedAverage(cluster, resolution, isStrip);
116  // Skip the cluster if the weighted average also fails
117  if (!optClusterWA) {
118  if (msgLvl(MSG::VERBOSE)) {
119  std::stringstream sstr{};
120  for (const Muon::sTgcPrepData& prd : cluster) {
121  sstr << m_idHelperSvc->stgcIdHelper().print_to_string(prd.identify())
122  << ", local pos: ("<< prd.localPosition().x() << "," << prd.localPosition().y()
123  << "), charge: " << prd.charge() << ", time: " << static_cast<int>(prd.time())
124  << std::endl;
125  }
126  ATH_MSG_VERBOSE("Reject invalid cluster..." << std::endl << std::endl << sstr.str());
127  }
128  continue;
129  }
131 
132  clusterId = (*optClusterWA).getClusterId();
133  reconstructedPosX = (*optClusterWA).getMeanPosition();
134  sigmaSq = (*optClusterWA).getErrorSquared();
135  }
136 
137  Amg::Vector2D localPosition(reconstructedPosX,posY);
138  auto covN = Amg::MatrixX(1,1);
139  covN(0,0) = sigmaSq;
140  ATH_MSG_DEBUG("Reconstructed a cluster at mean local position: "<< Amg::toString(localPosition,2)
141  << " with error on cluster: " << std::sqrt((covN)(0,0)));
142 
143  std::unique_ptr<sTgcPrepData> prdN = std::make_unique<sTgcPrepData>(clusterId,
144  hash,
145  std::move(localPosition),
146  std::move(rdoList),
147  std::move(covN),
148  cluster.at(0).detectorElement(),
149  std::accumulate(elementsCharge.begin(), elementsCharge.end(), 0),
150  (short int)0,
151  std::move(elementsChannel),
152  std::move(elementsTime),
153  std::move(elementsCharge));
154  prdN->setAuthor(author);
155  clustersVect.push_back(std::move(prdN));
156  }
157  }
158 
159  ATH_MSG_DEBUG("Size of the output cluster vector: " << clustersVect.size());
160 
161  return StatusCode::SUCCESS;
162 }
163 
164 
166 void CaruanaSTgcClusterBuilderTool::dumpStrips( std::vector<Muon::sTgcPrepData>& stripsVect,
167  std::vector<Muon::sTgcPrepData*>& clustersVect ) const
168 {
169 
170  ATH_MSG_INFO("====> Dumping all strips: ");
171  for ( const auto& it : stripsVect ) {
172  Identifier stripId = it.identify();
173  ATH_MSG_INFO("Strip identifier: " << m_idHelperSvc->stgcIdHelper().show_to_string(stripId) );
174  }
175 
176  ATH_MSG_INFO("Dumping all clusters: ");
177  for ( auto *it : clustersVect ) {
178  Identifier clusterId = it->identify();
179  ATH_MSG_INFO("***> New cluster identifier: " << m_idHelperSvc->stgcIdHelper().show_to_string(clusterId) );
180  ATH_MSG_INFO("Cluster size: " << it->rdoList().size() );
181  ATH_MSG_INFO("List of associated RDO's: ");
182 
183  }
184 
185  }
Muon::sTgcPrepData::setAuthor
void setAuthor(const Author a)
Definition: sTgcPrepData.h:116
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
Muon::CaruanaSTgcClusterBuilderTool::CaruanaSTgcClusterBuilderTool
CaruanaSTgcClusterBuilderTool(const std::string &, const std::string &, const IInterface *)
Default constructor.
Definition: CaruanaSTgcClusterBuilderTool.cxx:14
Amg::MatrixX
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > MatrixX
Dynamic Matrix - dynamic allocation.
Definition: EventPrimitives.h:29
python.PerfMonSerializer.p
def p
Definition: PerfMonSerializer.py:743
SG::ReadCondHandle
Definition: ReadCondHandle.h:44
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
Trk::locX
@ locX
Definition: ParamDefs.h:43
Muon::STgcClusterBuilderCommon::sortSTGCPrdPerLayer
std::array< std::vector< sTgcPrepData >, 8 > sortSTGCPrdPerLayer(std::vector< sTgcPrepData > &&stripPrds) const
Separate the sTGC PRDs by layer, from 0 to 7, and sort the PRDs per layer in ascending order of strip...
Definition: STgcClusterBuilderCommon.cxx:20
Amg::Vector2D
Eigen::Matrix< double, 2, 1 > Vector2D
Definition: GeoPrimitives.h:48
accumulate
bool accumulate(AccumulateMap &map, std::vector< module_t > const &modules, FPGATrackSimMatrixAccumulator const &acc)
Accumulates an accumulator (e.g.
Definition: FPGATrackSimMatrixAccumulator.cxx:22
EventPrimitivesHelpers.h
Muon::sTgcPrepData::Author::Caruana
@ Caruana
skel.it
it
Definition: skel.GENtoEVGEN.py:423
Muon::CaruanaSTgcClusterBuilderTool::dumpStrips
void dumpStrips(std::vector< Muon::sTgcPrepData > &stripsVect, std::vector< Muon::sTgcPrepData * > &clustersVect) const
private functions
Definition: CaruanaSTgcClusterBuilderTool.cxx:166
Muon::STgcClusterBuilderCommon
Definition: STgcClusterBuilderCommon.h:30
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
Muon
This class provides conversion from CSC RDO data to CSC Digits.
Definition: TrackSystemController.h:49
Dedxcorrection::resolution
double resolution[nGasTypes][nParametersResolution]
Definition: TRT_ToT_Corrections.h:46
sTgcPrepData.h
Amg::toString
std::string toString(const Translation3D &translation, int precision=4)
GeoPrimitvesToStringConverter.
Definition: GeoPrimitivesToStringConverter.h:40
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
beamspotman.n
n
Definition: beamspotman.py:731
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
CaruanaSTgcClusterBuilderTool.h
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
Muon::CaruanaSTgcClusterBuilderTool::initialize
virtual StatusCode initialize()
standard initialize method
Definition: CaruanaSTgcClusterBuilderTool.cxx:19
Muon::STgcClusterBuilderCommon::weightedAverage
std::optional< STgcClusterPosition > weightedAverage(const std::vector< sTgcPrepData > &cluster, const double resolution, bool isStrip) const
Compute the cluster position using the weighted average method.
Definition: STgcClusterBuilderCommon.cxx:83
Muon::CaruanaSTgcClusterBuilderTool::getClusters
StatusCode getClusters(const EventContext &ctx, std::vector< Muon::sTgcPrepData > &&stripsVect, std::vector< std::unique_ptr< Muon::sTgcPrepData >> &clustersVect) const
Definition: CaruanaSTgcClusterBuilderTool.cxx:30
dumpTgcDigiThreshold.isStrip
list isStrip
Definition: dumpTgcDigiThreshold.py:33
Amg::error
double error(const Amg::MatrixX &mat, int index)
return diagonal error of the matrix caller should ensure the matrix is symmetric and the index is in ...
Definition: EventPrimitivesHelpers.h:40
MuonDetectorManager.h
Muon::sTgcPrepData::Author::SimpleClusterBuilder
@ SimpleClusterBuilder
beamspotman.posY
posY
Definition: beamspotman.py:1624
STgcClusterBuilderCommon.h
Muon::sTgcPrepData::Author
Author
Definition: sTgcPrepData.h:100
CaloCondBlobAlgs_fillNoiseFromASCII.hash
dictionary hash
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:109
CaloCondBlobAlgs_fillNoiseFromASCII.author
string author
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:26
GeoPrimitivesToStringConverter.h
Muon::STgcClusterBuilderCommon::findStripCluster
std::vector< std::vector< sTgcPrepData > > findStripCluster(std::vector< sTgcPrepData > &&strips, const int maxMissingStrip) const
Find strip clusters, assuming the input vector of PRDs are sorted in ascending order of strip number.
Definition: STgcClusterBuilderCommon.cxx:44
Muon::sTgcPrepData::Author::unKnown
@ unKnown
python.Constants.VERBOSE
int VERBOSE
Definition: Control/AthenaCommon/python/Constants.py:14
Muon::STgcClusterBuilderCommon::caruanaGaussianFitting
std::optional< STgcClusterPosition > caruanaGaussianFitting(const std::vector< sTgcPrepData > &cluster, const double positionResolution, const double angularResolution, const MuonGM::MuonDetectorManager *detManager) const
Method to fit analytically a cluster to a Gaussian function to obtain the position of the cluster The...
Definition: STgcClusterBuilderCommon.cxx:155
AthAlgTool
Definition: AthAlgTool.h:26
Muon::sTgcPrepData
Class to represent sTgc measurements.
Definition: sTgcPrepData.h:20
Muon::CaruanaSTgcClusterBuilderTool::m_idHelperSvc
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
Definition: CaruanaSTgcClusterBuilderTool.h:55