ATLAS Offline Software
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 
8 #include "xAODMuon/MuonSegment.h"
11 
12 using namespace Muon::MuonStationIndex;
13 MuonSegmentPerformanceAlg::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();
54  if (chIndex == ChIndex::ChUnknown || chIndex == ChIndex::ChIndexMax) {
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();
85  if (chIndex == ChIndex::ChUnknown || chIndex == ChIndex::ChIndexMax) {
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 
105 std::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 }
130 std::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 
MuonSegmentPerformanceAlg::initialize
virtual StatusCode initialize() override
Definition: MuonSegmentPerformanceAlg.cxx:22
MuonSegmentPerformanceAlg::m_nfound
std::vector< counter_t > m_nfound
Definition: MuonSegmentPerformanceAlg.h:52
CSS
@ CSS
Definition: ParabolaCscClusterFitter.h:25
MuonSegmentPerformanceAlg::s_chIdxMax
constexpr static unsigned s_chIdxMax
Definition: MuonSegmentPerformanceAlg.h:31
MuonSegmentPerformanceAlg::m_segmentKey
SG::ReadHandleKey< xAOD::MuonSegmentContainer > m_segmentKey
Definition: MuonSegmentPerformanceAlg.h:44
Muon::MuonStationIndex
Definition: MuonStationIndex.h:13
MuonSegmentPerformanceAlg::m_truthSegmentKey
SG::ReadHandleKey< xAOD::MuonSegmentContainer > m_truthSegmentKey
Definition: MuonSegmentPerformanceAlg.h:45
index
Definition: index.py:1
MuonSegmentPerformanceAlg::m_hitCutString
std::vector< std::string > m_hitCutString
Definition: MuonSegmentPerformanceAlg.h:50
MuonSegmentPerformanceAlg::counter_t
std::array< int, s_chIdxMax > counter_t
Definition: MuonSegmentPerformanceAlg.h:32
MuonSegment.h
PlotCalibFromCool.begin
begin
Definition: PlotCalibFromCool.py:94
MuonSegmentPerformanceAlg::m_nhitCuts
std::vector< int > m_nhitCuts
Definition: MuonSegmentPerformanceAlg.h:49
SG::ConstAccessor
Helper class to provide constant type-safe access to aux data.
Definition: ConstAccessor.h:55
MuonSegmentPerformanceAlg::MuonSegmentPerformanceAlg
MuonSegmentPerformanceAlg(const std::string &name, ISvcLocator *pSvcLocator)
Definition: MuonSegmentPerformanceAlg.cxx:13
mergePhysValFiles.end
end
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:92
MuonSegmentPerformanceAlg::m_nevents
unsigned int m_nevents
Definition: MuonSegmentPerformanceAlg.h:48
MuonSegmentContainer.h
lumiFormat.i
int i
Definition: lumiFormat.py:85
SG::get
const T * get(const ReadCondHandleKey< T > &key, const EventContext &ctx)
Convenience function to retrieve an object given a ReadCondHandleKey.
Definition: ReadCondHandle.h:287
Muon::MuonStationIndex::chIndex
ChIndex chIndex(const std::string &index)
convert ChIndex name string to enum
Definition: MuonStationIndex.cxx:11
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
Muon::MuonStationIndex::chName
const std::string & chName(ChIndex index)
convert ChIndex into a string
Definition: MuonStationIndex.cxx:119
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
AthCommonDataStore< AthCommonMsg< Algorithm > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T, V, H > &t)
Definition: AthCommonDataStore.h:145
MuonSegmentPerformanceAlg::m_truthSegmenLinkKey
SG::ReadDecorHandleKey< xAOD::MuonSegmentContainer > m_truthSegmenLinkKey
Definition: MuonSegmentPerformanceAlg.h:46
jobOptions.fileOutput
fileOutput
Definition: jobOptions.py:39
checkCorrelInHIST.prefix
dictionary prefix
Definition: checkCorrelInHIST.py:391
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
MuonSegmentPerformanceAlg::printRatio
std::string printRatio(const std::string &prefix, unsigned int begin, unsigned int end, const counter_t &reco, const counter_t &truth) const
Definition: MuonSegmentPerformanceAlg.cxx:105
DataVector
Derived DataVector<T>.
Definition: DataVector.h:794
MuonSegmentPerformanceAlg::execute
virtual StatusCode execute() override
Definition: MuonSegmentPerformanceAlg.cxx:39
AthAlgorithm
Definition: AthAlgorithm.h:47
MuonSegmentPerformanceAlg::m_writeToFile
bool m_writeToFile
name of external file to write statistics
Definition: MuonSegmentPerformanceAlg.h:38
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
MuonR4::SegmentFit::toInt
constexpr int toInt(const ParamDefs p)
Definition: MuonHoughDefs.h:42
MuonSegmentPerformanceAlg::m_fileName
std::string m_fileName
Definition: MuonSegmentPerformanceAlg.h:39
SCT_CalibAlgs::nbins
@ nbins
Definition: SCT_CalibNumbers.h:10
DeMoScan.index
string index
Definition: DeMoScan.py:362
MuonSegmentPerformanceAlg::finalize
virtual StatusCode finalize() override
Definition: MuonSegmentPerformanceAlg.cxx:146
Base_Fragment.width
width
Definition: Sherpa_i/share/common/Base_Fragment.py:59
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
python.LArCondContChannels.isBarrel
isBarrel
Definition: LArCondContChannels.py:659
Muon::MuonStationIndex::ChIndex
ChIndex
enum to classify the different chamber layers in the muon spectrometer
Definition: MuonStationIndex.h:15
MuonStationIndex.h
ConstAccessor.h
Helper class to provide constant type-safe access to aux data.
MuonSegmentPerformanceAlg::m_nfake
std::vector< counter_t > m_nfake
Definition: MuonSegmentPerformanceAlg.h:53
MuonSegmentPerformanceAlg::m_ntruth
std::vector< counter_t > m_ntruth
Definition: MuonSegmentPerformanceAlg.h:51
CSL
@ CSL
Definition: ParabolaCscClusterFitter.h:25
MuonSegmentPerformanceAlg.h
PrepareReferenceFile.outfile
outfile
Definition: PrepareReferenceFile.py:42
SG::AllowEmpty
@ AllowEmpty
Definition: StoreGate/StoreGate/VarHandleKey.h:30
MuonSegmentReaderConfig.reco
reco
Definition: MuonSegmentReaderConfig.py:133