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  int icells = 0;
185  int icellsFound = 0;
186  for (CIfeped p = dataset_TpgBadStrip.begin(); p != dataset_TpgBadStrip.end(); p++) {
187  rd_badStrip = *p;
188 
189  //int fed_num=rd_badStrip.getFedId();
190  int tcc_num = rd_badStrip.getTCCId();
191  int tt_num = rd_badStrip.getTTId();
192  int strip_num = rd_badStrip.getStripId();
193 
194  //std::cout << fed_num << " " << tcc_num << " " << tt_num << " " << strip_num << std::endl;
195 
196  // EE data
197  int stripid;
198 
199  bool set_the_strip = false;
200  for (size_t istrip = 0; istrip < my_StripEcalLogicId_EE.size(); istrip++) {
201  if (!set_the_strip) {
202  if (my_StripEcalLogicId_EE[istrip].getID1() == tcc_num &&
203  my_StripEcalLogicId_EE[istrip].getID2() == tt_num &&
204  my_StripEcalLogicId_EE[istrip].getID3() == strip_num) {
205  stripid = my_StripEcalLogicId_EE[istrip].getLogicID();
206 
207  set_the_strip = true;
208  ++icellsFound;
209  break;
210  }
211  }
212  }
213 
214  if (set_the_strip) {
215  stripStatus->setValue(stripid, (unsigned int)rd_badStrip.getStatus());
216  }
217 
218  ++icells;
219  }
220 
221  // std::cout << " icells = " << icells << std::endl;
222 
223  edm::LogInfo("EcalTPGBadStripHandler") << "Finished badStrip reading.";
224 
225  Time_t snc = (Time_t)irun;
226 
227  m_to_transfer.push_back(std::make_pair((EcalTPGStripStatus*)stripStatus, snc));
228 
229  m_i_run_number = irun;
230  m_i_tag = the_config_tag;
231  m_i_version = the_config_version;
232  m_i_badStrip = badstripId;
233 
234  writeFile("last_tpg_badStrip_settings.txt");
235 
236  } else {
237  m_i_run_number = irun;
238  m_i_tag = the_config_tag;
239  m_i_version = the_config_version;
240 
241  writeFile("last_tpg_badStrip_settings.txt");
242 
243  // std::cout<< " even if the tag/version is not the same, the badStrip id is the same -> no transfer needed "<< std::endl;
244  }
245 
246  }
247 
248  catch (std::exception& e) {
249  std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" << the_config_tag << " version=" << the_config_version
250  << std::endl;
251  std::cout << e.what() << std::endl;
252  m_i_run_number = irun;
253  }
254 
255  } else if (nr == 0) {
256  m_i_run_number = irun;
257  // std::cout<< " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed "<< std::endl;
258  } else {
259  m_i_run_number = irun;
260  m_i_tag = the_config_tag;
261  m_i_version = the_config_version;
262 
263  writeFile("last_tpg_badStrip_settings.txt");
264  }
265  }
266  }
267 
268  delete econn;
269 
270  edm::LogInfo("EcalTPGBadStripHandler") << "Ecal - > end of getNewObjects -----------";
271 }
272 
274  //-------------------------------------------------------------
275 
276  m_i_tag = "";
277  m_i_version = 0;
278  m_i_run_number = 0;
279  m_i_badStrip = 0;
280 
281  FILE* inpFile; // input file
282  inpFile = fopen(inputFile, "r");
283  if (!inpFile) {
284  edm::LogError("EcalTPGBadStripHandler") << "*** Can not open file: " << inputFile;
285  return;
286  }
287 
288  char line[256];
289 
290  std::ostringstream str;
291 
292  fgets(line, 255, inpFile);
293  m_i_tag = to_string(line);
294  str << "gen tag " << m_i_tag << std::endl; // should I use this?
295 
296  fgets(line, 255, inpFile);
297  m_i_version = atoi(line);
298  str << "version= " << m_i_version << std::endl;
299 
300  fgets(line, 255, inpFile);
301  m_i_run_number = atoi(line);
302  str << "run_number= " << m_i_run_number << std::endl;
303 
304  fgets(line, 255, inpFile);
305  m_i_badStrip = atoi(line);
306  str << "badTT_config= " << m_i_badStrip << std::endl;
307 
308  fclose(inpFile); // close inp. file
309 }
310 
312  //-------------------------------------------------------------
313 
314  std::ofstream myfile;
315  myfile.open(inputFile);
316  myfile << m_i_tag << std::endl;
317  myfile << m_i_version << std::endl;
318  myfile << m_i_run_number << std::endl;
319  myfile << m_i_badStrip << std::endl;
320 
321  myfile.close();
322 }
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:303
void writeFile(const char *inputFile)
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)
void readFromFile(CaloCluster &c, FILE *file)
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)