ATLAS Offline Software
Loading...
Searching...
No Matches
NswOccupancyAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3*/
4#include "NswOccupancyAlg.h"
6
7#include <TH1I.h>
8#include <TH2I.h>
9#include <set>
10
11NswOccupancyAlg::NswOccupancyAlg(const std::string& name, ISvcLocator* pSvcLocator):
12 AthHistogramAlgorithm(name, pSvcLocator){}
13
15 ATH_CHECK(m_idHelperSvc.retrieve());
16 ATH_CHECK(m_patternCollKey.initialize());
17 std::stringstream selection_title{};
18 selection_title<<"Bin size "<<m_binWidth.value()<<";";
19 selection_title<<"Hit occupancy;";
20 m_singleBinOccupancy = new TH1I("SingleBinOccupancy", selection_title.str().c_str(), 50, 0, 50);
21 m_pairBinOccupancy = new TH1I("PairBinOccupancy", selection_title.str().c_str(), 50, 0, 50);
22 selection_title<<"; Pair occupancy";
23 m_pairVsSingleOccupancy = new TH2I("PairVsSingleOccupancy",selection_title.str().c_str(), 50,0,50,50,0,50);
24 std::stringstream folderName{};
25 folderName<<"/"<<m_fileStream.value()<<"/OccupancyBinWidth"<<m_binWidth.value()<<"/";
26 ATH_MSG_INFO("Write out histograms in "<<folderName.str());
27 ATH_CHECK(histSvc()->regHist(folderName.str() + m_singleBinOccupancy->GetName(), m_singleBinOccupancy));
28 ATH_CHECK(histSvc()->regHist(folderName.str() + m_pairBinOccupancy->GetName(), m_pairBinOccupancy));
29 ATH_CHECK(histSvc()->regHist(folderName.str() + m_pairVsSingleOccupancy->GetName(), m_pairVsSingleOccupancy));
30
31 return StatusCode::SUCCESS;
32}
34 const EventContext& ctx = Gaudi::Hive::currentContext();
36 if (!pattCol.isValid()){
37 ATH_MSG_FATAL("Failed to retrieve the pattern collection "<<m_patternCollKey.fullKey());
38 return StatusCode::FAILURE;
39 }
40
41 for (const Muon::MuonPatternCombination* patComb : *pattCol ) {
43 std::map<unsigned int, Layer> sectorsHits{};
44 for (const Muon::MuonPatternChamberIntersect& it :patComb->chamberData()) {
45 if (it.prepRawDataVec().empty()) continue;
46 const Identifier id = it.prepRawDataVec().front()->identify();
47 if (!m_idHelperSvc->isMM(id) && !m_idHelperSvc->issTgc(id)) continue;
48 Layer& hitIds = sectorsHits[m_idHelperSvc->sector(id)];
49 hitIds.reserve(it.prepRawDataVec().size() + hitIds.size());
50
51 for (const Trk::PrepRawData* prd : it.prepRawDataVec()) {
52 if (prd->type(Trk::PrepRawDataType::MMPrepData)) hitIds.push_back(prd->identify());
53 }
54 }
55 for (auto& [sector, hitIds] : sectorsHits ) {
56 LayerVec sortedHits = sortByLayer(hitIds);
57 for (Layer& lay : sortedHits) {
58 fillHistograms(lay);
59 }
60 }
61 }
62
63 return StatusCode::SUCCESS;
64}
65std::vector<std::vector<Identifier>> NswOccupancyAlg::sortByLayer(Layer& micromegaHits) const{
66 LayerVec layerVec;
67 std::stable_sort(micromegaHits.begin(), micromegaHits.end(),[this](const Identifier& a, const Identifier& b){
68 return m_idHelperSvc->gasGapId(a) < m_idHelperSvc->gasGapId(b);
69 });
70 std::set<Identifier> unique_set{};
71 for (const Identifier& id : micromegaHits) {
72 if (!unique_set.insert(id).second) continue;
73 if (layerVec.empty() || m_idHelperSvc->gasGapId(id) != m_idHelperSvc->gasGapId(layerVec.back().back()) ){
74 layerVec.emplace_back();
75 layerVec.back().reserve(micromegaHits.size());
76 }
77 layerVec.back().push_back(id);
78 }
79 for (Layer& lay : layerVec) {
80 std::sort(lay.begin(),lay.end(),[this] (const Identifier&a ,const Identifier& b){
81 return m_idHelperSvc->mmIdHelper().channel(a) < m_idHelperSvc->mmIdHelper().channel(b);
82 });
83 }
84 return layerVec;
85}
86void NswOccupancyAlg::fillHistograms(const Layer& clustInLay) {
87 if (clustInLay.size() < 2) return;
88 std::vector<unsigned int> histogram{};
89 const unsigned int firstCh = m_idHelperSvc->mmIdHelper().channel(clustInLay[0]);
90 const unsigned int lastCh = m_idHelperSvc->mmIdHelper().channel(clustInLay[clustInLay.size() -1]);
92 const unsigned int deltaCh = lastCh - firstCh;
93 const unsigned int nBins = deltaCh / m_binWidth + (deltaCh % m_binWidth > 0);
94 if (!nBins) return;
95 histogram.resize(nBins);
96 for (const Identifier&id : clustInLay) {
97 const int bin_num = (m_idHelperSvc->mmIdHelper().channel(id) - firstCh) %nBins;
98 ++(histogram[bin_num]);
99 }
100 for (unsigned int occup : histogram){
101 if (occup) m_singleBinOccupancy->Fill(occup);
102 }
103 for (unsigned int i = 0 ; i < histogram.size() -1 ; ++i){
104 unsigned int pair = histogram[i]+ histogram[i+1];
105 if (!pair) continue;
107
109 if (histogram[i+1]) m_pairVsSingleOccupancy->Fill(histogram[i+1], pair);
110
111 }
112
113}
114
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_FATAL(x)
#define ATH_MSG_INFO(x)
static Double_t a
Handle class for reading from StoreGate.
std::string histogram
Definition chains.cxx:52
const ServiceHandle< ITHistSvc > & histSvc() const
The standard THistSvc (for writing histograms and TTrees and more to a root file) Returns (kind of) a...
AthHistogramAlgorithm(const std::string &name, ISvcLocator *pSvcLocator)
Constructor with parameters:
This class holds information needed for the Moore and MoMu pattern recognition for a muon chamber.
The MuonPatternCombination class provides the means to store the output of the initial global pattern...
TH1 * m_pairVsSingleOccupancy
std::vector< Layer > LayerVec
void fillHistograms(const Layer &layers)
StatusCode initialize() override
Gaudi::Property< std::string > m_fileStream
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
std::vector< Identifier > Layer
Gaudi::Property< unsigned int > m_binWidth
LayerVec sortByLayer(std::vector< Identifier > &micromegaHits) const
StatusCode execute() override
NswOccupancyAlg(const std::string &name, ISvcLocator *pSvcLocator)
SG::ReadHandleKey< MuonPatternCombinationCollection > m_patternCollKey
virtual bool isValid() override final
Can the handle be successfully dereferenced?
STL class.
void sort(typename DataModel_detail::iterator< DVL > beg, typename DataModel_detail::iterator< DVL > end)
Specialization of sort for DataVector/List.
void stable_sort(DataModel_detail::iterator< DVL > beg, DataModel_detail::iterator< DVL > end)
Specialization of stable_sort for DataVector/List.