ATLAS Offline Software
Loading...
Searching...
No Matches
MuonSegmentPerformanceAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3*/
4
6
11
12using namespace Muon::MuonStationIndex;
13MuonSegmentPerformanceAlg::MuonSegmentPerformanceAlg(const std::string& name, ISvcLocator* pSvcLocator) :
14 AthAlgorithm(name, pSvcLocator),
15 m_writeToFile(false),
16 m_nevents(0) {
17
18 declareProperty("writeToFile", m_writeToFile = false);
19 declareProperty("FileName", m_fileName = "MuonSegmentPerformanceAlg.txt");
20}
21
23 // initialize cuts, please make sure the number of bins and the sizes of the cuts + string are always the same
24 unsigned int nbins = 3;
25 m_nevents = 0;
26 m_ntruth.resize(nbins);
27 m_nfound.resize(nbins);
28 m_nfake.resize(nbins);
29 m_nhitCuts = {3, 4, 5};
30 m_hitCutString = {"n==3 ", "n==4 ", "n>=5 "};
31
35
36 return StatusCode::SUCCESS;
37}
38
40 const EventContext& ctx{Gaudi::Hive::currentContext()};
41 const xAOD::MuonSegmentContainer* segments = nullptr;
42 const xAOD::MuonSegmentContainer* truthSegments = nullptr;
43 ATH_CHECK(SG::get(segments, m_segmentKey, ctx));
44 ATH_CHECK(SG::get(truthSegments, m_truthSegmentKey, ctx));
45 if (!segments || !truthSegments) {
46 return StatusCode::SUCCESS;
47 }
48 std::set<const xAOD::MuonSegment*> matchedSegments;
49 ++m_nevents;
50
51 bool missedSegment = false;
52 for (const auto seg : *truthSegments) {
53 ChIndex chIndex = seg->chamberIndex();
55 continue;
56 }
57 unsigned int index = 0;
58 if (seg->nPrecisionHits() < 3) continue;
59 while ((index < m_nhitCuts.size() - 1) && seg->nPrecisionHits() > m_nhitCuts[index] ) ++index;
60
62
63 if (index == 2 && (chIndex == ChIndex::CSS || chIndex == ChIndex::CSL) ){
64 ATH_MSG_WARNING(chName(chIndex)<<" with more than 4 layers ");
65 }
67 recoSegmentLinkAcc("recoSegmentLink");
68 const ElementLink<xAOD::MuonSegmentContainer>& recoLink = recoSegmentLinkAcc(*seg);
69 if (recoLink.isValid()) {
71 matchedSegments.insert(*recoLink);
72 } else {
73 ATH_MSG_DEBUG(" Missing segment in sector "
74 << seg->sector() << " " << chName(chIndex)
75 << " eta " << seg->etaIndex() << " nprec " << seg->nPrecisionHits() << " nphi " << seg->nPhiLayers()
76 << " nTrigEta " << seg->nTrigEtaLayers());
77 missedSegment = true;
78 }
79 }
80 if (missedSegment) ATH_MSG_DEBUG(" Dump Fake segments ");
81
82 for (const auto seg : *segments) {
83 if (matchedSegments.count(seg)) continue;
84 ChIndex chIndex = seg->chamberIndex();
86 ATH_MSG_WARNING("bad index ");
87 continue;
88 }
89
90 unsigned int index = 0;
91 if (seg->nPrecisionHits() < 3) continue;
92 while ((index < m_nhitCuts.size() - 1) and (seg->nPrecisionHits() > m_nhitCuts[index]) ) ++index;
93 if (missedSegment)
94 ATH_MSG_DEBUG(" Fake segment in sector "
95 << seg->sector() << " " << chName(chIndex)
96 << " eta " << seg->etaIndex() << " nprec " << seg->nPrecisionHits() << " nphi " << seg->nPhiLayers()
97 << " nTrigEta " << seg->nTrigEtaLayers());
98
100 }
101
102 return StatusCode::SUCCESS;
103}
104
105std::string MuonSegmentPerformanceAlg::printRatio(const std::string& prefix, unsigned int begin, unsigned int end,
106 const counter_t& reco, const counter_t& truth) const {
107 std::ostringstream sout;
108 unsigned int width = 9;
109 unsigned int precision = 3;
110 sout << std::endl << prefix;
111 for (unsigned int i = begin; i < end; ++i) {
112 sout << std::setw(width) << std::setprecision(precision);
113 if (truth[i] == 0)
114 sout << " ";
115 else
116 sout << static_cast<double>(reco[i]) / static_cast<double>(truth[i]);
117 }
118 sout << std::endl
119 << " #Events "
120 << " ";
121 for (unsigned int i = begin; i < end; ++i) {
122 sout << std::setw(width) << std::setprecision(precision);
123 if (truth[i] == 0)
124 sout << " ";
125 else
126 sout << static_cast<double>(truth[i]);
127 }
128 return sout.str();
129}
130std::string MuonSegmentPerformanceAlg::printRatio(const std::string& prefix, unsigned int begin, unsigned int end,
131 const counter_t& reco) const {
132 std::ostringstream sout;
133 unsigned int width = 9;
134 unsigned int precision = 3;
135 sout << std::endl << prefix;
136 for (unsigned int i = begin; i < end; ++i) {
137 sout << std::setw(width) << std::setprecision(precision);
138 if (m_nevents == 0)
139 sout << " ";
140 else
141 sout << static_cast<double>(reco[i]) / static_cast<double>(m_nevents);
142 }
143 return sout.str();
144}
145
147 std::ofstream fileOutput;
148 std::string outfile = "muonPerformance_segments.txt";
149 fileOutput.open(outfile.c_str(), std::ios::trunc);
150 std::ostringstream sout;
151 sout.precision(4);
152
153 unsigned int width = 9;
154 sout << "Segment finding efficiencies barrel" << std::endl;
155 sout << " Chambers ";
156 std::string prefix_eff = " Efficiency ";
157 std::string prefix_fake = " Fake rate ";
158 unsigned end{0};
159 for (unsigned int i = 0; i < s_chIdxMax; ++i){
160 if (!isBarrel(static_cast<ChIndex>(i))) {
161 break;
162 }
163 sout << std::setw(width) << chName(static_cast<ChIndex>(i));
164 ++end;
165 }
166 for (unsigned int j = 0; j < end; ++j) {
167 sout << printRatio(prefix_eff + m_hitCutString[j], 0, end, m_nfound[j], m_ntruth[j]);
168 }
169 sout << std::endl;
170 for (unsigned int j = 0; j < end; ++j) {
171 sout << printRatio(prefix_fake + m_hitCutString[j], 0, end, m_nfake[j]);
172 }
173 sout << std::endl;
174
175 sout << "Segment finding efficiencies endcaps" << std::endl;
176 sout << " Chambers ";
177 for (unsigned int i = end; i < s_chIdxMax; ++i)
178 sout << std::setw(width) << chName(static_cast<ChIndex>(i));
179 for (unsigned int j = 0; j < m_nfound.size(); ++j) {
180 sout << printRatio(prefix_eff + m_hitCutString[j], end, s_chIdxMax, m_nfound[j],
181 m_ntruth[j]);
182 }
183 sout << std::endl;
184 for (unsigned int j = 0; j < m_nfound.size(); ++j) {
185 sout << printRatio(prefix_fake + m_hitCutString[j], end, s_chIdxMax, m_nfake[j]);
186 }
187 sout << std::endl;
188 fileOutput << sout.str() << std::endl;
189 fileOutput.close();
190 return StatusCode::SUCCESS;
191}
192
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
Helper class to provide constant type-safe access to aux data.
const double width
AthAlgorithm(const std::string &name, ISvcLocator *pSvcLocator)
Constructor with parameters:
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T, V, H > &t)
virtual StatusCode initialize() override
std::vector< counter_t > m_ntruth
SG::ReadHandleKey< xAOD::MuonSegmentContainer > m_segmentKey
std::vector< counter_t > m_nfake
virtual StatusCode execute() override
MuonSegmentPerformanceAlg(const std::string &name, ISvcLocator *pSvcLocator)
std::array< int, s_chIdxMax > counter_t
std::vector< counter_t > m_nfound
std::string printRatio(const std::string &prefix, unsigned int begin, unsigned int end, const counter_t &reco, const counter_t &truth) const
SG::ReadHandleKey< xAOD::MuonSegmentContainer > m_truthSegmentKey
SG::ReadDecorHandleKey< xAOD::MuonSegmentContainer > m_truthSegmenLinkKey
static constexpr unsigned s_chIdxMax
bool m_writeToFile
name of external file to write statistics
virtual StatusCode finalize() override
std::vector< std::string > m_hitCutString
Helper class to provide constant type-safe access to aux data.
ChIndex chIndex(const std::string &index)
convert ChIndex name string to enum
constexpr int toInt(const EnumType enumVal)
bool isBarrel(const ChIndex index)
Returns true if the chamber index points to a barrel chamber.
const std::string & chName(ChIndex index)
convert ChIndex into a string
ChIndex
enum to classify the different chamber layers in the muon spectrometer
const T * get(const ReadCondHandleKey< T > &key, const EventContext &ctx)
Convenience function to retrieve an object given a ReadCondHandleKey.
Definition index.py:1
MuonSegmentContainer_v1 MuonSegmentContainer
Definition of the current "MuonSegment container version".