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 MuonSegmentPerformanceAlg::MuonSegmentPerformanceAlg(const std::string& name, ISvcLocator* pSvcLocator) :
13  AthAlgorithm(name, pSvcLocator),
14  m_writeToFile(false),
15  m_nevents(0) {
16 
17  declareProperty("writeToFile", m_writeToFile = false);
18  declareProperty("FileName", m_fileName = "MuonSegmentPerformanceAlg.txt");
19 }
20 
22  // initialize cuts, please make sure the number of bins and the sizes of the cuts + string are always the same
23  unsigned int nbins = 3;
24  m_nevents = 0;
25  m_ntruth.resize(nbins);
26  m_nfound.resize(nbins);
27  m_nfake.resize(nbins);
28  m_nhitCuts = {3, 4, 5};
29  m_hitCutString = {"n==3 ", "n==4 ", "n>=5 "};
30  for (unsigned int i = 0; i < nbins; ++i) {
34  }
35 
36  if (!m_segmentKey.key().empty()) ATH_CHECK(m_segmentKey.initialize());
37  if (!m_truthSegmentKey.key().empty()) ATH_CHECK(m_truthSegmentKey.initialize());
38  if (!m_truthSegmenLinkKey.key().empty()) ATH_CHECK(m_truthSegmenLinkKey.initialize());
39 
40  return StatusCode::SUCCESS;
41 }
42 
44  const xAOD::MuonSegmentContainer* segments = nullptr;
45  if (!retrieve(m_segmentKey, segments) || !segments) return StatusCode::SUCCESS;
46 
47  const xAOD::MuonSegmentContainer* truthSegments = nullptr;
48  if (!retrieve(m_truthSegmentKey, truthSegments) || !truthSegments) return StatusCode::SUCCESS;
49  std::set<const xAOD::MuonSegment*> matchedSegments;
50  ++m_nevents;
51 
52  bool missedSegment = false;
53  for (const auto seg : *truthSegments) {
54  int chIndex = seg->chamberIndex();
55  if (chIndex < 0 || chIndex >= Muon::MuonStationIndex::ChIndexMax) {
56  ATH_MSG_WARNING("bad index " << chIndex);
57  continue;
58  }
59  unsigned int index = 0;
60  if (seg->nPrecisionHits() < 3) continue;
61  while ((index < m_nhitCuts.size() - 1) && seg->nPrecisionHits() > m_nhitCuts[index] ) ++index;
62 
63  ++m_ntruth[index][chIndex];
64 
65  if (index == 2) {
66  if (Muon::MuonStationIndex::chName(static_cast<Muon::MuonStationIndex::ChIndex>(chIndex)) == "CSS")
67  ATH_MSG_WARNING(" CSS with more than 4 layers ");
68  if (Muon::MuonStationIndex::chName(static_cast<Muon::MuonStationIndex::ChIndex>(chIndex)) == "CSL")
69  ATH_MSG_WARNING(" CSL with more than 4 layers ");
70  }
72  recoSegmentLinkAcc("recoSegmentLink");
73  const ElementLink<xAOD::MuonSegmentContainer>& recoLink = recoSegmentLinkAcc(*seg);
74  if (recoLink.isValid()) {
75  ++m_nfound[index][chIndex];
76  matchedSegments.insert(*recoLink);
77  } else {
78  ATH_MSG_DEBUG(" Missing segment in sector "
79  << seg->sector() << " " << Muon::MuonStationIndex::chName(static_cast<Muon::MuonStationIndex::ChIndex>(chIndex))
80  << " eta " << seg->etaIndex() << " nprec " << seg->nPrecisionHits() << " nphi " << seg->nPhiLayers()
81  << " nTrigEta " << seg->nTrigEtaLayers());
82  missedSegment = true;
83  }
84  }
85  if (missedSegment) ATH_MSG_DEBUG(" Dump Fake segments ");
86 
87  for (const auto seg : *segments) {
88  if (matchedSegments.count(seg)) continue;
89  int chIndex = seg->chamberIndex();
90  if (chIndex < 0 || chIndex >= Muon::MuonStationIndex::ChIndexMax) {
91  ATH_MSG_WARNING("bad index " << chIndex);
92  continue;
93  }
94 
95  unsigned int index = 0;
96  if (seg->nPrecisionHits() < 3) continue;
97  while ((index < m_nhitCuts.size() - 1) and (seg->nPrecisionHits() > m_nhitCuts[index]) ) ++index;
98  if (missedSegment)
99  ATH_MSG_DEBUG(" Fake segment in sector "
100  << seg->sector() << " " << Muon::MuonStationIndex::chName(static_cast<Muon::MuonStationIndex::ChIndex>(chIndex))
101  << " eta " << seg->etaIndex() << " nprec " << seg->nPrecisionHits() << " nphi " << seg->nPhiLayers()
102  << " nTrigEta " << seg->nTrigEtaLayers());
103 
104  ++m_nfake[index][chIndex];
105  }
106 
107  return StatusCode::SUCCESS;
108 }
109 
110 std::string MuonSegmentPerformanceAlg::printRatio(const std::string& prefix, unsigned int begin, unsigned int end, const std::vector<int>& reco,
111  const std::vector<int>& truth) const {
112  std::ostringstream sout;
113  unsigned int width = 9;
114  unsigned int precision = 3;
115  sout << std::endl << prefix;
116  for (unsigned int i = begin; i < end; ++i) {
117  sout << std::setw(width) << std::setprecision(precision);
118  if (truth[i] == 0)
119  sout << " ";
120  else
121  sout << static_cast<double>(reco[i]) / static_cast<double>(truth[i]);
122  }
123  sout << std::endl
124  << " #Events "
125  << " ";
126  for (unsigned int i = begin; i < end; ++i) {
127  sout << std::setw(width) << std::setprecision(precision);
128  if (truth[i] == 0)
129  sout << " ";
130  else
131  sout << static_cast<double>(truth[i]);
132  }
133  return sout.str();
134 }
135 std::string MuonSegmentPerformanceAlg::printRatio(const std::string& prefix, unsigned int begin, unsigned int end,
136  const std::vector<int>& reco) const {
137  std::ostringstream sout;
138  unsigned int width = 9;
139  unsigned int precision = 3;
140  sout << std::endl << prefix;
141  for (unsigned int i = begin; i < end; ++i) {
142  sout << std::setw(width) << std::setprecision(precision);
143  if (m_nevents == 0)
144  sout << " ";
145  else
146  sout << static_cast<double>(reco[i]) / static_cast<double>(m_nevents);
147  }
148  return sout.str();
149 }
150 
152  std::ofstream fileOutput;
153  std::string outfile = "muonPerformance_segments.txt";
154  fileOutput.open(outfile.c_str(), std::ios::trunc);
155  std::ostringstream sout;
156  sout.precision(4);
157 
158  unsigned int width = 9;
159  sout << "Segment finding efficiencies barrel" << std::endl;
160  sout << " Chambers ";
161  std::string prefix_eff = " Efficiency ";
162  std::string prefix_fake = " Fake rate ";
163  for (unsigned int i = 0; i < Muon::MuonStationIndex::BEE; ++i)
165  for (unsigned int j = 0; j < m_nfound.size(); ++j) {
166  sout << printRatio(prefix_eff + m_hitCutString[j], 0, Muon::MuonStationIndex::BEE, m_nfound[j], m_ntruth[j]);
167  }
168  sout << std::endl;
169  for (unsigned int j = 0; j < m_nfound.size(); ++j) {
170  sout << printRatio(prefix_fake + m_hitCutString[j], 0, Muon::MuonStationIndex::BEE, m_nfake[j]);
171  }
172  sout << std::endl;
173 
174  sout << "Segment finding efficiencies endcaps" << std::endl;
175  sout << " Chambers ";
178  for (unsigned int j = 0; j < m_nfound.size(); ++j) {
180  m_ntruth[j]);
181  }
182  sout << std::endl;
183  for (unsigned int j = 0; j < m_nfound.size(); ++j) {
185  }
186  sout << std::endl;
187  fileOutput << sout.str() << std::endl;
188  fileOutput.close();
189  return StatusCode::SUCCESS;
190 }
191 
193  const xAOD::MuonSegmentContainer*& ptr) const {
195  if (!handle.isPresent() || !handle.isValid()) { // Cautious isPresent TODO remove once job options are more intelligent
196  ATH_MSG_WARNING("Unable to retrieve " << segments.key());
197  ptr = nullptr;
198  return false;
199  }
200  ptr = handle.cptr();
201  ATH_MSG_DEBUG("Retrieved " << segments.key() << " size " << ptr->size());
202  return true;
203 }
Muon::MuonStationIndex::chName
static const std::string & chName(ChIndex index)
convert ChIndex into a string
Definition: MuonStationIndex.cxx:157
MuonSegmentPerformanceAlg::initialize
virtual StatusCode initialize() override
Definition: MuonSegmentPerformanceAlg.cxx:21
MuonSegmentPerformanceAlg::m_segmentKey
SG::ReadHandleKey< xAOD::MuonSegmentContainer > m_segmentKey
Definition: MuonSegmentPerformanceAlg.h:42
SG::ReadHandle::cptr
const_pointer_type cptr()
Dereference the pointer.
MuonSegmentPerformanceAlg::m_ntruth
std::vector< std::vector< int > > m_ntruth
Definition: MuonSegmentPerformanceAlg.h:49
MuonSegmentPerformanceAlg::m_truthSegmentKey
SG::ReadHandleKey< xAOD::MuonSegmentContainer > m_truthSegmentKey
Definition: MuonSegmentPerformanceAlg.h:43
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
index
Definition: index.py:1
AthCommonDataStore< AthCommonMsg< Algorithm > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
MuonSegmentPerformanceAlg::m_hitCutString
std::vector< std::string > m_hitCutString
Definition: MuonSegmentPerformanceAlg.h:48
MuonSegment.h
PlotCalibFromCool.begin
begin
Definition: PlotCalibFromCool.py:94
MuonSegmentPerformanceAlg::printRatio
std::string printRatio(const std::string &prefix, unsigned int begin, unsigned int end, const std::vector< int > &reco, const std::vector< int > &truth) const
Definition: MuonSegmentPerformanceAlg.cxx:110
MuonSegmentPerformanceAlg::m_nhitCuts
std::vector< int > m_nhitCuts
Definition: MuonSegmentPerformanceAlg.h:47
SG::ConstAccessor
Helper class to provide constant type-safe access to aux data.
Definition: ConstAccessor.h:54
SG::VarHandleKey::key
const std::string & key() const
Return the StoreGate ID for the referenced object.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:141
SG::ReadHandleKey
Property holding a SG store/key/clid from which a ReadHandle is made.
Definition: StoreGate/StoreGate/ReadHandleKey.h:39
MuonSegmentPerformanceAlg::MuonSegmentPerformanceAlg
MuonSegmentPerformanceAlg(const std::string &name, ISvcLocator *pSvcLocator)
Definition: MuonSegmentPerformanceAlg.cxx:12
mergePhysValFiles.end
end
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:93
MuonSegmentPerformanceAlg::m_nevents
unsigned int m_nevents
Definition: MuonSegmentPerformanceAlg.h:46
MuonSegmentContainer.h
SCT_CalibAlgs::nbins
@ nbins
Definition: SCT_CalibNumbers.h:10
MuonSegmentPerformanceAlg::m_nfake
std::vector< std::vector< int > > m_nfake
Definition: MuonSegmentPerformanceAlg.h:51
MuonSegmentPerformanceAlg::m_nfound
std::vector< std::vector< int > > m_nfound
Definition: MuonSegmentPerformanceAlg.h:50
lumiFormat.i
int i
Definition: lumiFormat.py:92
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
MuonSegmentPerformanceAlg::m_truthSegmenLinkKey
SG::ReadDecorHandleKey< xAOD::MuonSegmentContainer > m_truthSegmenLinkKey
Definition: MuonSegmentPerformanceAlg.h:44
jobOptions.fileOutput
fileOutput
Definition: jobOptions.py:39
checkCorrelInHIST.prefix
dictionary prefix
Definition: checkCorrelInHIST.py:391
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
MuonSegmentPerformanceAlg::retrieve
bool retrieve(const SG::ReadHandleKey< xAOD::MuonSegmentContainer > &, const xAOD::MuonSegmentContainer *&ptr) const
Definition: MuonSegmentPerformanceAlg.cxx:192
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
MuonSegmentPerformanceAlg::execute
virtual StatusCode execute() override
Definition: MuonSegmentPerformanceAlg.cxx:43
AthAlgorithm
Definition: AthAlgorithm.h:47
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
MuonSegmentPerformanceAlg::m_writeToFile
bool m_writeToFile
name of external file to write statistics
Definition: MuonSegmentPerformanceAlg.h:36
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
MuonSegmentPerformanceAlg::m_fileName
std::string m_fileName
Definition: MuonSegmentPerformanceAlg.h:37
Muon::MuonStationIndex::ChIndexMax
@ ChIndexMax
Definition: MuonStationIndex.h:19
DeMoScan.index
string index
Definition: DeMoScan.py:362
MuonSegmentPerformanceAlg::finalize
virtual StatusCode finalize() override
Definition: MuonSegmentPerformanceAlg.cxx:151
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
Muon::MuonStationIndex::BEE
@ BEE
Definition: MuonStationIndex.h:17
Muon::MuonStationIndex::ChIndex
ChIndex
enum to classify the different chamber layers in the muon spectrometer
Definition: MuonStationIndex.h:15
SG::VarHandleBase::isPresent
bool isPresent() const
Is the referenced object present in SG?
Definition: StoreGate/src/VarHandleBase.cxx:397
MuonStationIndex.h
ConstAccessor.h
Helper class to provide constant type-safe access to aux data.
MuonSegmentPerformanceAlg.h
PrepareReferenceFile.outfile
outfile
Definition: PrepareReferenceFile.py:42
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
MuonSegmentReaderConfig.reco
reco
Definition: MuonSegmentReaderConfig.py:133