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  tmax.setToString(m_maxtime);
299  }
300  // Tm tmin = Tm((t_min.value() >> 32)*1000000);
301  Tm tmin = Tm(t_min.value());
302  /*
303  Tm strunz;
304  strunz.setToString("2011-04-11 20:50:08");
305  if (tmin < strunz) {
306  tmin = strunz;
307  }
308  */
309 
310  if (m_debug) {
311  std::cout << "Tmin: " << tmin << std::endl;
312  std::cout << "Tmax: " << tmax << std::endl;
313  }
314 
315  std::map<int, std::map<int, LMFSextuple> > d =
316  data.getCorrections(tmin, tmax, m_sequences);
317  // sice must be equal to the number of different SEQ_ID's found
318  std::cout << "Data organized into " << d.size() << " sequences" << std::endl;
319  // iterate over sequences
320  std::map<int, std::map<int, LMFSextuple> >::const_iterator iseq = d.begin();
321  std::map<int, std::map<int, LMFSextuple> >::const_iterator eseq = d.end();
322  std::cout << "===== Looping on Sequences" << std::endl;
323  while (iseq != eseq) {
324  std::cout << "==== SEQ_ID: " << iseq->first
325  << " contains " << iseq->second.size() << " crystals"
326  << std::endl << std::flush;
327  // iterate over crystals, but skip those sequences with wrong number of crystals
328  if (iseq->second.size() == (61200 + 14648)) {
329  std::map<int, LMFSextuple>::const_iterator is = iseq->second.begin();
330  std::map<int, LMFSextuple>::const_iterator es = iseq->second.end();
331  EcalLaserAPDPNRatios* apdpns_popcon = new EcalLaserAPDPNRatios();
332  Time_t t_last = 18446744073709551615ULL;
333  while (is != es) {
335  apdpnpair_temp.p1 = is->second.p[0];
336  apdpnpair_temp.p2 = is->second.p[1];
337  apdpnpair_temp.p3 = is->second.p[2];
339  timestamp_temp.t1 = edm::Timestamp(is->second.t[0].cmsNanoSeconds());
340  timestamp_temp.t2 = edm::Timestamp(is->second.t[1].cmsNanoSeconds());
341  timestamp_temp.t3 = edm::Timestamp(is->second.t[2].cmsNanoSeconds());
342  apdpns_popcon->setValue(detids[is->first], apdpnpair_temp);
343  if (logicId2Lmr.find(is->first) != logicId2Lmr.end()) {
344  int hashedIndex = logicId2Lmr[is->first] - 1;
345  if ((hashedIndex >= 0) && (hashedIndex <= 91)) {
346  apdpns_popcon->setTime( hashedIndex , timestamp_temp);
347  if (t_last > timestamp_temp.t1.value()) {
348  t_last = timestamp_temp.t1.value();
349  }
350  } else {
351  std::stringstream ss;
352  ss << "LOGIC_ID: " << is->first << " LMR: " << hashedIndex + 1
353  << " Out of range";
354  throw(std::runtime_error("[EcalLaserHandler::getNewObjects]" +
355  ss.str()));
356  }
357  } else {
358  std::stringstream ss;
359  ss << "LOGIC_ID: " << is->first << " Cannot determine LMR";
360  throw(std::runtime_error("[EcalLaserHandler::getNewObjects]" +
361  ss.str()));
362  }
363  is++;
364  }
365  if (m_fake) {
366  delete apdpns_popcon;
367  }
368  if ((iseq->second.size() > 0) && (!m_fake)) {
369  m_to_transfer.push_back(std::make_pair(apdpns_popcon,
370  Tm(t_last).cmsNanoSeconds()));
371  }
372  } else {
373  // Here we should put a warning
374  }
375  iseq++;
376  }
377  std::cout << "==== END OF LOOP ON SEQUENCES" << std::endl << std::flush;
378  delete econn;
379  std::cout << "Ecal -> end of getNewObjects -----------\n";
380 
381 
382 }
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
void dumpEndcapPayload(EcalLaserAPDPNRatios::EcalLaserAPDPNRatiosMap const &laserMap)
int ix() const
Definition: EEDetId.h:71
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)
#define abs(x)
Definition: mlp_lapack.h:159
void setToCurrentLocalTime()
Definition: Tm.cc:171
static EEDetId unhashIndex(int hi)
Definition: EEDetId.cc:115
bool checkAPDPN(const EcalLaserAPDPNRatios::EcalLaserAPDPNpair &old, const EcalLaserAPDPNRatios::EcalLaserAPDPNpair &current, int hashedIndex)
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:46
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:65
int iy() const
Definition: EEDetId.h:77
int ieta() const
get the crystal ieta
Definition: EBDetId.h:44
uint64_t cmsNanoSeconds() const
Definition: Tm.cc:116
TimeValue_t value() const
Definition: Timestamp.cc:72
static const double tmax[3]
std::vector< EcalLaserTimeStamp > EcalLaserTimeStampMap
std::vector< Item >::const_iterator const_iterator
static EBDetId unhashIndex(int hi)
get a DetId from a compact index for arrays
Definition: EBDetId.cc:12
double b
Definition: hdecay.h:120
void setValue(uint32_t rawId, const EcalLaserAPDPNpair &value)
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:43
double a
Definition: hdecay.h:121
void dumpBarrelPayload(EcalLaserAPDPNRatios::EcalLaserAPDPNRatiosMap const &laserMap)
tuple cout
Definition: gather_cfg.py:121
const Item & barrel(size_t hashedIndex) const
const Items & endcapItems() const
const Item & endcap(size_t hashedIndex) const
Definition: Tm.h:14
double diff(float x, float old_x)