CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
SiStripFEDErrorsDQM.cc
Go to the documentation of this file.
1 #include "SiStripFEDErrorsDQM.h"
2 
4 
5 using namespace std;
6 using namespace edm;
7 
10  iConfig_(iConfig),
11  fp_(iConfig.getUntrackedParameter<edm::FileInPath>("file",edm::FileInPath("CalibTracker/SiStripCommon/data/SiStripDetInfo.dat"))),
12  cablingCacheId_(0),
13  threshold_(iConfig.getUntrackedParameter<double>("Threshold",0)),
14  debug_(iConfig.getUntrackedParameter<unsigned int>("Debug",0))
15 {
16  obj_ = 0;
17  edm::LogInfo("SiStripFEDErrorsDQM") << "[SiStripFEDErrorsDQM::SiStripFEDErrorsDQM()]";
18 }
19 
21  edm::LogInfo("SiStripFEDErrorsDQM") << "[SiStripFEDErrorsDQM::~SiStripFEDErrorsDQM]";
22 }
23 
24 // ------------ method called to for each event ------------
26 {
27  //update cabling
28  updateCabling(iSetup);
29 
30  if (readBadAPVs())
31  {
32  // Save the parameters to the db.
34  if( mydbservice.isAvailable() ){
35  if( mydbservice->isNewTagRequest("SiStripBadStripRcd") ){
36  mydbservice->createNewIOV<SiStripBadStrip>(obj_, mydbservice->beginOfTime(),mydbservice->endOfTime(),"SiStripBadStripRcd");
37  } else {
38  mydbservice->appendSinceTime<SiStripBadStrip>(obj_, mydbservice->currentTime(),"SiStripBadStripRcd");
39  }
40  } else {
41  edm::LogError("SiStripFEDErrorsDQM")<<"Service is unavailable"<<std::endl;
42  }
43  }
44 
45 }
46 
47 // ------------ method called once each job just before starting event loop ------------
48 void
50 {
51 }
52 
53 // ------------ method called once each job just after ending the event loop ------------
54 void
56 }
57 
59 {
60  uint32_t currentCacheId = iSetup.get<SiStripFedCablingRcd>().cacheIdentifier();
61  if (cablingCacheId_ != currentCacheId) {
63  iSetup.get<SiStripFedCablingRcd>().get(cablingHandle);
64  cabling_ = cablingHandle.product();
65  cablingCacheId_ = currentCacheId;
66  }
67 }
68 
70 
71  //std::cout << "[SiStripFEDErrorsDQM::readBadAPVs]" << std::endl;
72 
74 
75  //std::cout << "[SiStripFEDErrorsDQM::readBadAPVs]: opened requested file" << std::endl;
76 
77  obj_=new SiStripBadStrip();
78 
80 
81  std::ostringstream lPath;
82  lPath << "Run " << getRunNumber() << "/SiStrip/Run summary/ReadoutView/";
83 
84  dqmStore_->setCurrentFolder(lPath.str());
85  LogTrace("SiStripFEDErrorsDQM") << "[SiStripFEDErrorsDQM::readBadAPVs] Now in " << dqmStore_->pwd() << std::endl;
86 
87  std::string lBaseDir = dqmStore_->pwd();
88 
89  std::vector<std::pair<std::string,unsigned int> > lFedsFolder;
90  //for FED errors, use summary folder and fedId=0
91  //do not put a slash or "goToDir" won't work...
92  lFedsFolder.push_back(std::pair<std::string,unsigned int>("FedMonitoringSummary",0));
93 
94  //for FE/channel/APV errors, they are written in a folder per FED,
95  //if there was at least one error.
96  //So just loop on folders and see which ones exist.
97  for (unsigned int ifed(FEDNumbering::MINSiStripFEDID);
99  ifed++){//loop on FEDs
100 
101  std::ostringstream lFedDir;
102  lFedDir << "FrontEndDriver" << ifed;
103  if (!goToDir(lFedDir.str())) continue;
104  //if (!dqmStore_->dirExists(lFedDir.str())) continue;
105  else {
106  if (debug_) LogTrace("SiStripFEDErrorsDQM") << "[SiStripFEDErrorsDQM::readBadAPVs] - Errors detected for FED " << ifed << std::endl;
107  lFedsFolder.push_back(std::pair<std::string,unsigned int>(lFedDir.str(),ifed));
108  }
109  dqmStore_->goUp();
110  }
111 
112  unsigned int nAPVsTotal = 0;
113  //retrieve total number of APVs valid and connected from cabling:
114  if (!cabling_) {
115  edm::LogError("SiStripFEDErrorsDQM") << "[SiStripFEDErrorsDQM::readBadAPVs] cabling not filled, return false " << std::endl;
116  return false;
117  }
118  auto lFedVec = cabling_->fedIds();
119  for (unsigned int iFed(0);iFed<lFedVec.size();iFed++){
120  if (*(lFedVec.begin() + iFed) < sistrip::FED_ID_MIN || *(lFedVec.begin() + iFed) > sistrip::FED_ID_MAX) {
121  edm::LogError("SiStripFEDErrorsDQM") << "[SiStripFEDErrorsDQM::readBadAPVs] Invalid fedid : " << *(lFedVec.begin() + iFed) << std::endl;
122  continue;
123  }
124  auto lConnVec = cabling_->fedConnections(*(lFedVec.begin() + iFed));
125  for (unsigned int iConn(0); iConn<lConnVec.size();iConn++){
126  const FedChannelConnection & lConnection = *(lConnVec.begin() + iConn);
127  if (!lConnection.isConnected()) continue;
128  unsigned int lDetid = lConnection.detId();
129  if (!lDetid || lDetid == sistrip::invalid32_) continue;
130  //2 APVs per channel....
131  nAPVsTotal += 2;
132  }
133  }
134 
135  edm::LogInfo("SiStripFEDErrorsDQM") << "[SiStripFEDErrorsDQM::readBadAPVs] Total number of APVs found : " << nAPVsTotal << std::endl;
136 
137  unsigned int nAPVsWithErrorTotal = 0;
138  unsigned int nFolders = 0;
139  float lNorm = 0;
140 
141 
142  for( std::vector<std::pair<std::string,unsigned int> >::const_iterator iFolder = lFedsFolder.begin();
143  iFolder != lFedsFolder.end();
144  ++iFolder ) {//loop on lFedsFolders
145  std::string lDirName = lBaseDir + "/" + (*iFolder).first;
146  unsigned int lFedId = (*iFolder).second;
147 
148  if (!goToDir((*iFolder).first)) continue;
149 
150  std::vector<MonitorElement *> lMeVec = dqmStore_->getContents(lDirName);
151 
152  if (nFolders == 0) {
153 
154  for( std::vector<MonitorElement *>::const_iterator iMe = lMeVec.begin();
155  iMe != lMeVec.end();
156  ++iMe ) {//loop on ME found in directory
157 
158  std::string lMeName = (*iMe)->getName() ;
159  if (lMeName.find("nFEDErrors") != lMeName.npos){
160  lNorm = (*iMe)->getEntries();
161  }
162  }
163  //if norm histo has not been found, no point in continuing....
164  if (lNorm < 1) {
165  edm::LogError("SiStripFEDErrorsDQM") << "[SiStripFEDErrorsDQM::readBadAPVs] nFEDErrors not found, norm is " << lNorm << std::endl;
166  return false;
167  }
168  }
169 
170  unsigned int nAPVsWithError = 0;
171 
172  for( std::vector<MonitorElement *>::const_iterator iMe = lMeVec.begin();
173  iMe != lMeVec.end();
174  ++iMe ) {//loop on ME found in directory
175 
176  if ((*iMe)->getEntries() == 0) continue;
177  std::string lMeName = (*iMe)->getName() ;
178 
179  bool lookForErrors = false;
180  if (nFolders == 0) {
181  //for the first element of lFedsFolder: this is FED errors
182  lookForErrors =
183  lMeName.find("DataMissing") != lMeName.npos ||
184  lMeName.find("AnyFEDErrors") != lMeName.npos ||
185  (lMeName.find("CorruptBuffer") != lMeName.npos &&
186  lMeName.find("nFED") == lMeName.npos);
187  }
188  else {
189  //for the others, it is channel or FE errors.
190  lookForErrors =
191  lMeName.find("APVAddressError") != lMeName.npos ||
192  lMeName.find("APVError") != lMeName.npos ||
193  lMeName.find("BadMajorityAddresses") != lMeName.npos ||
194  lMeName.find("FEMissing") != lMeName.npos ||
195  lMeName.find("OOSBits") != lMeName.npos ||
196  lMeName.find("UnlockedBits") != lMeName.npos;
197  }
198 
199  if (lookForErrors) readHistogram(*iMe,nAPVsWithError,lNorm,lFedId);
200 
201  }//loop on ME found in directory
202 
203  nAPVsWithErrorTotal += nAPVsWithError;
204  ++nFolders;
205 
206  dqmStore_->goUp();
207 
208  }//loop on lFedsFolders
209 
210  edm::LogInfo("SiStripFEDErrorsDQM") << "[SiStripFEDErrorsDQM::readBadAPVs] Total APVs with error found above threshold = " << nAPVsWithErrorTotal << std::endl;
211 
212  dqmStore_->cd();
213 
214  addErrors();
215 
216  return true;
217 }//method
218 
220  unsigned int & aCounter,
221  const float aNorm,
222  const unsigned int aFedId)
223 {
224  unsigned short lFlag = 0;
225  std::string lMeName = aMe->getName();
226  if (lMeName.find("DataMissing") != lMeName.npos) {
227  lFlag = 0;
228  }
229  else if (lMeName.find("AnyFEDErrors") != lMeName.npos) {
230  lFlag = 1;
231  }
232  else if (lMeName.find("CorruptBuffer") != lMeName.npos &&
233  lMeName.find("nFED") == lMeName.npos) {
234  lFlag = 2;
235  }
236  else if (lMeName.find("FEMissing") != lMeName.npos) {
237  lFlag = 3;
238  }
239  else if (lMeName.find("BadMajorityAddresses") != lMeName.npos) {
240  lFlag = 4;
241  }
242  else if (lMeName.find("UnlockedBits") != lMeName.npos) {
243  lFlag = 5;
244  }
245  else if (lMeName.find("OOSBits") != lMeName.npos) {
246  lFlag = 6;
247  }
248  else if (lMeName.find("APVAddressError") != lMeName.npos) {
249  lFlag = 7;
250  }
251  else if (lMeName.find("APVError") != lMeName.npos) {
252  lFlag = 8;
253  }
254  else {
255  edm::LogError("SiStripFEDErrorsDQM") << "[SiStripFEDErrorsDQM::readHistogramError] Shouldn't be here ..." << std::endl;
256  return;
257  }
258 
259  if (debug_) {
260  LogTrace("SiStripFEDErrorsDQM") << "[SiStripFEDErrorsDQM::readHistogramError] Reading histo : " << lMeName << ", flag = " << lFlag << std::endl;
261  }
262 
263  unsigned int lNBins = aMe->getNbinsX();
264  int lBinShift = 0;
265  bool lIsFedHist = false;
266  bool lIsAPVHist = false;
267  bool lIsFeHist = false;
268  bool lIsChHist = false;
269 
270  if (lNBins > 200) {
271  lBinShift = FEDNumbering::MINSiStripFEDID-1;//shift for FED ID from bin number
272  lIsFedHist = true;
273  }
274  else {
275  lBinShift = -1;//shift for channel/APV/FE id from bin number
276  if (lNBins > 100) lIsAPVHist = true;
277  else if (lNBins < 10) lIsFeHist = true;
278  else lIsChHist = true;
279  }
280 
281  if (debug_) {
282  LogTrace("SiStripFEDErrorsDQM") << "[SiStripFEDErrorsDQM::readHistogramError] lIsFedHist: " << lIsFedHist << std::endl
283  << "[SiStripFEDErrorsDQM::readHistogramError] lIsAPVHist: " << lIsAPVHist << std::endl
284  << "[SiStripFEDErrorsDQM::readHistogramError] lIsFeHist : " << lIsFeHist << std::endl
285  << "[SiStripFEDErrorsDQM::readHistogramError] lIsChHist : " << lIsChHist << std::endl;
286  }
287 
288  for (unsigned int ibin(1); ibin<lNBins+1; ibin++){
289  if (aMe->getBinContent(ibin)>0){
290  float lStat = aMe->getBinContent(ibin)*1./aNorm;
291  if (lStat <= threshold_) {
292  if (debug_) LogTrace("SiStripFEDErrorsDQM") << "[SiStripFEDErrorsDQM::readHistogramError] ---- Below threshold : " << lStat << std::endl;
293  continue;
294  }
295  if (lIsFedHist) {
296  unsigned int lFedId = ibin+lBinShift;
297  //loop on all enabled channels of this FED....
298  for (unsigned int iChId = 0;
299  iChId < sistrip::FEDCH_PER_FED;
300  iChId++) {//loop on channels
301  const FedChannelConnection & lConnection = cabling_->fedConnection(lFedId,iChId);
302  if (!lConnection.isConnected()) continue;
303  addBadAPV(lConnection,0,lFlag,aCounter);
304  }
305  }
306  else {
307  if(lIsFeHist) {
308  unsigned int iFeId = ibin+lBinShift;
309  //loop on all enabled channels of this FE....
310  for (unsigned int iFeCh = 0;
311  iFeCh < sistrip::FEDCH_PER_FEUNIT;
312  iFeCh++) {//loop on channels
313  unsigned int iChId = sistrip::FEDCH_PER_FEUNIT*iFeId+iFeCh;
314  const FedChannelConnection & lConnection = cabling_->fedConnection(aFedId,iChId);
315  if (!lConnection.isConnected()) continue;
316  addBadAPV(lConnection,0,lFlag,aCounter);
317  }
318  }
319  else {
320  unsigned int iChId = ibin+lBinShift;
321  if (lIsAPVHist) {
322  unsigned int iAPVid = iChId%2+1;
323  iChId = static_cast<unsigned int>(iChId/2.);
324  const FedChannelConnection & lConnection = cabling_->fedConnection(aFedId,iChId);
325  addBadAPV(lConnection,iAPVid,lFlag,aCounter);
326 
327  }//ifAPVhists
328  else {
329  const FedChannelConnection & lConnection = cabling_->fedConnection(aFedId,iChId);
330  addBadAPV(lConnection,0,lFlag,aCounter);
331  }
332  }//if not FE hist
333  }//if not FED hist
334  }//if entries in histo
335  }//loop on bins
336 }//method readHistogram
337 
339  const unsigned short aAPVNumber,
340  const unsigned short aFlag,
341  unsigned int & aCounter)
342 {
343  if (!aConnection.isConnected()) {
344  edm::LogWarning("SiStripFEDErrorsDQM") << "[SiStripFEDErrorsDQM::addBadAPV] Warning, incompatible cabling ! Channel is not connected, but entry found in histo ... " << std::endl;
345  return;
346  }
347  unsigned int lDetid = aConnection.detId();
348  if (!lDetid || lDetid == sistrip::invalid32_) {
349  edm::LogWarning("SiStripFEDErrorsDQM") << "[SiStripFEDErrorsDQM::addBadAPV] Warning, DetId is invalid: " << lDetid << std::endl;
350  return;
351  }
352  //unsigned short nChInModule = aConnection.nApvPairs();
353  unsigned short lApvNum = 0;
354  if (aAPVNumber < 2) {
355  lApvNum = 2*aConnection.apvPairNumber();
356  addBadStrips(aConnection,lDetid,lApvNum,aFlag,aCounter);
357  }
358  if (aAPVNumber == 0 || aAPVNumber == 2) {
359  lApvNum = 2*aConnection.apvPairNumber()+1;
360  addBadStrips(aConnection,lDetid,lApvNum,aFlag,aCounter);
361  }
362 }
363 
364 
366  const unsigned int aDetId,
367  const unsigned short aApvNum,
368  const unsigned short aFlag,
369  unsigned int & aCounter)
370 {
371  // std::vector<unsigned int> lStripVector;
372  unsigned int lBadStripRange;
373  unsigned short lFirstBadStrip=aApvNum*128;
374  unsigned short lConsecutiveBadStrips=128;
375 
376  lBadStripRange = obj_->encode(lFirstBadStrip,lConsecutiveBadStrips,aFlag);
377 
378  LogTrace("SiStripFEDErrorsDQM") << "[SiStripFEDErrorsDQM::addBadStrips] ---- Adding : detid " << aDetId
379  << " (FED " << aConnection.fedId()
380  << ", Ch " << aConnection.fedCh () << ")"
381  << ", APV " << aApvNum
382  << ", flag " << aFlag
383  << std::endl;
384 
385  detIdErrors_[aDetId].push_back(lBadStripRange);
386 
387  // lStripVector.push_back(lBadStripRange);
388  // SiStripBadStrip::Range lRange(lStripVector.begin(),lStripVector.end());
389  // if ( !obj_->put(aDetId,lRange) ) {
390  // edm::LogError("SiStripFEDErrorsDQM")<<"[SiStripFEDErrorsDQM::addBadStrips] detid already exists." << std::endl;
391  // }
392 
393  aCounter++;
394 }
395 
397 {
398  for( std::map<uint32_t, std::vector<uint32_t> >::const_iterator it = detIdErrors_.begin(); it != detIdErrors_.end(); ++it )
399  {
400 
401  std::vector<uint32_t> lList = it->second;
402 
403  //map of first strip number and flag
404  //purpose is to encode all existing flags into a unique one...
405  std::map<unsigned short,unsigned short> lAPVMap;
406  lAPVMap.clear();
407 
408  for (uint32_t iCh(0); iCh<lList.size(); iCh++) {
409  SiStripBadStrip::data lData = obj_->decode(lList.at(iCh));
410  unsigned short lFlag = 0;
411  setFlagBit(lFlag,lData.flag);
412 
413  //std::cout << " -- Detid " << it->first << ", strip " << lData.firstStrip << ", flag " << lData.flag << std::endl;
414 
415  std::pair<std::map<unsigned short,unsigned short>::iterator,bool> lInsert = lAPVMap.insert(std::pair<unsigned short,unsigned short>(lData.firstStrip,lFlag));
416  if (!lInsert.second) {
417  //std::cout << " ---- Adding bit : " << lData.flag << " to " << lInsert.first->second << ": ";
418  setFlagBit(lInsert.first->second,lData.flag);
419  //std::cout << lInsert.first->second << std::endl;
420  }
421  }
422 
423  //encode the new flag
424  std::vector<unsigned int> lStripVector;
425  unsigned short lConsecutiveBadStrips=128;
426 
427  for (std::map<unsigned short,unsigned short>::iterator lIter = lAPVMap.begin();
428  lIter != lAPVMap.end();
429  lIter++)
430  {
431  lStripVector.push_back(obj_->encode(lIter->first,lConsecutiveBadStrips,lIter->second));
432  }
433 
434  SiStripBadStrip::Range lRange(lStripVector.begin(),lStripVector.end());
435  if ( !obj_->put(it->first,lRange) ) {
436  edm::LogError("SiStripFEDErrorsDQM")<<"[SiStripFEDErrorsDQM::addBadStrips] detid already exists." << std::endl;
437  }
438  }
439 }
440 
441 void SiStripFEDErrorsDQM::setFlagBit(unsigned short & aFlag, const unsigned short aBit)
442 {
443 
444  aFlag = aFlag | (0x1 << aBit) ;
445 
446 
447 }
448 
449 
450 
451 
452 //define this as a plug-in
453 // DEFINE_FWK_MODULE(SiStripFEDErrorsDQM);
454 
455 
const std::string & getName(void) const
get name of ME
std::map< uint32_t, std::vector< unsigned int > > detIdErrors_
static const uint16_t FED_ID_MIN
FedChannelConnection fedConnection(uint16_t fed_id, uint16_t fed_ch) const
const uint16_t & fedCh() const
static const uint32_t invalid32_
Definition: Constants.h:15
uint16_t apvPairNumber() const
void addErrors()
Writes the errors to the db.
bool goToDir(const std::string &name)
Uses DQM utilities to access the requested dir.
uint32_t getRunNumber() const
Returns the run number from the cfg.
const uint16_t & fedId() const
void addBadAPV(const FedChannelConnection &aConnection, const unsigned short aAPVNumber, const unsigned short aFlag, unsigned int &aCounter)
SiStripFEDErrorsDQM(const edm::ParameterSet &iConfig)
void setFlagBit(unsigned short &aFlag, const unsigned short aBit)
void appendSinceTime(T *payloadObj, cond::Time_t sinceTime, const std::string &recordName, bool withlogging=false)
int iEvent
Definition: GenABIO.cc:230
bool isNewTagRequest(const std::string &recordName)
const uint32_t & detId() const
Class containning control, module, detector and connection information, at the level of a FED channel...
FedsConstIterRange fedIds() const
bool isAvailable() const
Definition: Service.h:46
#define LogTrace(id)
void createNewIOV(T *firstPayloadObj, cond::Time_t firstSinceTime, cond::Time_t firstTillTime, const std::string &recordName, bool withlogging=false)
virtual void analyze(const edm::Event &, const edm::EventSetup &)
unsigned short firstStrip
static const uint16_t FEDCH_PER_FEUNIT
const T & get() const
Definition: EventSetup.h:56
T const * product() const
Definition: ESHandle.h:86
ConnsConstIterRange fedConnections(uint16_t fed_id) const
double getBinContent(int binx) const
get content of bin (1-D)
static const uint16_t FEDCH_PER_FED
std::pair< ContainerIterator, ContainerIterator > Range
int getNbinsX(void) const
get # of bins in X-axis
const SiStripFedCabling * cabling_
static const uint16_t FED_ID_MAX
std::string fullPath() const
Definition: FileInPath.cc:184
bool put(const uint32_t &detID, const InputVector &vect)
unsigned int encode(const unsigned short &first, const unsigned short &NconsecutiveBadStrips, const unsigned short &flag=0)
void updateCabling(const edm::EventSetup &eventSetup)
void readHistogram(MonitorElement *aMe, unsigned int &aCounter, const float aNorm, const unsigned int aFedId)
void openRequestedFile()
Uses DQMStore to access the DQM file.
data decode(const unsigned int &value) const
void addBadStrips(const FedChannelConnection &aConnection, const unsigned int aDetId, const unsigned short aApvNum, const unsigned short aFlag, unsigned int &aCounter)