CMS 3D CMS Logo

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
SiStripDetVOffBuilder.cc
Go to the documentation of this file.
2 #include "boost/foreach.hpp"
3 
4 // constructor
6  onlineDbConnectionString(pset.getParameter<std::string>("onlineDB")),
7  authenticationPath(pset.getParameter<std::string>("authPath")),
8  whichTable(pset.getParameter<std::string>("queryType")),
9  lastValueFileName(pset.getParameter<std::string>("lastValueFile")),
10  fromFile(pset.getParameter<bool>("lastValueFromFile")),
11  psuDetIdMapFile_(pset.getParameter<std::string>("PsuDetIdMapFile")),
12  debug_(pset.getParameter<bool>("debugModeOn")),
13  tDefault(7,0),
14  tmax_par(pset.getParameter< std::vector<int> >("Tmax")),
15  tmin_par(pset.getParameter< std::vector<int> >("Tmin")),
16  tset_par(pset.getParameter< std::vector<int> >("TSetMin")),
17  detIdListFile_(pset.getParameter< std::string >("DetIdListFile")),
18  excludedDetIdListFile_(pset.getParameter< std::string >("ExcludedDetIdListFile")),
19  highVoltageOnThreshold_(pset.getParameter<double>("HighVoltageOnThreshold"))
20 {
21  lastStoredCondObj.first = NULL;
22  lastStoredCondObj.second = 0;
23 
24  edm::LogError("SiStripDetVOffBuilder") << "[SiStripDetVOffBuilder::SiStripDetVOffBuilder] constructor" << endl;
25 
26  // set up vectors based on pset parameters (tDefault purely for initialization)
27 
28  whichQuery=(whichTable == "STATUSCHANGE" || (whichTable == "LASTVALUE" && !fromFile));
29 
30  //Define the query interval [Tmin, Tmax]
31  //where Tmax comes from the cfg
32  // Tmin comes from the cfg for the first o2o, after that it is extracted from Offline DB
33 
34  tmax = coral::TimeStamp(tmax_par[0],tmax_par[1],tmax_par[2],tmax_par[3],tmax_par[4],tmax_par[5],tmax_par[6]);
35 
36  if (whichQuery) {
37  // Is there a better way to do this? TODO - investigate
38  tmin=coral::TimeStamp(tmin_par[0],tmin_par[1],tmin_par[2],tmin_par[3],tmin_par[4],tmin_par[5],tmin_par[6]);
39  }
40 
41  if (whichTable == "LASTVALUE") {
42  tsetmin = coral::TimeStamp(tset_par[0],tset_par[1],tset_par[2],tset_par[3],tset_par[4],tset_par[5],tset_par[6]);
43  }
44 
45  if (onlineDbConnectionString == "") {
46  edm::LogError("SiStripDetVOffBuilder") << "[SiStripDetVOffBuilder::SiStripDetVOffBuilder] DB name has not been set properly ... Returning ...";
47  return;
48  }
49 
50  if (fromFile && whichTable == "LASTVALUE" && lastValueFileName == "") {
51  edm::LogError("SiStripDetVOffBuilder") << "[SiStripDetVOffBuilder::SiStripDetVOffBuilder] File expected for lastValue table, but filename not specified ... Returning ...";
52  return;
53  }
54 
55  // write out the parameters
56  std::stringstream ss;
57  ss << "[SiStripDetVOffBuilder::SiStripDetVOffBuilder]\n"
58  << " Parameters:\n"
59  << " DB connection string: " << onlineDbConnectionString << "\n"
60  << " Authentication path: " << authenticationPath << "\n"
61  << " Table to be queried: " << whichTable << "\n";
62 
63  if (whichQuery){
64  ss << " Tmin: "; printPar(ss,tmin_par); ss << std::endl;
65  }
66  ss << " Tmax: " ; printPar(ss,tmax_par); ss << std::endl;
67 
68  if (whichTable == "LASTVALUE"){
69  ss << " TSetMin: "; printPar(ss,tset_par); ss << std::endl;
70  }
71  edm::LogError("SiStripDetVOffBuilder") << ss.str();
72 
73 }
74 
75 // destructor
77  edm::LogError("SiStripDetVOffBuilder") << "[SiStripDetVOffBuilder::" << __func__ << "]: destructing ...";
78 }
79 
80 void SiStripDetVOffBuilder::printPar(std::stringstream& ss, const std::vector<int>& par){
81  BOOST_FOREACH(int val, par){
82  ss << val << " ";
83  }
84 }
85 
87 {
88  // vectors for storing output from DB or text file
89  TimesAndValues timesAndValues;
90 
91  // Open the PVSS DB connection
93  edm::LogError("SiStripDetVOffBuilder") << "[SiStripDetVOffBuilder::BuildDetVOff]: Query type is " << whichTable << endl;
94 
95  if (whichTable == "LASTVALUE") {edm::LogError("SiStripDetVOffBuilder") << "[SiStripDetVOffBuilder::BuildDetVOff]: Use file? " << ((fromFile) ? "TRUE" : "FALSE");}
96 
97  if (lastStoredCondObj.second > 0) {edm::LogError("SiStripDetVOffBuilder") << "[SiStripDetVOffBuilder::BuildDetVOff]: retrieved last time stamp from DB: "
98  << lastStoredCondObj.second << endl;}
99  // access the information!
100 
101  if (whichQuery) {
102  if( whichTable == "STATUSCHANGE" ) {
103  statusChange( lastStoredCondObj.second, timesAndValues );
104  }
105  if( whichTable == "LASTVALUE" ) {
106  if( fromFile ) {
107  lastValueFromFile(timesAndValues);
108  }
109  else {
110  lastValue(timesAndValues);
111  }
112  }
113  }
114 
115  DetIdListTimeAndStatus dStruct;
116 
117  // build PSU - det ID map
118  buildPSUdetIdMap(timesAndValues, dStruct);
119 
120 
121  // initialize variables
122  modulesOff.clear();
123  cond::Time_t saveIovTime = 0;
124 
125 
126  // - If there is already an object stored in the database
127  // -- store it in the modulesOff vector
128  // -- set the saveIovTime as that
129  // -- set the payload stats to empty
130  // Successivamente:
131  // - loop su tutti gli elementi del detidV, che � stato letto dal pvss (questi elementi sono pair<vettore di detid, time>)
132  // -- setta il tempo dell'IOV:
133  // --- LASTVALUE -> iovtime settato a latestTime
134  // --- altrimenti iovtime = tempo associato al detId vector del loop
135 
136 
137  // check if there is already an object stored in the DB
138  // This happens only if you are using STATUSCHANGE
139  if (lastStoredCondObj.first != NULL && lastStoredCondObj.second > 0) {
140  modulesOff.push_back( lastStoredCondObj );
141  saveIovTime = lastStoredCondObj.second;
142  setPayloadStats(0, 0, 0);
143  }
144 
145 
146  for (unsigned int i = 0; i < dStruct.detidV.size(); i++) {
147 
148  // std::vector<uint32_t> detids = dStruct.detidV[i].first;
149  // removeDuplicates(detids);
150  std::vector<uint32_t> * detids = &(dStruct.detidV[i].first);
151 
152  // set the condition time for the transfer
153  cond::Time_t iovtime = 0;
154 
155  if (whichTable == "LASTVALUE") {iovtime = timesAndValues.latestTime;}
156 
157  else {iovtime = getCondTime((dStruct.detidV[i]).second);}
158 
159  // decide how to initialize modV
160  SiStripDetVOff *modV = 0;
161 
162  // When using STATUSCHANGE they are equal only for the first
163  // When using LASTVALUE they are equal only if the tmin was set to tsetmin
164 
165  if (iovtime != saveIovTime) { // time is different, so create new object
166 
167  // This can be only when using LASTVALUE or with a new tag
168  if (modulesOff.empty()) {
169  // create completely new object and set the initial state to Tracker all off
170  modV = new SiStripDetVOff();
171 
172  // Use the file
174  SiStripDetInfoFileReader reader(fp.fullPath());
175  const std::map<uint32_t, SiStripDetInfoFileReader::DetInfo > detInfos = reader.getAllData();
176 
177  // Careful: if a module is in the exclusion list it must be ignored and the initial status is set to ON.
178  // These modules are expected to not be in the PSU-DetId map, so they will never get any status change from the query.
180  std::vector< std::pair<uint32_t, std::string> > excludedDetIdMap;
181  if( excludedDetIdListFile_ != "" ) {
182  map.BuildMap(excludedDetIdListFile_, excludedDetIdMap);
183  }
184  for(std::map<uint32_t, SiStripDetInfoFileReader::DetInfo >::const_iterator it = detInfos.begin(); it != detInfos.end(); ++it) {
185  std::vector< std::pair<uint32_t, std::string> >::const_iterator exclIt = excludedDetIdMap.begin();
186  bool excluded = false;
187  for( ; exclIt != excludedDetIdMap.end(); ++exclIt ) {
188  if( it->first == exclIt->first ) {
189  excluded = true;
190  break;
191  }
192  }
193  if( !excluded ) {
194  modV->put( it->first, 1, 1 );
195  }
196  }
197 
198  }
199  else {modV = new SiStripDetVOff( *(modulesOff.back().first) );} // start from copy of previous object
200  }
201  else {
202  modV = (modulesOff.back()).first; // modify previous object
203  }
204 
205 
206 
207  // extract the detID vector before modifying for stats calculation
208  std::vector<uint32_t> beforeV;
209  modV->getDetIds(beforeV);
210 
211  std::pair<int, int> hvlv = extractDetIdVector(i, modV, dStruct);
212 
213  for (unsigned int j = 0; j < detids->size(); j++) {
214  if( debug_ ) cout << "at time = " << iovtime << " detid["<<j<<"] = " << (*detids)[j] << " has hv = " << hvlv.first << " and lv = " << hvlv.second << endl;
215  modV->put((*detids)[j],hvlv.first,hvlv.second);
216  }
217 
218  // calculate the stats for storage
219  unsigned int numAdded = 0, numRemoved = 0;
220  if (iovtime == saveIovTime) {
221  std::vector<uint32_t> oldStats = payloadStats.back();
222  numAdded = oldStats[1];
223  numRemoved = oldStats[2];
224  }
225  std::vector<uint32_t> afterV;
226  modV->getDetIds(afterV);
227 
228  if ((afterV.size() - beforeV.size()) > 0) {
229  numAdded += afterV.size() - beforeV.size();
230  } else if ((beforeV.size() - afterV.size()) > 0) {
231  numRemoved += beforeV.size() - afterV.size();
232  }
233 
234 
235  // store the object if it's a new object
236  if (iovtime != saveIovTime) {
237  SiStripDetVOff * testV = 0;
238  if (!modulesOff.empty()) {testV = modulesOff.back().first;}
239  if (modulesOff.empty() || !(*modV == *testV) ) {
240  modulesOff.push_back( std::make_pair(modV,iovtime) );
241  // save the time of the object
242  saveIovTime = iovtime;
243  // save stats
244  setPayloadStats(afterV.size(), numAdded, numRemoved);
245  }
246  } else {
247  (payloadStats.back())[0] = afterV.size();
248  (payloadStats.back())[1] = numAdded;
249  (payloadStats.back())[2] = numRemoved;
250  }
251  }
252 
253 
254  // compare the first element and the last from previous transfer
255  if (lastStoredCondObj.first != NULL && lastStoredCondObj.second > 0) {
256  if ( lastStoredCondObj.second == modulesOff[0].second &&
257  *(lastStoredCondObj.first) == *(modulesOff[0].first) ) {
258  std::vector< std::pair<SiStripDetVOff*,cond::Time_t> >::iterator moIt = modulesOff.begin();
259  modulesOff.erase(moIt);
260  std::vector< std::vector<uint32_t> >::iterator plIt = payloadStats.begin();
261  payloadStats.erase(plIt);
262  }
263  }
264 
265  if (debug_) {
266  std::cout << std::endl;
267  std::cout << "Size of modulesOff = " << modulesOff.size() << std::endl;
268  for (unsigned int i = 0; i < modulesOff.size(); i++) {
269  std::vector<uint32_t> finalids;
270  (modulesOff[i].first)->getDetIds(finalids);
271  std::cout << "Index = " << i << " Size of DetIds vector = " << finalids.size() << std::endl;
272  std::cout << "Time = " << modulesOff[i].second << std::endl;
273  for (unsigned int j = 0; j < finalids.size(); j++) {
274  std::cout << "detid = " << finalids[j] << " LV off = " << (modulesOff[i].first)->IsModuleLVOff(finalids[j]) << " HV off = "
275  << (modulesOff[i].first)->IsModuleHVOff(finalids[j]) << std::endl;
276  }
277  }
278  }
279 }
280 
281 int SiStripDetVOffBuilder::findSetting(uint32_t id, coral::TimeStamp changeDate, std::vector<uint32_t> settingID, std::vector<coral::TimeStamp> settingDate) {
282  int setting = -1;
283  // find out how many channel entries there are
284  std::vector<int> locations;
285  for (unsigned int i = 0; i < settingID.size(); i++) { if (settingID[i] == id) {locations.push_back((int)i);} }
286 
287  // simple cases
288  if (locations.size() == 0) {setting = -1;}
289  else if (locations.size() == 1) {setting = locations[0];}
290  // more than one entry for this channel
291  // NB. entries ordered by date!
292  else {
293  for (unsigned int j = 0; j < locations.size(); j++) {
294 #ifdef USING_NEW_CORAL
295  const boost::posix_time::ptime& testSec = changeDate.time();
296  const boost::posix_time::ptime& limitSec = settingDate[(unsigned int)locations[j]].time();
297 #else
298  long testSec = changeDate.time().ns();
299  long limitSec = settingDate[(unsigned int)locations[j]].time().ns();
300 #endif
301  if (testSec >= limitSec) {setting = locations[j];}
302  }
303  }
304  return setting;
305 }
306 
307 int SiStripDetVOffBuilder::findSetting(std::string dpname, coral::TimeStamp changeDate, std::vector<std::string> settingDpname, std::vector<coral::TimeStamp> settingDate) {
308  int setting = -1;
309  // find out how many channel entries there are
310  std::vector<int> locations;
311  for (unsigned int i = 0; i < settingDpname.size(); i++) { if (settingDpname[i] == dpname) {locations.push_back((int)i);} }
312 
313  // simple cases
314  if (locations.size() == 0) {setting = -1;}
315  else if (locations.size() == 1) {setting = locations[0];}
316  // more than one entry for this channel
317  // NB. entries ordered by date!
318  else {
319  for (unsigned int j = 0; j < locations.size(); j++) {
320 #ifdef USING_NEW_CORAL
321  const boost::posix_time::ptime& testSec = changeDate.time();
322  const boost::posix_time::ptime& limitSec = settingDate[(unsigned int)locations[j]].time();
323 #else
324  long testSec = changeDate.time().ns();
325  long limitSec = settingDate[(unsigned int)locations[j]].time().ns();
326 #endif
327  if (testSec >= limitSec) {setting = locations[j];}
328  }
329  }
330  return setting;
331 }
332 
333 void SiStripDetVOffBuilder::readLastValueFromFile(std::vector<uint32_t> &dpIDs, std::vector<float> &vmonValues, std::vector<coral::TimeStamp> &dateChange) {
334  std::ifstream lastValueFile(lastValueFileName.c_str());
335  if (lastValueFile.bad()) {
336  edm::LogError("SiStripDetVOffBuilder") << "[SiStripDetVOffBuilder::" << __func__ << "]: last Value file does not exist!";
337  return;
338  }
339 
340  dpIDs.clear();
341  vmonValues.clear();
342  dateChange.clear();
343  std::vector<std::string> changeDates;
344 
345  std::string line;
346  // remove the first line as it is the title line
347  // std::getline(lastValueFile,line);
348  // line.clear();
349  // now extract data
350  while( std::getline(lastValueFile,line) ) {
351  std::istringstream ss(line);
352  uint32_t dpid;
353  float vmon;
354  std::string changeDate;
355  ss >> std::skipws >> dpid >> vmon >> changeDate;
356  dpIDs.push_back(dpid);
357  vmonValues.push_back(vmon);
358  changeDates.push_back(changeDate);
359  }
360  lastValueFile.close();
361 
362  // Now convert dates to coral::TimeStamp
363  for (unsigned int i = 0; i < changeDates.size(); i++) {
364  std::string part = changeDates[i].substr(0,4);
365  int year = atoi(part.c_str());
366  part.clear();
367 
368  part = changeDates[i].substr(5,2);
369  int month = atoi(part.c_str());
370  part.clear();
371 
372  part = changeDates[i].substr(8,2);
373  int day = atoi(part.c_str());
374  part.clear();
375 
376  part = changeDates[i].substr(11,2);
377  int hour = atoi(part.c_str());
378  part.clear();
379 
380  part = changeDates[i].substr(14,2);
381  int minute = atoi(part.c_str());
382  part.clear();
383 
384  part = changeDates[i].substr(17,2);
385  int second = atoi(part.c_str());
386  part.clear();
387 
388  coral::TimeStamp date(year,month,day,hour,minute,second,0);
389  dateChange.push_back(date);
390  }
391 
392  if (changeDates.size() != dateChange.size()) {edm::LogError("SiStripDetVOffBuilder") << "[SiStripDetVOffBuilder::" << __func__ << "]: date conversion failed!!";}
393 }
394 
396 
397  // const boost::posix_time::ptime& t = coralTime.time();
398  cond::Time_t condTime = cond::time::from_boost(coralTime.time());
399 
400  // cout << "[SiStripDetVOffBuilder::getCondTime] Converting CoralTime into CondTime: "
401  // << " coralTime = (coralTimeInNs) " << coralTime.total_nanoseconds() << " condTime " << (condTime>> 32) << " - " << (condTime & 0xFFFFFFFF) << endl;
402 
403  return condTime;
404 }
405 
407 {
408  // This method is defined in the TimeConversions header and it does the following:
409  // - takes the seconds part of the iovTime (bit-shifting of 32)
410  // - adds the nanoseconds part (first 32 bits mask)
411  // - adds the time0 that is the time from begin of times (boost::posix_time::from_time_t(0);)
412  coral::TimeStamp coralTime(cond::time::to_boost(iovTime));
413 
414  if( debug_ ) {
415  unsigned long long iovSec = iovTime >> 32;
416  uint32_t iovNanoSec = uint32_t(iovTime);
417  cond::Time_t testTime=getCondTime(coralTime);
418  cout << "[SiStripDetVOffBuilder::getCoralTime] Converting CondTime into CoralTime: "
419  << " condTime = " << iovSec << " - " << iovNanoSec
420  << " getCondTime(coralTime) = " << (testTime>>32) << " - " << (testTime&0xFFFFFFFF) << endl;
421  }
422 
423  return coralTime;
424 }
425 
426 void SiStripDetVOffBuilder::removeDuplicates( std::vector<uint32_t> & vec ) {
427  std::sort(vec.begin(),vec.end());
428  std::vector<uint32_t>::iterator it = std::unique(vec.begin(),vec.end());
429  vec.resize( it - vec.begin() );
430 }
431 
433  lastStoredCondObj.first = lastPayload;
434  lastStoredCondObj.second = lastTimeStamp;
435 }
436 
437 cond::Time_t SiStripDetVOffBuilder::findMostRecentTimeStamp( std::vector<coral::TimeStamp> coralDate ) {
438  cond::Time_t latestDate = getCondTime(coralDate[0]);
439 
440  if( debug_ ) {
441  std::cout << "latestDate: condTime = "
442  << (latestDate>>32)
443  << " - "
444  << (latestDate&0xFFFFFFFF)
445  //<< " coralTime= " << coralDate[0]
446  << std::endl;
447  }
448 
449  for (unsigned int i = 1; i < coralDate.size(); i++) {
450  cond::Time_t testDate = getCondTime(coralDate[i]);
451  if (testDate > latestDate) {
452  latestDate = testDate;
453  }
454  }
455  return latestDate;
456 }
457 
458 void SiStripDetVOffBuilder::reduce( std::vector< std::pair<SiStripDetVOff*,cond::Time_t> >::iterator & it,
459  std::vector< std::pair<SiStripDetVOff*,cond::Time_t> >::iterator & initialIt,
460  std::vector< std::pair<SiStripDetVOff*,cond::Time_t> > & resultVec,
461  const bool last )
462 {
463  int first = 0;
464  // Check if it is the first
465  if( distance(resultVec.begin(), initialIt) == 0 ) {
466  first = 1;
467  }
468 
469  if( debug_ && ( it->first->getLVoffCounts() - initialIt->first->getLVoffCounts() == 0 ) && ( it->first->getHVoffCounts() - initialIt->first->getHVoffCounts() == 0 ) ) {
470  cout << "Same number of LV and HV at start and end of sequence: LV off = " << it->first->getLVoffCounts() << " HV off = " << it->first->getHVoffCounts() << endl;
471  }
472 
473  // if it was going off
474  if( ( it->first->getLVoffCounts() - initialIt->first->getLVoffCounts() > 0 ) || ( it->first->getHVoffCounts() - initialIt->first->getHVoffCounts() > 0 ) ) {
475  // Set the time of the current (last) iov as the time of the initial iov of the sequence
476  // replace the first iov with the last one
477  (it+last)->second = (initialIt)->second;
478  discardIOVs(it, initialIt, resultVec, last, 0);
479  if( debug_ ) cout << "going off" << endl;
480  }
481  // if it was going on
482  else if( ( it->first->getLVoffCounts() - initialIt->first->getLVoffCounts() <= 0 ) || ( it->first->getHVoffCounts() - initialIt->first->getHVoffCounts() <= 0 ) ) {
483  // replace the last minus one iov with the first one
484  discardIOVs(it, initialIt, resultVec, last, first);
485  if( debug_ ) cout << "going on" << endl;
486  }
487 }
488 
489 void SiStripDetVOffBuilder::discardIOVs( std::vector< std::pair<SiStripDetVOff*,cond::Time_t> >::iterator & it,
490  std::vector< std::pair<SiStripDetVOff*,cond::Time_t> >::iterator & initialIt,
491  std::vector< std::pair<SiStripDetVOff*,cond::Time_t> > & resultVec,
492  const bool last, const unsigned int first )
493 {
494  if( debug_ ) {
495  cout << "first = " << first << endl;
496  cout << "initial->first = " << initialIt->first << ", second = " << initialIt->second << endl;
497  cout << "last = " << last << endl;
498  }
499  if( last == true ) {
500  resultVec.erase(initialIt+first, it+1);
501  // Minus 2 because it will be incremented at the end of the loop becoming end()-1.
502  it = resultVec.end()-2;
503  }
504  else {
505  it = resultVec.erase(initialIt+first, it);
506  }
507 }
508 
509 void SiStripDetVOffBuilder::reduction(const uint32_t deltaTmin, const uint32_t maxIOVlength)
510 {
511  int count = 0;
512  std::vector< std::pair<SiStripDetVOff*,cond::Time_t> >::iterator initialIt;
513 
514  int resultVecSize = modulesOff.size();
515  int resultsIndex = 0;
516 
517  if( resultVecSize > 1 ) {
518  std::vector< std::pair<SiStripDetVOff*,cond::Time_t> >::iterator it = modulesOff.begin();
519  for( ; it != modulesOff.end()-1; ++it, ++resultsIndex ) {
520  unsigned long long deltaT = ((it+1)->second - it->second) >> 32;
521  unsigned long long deltaTsequence = 0;
522  if( count > 1 ) {
523  deltaTsequence = ((it+1)->second - initialIt->second) >> 32;
524  }
525  // Save the initial pair
526  if( (deltaT < deltaTmin) && ( (count == 0) || ( deltaTsequence < maxIOVlength ) ) ) {
527  // If we are not in a the sequence
528  if( count == 0 ) {
529  initialIt = it;
530  }
531  // Increase the counter in any case.
532  ++count;
533  }
534  // We do it only if the sequence is bigger than two cases
535  else if( count > 1 ) {
536  reduce(it, initialIt, modulesOff);
537  // reset all
538  count = 0;
539  }
540  else {
541  // reset all
542  count = 0;
543  }
544  // Border case
545  if( resultsIndex == resultVecSize-2 && count != 0 ) {
546  reduce(it, initialIt, modulesOff, true);
547  }
548  }
549  }
550 }
551 
553 {
554  // Setting tmin to the last value IOV of the database tag
555  if( lastTime > 0 ) {
556  tmin = getCoralTime(lastTime);
557  }
558 
559  coralInterface->doQuery(whichTable, tmin ,tmax, tStruct.changeDate, tStruct.actualValue, tStruct.dpname);
560 
561  // preset the size of the status vector
562  tStruct.actualStatus.resize(tStruct.actualValue.size());
563  tStruct.actualStatus.clear();
564 
565  BOOST_FOREACH(float val, tStruct.actualValue) {
566  tStruct.actualStatus.push_back(static_cast<int>(val));
567  }
568 }
569 
571 {
572  coralInterface->doQuery(whichTable, tmin ,tmax, tStruct.changeDate, tStruct.actualValue, tStruct.dpname);
573 
574  tStruct.latestTime = findMostRecentTimeStamp( tStruct.changeDate );
575 
576  // preset the size of the status vector
577  tStruct.actualStatus.resize(tStruct.actualValue.size());
578 
579  // retrieve the channel settings from the PVSS DB
580  std::vector<coral::TimeStamp> settingDate;
581  std::vector<float> settingValue;
582  std::vector<std::string> settingDpname;
583  std::vector<uint32_t> settingDpid;
584  coralInterface->doSettingsQuery(tsetmin,tmax,settingDate,settingValue,settingDpname,settingDpid);
585  LogDebug("SiStripDetVOffBuilder") << "[SiStripDetVOffBuilder::BuildDetVOff]: Channel settings retrieved";
586  LogDebug("SiStripDetVOffBuilder") << "[SiStripDetVOffBuilder::BuildDetVOff]: Number of PSU channels: " << settingDpname.size();
587 
588  unsigned int missing = 0;
589  std::stringstream ss;
590  for (unsigned int j = 0; j < tStruct.dpname.size(); j++) {
591  int setting = findSetting(tStruct.dpname[j],tStruct.changeDate[j],settingDpname,settingDate);
592  if (setting >= 0) {
593  if (tStruct.actualValue[j] > (highVoltageOnThreshold_*(settingValue[setting]))) {tStruct.actualStatus[j] = 1;}
594  else {tStruct.actualStatus[j] = 0;}
595  } else {
596  tStruct.actualStatus[j] = -1;
597  missing++;
598  ss << "Channel = " << tStruct.dpname[j] << std::endl;
599  }
600  }
601  LogDebug("SiStripDetVOffBuilder") << "[SiStripDetVOffBuilder::BuildDetVOff]: Number of channels with no setting information " << missing;
602  LogDebug("SiStripDetVOffBuilder") << "[SiStripDetVOffBuilder::BuildDetVOff]: Number of entries in dpname vector " << tStruct.dpname.size();
603 }
604 
606 {
607  readLastValueFromFile(tStruct.dpid,tStruct.actualValue,tStruct.changeDate);
608  tStruct.latestTime = findMostRecentTimeStamp( tStruct.changeDate );
609  LogDebug("SiStripDetVOffBuilder") << "[SiStripDetVOffBuilder::BuildDetVOff]: File access complete \n\t Number of values read from file: " << tStruct.dpid.size();
610 
611  // retrieve the channel settings from the PVSS DB
612  std::vector<coral::TimeStamp> settingDate;
613  std::vector<float> settingValue;
614  std::vector<std::string> settingDpname;
615  std::vector<uint32_t> settingDpid;
616 
617  coralInterface->doSettingsQuery(tsetmin,tmax,settingDate,settingValue,settingDpname,settingDpid);
618  LogDebug("SiStripDetVOffBuilder") << "[SiStripDetVOffBuilder::BuildDetVOff]: Channel settings retrieved";
619  LogDebug("SiStripDetVOffBuilder") << "[SiStripDetVOffBuilder::BuildDetVOff]: Number of PSU channels: " << settingDpname.size();
620 
621  unsigned int missing = 0;
622  std::stringstream ss;
623  // need to get the PSU channel names from settings
624  tStruct.dpname.clear();
625  tStruct.dpname.resize(tStruct. dpid.size());
626  for (unsigned int j = 0; j < tStruct.dpid.size(); j++) {
627  int setting = findSetting(tStruct.dpid[j],tStruct.changeDate[j],settingDpid,settingDate);
628  if (setting >= 0) {
629  if (tStruct.actualValue[j] > (highVoltageOnThreshold_*settingValue[setting])) {tStruct.actualStatus[j] = 1;}
630  else {
631  tStruct.actualStatus[j] = 0;
632  }
633  tStruct.dpname[j] = settingDpname[setting];
634  } else {
635  tStruct.actualStatus[j] = -1;
636  tStruct.dpname[j] = "UNKNOWN";
637  missing++;
638  ss << "DP ID = " << tStruct.dpid[j] << std::endl;
639  }
640  }
641  LogDebug("SiStripDetVOffBuilder") << "Number of missing psu channels = " << missing << std::endl;
642  LogDebug("SiStripDetVOffBuilder") << "IDs are: = " << ss.str();
643 }
644 
645 string SiStripDetVOffBuilder::timeToStream(const cond::Time_t & condTime, const string & comment)
646 {
647  stringstream ss;
648  ss << comment << (condTime>> 32) << " - " << (condTime & 0xFFFFFFFF) << std::endl;
649  return ss.str();
650 }
651 
652 string SiStripDetVOffBuilder::timeToStream(const coral::TimeStamp & coralTime, const string & comment)
653 {
654  stringstream ss;
655  ss << "Starting from IOV time in the database : year = " << coralTime.year()
656  << ", month = " << coralTime.month()
657  << ", day = " << coralTime.day()
658  << ", hour = " << coralTime.hour()
659  << ", minute = " << coralTime.minute()
660  << ", second = " << coralTime.second()
661  << ", nanosecond = " << coralTime.nanosecond() << std::endl;
662  return ss.str();
663 }
664 
666 {
667  SiStripPsuDetIdMap map_;
668  if( psuDetIdMapFile_ == "" ) {
669  map_.BuildMap();
670  }
671  else {
673  }
674  LogTrace("SiStripDetVOffBuilder") <<"[SiStripDetVOffBuilder::BuildDetVOff] DCU-DET ID map built";
675  map_.printMap();
676 
677  // use map info to build input for list of objects
678  // no need to check for duplicates, as put method for SiStripDetVOff checks for you!
679 
680  unsigned int ch0bad = 0, ch1bad = 0, ch2bad = 0, ch3bad = 0;
681  std::vector<unsigned int> numLvBad, numHvBad;
682 
683  for (unsigned int dp = 0; dp < psuStruct.dpname.size(); dp++) {
684  if (psuStruct.dpname[dp] != "UNKNOWN") {
685 
686  // figure out the channel
687  std::string board = psuStruct.dpname[dp];
688  std::string::size_type loc = board.size()-10;
689  board.erase(0,loc);
690  // now store!
691  std::vector<uint32_t> ids = map_.getDetID(psuStruct.dpname[dp]);
692 
693  if( debug_ ) cout << "dbname["<<dp<<"] = " << psuStruct.dpname[dp] << ", for time = " << timeToStream(psuStruct.changeDate[dp]) << std::endl;
694 
695  if (!ids.empty()) {
696  // DCU-PSU maps only channel000 and channel000 and channel001 switch on and off together
697  // so check only channel000
698  // if (board == "channel000" || board == "channel001") {
699  if (board == "channel000") {
700  detIdStruct.detidV.push_back( std::make_pair(ids,psuStruct.changeDate[dp]) );
701  if (psuStruct.actualStatus[dp] != 1) {
702  // if (board == "channel000") {ch0bad++;}
703  // if (board == "channel001") {ch1bad++;}
704  ++ch0bad;
705  ++ch1bad;
706  detIdStruct.StatusGood.push_back(false);
707  numLvBad.insert(numLvBad.end(),ids.begin(),ids.end());
708  }
709  else {
710  detIdStruct.StatusGood.push_back(true);
711  }
712  detIdStruct.isHV.push_back(0);
713  detIdStruct.psuName.push_back( psuStruct.dpname[dp] );
714  }
715  else if( board == "channel002" || board == "channel003" ) {
716  detIdStruct.detidV.push_back( std::make_pair(ids,psuStruct.changeDate[dp]) );
717  if( debug_ ) cout << "actualStatus = " << psuStruct.actualStatus[dp] << " for psu: " << psuStruct.dpname[dp] << endl;
718  if (psuStruct.actualStatus[dp] != 1) {
719  if (board == "channel002") {ch2bad++;}
720  if (board == "channel003") {ch3bad++;}
721  detIdStruct.StatusGood.push_back(false);
722  numHvBad.insert(numHvBad.end(),ids.begin(),ids.end());
723  }
724  else {
725  detIdStruct.StatusGood.push_back(true);
726  }
727  detIdStruct.isHV.push_back(1);
728  detIdStruct.psuName.push_back( psuStruct.dpname[dp] );
729  }
730  else {
731  if (board != "channel001") {
732  LogTrace("SiStripDetVOffBuilder") << "[SiStripDetVOffBuilder::" << __func__ << "] channel name not recognised! " << board;
733  }
734  }
735  }
736  } else {
737  detIdStruct.notMatched++;
738  }
739  }
740 
741  removeDuplicates(numLvBad);
742  removeDuplicates(numHvBad);
743 
744  // useful debugging stuff!
745  if( debug_ ) {
746  std::cout << "Bad 000 = " << ch0bad << " Bad 001 = " << ch1bad << std::endl;
747  std::cout << "Bad 002 = " << ch0bad << " Bad 003 = " << ch1bad << std::endl;
748  std::cout << "Number of bad LV detIDs = " << numLvBad.size() << std::endl;
749  std::cout << "Number of bad HV detIDs = " << numHvBad.size() << std::endl;
750  }
751 
752  LogTrace("SiStripDetVOffBuilder") << "[SiStripDetVOffBuilder::" << __func__ << "]: Number of PSUs retrieved from DB with map information " << detIdStruct.detidV.size();
753  LogTrace("SiStripDetVOffBuilder") << "[SiStripDetVOffBuilder::" << __func__ << "]: Number of PSUs retrieved from DB with no map information " << detIdStruct.notMatched;
754 
755  unsigned int dupCount = 0;
756  for (unsigned int t = 0; t < numLvBad.size(); t++) {
757  std::vector<unsigned int>::iterator iter = std::find(numHvBad.begin(),numHvBad.end(),numLvBad[t]);
758  if (iter != numHvBad.end()) {dupCount++;}
759  }
760  if( debug_ ) std::cout << "Number of channels with LV & HV bad = " << dupCount << std::endl;
761 }
762 
763 void SiStripDetVOffBuilder::setPayloadStats(const uint32_t afterV, const uint32_t numAdded, const uint32_t numRemoved)
764 {
765  std::vector<uint32_t> pStats(3,0);
766  pStats.push_back(afterV);
767  pStats.push_back(numAdded);
768  pStats.push_back(numRemoved);
769  payloadStats.push_back(pStats);
770 }
771 
772 pair<int, int> SiStripDetVOffBuilder::extractDetIdVector( const unsigned int i, SiStripDetVOff * modV, DetIdListTimeAndStatus & detIdStruct )
773 {
774  // set the LV and HV off flags ready for storing
775  int lv_off = -1, hv_off = -1;
776  if (detIdStruct.isHV[i] == 0) {lv_off = !(detIdStruct.StatusGood[i]);}
777  if (detIdStruct.isHV[i] == 1) {
778  hv_off = !(detIdStruct.StatusGood[i]);
779 
780  // TESTING WITHOUT THE FIX
781  // -----------------------
782 
783  if( psuDetIdMapFile_ == "" ) {
784  // temporary fix to handle the fact that we don't know which HV channel the detIDs are associated to
785  if (i > 0) {
786  std::string iChannel = detIdStruct.psuName[i].substr( (detIdStruct.psuName[i].size()-3) );
787  std::string iPsu = detIdStruct.psuName[i].substr(0, (detIdStruct.psuName[i].size()-3) );
788  if (iChannel == "002" || iChannel == "003") {
789  bool lastStatusOfOtherChannel = true;
790  for (unsigned int j = 0; j < i; j++) {
791  std::string jPsu = detIdStruct.psuName[j].substr(0, (detIdStruct.psuName[j].size()-3) );
792  std::string jChannel = detIdStruct.psuName[j].substr( (detIdStruct.psuName[j].size()-3) );
793  if (iPsu == jPsu && iChannel != jChannel && (jChannel == "002" || jChannel == "003")) {
794  if( debug_ ) cout << "psu["<<i<<"] = " << detIdStruct.psuName[i] << " with status = " << detIdStruct.StatusGood[i] << " and psu["<<j<<"] = " << detIdStruct.psuName[j] << " with status " << detIdStruct.StatusGood[j] << endl;
795  lastStatusOfOtherChannel = detIdStruct.StatusGood[j];
796  }
797  }
798  if (detIdStruct.StatusGood[i] != lastStatusOfOtherChannel) {
799  if( debug_ ) cout << "turning off hv" << endl;
800  hv_off = 1;
801  }
802  }
803  }
804  }
805 
806  // -----------------------
807 
808  }
809 
810  return make_pair(hv_off, lv_off);
811 }
#define LogDebug(id)
dictionary missing
Definition: combine.py:4
SiStripDetVOffBuilder(const edm::ParameterSet &, const edm::ActivityRegistry &)
int i
Definition: DBlmapReader.cc:9
void discardIOVs(std::vector< std::pair< SiStripDetVOff *, cond::Time_t > >::iterator &it, std::vector< std::pair< SiStripDetVOff *, cond::Time_t > >::iterator &initialIt, std::vector< std::pair< SiStripDetVOff *, cond::Time_t > > &resultVec, const bool last, const unsigned int first)
Removes IOVs as dictated by reduction.
std::pair< int, int > extractDetIdVector(const unsigned int i, SiStripDetVOff *modV, DetIdListTimeAndStatus &detIdStruct)
void printPar(std::stringstream &ss, const std::vector< int > &par)
void buildPSUdetIdMap(TimesAndValues &tStruct, DetIdListTimeAndStatus &dStruct)
std::vector< uint32_t > getDetID(std::string pvss)
#define NULL
Definition: scimark2.h:8
Extension to SiStripConfigDb to map PSU channels to DetIDs using DCU-PSU map and DCU-DetID map...
bool put(const uint32_t DetId, const int HVoff, const int LVoff)
Insert information for a single detId.
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:7
uint16_t size_type
std::auto_ptr< SiStripCoralIface > coralInterface
dictionary map
Definition: Association.py:160
void removeDuplicates(std::vector< uint32_t > &vec)
U second(std::pair< T, U > const &p)
const std::map< uint32_t, DetInfo > & getAllData() const
unsigned long long Time_t
Definition: Time.h:16
std::vector< coral::TimeStamp > changeDate
void getDetIds(std::vector< uint32_t > &DetIds_) const
int j
Definition: DBlmapReader.cc:9
void reduction(const uint32_t deltaTmin, const uint32_t maxIOVlength)
An interface class to the PVSS cond DB.
tuple pset
Definition: CrabTask.py:85
void lastValue(TimesAndValues &tStruct)
bool first
Definition: L1TdeRCT.cc:79
Time_t from_boost(boost::posix_time::ptime bt)
void readLastValueFromFile(std::vector< uint32_t > &dpIDs, std::vector< float > &vmonValues, std::vector< coral::TimeStamp > &dateChange)
#define LogTrace(id)
void lastValueFromFile(TimesAndValues &tStruct)
cond::Time_t findMostRecentTimeStamp(std::vector< coral::TimeStamp > coralDate)
std::vector< int > tmax_par
coral::TimeStamp getCoralTime(cond::Time_t iovTime)
cond::Time_t getCondTime(coral::TimeStamp coralTime)
part
Definition: HCALResponse.h:21
void setLastSiStripDetVOff(SiStripDetVOff *lastPayload, cond::Time_t lastTimeStamp)
std::vector< int > tmin_par
std::vector< std::vector< uint32_t > > payloadStats
void setPayloadStats(const uint32_t afterV, const uint32_t numAdded, const uint32_t numRemoved)
void reduce(std::vector< std::pair< SiStripDetVOff *, cond::Time_t > >::iterator &it, std::vector< std::pair< SiStripDetVOff *, cond::Time_t > >::iterator &initialIt, std::vector< std::pair< SiStripDetVOff *, cond::Time_t > > &resultVec, const bool last=false)
Operates the reduction of the fast sequences of ramping up and down of the voltages.
tuple cout
Definition: gather_cfg.py:41
std::string fullPath() const
Definition: FileInPath.cc:170
std::vector< std::pair< SiStripDetVOff *, cond::Time_t > > modulesOff
void statusChange(cond::Time_t &lastTime, TimesAndValues &tStruct)
std::pair< SiStripDetVOff *, cond::Time_t > lastStoredCondObj
std::string timeToStream(const coral::TimeStamp &coralTime, const string &comment="")
std::vector< int > tset_par
boost::posix_time::ptime to_boost(Time_t iValue)
int findSetting(uint32_t id, coral::TimeStamp changeDate, std::vector< uint32_t > settingID, std::vector< coral::TimeStamp > settingDate)
const double par[8 *NPar][4]
#define comment(par)
Definition: vmac.h:162