test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
EcalLaserHandler.cc
Go to the documentation of this file.
8 
9 #include<iostream>
10 #include<iomanip>
11 #include <sstream>
12 
14  : m_name(ps.getUntrackedParameter<std::string>("name","EcalLaserHandler")) {
15 
16  std::cout << "EcalLaser Source handler constructor\n" << std::endl;
17 
18  m_sequences = 1;
19  m_fake = true;
20 
21  m_sid= ps.getParameter<std::string>("OnlineDBSID");
22  m_user= ps.getParameter<std::string>("OnlineDBUser");
23  m_pass= ps.getParameter<std::string>("OnlineDBPassword");
24  m_debug=ps.getParameter<bool>("debug");
25  m_fake=ps.getParameter<bool>("fake");
26  m_sequences=static_cast<unsigned int>(atoi( ps.getParameter<std::string>("sequences").c_str()));
27  m_maxtime=ps.getParameter<std::string>("maxtime").c_str();
28  std::cout << "Starting O2O process on DB: " << m_sid
29  << " User: "<< m_user << std::endl;
30  if (m_fake) {
31  std::cout << "*******************************************" << std::endl;
32  std::cout << "This is a fake run. No change to offline DB" << std::endl;
33  std::cout << "*******************************************" << std::endl;
34  }
35 }
36 
38 {
39  // do nothing
40 }
41 
42 double popcon::EcalLaserHandler::diff(float a, float b) {
43  return std::abs(b- a)/a;
44 }
45 
48  int hashedIndex, const std::string &reason) {
49  std::cout << "===== " << reason << " =====" << std::endl;
50  if (hashedIndex < 0) {
51  EEDetId ee;
52  std::cout << "Triplets for " << ee.unhashIndex(-hashedIndex) << " bad: [" << old.p1 << ", "
53  << old.p2 << ", " << old.p3 << "] ==> [" << current.p1 << ", "
54  << current.p2 << ", " << current.p3 << "]" << std::endl;
55  } else {
56  EBDetId eb;
57  std::cout << "Triplets for " << eb.unhashIndex(hashedIndex) << " bad: [" << old.p1 << ", "
58  << old.p2 << ", " << old.p3 << "] ==> [" << current.p1 << ", "
59  << current.p2 << ", " << current.p3 << "]" << std::endl;
60  }
61 }
62 
65  int hashedIndex) {
66  bool ret = true;
67  if ((current.p1 < 0) || (current.p2 < 0) || (current.p3 < 0)) {
68  ret = false;
69  notifyProblems(old, current, hashedIndex, "Negative values");
70  } else if ((current.p1 > 10) || (current.p2 > 10) || (current.p3 > 10)) {
71  ret = false;
72  notifyProblems(old, current, hashedIndex, "Values too large");
73  } else if (((diff(old.p1, current.p1) > 0.2) && (old.p1 != 0) && (old.p1 != 1)) ||
74  ((diff(old.p2, current.p2) > 0.2) && (old.p2 != 0) && (old.p1 != 2)) ||
75  ((diff(old.p3, current.p3) > 0.2) && (old.p3 != 0) && (old.p1 != 3))) {
76  ret = false;
77  notifyProblems(old, current, hashedIndex, "Difference w.r.t. previous too large");
78  }
79  return ret;
80 }
81 
83  const EcalLaserAPDPNRatios::EcalLaserAPDPNRatiosMap &apdpns_popcon) {
84  bool ret = true;
85  for (int hashedIndex = 0; hashedIndex < 61200; hashedIndex++) {
88  ret = checkAPDPN(old, current, hashedIndex);
89  }
90  for (int hashedIndex = 0; hashedIndex < 14648; hashedIndex++) {
93  ret = checkAPDPN(old, current, -hashedIndex);
94  }
95  return ret;
96 }
97 
99  int c = 0;
102  EBDetId eb;
103  try {
104  EcalCondDBInterface *econn = new EcalCondDBInterface( m_sid, m_user, m_pass );
105  while (i != e) {
106  if (c % 1000 == 0) {
107  std::cout << std::setw(5) << c << ": " << eb.unhashIndex(c) << " "
108  << econn->getEcalLogicID("EB_crystal_angle", eb.unhashIndex(c).ieta(),
110  "EB_crystal_number").getLogicID()
111  << " " << std::setiosflags(std::ios::fixed) << std::setprecision(9)
112  << i->p1 << " " << i->p2 << " " << i->p3 << std::endl;
113  }
114  i++;
115  c++;
116  }
117  delete econn;
118  }
119  catch (std::runtime_error &e) {
120  std::cerr << e.what() << std::endl;
121  delete econn;
122  throw cms::Exception("OMDS not available");
123  }
124 }
125 
127  int c = 0;
130  EEDetId ee;
131  try {
132  EcalCondDBInterface *econn = new EcalCondDBInterface( m_sid, m_user, m_pass );
133  while (i != e) {
134  if (c % 1000 == 0) {
135  std::cout << std::setw(5) << c << ": " << ee.unhashIndex(c) << " "
136  << econn->getEcalLogicID("EE_crystal_number", ee.unhashIndex(c).zside(),
137  ee.unhashIndex(c).ix(), ee.unhashIndex(c).iy(),
138  "EE_crystal_number").getLogicID()
139  << " " << std::setiosflags(std::ios::fixed) << std::setprecision(9)
140  << i->p1 << " " << i->p2 << " " << i->p3 << std::endl;
141  }
142  i++;
143  c++;
144  }
145  delete econn;
146  }
147  catch (std::runtime_error &e) {
148  std::cerr << e.what() << std::endl;
149  delete econn;
150  throw cms::Exception("OMDS not available");
151  }
152 }
153 
155 {
156  std::cerr << "------- " << m_name
157  << " ---> getNewObjects" << std::endl;
158 
159  std::cout << "------- Ecal -> getNewObjects\n";
160 
161 
162  unsigned long long max_since= 1;
163  Ref payload= lastPayload();
164 
165  // here popcon tells us which is the last since of the last object in the
166  // offline DB
167  max_since=tagInfo().lastInterval.first;
168  // Tm max_since_tm((max_since >> 32)*1000000);
169  Tm max_since_tm(max_since);
170  // get the last object in the orcoff
171  edm::Timestamp t_min= edm::Timestamp(18446744073709551615ULL);
172 
173  const EcalLaserAPDPNRatios::EcalLaserAPDPNRatiosMap& laserRatiosMap =
174  payload->getLaserMap();
175  std::cout << "payload->getLaserMap(): OK " << std::endl;
176  std::cout << "Its size is " << laserRatiosMap.size() << std::endl;
177  const EcalLaserAPDPNRatios::EcalLaserTimeStampMap& laserTimeMap =
178  payload->getTimeMap();
179  std::cout << "payload->getTimeMap(): OK " << std::endl;
180  std::cout << "Last Object in Offline DB has SINCE = " << max_since
181  << " -> " << max_since_tm.cmsNanoSeconds()
182  << " (" << max_since_tm << ")"
183  << " and SIZE = " << tagInfo().size
184  << std::endl;
185  // loop through light modules and determine the minimum date among the
186  // available channels
187  if (m_debug) {
188  dumpBarrelPayload(laserRatiosMap);
189  dumpEndcapPayload(laserRatiosMap);
190  }
191  for (int i=0; i<92; i++) {
193  if( t_min > timestamp.t1) {
194  t_min=timestamp.t1;
195  }
196  }
197 
198  std::cout <<"WOW: we just retrieved the last valid record from DB "
199  << std::endl;
200  //std::cout <<"Its tmin is "<< Tm((t_min.value() >> 32)*1000000)
201  std::cout <<"Its tmin is "<< Tm(t_min.value())
202  << std::endl;
203 
204  // connect to the database
205  try {
206  std::cout << "Making connection..." << std::flush;
207  econn = new EcalCondDBInterface( m_sid, m_user, m_pass );
208  std::cout << "Done." << std::endl;
209  } catch (std::runtime_error &e) {
210  std::cout << " connection parameters " << m_sid << "/" << m_user;
211  if (m_debug) {
212  std::cout << "/" << m_pass <<std::endl;
213  } else {
214  std::cout << "/**********" <<std::endl;
215  }
216  std::cerr << e.what() << std::endl;
217  throw cms::Exception("OMDS not available");
218  }
219 
220  // retrieve the lists of logic_ids, to build the detids
221  std::vector<EcalLogicID> crystals_EB =
222  econn->getEcalLogicIDSetOrdered( "EB_crystal_angle",
223  -85,85,1,360,
225  "EB_crystal_number", 4 );
226  std::vector<EcalLogicID> crystals_EE =
227  econn->getEcalLogicIDSetOrdered( "EE_crystal_number",
228  -1,1,1,100,
229  1,100,
230  "EE_crystal_number", 4 );
231 
232  std::vector<EcalLogicID>::const_iterator ieb = crystals_EB.begin();
233  std::vector<EcalLogicID>::const_iterator eeb = crystals_EB.end();
234 
235  std::cout << "Got list of " << crystals_EB.size() << " crystals in EB"
236  << std::endl;
237  std::cout << "Got list of " << crystals_EE.size() << " crystals in EE"
238  << std::endl;
239  // loop through barrel
240  int count = 0;
241  // prepare a map to associate EB logic id's to detids
242  std::map<int, int> detids;
243  while (ieb != eeb) {
244  int iEta = ieb->getID1();
245  int iPhi = ieb->getID2();
246  count++;
247  EBDetId ebdetid(iEta,iPhi);
248  // unsigned int hieb = ebdetid.hashedIndex();
249  detids[ieb->getLogicID()] = ebdetid;
250  ieb++;
251  }
252  std::cout << "Validated " << count << " logic ID's for EB" << std::endl;
253 
254  // do the same for EE
255 
256  std::vector<EcalLogicID>::const_iterator iee = crystals_EE.begin();
257  std::vector<EcalLogicID>::const_iterator eee = crystals_EE.end();
258 
259  count = 0;
260  while (iee != eee) {
261  int iSide = iee->getID1();
262  int iX = iee->getID2();
263  int iY = iee->getID3();
264  EEDetId eedetidpos(iX,iY,iSide);
265  // int hi = eedetidpos.hashedIndex();
266  detids[iee->getLogicID()] = eedetidpos;
267  count ++;
268  iee++;
269  }
270  std::cout << "Validated " << count << " logic ID's for EE" << std::endl;
271 
272  // get association between ecal logic id and LMR
273  std::map<int, int> logicId2Lmr = econn->getEcalLogicID2LmrMap();
274 
275  std::cout << "Retrieving corrections from ONLINE DB ... " << std::endl;
276 
277  LMFCorrCoefDat data(econn);
278  if (m_debug) {
279  data.debug();
280  }
281  // get all data in the database taken after the last available time in ORCOFF
282  // we associate another map, whose key is the crystal ID and whose value is a
283  // sextuple (p1, p2, p3, t1, t2, t3)
284  Tm tmax;
285  if (m_maxtime[0] == '-') {
286  // this is a time relative to now
287  tmax.setToCurrentLocalTime();
288  if (m_debug) {
289  std::cout << "Subtracting " << m_maxtime.substr(1) << " hours "
290  << "to " << tmax.str() << std::endl;
291  std::cout << "tmax was " << tmax.microsTime() << " ns" << std::endl;
292  }
293  tmax -= atoi(m_maxtime.substr(1).c_str())*3600;//
294  if (m_debug) {
295  std::cout << "tmax is " << tmax.microsTime() << " ns" << std::endl;
296  }
297  } else {
298  if (m_debug) {
299  std::cout << "Setting t_max to " << m_maxtime << std::endl;
300  }
301  tmax.setToString(m_maxtime);
302  }
303  // Tm tmin = Tm((t_min.value() >> 32)*1000000);
304  Tm tmin = Tm(t_min.value());
305  /*
306  Tm strunz;
307  strunz.setToString("2011-04-11 20:50:08");
308  if (tmin < strunz) {
309  tmin = strunz;
310  }
311  */
312 
313  if (m_debug) {
314  std::cout << "Tmin: " << tmin << std::endl;
315  std::cout << "Tmax: " << tmax << std::endl;
316  }
317 
318  std::map<int, std::map<int, LMFSextuple> > d =
319  data.getCorrections(tmin, tmax, m_sequences);
320  // sice must be equal to the number of different SEQ_ID's found
321  std::cout << "Data organized into " << d.size() << " sequences" << std::endl;
322  // iterate over sequences
323  std::map<int, std::map<int, LMFSextuple> >::const_iterator iseq = d.begin();
324  std::map<int, std::map<int, LMFSextuple> >::const_iterator eseq = d.end();
325  std::cout << "===== Looping on Sequences" << std::endl;
326  while (iseq != eseq) {
327  std::cout << "==== SEQ_ID: " << iseq->first
328  << " contains " << iseq->second.size() << " crystals"
329  << std::endl << std::flush;
330  // iterate over crystals, but skip those sequences with wrong number of crystals
331  if (iseq->second.size() == (61200 + 14648)) {
332  std::map<int, LMFSextuple>::const_iterator is = iseq->second.begin();
333  std::map<int, LMFSextuple>::const_iterator es = iseq->second.end();
334  EcalLaserAPDPNRatios* apdpns_popcon = new EcalLaserAPDPNRatios();
335  Time_t t_last = 18446744073709551615ULL;
336  while (is != es) {
338  apdpnpair_temp.p1 = is->second.p[0];
339  apdpnpair_temp.p2 = is->second.p[1];
340  apdpnpair_temp.p3 = is->second.p[2];
342  timestamp_temp.t1 = edm::Timestamp(is->second.t[0].cmsNanoSeconds());
343  timestamp_temp.t2 = edm::Timestamp(is->second.t[1].cmsNanoSeconds());
344  timestamp_temp.t3 = edm::Timestamp(is->second.t[2].cmsNanoSeconds());
345  apdpns_popcon->setValue(detids[is->first], apdpnpair_temp);
346  if (logicId2Lmr.find(is->first) != logicId2Lmr.end()) {
347  int hashedIndex = logicId2Lmr[is->first] - 1;
348  if ((hashedIndex >= 0) && (hashedIndex <= 91)) {
349  apdpns_popcon->setTime( hashedIndex , timestamp_temp);
350  if (t_last > timestamp_temp.t1.value()) {
351  t_last = timestamp_temp.t1.value();
352  }
353  } else {
354  std::stringstream ss;
355  ss << "LOGIC_ID: " << is->first << " LMR: " << hashedIndex + 1
356  << " Out of range";
357  throw(std::runtime_error("[EcalLaserHandler::getNewObjects]" +
358  ss.str()));
359  }
360  } else {
361  std::stringstream ss;
362  ss << "LOGIC_ID: " << is->first << " Cannot determine LMR";
363  throw(std::runtime_error("[EcalLaserHandler::getNewObjects]" +
364  ss.str()));
365  }
366  is++;
367  }
368  if (m_fake) {
369  delete apdpns_popcon;
370  }
371  if ((iseq->second.size() > 0) && (!m_fake)) {
372  m_to_transfer.push_back(std::make_pair(apdpns_popcon,
373  Tm(t_last).cmsNanoSeconds()));
374  }
375  } else {
376  // Here we should put a warning
377  }
378  iseq++;
379  }
380  std::cout << "==== END OF LOOP ON SEQUENCES" << std::endl << std::flush;
381  delete econn;
382  std::cout << "Ecal -> end of getNewObjects -----------\n";
383 
384 
385 }
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
tuple ret
prodAgent to be discontinued
void dumpEndcapPayload(EcalLaserAPDPNRatios::EcalLaserAPDPNRatiosMap const &laserMap)
int ix() const
Definition: EEDetId.h:76
std::map< int, std::map< int, LMFSextuple > > getCorrections(const Tm &t)
const Items & barrelItems() const
void notifyProblems(const EcalLaserAPDPNRatios::EcalLaserAPDPNpair &old, const EcalLaserAPDPNRatios::EcalLaserAPDPNpair &current, int hashedIndex, const std::string &reason)
void setToCurrentLocalTime()
Definition: Tm.cc:171
static EEDetId unhashIndex(int hi)
Definition: EEDetId.cc:99
bool checkAPDPN(const EcalLaserAPDPNRatios::EcalLaserAPDPNpair &old, const EcalLaserAPDPNRatios::EcalLaserAPDPNpair &current, int hashedIndex)
list diff
Definition: mps_update.py:85
uint64_t microsTime() const
Definition: Tm.cc:126
bool checkAPDPNs(const EcalLaserAPDPNRatios::EcalLaserAPDPNRatiosMap &laserMap, const EcalLaserAPDPNRatios::EcalLaserAPDPNRatiosMap &apdpns_popcon)
int iphi() const
get the crystal iphi
Definition: EBDetId.h:53
tuple d
Definition: ztail.py:151
int hashedIndex(int ieta, int iphi)
Definition: EcalPyUtils.cc:42
EcalLaserHandler(edm::ParameterSet const &)
void setTime(int hashedIndex, const EcalLaserTimeStamp &value)
void setToString(const std::string s)
Definition: Tm.cc:193
EcalLogicID getEcalLogicID(std::string name, int id1=EcalLogicID::NULLID, int id2=EcalLogicID::NULLID, int id3=EcalLogicID::NULLID, std::string mapsTo="")
int zside() const
Definition: EEDetId.h:70
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
int iy() const
Definition: EEDetId.h:82
int ieta() const
get the crystal ieta
Definition: EBDetId.h:51
uint64_t cmsNanoSeconds() const
Definition: Tm.cc:116
static const double tmax[3]
std::vector< EcalLaserTimeStamp > EcalLaserTimeStampMap
std::vector< Item >::const_iterator const_iterator
double b
Definition: hdecay.h:120
void setValue(uint32_t rawId, const EcalLaserAPDPNpair &value)
static EBDetId unhashIndex(int hi)
get a DetId from a compact index for arrays
Definition: EBDetId.h:114
std::string str() const
Definition: Tm.cc:89
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
static const int NULLID
Definition: EcalLogicID.h:42
double a
Definition: hdecay.h:121
void dumpBarrelPayload(EcalLaserAPDPNRatios::EcalLaserAPDPNRatiosMap const &laserMap)
tuple cout
Definition: gather_cfg.py:145
const Item & barrel(size_t hashedIndex) const
const Items & endcapItems() const
const Item & endcap(size_t hashedIndex) const
Definition: Tm.h:13
double diff(float x, float old_x)
TimeValue_t value() const
Definition: Timestamp.h:56