CMS 3D CMS Logo

SiPixelStatusManager.cc
Go to the documentation of this file.
1 
11 #include <vector>
12 #include <cmath>
13 #include <climits>
14 
15 #include <iostream>
16 
17 using namespace edm;
18 using namespace std;
19 
20 //--------------------------------------------------------------------------------------------------
22 
23 //--------------------------------------------------------------------------------------------------
25  : outputBase_(iConfig.getParameter<ParameterSet>("SiPixelStatusManagerParameters")
26  .getUntrackedParameter<std::string>("outputBase")),
27  aveDigiOcc_(iConfig.getParameter<edm::ParameterSet>("SiPixelStatusManagerParameters")
28  .getUntrackedParameter<int>("aveDigiOcc")),
29  nLumi_(iConfig.getParameter<edm::ParameterSet>("SiPixelStatusManagerParameters")
30  .getUntrackedParameter<int>("resetEveryNLumi")),
31  moduleName_(iConfig.getParameter<ParameterSet>("SiPixelStatusManagerParameters")
32  .getUntrackedParameter<std::string>("moduleName")),
33  label_(iConfig.getParameter<ParameterSet>("SiPixelStatusManagerParameters")
34  .getUntrackedParameter<std::string>("label")) {
35  edm::InputTag siPixelStatusTag_(moduleName_, label_);
36  siPixelStatusToken_ = iC.consumes<SiPixelDetectorStatus, edm::InLumi>(siPixelStatusTag_);
37 
38  LogInfo("SiPixelStatusManager") << "Output base: " << outputBase_ << std::endl;
39  reset();
40 }
41 
42 //--------------------------------------------------------------------------------------------------
44 
45 //--------------------------------------------------------------------------------------------------
47  siPixelStatusMap_.clear();
48  siPixelStatusVtr_.clear();
49 }
50 
51 //--------------------------------------------------------------------------------------------------
53  return (status1.getLSRange().first < status2.getLSRange().first);
54 }
55 
57  //only create std::map payloads when the number of non-zero DIGI lumi sections is greater than ZERO otherwise segmentation fault
58  if (!siPixelStatusVtr_.empty()) {
59  // sort the vector according to lumi
61 
62  // create FEDerror25 ROCs and bad ROCs from PCL
65 
66  // realse the cost of siPixelStatusVtr_ since it is not needed anymore
67  siPixelStatusVtr_.clear();
68  }
69 }
70 
71 //--------------------------------------------------------------------------------------------------
73  edm::Handle<SiPixelDetectorStatus> siPixelStatusHandle;
74  iLumi.getByToken(siPixelStatusToken_, siPixelStatusHandle);
75 
76  if (siPixelStatusHandle.isValid()) { // check the product
77  SiPixelDetectorStatus tmpStatus = (*siPixelStatusHandle);
78  if (tmpStatus.digiOccDET() > 0) { // only put in SiPixelDetectorStatus with non zero digi (pixel hit)
79  siPixelStatusVtr_.push_back(tmpStatus);
80  }
81  } else {
82  edm::LogWarning("SiPixelStatusManager") << " SiPixelDetectorStatus is not valid for run " << iLumi.run() << " lumi "
83  << iLumi.luminosityBlock() << std::endl;
84  }
85 }
86 
87 //--------------------------------------------------------------------------------------------------
89  siPixelStatusVtr_iterator firstStatus = siPixelStatusVtr_.begin();
91 
92  siPixelStatusMap_.clear();
93 
94  // doesn't work for nLumi_=1 cos any integer can be completely divided by 1
95  if (outputBase_ == "nLumibased" && nLumi_ > 1) {
96  // if the total number of Lumi Blocks can't be completely divided by nLumi_,
97  // the residual Lumi Blocks will be as the last IOV
98  int iterationLumi = 0;
99 
100  LuminosityBlockNumber_t tmpLumi;
101  SiPixelDetectorStatus tmpSiPixelStatus;
102  for (siPixelStatusVtr_iterator it = firstStatus; it != lastStatus; it++) {
103  // this is the begining of an IOV
104  if (iterationLumi % nLumi_ == 0) {
105  tmpLumi = edm::LuminosityBlockNumber_t(it->getLSRange().first);
106  tmpSiPixelStatus = (*it);
107  }
108 
109  // keep update detector status up to nLumi_ lumi sections
110  if (iterationLumi % nLumi_ > 0) {
111  tmpSiPixelStatus.updateDetectorStatus((*it));
112  tmpSiPixelStatus.setLSRange(int(tmpLumi), (*it).getLSRange().second);
113  }
114 
115  siPixelStatusVtr_iterator currentIt = it;
116  siPixelStatusVtr_iterator nextIt = std::next(currentIt);
117  // wirte out if current lumi is the last lumi-section in the IOV
118  if (iterationLumi % nLumi_ == nLumi_ - 1 || nextIt == lastStatus) {
119  // fill it into a new map (with IOV structured)
120  siPixelStatusMap_[tmpLumi] = tmpSiPixelStatus;
121  }
122 
123  iterationLumi = iterationLumi + 1;
124  }
125 
126  // check whether there is not enough number of Lumi in the last IOV
127  // (only when siPixelStatusVtr_.size() > nLumi_ or equivalently current siPixelStatusMap_.size()>1
128  // (otherwise there will be only one IOV, and not previous IOV before the last IOV)
129  // and the number of lumi can not be completely divided by the nLumi_.
130  // (then the number of lumis in the last IOV is equal to the residual, which is less than nLumi_)
131  // if it is, combine last IOV with the IOV before it
132  if (siPixelStatusVtr_.size() % nLumi_ != 0 && siPixelStatusMap_.size() > 1) {
133  // start from the iterator of the end of std::map
135  // the last IOV
136  siPixelStatusMap_iterator iterLastIOV = std::prev(iterEnd);
137  // the IOV before the last IOV
138  siPixelStatusMap_iterator iterBeforeLastIOV = std::prev(iterLastIOV);
139 
140  // combine the last IOV data to the IOV before the last IOV
141  (iterBeforeLastIOV->second).updateDetectorStatus(iterLastIOV->second);
142  (iterBeforeLastIOV->second)
143  .setLSRange((iterBeforeLastIOV->second).getLSRange().first, (iterLastIOV->second).getLSRange().second);
144 
145  // delete the last IOV, so the IOV before the last IOV becomes the new last IOV
146  siPixelStatusMap_.erase(iterLastIOV);
147  }
148 
149  } else if (outputBase_ == "dynamicLumibased") {
150  double aveDigiOcc = 1.0 * aveDigiOcc_;
151 
153  SiPixelDetectorStatus tmpSiPixelStatus;
154  bool isNewIOV = true;
155 
156  for (siPixelStatusVtr_iterator it = firstStatus; it != lastStatus; it++) {
157  if (isNewIOV) { // if it is new IOV, init with the current data
158  tmpLumi = edm::LuminosityBlockNumber_t(it->getLSRange().first);
159  tmpSiPixelStatus = (*it);
160  } else { // if it is not new IOV, append current data
161  tmpSiPixelStatus.updateDetectorStatus((*it));
162  tmpSiPixelStatus.setLSRange(int(tmpLumi), (*it).getLSRange().second);
163  }
164 
165  // if reaching the end of data, write the last IOV to the map whatsoevec
166  siPixelStatusVtr_iterator currentIt = it;
167  siPixelStatusVtr_iterator nextIt = std::next(currentIt);
168  if (tmpSiPixelStatus.perRocDigiOcc() < aveDigiOcc && nextIt != lastStatus) {
169  isNewIOV = false; // if digi occ is not enough, next data will not belong to new IOV
170  } else { // if (accunumated) digi occ is enough, write the data to the map
171  isNewIOV = true;
172  siPixelStatusMap_[tmpLumi] = tmpSiPixelStatus;
173  // so next loop is the begining of a new IOV
174  }
175 
176  } // end of siPixelStatusMap
177 
178  // check whether last IOV has enough statistics
179  // (ONLY when there are more than oneIOV(otherwise there is NO previous IOV before the last IOV) )
180  // if not, combine with previous IOV
181  if (siPixelStatusMap_.size() > 1) {
182  // start from the end iterator of the std::map
184  // the last IOV
185  siPixelStatusMap_iterator iterLastIOV = std::prev(iterEnd);
186  // if the statistics of the last IOV is not enough
187  if ((iterLastIOV->second).perRocDigiOcc() < aveDigiOcc) {
188  // the IOV before the last IOV of the map
189  siPixelStatusMap_iterator iterBeforeLastIOV = std::prev(iterLastIOV);
190  // combine the last IOV data to the IOV before the last IOV
191  (iterBeforeLastIOV->second).updateDetectorStatus(iterLastIOV->second);
192  (iterBeforeLastIOV->second)
193  .setLSRange((iterBeforeLastIOV->second).getLSRange().first, (iterLastIOV->second).getLSRange().second);
194  // erase the last IOV, so the IOV before the last IOV becomes the new last IOV
195  siPixelStatusMap_.erase(iterLastIOV);
196  }
197  }
198 
199  } else if (outputBase_ == "runbased" || ((int(siPixelStatusVtr_.size()) <= nLumi_ && outputBase_ == "nLumibased"))) {
200  edm::LuminosityBlockNumber_t tmpLumi = edm::LuminosityBlockNumber_t(firstStatus->getLSRange().first);
201  SiPixelDetectorStatus tmpSiPixelStatus = (*firstStatus);
202 
203  siPixelStatusVtr_iterator nextStatus = ++siPixelStatusVtr_.begin();
204  for (siPixelStatusVtr_iterator it = nextStatus; it != lastStatus; it++) {
205  tmpSiPixelStatus.updateDetectorStatus((*it));
206  tmpSiPixelStatus.setLSRange(int(tmpLumi), (*it).getLSRange().second);
207  }
208 
209  siPixelStatusMap_[tmpLumi] = tmpSiPixelStatus;
210 
211  } else {
212  LogInfo("SiPixelStatusManager") << "Unrecognized payload outputBase parameter: " << outputBase_ << endl;
213  }
214 }
215 
217  // initialize the first IOV and SiPixelDetector status (in the first IOV)
218  siPixelStatusVtr_iterator firstStatus = siPixelStatusVtr_.begin();
219  edm::LuminosityBlockNumber_t firstLumi = edm::LuminosityBlockNumber_t(firstStatus->getLSRange().first);
220  SiPixelDetectorStatus firstFEDerror25 = (*firstStatus);
221  FEDerror25Map_[firstLumi] = firstFEDerror25.getFEDerror25Rocs();
222 
223  siPixelStatusVtr_iterator lastStatus = siPixelStatusVtr_.end();
224 
226  bool sameAsLastIOV = true;
227  edm::LuminosityBlockNumber_t previousLumi = firstLumi;
228 
229  siPixelStatusVtr_iterator secondStatus = std::next(siPixelStatusVtr_.begin());
230  for (siPixelStatusVtr_iterator it = secondStatus; it != lastStatus; it++) {
231  // init for each lumi section (iterator)
232  edm::LuminosityBlockNumber_t tmpLumi = edm::LuminosityBlockNumber_t(it->getLSRange().first);
233  SiPixelDetectorStatus tmpFEDerror25 = (*it);
234 
235  std::map<int, std::vector<int> > tmpBadRocLists = tmpFEDerror25.getFEDerror25Rocs();
236 
237  std::map<int, SiPixelModuleStatus>::iterator itModEnd = tmpFEDerror25.end();
238  for (std::map<int, SiPixelModuleStatus>::iterator itMod = tmpFEDerror25.begin(); itMod != itModEnd; ++itMod) {
239  int detid = itMod->first;
240  // if the badroc list differs for any detid, update the payload
241  if (tmpBadRocLists[detid] != (FEDerror25Map_[previousLumi])[detid]) {
242  sameAsLastIOV = false;
243  break; // jump out of the loop once a new payload is found
244  }
245  }
246 
247  if (sameAsLastIOV == false) {
248  //only write new IOV when this Lumi's FEDerror25 ROC list is not equal to the previous one
249  FEDerror25Map_[tmpLumi] = tmpBadRocLists;
250  // and reset
251  previousLumi = tmpLumi;
252  sameAsLastIOV = true;
253  }
254  }
255 }
void updateDetectorStatus(SiPixelDetectorStatus newData)
unsigned long int digiOccDET()
edm::EDGetTokenT< SiPixelDetectorStatus > siPixelStatusToken_
std::map< int, std::vector< int > > getFEDerror25Rocs()
unsigned int LuminosityBlockNumber_t
std::pair< int, int > getLSRange()
std::vector< SiPixelDetectorStatus >::iterator siPixelStatusVtr_iterator
void setLSRange(int ls0, int ls1)
std::map< edm::LuminosityBlockNumber_t, std::map< int, std::vector< int > > > FEDerror25Map_
std::vector< SiPixelDetectorStatus > siPixelStatusVtr_
Log< level::Info, false > LogInfo
std::map< edm::LuminosityBlockNumber_t, SiPixelDetectorStatus > siPixelStatusMap_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
bool isValid() const
Definition: HandleBase.h:70
HLT enums.
static bool rankByLumi(SiPixelDetectorStatus status1, SiPixelDetectorStatus status2)
Log< level::Warning, false > LogWarning
void readLumi(const edm::LuminosityBlock &)
LuminosityBlockNumber_t luminosityBlock() const
std::map< edm::LuminosityBlockNumber_t, SiPixelDetectorStatus >::iterator siPixelStatusMap_iterator