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}
33StatusCode NswOccupancyAlg::execute(const EventContext& ctx) {
35 if (!pattCol.isValid()){
36 ATH_MSG_FATAL("Failed to retrieve the pattern collection "<<m_patternCollKey.fullKey());
37 return StatusCode::FAILURE;
38 }
39
40 for (const Muon::MuonPatternCombination* patComb : *pattCol ) {
42 std::map<unsigned int, Layer> sectorsHits{};
43 for (const Muon::MuonPatternChamberIntersect& it :patComb->chamberData()) {
44 if (it.prepRawDataVec().empty()) continue;
45 const Identifier id = it.prepRawDataVec().front()->identify();
46 if (!m_idHelperSvc->isMM(id) && !m_idHelperSvc->issTgc(id)) continue;
47 Layer& hitIds = sectorsHits[m_idHelperSvc->sector(id)];
48 hitIds.reserve(it.prepRawDataVec().size() + hitIds.size());
49
50 for (const Trk::PrepRawData* prd : it.prepRawDataVec()) {
51 if (prd->type(Trk::PrepRawDataType::MMPrepData)) hitIds.push_back(prd->identify());
52 }
53 }
54 for (auto& [sector, hitIds] : sectorsHits ) {
55 LayerVec sortedHits = sortByLayer(hitIds);
56 for (Layer& lay : sortedHits) {
57 fillHistograms(lay);
58 }
59 }
60 }
61
62 return StatusCode::SUCCESS;
63}
64std::vector<std::vector<Identifier>> NswOccupancyAlg::sortByLayer(Layer& micromegaHits) const{
65 LayerVec layerVec;
66 std::stable_sort(micromegaHits.begin(), micromegaHits.end(),[this](const Identifier& a, const Identifier& b){
67 return m_idHelperSvc->gasGapId(a) < m_idHelperSvc->gasGapId(b);
68 });
69 std::set<Identifier> unique_set{};
70 for (const Identifier& id : micromegaHits) {
71 if (!unique_set.insert(id).second) continue;
72 if (layerVec.empty() || m_idHelperSvc->gasGapId(id) != m_idHelperSvc->gasGapId(layerVec.back().back()) ){
73 layerVec.emplace_back();
74 layerVec.back().reserve(micromegaHits.size());
75 }
76 layerVec.back().push_back(id);
77 }
78 for (Layer& lay : layerVec) {
79 std::sort(lay.begin(),lay.end(),[this] (const Identifier&a ,const Identifier& b){
80 return m_idHelperSvc->mmIdHelper().channel(a) < m_idHelperSvc->mmIdHelper().channel(b);
81 });
82 }
83 return layerVec;
84}
85void NswOccupancyAlg::fillHistograms(const Layer& clustInLay) {
86 if (clustInLay.size() < 2) return;
87 std::vector<unsigned int> histogram{};
88 const unsigned int firstCh = m_idHelperSvc->mmIdHelper().channel(clustInLay[0]);
89 const unsigned int lastCh = m_idHelperSvc->mmIdHelper().channel(clustInLay[clustInLay.size() -1]);
91 const unsigned int deltaCh = lastCh - firstCh;
92 const unsigned int nBins = deltaCh / m_binWidth + (deltaCh % m_binWidth > 0);
93 if (!nBins) return;
94 histogram.resize(nBins);
95 for (const Identifier&id : clustInLay) {
96 const int bin_num = (m_idHelperSvc->mmIdHelper().channel(id) - firstCh) %nBins;
97 ++(histogram[bin_num]);
98 }
99 for (unsigned int occup : histogram){
100 if (occup) m_singleBinOccupancy->Fill(occup);
101 }
102 for (unsigned int i = 0 ; i < histogram.size() -1 ; ++i){
103 unsigned int pair = histogram[i]+ histogram[i+1];
104 if (!pair) continue;
106
108 if (histogram[i+1]) m_pairVsSingleOccupancy->Fill(histogram[i+1], pair);
109
110 }
111
112}
113
#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
NswOccupancyAlg(const std::string &name, ISvcLocator *pSvcLocator)
StatusCode execute(const EventContext &ctx) override
Execute method.
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.