CMS 3D CMS Logo

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