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 
179  for (CIfeped p = dataset_TpgBadXT.begin(); p != dataset_TpgBadXT.end(); p++) {
180  rd_badXt = *p;
181 
182  int fed_id = rd_badXt.getFedId();
183  //int tcc_id=rd_badXt.getTCCId();
184  int tt_id = rd_badXt.getTTId();
185  int xt_id = rd_badXt.getXTId();
186 
187  // EB data
188  if (fed_id >= 610 && fed_id <= 645) {
189  // logic id is 1011ssxxxx
190  // get SM id
191  int sm_num = 0;
192  if (fed_id <= 627)
193  sm_num = fed_id - 609 + 18;
194  if (fed_id > 627)
195  sm_num = fed_id - 627;
196 
197  // get crystal id
198  int xt_num = 0;
199 
200  for (size_t ixt = 0; ixt < my_EcalLogicId.size(); ixt++) {
201  if (my_EcalLogicId[ixt].getID1() == fed_id && my_EcalLogicId[ixt].getID2() == tt_id &&
202  my_EcalLogicId[ixt].getID3() == xt_id) {
203  //1011060504
204  int ecid = my_EcalLogicId[ixt].getLogicID();
205  xt_num = (ecid) - (101100 + sm_num) * 10000;
206  }
207  }
208 
209  std::cout << " masking crystal " << sm_num << "/" << xt_num << " from fed/tt/xt" << fed_id << "/"
210  << tt_id << "/" << xt_id << std::endl;
211  if (sm_num == 0 && xt_num == 0) {
212  std::cout << " ERROR FOR crystal from fed/tt/xt" << fed_id << "/" << tt_id << "/" << xt_id
213  << std::endl;
214  }
215  EBDetId ebdetid(sm_num, xt_num, EBDetId::SMCRYSTALMODE);
216 
217  badXt->setValue(ebdetid.rawId(), rd_badXt.getStatus());
218  } else {
219  // EE data
220 
221  long x = 0;
222  long y = 0;
223  long z = 0;
224 
225  for (size_t ixt = 0; ixt < my_EcalLogicId_EE.size(); ixt++) {
226  if (my_EcalLogicId_EE[ixt].getID1() == fed_id && my_EcalLogicId_EE[ixt].getID2() == tt_id &&
227  my_EcalLogicId_EE[ixt].getID3() == xt_id) {
228  long ecid = (long)my_EcalLogicId_EE[ixt].getLogicID();
229  // logic_id 201Zxxxyyy Z=0 / 2 -> z= -1 / 1 , x -> 1 100, y -> 1 100
230  y = ecid - ((long)(ecid / 1000)) * 1000;
231  x = (ecid - y) / 1000;
232  x = x - ((long)(x / 1000)) * 1000;
233  z = (ecid - y - x * 1000) / 1000000 - 2010;
234  if (z == 0)
235  z = -1;
236  if (z == 2)
237  z = 1;
238  }
239  }
240 
241  if (x == 0 && y == 0 && z == 0) {
242  std::cout << " ERROR FOR crystal from fed/tt/xt" << fed_id << "/" << tt_id << "/" << xt_id
243  << std::endl;
244  }
245  EEDetId eedetid(x, y, z);
246  badXt->setValue(eedetid.rawId(), rd_badXt.getStatus());
247  }
248  } //end for over data
249 
250  edm::LogInfo("EcalTPGBadXTHandler") << "Finished badXT reading";
251 
252  Time_t snc = (Time_t)irun;
253  m_to_transfer.push_back(std::make_pair((EcalTPGCrystalStatus*)badXt, snc));
254 
255  m_i_run_number = irun;
256  m_i_tag = the_config_tag;
257  m_i_version = the_config_version;
258  m_i_badXT = badxtId;
259 
260  writeFile("last_tpg_badXT_settings.txt");
261 
262  } else {
263  m_i_run_number = irun;
264  m_i_tag = the_config_tag;
265  m_i_version = the_config_version;
266 
267  writeFile("last_tpg_badXT_settings.txt");
268 
269  std::cout << " even if the tag/version is not the same, the badXT id is the same -> no transfer needed "
270  << std::endl;
271  }
272 
273  }
274 
275  catch (std::exception& e) {
276  std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" << the_config_tag << " version=" << the_config_version
277  << std::endl;
278  std::cout << e.what() << std::endl;
279  m_i_run_number = irun;
280  }
281  std::cout << " **************** " << std::endl;
282 
283  } else if (nr == 0) {
284  m_i_run_number = irun;
285  std::cout << " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed " << std::endl;
286  std::cout << " **************** " << std::endl;
287  } else {
288  m_i_run_number = irun;
289  m_i_tag = the_config_tag;
290  m_i_version = the_config_version;
291  std::cout << " the tag/version is the same -> no transfer needed " << std::endl;
292  std::cout << " **************** " << std::endl;
293  writeFile("last_tpg_badXT_settings.txt");
294  }
295 
296  } //end for over kr (nr of runs)
297  } //end if
298 
299  delete econn;
300 
301  edm::LogInfo("EcalTPGBadXTHandler") << "Ecal - > end of getNewObjects -----------";
302 }
303 
305  //-------------------------------------------------------------
306 
307  m_i_tag = "";
308  m_i_version = 0;
309  m_i_run_number = 0;
310  m_i_badXT = 0;
311 
312  FILE* inpFile; // input file
313  inpFile = fopen(inputFile, "r");
314  if (!inpFile) {
315  edm::LogError("EcalTPGBadXTHandler") << "*** Can not open file: " << inputFile;
316  return;
317  }
318 
319  char line[256];
320 
321  std::ostringstream str;
322 
323  fgets(line, 255, inpFile);
324  m_i_tag = to_string(line);
325  str << "gen tag " << m_i_tag << std::endl; // should I use this?
326 
327  fgets(line, 255, inpFile);
328  m_i_version = atoi(line);
329  str << "version= " << m_i_version << std::endl;
330 
331  fgets(line, 255, inpFile);
332  m_i_run_number = atoi(line);
333  str << "run_number= " << m_i_run_number << std::endl;
334 
335  fgets(line, 255, inpFile);
336  m_i_badXT = atoi(line);
337  str << "badXT_config= " << m_i_badXT << std::endl;
338 
339  fclose(inpFile); // close inp. file
340 }
341 
343  //-------------------------------------------------------------
344 
345  std::ofstream myfile;
346  myfile.open(inputFile);
347  myfile << m_i_tag << std::endl;
348  myfile << m_i_version << std::endl;
349  myfile << m_i_run_number << std::endl;
350  myfile << m_i_badXT << std::endl;
351 
352  myfile.close();
353 }
354 
357  // barrel
358  for (int ieta = -EBDetId::MAX_IETA; ieta <= EBDetId::MAX_IETA; ++ieta) {
359  if (ieta == 0)
360  continue;
361  for (int iphi = EBDetId::MIN_IPHI; iphi <= EBDetId::MAX_IPHI; ++iphi) {
362  if (EBDetId::validDetId(ieta, iphi)) {
363  EBDetId ebid(ieta, iphi);
364  ical->setValue(ebid, 0);
365  }
366  }
367  }
368  // endcap
369  for (int iX = EEDetId::IX_MIN; iX <= EEDetId::IX_MAX; ++iX) {
370  for (int iY = EEDetId::IY_MIN; iY <= EEDetId::IY_MAX; ++iY) {
371  // make an EEDetId since we need EEDetId::rawId() to be used as the key for the pedestals
372  if (EEDetId::validDetId(iX, iY, 1)) {
373  EEDetId eedetidpos(iX, iY, 1);
374  ical->setValue(eedetidpos, 0);
375  }
376  if (EEDetId::validDetId(iX, iY, -1)) {
377  EEDetId eedetidneg(iX, iY, -1);
378  ical->setValue(eedetidneg, 0);
379  }
380  }
381  }
382  return ical;
383 }
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
int getVersion() const
Log< level::Error, false > LogError
void setVersion(int id)
static const int IX_MIN
Definition: EEDetId.h:290
static std::string to_string(const XMLCh *ch)
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)