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.20 2011/10/04 17:25:59 amagnan 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 
56 
57 #include "DQM/SiStripMonitorHardware/interface/CMHistograms.hh"
58 
59 //
60 // Class declaration
61 //
62 
64 {
65  public:
66 
69  private:
70 
71  struct Statistics {
72  double Mean;
73  double Rms;
74  double Counter;
75  };
76 
77  virtual void beginJob();
78  virtual void analyze(const edm::Event&, const edm::EventSetup&);
79  virtual void endJob();
80 
81  //update the cabling if necessary
82  void updateCabling(const edm::EventSetup& eventSetup);
83 
84 
85  void fillMaps(uint32_t aDetId, unsigned short aChInModule, std::pair<uint16_t,uint16_t> aMedians);
86 
87  //tag of FEDRawData collection
89  //folder name for histograms in DQMStore
90  std::string folderName_;
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  //write the DQMStore to a root file at the end of the job
102  std::string dqmStoreFileName_;
103  //the DQMStore
105  //FED cabling
106  uint32_t cablingCacheId_;
108 
109  //add parameter to save computing time if TkHistoMap are not filled
111 
112  CMHistograms cmHists_;
113 
114  std::map<unsigned int,Statistics> CommonModes_;
115  std::map<unsigned int,Statistics> CommonModesAPV0minusAPV1_;
116 
118 
119  unsigned int evt_;
120 
121 };
122 
123 
124 //
125 // Constructors and destructor
126 //
127 
129  : rawDataTag_(iConfig.getUntrackedParameter<edm::InputTag>("RawDataTag",edm::InputTag("source",""))),
130  folderName_(iConfig.getUntrackedParameter<std::string>("HistogramFolderName","SiStrip/ReadoutView/CMMonitoring")),
131  fedIdVec_(iConfig.getUntrackedParameter<std::vector<unsigned int> >("FedIdVec")),
132  fillAllDetailedHistograms_(iConfig.getUntrackedParameter<bool>("FillAllDetailedHistograms",false)),
133  fillWithEvtNum_(iConfig.getUntrackedParameter<bool>("FillWithEventNumber",false)),
134  fillWithLocalEvtNum_(iConfig.getUntrackedParameter<bool>("FillWithLocalEventNumber",false)),
135  printDebug_(iConfig.getUntrackedParameter<unsigned int>("PrintDebugMessages",1)),
136  writeDQMStore_(iConfig.getUntrackedParameter<bool>("WriteDQMStore",false)),
137  dqmStoreFileName_(iConfig.getUntrackedParameter<std::string>("DQMStoreFileName","DQMStore.root")),
138  dqm_(0),
139  cablingCacheId_(0)
140 
141 {
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 
194  //static bool firstEvent = true;
195  //static bool isBeingFilled = false;
196  //update cabling
197  updateCabling(iSetup);
198 
199  //get raw data
200  edm::Handle<FEDRawDataCollection> rawDataCollectionHandle;
201  iEvent.getByLabel(rawDataTag_,rawDataCollectionHandle);
202  const FEDRawDataCollection& rawDataCollection = *rawDataCollectionHandle;
203 
204  //FED errors
205  FEDErrors lFedErrors;
206 
207  //loop over siStrip FED IDs
208  for (unsigned int fedId = FEDNumbering::MINSiStripFEDID;
210  fedId++) {//loop over FED IDs
211  const FEDRawData& fedData = rawDataCollection.FEDData(fedId);
212 
213  //create an object to fill all errors
214  lFedErrors.initialiseFED(fedId,cabling_);
215 
216  //Do detailed check
217  //first check if data exists
218  bool lDataExist = lFedErrors.checkDataPresent(fedData);
219  if (!lDataExist) {
220  continue;
221  }
222 
223  std::auto_ptr<const sistrip::FEDBuffer> buffer;
224 
225  if (!lFedErrors.fillFatalFEDErrors(fedData,0)) {
226  continue;
227  }
228  else {
229  //need to construct full object to go any further
230  buffer.reset(new sistrip::FEDBuffer(fedData.data(),fedData.size(),true));
231  bool channelLengthsOK = buffer->checkChannelLengthsMatchBufferLength();
232  bool channelPacketCodesOK = buffer->checkChannelPacketCodes();
233  bool feLengthsOK = buffer->checkFEUnitLengths();
234  if ( !channelLengthsOK ||
235  !channelPacketCodesOK ||
236  !feLengthsOK ) {
237  continue;
238  }
239  }
240 
241  std::ostringstream infoStream;
242 
243 
244  if (printDebug_ > 1) {
245  infoStream << " --- Processing FED #" << fedId << std::endl;
246  }
247 
248 
249  std::vector<CMHistograms::CMvalues> values;
250 
251  for (unsigned int iCh = 0;
252  iCh < sistrip::FEDCH_PER_FED;
253  iCh++) {//loop on channels
254 
255  const FedChannelConnection & lConnection = cabling_->connection(fedId,iCh);
256  bool connected = lConnection.isConnected();
257 
258  //std::cout << "FedID " << fedId << ", ch " << iCh << ", nAPVPairs " << lConnection.nApvPairs() << " apvPairNumber " << lConnection.apvPairNumber() << std::endl;
259 
260  if (!connected) {
261  continue;
262  }
263 
264  uint32_t lDetId = lConnection.detId();
265  unsigned short nChInModule = lConnection.nApvPairs();
266 
267  if (!lDetId || lDetId == sistrip::invalid32_) continue;
268 
269  bool lFailUnpackerChannelCheck = !buffer->channelGood(iCh) && connected;
270 
271  if (lFailUnpackerChannelCheck) {
272  continue;
273  }
274 
275 
276  //short lAPVPair = lConnection.apvPairNumber();
277  //short lSubDet = DetId(lDetId).subdetId();
278 
279 // if (firstEvent){
280 // infoStream << "Subdet " << lSubDet << ", " ;
281 // if (lSubDet == 3) {
282 // TIBDetId lId(lDetId);
283 // infoStream << "TIB layer " << lId.layer() << ", fedID " << fedId << ", channel " << iCh << std::endl;
284 // }
285 // else if (lSubDet == 4) {
286 // TIDDetId lId(lDetId);
287 // infoStream << "TID side " << lId.side() << " wheel " << lId.wheel() << ", ring " << lId.ring() << ", fedID " << fedId << ", channel " << iCh << std::endl;
288 // }
289 // else if (lSubDet == 5) {
290 // TOBDetId lId(lDetId);
291 // infoStream << "TOB side " << lId.rod()[0] << " layer " << lId.layer() << ", rod " << lId.rodNumber() << ", fedID " << fedId << ", channel " << iCh << std::endl;
292 // }
293 // else if (lSubDet == 6) {
294 // TECDetId lId(lDetId);
295 // infoStream << "TEC side " << lId.side() << " wheel " << lId.wheel() << ", petal " << lId.petalNumber() << ", ring " << lId.ring() << ", fedID " << fedId << ", channel " << iCh << std::endl;
296 // }
297 // isBeingFilled=true;
298 // }
299 
300  std::ostringstream lMode;
301  lMode << buffer->readoutMode();
302  if (printDebug_ > 1) {
303  static bool lFirst = true;
304  if (lFirst) {
305  std::cout << "Readout mode: " << lMode.str() << std::endl;
306  lFirst = false;
307  }
308 
309  }
310 
311  const sistrip::FEDChannel & lChannel = buffer->channel(iCh);
312  std::pair<uint16_t,uint16_t> medians = std::pair<uint16_t,uint16_t>(0,0);
313 
314  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));
315 
316  CMHistograms::CMvalues lVal;
317  lVal.ChannelID = iCh;
318  lVal.Medians = std::pair<uint16_t,uint16_t>(medians.first,medians.second);
319  lVal.PreviousMedians = prevMedians_[fedId][iCh];
320 
321 // if (medians.second-medians.first > 26){
322 // std::ostringstream info;
323 // if (medians.second-medians.first > 44) info << " --- Second bump: event " << iEvent.id().event() << ", FED/Channel " << fedId << "/" << iCh << ", delta=" << medians.second-medians.first << std::endl;
324 // else info << " --- First bump: event " << iEvent.id().event() << ", FED/Channel " << fedId << "/" << iCh << ", delta=" << medians.second-medians.first << std::endl;
325 // edm::LogVerbatim("SiStripMonitorHardware") << info.str();
326 // }
327 
328  if (printDebug_ > 1) {
329  if (lChannel.length() > 7) {
330  infoStream << "Medians for channel #" << iCh << " (length " << lChannel.length() << "): " << medians.first << ", " << medians.second << std::endl;
331  }
332  }
333 
334  values.push_back(lVal);
335 
336  //if (iEvent.id().event() > 1000)
337  fillMaps(lDetId,nChInModule,medians);
338 
339  prevMedians_[fedId][iCh] = std::pair<uint16_t,uint16_t>(medians.first,medians.second);
340 
341  }//loop on channels
342 
343  float lTime = 0;
344  if (fillWithEvtNum_) lTime = iEvent.id().event();
345  else if (fillWithLocalEvtNum_) lTime = evt_;//iEvent.id().event();
346  else lTime = iEvent.orbitNumber()/11223.;
347 
348  cmHists_.fillHistograms(values,lTime,fedId);
349 
350  //if (printDebug_ > 0 && isBeingFilled && firstEvent) edm::LogVerbatim("SiStripMonitorHardware") << infoStream.str();
351 
352 
353 
354  }//loop on FEDs
355 
356 
357  //if (isBeingFilled)
358  //firstEvent = false;
359 
360  evt_++;
361 
362 }//analyze method
363 
364 // ------------ method called once each job just before starting event loop ------------
365 void
367 {
368  //get DQM store
371 
372  cmHists_.bookTopLevelHistograms(dqm_);
373 
374  if (fillAllDetailedHistograms_) cmHists_.bookAllFEDHistograms();
375 
376 }
377 
378 // ------------ method called once each job just after ending the event loop ------------
379 void
381 {
382 
383  if (doTkHistoMap_) {//if TkHistoMap is enabled
384  std::map<unsigned int,Statistics>::iterator fracIter;
385 
386  //int ele = 0;
387  //int nBadChannels = 0;
388  for (fracIter = CommonModes_.begin(); fracIter!=CommonModes_.end(); fracIter++){
389  uint32_t detid = fracIter->first;
390  //if ((fracIter->second).second != 0) {
391  //std::cout << "------ ele #" << ele << ", Frac for detid #" << detid << " = " <<(fracIter->second).second << "/" << (fracIter->second).first << std::endl;
392  //nBadChannels++;
393  //}
394  float mean = 0;
395  float rms = 0;
396  Statistics lStat = fracIter->second;
397  if (lStat.Counter > 0) mean = lStat.Mean/lStat.Counter;
398  if (lStat.Counter > 1) rms = sqrt(lStat.Rms/(lStat.Counter-1)-(mean*mean));
399  cmHists_.fillTkHistoMap(cmHists_.tkHistoMapPointer(0),detid,mean);
400  cmHists_.fillTkHistoMap(cmHists_.tkHistoMapPointer(1),detid,rms);
401 
402  if (printDebug_ > 1) {
403  std::ostringstream message;
404  message << "TkHistoMap CM: Detid " << detid << ", mean = " << mean << ", rms = " << rms << ", counter = " << lStat.Counter << std::endl;
405  edm::LogVerbatim("SiStripMonitorHardware") << message.str();
406  }
407 
408  //ele++;
409  }
410 
411  for (fracIter = CommonModesAPV0minusAPV1_.begin(); fracIter!=CommonModesAPV0minusAPV1_.end(); fracIter++){
412  uint32_t detid = fracIter->first;
413  //if ((fracIter->second).second != 0) {
414  //std::cout << "------ ele #" << ele << ", Frac for detid #" << detid << " = " <<(fracIter->second).second << "/" << (fracIter->second).first << std::endl;
415  //nBadChannels++;
416  //}
417  float mean = 0;
418  float rms = 0;
419  Statistics lStat = fracIter->second;
420  if (lStat.Counter > 0) mean = lStat.Mean/lStat.Counter;
421  if (lStat.Counter > 1) rms = sqrt(lStat.Rms/(lStat.Counter-1)-(mean*mean));
422  cmHists_.fillTkHistoMap(cmHists_.tkHistoMapPointer(2),detid,mean);
423  cmHists_.fillTkHistoMap(cmHists_.tkHistoMapPointer(3),detid,rms);
424 
425  if (printDebug_ > 1) {
426  std::ostringstream message;
427  message << "TkHistoMap APV0minusAPV1: Detid " << detid << ", mean = " << mean << ", rms = " << rms << ", counter = " << lStat.Counter << std::endl;
428  edm::LogVerbatim("SiStripMonitorHardware") << message.str();
429  }
430 
431  //ele++;
432  }
433 
434  }//if TkHistoMap is enabled
435 
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: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:2113
#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:46
int orbitNumber() const
Definition: EventBase.h:63
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:356
#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:429
virtual void analyze(const edm::Event &, const edm::EventSetup &)