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  int icells = 0;
189  for (CIfeped p = dataset_TpgBadTT.begin(); p != dataset_TpgBadTT.end(); p++) {
190  rd_badTT = *p;
191 
192  int tcc_num = rd_badTT.getTCCId();
193  int tt_num = rd_badTT.getTTId();
194 
195  std::cout << " tcc/tt" << tcc_num << "/" << tt_num << std::endl;
196 
197  if (tcc_num > 36 && tcc_num <= 72) {
198  // SM number
199  int smid = tcc_num - 54;
200  if (tcc_num < 55)
201  smid = tcc_num - 18;
202  // TT number
203  int towerid = tt_num;
204 
205  int tow_eta = (towerid - 1) / 4;
206  int tow_phi = ((towerid - 1) - tow_eta * 4);
207  int axt = (tow_eta * 5) * 20 + tow_phi * 5 + 1;
208 
209  EBDetId id(smid, axt, EBDetId::SMCRYSTALMODE);
210  const EcalTrigTowerDetId towid = id.tower();
211  towerStatus->setValue(towid.rawId(), rd_badTT.getStatus());
212 
213  ++icells;
214  } else {
215  // EE data
216 
217  // TCC number
218  int tccid = tcc_num;
219  // TT number
220  int towerid = tt_num;
221 
222  bool set_the_tower = false;
223  int towid;
224  for (size_t itower = 0; itower < my_TTEcalLogicId_EE.size(); itower++) {
225  if (!set_the_tower) {
226  if (my_TTEcalLogicId_EE[itower].getID1() == tccid &&
227  my_TTEcalLogicId_EE[itower].getID2() == towerid) {
228  towid = my_TTEcalLogicId_EE[itower].getLogicID();
229  set_the_tower = true;
230  break;
231  }
232  }
233  }
234 
235  if (set_the_tower) {
236  towerStatus->setValue(towid, rd_badTT.getStatus());
237 
238  } else {
239  std::cout << " these may be the additional towers TCC/TT " << tccid << "/" << towerid << std::endl;
240  }
241 
242  ++icells;
243  }
244  }
245 
246  edm::LogInfo("EcalTPGBadTTHandler") << "Finished badTT reading.";
247 
248  Time_t snc = (Time_t)irun;
249 
250  m_to_transfer.push_back(std::make_pair((EcalTPGTowerStatus*)towerStatus, snc));
251 
252  m_i_run_number = irun;
253  m_i_tag = the_config_tag;
254  m_i_version = the_config_version;
255  m_i_badTT = badttId;
256 
257  writeFile("last_tpg_badTT_settings.txt");
258 
259  } else {
260  m_i_run_number = irun;
261  m_i_tag = the_config_tag;
262  m_i_version = the_config_version;
263 
264  writeFile("last_tpg_badTT_settings.txt");
265 
266  // std::cout<< " even if the tag/version is not the same, the badTT id is the same -> no transfer needed "<< std::endl;
267  }
268 
269  }
270 
271  catch (std::exception& e) {
272  std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" << the_config_tag << " version=" << the_config_version
273  << std::endl;
274  std::cout << e.what() << std::endl;
275  m_i_run_number = irun;
276  }
277 
278  } else if (nr == 0) {
279  m_i_run_number = irun;
280  // std::cout<< " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed "<< std::endl;
281  } else {
282  m_i_run_number = irun;
283  m_i_tag = the_config_tag;
284  m_i_version = the_config_version;
285 
286  writeFile("last_tpg_badTT_settings.txt");
287  }
288  }
289  }
290 
291  delete econn;
292 
293  edm::LogInfo("EcalTPGBadTTHandler") << "Ecal - > end of getNewObjects -----------";
294 }
295 
297  //-------------------------------------------------------------
298 
299  m_i_tag = "";
300  m_i_version = 0;
301  m_i_run_number = 0;
302  m_i_badTT = 0;
303 
304  FILE* inpFile; // input file
305  inpFile = fopen(inputFile, "r");
306  if (!inpFile) {
307  edm::LogError("EcalTPGBadTTHandler") << "*** Can not open file: " << inputFile;
308  return;
309  }
310 
311  char line[256];
312 
313  std::ostringstream str;
314 
315  fgets(line, 255, inpFile);
316  m_i_tag = to_string(line);
317  str << "gen tag " << m_i_tag << std::endl; // should I use this?
318 
319  fgets(line, 255, inpFile);
320  m_i_version = atoi(line);
321  str << "version= " << m_i_version << std::endl;
322 
323  fgets(line, 255, inpFile);
324  m_i_run_number = atoi(line);
325  str << "run_number= " << m_i_run_number << std::endl;
326 
327  fgets(line, 255, inpFile);
328  m_i_badTT = atoi(line);
329  str << "badTT_config= " << m_i_badTT << std::endl;
330 
331  fclose(inpFile); // close inp. file
332 }
333 
335  //-------------------------------------------------------------
336 
337  std::ofstream myfile;
338  myfile.open(inputFile);
339  myfile << m_i_tag << std::endl;
340  myfile << m_i_version << std::endl;
341  myfile << m_i_run_number << std::endl;
342  myfile << m_i_badTT << std::endl;
343 
344  myfile.close();
345 }
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
std::string to_string(const V &value)
Definition: OMSAccess.h:71
int getVersion() const
Log< level::Error, false > LogError
void setVersion(int id)
void readFromFile(CaloCluster &c, FILE *file)
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 &)