CMS 3D CMS Logo

EcalTPGWeightGroupHandler.cc
Go to the documentation of this file.
2 
11 
16 
17 #include <iostream>
18 #include <fstream>
19 
20 #include <ctime>
21 #include <unistd.h>
22 
23 #include <string>
24 #include <cstdio>
25 #include <typeinfo>
26 #include <sstream>
27 
28 const Int_t kEBStrips = 12240, kEEStrips = 2936;
29 
31  : m_name(ps.getUntrackedParameter<std::string>("name", "EcalTPGWeightGroupHandler")) {
32  edm::LogInfo("EcalTPGWeightGroupHandler") << "EcalTPGWeightGroup Source handler constructor.";
33  m_firstRun = static_cast<unsigned int>(atoi(ps.getParameter<std::string>("firstRun").c_str()));
34  m_lastRun = static_cast<unsigned int>(atoi(ps.getParameter<std::string>("lastRun").c_str()));
35  m_sid = ps.getParameter<std::string>("OnlineDBSID");
36  m_user = ps.getParameter<std::string>("OnlineDBUser");
37  m_pass = ps.getParameter<std::string>("OnlineDBPassword");
38  m_locationsource = ps.getParameter<std::string>("LocationSource");
39  m_location = ps.getParameter<std::string>("Location");
40  m_gentag = ps.getParameter<std::string>("GenTag");
41  m_runtype = ps.getParameter<std::string>("RunType");
42  m_file_type = ps.getParameter<std::string>("fileType"); // xml/txt
43  m_file_name = ps.getParameter<std::string>("fileName");
44 
45  edm::LogInfo("EcalTPGWeightGroupHandler") << m_sid << "/" << m_user << "/" << m_location << "/" << m_gentag;
46 }
47 
49 
51  if (m_file_type == "txt") {
52  readtxtFile();
53  } else if (m_file_type == "xml") {
54  readxmlFile();
55  } else {
56  edm::LogInfo("EcalTPGWeightGroupHandler") << "Started GetNewObjects!!!";
57 
58  //check whats already inside of database
59  if (tagInfo().size) {
60  //check whats already inside of database
61  edm::LogInfo(" got offlineInfo = ");
62  edm::LogInfo(" tag name = ") << tagInfo().name;
63  edm::LogInfo(" size = ") << tagInfo().size;
64  } else {
65  edm::LogInfo(" First object for this tag ");
66  }
67 
68  unsigned int max_since = 0;
69  max_since = static_cast<unsigned int>(tagInfo().lastInterval.since);
70  edm::LogInfo("EcalTPGWeightGroupHandler") << "max_since : " << max_since;
71 
72  edm::LogInfo("EcalTPGWeightGroupHandler") << "retrieved last payload ";
73 
74  // here we retrieve all the runs after the last from online DB
75 
76  edm::LogInfo("EcalTPGWeightGroupHandler") << "Retrieving run list from ONLINE DB ... ";
77 
78  edm::LogInfo("EcalTPGWeightGroupHandler") << "Making connection...";
79  econn = new EcalCondDBInterface(m_sid, m_user, m_pass);
80  edm::LogInfo("EcalTPGWeightGroupHandler") << "Done.";
81 
82  if (!econn) {
83  edm::LogInfo(" connection parameters ") << m_sid << "/" << m_user;
84  // cerr << e.what();
85  throw cms::Exception("OMDS not available");
86  }
87 
89  my_locdef.setLocation(m_location);
90 
92  my_rundef.setRunType(m_runtype);
93 
94  RunTag my_runtag;
95  my_runtag.setLocationDef(my_locdef);
96  my_runtag.setRunTypeDef(my_rundef);
97  my_runtag.setGeneralTag(m_gentag);
98 
99  readFromFile("last_tpg_weightGroup_settings.txt");
100 
101  unsigned int min_run;
102 
103  if (m_firstRun < m_i_run_number) {
104  min_run = m_i_run_number + 1;
105  } else {
106  min_run = m_firstRun;
107  }
108 
109  if (min_run < max_since) {
110  min_run = max_since + 1; // we have to add 1 to the last transferred one
111  }
112 
113  edm::LogInfo("m_i_run_number") << m_i_run_number << "m_firstRun " << m_firstRun << "max_since " << max_since;
114 
115  unsigned int max_run = m_lastRun;
116  edm::LogInfo("EcalTPGWeightGroupHandler") << "min_run= " << min_run << " max_run= " << max_run;
117 
118  RunList my_list;
119  my_list = econn->fetchGlobalRunListByLocation(my_runtag, min_run, max_run, my_locdef);
120  // my_list=econn->fetchRunListByLocation(my_runtag,min_run,max_run,my_locdef);
121 
122  std::vector<RunIOV> run_vec = my_list.getRuns();
123  size_t num_runs = run_vec.size();
124 
125  edm::LogInfo("number of runs is : ") << num_runs;
126 
127  unsigned int irun = 0;
128  if (num_runs > 0) {
129  // going to query the ecal logic id
130  std::vector<EcalLogicID> my_StripEcalLogicId_EE;
131  my_StripEcalLogicId_EE =
132  econn->getEcalLogicIDSetOrdered("ECAL_readout_strip", 1, 2000, 1, 70, 0, 5, "EE_offline_stripid", 123);
133 
134  edm::LogInfo(" GOT the logic ID for the EE trigger strips ");
135 
136  for (size_t kr = 0; kr < run_vec.size(); kr++) {
137  irun = static_cast<unsigned int>(run_vec[kr].getRunNumber());
138 
139  edm::LogInfo(" **************** ");
140  edm::LogInfo(" **************** ");
141  edm::LogInfo(" run= ") << irun;
142 
143  // retrieve the data :
144  std::map<EcalLogicID, RunTPGConfigDat> dataset;
145  econn->fetchDataSet(&dataset, &run_vec[kr]);
146 
147  std::string the_config_tag = "";
148  int the_config_version = 0;
149 
150  std::map<EcalLogicID, RunTPGConfigDat>::const_iterator it;
151 
152  int nr = 0;
153  for (it = dataset.begin(); it != dataset.end(); it++) {
154  ++nr;
155  //EcalLogicID ecalid = it->first;
156 
157  RunTPGConfigDat dat = it->second;
158  the_config_tag = dat.getConfigTag();
159  the_config_version = dat.getVersion();
160  }
161 
162  // it is all the same for all SM... get the last one
163 
164  edm::LogInfo(" run= ") << irun << " tag " << the_config_tag << " version=" << the_config_version;
165 
166  // here we should check if it is the same as previous run.
167 
168  if ((the_config_tag != m_i_tag || the_config_version != m_i_version) && nr > 0) {
169  edm::LogInfo("the tag is different from last transferred run ... retrieving last config set from DB");
170 
171  FEConfigMainInfo fe_main_info;
172  fe_main_info.setConfigTag(the_config_tag);
173  fe_main_info.setVersion(the_config_version);
174 
175  try {
176  edm::LogInfo(" before fetch config set");
177  econn->fetchConfigSet(&fe_main_info);
178  edm::LogInfo(" after fetch config set");
179 
180  // now get TPGWeightGroup
181  int wId = fe_main_info.getWeiId();
182 
183  if (wId != m_i_weightGroup) {
184  FEConfigWeightInfo fe_w_info;
185  fe_w_info.setId(wId);
186  econn->fetchConfigSet(&fe_w_info);
187  std::map<EcalLogicID, FEConfigWeightDat> dataset_TpgW;
188  econn->fetchDataSet(&dataset_TpgW, &fe_w_info);
189 
190  EcalTPGWeightGroup* weightG = new EcalTPGWeightGroup;
191  typedef std::map<EcalLogicID, FEConfigWeightDat>::const_iterator CIfesli;
192  EcalLogicID ecid_xt;
193  int weightGroup;
194 
195  std::map<std::string, int> map;
197 
198  for (CIfesli p = dataset_TpgW.begin(); p != dataset_TpgW.end(); p++) {
199  ecid_xt = p->first;
200  weightGroup = p->second.getWeightGroupId();
201 
202  std::string ecid_name = ecid_xt.getName();
203 
204  // EB data
205  if (ecid_name == "EB_VFE") {
206  int sm = ecid_xt.getID1();
207  int tt = ecid_xt.getID2();
208  int strip = ecid_xt.getID3();
209  int tcc = sm + 54;
210  if (sm > 18)
211  tcc = sm + 18;
212 
213  // simple formula to calculate the Srip EB identifier
214 
215  unsigned int stripEBId = 303176 + (tt - 1) * 64 + (strip - 1) * 8 + (tcc - 37) * 8192;
216 
217  weightG->setValue(stripEBId, weightGroup);
218  } else if (ecid_name == "ECAL_readout_strip") {
219  // EE data to add
220  int id1 = ecid_xt.getID1();
221  int id2 = ecid_xt.getID2();
222  int id3 = ecid_xt.getID3();
223 
224  bool set_the_strip = false;
225  int stripEEId;
226  for (size_t istrip = 0; istrip < my_StripEcalLogicId_EE.size(); istrip++) {
227  if (!set_the_strip) {
228  if (my_StripEcalLogicId_EE[istrip].getID1() == id1 &&
229  my_StripEcalLogicId_EE[istrip].getID2() == id2 &&
230  my_StripEcalLogicId_EE[istrip].getID3() == id3) {
231  stripEEId = my_StripEcalLogicId_EE[istrip].getLogicID();
232  set_the_strip = true;
233  break;
234  }
235  }
236  }
237 
238  if (set_the_strip) {
239  weightG->setValue(stripEEId, weightGroup);
240 
241  } else {
242  edm::LogInfo(" these may be the additional towers TCC/TT ") << id1 << "/" << id2;
243  }
244  }
245  }
246 
247  Time_t snc = (Time_t)irun;
248 
249  m_to_transfer.push_back(std::make_pair((EcalTPGWeightGroup*)weightG, snc));
250 
251  m_i_run_number = irun;
252  m_i_tag = the_config_tag;
253  m_i_version = the_config_version;
254  m_i_weightGroup = wId;
255 
256  writeFile("last_tpg_weightGroup_settings.txt");
257 
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_weightGroup_settings.txt");
264 
265  edm::LogInfo(
266  " even if the tag/version is not the same, the weight group id is the same -> no transfer needed ");
267  }
268 
269  }
270 
271  catch (std::exception& e) {
272  edm::LogInfo("ERROR: THIS CONFIG DOES NOT EXIST: tag=")
273  << the_config_tag << " version=" << the_config_version;
274  edm::LogInfo("Exception") << e.what();
275  m_i_run_number = irun;
276  }
277  edm::LogInfo(" **************** ");
278 
279  } else if (nr == 0) {
280  m_i_run_number = irun;
281  edm::LogInfo(" no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed ");
282  edm::LogInfo(" **************** ");
283  } else {
284  m_i_run_number = irun;
285  m_i_tag = the_config_tag;
286  m_i_version = the_config_version;
287  edm::LogInfo(" the tag/version is the same -> no transfer needed ");
288  edm::LogInfo(" **************** ");
289  writeFile("last_tpg_weightGroup_settings.txt");
290  }
291  }
292  }
293 
294  delete econn;
295  } // usual way
296  edm::LogInfo("EcalTPGWeightGroupHandler") << "Ecal - > end of getNewObjects -----------";
297 }
298 
300  edm::LogInfo(" reading the input file ") << m_file_name;
301  std::ifstream fInput;
302  fInput.open(m_file_name);
303  if (!fInput.is_open()) {
304  edm::LogInfo("ERROR : cannot open file ") << m_file_name;
305  exit(1);
306  }
307  int weightGroup, stripEBId, stripEEId;
308  EcalTPGWeightGroup* weightG = new EcalTPGWeightGroup;
309  for (int strip = 0; strip < kEBStrips; strip++) {
310  fInput >> stripEBId >> weightGroup;
311  weightG->setValue(stripEBId, weightGroup);
312  }
313  for (int strip = 0; strip < kEEStrips; strip++) {
314  fInput >> stripEEId >> weightGroup;
315  weightG->setValue(stripEEId, weightGroup);
316  }
317  try {
318  Time_t snc = (Time_t)m_firstRun;
319  m_to_transfer.push_back(std::make_pair((EcalTPGWeightGroup*)weightG, snc));
320  } catch (std::exception& e) {
321  edm::LogInfo("EcalTPGWeightGroupHandler::readtxtFile error : ") << e.what();
322  }
323  edm::LogInfo(" **************** ");
324 }
325 
327  edm::LogInfo(" reading the input file ") << m_file_name;
328  std::ifstream fxml;
329  fxml.open(m_file_name);
330  if (!fxml.is_open()) {
331  edm::LogInfo("ERROR : cannot open file ") << m_file_name;
332  exit(1);
333  }
334  std::string dummyLine, bid;
335  int weightGroup, stripEBId, stripEEId;
336  EcalTPGWeightGroup* weightG = new EcalTPGWeightGroup;
337  for (int i = 0; i < 6; i++)
338  std::getline(fxml, dummyLine); // skip first lines
339  fxml >> bid;
340  std::size_t found = bid.find("</");
341  std::string stt = bid.substr(7, found - 7);
342  for (int i = 0; i < 2; i++)
343  std::getline(fxml, dummyLine); // <item_version>0</item_version>
344  for (int strip = 0; strip < kEBStrips; strip++) {
345  std::getline(fxml, dummyLine); // <item
346  fxml >> bid; // <first
347  found = bid.find("</");
348  stt = bid.substr(7, found - 7);
349  std::istringstream sg1(stt);
350  sg1 >> stripEBId;
351  std::getline(fxml, dummyLine);
352  fxml >> bid; // <second
353  found = bid.find("</");
354  stt = bid.substr(8, found - 8);
355  std::istringstream sg2(stt);
356  sg2 >> weightGroup;
357  weightG->setValue(stripEBId, weightGroup);
358  for (int i = 0; i < 2; i++)
359  std::getline(fxml, dummyLine); // </item>
360  }
361  for (int strip = 0; strip < kEEStrips; strip++) {
362  std::getline(fxml, dummyLine); // <item
363  fxml >> bid; // <first
364  found = bid.find("</");
365  stt = bid.substr(7, found - 7);
366  std::istringstream sg1(stt);
367  sg1 >> stripEEId;
368  std::getline(fxml, dummyLine);
369  fxml >> bid; // <second
370  found = bid.find("</");
371  stt = bid.substr(8, found - 8);
372  std::istringstream sg2(stt);
373  sg2 >> weightGroup;
374  weightG->setValue(stripEEId, weightGroup);
375  for (int i = 0; i < 2; i++)
376  std::getline(fxml, dummyLine); // </item>
377  }
378  try {
379  Time_t snc = (Time_t)m_firstRun;
380  m_to_transfer.push_back(std::make_pair((EcalTPGWeightGroup*)weightG, snc));
381  } catch (std::exception& e) {
382  edm::LogInfo("EcalTPGWeightGroupHandler::readtxtFile error : ") << e.what();
383  }
384  edm::LogInfo(" **************** ");
385 }
386 
388  //-------------------------------------------------------------
389 
390  m_i_tag = "";
391  m_i_version = 0;
392  m_i_run_number = 0;
393  m_i_weightGroup = 0;
394 
395  FILE* inpFile; // input file
396  inpFile = fopen(inputFile, "r");
397  if (!inpFile) {
398  edm::LogError("EcalTPGWeightGroupHandler") << "*** Can not open file: " << inputFile;
399  return;
400  }
401 
402  char line[256];
403 
404  std::ostringstream str;
405 
406  fgets(line, 255, inpFile);
407  m_i_tag = to_string(line);
408  str << "gen tag " << m_i_tag << std::endl; // should I use this?
409 
410  fgets(line, 255, inpFile);
411  m_i_version = atoi(line);
412  str << "version= " << m_i_version << std::endl;
413 
414  fgets(line, 255, inpFile);
415  m_i_run_number = atoi(line);
416  str << "run_number= " << m_i_run_number << std::endl;
417 
418  fgets(line, 255, inpFile);
419  m_i_weightGroup = atoi(line);
420  str << "weightGroup_config= " << m_i_weightGroup << std::endl;
421 
422  fclose(inpFile); // close inp. file
423 }
424 
426  //-------------------------------------------------------------
427 
428  std::ofstream myfile;
429  myfile.open(inputFile);
430  myfile << m_i_tag << std::endl;
431  myfile << m_i_version << std::endl;
432  myfile << m_i_run_number << std::endl;
433  myfile << m_i_weightGroup << std::endl;
434 
435  myfile.close();
436 }
size
Write out results.
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 getID1() const
Definition: EcalLogicID.cc:30
std::string getName() const
Definition: EcalLogicID.cc:26
const Int_t kEBStrips
Definition: RunTag.h:13
int getID2() const
Definition: EcalLogicID.cc:32
int getVersion() const
int getWeiId() const
Log< level::Error, false > LogError
void setVersion(int id)
static std::string to_string(const XMLCh *ch)
const Int_t kEEStrips
Definition: TTTypes.h:54
EcalTPGWeightGroupHandler(edm::ParameterSet const &)
void setValue(const uint32_t &rawId, const uint32_t &ObjectId)
Definition: EcalTPGGroups.cc:7
void setLocationDef(const LocationDef &locDef)
Definition: RunTag.cc:33
Log< level::Info, false > LogInfo
std::string getConfigTag() const
cond::Time_t Time_t
Definition: Time.h:18
int getID3() const
Definition: EcalLogicID.cc:34
void setConfigTag(std::string x)
Definition: IODConfig.h:29
void setGeneralTag(std::string tag)
Definition: RunTag.cc:24
#define str(s)
def exit(msg="")