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