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 
32 
38 
41 
43 
46 
47 #include "DQM/SiStripMonitorHardware/interface/FEDHistograms.hh"
48 #include "DQM/SiStripMonitorHardware/interface/FEDErrors.hh"
49 
54 
55 #include "DQM/SiStripMonitorHardware/interface/CMHistograms.hh"
56 
57 //
58 // Class declaration
59 //
60 
62 {
63  public:
64 
67  private:
68 
69  struct Statistics {
70  double Mean;
71  double Rms;
72  double Counter;
73  };
74 
75  virtual void beginJob() override;
76  virtual void analyze(const edm::Event&, const edm::EventSetup&) override;
77  virtual void endJob() override;
78 
79  //update the cabling if necessary
80  void updateCabling(const edm::EventSetup& eventSetup);
81 
82 
83  void fillMaps(uint32_t aDetId, unsigned short aChInModule, std::pair<uint16_t,uint16_t> aMedians);
84 
85  //tag of FEDRawData collection
88  //folder name for histograms in DQMStore
90  //vector of fedIDs which will have detailed histograms made
91  std::vector<unsigned int> fedIdVec_;
92  //book detailed histograms even if they will be empty (for merging)
94  //do histos vs time with time=event number. Default time = orbit number (s)
97  //print debug messages when problems are found: 1=error debug, 2=light debug, 3=full debug
98  unsigned int printDebug_;
99  //write the DQMStore to a root file at the end of the job
102  //the DQMStore
104  //FED cabling
105  uint32_t cablingCacheId_;
107 
108  //add parameter to save computing time if TkHistoMap are not filled
110 
111  CMHistograms cmHists_;
112 
113  std::map<unsigned int,Statistics> CommonModes_;
114  std::map<unsigned int,Statistics> CommonModesAPV0minusAPV1_;
115 
117 
118  unsigned int evt_;
119 
120 };
121 
122 
123 //
124 // Constructors and destructor
125 //
126 
128  : rawDataTag_(iConfig.getUntrackedParameter<edm::InputTag>("RawDataTag",edm::InputTag("source",""))),
129  folderName_(iConfig.getUntrackedParameter<std::string>("HistogramFolderName","SiStrip/ReadoutView/CMMonitoring")),
130  fedIdVec_(iConfig.getUntrackedParameter<std::vector<unsigned int> >("FedIdVec")),
131  fillAllDetailedHistograms_(iConfig.getUntrackedParameter<bool>("FillAllDetailedHistograms",false)),
132  fillWithEvtNum_(iConfig.getUntrackedParameter<bool>("FillWithEventNumber",false)),
133  fillWithLocalEvtNum_(iConfig.getUntrackedParameter<bool>("FillWithLocalEventNumber",false)),
134  printDebug_(iConfig.getUntrackedParameter<unsigned int>("PrintDebugMessages",1)),
135  writeDQMStore_(iConfig.getUntrackedParameter<bool>("WriteDQMStore",false)),
136  dqmStoreFileName_(iConfig.getUntrackedParameter<std::string>("DQMStoreFileName","DQMStore.root")),
137  dqm_(0),
138  cablingCacheId_(0)
139 
140 {
141  rawDataToken_ = consumes<FEDRawDataCollection>(rawDataTag_);
142  //print config to debug log
143  std::ostringstream debugStream;
144  if (printDebug_>1) {
145  debugStream << "[SiStripCMMonitorPlugin]Configuration for SiStripCMMonitorPlugin: " << std::endl
146  << "[SiStripCMMonitorPlugin]\tRawDataTag: " << rawDataTag_ << std::endl
147  << "[SiStripCMMonitorPlugin]\tHistogramFolderName: " << folderName_ << std::endl
148  << "[SiStripCMMonitorPlugin]\tFillAllDetailedHistograms? " << (fillAllDetailedHistograms_ ? "yes" : "no") << std::endl
149  << "[SiStripCMMonitorPlugin]\tFillWithEventNumber?" << (fillWithEvtNum_ ? "yes" : "no") << std::endl
150  << "[SiStripCMMonitorPlugin]\tPrintDebugMessages? " << (printDebug_ ? "yes" : "no") << std::endl
151  << "[SiStripCMMonitorPlugin]\tWriteDQMStore? " << (writeDQMStore_ ? "yes" : "no") << std::endl;
152  if (writeDQMStore_) debugStream << "[SiStripCMMonitorPlugin]\tDQMStoreFileName: " << dqmStoreFileName_ << std::endl;
153  }
154 
155  std::ostringstream* pDebugStream = (printDebug_>1 ? &debugStream : NULL);
156 
157  cmHists_.initialise(iConfig,pDebugStream);
158 
159  doTkHistoMap_ = cmHists_.tkHistoMapEnabled();
160 
161  CommonModes_.clear();
163 
164  for (unsigned int fedId(FEDNumbering::MINSiStripFEDID); fedId <= FEDNumbering::MAXSiStripFEDID; fedId++){
165  for (unsigned int iCh(0); iCh<sistrip::FEDCH_PER_FED; iCh++){
166  prevMedians_[fedId][iCh] = std::pair<uint16_t,uint16_t>(0,0);
167  }
168  }
169 
170 
171  if (printDebug_) {
172  LogTrace("SiStripMonitorHardware") << debugStream.str();
173  }
174 
175  evt_ = 0;
176 
177 }
178 
180 {
181 }
182 
183 
184 //
185 // Member functions
186 //
187 
188 // ------------ method called to for each event ------------
189 void
191  const edm::EventSetup& iSetup)
192 {
193  //Retrieve tracker topology from geometry
194  edm::ESHandle<TrackerTopology> tTopoHandle;
195  iSetup.get<IdealGeometryRecord>().get(tTopoHandle);
196  const TrackerTopology* const tTopo = tTopoHandle.product();
197 
198  //static bool firstEvent = true;
199  //static bool isBeingFilled = false;
200  //update cabling
201  updateCabling(iSetup);
202 
203  //get raw data
204  edm::Handle<FEDRawDataCollection> rawDataCollectionHandle;
205  // iEvent.getByLabel(rawDataTag_,rawDataCollectionHandle);
206  iEvent.getByToken(rawDataToken_,rawDataCollectionHandle);
207  const FEDRawDataCollection& rawDataCollection = *rawDataCollectionHandle;
208 
209  //FED errors
210  FEDErrors lFedErrors;
211 
212  //loop over siStrip FED IDs
213  for (unsigned int fedId = FEDNumbering::MINSiStripFEDID;
215  fedId++) {//loop over FED IDs
216  const FEDRawData& fedData = rawDataCollection.FEDData(fedId);
217 
218  //create an object to fill all errors
219  lFedErrors.initialiseFED(fedId,cabling_,tTopo);
220 
221  //Do detailed check
222  //first check if data exists
223  bool lDataExist = lFedErrors.checkDataPresent(fedData);
224  if (!lDataExist) {
225  continue;
226  }
227 
228  std::auto_ptr<const sistrip::FEDBuffer> buffer;
229 
230  if (!lFedErrors.fillFatalFEDErrors(fedData,0)) {
231  continue;
232  }
233  else {
234  //need to construct full object to go any further
235  buffer.reset(new sistrip::FEDBuffer(fedData.data(),fedData.size(),true));
236  bool channelLengthsOK = buffer->checkChannelLengthsMatchBufferLength();
237  bool channelPacketCodesOK = buffer->checkChannelPacketCodes();
238  bool feLengthsOK = buffer->checkFEUnitLengths();
239  if ( !channelLengthsOK ||
240  !channelPacketCodesOK ||
241  !feLengthsOK ) {
242  continue;
243  }
244  }
245 
246  std::ostringstream infoStream;
247 
248 
249  if (printDebug_ > 1) {
250  infoStream << " --- Processing FED #" << fedId << std::endl;
251  }
252 
253 
254  std::vector<CMHistograms::CMvalues> values;
255 
256  for (unsigned int iCh = 0;
257  iCh < sistrip::FEDCH_PER_FED;
258  iCh++) {//loop on channels
259 
260  const FedChannelConnection & lConnection = cabling_->fedConnection(fedId,iCh);
261  bool connected = lConnection.isConnected();
262 
263  //std::cout << "FedID " << fedId << ", ch " << iCh << ", nAPVPairs " << lConnection.nApvPairs() << " apvPairNumber " << lConnection.apvPairNumber() << std::endl;
264 
265  if (!connected) {
266  continue;
267  }
268 
269  uint32_t lDetId = lConnection.detId();
270  unsigned short nChInModule = lConnection.nApvPairs();
271 
272  if (!lDetId || lDetId == sistrip::invalid32_) continue;
273 
274  bool lFailUnpackerChannelCheck = !buffer->channelGood(iCh) && connected;
275 
276  if (lFailUnpackerChannelCheck) {
277  continue;
278  }
279 
280 
281  //short lAPVPair = lConnection.apvPairNumber();
282  //short lSubDet = DetId(lDetId).subdetId();
283 
284 // if (firstEvent){
285 // infoStream << "Subdet " << lSubDet << ", " ;
286 // if (lSubDet == 3) {
287 //
288 // infoStream << "TIB layer " << tTopo->tibLayer(lDetId) << ", fedID " << fedId << ", channel " << iCh << std::endl;
289 // }
290 // else if (lSubDet == 4) {
291 //
292 // infoStream << "TID side " << tTopo->tibSide(lDetId) << " wheel " << tTopo->tibWheel(lDetId) << ", ring " << tTopo->tibRing(lDetId) << ", fedID " << fedId << ", channel " << iCh << std::endl;
293 // }
294 // else if (lSubDet == 5) {
295 //
296 // infoStream << "TOB side " << tTopo->tibRod(lDetId)[0] << " layer " << tTopo->tibLayer(lDetId) << ", rod " << tTopo->tibRodNumber(lDetId) << ", fedID " << fedId << ", channel " << iCh << std::endl;
297 // }
298 // else if (lSubDet == 6) {
299 //
300 // infoStream << "TEC side " << tTopo->tibSide(lDetId) << " wheel " << tTopo->tibWheel(lDetId) << ", petal " << tTopo->tibPetalNumber(lDetId) << ", ring " << tTopo->tibRing(lDetId) << ", fedID " << fedId << ", channel " << iCh << std::endl;
301 // }
302 // isBeingFilled=true;
303 // }
304 
305  std::ostringstream lMode;
306  lMode << buffer->readoutMode();
307  if (printDebug_ > 1) {
308  static bool lFirst = true;
309  if (lFirst) {
310  std::cout << "Readout mode: " << lMode.str() << std::endl;
311  lFirst = false;
312  }
313 
314  }
315 
316  const sistrip::FEDChannel & lChannel = buffer->channel(iCh);
317  std::pair<uint16_t,uint16_t> medians = std::pair<uint16_t,uint16_t>(0,0);
318 
319  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));
320 
321  CMHistograms::CMvalues lVal;
322  lVal.ChannelID = iCh;
323  lVal.Medians = std::pair<uint16_t,uint16_t>(medians.first,medians.second);
324  lVal.PreviousMedians = prevMedians_[fedId][iCh];
325 
326 // if (medians.second-medians.first > 26){
327 // std::ostringstream info;
328 // if (medians.second-medians.first > 44) info << " --- Second bump: event " << iEvent.id().event() << ", FED/Channel " << fedId << "/" << iCh << ", delta=" << medians.second-medians.first << std::endl;
329 // else info << " --- First bump: event " << iEvent.id().event() << ", FED/Channel " << fedId << "/" << iCh << ", delta=" << medians.second-medians.first << std::endl;
330 // edm::LogVerbatim("SiStripMonitorHardware") << info.str();
331 // }
332 
333  if (printDebug_ > 1) {
334  if (lChannel.length() > 7) {
335  infoStream << "Medians for channel #" << iCh << " (length " << lChannel.length() << "): " << medians.first << ", " << medians.second << std::endl;
336  }
337  }
338 
339  values.push_back(lVal);
340 
341  //if (iEvent.id().event() > 1000)
342  fillMaps(lDetId,nChInModule,medians);
343 
344  prevMedians_[fedId][iCh] = std::pair<uint16_t,uint16_t>(medians.first,medians.second);
345 
346  }//loop on channels
347 
348  float lTime = 0;
349  if (fillWithEvtNum_) lTime = iEvent.id().event();
350  else if (fillWithLocalEvtNum_) lTime = evt_;//iEvent.id().event();
351  else lTime = iEvent.orbitNumber()/11223.;
352 
353  cmHists_.fillHistograms(values,lTime,fedId);
354 
355  //if (printDebug_ > 0 && isBeingFilled && firstEvent) edm::LogVerbatim("SiStripMonitorHardware") << infoStream.str();
356 
357 
358 
359  }//loop on FEDs
360 
361 
362  //if (isBeingFilled)
363  //firstEvent = false;
364 
365  evt_++;
366 
367 }//analyze method
368 
369 // ------------ method called once each job just before starting event loop ------------
370 void
372 {
373  //get DQM store
376 
377  cmHists_.bookTopLevelHistograms(dqm_);
378 
379  if (fillAllDetailedHistograms_) cmHists_.bookAllFEDHistograms();
380 
381 }
382 
383 // ------------ method called once each job just after ending the event loop ------------
384 void
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 
442 }
443 
445 {
446  uint32_t currentCacheId = eventSetup.get<SiStripFedCablingRcd>().cacheIdentifier();
447  if (cablingCacheId_ != currentCacheId) {
448  edm::ESHandle<SiStripFedCabling> cablingHandle;
449  eventSetup.get<SiStripFedCablingRcd>().get(cablingHandle);
450  cabling_ = cablingHandle.product();
451  cablingCacheId_ = currentCacheId;
452  }
453 }
454 
455 
456 void SiStripCMMonitorPlugin::fillMaps(uint32_t aDetId, unsigned short aChInModule, std::pair<uint16_t,uint16_t> aMedians)
457 {
458 
459  if (doTkHistoMap_){//if TkHistMap is enabled
460  std::pair<std::map<unsigned int,Statistics>::iterator,bool> alreadyThere[2];
461 
462  Statistics lStat;
463  lStat.Mean = (aMedians.first+aMedians.second)*1./(2*aChInModule);
464  lStat.Rms = (aMedians.first+aMedians.second)*(aMedians.first+aMedians.second)*1./(4*aChInModule);
465  lStat.Counter = 1./aChInModule;
466 
467  alreadyThere[0] = CommonModes_.insert(std::pair<unsigned int,Statistics>(aDetId,lStat));
468  if (!alreadyThere[0].second) {
469  ((alreadyThere[0].first)->second).Mean += (aMedians.first+aMedians.second)*1./(2*aChInModule);
470  ((alreadyThere[0].first)->second).Rms += (aMedians.first+aMedians.second)*(aMedians.first+aMedians.second)*1./(4*aChInModule);
471  ((alreadyThere[0].first)->second).Counter += 1./aChInModule;
472  }
473 
474  lStat.Mean = (aMedians.first-aMedians.second)*1./aChInModule;
475  lStat.Rms = (aMedians.first-aMedians.second)*(aMedians.first-aMedians.second)*1./aChInModule;
476  lStat.Counter = 1./aChInModule;
477 
478  alreadyThere[1] = CommonModesAPV0minusAPV1_.insert(std::pair<unsigned int,Statistics>(aDetId,lStat));
479  if (!alreadyThere[1].second) {
480  ((alreadyThere[1].first)->second).Mean += (aMedians.first-aMedians.second)*1./aChInModule;
481  ((alreadyThere[1].first)->second).Rms += (aMedians.first-aMedians.second)*(aMedians.first-aMedians.second)*1./aChInModule;
482  ((alreadyThere[1].first)->second).Counter += 1./aChInModule;
483  }
484 
485  }
486 
487 }
488 
489 
490 //
491 // Define as a plug-in
492 //
493 
EventNumber_t event() const
Definition: EventID.h:44
std::pair< uint16_t, uint16_t > prevMedians_[FEDNumbering::MAXSiStripFEDID+1][sistrip::FEDCH_PER_FED]
virtual void endJob() override
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:434
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)
#define NULL
Definition: scimark2.h:8
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:243
SiStripCMMonitorPlugin(const edm::ParameterSet &)
const FEDRawData & FEDData(int fedid) const
retrieve data for fed
void updateCabling(const edm::EventSetup &eventSetup)
const uint32_t & detId() const
Class containning control, module, detector and connection information, at the level of a FED channel...
T sqrt(T t)
Definition: SSEVec.h:48
edm::EDGetTokenT< FEDRawDataCollection > rawDataToken_
void save(const std::string &filename, const std::string &path="", const std::string &pattern="", const std::string &rewrite="", const uint32_t run=0, SaveReferenceTag ref=SaveWithReference, int minStatus=dqm::qstatus::STATUS_OK, const std::string &fileupdate="RECREATE")
Definition: DQMStore.cc:2296
int orbitNumber() const
Definition: EventBase.h:63
#define LogTrace(id)
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:62
const SiStripFedCabling * cabling_
Contains cabling info at the device level, including DetId, APV pair numbers, hardware addresses...
edm::EventID id() const
Definition: EventBase.h:56
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
virtual void beginJob() override
void setCurrentFolder(const std::string &fullpath)
Definition: DQMStore.cc:584