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