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
static uint8_t physicalOrderForStripInChannel(const uint8_t readoutOrderStripIndexInChannel)
std::pair< uint8_t, uint8_t > apvAddress
static const uint16_t FED_ID_MIN
iterator find(det_id_type id)
Definition: DetSetVector.h:264
static std::unique_ptr< DSVRawDigis > reorderDigis(const DSVRawDigis *inputPayloadDigis)
static const bool isValid(const Frame &aFrame, const FrameQuality &aQuality, const uint16_t aExpectedPos)
static const uint32_t invalid32_
Definition: Constants.h:15
void newChannel(const uint32_t key, const uint16_t firstItem=0)
DSVRawDigis::detset DetSetRawDigis
static const uint16_t SPY_SAMPLES_PER_CHANNEL
sistrip classes
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.
static const Frame extractFrameInfo(const edm::DetSetVector< SiStripRawDigi >::detset &channelDigis, bool aPrintDebug=false)
std::unique_ptr< edm::DetSetVector< T > > createDetSetVector()
FedsConstIterRange fedIds() const
static std::unique_ptr< DSVRawDigis > mergeModuleChannels(const DSVRawDigis *inputPhysicalOrderChannelDigis, const SiStripFedCabling &cabling)
iterator end()
Return the off-the-end iterator.
Definition: DetSetVector.h:325
size_type size() const
Return the number of contained DetSets.
Definition: DetSetVector.h:259
static std::pair< uint16_t, uint32_t > findMajorityValue(std::vector< uint16_t > &values, const uint16_t aFedId=0)
static const uint16_t STRIPS_PER_FEDCH
edm::DetSetVector< SiStripRawDigi > DSVRawDigis
Constants and enumerated types for FED/FEC systems.
static const uint16_t invalid_
Definition: Constants.h:16
ConnsConstIterRange fedConnections(uint16_t fed_id) const
Contains cabling info at the device level, including DetId, APV pair numbers, hardware addresses...
static const uint16_t FEDCH_PER_FED
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)
iterator begin()
Return an iterator to the first DetSet.
Definition: DetSetVector.h:314
static const uint16_t FED_ID_MAX
collection_type::const_iterator const_iterator
Definition: DetSet.h:32
collection_type::const_iterator const_iterator
Definition: DetSetVector.h:102