CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros 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 
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 
68  private:
69 
70  struct Statistics {
71  float Mean;
72  float Rms;
73  float Counter;
74  };
75 
76  virtual 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& ) ;
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 : NULL);
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  cmHists_.bookTopLevelHistograms(ibooker);
184 
185  if (fillAllDetailedHistograms_) cmHists_.bookAllFEDHistograms(ibooker);
186 }
187 
189 {
190 
191 }
192 
193 // ------------ method called to for each event ------------
194 void
196  const edm::EventSetup& iSetup)
197 {
198  //Retrieve tracker topology from geometry
199  edm::ESHandle<TrackerTopology> tTopoHandle;
200  iSetup.get<IdealGeometryRecord>().get(tTopoHandle);
201  const TrackerTopology* const tTopo = tTopoHandle.product();
202 
203  //static bool firstEvent = true;
204  //static bool isBeingFilled = false;
205  //update cabling
206  updateCabling(iSetup);
207 
208  //get raw data
209  edm::Handle<FEDRawDataCollection> rawDataCollectionHandle;
210  iEvent.getByToken(rawDataToken_,rawDataCollectionHandle);
211  const FEDRawDataCollection& rawDataCollection = *rawDataCollectionHandle;
212 
213  //FED errors
214  FEDErrors lFedErrors;
215 
216  //loop over siStrip FED IDs
217  for (unsigned int fedId = FEDNumbering::MINSiStripFEDID;
219  fedId++) {//loop over FED IDs
220  const FEDRawData& fedData = rawDataCollection.FEDData(fedId);
221 
222  //create an object to fill all errors
223  lFedErrors.initialiseFED(fedId,cabling_,tTopo);
224 
225  //Do detailed check
226  //first check if data exists
227  bool lDataExist = lFedErrors.checkDataPresent(fedData);
228  if (!lDataExist) {
229  continue;
230  }
231 
232  std::auto_ptr<const sistrip::FEDBuffer> buffer;
233 
234  if (!lFedErrors.fillFatalFEDErrors(fedData,0)) {
235  continue;
236  }
237  else {
238  //need to construct full object to go any further
239  buffer.reset(new sistrip::FEDBuffer(fedData.data(),fedData.size(),true));
240  bool channelLengthsOK = buffer->checkChannelLengthsMatchBufferLength();
241  bool channelPacketCodesOK = buffer->checkChannelPacketCodes();
242  bool feLengthsOK = buffer->checkFEUnitLengths();
243  if ( !channelLengthsOK ||
244  !channelPacketCodesOK ||
245  !feLengthsOK ) {
246  continue;
247  }
248  }
249 
250  std::ostringstream infoStream;
251 
252 
253  if (printDebug_ > 1) {
254  infoStream << " --- Processing FED #" << fedId << std::endl;
255  }
256 
257 
258  std::vector<CMHistograms::CMvalues> values;
259 
260  for (unsigned int iCh = 0;
261  iCh < sistrip::FEDCH_PER_FED;
262  iCh++) {//loop on channels
263 
264  const FedChannelConnection & lConnection = cabling_->fedConnection(fedId,iCh);
265  bool connected = lConnection.isConnected();
266 
267  //std::cout << "FedID " << fedId << ", ch " << iCh << ", nAPVPairs " << lConnection.nApvPairs() << " apvPairNumber " << lConnection.apvPairNumber() << std::endl;
268 
269  if (!connected) {
270  continue;
271  }
272 
273  uint32_t lDetId = lConnection.detId();
274  unsigned short nChInModule = lConnection.nApvPairs();
275 
276  if (!lDetId || lDetId == sistrip::invalid32_) continue;
277 
278  bool lFailUnpackerChannelCheck = !buffer->channelGood(iCh) && connected;
279 
280  if (lFailUnpackerChannelCheck) {
281  continue;
282  }
283 
284 
285  //short lAPVPair = lConnection.apvPairNumber();
286  //short lSubDet = DetId(lDetId).subdetId();
287 
288 // if (firstEvent){
289 // infoStream << "Subdet " << lSubDet << ", " ;
290 // if (lSubDet == 3) {
291 //
292 // infoStream << "TIB layer " << tTopo->tibLayer(lDetId) << ", fedID " << fedId << ", channel " << iCh << std::endl;
293 // }
294 // else if (lSubDet == 4) {
295 //
296 // infoStream << "TID side " << tTopo->tibSide(lDetId) << " wheel " << tTopo->tibWheel(lDetId) << ", ring " << tTopo->tibRing(lDetId) << ", fedID " << fedId << ", channel " << iCh << std::endl;
297 // }
298 // else if (lSubDet == 5) {
299 //
300 // infoStream << "TOB side " << tTopo->tibRod(lDetId)[0] << " layer " << tTopo->tibLayer(lDetId) << ", rod " << tTopo->tibRodNumber(lDetId) << ", fedID " << fedId << ", channel " << iCh << std::endl;
301 // }
302 // else if (lSubDet == 6) {
303 //
304 // infoStream << "TEC side " << tTopo->tibSide(lDetId) << " wheel " << tTopo->tibWheel(lDetId) << ", petal " << tTopo->tibPetalNumber(lDetId) << ", ring " << tTopo->tibRing(lDetId) << ", fedID " << fedId << ", channel " << iCh << std::endl;
305 // }
306 // isBeingFilled=true;
307 // }
308 
309  std::ostringstream lMode;
310  lMode << buffer->readoutMode();
311  if (evt_ == 0 && printDebug_ > 1)
312  std::cout << "Readout mode: " << lMode.str() << std::endl;
313 
314 
315  const sistrip::FEDChannel & lChannel = buffer->channel(iCh);
316  std::pair<uint16_t,uint16_t> medians = std::pair<uint16_t,uint16_t>(0,0);
317 
318  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));
319 
320  CMHistograms::CMvalues lVal;
321  lVal.ChannelID = iCh;
322  lVal.Medians = std::pair<uint16_t,uint16_t>(medians.first,medians.second);
323  lVal.PreviousMedians = prevMedians_[fedId][iCh];
324 
325 // if (medians.second-medians.first > 26){
326 // std::ostringstream info;
327 // if (medians.second-medians.first > 44) info << " --- Second bump: event " << iEvent.id().event() << ", FED/Channel " << fedId << "/" << iCh << ", delta=" << medians.second-medians.first << std::endl;
328 // else info << " --- First bump: event " << iEvent.id().event() << ", FED/Channel " << fedId << "/" << iCh << ", delta=" << medians.second-medians.first << std::endl;
329 // edm::LogVerbatim("SiStripMonitorHardware") << info.str();
330 // }
331 
332  if (printDebug_ > 1) {
333  if (lChannel.length() > 7) {
334  infoStream << "Medians for channel #" << iCh << " (length " << lChannel.length() << "): " << medians.first << ", " << medians.second << std::endl;
335  }
336  }
337 
338  values.push_back(lVal);
339 
340  //if (iEvent.id().event() > 1000)
341  fillMaps(lDetId,nChInModule,medians);
342 
343  prevMedians_[fedId][iCh] = std::pair<uint16_t,uint16_t>(medians.first,medians.second);
344 
345  }//loop on channels
346 
347  float lTime = 0;
348  if (fillWithEvtNum_) {
349  // casting from unsigned long long to a float here
350  // doing it explicitely
351  lTime = static_cast<float>(iEvent.id().event());
352  } else {
353  if (fillWithLocalEvtNum_) {
354  // casting from unsigned long long to a float here
355  // doing it explicitely
356  lTime = static_cast<float>(evt_);
357  } else {
358  lTime = iEvent.orbitNumber()/11223.;
359  }
360  }
361 
362  cmHists_.fillHistograms(values,lTime,fedId);
363 
364  //if (printDebug_ > 0 && isBeingFilled && firstEvent) edm::LogVerbatim("SiStripMonitorHardware") << infoStream.str();
365 
366 
367 
368  }//loop on FEDs
369 
370 
371  //if (isBeingFilled)
372  //firstEvent = false;
373 
374  evt_++;
375 
376 }//analyze method
377 
378 // ------------ method called once each job just after ending the event loop ------------
379 /* //to be moved to harvesting step
380 void
381 SiStripCMMonitorPlugin::endJob()
382 {
383 
384  if (doTkHistoMap_) {//if TkHistoMap is enabled
385  std::map<unsigned int,Statistics>::iterator fracIter;
386 
387  //int ele = 0;
388  //int nBadChannels = 0;
389  for (fracIter = CommonModes_.begin(); fracIter!=CommonModes_.end(); fracIter++){
390  uint32_t detid = fracIter->first;
391  //if ((fracIter->second).second != 0) {
392  //std::cout << "------ ele #" << ele << ", Frac for detid #" << detid << " = " <<(fracIter->second).second << "/" << (fracIter->second).first << std::endl;
393  //nBadChannels++;
394  //}
395  float mean = 0;
396  float rms = 0;
397  Statistics lStat = fracIter->second;
398  if (lStat.Counter > 0) mean = lStat.Mean/lStat.Counter;
399  if (lStat.Counter > 1) rms = sqrt(lStat.Rms/(lStat.Counter-1)-(mean*mean));
400  cmHists_.fillTkHistoMap(cmHists_.tkHistoMapPointer(0),detid,mean);
401  cmHists_.fillTkHistoMap(cmHists_.tkHistoMapPointer(1),detid,rms);
402 
403  if (printDebug_ > 1) {
404  std::ostringstream message;
405  message << "TkHistoMap CM: Detid " << detid << ", mean = " << mean << ", rms = " << rms << ", counter = " << lStat.Counter << std::endl;
406  edm::LogVerbatim("SiStripMonitorHardware") << message.str();
407  }
408 
409  //ele++;
410  }
411 
412  for (fracIter = CommonModesAPV0minusAPV1_.begin(); fracIter!=CommonModesAPV0minusAPV1_.end(); fracIter++){
413  uint32_t detid = fracIter->first;
414  //if ((fracIter->second).second != 0) {
415  //std::cout << "------ ele #" << ele << ", Frac for detid #" << detid << " = " <<(fracIter->second).second << "/" << (fracIter->second).first << std::endl;
416  //nBadChannels++;
417  //}
418  float mean = 0;
419  float rms = 0;
420  Statistics lStat = fracIter->second;
421  if (lStat.Counter > 0) mean = lStat.Mean/lStat.Counter;
422  if (lStat.Counter > 1) rms = sqrt(lStat.Rms/(lStat.Counter-1)-(mean*mean));
423  cmHists_.fillTkHistoMap(cmHists_.tkHistoMapPointer(2),detid,mean);
424  cmHists_.fillTkHistoMap(cmHists_.tkHistoMapPointer(3),detid,rms);
425 
426  if (printDebug_ > 1) {
427  std::ostringstream message;
428  message << "TkHistoMap APV0minusAPV1: Detid " << detid << ", mean = " << mean << ", rms = " << rms << ", counter = " << lStat.Counter << std::endl;
429  edm::LogVerbatim("SiStripMonitorHardware") << message.str();
430  }
431 
432  //ele++;
433  }
434 
435  }//if TkHistoMap is enabled
436 
437 }
438 */
440 {
441  uint32_t currentCacheId = eventSetup.get<SiStripFedCablingRcd>().cacheIdentifier();
442  if (cablingCacheId_ != currentCacheId) {
443  edm::ESHandle<SiStripFedCabling> cablingHandle;
444  eventSetup.get<SiStripFedCablingRcd>().get(cablingHandle);
445  cabling_ = cablingHandle.product();
446  cablingCacheId_ = currentCacheId;
447  }
448 }
449 
450 
451 void SiStripCMMonitorPlugin::fillMaps(uint32_t aDetId, unsigned short aChInModule, std::pair<uint16_t,uint16_t> aMedians)
452 {
453 
454  if (doTkHistoMap_){//if TkHistMap is enabled
455  std::pair<std::map<unsigned int,Statistics>::iterator,bool> alreadyThere[2];
456 
457  Statistics lStat;
458  lStat.Mean = (aMedians.first+aMedians.second)*1./(2*aChInModule);
459  lStat.Rms = (aMedians.first+aMedians.second)*(aMedians.first+aMedians.second)*1./(4*aChInModule);
460  lStat.Counter = 1./aChInModule;
461 
462  alreadyThere[0] = CommonModes_.insert(std::pair<unsigned int,Statistics>(aDetId,lStat));
463  if (!alreadyThere[0].second) {
464  ((alreadyThere[0].first)->second).Mean += (aMedians.first+aMedians.second)*1./(2*aChInModule);
465  ((alreadyThere[0].first)->second).Rms += (aMedians.first+aMedians.second)*(aMedians.first+aMedians.second)*1./(4*aChInModule);
466  ((alreadyThere[0].first)->second).Counter += 1./aChInModule;
467  }
468 
469  lStat.Mean = (aMedians.first-aMedians.second)*1./aChInModule;
470  lStat.Rms = (aMedians.first-aMedians.second)*(aMedians.first-aMedians.second)*1./aChInModule;
471  lStat.Counter = 1./aChInModule;
472 
473  alreadyThere[1] = CommonModesAPV0minusAPV1_.insert(std::pair<unsigned int,Statistics>(aDetId,lStat));
474  if (!alreadyThere[1].second) {
475  ((alreadyThere[1].first)->second).Mean += (aMedians.first-aMedians.second)*1./aChInModule;
476  ((alreadyThere[1].first)->second).Rms += (aMedians.first-aMedians.second)*(aMedians.first-aMedians.second)*1./aChInModule;
477  ((alreadyThere[1].first)->second).Counter += 1./aChInModule;
478  }
479 
480  }
481 
482 }
483 
484 
485 //
486 // Define as a plug-in
487 //
488 
EventNumber_t event() const
Definition: EventID.h:41
std::pair< uint16_t, uint16_t > prevMedians_[FEDNumbering::MAXSiStripFEDID+1][sistrip::FEDCH_PER_FED]
virtual 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:449
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
#define NULL
Definition: scimark2.h:8
std::vector< unsigned int > fedIdVec_
void dqmBeginRun(const edm::Run &, const edm::EventSetup &)
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:274
const T & get() const
Definition: EventSetup.h:55
std::map< unsigned int, Statistics > CommonModes_
const uint16_t & nApvPairs() const
T const * product() const
Definition: ESHandle.h:86
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
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
tuple cout
Definition: gather_cfg.py:121
volatile std::atomic< bool > shutdown_flag false
Definition: Run.h:41