ATLAS Offline Software
MdtROD_Encoder.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // Implementation of MdtROD_Encoder class
6 
7 #include "MdtROD_Encoder.h"
8 
9 #include <cmath>
10 
11 #include <algorithm>
12 #include <map>
13 
14 #include "MDT_Hid2RESrcID.h"
15 #include "MdtAmtReadOut.h"
16 #include "MdtCsmReadOut.h"
17 #include "MdtHptdcReadOut.h"
19 #include "StoreGate/StoreGateSvc.h"
20 
22  if (!m_mdtIdHelper.retrieve().isSuccess()) { throw std::runtime_error("MdtROD_Encoder -- Failed to initialize the IdHelperSvc"); }
23 }
24 // Add a CSM
25 void MdtROD_Encoder::add(const MdtCsm* csm) { m_vMdtCsm.push_back(csm); }
26 
27 // Clear the vector of CSMs
28 void MdtROD_Encoder::clear() { m_vMdtCsm.erase(m_vMdtCsm.begin(), m_vMdtCsm.end()); }
29 
34 void MdtROD_Encoder::fillROD(std::vector<uint32_t>& v) {
35  MdtAmtReadOut amtReadOut{};
36  MdtHptdcReadOut hptdcReadOut{};
37  MdtCsmReadOut csmReadOut{};
38 
39  uint32_t mrod_wcnt{0};
40 
41  using hit_vector = std::vector<const MdtAmtHit*>;
42  using tdc_map = std::map<uint16_t, hit_vector>;
43  using masked_map = std::map<uint16_t, uint32_t>;
44 
45  tdc_map tdcMap;
46  masked_map maskedMap;
47 
48  // Insert the Beginning of Block Word (Lvl1ID not set yet)
49  uint32_t lvl1Id = 0;
50  v.push_back(csmReadOut.makeBOB(lvl1Id));
51 
52  // make the body of the ROD
53  // Loop on the CSMs
54  for (const MdtCsm* csm : m_vMdtCsm) {
55  const bool isHPTDC = m_mdtIdHelper->hasHPTDC(csm->identify());
56 
57  uint16_t ctwc = 0; // Trailer word count initialized
58 
59  v.push_back(csmReadOut.makeLWC(0)); // Link Word Count
60  v.push_back(csmReadOut.makeBOL(csm->MrodId(), csm->CsmId())); // Beginning of link
61 
62  v.push_back(csmReadOut.makeTLP(0));
63  ++ctwc; // Trailer word count starts from TLP
64 
65  // Group the hits of this CSM according to the TDC number
66  tdcMap.clear();
67  maskedMap.clear();
68  for (const MdtAmtHit* amt : *csm) {
69  uint16_t tdcNum = amt->tdcId();
70 
71  if (!amt->isMasked()) {
72  tdcMap[tdcNum].push_back(amt);
73  } else {
74  maskedMap[tdcNum] = maskedMap[tdcNum] | (int)pow(2, amt->channelId());
75  }
76  }
77 
78  // Loop on the TDCs and build the corresponding MROD blocks
79  uint16_t jt = 0;
80  tdc_map::const_iterator it_tdc = tdcMap.begin();
81  for (; it_tdc != tdcMap.end(); ++it_tdc) {
82  // Beginning of TDC header word
83  // Need to fix event counter and bcid
84  v.push_back((isHPTDC ? hptdcReadOut.makeBOT((*it_tdc).first, 0, 0) : amtReadOut.makeBOT((*it_tdc).first, 0, 0)));
85  ++ctwc;
86 
87  uint32_t maskedFlags = maskedMap[(*it_tdc).first];
88  uint16_t wcnt = 0; // TDC word count
89 
90  // Masked channels flags
91  if (maskedFlags != 0) {
92  v.push_back((isHPTDC ? 0 : amtReadOut.makeTMC(jt, maskedFlags)));
93  ++ctwc;
94  ++wcnt;
95  }
96 
97  // Loop on the hits for this TDC
98  hit_vector::const_iterator it_amtvec = (*it_tdc).second.begin();
99  for (; it_amtvec != (*it_tdc).second.end(); ++it_amtvec) {
100  uint16_t chan = (*it_amtvec)->channelId();
101 
102  uint16_t coarse = (*it_amtvec)->coarse();
103  uint16_t fine = (*it_amtvec)->fine();
104  uint16_t width = (*it_amtvec)->width();
105  uint16_t tdcId = (*it_amtvec)->tdcId();
106 
107  // Add a "Single Measurement" word
108  // v.push_back( isHPTDC ? hptdcReadOut.makeTSM(tdcId, chan, leading, coarse, fine)
109  // : amtReadOut.makeTSM(jt, chan, leading, errflag, coarse, fine) );
110 
111  // Add a combined measurement data word
112  v.push_back(
113  (isHPTDC ? hptdcReadOut.makeTCM(tdcId, chan, width, coarse, fine) : amtReadOut.makeTCM(jt, chan, width, coarse, fine)));
114 
115  ++ctwc; // CSM word count
116  ++wcnt; // TDC word count
117  }
118 
119  uint16_t jt{0}, tdcId{0}, ecnt{0}; // Event counter
120  // End of TDC trailer
121  v.push_back((isHPTDC ? hptdcReadOut.makeEOT(tdcId, ecnt, wcnt + 2) : amtReadOut.makeEOT(jt, ecnt, wcnt + 2)));
122  ++ctwc;
123  }
124 
125  // Last word of each csm: TWC trailer word count
126  uint16_t ecnt = 0;
127  v.push_back(csmReadOut.makeTWC(ecnt, ctwc));
128 
129  // Update the MROD word count
130  mrod_wcnt += ctwc;
131  mrod_wcnt += 2;
132  }
133 
134  // Close the MROD: MROD word count in EOB
135  v.push_back(csmReadOut.makeEOB(mrod_wcnt));
136 }
MdtAmtHit
MDT RDO's : data from a single channel of an AMT Atlas Muon TDC.
Definition: MdtAmtHit.h:20
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
MdtAmtReadOut
Definition: MdtAmtReadOut.h:15
xAOD::uint32_t
setEventNumber uint32_t
Definition: EventInfo_v1.cxx:127
MdtCsmReadOut.h
conifer::pow
constexpr int pow(int x)
Definition: conifer.h:20
MdtROD_Encoder::add
void add(const MdtCsm *csm)
initialize the map
Definition: MdtROD_Encoder.cxx:25
MdtCsm
MDT RDOs : Chamber Service Module, container of AmtHits of a single Mdt chamber.
Definition: MdtCsm.h:19
MdtROD_Encoder::clear
void clear()
clear the current csm list
Definition: MdtROD_Encoder.cxx:28
MdtROD_Encoder::m_mdtIdHelper
ServiceHandle< Muon::IMuonIdHelperSvc > m_mdtIdHelper
Definition: MdtROD_Encoder.h:43
MdtCsmReadOut
Definition: MdtCsmReadOut.h:15
xAOD::uint16_t
setWord1 uint16_t
Definition: eFexEMRoI_v1.cxx:88
MdtROD_Encoder::m_vMdtCsm
std::vector< const MdtCsm * > m_vMdtCsm
Definition: MdtROD_Encoder.h:44
MdtAmtReadOut.h
MdtHptdcReadOut
Definition: MdtHptdcReadOut.h:15
ReadCellNoiseFromCool.chan
chan
Definition: ReadCellNoiseFromCool.py:52
MdtROD_Encoder.h
MdtIdHelper.h
python.PyAthena.v
v
Definition: PyAthena.py:157
Base_Fragment.width
width
Definition: Sherpa_i/share/common/Base_Fragment.py:59
MDT_Hid2RESrcID.h
MdtROD_Encoder::fillROD
void fillROD(std::vector< uint32_t > &v)
convert all Csm in the current list to a vector of 32bit words
Definition: MdtROD_Encoder.cxx:34
MdtROD_Encoder::MdtROD_Encoder
MdtROD_Encoder()
constructor
Definition: MdtROD_Encoder.cxx:21
StoreGateSvc.h
MdtHptdcReadOut.h