CMS 3D CMS Logo

SiStripSpyDigiConverter.cc
Go to the documentation of this file.
1 #include <vector>
2 
5 
11 
14 
17 
20 
21 namespace sistrip {
22 
23  std::unique_ptr<SpyDigiConverter::DSVRawDigis> SpyDigiConverter::extractPayloadDigis(
24  const DSVRawDigis* inputScopeDigis,
25  std::vector<uint32_t>* pAPVAddresses,
26  const bool discardDigisWithAPVAddrErr,
28  const uint16_t expectedPos) {
29  // Data is already sorted so push back fast into vector to avoid sorts and create DSV later
30  std::vector<DetSetRawDigis> outputData;
31  outputData.reserve(inputScopeDigis->size());
32 
33  //APV address vector indexed by fedid, majority value written.
34  pAPVAddresses->resize(sistrip::FED_ID_MAX + 1, 0);
35  std::vector<uint16_t> lAddrVec;
36  lAddrVec.reserve(2 * sistrip::FEDCH_PER_FED);
37  uint16_t lPreviousFedId = 0;
38  std::vector<uint16_t> lHeaderBitVec;
39  lHeaderBitVec.reserve(sistrip::FEDCH_PER_FED);
40  std::vector<uint16_t> lTrailBitVec;
41  lTrailBitVec.reserve(sistrip::FEDCH_PER_FED);
42 
43  //local DSVRawDigis per FED
44  std::vector<DSVRawDigis::const_iterator> lFedScopeDigis;
45  lFedScopeDigis.reserve(sistrip::FEDCH_PER_FED);
46 
47  // Loop over channels in input collection
48  DSVRawDigis::const_iterator inputChannel = inputScopeDigis->begin();
49  const DSVRawDigis::const_iterator endChannels = inputScopeDigis->end();
50  bool hasBeenProcessed = false;
51 
52  for (; inputChannel != endChannels; ++inputChannel) {
53  const uint32_t lFedIndex = inputChannel->detId();
54  const uint16_t fedId = static_cast<uint16_t>((lFedIndex >> 16) & 0xFFFF);
55 
56  // Fill frame parameters. Second parameter is to print debug info (if logDebug enabled....)
57  const sistrip::SpyUtilities::Frame lFrame = sistrip::SpyUtilities::extractFrameInfo(*inputChannel, false);
58 
59  if (lPreviousFedId == 0) {
60  lPreviousFedId = fedId;
61  }
62 
63  //print out warning only for non-empty frames....
64  if (!sistrip::SpyUtilities::isValid(lFrame, aQuality, expectedPos)) {
66  // edm::LogWarning("SiStripSpyDigiConverter") << " FED ID: " << fedId << ", channel: " << fedCh << std::endl
67  // << sistrip::SpyUtilities::print(lFrame,
68  // std::string(" -- Invalid Frame ")
69  // );
70  }
71  continue;
72  }
73 
74  //fill local vectors per FED
75  if (fedId == lPreviousFedId) {
76  if (hasBeenProcessed)
77  hasBeenProcessed = false;
78  }
79  if (fedId != lPreviousFedId) {
80  SpyDigiConverter::processFED(lPreviousFedId,
81  discardDigisWithAPVAddrErr,
82  pAPVAddresses,
83  outputData,
84  lAddrVec,
85  lHeaderBitVec,
86  lTrailBitVec,
87  lFedScopeDigis);
88  lPreviousFedId = fedId;
89  hasBeenProcessed = true;
90  }
91  // add digis
92  lFedScopeDigis.push_back(inputChannel);
93  lAddrVec.push_back(lFrame.apvAddress.first);
94  lAddrVec.push_back(lFrame.apvAddress.second);
95  lHeaderBitVec.push_back(lFrame.firstHeaderBit);
96  lTrailBitVec.push_back(lFrame.firstTrailerBit);
97 
98  } // end of loop over channels.
99 
100  //process the last one if not already done.
101  if (!hasBeenProcessed) {
102  SpyDigiConverter::processFED(lPreviousFedId,
103  discardDigisWithAPVAddrErr,
104  pAPVAddresses,
105  outputData,
106  lAddrVec,
107  lHeaderBitVec,
108  lTrailBitVec,
109  lFedScopeDigis);
110  }
111 
112  //return DSV of output
113  return std::unique_ptr<DSVRawDigis>(new DSVRawDigis(outputData, true));
114 
115  } // end of SpyDigiConverter::extractPayloadDigis method
116 
117  void SpyDigiConverter::processFED(const uint16_t aPreviousFedId,
118  const bool discardDigisWithAPVAddrErr,
119  std::vector<uint32_t>* pAPVAddresses,
120  std::vector<DetSetRawDigis>& outputData,
121  std::vector<uint16_t>& aAddrVec,
122  std::vector<uint16_t>& aHeaderBitVec,
123  std::vector<uint16_t>& aTrailBitVec,
124  std::vector<DSVRawDigis::const_iterator>& aFedScopeDigis) {
125  //extract majority address
126  uint32_t lMaj = sistrip::SpyUtilities::findMajorityValue(aAddrVec, aPreviousFedId).first;
127  if (pAPVAddresses)
128  (*pAPVAddresses)[aPreviousFedId] = lMaj;
129 
130  //loop over iterators and fill payload
131  std::vector<DSVRawDigis::const_iterator>::iterator lIter;
132  unsigned int lCh = 0;
133  for (lIter = aFedScopeDigis.begin(); lIter != aFedScopeDigis.end(); ++lIter, ++lCh) {
134  //discard if APV address different from majority.
135  //Keep if only one of them is wrong: the other APV might be alright ??
136 
137  if (discardDigisWithAPVAddrErr && aAddrVec[2 * lCh] != lMaj && aAddrVec[2 * lCh + 1] != lMaj) {
138  continue;
139  }
140 
141  DetSetRawDigis::const_iterator iDigi = (*lIter)->begin();
142  const DetSetRawDigis::const_iterator endOfChannel = (*lIter)->end();
143 
144  if (iDigi == endOfChannel) {
145  continue;
146  }
147 
148  //header starts in sample firstHeaderBit and is 18+6 samples long
149  const DetSetRawDigis::const_iterator payloadBegin = iDigi + aHeaderBitVec[lCh] + 24;
150  const DetSetRawDigis::const_iterator payloadEnd = payloadBegin + STRIPS_PER_FEDCH;
151 
152  if (payloadEnd - iDigi >= endOfChannel - iDigi)
153  continue; // few-cases where this is possible, i.e. nothing above frame-threhsold
154 
155  // Copy data into output collection
156  // Create new detSet with same key (in this case it is the fedKey, not detId)
157  outputData.push_back(DetSetRawDigis((*lIter)->detId()));
158  std::vector<SiStripRawDigi>& outputDetSetData = outputData.back().data;
159  outputDetSetData.resize(STRIPS_PER_FEDCH);
160  std::vector<SiStripRawDigi>::iterator outputBegin = outputDetSetData.begin();
161  std::copy(payloadBegin, payloadEnd, outputBegin);
162  }
163 
164  aFedScopeDigis.clear();
165  aAddrVec.clear();
166  aHeaderBitVec.clear();
167  aTrailBitVec.clear();
168 
169  aAddrVec.reserve(2 * sistrip::FEDCH_PER_FED);
170  aHeaderBitVec.reserve(sistrip::FEDCH_PER_FED);
171  aTrailBitVec.reserve(sistrip::FEDCH_PER_FED);
172  aFedScopeDigis.reserve(sistrip::FEDCH_PER_FED);
173  }
174 
175  std::unique_ptr<SpyDigiConverter::DSVRawDigis> SpyDigiConverter::reorderDigis(const DSVRawDigis* inputPayloadDigis) {
176  // Data is already sorted so push back fast into vector to avoid sorts and create DSV later
177  std::vector<DetSetRawDigis> outputData;
178  outputData.reserve(inputPayloadDigis->size());
179 
180  // Loop over channels in input collection
181  for (DSVRawDigis::const_iterator inputChannel = inputPayloadDigis->begin();
182  inputChannel != inputPayloadDigis->end();
183  ++inputChannel) {
184  const std::vector<SiStripRawDigi>& inputDetSetData = inputChannel->data;
185  outputData.push_back(DetSetRawDigis(inputChannel->detId()));
186  std::vector<SiStripRawDigi>& outputDetSetData = outputData.back().data;
187  outputDetSetData.resize(STRIPS_PER_FEDCH);
188  // Copy the data into the output vector reordering
189  for (uint16_t readoutOrderStripIndex = 0; readoutOrderStripIndex < inputDetSetData.size();
190  ++readoutOrderStripIndex) {
191  const uint16_t physicalOrderStripIndex =
192  FEDStripOrdering::physicalOrderForStripInChannel(readoutOrderStripIndex);
193  outputDetSetData.at(physicalOrderStripIndex) = inputDetSetData.at(readoutOrderStripIndex);
194  }
195  }
196 
197  //return DSV of output
198  return std::unique_ptr<DSVRawDigis>(new DSVRawDigis(outputData, true));
199  } // end of SpyDigiConverter::reorderDigis method.
200 
201  std::unique_ptr<SpyDigiConverter::DSVRawDigis> SpyDigiConverter::mergeModuleChannels(
202  const DSVRawDigis* inputPhysicalOrderChannelDigis, const SiStripFedCabling& cabling) {
203  // Create filler for detSetVector to create output (with maximum number of DetSets and digis)
204  uint16_t nFeds = static_cast<uint16_t>(FED_ID_MAX - FED_ID_MIN + 1);
205 
206  RawDigiDetSetVectorFiller dsvFiller(nFeds * FEDCH_PER_FED / 2, nFeds * FEDCH_PER_FED * STRIPS_PER_FEDCH);
207  // Loop over FEDs in cabling
208  auto iFed = cabling.fedIds().begin();
209  auto endFeds = cabling.fedIds().end();
210  for (; iFed != endFeds; ++iFed) {
211  // Loop over cabled channels
212  auto conns = cabling.fedConnections(*iFed);
213  auto iConn = conns.begin();
214  auto endConns = conns.end();
215  for (; iConn != endConns; ++iConn) {
216  // Skip channels not connected to a detector.
217  if (!iConn->isConnected())
218  continue;
219  if (iConn->detId() == sistrip::invalid32_)
220  continue;
221 
222  // Find the data from the input collection
223  const uint32_t fedIndex = ((iConn->fedId() & sistrip::invalid_) << 16) | (iConn->fedCh() & sistrip::invalid_);
224  const DSVRawDigis::const_iterator iDetSet = inputPhysicalOrderChannelDigis->find(fedIndex);
225  if (iDetSet == inputPhysicalOrderChannelDigis->end()) {
226  // NOTE: It will display this warning if channel hasn't been unpacked...
227  // Will comment out for now.
228  //edm::LogWarning("SiStripSpyDigiConverter") << "No data found for FED ID: " << iConn->fedId() << " channel: " << iConn->fedCh();
229  continue;
230  }
231 
232  // Start a new channel indexed by the detId in the filler
233  dsvFiller.newChannel(iConn->detId(), iConn->apvPairNumber() * STRIPS_PER_FEDCH);
234 
235  // Add the data
236  DetSetRawDigis::const_iterator iDigi = iDetSet->begin();
237  const DetSetRawDigis::const_iterator endDetSetDigis = iDetSet->end();
238  for (; iDigi != endDetSetDigis; ++iDigi) {
239  dsvFiller.addItem(*iDigi);
240  } // end of loop over the digis.
241  } // end of loop over channels.
242  } // end of loop over FEDs
243 
244  return dsvFiller.createDetSetVector();
245  } // end of SpyDigiConverter::mergeModuleChannels method.
246 
247 } // namespace sistrip
edm::DetSetVector< SiStripRawDigi >
SiStripSpyDigiConverter.h
MessageLogger.h
sistrip::SpyUtilities::isValid
static const bool isValid(const Frame &aFrame, const FrameQuality &aQuality, const uint16_t aExpectedPos)
Definition: SiStripSpyUtilities.cc:222
sistrip::SpyUtilities::Frame::firstTrailerBit
uint16_t firstTrailerBit
Definition: SiStripSpyUtilities.h:31
sistrip::invalid32_
static const uint32_t invalid32_
Definition: Constants.h:15
filterCSVwithJSON.copy
copy
Definition: filterCSVwithJSON.py:36
edm::DetSetVector::end
iterator end()
Return the off-the-end iterator.
Definition: DetSetVector.h:325
SiStripRawDigi.h
sistrip::SpyUtilities::FrameQuality
Definition: SiStripSpyUtilities.h:37
SiStripFedCabling.h
sistrip::SpyDigiConverter::DSVRawDigis
edm::DetSetVector< SiStripRawDigi > DSVRawDigis
Definition: SiStripSpyDigiConverter.h:27
sistrip::FEDStripOrdering::physicalOrderForStripInChannel
static uint8_t physicalOrderForStripInChannel(const uint8_t readoutOrderStripIndexInChannel)
Definition: SiStripFEDBufferComponents.h:812
sistrip::SpyUtilities::Frame::apvAddress
std::pair< uint8_t, uint8_t > apvAddress
Definition: SiStripSpyUtilities.h:34
edm::DetSetVector::begin
iterator begin()
Return an iterator to the first DetSet.
Definition: DetSetVector.h:314
sistrip::FED_ID_MIN
static const uint16_t FED_ID_MIN
Definition: ConstantsForHardwareSystems.h:21
ConstantsForHardwareSystems.h
Constants and enumerated types for FED/FEC systems.
SiStripFedCabling
Contains cabling info at the device level, including DetId, APV pair numbers, hardware addresses,...
Definition: SiStripFedCabling.h:25
sistrip::SpyDigiConverter::DetSetRawDigis
DSVRawDigis::detset DetSetRawDigis
Definition: SiStripSpyDigiConverter.h:52
sistrip::SpyDigiConverter::extractPayloadDigis
static std::unique_ptr< DSVRawDigis > extractPayloadDigis(const DSVRawDigis *inputScopeDigis, std::vector< uint32_t > *pAPVAddresses, const bool discardDigisWithAPVAddrErr, const sistrip::SpyUtilities::FrameQuality &aQuality, const uint16_t expectedPos)
Extract frames from the scope digis.
Definition: SiStripSpyDigiConverter.cc:23
sistrip::DetSetVectorFiller::newChannel
void newChannel(const uint32_t key, const uint16_t firstItem=0)
Definition: SiStripDetSetVectorFiller.h:59
sistrip::SPY_SAMPLES_PER_CHANNEL
static const uint16_t SPY_SAMPLES_PER_CHANNEL
Definition: SiStripFEDSpyBuffer.h:23
sistrip::SpyDigiConverter::reorderDigis
static std::unique_ptr< DSVRawDigis > reorderDigis(const DSVRawDigis *inputPayloadDigis)
Definition: SiStripSpyDigiConverter.cc:175
edm::DetSetVector::size
size_type size() const
Return the number of contained DetSets.
Definition: DetSetVector.h:259
sistrip::SpyUtilities::extractFrameInfo
static const Frame extractFrameInfo(const edm::DetSetVector< SiStripRawDigi >::detset &channelDigis, bool aPrintDebug=false)
Definition: SiStripSpyUtilities.cc:109
SiStripFedKey.h
sistrip::SpyUtilities::findMajorityValue
static std::pair< uint16_t, uint32_t > findMajorityValue(std::vector< uint16_t > &values, const uint16_t aFedId=0)
Definition: SiStripSpyUtilities.cc:409
edm::DetSetVector< SiStripRawDigi >::const_iterator
collection_type::const_iterator const_iterator
Definition: DetSetVector.h:102
SiStripDetSetVectorFiller.h
SiStripFEDBufferComponents.h
sistrip::STRIPS_PER_FEDCH
static const uint16_t STRIPS_PER_FEDCH
Definition: ConstantsForHardwareSystems.h:44
edm::DetSetVector::find
iterator find(det_id_type id)
Definition: DetSetVector.h:264
sistrip::SpyDigiConverter::mergeModuleChannels
static std::unique_ptr< DSVRawDigis > mergeModuleChannels(const DSVRawDigis *inputPhysicalOrderChannelDigis, const SiStripFedCabling &cabling)
Definition: SiStripSpyDigiConverter.cc:201
sistrip::DetSetVectorFiller::createDetSetVector
std::unique_ptr< edm::DetSetVector< T > > createDetSetVector()
Definition: SiStripDetSetVectorFiller.h:70
sistrip::DetSetVectorFiller::addItem
void addItem(const T &item)
Definition: SiStripDetSetVectorFiller.h:64
sistrip::invalid_
static const uint16_t invalid_
Definition: Constants.h:16
DetSetVector.h
l1tstage2_dqm_sourceclient-live_cfg.fedId
fedId
Definition: l1tstage2_dqm_sourceclient-live_cfg.py:88
sistrip::FED_ID_MAX
static const uint16_t FED_ID_MAX
Definition: ConstantsForHardwareSystems.h:22
sistrip::SpyUtilities::Frame::firstHeaderBit
uint16_t firstHeaderBit
Definition: SiStripSpyUtilities.h:30
sistrip::SpyDigiConverter::processFED
static void processFED(const uint16_t aPreviousFedId, const bool discardDigisWithAPVAddrErr, std::vector< uint32_t > *pAPVAddresses, std::vector< DetSetRawDigis > &outputData, std::vector< uint16_t > &aAddrVec, std::vector< uint16_t > &aHeaderBitVec, std::vector< uint16_t > &aTrailBitVec, std::vector< DSVRawDigis::const_iterator > &aFedScopeDigis)
Definition: SiStripSpyDigiConverter.cc:117
FedChannelConnection.h
sistrip::DetSetVectorFiller
Definition: SiStripDetSetVectorFiller.h:18
Exception.h
sistrip::FEDCH_PER_FED
static const uint16_t FEDCH_PER_FED
Definition: ConstantsForHardwareSystems.h:30
SiStripFedCabling::fedIds
FedsConstIterRange fedIds() const
Definition: SiStripFedCabling.h:154
DetSet.h
sistrip::SpyUtilities::Frame
Definition: SiStripSpyUtilities.h:26
sistrip
sistrip classes
Definition: SiStripQualityHelpers.h:14
SiStripFedCabling::fedConnections
ConnsConstIterRange fedConnections(uint16_t fed_id) const
Definition: SiStripFedCabling.cc:160
SiStripFEDSpyBuffer.h