ATLAS Offline Software
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"
5 #include <StoreGate/ReadHandle.h>
6 
7 #include <TH1I.h>
8 #include <TH2I.h>
9 #include <set>
10 
11 NswOccupancyAlg::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));
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 }
65 std::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 }
86 void 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;
106  m_pairBinOccupancy->Fill(pair);
107 
109  if (histogram[i+1]) m_pairVsSingleOccupancy->Fill(histogram[i+1], pair);
110 
111  }
112 
113 }
114 
AthHistogramAlgorithm::histSvc
const ServiceHandle< ITHistSvc > & histSvc() const
The standard THistSvc (for writing histograms and TTrees and more to a root file) Returns (kind of) a...
Definition: AthHistogramAlgorithm.h:113
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
NswOccupancyAlg::execute
StatusCode execute() override
Definition: NswOccupancyAlg.cxx:33
Trk::PrepRawDataType::MMPrepData
@ MMPrepData
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
TH1I
Definition: rootspy.cxx:332
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
NswOccupancyAlg::m_pairBinOccupancy
TH1 * m_pairBinOccupancy
Definition: NswOccupancyAlg.h:44
skel.it
it
Definition: skel.GENtoEVGEN.py:423
NswOccupancyAlg::fillHistograms
void fillHistograms(const Layer &layers)
Definition: NswOccupancyAlg.cxx:86
NswOccupancyAlg::m_patternCollKey
SG::ReadHandleKey< MuonPatternCombinationCollection > m_patternCollKey
Definition: NswOccupancyAlg.h:33
NswOccupancyAlg::m_singleBinOccupancy
TH1 * m_singleBinOccupancy
Definition: NswOccupancyAlg.h:43
NswOccupancyAlg::LayerVec
std::vector< Layer > LayerVec
Definition: NswOccupancyAlg.h:26
NswOccupancyAlg::Layer
std::vector< Identifier > Layer
Definition: NswOccupancyAlg.h:25
NswOccupancyAlg.h
lumiFormat.i
int i
Definition: lumiFormat.py:92
Muon::MuonPatternChamberIntersect
This class holds information needed for the Moore and MoMu pattern recognition for a muon chamber.
Definition: MuonPatternChamberIntersect.h:38
Identifier
Definition: DetectorDescription/Identifier/Identifier/Identifier.h:32
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
TH2I
Definition: rootspy.cxx:410
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
TH1::Fill
int Fill(double)
Definition: rootspy.cxx:285
AthHistogramAlgorithm
Definition: AthHistogramAlgorithm.h:32
Trk::PrepRawData
Definition: PrepRawData.h:62
dumpTgcDigiJitter.nBins
list nBins
Definition: dumpTgcDigiJitter.py:29
CaloCellTimeCorrFiller.folderName
string folderName
Definition: CaloCellTimeCorrFiller.py:20
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
NswOccupancyAlg::sortByLayer
LayerVec sortByLayer(std::vector< Identifier > &micromegaHits) const
Definition: NswOccupancyAlg.cxx:65
NswOccupancyAlg::m_pairVsSingleOccupancy
TH1 * m_pairVsSingleOccupancy
Definition: NswOccupancyAlg.h:45
NswOccupancyAlg::m_idHelperSvc
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
Definition: NswOccupancyAlg.h:22
a
TList * a
Definition: liststreamerinfos.cxx:10
NswOccupancyAlg::initialize
StatusCode initialize() override
Definition: NswOccupancyAlg.cxx:14
NswOccupancyAlg::NswOccupancyAlg
NswOccupancyAlg(const std::string &name, ISvcLocator *pSvcLocator)
Definition: NswOccupancyAlg.cxx:11
NswOccupancyAlg::m_fileStream
Gaudi::Property< std::string > m_fileStream
Definition: NswOccupancyAlg.h:40
ReadHandle.h
Handle class for reading from StoreGate.
Muon::MuonPatternCombination
The MuonPatternCombination class provides the means to store the output of the initial global pattern...
Definition: MuonPatternCombination.h:29
histogram
std::string histogram
Definition: chains.cxx:52
NswOccupancyAlg::m_binWidth
Gaudi::Property< unsigned int > m_binWidth
Definition: NswOccupancyAlg.h:41