Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:14:33

0001 #include <iostream>
0002 #include <fstream>
0003 #include <map>
0004 #include <cmath>
0005 #include <vector>
0006 
0007 #include <TString.h>
0008 #include <TMath.h>
0009 #include <TH1.h>
0010 
0011 #include "CalibTracker/SiPixelQuality/interface/SiPixelDetectorStatus.h"
0012 
0013 // ----------------------------------------------------------------------
0014 SiPixelDetectorStatus::SiPixelDetectorStatus() : fLS0_(99999999), fLS1_(0), fRun0_(99999999), fRun1_(0) {
0015   fDetHits_ = 0;
0016   ftotalevents_ = 0;
0017 }
0018 
0019 // ----------------------------------------------------------------------
0020 SiPixelDetectorStatus::~SiPixelDetectorStatus() {}
0021 
0022 // ----------------------------------------------------------------------
0023 void SiPixelDetectorStatus::readFromFile(std::string filename) {
0024   std::ifstream INS;
0025   std::string sline;
0026   INS.open(filename.c_str());
0027 
0028   int oldDetId(-1);
0029   int detid(0), roc(0), hits(0), nroc(0);
0030   SiPixelModuleStatus* pMod(nullptr);
0031   bool readOK(false);
0032   while (std::getline(INS, sline)) {
0033     if (std::string::npos != sline.find("# SiPixelDetectorStatus START")) {
0034       readOK = true;
0035       continue;
0036     }
0037     if (!readOK)
0038       continue;
0039 
0040     if (std::string::npos != sline.find("# SiPixelDetectorStatus END")) {
0041       pMod->setNrocs(nroc + 1);
0042       break;
0043     }
0044 
0045     if (sline.find("# SiPixelDetectorStatus for LS") != std::string::npos) {
0046       std::sscanf(sline.c_str(), "# SiPixelDetectorStatus for LS  %d .. %d", &fLS0_, &fLS1_);
0047       continue;
0048     }
0049     if (sline.find("# SiPixelDetectorStatus for run") != std::string::npos) {
0050       std::sscanf(sline.c_str(), "# SiPixelDetectorStatus for run %d .. %d", &fRun0_, &fRun1_);
0051       continue;
0052     }
0053     if (sline.find("# SiPixelDetectorStatus total hits = ") != std::string::npos) {
0054       std::sscanf(sline.c_str(), "# SiPixelDetectorStatus total hits = %ld", &fDetHits_);
0055       continue;
0056     }
0057 
0058     std::sscanf(sline.c_str(), "%d %d %d", &detid, &roc, &hits);
0059     if (roc > nroc)
0060       nroc = roc;
0061     if (detid != oldDetId) {
0062       if (pMod) {
0063         pMod->setNrocs(nroc + 1);  // roc ranges from 0, "+1" to get number of rocs per module
0064       }
0065 
0066       oldDetId = detid;
0067       if (getModule(detid) == nullptr) {
0068         addModule(detid, nroc + 1);  // roc ranges from 0, "+1" to get number of rocs per module
0069       }
0070 
0071       pMod = getModule(detid);
0072       nroc = 0;
0073     }
0074     // for existing module, update its content
0075     if (pMod != nullptr) {
0076       fDetHits_ += hits;
0077       pMod->updateModuleDIGI(roc, hits);
0078     }
0079   }
0080 
0081   INS.close();
0082 }
0083 
0084 // ----------------------------------------------------------------------
0085 void SiPixelDetectorStatus::dumpToFile(std::ofstream& OD) {
0086   OD << "# SiPixelDetectorStatus START" << std::endl;
0087   OD << "# SiPixelDetectorStatus for LS  " << fLS0_ << " .. " << fLS1_ << std::endl;
0088   OD << "# SiPixelDetectorStatus for run " << fRun0_ << " .. " << fRun1_ << std::endl;
0089   OD << "# SiPixelDetectorStatus total hits = " << fDetHits_ << std::endl;
0090 
0091   for (std::map<int, SiPixelModuleStatus>::iterator it = SiPixelDetectorStatus::begin();
0092        it != SiPixelDetectorStatus::end();
0093        ++it) {
0094     for (int iroc = 0; iroc < it->second.nrocs(); ++iroc) {
0095       OD << Form("%10d %2d %3d", it->first, iroc, int(it->second.getRoc(iroc)->digiOccROC())) << std::endl;
0096     }
0097   }
0098   OD << "# SiPixelDetectorStatus END" << std::endl;
0099 }
0100 
0101 // ----------------------------------------------------------------------
0102 void SiPixelDetectorStatus::addModule(int detid, int nrocs) {
0103   // only need to add NEW modules
0104   if (fModules_.find(detid) == fModules_.end()) {
0105     SiPixelModuleStatus a(detid, nrocs);
0106     fModules_.insert(std::make_pair(detid, a));
0107   }
0108 }
0109 
0110 // ----------------------------------------------------------------------
0111 void SiPixelDetectorStatus::addModule(int detid, SiPixelModuleStatus a) { fModules_.insert(std::make_pair(detid, a)); }
0112 
0113 // ----------------------------------------------------------------------
0114 void SiPixelDetectorStatus::fillDIGI(int detid, int roc) {
0115   ++fDetHits_;
0116   fModules_[detid].fillDIGI(roc);
0117 }
0118 
0119 // ----------------------------------------------------------------------
0120 void SiPixelDetectorStatus::fillFEDerror25(int detid, PixelFEDChannel ch) {
0121   if (fModules_.find(detid) != fModules_.end()) {
0122     fModules_[detid].fillFEDerror25(ch);
0123   }
0124 }
0125 
0126 // FEDerror25 effected ROCs in for each module
0127 std::map<int, std::vector<int>> SiPixelDetectorStatus::getFEDerror25Rocs() {
0128   std::map<int, std::vector<int>> badRocLists;
0129 
0130   for (std::map<int, SiPixelModuleStatus>::iterator itMod = SiPixelDetectorStatus::begin();
0131        itMod != SiPixelDetectorStatus::end();
0132        ++itMod) {
0133     int detid = itMod->first;
0134     // FEDerror25 effected ROCs in a given module
0135     std::vector<int> list;
0136     SiPixelModuleStatus modStatus = itMod->second;
0137     for (int iroc = 0; iroc < modStatus.nrocs(); ++iroc) {
0138       SiPixelRocStatus* roc = modStatus.getRoc(iroc);
0139       if (roc->isFEDerror25()) {
0140         list.push_back(iroc);
0141       }
0142       badRocLists[detid] = list;
0143     }
0144   }
0145 
0146   return badRocLists;
0147 }
0148 
0149 // ----------------------------------------------------------------------
0150 std::map<int, SiPixelModuleStatus>::iterator SiPixelDetectorStatus::begin() { return fModules_.begin(); }
0151 
0152 // ----------------------------------------------------------------------
0153 std::map<int, SiPixelModuleStatus>::iterator SiPixelDetectorStatus::end() { return fModules_.end(); }
0154 
0155 // ----------------------------------------------------------------------
0156 int SiPixelDetectorStatus::nmodules() { return static_cast<int>(fModules_.size()); }
0157 
0158 // ----------------------------------------------------------------------
0159 SiPixelModuleStatus* SiPixelDetectorStatus::getModule(int detid) {
0160   if (fModules_.find(detid) == fModules_.end()) {
0161     return nullptr;
0162   }
0163   return &(fModules_[detid]);
0164 }
0165 
0166 bool SiPixelDetectorStatus::findModule(int detid) {
0167   if (fModules_.find(detid) == fModules_.end())
0168     return false;
0169   else
0170     return true;
0171 }
0172 
0173 // ----------------------------------------------------------------------
0174 double SiPixelDetectorStatus::perRocDigiOcc() {
0175   unsigned long int ave(0);
0176   int nrocs(0);
0177   for (std::map<int, SiPixelModuleStatus>::iterator it = SiPixelDetectorStatus::begin();
0178        it != SiPixelDetectorStatus::end();
0179        ++it) {
0180     unsigned long int inc = it->second.digiOccMOD();
0181     ave += inc;
0182     nrocs += it->second.nrocs();
0183   }
0184   return (1.0 * ave) / nrocs;
0185 }
0186 
0187 double SiPixelDetectorStatus::perRocDigiOccVar() {
0188   double fDetAverage = SiPixelDetectorStatus::perRocDigiOcc();
0189 
0190   double sig = 0.0;
0191   int nrocs(0);
0192   for (std::map<int, SiPixelModuleStatus>::iterator it = SiPixelDetectorStatus::begin();
0193        it != SiPixelDetectorStatus::end();
0194        ++it) {
0195     unsigned long int inc = it->second.digiOccMOD();
0196     sig += (fDetAverage - inc) * (fDetAverage - inc);
0197     nrocs += it->second.nrocs();
0198   }
0199 
0200   double fDetSigma = sig / (nrocs - 1);
0201   return TMath::Sqrt(fDetSigma);
0202 }
0203 
0204 /*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/
0205 
0206 // combine status from different data (coming from different run/lumi)
0207 void SiPixelDetectorStatus::updateDetectorStatus(SiPixelDetectorStatus newData) {
0208   // loop over new data status
0209   for (std::map<int, SiPixelModuleStatus>::iterator it = newData.begin(); it != newData.end(); ++it) {
0210     int detid = it->first;
0211 
0212     if (fModules_.find(detid) != fModules_.end()) {  // if the detid is in the module lists
0213       fModules_[detid].updateModuleStatus(*(newData.getModule(detid)));
0214     } else {  // if new module, add(insert) the module data
0215       fModules_.insert(std::make_pair(detid, *(newData.getModule(detid))));
0216     }
0217   }
0218 
0219   fDetHits_ = fDetHits_ + newData.digiOccDET();
0220   ftotalevents_ = ftotalevents_ + newData.getNevents();
0221 }
0222 
0223 void SiPixelDetectorStatus::resetDetectorStatus() {
0224   fModules_.clear();
0225   fDetHits_ = 0;
0226   ftotalevents_ = 0;
0227   fRun0_ = 99999999;
0228   fRun1_ = 0;
0229   fLS0_ = 99999999;
0230   fLS1_ = 0;
0231 }