CMS 3D CMS Logo

SiStripCMMonitor.cc
Go to the documentation of this file.
1 
2 // -*- C++ -*-
3 //
4 // Package: DQM/SiStripMonitorHardware
5 // Class: SiStripCMMonitorPlugin
6 //
11 //
12 // Created: 2009/07/22
13 //
14 
15 #include <sstream>
16 #include <memory>
17 #include <list>
18 #include <algorithm>
19 #include <cassert>
20 
31 
37 
40 
42 
45 
46 #include "DQM/SiStripMonitorHardware/interface/FEDHistograms.hh"
47 #include "DQM/SiStripMonitorHardware/interface/FEDErrors.hh"
48 
53 
54 #include "DQM/SiStripMonitorHardware/interface/CMHistograms.hh"
55 
57 
58 //
59 // Class declaration
60 //
61 
63 {
64  public:
65 
67  ~SiStripCMMonitorPlugin() override;
68  private:
69 
70  struct Statistics {
71  float Mean;
72  float Rms;
73  float Counter;
74  };
75 
76  void analyze(const edm::Event&, const edm::EventSetup&) override;
77  void bookHistograms(DQMStore::IBooker &, edm::Run const &, edm::EventSetup const &) override;
78  void dqmBeginRun(const edm::Run& , const edm::EventSetup& ) override;
79 
80  //update the cabling if necessary
81  void updateCabling(const edm::EventSetup& eventSetup);
82 
83 
84  void fillMaps(uint32_t aDetId, unsigned short aChInModule, std::pair<uint16_t,uint16_t> aMedians);
85 
86  //tag of FEDRawData collection
89  //folder name for histograms in DQMStore
91  //vector of fedIDs which will have detailed histograms made
92  std::vector<unsigned int> fedIdVec_;
93  //book detailed histograms even if they will be empty (for merging)
95  //do histos vs time with time=event number. Default time = orbit number (s)
98  //print debug messages when problems are found: 1=error debug, 2=light debug, 3=full debug
99  unsigned int printDebug_;
100  //FED cabling
101  uint32_t cablingCacheId_;
103 
104  //add parameter to save computing time if TkHistoMap are not filled
106 
107  CMHistograms cmHists_;
108 
109  std::map<unsigned int,Statistics> CommonModes_;
110  std::map<unsigned int,Statistics> CommonModesAPV0minusAPV1_;
111 
113 
115 
116 };
117 
118 
119 //
120 // Constructors and destructor
121 //
122 
124  : rawDataTag_(iConfig.getUntrackedParameter<edm::InputTag>("RawDataTag",edm::InputTag("source",""))),
125  folderName_(iConfig.getUntrackedParameter<std::string>("HistogramFolderName","SiStrip/ReadoutView/CMMonitoring")),
126  fedIdVec_(iConfig.getUntrackedParameter<std::vector<unsigned int> >("FedIdVec")),
127  fillAllDetailedHistograms_(iConfig.getUntrackedParameter<bool>("FillAllDetailedHistograms",false)),
128  fillWithEvtNum_(iConfig.getUntrackedParameter<bool>("FillWithEventNumber",false)),
129  fillWithLocalEvtNum_(iConfig.getUntrackedParameter<bool>("FillWithLocalEventNumber",false)),
130  printDebug_(iConfig.getUntrackedParameter<unsigned int>("PrintDebugMessages",1)),
131  cablingCacheId_(0)
132 
133 {
134  rawDataToken_ = consumes<FEDRawDataCollection>(rawDataTag_);
135  //print config to debug log
136  std::ostringstream debugStream;
137  if (printDebug_>1) {
138  debugStream << "[SiStripCMMonitorPlugin]Configuration for SiStripCMMonitorPlugin: " << std::endl
139  << "[SiStripCMMonitorPlugin]\tRawDataTag: " << rawDataTag_ << std::endl
140  << "[SiStripCMMonitorPlugin]\tHistogramFolderName: " << folderName_ << std::endl
141  << "[SiStripCMMonitorPlugin]\tFillAllDetailedHistograms? " << (fillAllDetailedHistograms_ ? "yes" : "no") << std::endl
142  << "[SiStripCMMonitorPlugin]\tFillWithEventNumber?" << (fillWithEvtNum_ ? "yes" : "no") << std::endl
143  << "[SiStripCMMonitorPlugin]\tPrintDebugMessages? " << (printDebug_ ? "yes" : "no") << std::endl;
144  }
145 
146  std::ostringstream* pDebugStream = (printDebug_>1 ? &debugStream : nullptr);
147 
148  cmHists_.initialise(iConfig,pDebugStream);
149 
150  doTkHistoMap_ = cmHists_.tkHistoMapEnabled();
151 
152  CommonModes_.clear();
154 
156  for (unsigned int iCh(0); iCh<sistrip::FEDCH_PER_FED; iCh++){
157  prevMedians_[fedId][iCh] = std::pair<uint16_t,uint16_t>(0,0);
158  }
159  }
160 
161 
162  if (printDebug_)
163  LogTrace("SiStripMonitorHardware") << debugStream.str();
164 
165  evt_ = 0;
166 
167 }
168 
170 {
171 }
172 
173 
174 //
175 // Member functions
176 //
177 
178 
180 {
181  ibooker.setCurrentFolder(folderName_);
182 
183  edm::ESHandle<TkDetMap> tkDetMapHandle;
184  eSetup.get<TrackerTopologyRcd>().get(tkDetMapHandle);
185  const TkDetMap* tkDetMap = tkDetMapHandle.product();
186 
187  cmHists_.bookTopLevelHistograms(ibooker, tkDetMap);
188 
189  if (fillAllDetailedHistograms_) cmHists_.bookAllFEDHistograms(ibooker);
190 }
191 
193 {
194 
195 }
196 
197 // ------------ method called to for each event ------------
198 void
200  const edm::EventSetup& iSetup)
201 {
202  //Retrieve tracker topology from geometry
203  edm::ESHandle<TrackerTopology> tTopoHandle;
204  iSetup.get<TrackerTopologyRcd>().get(tTopoHandle);
205  const TrackerTopology* const tTopo = tTopoHandle.product();
206 
207  //static bool firstEvent = true;
208  //static bool isBeingFilled = false;
209  //update cabling
210  updateCabling(iSetup);
211 
212  //get raw data
213  edm::Handle<FEDRawDataCollection> rawDataCollectionHandle;
214  iEvent.getByToken(rawDataToken_,rawDataCollectionHandle);
215  const FEDRawDataCollection& rawDataCollection = *rawDataCollectionHandle;
216 
217  //FED errors
218  FEDErrors lFedErrors;
219 
220  //loop over siStrip FED IDs
221  for (unsigned int fedId = FEDNumbering::MINSiStripFEDID;
223  fedId++) {//loop over FED IDs
224  const FEDRawData& fedData = rawDataCollection.FEDData(fedId);
225 
226  //create an object to fill all errors
227  lFedErrors.initialiseFED(fedId,cabling_,tTopo);
228 
229  //Do detailed check
230  //first check if data exists
231  bool lDataExist = lFedErrors.checkDataPresent(fedData);
232  if (!lDataExist) {
233  continue;
234  }
235 
236  std::auto_ptr<const sistrip::FEDBuffer> buffer;
237 
238  if (!lFedErrors.fillFatalFEDErrors(fedData,0)) {
239  continue;
240  }
241  else {
242  //need to construct full object to go any further
243  buffer.reset(new sistrip::FEDBuffer(fedData.data(),fedData.size(),true));
244  bool channelLengthsOK = buffer->checkChannelLengthsMatchBufferLength();
245  bool channelPacketCodesOK = buffer->checkChannelPacketCodes();
246  bool feLengthsOK = buffer->checkFEUnitLengths();
247  if ( !channelLengthsOK ||
248  !channelPacketCodesOK ||
249  !feLengthsOK ) {
250  continue;
251  }
252  }
253 
254  std::ostringstream infoStream;
255 
256 
257  if (printDebug_ > 1) {
258  infoStream << " --- Processing FED #" << fedId << std::endl;
259  }
260 
261 
262  std::vector<CMHistograms::CMvalues> values;
263 
264  for (unsigned int iCh = 0;
265  iCh < sistrip::FEDCH_PER_FED;
266  iCh++) {//loop on channels
267 
268  const FedChannelConnection & lConnection = cabling_->fedConnection(fedId,iCh);
269  bool connected = lConnection.isConnected();
270 
271  //std::cout << "FedID " << fedId << ", ch " << iCh << ", nAPVPairs " << lConnection.nApvPairs() << " apvPairNumber " << lConnection.apvPairNumber() << std::endl;
272 
273  if (!connected) {
274  continue;
275  }
276 
277  uint32_t lDetId = lConnection.detId();
278  unsigned short nChInModule = lConnection.nApvPairs();
279 
280  if (!lDetId || lDetId == sistrip::invalid32_) continue;
281 
282  bool lFailUnpackerChannelCheck = !buffer->channelGood(iCh, true) && connected;
283 
284  if (lFailUnpackerChannelCheck) {
285  continue;
286  }
287 
288 
289  //short lAPVPair = lConnection.apvPairNumber();
290  //short lSubDet = DetId(lDetId).subdetId();
291 
292 // if (firstEvent){
293 // infoStream << "Subdet " << lSubDet << ", " ;
294 // if (lSubDet == 3) {
295 //
296 // infoStream << "TIB layer " << tTopo->tibLayer(lDetId) << ", fedID " << fedId << ", channel " << iCh << std::endl;
297 // }
298 // else if (lSubDet == 4) {
299 //
300 // infoStream << "TID side " << tTopo->tibSide(lDetId) << " wheel " << tTopo->tibWheel(lDetId) << ", ring " << tTopo->tibRing(lDetId) << ", fedID " << fedId << ", channel " << iCh << std::endl;
301 // }
302 // else if (lSubDet == 5) {
303 //
304 // infoStream << "TOB side " << tTopo->tibRod(lDetId)[0] << " layer " << tTopo->tibLayer(lDetId) << ", rod " << tTopo->tibRodNumber(lDetId) << ", fedID " << fedId << ", channel " << iCh << std::endl;
305 // }
306 // else if (lSubDet == 6) {
307 //
308 // infoStream << "TEC side " << tTopo->tibSide(lDetId) << " wheel " << tTopo->tibWheel(lDetId) << ", petal " << tTopo->tibPetalNumber(lDetId) << ", ring " << tTopo->tibRing(lDetId) << ", fedID " << fedId << ", channel " << iCh << std::endl;
309 // }
310 // isBeingFilled=true;
311 // }
312 
313  std::ostringstream lMode;
314  lMode << buffer->readoutMode();
315  if (evt_ == 0 && printDebug_ > 1)
316  std::cout << "Readout mode: " << lMode.str() << std::endl;
317 
318 
319  const sistrip::FEDChannel & lChannel = buffer->channel(iCh);
320  std::pair<uint16_t,uint16_t> medians = std::pair<uint16_t,uint16_t>(0,0);
321 
322  if (lMode.str().find("Zero suppressed") != lMode.str().npos && lMode.str().find("lite") == lMode.str().npos) medians = std::pair<uint16_t,uint16_t>(lChannel.cmMedian(0),lChannel.cmMedian(1));
323 
324  CMHistograms::CMvalues lVal;
325  lVal.ChannelID = iCh;
326  lVal.Medians = std::pair<uint16_t,uint16_t>(medians.first,medians.second);
327  lVal.PreviousMedians = prevMedians_[fedId][iCh];
328 
329 // if (medians.second-medians.first > 26){
330 // std::ostringstream info;
331 // if (medians.second-medians.first > 44) info << " --- Second bump: event " << iEvent.id().event() << ", FED/Channel " << fedId << "/" << iCh << ", delta=" << medians.second-medians.first << std::endl;
332 // else info << " --- First bump: event " << iEvent.id().event() << ", FED/Channel " << fedId << "/" << iCh << ", delta=" << medians.second-medians.first << std::endl;
333 // edm::LogVerbatim("SiStripMonitorHardware") << info.str();
334 // }
335 
336  if (printDebug_ > 1) {
337  if (lChannel.length() > 7) {
338  infoStream << "Medians for channel #" << iCh << " (length " << lChannel.length() << "): " << medians.first << ", " << medians.second << std::endl;
339  }
340  }
341 
342  values.push_back(lVal);
343 
344  //if (iEvent.id().event() > 1000)
345  fillMaps(lDetId,nChInModule,medians);
346 
347  prevMedians_[fedId][iCh] = std::pair<uint16_t,uint16_t>(medians.first,medians.second);
348 
349  }//loop on channels
350 
351  float lTime = 0;
352  if (fillWithEvtNum_) {
353  // casting from unsigned long long to a float here
354  // doing it explicitely
355  lTime = static_cast<float>(iEvent.id().event());
356  } else {
357  if (fillWithLocalEvtNum_) {
358  // casting from unsigned long long to a float here
359  // doing it explicitely
360  lTime = static_cast<float>(evt_);
361  } else {
362  lTime = iEvent.orbitNumber()/11223.;
363  }
364  }
365 
366  cmHists_.fillHistograms(values,lTime,fedId);
367 
368  //if (printDebug_ > 0 && isBeingFilled && firstEvent) edm::LogVerbatim("SiStripMonitorHardware") << infoStream.str();
369 
370 
371 
372  }//loop on FEDs
373 
374 
375  //if (isBeingFilled)
376  //firstEvent = false;
377 
378  evt_++;
379 
380 }//analyze method
381 
382 // ------------ method called once each job just after ending the event loop ------------
383 /* //to be moved to harvesting step
384 void
385 SiStripCMMonitorPlugin::endJob()
386 {
387 
388  if (doTkHistoMap_) {//if TkHistoMap is enabled
389  std::map<unsigned int,Statistics>::iterator fracIter;
390 
391  //int ele = 0;
392  //int nBadChannels = 0;
393  for (fracIter = CommonModes_.begin(); fracIter!=CommonModes_.end(); fracIter++){
394  uint32_t detid = fracIter->first;
395  //if ((fracIter->second).second != 0) {
396  //std::cout << "------ ele #" << ele << ", Frac for detid #" << detid << " = " <<(fracIter->second).second << "/" << (fracIter->second).first << std::endl;
397  //nBadChannels++;
398  //}
399  float mean = 0;
400  float rms = 0;
401  Statistics lStat = fracIter->second;
402  if (lStat.Counter > 0) mean = lStat.Mean/lStat.Counter;
403  if (lStat.Counter > 1) rms = sqrt(lStat.Rms/(lStat.Counter-1)-(mean*mean));
404  cmHists_.fillTkHistoMap(cmHists_.tkHistoMapPointer(0),detid,mean);
405  cmHists_.fillTkHistoMap(cmHists_.tkHistoMapPointer(1),detid,rms);
406 
407  if (printDebug_ > 1) {
408  std::ostringstream message;
409  message << "TkHistoMap CM: Detid " << detid << ", mean = " << mean << ", rms = " << rms << ", counter = " << lStat.Counter << std::endl;
410  edm::LogVerbatim("SiStripMonitorHardware") << message.str();
411  }
412 
413  //ele++;
414  }
415 
416  for (fracIter = CommonModesAPV0minusAPV1_.begin(); fracIter!=CommonModesAPV0minusAPV1_.end(); fracIter++){
417  uint32_t detid = fracIter->first;
418  //if ((fracIter->second).second != 0) {
419  //std::cout << "------ ele #" << ele << ", Frac for detid #" << detid << " = " <<(fracIter->second).second << "/" << (fracIter->second).first << std::endl;
420  //nBadChannels++;
421  //}
422  float mean = 0;
423  float rms = 0;
424  Statistics lStat = fracIter->second;
425  if (lStat.Counter > 0) mean = lStat.Mean/lStat.Counter;
426  if (lStat.Counter > 1) rms = sqrt(lStat.Rms/(lStat.Counter-1)-(mean*mean));
427  cmHists_.fillTkHistoMap(cmHists_.tkHistoMapPointer(2),detid,mean);
428  cmHists_.fillTkHistoMap(cmHists_.tkHistoMapPointer(3),detid,rms);
429 
430  if (printDebug_ > 1) {
431  std::ostringstream message;
432  message << "TkHistoMap APV0minusAPV1: Detid " << detid << ", mean = " << mean << ", rms = " << rms << ", counter = " << lStat.Counter << std::endl;
433  edm::LogVerbatim("SiStripMonitorHardware") << message.str();
434  }
435 
436  //ele++;
437  }
438 
439  }//if TkHistoMap is enabled
440 
441 }
442 */
444 {
445  uint32_t currentCacheId = eventSetup.get<SiStripFedCablingRcd>().cacheIdentifier();
446  if (cablingCacheId_ != currentCacheId) {
447  edm::ESHandle<SiStripFedCabling> cablingHandle;
448  eventSetup.get<SiStripFedCablingRcd>().get(cablingHandle);
449  cabling_ = cablingHandle.product();
450  cablingCacheId_ = currentCacheId;
451  }
452 }
453 
454 
455 void SiStripCMMonitorPlugin::fillMaps(uint32_t aDetId, unsigned short aChInModule, std::pair<uint16_t,uint16_t> aMedians)
456 {
457 
458  if (doTkHistoMap_){//if TkHistMap is enabled
459  std::pair<std::map<unsigned int,Statistics>::iterator,bool> alreadyThere[2];
460 
461  Statistics lStat;
462  lStat.Mean = (aMedians.first+aMedians.second)*1./(2*aChInModule);
463  lStat.Rms = (aMedians.first+aMedians.second)*(aMedians.first+aMedians.second)*1./(4*aChInModule);
464  lStat.Counter = 1./aChInModule;
465 
466  alreadyThere[0] = CommonModes_.insert(std::pair<unsigned int,Statistics>(aDetId,lStat));
467  if (!alreadyThere[0].second) {
468  ((alreadyThere[0].first)->second).Mean += (aMedians.first+aMedians.second)*1./(2*aChInModule);
469  ((alreadyThere[0].first)->second).Rms += (aMedians.first+aMedians.second)*(aMedians.first+aMedians.second)*1./(4*aChInModule);
470  ((alreadyThere[0].first)->second).Counter += 1./aChInModule;
471  }
472 
473  lStat.Mean = (aMedians.first-aMedians.second)*1./aChInModule;
474  lStat.Rms = (aMedians.first-aMedians.second)*(aMedians.first-aMedians.second)*1./aChInModule;
475  lStat.Counter = 1./aChInModule;
476 
477  alreadyThere[1] = CommonModesAPV0minusAPV1_.insert(std::pair<unsigned int,Statistics>(aDetId,lStat));
478  if (!alreadyThere[1].second) {
479  ((alreadyThere[1].first)->second).Mean += (aMedians.first-aMedians.second)*1./aChInModule;
480  ((alreadyThere[1].first)->second).Rms += (aMedians.first-aMedians.second)*(aMedians.first-aMedians.second)*1./aChInModule;
481  ((alreadyThere[1].first)->second).Counter += 1./aChInModule;
482  }
483 
484  }
485 
486 }
487 
488 
489 //
490 // Define as a plug-in
491 //
492 
EventNumber_t event() const
Definition: EventID.h:41
std::pair< uint16_t, uint16_t > prevMedians_[FEDNumbering::MAXSiStripFEDID+1][sistrip::FEDCH_PER_FED]
void analyze(const edm::Event &, const edm::EventSetup &) override
FedChannelConnection fedConnection(uint16_t fed_id, uint16_t fed_ch) const
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:519
static const uint32_t invalid32_
Definition: Constants.h:15
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
void fillMaps(uint32_t aDetId, unsigned short aChInModule, std::pair< uint16_t, uint16_t > aMedians)
unsigned long long EventNumber_t
std::vector< unsigned int > fedIdVec_
std::map< unsigned int, Statistics > CommonModesAPV0minusAPV1_
size_t size() const
Lenght of the data buffer in bytes.
Definition: FEDRawData.h:47
U second(std::pair< T, U > const &p)
uint16_t cmMedian(const uint8_t apvIndex) const
int iEvent
Definition: GenABIO.cc:230
SiStripCMMonitorPlugin(const edm::ParameterSet &)
const FEDRawData & FEDData(int fedid) const
retrieve data for fed
void updateCabling(const edm::EventSetup &eventSetup)
void bookHistograms(DQMStore::IBooker &, edm::Run const &, edm::EventSetup const &) override
const uint32_t & detId() const
Class containning control, module, detector and connection information, at the level of a FED channel...
edm::EventNumber_t evt_
edm::EDGetTokenT< FEDRawDataCollection > rawDataToken_
int orbitNumber() const
Definition: EventBase.h:67
#define LogTrace(id)
void setCurrentFolder(const std::string &fullpath)
Definition: DQMStore.cc:279
const T & get() const
Definition: EventSetup.h:59
std::map< unsigned int, Statistics > CommonModes_
const uint16_t & nApvPairs() const
const SiStripFedCabling * cabling_
Contains cabling info at the device level, including DetId, APV pair numbers, hardware addresses...
edm::EventID id() const
Definition: EventBase.h:60
HLT enums.
static const uint16_t FEDCH_PER_FED
const unsigned char * data() const
Return a const pointer to the beginning of the data buffer.
Definition: FEDRawData.cc:28
T const * product() const
Definition: ESHandle.h:86
void dqmBeginRun(const edm::Run &, const edm::EventSetup &) override
Definition: Run.h:43