CMS 3D CMS Logo

EcalTPGBadStripHandler.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", "EcalTPGBadStripHandler")) {
26  edm::LogInfo("EcalTPGBadStripHandler") << "EcalTPGStripStatus 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("EcalTPGBadStripHandler") << m_sid << "/" << m_user << "/" << m_location << "/" << m_gentag;
38 }
39 
41 
43  edm::LogInfo("EcalTPGBadStripHandler") << "Started GetNewObjects!!!";
44 
45  unsigned int max_since = 0;
46  max_since = static_cast<unsigned int>(tagInfo().lastInterval.since);
47  edm::LogInfo("EcalTPGBadStripHandler") << "max_since : " << max_since;
48  edm::LogInfo("EcalTPGBadStripHandler") << "retrieved last payload ";
49 
50  // here we retrieve all the runs after the last from online DB
51  edm::LogInfo("EcalTPGBadStripHandler") << "Retrieving run list from ONLINE DB ... ";
52 
53  edm::LogInfo("EcalTPGBadStripHandler") << "Making connection...";
54  econn = new EcalCondDBInterface(m_sid, m_user, m_pass);
55  edm::LogInfo("EcalTPGBadStripHandler") << "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_badStrip_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("EcalTPGBadStripHandler") << "min_run= " << min_run << "max_run= " << max_run;
91 
92  RunList my_list;
93  my_list = econn->fetchGlobalRunListByLocation(my_runtag, min_run, max_run, my_locdef);
94  //my_list=econn->fetchRunListByLocation(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 
105  if (num_runs > 0) {
106  // going to query the ecal logic id
107  std::vector<EcalLogicID> my_StripEcalLogicId_EE;
108  my_StripEcalLogicId_EE = econn->getEcalLogicIDSetOrdered("EE_trigger_strip",
109  1,
110  1000, //"TCC"
111  1,
112  100, //tower
113  0,
114  5, //strip
115  "EE_offline_stripid",
116  123);
117 
118  std::cout << " GOT the logic ID for the EE trigger strips " << std::endl;
119 
120  for (size_t kr = 0; kr < run_vec.size(); kr++) {
121  irun = static_cast<unsigned int>(run_vec[kr].getRunNumber());
122 
123  std::cout << " **************** " << std::endl;
124  std::cout << " **************** " << std::endl;
125  std::cout << " run= " << irun << std::endl;
126 
127  // retrieve the data :
128  std::map<EcalLogicID, RunTPGConfigDat> dataset;
129  econn->fetchDataSet(&dataset, &run_vec[kr]);
130 
131  std::string the_config_tag = "";
132  int the_config_version = 0;
133 
134  std::map<EcalLogicID, RunTPGConfigDat>::const_iterator it;
135 
136  int nr = 0;
137  for (it = dataset.begin(); it != dataset.end(); it++) {
138  ++nr;
139  //EcalLogicID ecalid = it->first;
140 
141  RunTPGConfigDat dat = it->second;
142  the_config_tag = dat.getConfigTag();
143  the_config_version = dat.getVersion();
144  }
145 
146  // it is all the same for all SM... get the last one
147 
148  std::cout << " run= " << irun << " tag " << the_config_tag << " version=" << the_config_version << std::endl;
149 
150  // here we should check if it is the same as previous run.
151 
152  if ((the_config_tag != m_i_tag || the_config_version != m_i_version) && nr > 0) {
153  std::cout << " run= " << irun << " tag " << the_config_tag << " version=" << the_config_version << std::endl;
154  std::cout << "the tag is different from last transferred run ... retrieving last config set from DB"
155  << std::endl;
156 
157  FEConfigMainInfo fe_main_info;
158  fe_main_info.setConfigTag(the_config_tag);
159  fe_main_info.setVersion(the_config_version);
160 
161  try {
162  econn->fetchConfigSet(&fe_main_info);
163 
164  // now get TPGStripStatus
165  int badstripId = fe_main_info.getBstId();
166 
167  if (badstripId != m_i_badStrip) {
168  FEConfigBadStripInfo fe_badStrip_info;
169  fe_badStrip_info.setId(badstripId);
170 
171  econn->fetchConfigSet(&fe_badStrip_info);
172 
173  std::vector<FEConfigBadStripDat> dataset_TpgBadStrip;
174 
175  econn->fetchConfigDataSet(&dataset_TpgBadStrip, &fe_badStrip_info);
176 
177  EcalTPGStripStatus* stripStatus = new EcalTPGStripStatus;
178  typedef std::vector<FEConfigBadStripDat>::const_iterator CIfeped;
179 
180  FEConfigBadStripDat rd_badStrip;
181  //unsigned int rd_stripStatus;
182 
183  // put at 1 the strip that are bad
184  for (CIfeped p = dataset_TpgBadStrip.begin(); p != dataset_TpgBadStrip.end(); p++) {
185  rd_badStrip = *p;
186 
187  //int fed_num=rd_badStrip.getFedId();
188  int tcc_num = rd_badStrip.getTCCId();
189  int tt_num = rd_badStrip.getTTId();
190  int strip_num = rd_badStrip.getStripId();
191 
192  //std::cout << fed_num << " " << tcc_num << " " << tt_num << " " << strip_num << std::endl;
193 
194  // EE data
195  int stripid;
196 
197  bool set_the_strip = false;
198  for (size_t istrip = 0; istrip < my_StripEcalLogicId_EE.size(); istrip++) {
199  if (!set_the_strip) {
200  if (my_StripEcalLogicId_EE[istrip].getID1() == tcc_num &&
201  my_StripEcalLogicId_EE[istrip].getID2() == tt_num &&
202  my_StripEcalLogicId_EE[istrip].getID3() == strip_num) {
203  stripid = my_StripEcalLogicId_EE[istrip].getLogicID();
204 
205  set_the_strip = true;
206  break;
207  }
208  }
209  }
210 
211  if (set_the_strip) {
212  stripStatus->setValue(stripid, (unsigned int)rd_badStrip.getStatus());
213  }
214  }
215 
216  edm::LogInfo("EcalTPGBadStripHandler") << "Finished badStrip reading.";
217 
218  Time_t snc = (Time_t)irun;
219 
220  m_to_transfer.push_back(std::make_pair((EcalTPGStripStatus*)stripStatus, snc));
221 
222  m_i_run_number = irun;
223  m_i_tag = the_config_tag;
224  m_i_version = the_config_version;
225  m_i_badStrip = badstripId;
226 
227  writeFile("last_tpg_badStrip_settings.txt");
228 
229  } else {
230  m_i_run_number = irun;
231  m_i_tag = the_config_tag;
232  m_i_version = the_config_version;
233 
234  writeFile("last_tpg_badStrip_settings.txt");
235 
236  // std::cout<< " even if the tag/version is not the same, the badStrip id is the same -> no transfer needed "<< std::endl;
237  }
238 
239  }
240 
241  catch (std::exception& e) {
242  std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" << the_config_tag << " version=" << the_config_version
243  << std::endl;
244  std::cout << e.what() << std::endl;
245  m_i_run_number = irun;
246  }
247 
248  } else if (nr == 0) {
249  m_i_run_number = irun;
250  // std::cout<< " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed "<< std::endl;
251  } else {
252  m_i_run_number = irun;
253  m_i_tag = the_config_tag;
254  m_i_version = the_config_version;
255 
256  writeFile("last_tpg_badStrip_settings.txt");
257  }
258  }
259  }
260 
261  delete econn;
262 
263  edm::LogInfo("EcalTPGBadStripHandler") << "Ecal - > end of getNewObjects -----------";
264 }
265 
267  //-------------------------------------------------------------
268 
269  m_i_tag = "";
270  m_i_version = 0;
271  m_i_run_number = 0;
272  m_i_badStrip = 0;
273 
274  FILE* inpFile; // input file
275  inpFile = fopen(inputFile, "r");
276  if (!inpFile) {
277  edm::LogError("EcalTPGBadStripHandler") << "*** Can not open file: " << inputFile;
278  return;
279  }
280 
281  char line[256];
282 
283  std::ostringstream str;
284 
285  fgets(line, 255, inpFile);
286  m_i_tag = to_string(line);
287  str << "gen tag " << m_i_tag << std::endl; // should I use this?
288 
289  fgets(line, 255, inpFile);
290  m_i_version = atoi(line);
291  str << "version= " << m_i_version << std::endl;
292 
293  fgets(line, 255, inpFile);
294  m_i_run_number = atoi(line);
295  str << "run_number= " << m_i_run_number << std::endl;
296 
297  fgets(line, 255, inpFile);
298  m_i_badStrip = atoi(line);
299  str << "badTT_config= " << m_i_badStrip << std::endl;
300 
301  fclose(inpFile); // close inp. file
302 }
303 
305  //-------------------------------------------------------------
306 
307  std::ofstream myfile;
308  myfile.open(inputFile);
309  myfile << m_i_tag << std::endl;
310  myfile << m_i_version << std::endl;
311  myfile << m_i_run_number << std::endl;
312  myfile << m_i_badStrip << std::endl;
313 
314  myfile.close();
315 }
void setRunTypeDef(const RunTypeDef &runTypeDef)
Definition: RunTag.cc:42
int getBstId() const
std::vector< RunIOV > getRuns()
Definition: RunList.cc:25
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
void writeFile(const char *inputFile)
Definition: RunTag.h:13
int getVersion() const
Log< level::Error, false > LogError
void setVersion(int id)
static std::string to_string(const XMLCh *ch)
EcalTPGBadStripHandler(edm::ParameterSet const &)
void setLocationDef(const LocationDef &locDef)
Definition: RunTag.cc:33
Log< level::Info, false > LogInfo
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
void setGeneralTag(std::string tag)
Definition: RunTag.cc:24
#define str(s)
void setValue(const uint32_t &id, const uint16_t &val)