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.getByToken(rawDataToken_,rawDataCollectionHandle);
206  const FEDRawDataCollection& rawDataCollection = *rawDataCollectionHandle;
207 
208  //FED errors
209  FEDErrors lFedErrors;
210 
211  //loop over siStrip FED IDs
212  for (unsigned int fedId = FEDNumbering::MINSiStripFEDID;
214  fedId++) {//loop over FED IDs
215  const FEDRawData& fedData = rawDataCollection.FEDData(fedId);
216 
217  //create an object to fill all errors
218  lFedErrors.initialiseFED(fedId,cabling_,tTopo);
219 
220  //Do detailed check
221  //first check if data exists
222  bool lDataExist = lFedErrors.checkDataPresent(fedData);
223  if (!lDataExist) {
224  continue;
225  }
226 
227  std::auto_ptr<const sistrip::FEDBuffer> buffer;
228 
229  if (!lFedErrors.fillFatalFEDErrors(fedData,0)) {
230  continue;
231  }
232  else {
233  //need to construct full object to go any further
234  buffer.reset(new sistrip::FEDBuffer(fedData.data(),fedData.size(),true));
235  bool channelLengthsOK = buffer->checkChannelLengthsMatchBufferLength();
236  bool channelPacketCodesOK = buffer->checkChannelPacketCodes();
237  bool feLengthsOK = buffer->checkFEUnitLengths();
238  if ( !channelLengthsOK ||
239  !channelPacketCodesOK ||
240  !feLengthsOK ) {
241  continue;
242  }
243  }
244 
245  std::ostringstream infoStream;
246 
247 
248  if (printDebug_ > 1) {
249  infoStream << " --- Processing FED #" << fedId << std::endl;
250  }
251 
252 
253  std::vector<CMHistograms::CMvalues> values;
254 
255  for (unsigned int iCh = 0;
256  iCh < sistrip::FEDCH_PER_FED;
257  iCh++) {//loop on channels
258 
259  const FedChannelConnection & lConnection = cabling_->fedConnection(fedId,iCh);
260  bool connected = lConnection.isConnected();
261 
262  //std::cout << "FedID " << fedId << ", ch " << iCh << ", nAPVPairs " << lConnection.nApvPairs() << " apvPairNumber " << lConnection.apvPairNumber() << std::endl;
263 
264  if (!connected) {
265  continue;
266  }
267 
268  uint32_t lDetId = lConnection.detId();
269  unsigned short nChInModule = lConnection.nApvPairs();
270 
271  if (!lDetId || lDetId == sistrip::invalid32_) continue;
272 
273  bool lFailUnpackerChannelCheck = !buffer->channelGood(iCh) && connected;
274 
275  if (lFailUnpackerChannelCheck) {
276  continue;
277  }
278 
279 
280  //short lAPVPair = lConnection.apvPairNumber();
281  //short lSubDet = DetId(lDetId).subdetId();
282 
283 // if (firstEvent){
284 // infoStream << "Subdet " << lSubDet << ", " ;
285 // if (lSubDet == 3) {
286 //
287 // infoStream << "TIB layer " << tTopo->tibLayer(lDetId) << ", fedID " << fedId << ", channel " << iCh << std::endl;
288 // }
289 // else if (lSubDet == 4) {
290 //
291 // infoStream << "TID side " << tTopo->tibSide(lDetId) << " wheel " << tTopo->tibWheel(lDetId) << ", ring " << tTopo->tibRing(lDetId) << ", fedID " << fedId << ", channel " << iCh << std::endl;
292 // }
293 // else if (lSubDet == 5) {
294 //
295 // infoStream << "TOB side " << tTopo->tibRod(lDetId)[0] << " layer " << tTopo->tibLayer(lDetId) << ", rod " << tTopo->tibRodNumber(lDetId) << ", fedID " << fedId << ", channel " << iCh << std::endl;
296 // }
297 // else if (lSubDet == 6) {
298 //
299 // infoStream << "TEC side " << tTopo->tibSide(lDetId) << " wheel " << tTopo->tibWheel(lDetId) << ", petal " << tTopo->tibPetalNumber(lDetId) << ", ring " << tTopo->tibRing(lDetId) << ", fedID " << fedId << ", channel " << iCh << std::endl;
300 // }
301 // isBeingFilled=true;
302 // }
303 
304  std::ostringstream lMode;
305  lMode << buffer->readoutMode();
306  if (printDebug_ > 1) {
307  static bool lFirst = true;
308  if (lFirst) {
309  std::cout << "Readout mode: " << lMode.str() << std::endl;
310  lFirst = false;
311  }
312 
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_) lTime = iEvent.id().event();
349  else if (fillWithLocalEvtNum_) lTime = evt_;//iEvent.id().event();
350  else lTime = iEvent.orbitNumber()/11223.;
351 
352  cmHists_.fillHistograms(values,lTime,fedId);
353 
354  //if (printDebug_ > 0 && isBeingFilled && firstEvent) edm::LogVerbatim("SiStripMonitorHardware") << infoStream.str();
355 
356 
357 
358  }//loop on FEDs
359 
360 
361  //if (isBeingFilled)
362  //firstEvent = false;
363 
364  evt_++;
365 
366 }//analyze method
367 
368 // ------------ method called once each job just before starting event loop ------------
369 void
371 {
372  //get DQM store
375 
376  cmHists_.bookTopLevelHistograms(dqm_);
377 
378  if (fillAllDetailedHistograms_) cmHists_.bookAllFEDHistograms();
379 
380 }
381 
382 // ------------ method called once each job just after ending the event loop ------------
383 void
385 {
386 
387  if (doTkHistoMap_) {//if TkHistoMap is enabled
388  std::map<unsigned int,Statistics>::iterator fracIter;
389 
390  //int ele = 0;
391  //int nBadChannels = 0;
392  for (fracIter = CommonModes_.begin(); fracIter!=CommonModes_.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(0),detid,mean);
404  cmHists_.fillTkHistoMap(cmHists_.tkHistoMapPointer(1),detid,rms);
405 
406  if (printDebug_ > 1) {
407  std::ostringstream message;
408  message << "TkHistoMap CM: Detid " << detid << ", mean = " << mean << ", rms = " << rms << ", counter = " << lStat.Counter << std::endl;
409  edm::LogVerbatim("SiStripMonitorHardware") << message.str();
410  }
411 
412  //ele++;
413  }
414 
415  for (fracIter = CommonModesAPV0minusAPV1_.begin(); fracIter!=CommonModesAPV0minusAPV1_.end(); fracIter++){
416  uint32_t detid = fracIter->first;
417  //if ((fracIter->second).second != 0) {
418  //std::cout << "------ ele #" << ele << ", Frac for detid #" << detid << " = " <<(fracIter->second).second << "/" << (fracIter->second).first << std::endl;
419  //nBadChannels++;
420  //}
421  float mean = 0;
422  float rms = 0;
423  Statistics lStat = fracIter->second;
424  if (lStat.Counter > 0) mean = lStat.Mean/lStat.Counter;
425  if (lStat.Counter > 1) rms = sqrt(lStat.Rms/(lStat.Counter-1)-(mean*mean));
426  cmHists_.fillTkHistoMap(cmHists_.tkHistoMapPointer(2),detid,mean);
427  cmHists_.fillTkHistoMap(cmHists_.tkHistoMapPointer(3),detid,rms);
428 
429  if (printDebug_ > 1) {
430  std::ostringstream message;
431  message << "TkHistoMap APV0minusAPV1: Detid " << detid << ", mean = " << mean << ", rms = " << rms << ", counter = " << lStat.Counter << std::endl;
432  edm::LogVerbatim("SiStripMonitorHardware") << message.str();
433  }
434 
435  //ele++;
436  }
437 
438  }//if TkHistoMap is enabled
439 
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: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:230
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_
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
void save(const std::string &filename, const std::string &path="", const std::string &pattern="", const std::string &rewrite="", const uint32_t run=0, const uint32_t lumi=0, SaveReferenceTag ref=SaveWithReference, int minStatus=dqm::qstatus::STATUS_OK, const std::string &fileupdate="RECREATE", const bool resetMEsAfterWriting=false)
Definition: DQMStore.cc:2490
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:655