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  }
359 
360  try {
361  // create unpacker
363 
364  // unpack
365  clusterizer.addFed(unpacker,ipair,record);
367  throw;
368  } catch (const cms::Exception& e) {
369  if (edm::isDebugEnabled()) {
370  std::ostringstream ss;
371  ss << "Unordered clusters for channel " << fedCh << " on FED " << fedId << ": " << e.what();
373  }
374  continue;
375  }
376 
377  } else {
378 
380  try {
381  // create unpacker
383 
384  // unpack
385  clusterizer.addFed(unpacker,ipair,record);
386  /*
387  while (unpacker.hasData()) {
388  clusterizer.stripByStripAdd(unpacker.sampleNumber()+ipair*256,unpacker.adc(),record);
389  unpacker++;
390  }
391  */
393  throw;
394  }catch (const cms::Exception& e) {
395  if (edm::isDebugEnabled()) {
396  std::ostringstream ss;
397  ss << "Unordered clusters for channel " << fedCh << " on FED " << fedId << ": " << e.what();
399  }
400  continue;
401  }
402  } else if (mode == sistrip::READOUT_MODE_VIRGIN_RAW ) {
403 
404  // create unpacker
406 
407  // unpack
408  std::vector<int16_t> digis;
409  while (unpacker.hasData()) {
410  digis.push_back(unpacker.adc());
411  unpacker++;
412  }
413 
414  //process raw
415  uint32_t id = conn->detId();
416  edm::DetSet<SiStripDigi> zsdigis(id);
417  //rawAlgos_->subtractorPed->subtract( id, ipair*256, digis);
418  //rawAlgos_->subtractorCMN->subtract( id, digis);
419  //rawAlgos_->suppressor->suppress( digis, zsdigis);
420  uint16_t firstAPV = ipair*2;
421  rawAlgos.SuppressVirginRawData(id, firstAPV,digis, zsdigis);
422  for( edm::DetSet<SiStripDigi>::const_iterator it = zsdigis.begin(); it!=zsdigis.end(); it++) {
423  clusterizer.stripByStripAdd( it->strip(), it->adc(), record);
424  }
425  }
426 
427  else if (mode == sistrip::READOUT_MODE_PROC_RAW ) {
428 
429  // create unpacker
431 
432  // unpack
433  std::vector<int16_t> digis;
434  while (unpacker.hasData()) {
435  digis.push_back(unpacker.adc());
436  unpacker++;
437  }
438 
439  //process raw
440  uint32_t id = conn->detId();
441  edm::DetSet<SiStripDigi> zsdigis(id);
442  //rawAlgos_->subtractorCMN->subtract( id, digis);
443  //rawAlgos_->suppressor->suppress( digis, zsdigis);
444  uint16_t firstAPV = ipair*2;
445  rawAlgos.SuppressProcessedRawData(id, firstAPV,digis, zsdigis);
446  for( edm::DetSet<SiStripDigi>::const_iterator it = zsdigis.begin(); it!=zsdigis.end(); it++) {
447  clusterizer.stripByStripAdd( it->strip(), it->adc(), record);
448  }
449  } else {
451  << "[ClustersFromRawProducer::"
452  << __func__ << "]"
453  << " FEDRawData readout mode "
454  << mode
455  << " from FED id "
456  << fedId
457  << " not supported.";
458  continue;
459  }
460  }
461 
462  } // end loop over conn
463 
464  clusterizer.stripByStripEnd(record);
465  incAct();
466  if(!record.empty()) incNoZ();
467 
468  // COUT << "filled " << record.size() << std::endl;
470  edm::LogError(sistrip::mlRawToCluster_) << "too many Sistrip Clusters to fit space allocated for OnDemand";
471  clusterizer.cleanState();
472 }
473 
474 }
475 
476 
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)
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e< void, edm::EventIDconst &, edm::Timestampconst & > We also list in braces which AR_WATCH_USING_METHOD_ is used for those or
Definition: Activities.doc:12
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