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 // $Id: SiStripCMMonitor.cc,v 1.3 2013/01/03 18:59:36 wmtan Exp $
14 //
15 
16 #include <sstream>
17 #include <memory>
18 #include <list>
19 #include <algorithm>
20 #include <cassert>
21 
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();
76  virtual void analyze(const edm::Event&, const edm::EventSetup&);
77  virtual void endJob();
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
87  //folder name for histograms in DQMStore
89  //vector of fedIDs which will have detailed histograms made
90  std::vector<unsigned int> fedIdVec_;
91  //book detailed histograms even if they will be empty (for merging)
93  //do histos vs time with time=event number. Default time = orbit number (s)
96  //print debug messages when problems are found: 1=error debug, 2=light debug, 3=full debug
97  unsigned int printDebug_;
98  //write the DQMStore to a root file at the end of the job
101  //the DQMStore
103  //FED cabling
104  uint32_t cablingCacheId_;
106 
107  //add parameter to save computing time if TkHistoMap are not filled
109 
110  CMHistograms cmHists_;
111 
112  std::map<unsigned int,Statistics> CommonModes_;
113  std::map<unsigned int,Statistics> CommonModesAPV0minusAPV1_;
114 
116 
117  unsigned int evt_;
118 
119 };
120 
121 
122 //
123 // Constructors and destructor
124 //
125 
127  : rawDataTag_(iConfig.getUntrackedParameter<edm::InputTag>("RawDataTag",edm::InputTag("source",""))),
128  folderName_(iConfig.getUntrackedParameter<std::string>("HistogramFolderName","SiStrip/ReadoutView/CMMonitoring")),
129  fedIdVec_(iConfig.getUntrackedParameter<std::vector<unsigned int> >("FedIdVec")),
130  fillAllDetailedHistograms_(iConfig.getUntrackedParameter<bool>("FillAllDetailedHistograms",false)),
131  fillWithEvtNum_(iConfig.getUntrackedParameter<bool>("FillWithEventNumber",false)),
132  fillWithLocalEvtNum_(iConfig.getUntrackedParameter<bool>("FillWithLocalEventNumber",false)),
133  printDebug_(iConfig.getUntrackedParameter<unsigned int>("PrintDebugMessages",1)),
134  writeDQMStore_(iConfig.getUntrackedParameter<bool>("WriteDQMStore",false)),
135  dqmStoreFileName_(iConfig.getUntrackedParameter<std::string>("DQMStoreFileName","DQMStore.root")),
136  dqm_(0),
137  cablingCacheId_(0)
138 
139 {
140  //print config to debug log
141  std::ostringstream debugStream;
142  if (printDebug_>1) {
143  debugStream << "[SiStripCMMonitorPlugin]Configuration for SiStripCMMonitorPlugin: " << std::endl
144  << "[SiStripCMMonitorPlugin]\tRawDataTag: " << rawDataTag_ << std::endl
145  << "[SiStripCMMonitorPlugin]\tHistogramFolderName: " << folderName_ << std::endl
146  << "[SiStripCMMonitorPlugin]\tFillAllDetailedHistograms? " << (fillAllDetailedHistograms_ ? "yes" : "no") << std::endl
147  << "[SiStripCMMonitorPlugin]\tFillWithEventNumber?" << (fillWithEvtNum_ ? "yes" : "no") << std::endl
148  << "[SiStripCMMonitorPlugin]\tPrintDebugMessages? " << (printDebug_ ? "yes" : "no") << std::endl
149  << "[SiStripCMMonitorPlugin]\tWriteDQMStore? " << (writeDQMStore_ ? "yes" : "no") << std::endl;
150  if (writeDQMStore_) debugStream << "[SiStripCMMonitorPlugin]\tDQMStoreFileName: " << dqmStoreFileName_ << std::endl;
151  }
152 
153  std::ostringstream* pDebugStream = (printDebug_>1 ? &debugStream : NULL);
154 
155  cmHists_.initialise(iConfig,pDebugStream);
156 
157  doTkHistoMap_ = cmHists_.tkHistoMapEnabled();
158 
159  CommonModes_.clear();
161 
162  for (unsigned int fedId(FEDNumbering::MINSiStripFEDID); fedId <= FEDNumbering::MAXSiStripFEDID; fedId++){
163  for (unsigned int iCh(0); iCh<sistrip::FEDCH_PER_FED; iCh++){
164  prevMedians_[fedId][iCh] = std::pair<uint16_t,uint16_t>(0,0);
165  }
166  }
167 
168 
169  if (printDebug_) {
170  LogTrace("SiStripMonitorHardware") << debugStream.str();
171  }
172 
173  evt_ = 0;
174 
175 }
176 
178 {
179 }
180 
181 
182 //
183 // Member functions
184 //
185 
186 // ------------ method called to for each event ------------
187 void
189  const edm::EventSetup& iSetup)
190 {
191  //Retrieve tracker topology from geometry
192  edm::ESHandle<TrackerTopology> tTopoHandle;
193  iSetup.get<IdealGeometryRecord>().get(tTopoHandle);
194  const TrackerTopology* const tTopo = tTopoHandle.product();
195 
196  //static bool firstEvent = true;
197  //static bool isBeingFilled = false;
198  //update cabling
199  updateCabling(iSetup);
200 
201  //get raw data
202  edm::Handle<FEDRawDataCollection> rawDataCollectionHandle;
203  iEvent.getByLabel(rawDataTag_,rawDataCollectionHandle);
204  const FEDRawDataCollection& rawDataCollection = *rawDataCollectionHandle;
205 
206  //FED errors
207  FEDErrors lFedErrors;
208 
209  //loop over siStrip FED IDs
210  for (unsigned int fedId = FEDNumbering::MINSiStripFEDID;
212  fedId++) {//loop over FED IDs
213  const FEDRawData& fedData = rawDataCollection.FEDData(fedId);
214 
215  //create an object to fill all errors
216  lFedErrors.initialiseFED(fedId,cabling_,tTopo);
217 
218  //Do detailed check
219  //first check if data exists
220  bool lDataExist = lFedErrors.checkDataPresent(fedData);
221  if (!lDataExist) {
222  continue;
223  }
224 
225  std::auto_ptr<const sistrip::FEDBuffer> buffer;
226 
227  if (!lFedErrors.fillFatalFEDErrors(fedData,0)) {
228  continue;
229  }
230  else {
231  //need to construct full object to go any further
232  buffer.reset(new sistrip::FEDBuffer(fedData.data(),fedData.size(),true));
233  bool channelLengthsOK = buffer->checkChannelLengthsMatchBufferLength();
234  bool channelPacketCodesOK = buffer->checkChannelPacketCodes();
235  bool feLengthsOK = buffer->checkFEUnitLengths();
236  if ( !channelLengthsOK ||
237  !channelPacketCodesOK ||
238  !feLengthsOK ) {
239  continue;
240  }
241  }
242 
243  std::ostringstream infoStream;
244 
245 
246  if (printDebug_ > 1) {
247  infoStream << " --- Processing FED #" << fedId << std::endl;
248  }
249 
250 
251  std::vector<CMHistograms::CMvalues> values;
252 
253  for (unsigned int iCh = 0;
254  iCh < sistrip::FEDCH_PER_FED;
255  iCh++) {//loop on channels
256 
257  const FedChannelConnection & lConnection = cabling_->connection(fedId,iCh);
258  bool connected = lConnection.isConnected();
259 
260  //std::cout << "FedID " << fedId << ", ch " << iCh << ", nAPVPairs " << lConnection.nApvPairs() << " apvPairNumber " << lConnection.apvPairNumber() << std::endl;
261 
262  if (!connected) {
263  continue;
264  }
265 
266  uint32_t lDetId = lConnection.detId();
267  unsigned short nChInModule = lConnection.nApvPairs();
268 
269  if (!lDetId || lDetId == sistrip::invalid32_) continue;
270 
271  bool lFailUnpackerChannelCheck = !buffer->channelGood(iCh) && connected;
272 
273  if (lFailUnpackerChannelCheck) {
274  continue;
275  }
276 
277 
278  //short lAPVPair = lConnection.apvPairNumber();
279  //short lSubDet = DetId(lDetId).subdetId();
280 
281 // if (firstEvent){
282 // infoStream << "Subdet " << lSubDet << ", " ;
283 // if (lSubDet == 3) {
284 //
285 // infoStream << "TIB layer " << tTopo->tibLayer(lDetId) << ", fedID " << fedId << ", channel " << iCh << std::endl;
286 // }
287 // else if (lSubDet == 4) {
288 //
289 // infoStream << "TID side " << tTopo->tibSide(lDetId) << " wheel " << tTopo->tibWheel(lDetId) << ", ring " << tTopo->tibRing(lDetId) << ", fedID " << fedId << ", channel " << iCh << std::endl;
290 // }
291 // else if (lSubDet == 5) {
292 //
293 // infoStream << "TOB side " << tTopo->tibRod(lDetId)[0] << " layer " << tTopo->tibLayer(lDetId) << ", rod " << tTopo->tibRodNumber(lDetId) << ", fedID " << fedId << ", channel " << iCh << std::endl;
294 // }
295 // else if (lSubDet == 6) {
296 //
297 // infoStream << "TEC side " << tTopo->tibSide(lDetId) << " wheel " << tTopo->tibWheel(lDetId) << ", petal " << tTopo->tibPetalNumber(lDetId) << ", ring " << tTopo->tibRing(lDetId) << ", fedID " << fedId << ", channel " << iCh << std::endl;
298 // }
299 // isBeingFilled=true;
300 // }
301 
302  std::ostringstream lMode;
303  lMode << buffer->readoutMode();
304  if (printDebug_ > 1) {
305  static bool lFirst = true;
306  if (lFirst) {
307  std::cout << "Readout mode: " << lMode.str() << std::endl;
308  lFirst = false;
309  }
310 
311  }
312 
313  const sistrip::FEDChannel & lChannel = buffer->channel(iCh);
314  std::pair<uint16_t,uint16_t> medians = std::pair<uint16_t,uint16_t>(0,0);
315 
316  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));
317 
318  CMHistograms::CMvalues lVal;
319  lVal.ChannelID = iCh;
320  lVal.Medians = std::pair<uint16_t,uint16_t>(medians.first,medians.second);
321  lVal.PreviousMedians = prevMedians_[fedId][iCh];
322 
323 // if (medians.second-medians.first > 26){
324 // std::ostringstream info;
325 // if (medians.second-medians.first > 44) info << " --- Second bump: event " << iEvent.id().event() << ", FED/Channel " << fedId << "/" << iCh << ", delta=" << medians.second-medians.first << std::endl;
326 // else info << " --- First bump: event " << iEvent.id().event() << ", FED/Channel " << fedId << "/" << iCh << ", delta=" << medians.second-medians.first << std::endl;
327 // edm::LogVerbatim("SiStripMonitorHardware") << info.str();
328 // }
329 
330  if (printDebug_ > 1) {
331  if (lChannel.length() > 7) {
332  infoStream << "Medians for channel #" << iCh << " (length " << lChannel.length() << "): " << medians.first << ", " << medians.second << std::endl;
333  }
334  }
335 
336  values.push_back(lVal);
337 
338  //if (iEvent.id().event() > 1000)
339  fillMaps(lDetId,nChInModule,medians);
340 
341  prevMedians_[fedId][iCh] = std::pair<uint16_t,uint16_t>(medians.first,medians.second);
342 
343  }//loop on channels
344 
345  float lTime = 0;
346  if (fillWithEvtNum_) lTime = iEvent.id().event();
347  else if (fillWithLocalEvtNum_) lTime = evt_;//iEvent.id().event();
348  else lTime = iEvent.orbitNumber()/11223.;
349 
350  cmHists_.fillHistograms(values,lTime,fedId);
351 
352  //if (printDebug_ > 0 && isBeingFilled && firstEvent) edm::LogVerbatim("SiStripMonitorHardware") << infoStream.str();
353 
354 
355 
356  }//loop on FEDs
357 
358 
359  //if (isBeingFilled)
360  //firstEvent = false;
361 
362  evt_++;
363 
364 }//analyze method
365 
366 // ------------ method called once each job just before starting event loop ------------
367 void
369 {
370  //get DQM store
373 
374  cmHists_.bookTopLevelHistograms(dqm_);
375 
376  if (fillAllDetailedHistograms_) cmHists_.bookAllFEDHistograms();
377 
378 }
379 
380 // ------------ method called once each job just after ending the event loop ------------
381 void
383 {
384 
385  if (doTkHistoMap_) {//if TkHistoMap is enabled
386  std::map<unsigned int,Statistics>::iterator fracIter;
387 
388  //int ele = 0;
389  //int nBadChannels = 0;
390  for (fracIter = CommonModes_.begin(); fracIter!=CommonModes_.end(); fracIter++){
391  uint32_t detid = fracIter->first;
392  //if ((fracIter->second).second != 0) {
393  //std::cout << "------ ele #" << ele << ", Frac for detid #" << detid << " = " <<(fracIter->second).second << "/" << (fracIter->second).first << std::endl;
394  //nBadChannels++;
395  //}
396  float mean = 0;
397  float rms = 0;
398  Statistics lStat = fracIter->second;
399  if (lStat.Counter > 0) mean = lStat.Mean/lStat.Counter;
400  if (lStat.Counter > 1) rms = sqrt(lStat.Rms/(lStat.Counter-1)-(mean*mean));
401  cmHists_.fillTkHistoMap(cmHists_.tkHistoMapPointer(0),detid,mean);
402  cmHists_.fillTkHistoMap(cmHists_.tkHistoMapPointer(1),detid,rms);
403 
404  if (printDebug_ > 1) {
405  std::ostringstream message;
406  message << "TkHistoMap CM: Detid " << detid << ", mean = " << mean << ", rms = " << rms << ", counter = " << lStat.Counter << std::endl;
407  edm::LogVerbatim("SiStripMonitorHardware") << message.str();
408  }
409 
410  //ele++;
411  }
412 
413  for (fracIter = CommonModesAPV0minusAPV1_.begin(); fracIter!=CommonModesAPV0minusAPV1_.end(); fracIter++){
414  uint32_t detid = fracIter->first;
415  //if ((fracIter->second).second != 0) {
416  //std::cout << "------ ele #" << ele << ", Frac for detid #" << detid << " = " <<(fracIter->second).second << "/" << (fracIter->second).first << std::endl;
417  //nBadChannels++;
418  //}
419  float mean = 0;
420  float rms = 0;
421  Statistics lStat = fracIter->second;
422  if (lStat.Counter > 0) mean = lStat.Mean/lStat.Counter;
423  if (lStat.Counter > 1) rms = sqrt(lStat.Rms/(lStat.Counter-1)-(mean*mean));
424  cmHists_.fillTkHistoMap(cmHists_.tkHistoMapPointer(2),detid,mean);
425  cmHists_.fillTkHistoMap(cmHists_.tkHistoMapPointer(3),detid,rms);
426 
427  if (printDebug_ > 1) {
428  std::ostringstream message;
429  message << "TkHistoMap APV0minusAPV1: Detid " << detid << ", mean = " << mean << ", rms = " << rms << ", counter = " << lStat.Counter << std::endl;
430  edm::LogVerbatim("SiStripMonitorHardware") << message.str();
431  }
432 
433  //ele++;
434  }
435 
436  }//if TkHistoMap is enabled
437 
439 }
440 
442 {
443  uint32_t currentCacheId = eventSetup.get<SiStripFedCablingRcd>().cacheIdentifier();
444  if (cablingCacheId_ != currentCacheId) {
445  edm::ESHandle<SiStripFedCabling> cablingHandle;
446  eventSetup.get<SiStripFedCablingRcd>().get(cablingHandle);
447  cabling_ = cablingHandle.product();
448  cablingCacheId_ = currentCacheId;
449  }
450 }
451 
452 
453 void SiStripCMMonitorPlugin::fillMaps(uint32_t aDetId, unsigned short aChInModule, std::pair<uint16_t,uint16_t> aMedians)
454 {
455 
456  if (doTkHistoMap_){//if TkHistMap is enabled
457  std::pair<std::map<unsigned int,Statistics>::iterator,bool> alreadyThere[2];
458 
459  Statistics lStat;
460  lStat.Mean = (aMedians.first+aMedians.second)*1./(2*aChInModule);
461  lStat.Rms = (aMedians.first+aMedians.second)*(aMedians.first+aMedians.second)*1./(4*aChInModule);
462  lStat.Counter = 1./aChInModule;
463 
464  alreadyThere[0] = CommonModes_.insert(std::pair<unsigned int,Statistics>(aDetId,lStat));
465  if (!alreadyThere[0].second) {
466  ((alreadyThere[0].first)->second).Mean += (aMedians.first+aMedians.second)*1./(2*aChInModule);
467  ((alreadyThere[0].first)->second).Rms += (aMedians.first+aMedians.second)*(aMedians.first+aMedians.second)*1./(4*aChInModule);
468  ((alreadyThere[0].first)->second).Counter += 1./aChInModule;
469  }
470 
471  lStat.Mean = (aMedians.first-aMedians.second)*1./aChInModule;
472  lStat.Rms = (aMedians.first-aMedians.second)*(aMedians.first-aMedians.second)*1./aChInModule;
473  lStat.Counter = 1./aChInModule;
474 
475  alreadyThere[1] = CommonModesAPV0minusAPV1_.insert(std::pair<unsigned int,Statistics>(aDetId,lStat));
476  if (!alreadyThere[1].second) {
477  ((alreadyThere[1].first)->second).Mean += (aMedians.first-aMedians.second)*1./aChInModule;
478  ((alreadyThere[1].first)->second).Rms += (aMedians.first-aMedians.second)*(aMedians.first-aMedians.second)*1./aChInModule;
479  ((alreadyThere[1].first)->second).Counter += 1./aChInModule;
480  }
481 
482  }
483 
484 }
485 
486 
487 //
488 // Define as a plug-in
489 //
490 
EventNumber_t event() const
Definition: EventID.h:44
std::pair< uint16_t, uint16_t > prevMedians_[FEDNumbering::MAXSiStripFEDID+1][sistrip::FEDCH_PER_FED]
static const uint32_t invalid32_
Definition: Constants.h:16
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
void fillMaps(uint32_t aDetId, unsigned short aChInModule, std::pair< uint16_t, uint16_t > aMedians)
void save(const std::string &filename, const std::string &path="", const std::string &pattern="", const std::string &rewrite="", SaveReferenceTag ref=SaveWithReference, int minStatus=dqm::qstatus::STATUS_OK, const std::string &fileupdate="RECREATE")
Definition: DQMStore.cc:2118
#define NULL
Definition: scimark2.h:8
const FedChannelConnection & connection(uint16_t fed_id, uint16_t fed_ch) const
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:49
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
int orbitNumber() const
Definition: EventBase.h:63
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:361
#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:29
tuple cout
Definition: gather_cfg.py:121
void setCurrentFolder(const std::string &fullpath)
Definition: DQMStore.cc:434
virtual void analyze(const edm::Event &, const edm::EventSetup &)