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