CMS 3D CMS Logo

SiPixelDetectorStatus.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <fstream>
3 #include <map>
4 #include <cmath>
5 #include <vector>
6 
7 #include <TString.h>
8 #include <TMath.h>
9 #include <TH1.h>
10 
12 
13 // ----------------------------------------------------------------------
14 SiPixelDetectorStatus::SiPixelDetectorStatus() : fLS0_(99999999), fLS1_(0), fRun0_(99999999), fRun1_(0) {
15  fDetHits_ = 0;
16  ftotalevents_ = 0;
17 }
18 
19 // ----------------------------------------------------------------------
21 
22 // ----------------------------------------------------------------------
24  std::ifstream INS;
25  std::string sline;
26  INS.open(filename.c_str());
27 
28  int oldDetId(-1);
29  int detid(0), roc(0), hits(0), nroc(0);
30  SiPixelModuleStatus* pMod(nullptr);
31  bool readOK(false);
32  while (std::getline(INS, sline)) {
33  if (std::string::npos != sline.find("# SiPixelDetectorStatus START")) {
34  readOK = true;
35  continue;
36  }
37  if (!readOK)
38  continue;
39 
40  if (std::string::npos != sline.find("# SiPixelDetectorStatus END")) {
41  pMod->setNrocs(nroc + 1);
42  break;
43  }
44 
45  if (sline.find("# SiPixelDetectorStatus for LS") != std::string::npos) {
46  std::sscanf(sline.c_str(), "# SiPixelDetectorStatus for LS %d .. %d", &fLS0_, &fLS1_);
47  continue;
48  }
49  if (sline.find("# SiPixelDetectorStatus for run") != std::string::npos) {
50  std::sscanf(sline.c_str(), "# SiPixelDetectorStatus for run %d .. %d", &fRun0_, &fRun1_);
51  continue;
52  }
53  if (sline.find("# SiPixelDetectorStatus total hits = ") != std::string::npos) {
54  std::sscanf(sline.c_str(), "# SiPixelDetectorStatus total hits = %ld", &fDetHits_);
55  continue;
56  }
57 
58  std::sscanf(sline.c_str(), "%d %d %d", &detid, &roc, &hits);
59  if (roc > nroc)
60  nroc = roc;
61  if (detid != oldDetId) {
62  if (pMod) {
63  pMod->setNrocs(nroc + 1); // roc ranges from 0, "+1" to get number of rocs per module
64  }
65 
66  oldDetId = detid;
67  if (getModule(detid) == nullptr) {
68  addModule(detid, nroc + 1); // roc ranges from 0, "+1" to get number of rocs per module
69  }
70 
71  pMod = getModule(detid);
72  nroc = 0;
73  }
74  // for existing module, update its content
75  if (pMod != nullptr) {
76  fDetHits_ += hits;
77  pMod->updateModuleDIGI(roc, hits);
78  }
79  }
80 
81  INS.close();
82 }
83 
84 // ----------------------------------------------------------------------
85 void SiPixelDetectorStatus::dumpToFile(std::ofstream& OD) {
86  OD << "# SiPixelDetectorStatus START" << std::endl;
87  OD << "# SiPixelDetectorStatus for LS " << fLS0_ << " .. " << fLS1_ << std::endl;
88  OD << "# SiPixelDetectorStatus for run " << fRun0_ << " .. " << fRun1_ << std::endl;
89  OD << "# SiPixelDetectorStatus total hits = " << fDetHits_ << std::endl;
90 
91  for (std::map<int, SiPixelModuleStatus>::iterator it = SiPixelDetectorStatus::begin();
93  ++it) {
94  for (int iroc = 0; iroc < it->second.nrocs(); ++iroc) {
95  OD << Form("%10d %2d %3d", it->first, iroc, int(it->second.getRoc(iroc)->digiOccROC())) << std::endl;
96  }
97  }
98  OD << "# SiPixelDetectorStatus END" << std::endl;
99 }
100 
101 // ----------------------------------------------------------------------
103  // only need to add NEW modules
104  if (fModules_.find(detid) == fModules_.end()) {
105  SiPixelModuleStatus a(detid, nrocs);
106  fModules_.insert(std::make_pair(detid, a));
107  }
108 }
109 
110 // ----------------------------------------------------------------------
111 void SiPixelDetectorStatus::addModule(int detid, SiPixelModuleStatus a) { fModules_.insert(std::make_pair(detid, a)); }
112 
113 // ----------------------------------------------------------------------
114 void SiPixelDetectorStatus::fillDIGI(int detid, int roc) {
115  ++fDetHits_;
116  fModules_[detid].fillDIGI(roc);
117 }
118 
119 // ----------------------------------------------------------------------
121  if (fModules_.find(detid) != fModules_.end()) {
122  fModules_[detid].fillFEDerror25(ch);
123  }
124 }
125 
126 // FEDerror25 effected ROCs in for each module
127 std::map<int, std::vector<int>> SiPixelDetectorStatus::getFEDerror25Rocs() {
128  std::map<int, std::vector<int>> badRocLists;
129 
130  for (std::map<int, SiPixelModuleStatus>::iterator itMod = SiPixelDetectorStatus::begin();
131  itMod != SiPixelDetectorStatus::end();
132  ++itMod) {
133  int detid = itMod->first;
134  // FEDerror25 effected ROCs in a given module
135  std::vector<int> list;
136  SiPixelModuleStatus modStatus = itMod->second;
137  for (int iroc = 0; iroc < modStatus.nrocs(); ++iroc) {
138  SiPixelRocStatus* roc = modStatus.getRoc(iroc);
139  if (roc->isFEDerror25()) {
140  list.push_back(iroc);
141  }
142  badRocLists[detid] = list;
143  }
144  }
145 
146  return badRocLists;
147 }
148 
149 // ----------------------------------------------------------------------
150 std::map<int, SiPixelModuleStatus>::iterator SiPixelDetectorStatus::begin() { return fModules_.begin(); }
151 
152 // ----------------------------------------------------------------------
153 std::map<int, SiPixelModuleStatus>::iterator SiPixelDetectorStatus::end() { return fModules_.end(); }
154 
155 // ----------------------------------------------------------------------
156 int SiPixelDetectorStatus::nmodules() { return static_cast<int>(fModules_.size()); }
157 
158 // ----------------------------------------------------------------------
160  if (fModules_.find(detid) == fModules_.end()) {
161  return nullptr;
162  }
163  return &(fModules_[detid]);
164 }
165 
167  if (fModules_.find(detid) == fModules_.end())
168  return false;
169  else
170  return true;
171 }
172 
173 // ----------------------------------------------------------------------
175  unsigned long int ave(0);
176  int nrocs(0);
177  for (std::map<int, SiPixelModuleStatus>::iterator it = SiPixelDetectorStatus::begin();
179  ++it) {
180  unsigned long int inc = it->second.digiOccMOD();
181  ave += inc;
182  nrocs += it->second.nrocs();
183  }
184  return (1.0 * ave) / nrocs;
185 }
186 
188  double fDetAverage = SiPixelDetectorStatus::perRocDigiOcc();
189 
190  double sig = 0.0;
191  int nrocs(0);
192  for (std::map<int, SiPixelModuleStatus>::iterator it = SiPixelDetectorStatus::begin();
194  ++it) {
195  unsigned long int inc = it->second.digiOccMOD();
196  sig += (fDetAverage - inc) * (fDetAverage - inc);
197  nrocs += it->second.nrocs();
198  }
199 
200  double fDetSigma = sig / (nrocs - 1);
201  return TMath::Sqrt(fDetSigma);
202 }
203 
204 /*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/
205 
206 // combine status from different data (coming from different run/lumi)
208  // loop over new data status
209  for (std::map<int, SiPixelModuleStatus>::iterator it = newData.begin(); it != newData.end(); ++it) {
210  int detid = it->first;
211 
212  if (fModules_.find(detid) != fModules_.end()) { // if the detid is in the module lists
213  fModules_[detid].updateModuleStatus(*(newData.getModule(detid)));
214  } else { // if new module, add(insert) the module data
215  fModules_.insert(std::make_pair(detid, *(newData.getModule(detid))));
216  }
217  }
218 
219  fDetHits_ = fDetHits_ + newData.digiOccDET();
220  ftotalevents_ = ftotalevents_ + newData.getNevents();
221 }
222 
224  fModules_.clear();
225  fDetHits_ = 0;
226  ftotalevents_ = 0;
227  fRun0_ = 99999999;
228  fRun1_ = 0;
229  fLS0_ = 99999999;
230  fLS1_ = 0;
231 }
void addModule(int detid, int nrocs)
void updateDetectorStatus(SiPixelDetectorStatus newData)
void updateModuleDIGI(int roc, unsigned int nhits)
combine new data to update(topup) module status
void dumpToFile(std::ofstream &outFile)
unsigned long int digiOccDET()
void fillFEDerror25(int detid, PixelFEDChannel ch)
std::map< int, std::vector< int > > getFEDerror25Rocs()
void fillDIGI(int detid, int roc)
unsigned long int fDetHits_
unsigned long int ftotalevents_
void readFromFile(std::string filename)
std::map< int, SiPixelModuleStatus >::iterator end()
std::map< int, SiPixelModuleStatus >::iterator begin()
std::map< int, SiPixelModuleStatus > fModules_
double a
Definition: hdecay.h:119
unsigned long int getNevents()
SiPixelRocStatus * getRoc(int i)
get a ROC
SiPixelModuleStatus * getModule(int detid)