CMS 3D CMS Logo

EcalTPGBadTTHandler.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", "EcalTPGBadTTHandler")) {
26  edm::LogInfo("EcalTPGBadTTHandler") << "EcalTPGTowerStatus 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("EcalTPGBadTTHandler") << m_sid << "/" << m_user << "/" << m_location << "/" << m_gentag;
38 }
39 
41 
43  edm::LogInfo("EcalTPGBadTTHandler") << "Started GetNewObjects!!!";
44 
45  unsigned int max_since = 0;
46  max_since = static_cast<unsigned int>(tagInfo().lastInterval.since);
47  edm::LogInfo("EcalTPGBadTTHandler") << "max_since : " << max_since;
48  edm::LogInfo("EcalTPGBadTTHandler") << "retrieved last payload ";
49 
50  // here we retrieve all the runs after the last from online DB
51  edm::LogInfo("EcalTPGBadTTHandler") << "Retrieving run list from ONLINE DB ... ";
52 
53  edm::LogInfo("EcalTPGBadTTHandler") << "Making connection...";
54  econn = new EcalCondDBInterface(m_sid, m_user, m_pass);
55  edm::LogInfo("EcalTPGBadTTHandler") << "Done.";
56 
57  if (!econn) {
58  std::cout << " connection parameters " << m_sid << "/" << m_user << std::endl;
59  throw cms::Exception("OMDS not available");
60  }
61 
63  my_locdef.setLocation(m_location);
64 
66  my_rundef.setRunType(m_runtype);
67 
68  RunTag my_runtag;
69  my_runtag.setLocationDef(my_locdef);
70  my_runtag.setRunTypeDef(my_rundef);
71  my_runtag.setGeneralTag(m_gentag);
72 
73  readFromFile("last_tpg_badTT_settings.txt");
74 
75  unsigned int min_run;
76 
77  if (m_firstRun < m_i_run_number) {
78  min_run = m_i_run_number + 1;
79  } else {
80  min_run = m_firstRun;
81  }
82  if (min_run < max_since) {
83  min_run = max_since + 1; // we have to add 1 to the last transferred one
84  }
85 
86  std::cout << "m_i_run_number" << m_i_run_number << "m_firstRun " << m_firstRun << "max_since " << max_since
87  << std::endl;
88 
89  unsigned int max_run = m_lastRun;
90  edm::LogInfo("EcalTPGBadTTHandler") << "min_run= " << min_run << "max_run= " << max_run;
91 
92  RunList my_list;
93  // my_list=econn->fetchRunListByLocation(my_runtag, min_run, max_run, my_locdef);
94  my_list = econn->fetchGlobalRunListByLocation(my_runtag, min_run, max_run, my_locdef);
95 
96  std::vector<RunIOV> run_vec = my_list.getRuns();
97  size_t num_runs = run_vec.size();
98 
99  std::cout << "number of runs is : " << num_runs << std::endl;
100 
101  std::string str = "";
102 
103  unsigned int irun = 0;
104  if (num_runs > 0) {
105  // going to query the ecal logic id
106  std::vector<EcalLogicID> my_TTEcalLogicId_EE;
107  my_TTEcalLogicId_EE = econn->getEcalLogicIDSetOrdered(
108  "EE_trigger_tower", 1, 200, 1, 70, EcalLogicID::NULLID, EcalLogicID::NULLID, "EE_offline_towerid", 12);
109  std::cout << " GOT the logic ID for the EE trigger towers " << std::endl;
110 
111  for (size_t kr = 0; kr < run_vec.size(); kr++) {
112  irun = static_cast<unsigned int>(run_vec[kr].getRunNumber());
113 
114  // retrieve the data :
115  std::map<EcalLogicID, RunTPGConfigDat> dataset;
116  econn->fetchDataSet(&dataset, &run_vec[kr]);
117 
118  std::string the_config_tag = "";
119  int the_config_version = 0;
120 
121  std::map<EcalLogicID, RunTPGConfigDat>::const_iterator it;
122 
123  int nr = 0;
124  for (it = dataset.begin(); it != dataset.end(); it++) {
125  ++nr;
126  //EcalLogicID ecalid = it->first;
127 
128  RunTPGConfigDat dat = it->second;
129  the_config_tag = dat.getConfigTag();
130  the_config_version = dat.getVersion();
131  }
132 
133  // it is all the same for all SM... get the last one
134 
135  // here we should check if it is the same as previous run.
136 
137  if ((the_config_tag != m_i_tag || the_config_version != m_i_version) && nr > 0) {
138  std::cout << " run= " << irun << " tag " << the_config_tag << " version=" << the_config_version << std::endl;
139  std::cout << "the tag is different from last transferred run ... retrieving last config set from DB"
140  << std::endl;
141 
142  FEConfigMainInfo fe_main_info;
143  fe_main_info.setConfigTag(the_config_tag);
144  fe_main_info.setVersion(the_config_version);
145 
146  try {
147  econn->fetchConfigSet(&fe_main_info);
148 
149  // now get TPGTowerStatus
150  int badttId = fe_main_info.getBttId();
151 
152  if (badttId != m_i_badTT) {
153  FEConfigBadTTInfo fe_badTT_info;
154  fe_badTT_info.setId(badttId);
155 
156  econn->fetchConfigSet(&fe_badTT_info);
157 
158  std::vector<FEConfigBadTTDat> dataset_TpgBadTT;
159 
160  econn->fetchConfigDataSet(&dataset_TpgBadTT, &fe_badTT_info);
161 
162  EcalTPGTowerStatus* towerStatus = new EcalTPGTowerStatus;
163  typedef std::vector<FEConfigBadTTDat>::const_iterator CIfeped;
164  EcalLogicID ecid_xt;
165  FEConfigBadTTDat rd_badTT;
166 
167  // reset the map
168  // EB
169  for (int ism = 1; ism <= 36; ism++) {
170  for (int ito = 1; ito <= 68; ito++) {
171  int tow_eta = (ito - 1) / 4;
172  int tow_phi = ((ito - 1) - tow_eta * 4);
173  int axt = (tow_eta * 5) * 20 + tow_phi * 5 + 1;
175  const EcalTrigTowerDetId towid = id.tower();
176  int tower_status = 0;
177  towerStatus->setValue(towid.rawId(), tower_status);
178  }
179  }
180  //EE
181  for (size_t itower = 0; itower < my_TTEcalLogicId_EE.size(); itower++) {
182  int towid = my_TTEcalLogicId_EE[itower].getLogicID();
183  int tower_status = 0;
184  towerStatus->setValue(towid, tower_status);
185  }
186 
187  // now put at 1 those that are bad
188  for (CIfeped p = dataset_TpgBadTT.begin(); p != dataset_TpgBadTT.end(); p++) {
189  rd_badTT = *p;
190 
191  int tcc_num = rd_badTT.getTCCId();
192  int tt_num = rd_badTT.getTTId();
193 
194  std::cout << " tcc/tt" << tcc_num << "/" << tt_num << std::endl;
195 
196  if (tcc_num > 36 && tcc_num <= 72) {
197  // SM number
198  int smid = tcc_num - 54;
199  if (tcc_num < 55)
200  smid = tcc_num - 18;
201  // TT number
202  int towerid = tt_num;
203 
204  int tow_eta = (towerid - 1) / 4;
205  int tow_phi = ((towerid - 1) - tow_eta * 4);
206  int axt = (tow_eta * 5) * 20 + tow_phi * 5 + 1;
207 
208  EBDetId id(smid, axt, EBDetId::SMCRYSTALMODE);
209  const EcalTrigTowerDetId towid = id.tower();
210  towerStatus->setValue(towid.rawId(), rd_badTT.getStatus());
211 
212  } else {
213  // EE data
214 
215  // TCC number
216  int tccid = tcc_num;
217  // TT number
218  int towerid = tt_num;
219 
220  bool set_the_tower = false;
221  int towid;
222  for (size_t itower = 0; itower < my_TTEcalLogicId_EE.size(); itower++) {
223  if (!set_the_tower) {
224  if (my_TTEcalLogicId_EE[itower].getID1() == tccid &&
225  my_TTEcalLogicId_EE[itower].getID2() == towerid) {
226  towid = my_TTEcalLogicId_EE[itower].getLogicID();
227  set_the_tower = true;
228  break;
229  }
230  }
231  }
232 
233  if (set_the_tower) {
234  towerStatus->setValue(towid, rd_badTT.getStatus());
235 
236  } else {
237  std::cout << " these may be the additional towers TCC/TT " << tccid << "/" << towerid << std::endl;
238  }
239  }
240  }
241 
242  edm::LogInfo("EcalTPGBadTTHandler") << "Finished badTT reading.";
243 
244  Time_t snc = (Time_t)irun;
245 
246  m_to_transfer.push_back(std::make_pair((EcalTPGTowerStatus*)towerStatus, snc));
247 
248  m_i_run_number = irun;
249  m_i_tag = the_config_tag;
250  m_i_version = the_config_version;
251  m_i_badTT = badttId;
252 
253  writeFile("last_tpg_badTT_settings.txt");
254 
255  } else {
256  m_i_run_number = irun;
257  m_i_tag = the_config_tag;
258  m_i_version = the_config_version;
259 
260  writeFile("last_tpg_badTT_settings.txt");
261 
262  // std::cout<< " even if the tag/version is not the same, the badTT id is the same -> no transfer needed "<< std::endl;
263  }
264 
265  }
266 
267  catch (std::exception& e) {
268  std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" << the_config_tag << " version=" << the_config_version
269  << std::endl;
270  std::cout << e.what() << std::endl;
271  m_i_run_number = irun;
272  }
273 
274  } else if (nr == 0) {
275  m_i_run_number = irun;
276  // std::cout<< " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed "<< std::endl;
277  } else {
278  m_i_run_number = irun;
279  m_i_tag = the_config_tag;
280  m_i_version = the_config_version;
281 
282  writeFile("last_tpg_badTT_settings.txt");
283  }
284  }
285  }
286 
287  delete econn;
288 
289  edm::LogInfo("EcalTPGBadTTHandler") << "Ecal - > end of getNewObjects -----------";
290 }
291 
293  //-------------------------------------------------------------
294 
295  m_i_tag = "";
296  m_i_version = 0;
297  m_i_run_number = 0;
298  m_i_badTT = 0;
299 
300  FILE* inpFile; // input file
301  inpFile = fopen(inputFile, "r");
302  if (!inpFile) {
303  edm::LogError("EcalTPGBadTTHandler") << "*** Can not open file: " << inputFile;
304  return;
305  }
306 
307  char line[256];
308 
309  std::ostringstream str;
310 
311  fgets(line, 255, inpFile);
312  m_i_tag = to_string(line);
313  str << "gen tag " << m_i_tag << std::endl; // should I use this?
314 
315  fgets(line, 255, inpFile);
316  m_i_version = atoi(line);
317  str << "version= " << m_i_version << std::endl;
318 
319  fgets(line, 255, inpFile);
320  m_i_run_number = atoi(line);
321  str << "run_number= " << m_i_run_number << std::endl;
322 
323  fgets(line, 255, inpFile);
324  m_i_badTT = atoi(line);
325  str << "badTT_config= " << m_i_badTT << std::endl;
326 
327  fclose(inpFile); // close inp. file
328 }
329 
331  //-------------------------------------------------------------
332 
333  std::ofstream myfile;
334  myfile.open(inputFile);
335  myfile << m_i_tag << std::endl;
336  myfile << m_i_version << std::endl;
337  myfile << m_i_run_number << std::endl;
338  myfile << m_i_badTT << std::endl;
339 
340  myfile.close();
341 }
void setRunTypeDef(const RunTypeDef &runTypeDef)
Definition: RunTag.cc:42
std::vector< RunIOV > getRuns()
Definition: RunList.cc:25
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
int getStatus() const
Definition: RunTag.h:13
int getBttId() const
int getTTId() const
int getVersion() const
Log< level::Error, false > LogError
void setVersion(int id)
static std::string to_string(const XMLCh *ch)
void writeFile(const char *inputFile)
int getTCCId() const
void setLocationDef(const LocationDef &locDef)
Definition: RunTag.cc:33
Log< level::Info, false > LogInfo
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:57
void readFromFile(const char *inputFile)
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 NULLID
Definition: EcalLogicID.h:35
void setValue(const uint32_t &id, const uint16_t &val)
void setGeneralTag(std::string tag)
Definition: RunTag.cc:24
static const int SMCRYSTALMODE
Definition: EBDetId.h:159
#define str(s)
int ism(int ieta, int iphi)
Definition: EcalPyUtils.cc:49
EcalTPGBadTTHandler(edm::ParameterSet const &)