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 
20  m_sid= ps.getParameter<std::string>("OnlineDBSID");
21  m_user= ps.getParameter<std::string>("OnlineDBUser");
22  m_pass= ps.getParameter<std::string>("OnlineDBPassword");
23  m_debug=ps.getParameter<bool>("debug");
24  m_sequences=static_cast<unsigned int>(atoi( ps.getParameter<std::string>("sequences").c_str()));
25  m_maxtime=ps.getParameter<std::string>("maxtime").c_str();
26  std::cout << "Starting O2O process on DB: " << m_sid
27  << " User: "<< m_user << std::endl;
28 }
29 
31 {
32  // do nothing
33 }
34 
35 double popcon::EcalLaserHandler::diff(float a, float b) {
36  return std::abs(b- a)/a;
37 }
38 
41  int hashedIndex, const std::string &reason) {
42  std::cout << "===== " << reason << " =====" << std::endl;
43  if (hashedIndex < 0) {
44  EEDetId ee;
45  std::cout << "Triplets for " << ee.unhashIndex(-hashedIndex) << " bad: [" << old.p1 << ", "
46  << old.p2 << ", " << old.p3 << "] ==> [" << current.p1 << ", "
47  << current.p2 << ", " << current.p3 << "]" << std::endl;
48  } else {
49  EBDetId eb;
50  std::cout << "Triplets for " << eb.unhashIndex(hashedIndex) << " bad: [" << old.p1 << ", "
51  << old.p2 << ", " << old.p3 << "] ==> [" << current.p1 << ", "
52  << current.p2 << ", " << current.p3 << "]" << std::endl;
53  }
54 }
55 
58  int hashedIndex) {
59  bool ret = true;
60  if ((current.p1 < 0) || (current.p2 < 0) || (current.p3 < 0)) {
61  ret = false;
62  notifyProblems(old, current, hashedIndex, "Negative values");
63  } else if ((current.p1 > 10) || (current.p2 > 10) || (current.p3 > 0)) {
64  ret = false;
65  notifyProblems(old, current, hashedIndex, "Values too large");
66  } else if (((diff(old.p1, current.p1) > 0.2) && (old.p1 != 0) && (old.p1 != 1)) ||
67  ((diff(old.p2, current.p2) > 0.2) && (old.p2 != 0) && (old.p1 != 2)) ||
68  ((diff(old.p3, current.p3) > 0.2) && (old.p3 != 0) && (old.p1 != 3))) {
69  ret = false;
70  notifyProblems(old, current, hashedIndex, "Difference w.r.t. previous too large");
71  }
72  return ret;
73 }
74 
76  const EcalLaserAPDPNRatios::EcalLaserAPDPNRatiosMap &apdpns_popcon) {
77  bool ret = true;
78  for (int hashedIndex = 0; hashedIndex < 61200; hashedIndex++) {
81  ret = checkAPDPN(old, current, hashedIndex);
82  }
83  for (int hashedIndex = 0; hashedIndex < 14648; hashedIndex++) {
86  ret = checkAPDPN(old, current, -hashedIndex);
87  }
88  return ret;
89 }
90 
92  int c = 0;
95  EBDetId eb;
96  try {
97  EcalCondDBInterface *econn = new EcalCondDBInterface( m_sid, m_user, m_pass );
98  while (i != e) {
99  if (c % 1000 == 0) {
100  std::cout << std::setw(5) << c << ": " << eb.unhashIndex(c) << " "
101  << econn->getEcalLogicID("EB_crystal_angle", eb.unhashIndex(c).ieta(),
103  "EB_crystal_number").getLogicID()
104  << " " << std::setiosflags(std::ios::fixed) << std::setprecision(9)
105  << i->p1 << " " << i->p2 << " " << i->p3 << std::endl;
106  }
107  i++;
108  c++;
109  }
110  delete econn;
111  }
112  catch (std::runtime_error &e) {
113  std::cerr << e.what() << std::endl;
114  delete econn;
115  throw cms::Exception("OMDS not available");
116  }
117 }
118 
120  int c = 0;
123  EEDetId ee;
124  try {
125  EcalCondDBInterface *econn = new EcalCondDBInterface( m_sid, m_user, m_pass );
126  while (i != e) {
127  if (c % 1000 == 0) {
128  std::cout << std::setw(5) << c << ": " << ee.unhashIndex(c) << " "
129  << econn->getEcalLogicID("EE_crystal_number", ee.unhashIndex(c).zside(),
130  ee.unhashIndex(c).ix(), ee.unhashIndex(c).iy(),
131  "EE_crystal_number").getLogicID()
132  << " " << std::setiosflags(std::ios::fixed) << std::setprecision(9)
133  << i->p1 << " " << i->p2 << " " << i->p3 << std::endl;
134  }
135  i++;
136  c++;
137  }
138  delete econn;
139  }
140  catch (std::runtime_error &e) {
141  std::cerr << e.what() << std::endl;
142  delete econn;
143  throw cms::Exception("OMDS not available");
144  }
145 }
146 
148 {
149  std::cerr << "------- " << m_name
150  << " ---> getNewObjects" << std::endl;
151 
152  std::cout << "------- Ecal -> getNewObjects\n";
153 
154 
155  unsigned long long max_since= 1;
156  Ref payload= lastPayload();
157 
158  // here popcon tells us which is the last since of the last object in the
159  // offline DB
160  max_since=tagInfo().lastInterval.first;
161  Tm max_since_tm(max_since);
162 
163  // get the last object in the orcoff
164  edm::Timestamp t_min= edm::Timestamp(18446744073709551615ULL);
165 
166  const EcalLaserAPDPNRatios::EcalLaserAPDPNRatiosMap& laserRatiosMap =
167  payload->getLaserMap();
168  std::cout << "payload->getLaserMap(): OK " << std::endl;
169  std::cout << "Its size is " << laserRatiosMap.size() << std::endl;
170  const EcalLaserAPDPNRatios::EcalLaserTimeStampMap& laserTimeMap =
171  payload->getTimeMap();
172  std::cout << "payload->getTimeMap(): OK " << std::endl;
173  std::cout << "Last Object in Offline DB has SINCE = " << max_since
174  << " (" << max_since_tm.str() << ")"
175  << " and SIZE = " << tagInfo().size
176  << std::endl;
177  // loop through light modules and determine the minimum date among the
178  // available channels
179  dumpBarrelPayload(laserRatiosMap);
180  dumpEndcapPayload(laserRatiosMap);
181  for (int i=0; i<92; i++) {
183  if( t_min > timestamp.t1) {
184  t_min=timestamp.t1;
185  }
186  }
187 
188  std::cout <<"WOW: we just retrieved the last valid record from DB "
189  << std::endl;
190  std::cout <<"Its tmin is "<< Tm(t_min.value()).str() << std::endl;
191 
192  // connect to the database
193  try {
194  std::cout << "Making connection..." << std::flush;
195  econn = new EcalCondDBInterface( m_sid, m_user, m_pass );
196  std::cout << "Done." << std::endl;
197  } catch (std::runtime_error &e) {
198  std::cout << " connection parameters " << m_sid << "/" << m_user;
199  if (m_debug) {
200  std::cout << "/" << m_pass <<std::endl;
201  } else {
202  std::cout << "/**********" <<std::endl;
203  }
204  std::cerr << e.what() << std::endl;
205  throw cms::Exception("OMDS not available");
206  }
207 
208  // retrieve the lists of logic_ids, to build the detids
209  std::vector<EcalLogicID> crystals_EB =
210  econn->getEcalLogicIDSetOrdered( "EB_crystal_angle",
211  -85,85,1,360,
213  "EB_crystal_number", 4 );
214  std::vector<EcalLogicID> crystals_EE =
215  econn->getEcalLogicIDSetOrdered( "EE_crystal_number",
216  -1,1,1,100,
217  1,100,
218  "EE_crystal_number", 4 );
219 
220  std::vector<EcalLogicID>::const_iterator ieb = crystals_EB.begin();
221  std::vector<EcalLogicID>::const_iterator eeb = crystals_EB.end();
222 
223  std::cout << "Got list of " << crystals_EB.size() << " crystals in EB"
224  << std::endl;
225  std::cout << "Got list of " << crystals_EE.size() << " crystals in EE"
226  << std::endl;
227  // loop through barrel
228  int count = 0;
229  // prepare a map to associate EB logic id's to detids
230  std::map<int, int> detids;
231  while (ieb != eeb) {
232  int iEta = ieb->getID1();
233  int iPhi = ieb->getID2();
234  count++;
235  EBDetId ebdetid(iEta,iPhi);
236  // unsigned int hieb = ebdetid.hashedIndex();
237  detids[ieb->getLogicID()] = ebdetid;
238  ieb++;
239  }
240  std::cout << "Validated " << count << " logic ID's for EB" << std::endl;
241 
242  // do the same for EE
243 
244  std::vector<EcalLogicID>::const_iterator iee = crystals_EE.begin();
245  std::vector<EcalLogicID>::const_iterator eee = crystals_EE.end();
246 
247  count = 0;
248  while (iee != eee) {
249  int iSide = iee->getID1();
250  int iX = iee->getID2();
251  int iY = iee->getID3();
252  EEDetId eedetidpos(iX,iY,iSide);
253  // int hi = eedetidpos.hashedIndex();
254  detids[iee->getLogicID()] = eedetidpos;
255  count ++;
256  iee++;
257  }
258  std::cout << "Validated " << count << " logic ID's for EE" << std::endl;
259 
260  // get association between ecal logic id and LMR
261  std::map<int, int> logicId2Lmr = econn->getEcalLogicID2LmrMap();
262 
263  std::cout << "Retrieving corrections from ONLINE DB ... " << std::endl;
264 
265  LMFCorrCoefDat data(econn);
266  if (m_debug) {
267  data.debug();
268  }
269  // get all data in the database taken after the last available time in ORCOFF
270  // we associate another map, whose key is the crystal ID and whose value is a
271  // sextuple (p1, p2, p3, t1, t2, t3)
272  Tm tmax;
273  tmax.setToString(m_maxtime);
274  Tm tmin = Tm(t_min.value());
275  // GO DEBUGGING
276  Tm tgo;
277  tgo.setToString("2011-02-24 23:59:59");
278 
279  if (tmin.microsTime() < tgo.microsTime()) {
280  tmin.setToMicrosTime(tgo.microsTime());
281  }
282  std::map<int, std::map<int, LMFSextuple> > d =
283  data.getCorrections(tmin, tmax, m_sequences);
284  // sice must be equal to the number of different SEQ_ID's found
285  std::cout << "Data organized into " << d.size() << " sequences" << std::endl;
286  // iterate over sequences
287  std::map<int, std::map<int, LMFSextuple> >::const_iterator iseq = d.begin();
288  std::map<int, std::map<int, LMFSextuple> >::const_iterator eseq = d.end();
289  std::cout << "===== Looping on Sequences" << std::endl;
290  while (iseq != eseq) {
291  std::cout << "==== SEQ_ID: " << iseq->first
292  << " contains " << iseq->second.size() << " crystals"
293  << std::endl << std::flush;
294  // iterate over crystals
295  std::map<int, LMFSextuple>::const_iterator is = iseq->second.begin();
296  std::map<int, LMFSextuple>::const_iterator es = iseq->second.end();
297  EcalLaserAPDPNRatios* apdpns_popcon = new EcalLaserAPDPNRatios();
298  Time_t t_last = t_min.value();
299  while (is != es) {
301  apdpnpair_temp.p1 = is->second.p[0];
302  apdpnpair_temp.p2 = is->second.p[1];
303  apdpnpair_temp.p3 = is->second.p[2];
305  timestamp_temp.t1 = edm::Timestamp(is->second.t[0].microsTime());
306  timestamp_temp.t2 = edm::Timestamp(is->second.t[1].microsTime());
307  timestamp_temp.t3 = edm::Timestamp(is->second.t[2].microsTime());
308  apdpns_popcon->setValue(detids[is->first], apdpnpair_temp);
309  if (logicId2Lmr.find(is->first) != logicId2Lmr.end()) {
310  int hashedIndex = logicId2Lmr[is->first] - 1;
311  if ((hashedIndex >= 0) && (hashedIndex <= 91)) {
312  apdpns_popcon->setTime( hashedIndex , timestamp_temp);
313  t_last = timestamp_temp.t1.value();
314  } else {
315  std::stringstream ss;
316  ss << "LOGIC_ID: " << is->first << " LMR: " << hashedIndex + 1
317  << " Out of range";
318  throw(std::runtime_error("[EcalLaserHandler::getNewObjects]" +
319  ss.str()));
320  }
321  } else {
322  std::stringstream ss;
323  ss << "LOGIC_ID: " << is->first << " Cannot determine LMR";
324  throw(std::runtime_error("[EcalLaserHandler::getNewObjects]" +
325  ss.str()));
326  }
327  is++;
328  }
329  if (iseq->second.size() > 0) {
330  m_to_transfer.push_back(std::make_pair(apdpns_popcon, t_last));
331  }
332  iseq++;
333  }
334  std::cout << "==== END OF LOOP ON SEQUENCES" << std::endl << std::flush;
335  delete econn;
336  std::cout << "Ecal -> end of getNewObjects -----------\n";
337 
338 
339 }
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)
string reason
Definition: Association.py:132
#define abs(x)
Definition: mlp_lapack.h:159
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:109
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 setToMicrosTime(uint64_t micros)
Definition: Tm.cc:141
void setToString(const std::string s)
Definition: Tm.cc:172
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
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: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:41
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)