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  << "[sistrip::RawToClustersLazyGetter::"
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  << "[sistrip::RawToClustersLazyGetter::"
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 
131  StripClusterizerAlgorithm & clusterizer;
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  productLabel_(conf.getParameter<edm::InputTag>("ProductLabel")),
183  cabling_(nullptr),
184  clusterizer_(StripClusterizerAlgorithmFactory::create(conf.getParameter<edm::ParameterSet>("Clusterizer"))),
185  rawAlgos_(SiStripRawProcessingFactory::create(conf.getParameter<edm::ParameterSet>("Algorithms"))),
186  doAPVEmulatorCheck_(conf.existsAs<bool>("DoAPVEmulatorCheck") ? conf.getParameter<bool>("DoAPVEmulatorCheck") : true)
187  {
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.getByLabel( productLabel_, rawData);
206 
207 
208  std::auto_ptr< edmNew::DetSetVector<SiStripCluster> >
209  output( onDemand ?
210  new edmNew::DetSetVector<SiStripCluster>(boost::shared_ptr<edmNew::DetSetVector<SiStripCluster>::Getter>(new ClusterFiller(*rawData, *clusterizer_,
212  ),
213  clusterizer_->allDetIds())
215 
216  if(onDemand) assert(output->onDemand());
217 
218  output->reserve(15000,6*10000);
219 
220 
221 
222  if (!onDemand) {
223  run(*rawData, *output);
224 
225  COUT << output->dataSize() << " clusters from "
226  << output->size() << " modules"
227  << std::endl;
228  }
229 
230  ev.put(output);
231 
232  }
233 
234 private:
235 
236  void initialize(const edm::EventSetup& es);
237 
239 
240 
241  private:
242 
243  bool onDemand;
244 
246 
248 
249  std::auto_ptr<StripClusterizerAlgorithm> clusterizer_;
250  std::auto_ptr<SiStripRawProcessingAlgorithms> rawAlgos_;
251 
252 
253  // March 2012: add flag for disabling APVe check in configuration
255 
256 };
257 
260 
261 
262 
263 
265 
266  (*clusterizer_).initialize(es);
267  cabling_ = (*clusterizer_).cabling();
268  (*rawAlgos_).initialize(es);
269 
270 }
271 
274 
275  ClusterFiller filler(rawColl, *clusterizer_, *rawAlgos_, doAPVEmulatorCheck_);
276 
277  // loop over good det in cabling
278  for ( auto idet : clusterizer_->allDetIds()) {
279 
281 
282  filler.fill(record);
283 
284  if(record.empty()) record.abort();
285 
286  } // end loop over dets
287 }
288 
290 
291  incReady();
292 
293  auto idet= record.id();
294 
295  // COUT << "filling " << idet << std::endl;
296 
297  if (!clusterizer.stripByStripBegin(idet)) { return; }
298 
299  incSet();
300 
301  // Loop over apv-pairs of det
302  for (auto const conn : clusterizer.currentConnection()) {
303  if unlikely(!conn) continue;
304 
305  const uint16_t fedId = conn->fedId();
306 
307  // If fed id is null or connection is invalid continue
308  if unlikely( !fedId || !conn->isConnected() ) { continue; }
309 
310 
311  // If Fed hasnt already been initialised, extract data and initialise
312  if (!done[fedId]) { buffers[fedId].reset(fillBuffer(fedId, rawColl)); done[fedId]=true;}
313  auto buffer = buffers[fedId].get();
314  if unlikely(!buffer) continue;
315 
316  // check channel
317  const uint8_t fedCh = conn->fedCh();
318 
319  if unlikely(!buffer->channelGood(fedCh,doAPVEmulatorCheck)) {
320  if (edm::isDebugEnabled()) {
321  std::ostringstream ss;
322  ss << "Problem unpacking channel " << fedCh << " on FED " << fedId;
324  }
325  continue;
326  }
327 
328  // Determine APV std::pair number
329  uint16_t ipair = conn->apvPairNumber();
330 
331 
332  const sistrip::FEDReadoutMode mode = buffer->readoutMode();
333 
334 
336 
337  try {
338  // create unpacker
340 
341  // unpack
342  clusterizer.addFed(unpacker,ipair,record);
343  /*
344  while (unpacker.hasData()) {
345  clusterizer.stripByStripAdd(unpacker.sampleNumber()+ipair*256,unpacker.adc(),record);
346  unpacker++;
347  }
348  */
349  } catch (const cms::Exception& e) {
350  if (edm::isDebugEnabled()) {
351  std::ostringstream ss;
352  ss << "Unordered clusters for channel " << fedCh << " on FED " << fedId << ": " << e.what();
354  }
355  continue;
356  }
357  } else {
358 
360  try {
361  // create unpacker
363 
364  // unpack
365  clusterizer.addFed(unpacker,ipair,record);
366  /*
367  while (unpacker.hasData()) {
368  clusterizer.stripByStripAdd(unpacker.sampleNumber()+ipair*256,unpacker.adc(),record);
369  unpacker++;
370  }
371  */
372  } catch (const cms::Exception& e) {
373  if (edm::isDebugEnabled()) {
374  std::ostringstream ss;
375  ss << "Unordered clusters for channel " << fedCh << " on FED " << fedId << ": " << e.what();
377  }
378  continue;
379  }
380  } else if (mode == sistrip::READOUT_MODE_VIRGIN_RAW ) {
381 
382  // create unpacker
384 
385  // unpack
386  std::vector<int16_t> digis;
387  while (unpacker.hasData()) {
388  digis.push_back(unpacker.adc());
389  unpacker++;
390  }
391 
392  //process raw
393  uint32_t id = conn->detId();
394  edm::DetSet<SiStripDigi> zsdigis(id);
395  //rawAlgos_->subtractorPed->subtract( id, ipair*256, digis);
396  //rawAlgos_->subtractorCMN->subtract( id, digis);
397  //rawAlgos_->suppressor->suppress( digis, zsdigis);
398  uint16_t firstAPV = ipair*2;
399  rawAlgos.SuppressVirginRawData(id, firstAPV,digis, zsdigis);
400  for( edm::DetSet<SiStripDigi>::const_iterator it = zsdigis.begin(); it!=zsdigis.end(); it++) {
401  clusterizer.stripByStripAdd( it->strip(), it->adc(), record);
402  }
403  }
404 
405  else if (mode == sistrip::READOUT_MODE_PROC_RAW ) {
406 
407  // create unpacker
409 
410  // unpack
411  std::vector<int16_t> digis;
412  while (unpacker.hasData()) {
413  digis.push_back(unpacker.adc());
414  unpacker++;
415  }
416 
417  //process raw
418  uint32_t id = conn->detId();
419  edm::DetSet<SiStripDigi> zsdigis(id);
420  //rawAlgos_->subtractorCMN->subtract( id, digis);
421  //rawAlgos_->suppressor->suppress( digis, zsdigis);
422  uint16_t firstAPV = ipair*2;
423  rawAlgos.SuppressProcessedRawData(id, firstAPV,digis, zsdigis);
424  for( edm::DetSet<SiStripDigi>::const_iterator it = zsdigis.begin(); it!=zsdigis.end(); it++) {
425  clusterizer.stripByStripAdd( it->strip(), it->adc(), record);
426  }
427  } else {
429  << "[sistrip::RawToClustersLazyGetter::"
430  << __func__ << "]"
431  << " FEDRawData readout mode "
432  << mode
433  << " from FED id "
434  << fedId
435  << " not supported.";
436  continue;
437  }
438  }
439 
440  } // end loop over conn
441 
442  clusterizer.stripByStripEnd(record);
443  incAct();
444  if(!record.empty()) incNoZ();
445 
446  // COUT << "filled " << record.size() << std::endl;
447 
448 
449 }
450 
451 
static FEDRawChannelUnpacker procRawModeUnpacker(const FEDChannel &channel)
virtual char const * what() const
Definition: Exception.cc:141
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_[]
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
std::auto_ptr< SiStripRawProcessingAlgorithms > rawAlgos_
static FEDRawChannelUnpacker virginRawModeUnpacker(const FEDChannel &channel)
#define nullptr
std::auto_ptr< StripClusterizerAlgorithm > clusterizer_
void initialize(const edm::EventSetup &es)
size_t size() const
Lenght of the data buffer in bytes.
Definition: FEDRawData.h:47
#define unlikely(x)
Definition: Likely.h:21
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:116
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)
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:390
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)
#define likely(x)
Definition: Likely.h:20
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
SurfaceDeformation * create(int type, const std::vector< double > &params)
tuple conn
Definition: results_mgr.py:53
Definition: Run.h:41