test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ClustersFromRawProducer.cc
Go to the documentation of this file.
1 /*
2  */
5 
8 
9 
12 
16 
18 
19 
26 
27 
29 #include <sstream>
30 #include <memory>
31 #include <atomic>
32 #include <mutex>
33 
35 
36 
37 // #define VIDEBUG
38 #ifdef VIDEBUG
39 #include<iostream>
40 #define COUT std::cout << "VI "
41 #else
42 #define COUT LogDebug("")
43 #endif
44 
45 
46 namespace {
47  std::unique_ptr<sistrip::FEDBuffer> fillBuffer(int fedId, const FEDRawDataCollection& rawColl) {
48  std::unique_ptr<sistrip::FEDBuffer> buffer;
49 
50  // Retrieve FED raw data for given FED
51  const FEDRawData& rawData = rawColl.FEDData(fedId);
52 
53  // Check on FEDRawData pointer
54  if unlikely( !rawData.data() ) {
55  if (edm::isDebugEnabled()) {
57  << "[ClustersFromRawProducer::"
58  << __func__
59  << "]"
60  << " NULL pointer to FEDRawData for FED id "
61  << fedId;
62  }
63  return buffer;
64  }
65 
66  // Check on FEDRawData size
67  if unlikely( !rawData.size() ) {
68  if (edm::isDebugEnabled()) {
70  << "[ClustersFromRawProducer::"
71  << __func__ << "]"
72  << " FEDRawData has zero size for FED id "
73  << fedId;
74  }
75  return buffer;
76  }
77 
78  // construct FEDBuffer
79  try {
80  buffer.reset(new sistrip::FEDBuffer(rawData.data(),rawData.size()));
81  if unlikely(!buffer->doChecks(false)) throw cms::Exception("FEDBuffer") << "FED Buffer check fails for FED ID" << fedId << ".";
82  }
83  catch (const cms::Exception& e) {
84  if (edm::isDebugEnabled()) {
86  << "Exception caught when creating FEDBuffer object for FED " << fedId << ": " << e.what();
87  }
88  return std::unique_ptr<sistrip::FEDBuffer>();
89  }
90 
91  /*
92  // dump of FEDRawData to stdout
93  if ( dump_ ) {
94  std::stringstream ss;
95  RawToDigiUnpacker::dumpRawData( fedId, rawData, ss );
96  LogTrace(mlRawToDigi_)
97  << ss.str();
98  }
99  */
100 
101  return buffer;
102 
103  }
104 
105 
106  class ClusterFiller final : public StripClusterizerAlgorithm::output_t::Getter {
107  public:
108  ClusterFiller(const FEDRawDataCollection& irawColl,
109  StripClusterizerAlgorithm & iclusterizer,
110  SiStripRawProcessingAlgorithms & irawAlgos,
111  bool idoAPVEmulatorCheck):
112  rawColl(irawColl),
113  clusterizer(iclusterizer),
114  rawAlgos(irawAlgos),
115  doAPVEmulatorCheck(idoAPVEmulatorCheck){
116  incTot(clusterizer.allDetIds().size());
117  for (auto & d : done) d=nullptr;
118  }
119 
120 
121  ~ClusterFiller() { printStat();}
122 
124 
125  private:
126 
127 
128  std::unique_ptr<sistrip::FEDBuffer> buffers[1024];
129  std::atomic<sistrip::FEDBuffer*> done[1024];
130 
131 
132  const FEDRawDataCollection& rawColl;
133 
136 
137 
138  // March 2012: add flag for disabling APVe check in configuration
139  bool doAPVEmulatorCheck;
140 
141 
142 #ifdef VIDEBUG
143  struct Stat {
144  Stat() : totDet(0), detReady(0),detSet(0),detAct(0),detNoZ(0),totClus(0){}
145  std::atomic<int> totDet; // all dets
146  std::atomic<int> detReady; // dets "updated"
147  std::atomic<int> detSet; // det actually set not empty
148  std::atomic<int> detAct; // det actually set with content
149  std::atomic<int> detNoZ; // det actually set with content
150  std::atomic<int> totClus; // total number of clusters
151  };
152 
153  mutable Stat stat;
154  // void zeroStat() const { stat = std::move(Stat()); }
155  void incTot(int n) const { stat.totDet=n;}
156  void incReady() const { stat.detReady++;}
157  void incSet() const { stat.detSet++;}
158  void incAct() const { stat.detAct++;}
159  void incNoZ() const { stat.detNoZ++;}
160  void incClus(int n) const { stat.totClus+=n;}
161  void printStat() const {
162  COUT << "VI clusters " << stat.totDet <<','<< stat.detReady <<','<< stat.detSet <<','<< stat.detAct<<','<< stat.detNoZ <<','<< stat.totClus << std::endl;
163  }
164 
165 #else
166  static void zeroStat(){}
167  static void incTot(int){}
168  static void incReady() {}
169  static void incSet() {}
170  static void incAct() {}
171  static void incNoZ() {}
172  static void incClus(int){}
173  static void printStat(){}
174 #endif
175 
176  };
177 
178 
179 } // namespace
180 
181 
182 
184 
185  public:
186 
188  onDemand(conf.getParameter<bool>("onDemand")),
189  cabling_(nullptr),
190  clusterizer_(StripClusterizerAlgorithmFactory::create(conf.getParameter<edm::ParameterSet>("Clusterizer"))),
191  rawAlgos_(SiStripRawProcessingFactory::create(conf.getParameter<edm::ParameterSet>("Algorithms"))),
192  doAPVEmulatorCheck_(conf.existsAs<bool>("DoAPVEmulatorCheck") ? conf.getParameter<bool>("DoAPVEmulatorCheck") : true)
193  {
194  productToken_ = consumes<FEDRawDataCollection>(conf.getParameter<edm::InputTag>("ProductLabel"));
195  produces< edmNew::DetSetVector<SiStripCluster> > ();
196  assert(clusterizer_.get());
197  assert(rawAlgos_.get());
198  }
199 
200 
201  void beginRun( const edm::Run&, const edm::EventSetup& es) {
202  initialize(es);
203  }
204 
205 
206  void produce(edm::Event& ev, const edm::EventSetup& es) {
207 
208  initialize(es);
209 
210  // get raw data
212  ev.getByToken( productToken_, rawData);
213 
214 
215  std::auto_ptr< edmNew::DetSetVector<SiStripCluster> >
216  output( onDemand ?
217  new edmNew::DetSetVector<SiStripCluster>(std::shared_ptr<edmNew::DetSetVector<SiStripCluster>::Getter>(std::make_shared<ClusterFiller>(*rawData, *clusterizer_,
219  ),
220  clusterizer_->allDetIds())
222 
223  if(onDemand) assert(output->onDemand());
224 
225  output->reserve(15000,24*10000);
226 
227 
228  if (!onDemand) {
229  run(*rawData, *output);
230  output->shrink_to_fit();
231  COUT << output->dataSize() << " clusters from "
232  << output->size() << " modules"
233  << std::endl;
234  }
235 
236  ev.put(output);
237 
238  }
239 
240 private:
241 
242  void initialize(const edm::EventSetup& es);
243 
245 
246 
247  private:
248 
249  bool onDemand;
250 
252 
254 
255  std::auto_ptr<StripClusterizerAlgorithm> clusterizer_;
256  std::auto_ptr<SiStripRawProcessingAlgorithms> rawAlgos_;
257 
258 
259  // March 2012: add flag for disabling APVe check in configuration
261 
262 };
263 
266 
267 
268 
269 
271 
272  (*clusterizer_).initialize(es);
273  cabling_ = (*clusterizer_).cabling();
274  (*rawAlgos_).initialize(es);
275 
276 }
277 
280 
281  ClusterFiller filler(rawColl, *clusterizer_, *rawAlgos_, doAPVEmulatorCheck_);
282 
283  // loop over good det in cabling
284  for ( auto idet : clusterizer_->allDetIds()) {
285 
287 
288  filler.fill(record);
289 
290  if(record.empty()) record.abort();
291 
292  } // end loop over dets
293 }
294 
296 try { // edmNew::CapacityExaustedException
297  incReady();
298 
299  auto idet= record.id();
300 
301  COUT << "filling " << idet << std::endl;
302 
303  auto const & det = clusterizer.stripByStripBegin(idet);
304  if (!det.valid()) return;
306 
307  incSet();
308 
309  // Loop over apv-pairs of det
310  for (auto const conn : clusterizer.currentConnection(det)) {
311  if unlikely(!conn) continue;
312 
313  const uint16_t fedId = conn->fedId();
314 
315  // If fed id is null or connection is invalid continue
316  if unlikely( !fedId || !conn->isConnected() ) { continue; }
317 
318 
319  // If Fed hasnt already been initialised, extract data and initialise
320  sistrip::FEDBuffer * buffer = done[fedId];
321  if (!buffer) {
322  buffer = fillBuffer(fedId, rawColl).release();
323  if (!buffer) { continue;}
324  sistrip::FEDBuffer * exp = nullptr;
325  if (done[fedId].compare_exchange_strong(exp, buffer)) buffers[fedId].reset(buffer);
326  else { delete buffer; buffer = done[fedId]; }
327  }
328  assert(buffer);
329 
330  // check channel
331  const uint8_t fedCh = conn->fedCh();
332 
333  if unlikely(!buffer->channelGood(fedCh,doAPVEmulatorCheck)) {
334  if (edm::isDebugEnabled()) {
335  std::ostringstream ss;
336  ss << "Problem unpacking channel " << fedCh << " on FED " << fedId;
338  }
339  continue;
340  }
341 
342  // Determine APV std::pair number
343  uint16_t ipair = conn->apvPairNumber();
344 
345 
346  const sistrip::FEDReadoutMode mode = buffer->readoutMode();
347 
348 
350 
351  try {
352  // create unpacker
354 
355  // unpack
356  clusterizer.addFed(state,unpacker,ipair,record);
357  /*
358  while (unpacker.hasData()) {
359  clusterizer.stripByStripAdd(unpacker.sampleNumber()+ipair*256,unpacker.adc(),record);
360  unpacker++;
361  }
362  */
364  throw;
365  } catch (const cms::Exception& e) {
366  if (edm::isDebugEnabled()) {
367  std::ostringstream ss;
368  ss << "Unordered clusters for channel " << fedCh << " on FED " << fedId << ": " << e.what();
370  }
371  continue;
372  }
373  } else {
374 
376  try {
377  // create unpacker
379 
380  // unpack
381  clusterizer.addFed(state,unpacker,ipair,record);
382  /*
383  while (unpacker.hasData()) {
384  clusterizer.stripByStripAdd(unpacker.sampleNumber()+ipair*256,unpacker.adc(),record);
385  unpacker++;
386  }
387  */
389  throw;
390  }catch (const cms::Exception& e) {
391  if (edm::isDebugEnabled()) {
392  std::ostringstream ss;
393  ss << "Unordered clusters for channel " << fedCh << " on FED " << fedId << ": " << e.what();
395  }
396  continue;
397  }
398  } else if (mode == sistrip::READOUT_MODE_VIRGIN_RAW ) {
399 
400  // create unpacker
402 
403  // unpack
404  std::vector<int16_t> digis;
405  while (unpacker.hasData()) {
406  digis.push_back(unpacker.adc());
407  unpacker++;
408  }
409 
410  //process raw
411  uint32_t id = conn->detId();
412  edm::DetSet<SiStripDigi> zsdigis(id);
413  //rawAlgos_->subtractorPed->subtract( id, ipair*256, digis);
414  //rawAlgos_->subtractorCMN->subtract( id, digis);
415  //rawAlgos_->suppressor->suppress( digis, zsdigis);
416  uint16_t firstAPV = ipair*2;
417  rawAlgos.SuppressVirginRawData(id, firstAPV,digis, zsdigis);
418  for( edm::DetSet<SiStripDigi>::const_iterator it = zsdigis.begin(); it!=zsdigis.end(); it++) {
419  clusterizer.stripByStripAdd(state, it->strip(), it->adc(), record);
420  }
421  }
422 
423  else if (mode == sistrip::READOUT_MODE_PROC_RAW ) {
424 
425  // create unpacker
427 
428  // unpack
429  std::vector<int16_t> digis;
430  while (unpacker.hasData()) {
431  digis.push_back(unpacker.adc());
432  unpacker++;
433  }
434 
435  //process raw
436  uint32_t id = conn->detId();
437  edm::DetSet<SiStripDigi> zsdigis(id);
438  //rawAlgos_->subtractorCMN->subtract( id, digis);
439  //rawAlgos_->suppressor->suppress( digis, zsdigis);
440  uint16_t firstAPV = ipair*2;
441  rawAlgos.SuppressProcessedRawData(id, firstAPV,digis, zsdigis);
442  for( edm::DetSet<SiStripDigi>::const_iterator it = zsdigis.begin(); it!=zsdigis.end(); it++) {
443  clusterizer.stripByStripAdd(state, it->strip(), it->adc(), record);
444  }
445  } else {
447  << "[ClustersFromRawProducer::"
448  << __func__ << "]"
449  << " FEDRawData readout mode "
450  << mode
451  << " from FED id "
452  << fedId
453  << " not supported.";
454  continue;
455  }
456  }
457 
458  } // end loop over conn
459  clusterizer.stripByStripEnd(state,record);
460 
461  incAct();
462  if(!record.empty()) incNoZ();
463 
464  COUT << "filled " << record.size() << std::endl;
465  for ( auto const & cl : record ) COUT << cl.firstStrip() << ','<< cl.amplitudes().size() << std::endl;
466  incClus(record.size());
468  edm::LogError(sistrip::mlRawToCluster_) << "too many Sistrip Clusters to fit space allocated for OnDemand";
469 }
470 
471 }
472 
473 
static FEDRawChannelUnpacker procRawModeUnpacker(const FEDChannel &channel)
virtual char const * what() const
Definition: Exception.cc:141
T getParameter(std::string const &) const
bool isDebugEnabled()
static FEDZSChannelUnpacker zeroSuppressedModeUnpacker(const FEDChannel &channel)
string fill
Definition: lumiContext.py:319
void run(const FEDRawDataCollection &rawColl, edmNew::DetSetVector< SiStripCluster > &output)
static const char mlRawToCluster_[]
JetCorrectorParameters::Record record
Definition: classes.h:7
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:462
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
std::auto_ptr< SiStripRawProcessingAlgorithms > rawAlgos_
static FEDRawChannelUnpacker virginRawModeUnpacker(const FEDChannel &channel)
FEDReadoutMode readoutMode() const
assert(m_qm.get())
bool ev
std::auto_ptr< StripClusterizerAlgorithm > clusterizer_
void initialize(const edm::EventSetup &es)
#define nullptr
virtual bool channelGood(const uint8_t internalFEDannelNum, const bool doAPVeCheck=true) const
size_t size() const
Lenght of the data buffer in bytes.
Definition: FEDRawData.h:47
#define unlikely(x)
tuple d
Definition: ztail.py:151
#define likely(x)
const FEDRawData & FEDData(int fedid) const
retrieve data for fed
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:121
void beginRun(const edm::Run &, const edm::EventSetup &es)
void produce(edm::Event &ev, const edm::EventSetup &es)
const FEDChannel & channel(const uint8_t internalFEDChannelNum) const
static FEDZSChannelUnpacker zeroSuppressedLiteModeUnpacker(const FEDChannel &channel)
SiStripDetCabling const * cabling_
string const
Definition: compareJSON.py:14
SiStripClusterizerFromRaw(const edm::ParameterSet &conf)
if(dp >Float(M_PI)) dp-
const unsigned char * data() const
Return a const pointer to the beginning of the data buffer.
Definition: FEDRawData.cc:28
#define COUT
collection_type::const_iterator const_iterator
Definition: DetSet.h:33
edm::EDGetTokenT< FEDRawDataCollection > productToken_
Definition: Run.h:43