CMS 3D CMS Logo

EcalTPGBadXTHandler.cc
Go to the documentation of this file.
2 
10 
12 
13 #include <iostream>
14 #include <fstream>
15 
16 #include <ctime>
17 #include <unistd.h>
18 
19 #include <string>
20 #include <cstdio>
21 #include <typeinfo>
22 #include <sstream>
23 
25  : m_name(ps.getUntrackedParameter<std::string>("name", "EcalTPGBadXTHandler")) {
26  edm::LogInfo("EcalTPGBadXTHandler") << "EcalTPGBadXT Source handler constructor.";
27  m_firstRun = static_cast<unsigned int>(atoi(ps.getParameter<std::string>("firstRun").c_str()));
28  m_lastRun = static_cast<unsigned int>(atoi(ps.getParameter<std::string>("lastRun").c_str()));
29  m_sid = ps.getParameter<std::string>("OnlineDBSID");
30  m_user = ps.getParameter<std::string>("OnlineDBUser");
31  m_pass = ps.getParameter<std::string>("OnlineDBPassword");
32  m_locationsource = ps.getParameter<std::string>("LocationSource");
33  m_location = ps.getParameter<std::string>("Location");
34  m_gentag = ps.getParameter<std::string>("GenTag");
35  m_runtype = ps.getParameter<std::string>("RunType");
36 
37  edm::LogInfo("EcalTPGBadXTHandler") << m_sid << "/" << m_user << "/" << m_location << "/" << m_gentag;
38 }
39 
41 
43  edm::LogInfo("EcalTPGBadXTHandler") << "Started GetNewObjects!!!";
44 
45  unsigned int max_since = 0;
46  max_since = static_cast<unsigned int>(tagInfo().lastInterval.since);
47  edm::LogInfo("EcalTPGBadXTHandler") << "max_since : " << max_since;
48  edm::LogInfo("EcalTPGBadXTHandler") << "retrieved last payload ";
49 
50  // here we retrieve all the runs after the last from online DB
51  edm::LogInfo("EcalTPGBadXTHandler") << "Retrieving run list from ONLINE DB ... ";
52 
53  edm::LogInfo("EcalTPGBadXTHandler") << "Making connection...";
54  econn = new EcalCondDBInterface(m_sid, m_user, m_pass);
55  edm::LogInfo("EcalTPGBadXTHandler") << "Done.";
56 
57  if (!econn) {
58  std::cout << " connection parameters " << m_sid << "/" << m_user << std::endl;
59  // cerr << e.what() << std::endl;
60  throw cms::Exception("OMDS not available");
61  }
62 
64  my_locdef.setLocation(m_location);
65 
67  my_rundef.setRunType(m_runtype);
68 
69  RunTag my_runtag;
70  my_runtag.setLocationDef(my_locdef);
71  my_runtag.setRunTypeDef(my_rundef);
72  my_runtag.setGeneralTag(m_gentag);
73 
74  readFromFile("last_tpg_badXT_settings.txt");
75 
76  unsigned int min_run;
77 
78  if (m_firstRun < m_i_run_number) {
79  min_run = m_i_run_number + 1;
80  } else {
81  min_run = m_firstRun;
82  }
83  if (min_run < max_since) {
84  min_run = max_since + 1; // we have to add 1 to the last transferred one
85  }
86 
87  std::cout << "m_i_run_number" << m_i_run_number << "m_firstRun " << m_firstRun << "max_since " << max_since
88  << std::endl;
89 
90  unsigned int max_run = m_lastRun;
91  edm::LogInfo("EcalTPGBadXTHandler") << "min_run= " << min_run << "max_run= " << max_run;
92 
93  RunList my_list;
94  my_list = econn->fetchGlobalRunListByLocation(my_runtag, min_run, max_run, my_locdef);
95  // my_list=econn->fetchRunListByLocation(my_runtag, min_run, max_run, my_locdef);
96 
97  std::vector<RunIOV> run_vec = my_list.getRuns();
98  size_t num_runs = run_vec.size();
99 
100  std::cout << "number of runs is : " << num_runs << std::endl;
101 
102  std::vector<EcalLogicID> my_EcalLogicId;
103  std::vector<EcalLogicID> my_EcalLogicId_EE;
104 
105  unsigned int irun = 0;
106  if (num_runs > 0) {
107  my_EcalLogicId = econn->getEcalLogicIDSetOrdered(
108  "ECAL_crystal_number_fedccuxt", 610, 650, 1, 100, 0, 100, "EB_crystal_number", 123);
109 
110  my_EcalLogicId_EE = econn->getEcalLogicIDSetOrdered(
111  "ECAL_crystal_number_fedccuxt", 600, 700, 1, 100, 0, 100, "EE_crystal_number", 123);
112 
113  for (size_t kr = 0; kr < run_vec.size(); kr++) {
114  std::cout << "here we are in run " << kr << std::endl;
115  irun = static_cast<unsigned int>(run_vec[kr].getRunNumber());
116 
117  std::cout << " **************** " << std::endl;
118  std::cout << " **************** " << std::endl;
119  std::cout << " run= " << irun << std::endl;
120 
121  // retrieve the data :
122  std::map<EcalLogicID, RunTPGConfigDat> dataset;
123  econn->fetchDataSet(&dataset, &run_vec[kr]);
124 
125  std::string the_config_tag = "";
126  int the_config_version = 0;
127 
128  std::map<EcalLogicID, RunTPGConfigDat>::const_iterator it;
129 
130  int nr = 0;
131  for (it = dataset.begin(); it != dataset.end(); it++) {
132  ++nr;
133  //EcalLogicID ecalid = it->first;
134 
135  RunTPGConfigDat dat = it->second;
136  the_config_tag = dat.getConfigTag();
137  the_config_version = dat.getVersion();
138  }
139 
140  // it is all the same for all SM... get the last one
141 
142  std::cout << " run= " << irun << " tag " << the_config_tag << " version=" << the_config_version << std::endl;
143 
144  // here we should check if it is the same as previous run.
145 
146  if ((the_config_tag != m_i_tag || the_config_version != m_i_version) && nr > 0) {
147  std::cout << "the tag is different from last transferred run ... retrieving last config set from DB"
148  << std::endl;
149 
150  FEConfigMainInfo fe_main_info;
151  fe_main_info.setConfigTag(the_config_tag);
152  fe_main_info.setVersion(the_config_version);
153  std::cout << " version=" << fe_main_info.getVersion() << std::endl;
154 
155  try {
156  std::cout << " before fetch config set" << std::endl;
157  econn->fetchConfigSet(&fe_main_info);
158  std::cout << " after fetch config set" << std::endl;
159 
160  // now get TPGBadXT
161  int badxtId = fe_main_info.getBxtId();
162 
163  if (badxtId != m_i_badXT && badxtId != 0) {
164  FEConfigBadXTInfo fe_badXt_info;
165  fe_badXt_info.setId(badxtId);
166  econn->fetchConfigSet(&fe_badXt_info);
167  std::vector<FEConfigBadXTDat> dataset_TpgBadXT;
168  econn->fetchConfigDataSet(&dataset_TpgBadXT, &fe_badXt_info);
169 
170  // NB new
171 
172  EcalTPGCrystalStatus* badXt;
173  badXt = produceEcalTrgChannelStatus();
174 
175  typedef std::vector<FEConfigBadXTDat>::const_iterator CIfeped;
176  EcalLogicID ecid_xt;
177  FEConfigBadXTDat rd_badXt;
178  int icells = 0;
179 
180  for (CIfeped p = dataset_TpgBadXT.begin(); p != dataset_TpgBadXT.end(); p++) {
181  rd_badXt = *p;
182 
183  int fed_id = rd_badXt.getFedId();
184  //int tcc_id=rd_badXt.getTCCId();
185  int tt_id = rd_badXt.getTTId();
186  int xt_id = rd_badXt.getXTId();
187 
188  // EB data
189  if (fed_id >= 610 && fed_id <= 645) {
190  // logic id is 1011ssxxxx
191  // get SM id
192  int sm_num = 0;
193  if (fed_id <= 627)
194  sm_num = fed_id - 609 + 18;
195  if (fed_id > 627)
196  sm_num = fed_id - 627;
197 
198  // get crystal id
199  int xt_num = 0;
200 
201  for (size_t ixt = 0; ixt < my_EcalLogicId.size(); ixt++) {
202  if (my_EcalLogicId[ixt].getID1() == fed_id && my_EcalLogicId[ixt].getID2() == tt_id &&
203  my_EcalLogicId[ixt].getID3() == xt_id) {
204  //1011060504
205  int ecid = my_EcalLogicId[ixt].getLogicID();
206  xt_num = (ecid) - (101100 + sm_num) * 10000;
207  }
208  }
209 
210  std::cout << " masking crystal " << sm_num << "/" << xt_num << " from fed/tt/xt" << fed_id << "/"
211  << tt_id << "/" << xt_id << std::endl;
212  if (sm_num == 0 && xt_num == 0) {
213  std::cout << " ERROR FOR crystal from fed/tt/xt" << fed_id << "/" << tt_id << "/" << xt_id
214  << std::endl;
215  }
216  EBDetId ebdetid(sm_num, xt_num, EBDetId::SMCRYSTALMODE);
217 
218  badXt->setValue(ebdetid.rawId(), rd_badXt.getStatus());
219  ++icells;
220  } else {
221  // EE data
222 
223  long x = 0;
224  long y = 0;
225  long z = 0;
226 
227  for (size_t ixt = 0; ixt < my_EcalLogicId_EE.size(); ixt++) {
228  if (my_EcalLogicId_EE[ixt].getID1() == fed_id && my_EcalLogicId_EE[ixt].getID2() == tt_id &&
229  my_EcalLogicId_EE[ixt].getID3() == xt_id) {
230  long ecid = (long)my_EcalLogicId_EE[ixt].getLogicID();
231  // logic_id 201Zxxxyyy Z=0 / 2 -> z= -1 / 1 , x -> 1 100, y -> 1 100
232  y = ecid - ((long)(ecid / 1000)) * 1000;
233  x = (ecid - y) / 1000;
234  x = x - ((long)(x / 1000)) * 1000;
235  z = (ecid - y - x * 1000) / 1000000 - 2010;
236  if (z == 0)
237  z = -1;
238  if (z == 2)
239  z = 1;
240  }
241  }
242 
243  if (x == 0 && y == 0 && z == 0) {
244  std::cout << " ERROR FOR crystal from fed/tt/xt" << fed_id << "/" << tt_id << "/" << xt_id
245  << std::endl;
246  }
247  EEDetId eedetid(x, y, z);
248  badXt->setValue(eedetid.rawId(), rd_badXt.getStatus());
249  ++icells;
250  }
251  } //end for over data
252 
253  edm::LogInfo("EcalTPGBadXTHandler") << "Finished badXT reading";
254 
255  Time_t snc = (Time_t)irun;
256  m_to_transfer.push_back(std::make_pair((EcalTPGCrystalStatus*)badXt, snc));
257 
258  m_i_run_number = irun;
259  m_i_tag = the_config_tag;
260  m_i_version = the_config_version;
261  m_i_badXT = badxtId;
262 
263  writeFile("last_tpg_badXT_settings.txt");
264 
265  } else {
266  m_i_run_number = irun;
267  m_i_tag = the_config_tag;
268  m_i_version = the_config_version;
269 
270  writeFile("last_tpg_badXT_settings.txt");
271 
272  std::cout << " even if the tag/version is not the same, the badXT id is the same -> no transfer needed "
273  << std::endl;
274  }
275 
276  }
277 
278  catch (std::exception& e) {
279  std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" << the_config_tag << " version=" << the_config_version
280  << std::endl;
281  std::cout << e.what() << std::endl;
282  m_i_run_number = irun;
283  }
284  std::cout << " **************** " << std::endl;
285 
286  } else if (nr == 0) {
287  m_i_run_number = irun;
288  std::cout << " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed " << std::endl;
289  std::cout << " **************** " << std::endl;
290  } else {
291  m_i_run_number = irun;
292  m_i_tag = the_config_tag;
293  m_i_version = the_config_version;
294  std::cout << " the tag/version is the same -> no transfer needed " << std::endl;
295  std::cout << " **************** " << std::endl;
296  writeFile("last_tpg_badXT_settings.txt");
297  }
298 
299  } //end for over kr (nr of runs)
300  } //end if
301 
302  delete econn;
303 
304  edm::LogInfo("EcalTPGBadXTHandler") << "Ecal - > end of getNewObjects -----------";
305 }
306 
308  //-------------------------------------------------------------
309 
310  m_i_tag = "";
311  m_i_version = 0;
312  m_i_run_number = 0;
313  m_i_badXT = 0;
314 
315  FILE* inpFile; // input file
316  inpFile = fopen(inputFile, "r");
317  if (!inpFile) {
318  edm::LogError("EcalTPGBadXTHandler") << "*** Can not open file: " << inputFile;
319  return;
320  }
321 
322  char line[256];
323 
324  std::ostringstream str;
325 
326  fgets(line, 255, inpFile);
327  m_i_tag = to_string(line);
328  str << "gen tag " << m_i_tag << std::endl; // should I use this?
329 
330  fgets(line, 255, inpFile);
331  m_i_version = atoi(line);
332  str << "version= " << m_i_version << std::endl;
333 
334  fgets(line, 255, inpFile);
335  m_i_run_number = atoi(line);
336  str << "run_number= " << m_i_run_number << std::endl;
337 
338  fgets(line, 255, inpFile);
339  m_i_badXT = atoi(line);
340  str << "badXT_config= " << m_i_badXT << std::endl;
341 
342  fclose(inpFile); // close inp. file
343 }
344 
346  //-------------------------------------------------------------
347 
348  std::ofstream myfile;
349  myfile.open(inputFile);
350  myfile << m_i_tag << std::endl;
351  myfile << m_i_version << std::endl;
352  myfile << m_i_run_number << std::endl;
353  myfile << m_i_badXT << std::endl;
354 
355  myfile.close();
356 }
357 
360  // barrel
361  for (int ieta = -EBDetId::MAX_IETA; ieta <= EBDetId::MAX_IETA; ++ieta) {
362  if (ieta == 0)
363  continue;
364  for (int iphi = EBDetId::MIN_IPHI; iphi <= EBDetId::MAX_IPHI; ++iphi) {
365  if (EBDetId::validDetId(ieta, iphi)) {
366  EBDetId ebid(ieta, iphi);
367  ical->setValue(ebid, 0);
368  }
369  }
370  }
371  // endcap
372  for (int iX = EEDetId::IX_MIN; iX <= EEDetId::IX_MAX; ++iX) {
373  for (int iY = EEDetId::IY_MIN; iY <= EEDetId::IY_MAX; ++iY) {
374  // make an EEDetId since we need EEDetId::rawId() to be used as the key for the pedestals
375  if (EEDetId::validDetId(iX, iY, 1)) {
376  EEDetId eedetidpos(iX, iY, 1);
377  ical->setValue(eedetidpos, 0);
378  }
379  if (EEDetId::validDetId(iX, iY, -1)) {
380  EEDetId eedetidneg(iX, iY, -1);
381  ical->setValue(eedetidneg, 0);
382  }
383  }
384  }
385  return ical;
386 }
void setRunTypeDef(const RunTypeDef &runTypeDef)
Definition: RunTag.cc:42
std::vector< RunIOV > getRuns()
Definition: RunList.cc:25
int getFedId() const
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
static const int MIN_IPHI
Definition: EBDetId.h:135
void writeFile(const char *inputFile)
int getVersion() const
int getStatus() const
Definition: RunTag.h:13
std::string to_string(const V &value)
Definition: OMSAccess.h:71
int getVersion() const
Log< level::Error, false > LogError
void setVersion(int id)
static const int IX_MIN
Definition: EEDetId.h:290
void readFromFile(CaloCluster &c, FILE *file)
static const int IY_MIN
Definition: EEDetId.h:294
static bool validDetId(int i, int j)
check if a valid index combination
Definition: EBDetId.h:118
void setValue(const uint32_t id, const Item &item)
int getXTId() const
EcalTPGCrystalStatusMap EcalTPGCrystalStatus
int getTTId() const
void setLocationDef(const LocationDef &locDef)
Definition: RunTag.cc:33
EcalTPGCrystalStatus * produceEcalTrgChannelStatus()
static const int IX_MAX
Definition: EEDetId.h:298
EcalTPGBadXTHandler(edm::ParameterSet const &)
Log< level::Info, false > LogInfo
static const int MAX_IPHI
Definition: EBDetId.h:137
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:57
static bool validDetId(int crystal_ix, int crystal_iy, int iz)
Definition: EEDetId.h:248
int getBxtId() const
static const int MAX_IETA
Definition: EBDetId.h:136
std::string getConfigTag() const
cond::Time_t Time_t
Definition: Time.h:18
void setConfigTag(std::string x)
Definition: IODConfig.h:29
static const int IY_MAX
Definition: EEDetId.h:302
void setGeneralTag(std::string tag)
Definition: RunTag.cc:24
static const int SMCRYSTALMODE
Definition: EBDetId.h:159
#define str(s)
void readFromFile(const char *inputFile)