CMS 3D CMS Logo

EcalTPGOddWeightIdMapHandler.cc
Go to the documentation of this file.
2 
10 
11 #include <iostream>
12 #include <fstream>
13 
14 #include <ctime>
15 #include <unistd.h>
16 
17 #include <string>
18 #include <cstdio>
19 #include <typeinfo>
20 #include <sstream>
21 
23  : m_name(ps.getUntrackedParameter<std::string>("name", "EcalTPGOddWeightIdMapHandler")) {
24  edm::LogInfo("EcalTPGOddWeightIdMapHandler") << "EcalTPGOddWeightIdMap Source handler constructor";
25  m_firstRun = static_cast<unsigned int>(atoi(ps.getParameter<std::string>("firstRun").c_str()));
26  m_lastRun = static_cast<unsigned int>(atoi(ps.getParameter<std::string>("lastRun").c_str()));
27  m_sid = ps.getParameter<std::string>("OnlineDBSID");
28  m_user = ps.getParameter<std::string>("OnlineDBUser");
29  m_pass = ps.getParameter<std::string>("OnlineDBPassword");
30  m_locationsource = ps.getParameter<std::string>("LocationSource");
31  m_location = ps.getParameter<std::string>("Location");
32  m_gentag = ps.getParameter<std::string>("GenTag");
33  m_runtype = ps.getParameter<std::string>("RunType");
34  m_file_type = ps.getParameter<std::string>("fileType"); // xml/txt
35  m_file_name = ps.getParameter<std::string>("fileName");
36 
37  edm::LogInfo("EcalTPGOddWeightIdMapHandler") << m_sid << "/" << m_user << "/" << m_location << "/" << m_gentag;
38 }
39 
41 
43  if (m_file_type == "txt") {
44  readtxtFile();
45  } else if (m_file_type == "xml") {
46  readxmlFile();
47  } else {
48  edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << "Started GetNewObjects!!!";
49 
50  //check whats already inside of database
51  if (tagInfo().size) {
52  //check whats already inside of database
53  edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << "got offlineInfo = ";
54  edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << "tag name = " << tagInfo().name;
55  edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << "size = " << tagInfo().size;
56 
57  } else {
58  edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << " First object for this tag ";
59  }
60 
61  unsigned int max_since = 0;
62  max_since = static_cast<unsigned int>(tagInfo().lastInterval.since);
63  edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << "max_since : " << max_since;
64 
65  edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << "retrieved last payload ";
66 
67  // here we retrieve all the runs after the last from online DB
68  edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << "Retrieving run list from ONLINE DB ... ";
69 
70  edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << "Making connection...";
71  auto econn = std::make_shared<EcalCondDBInterface>(m_sid, m_user, m_pass);
72 
73  edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << "Done.";
74 
75  if (!econn) {
76  edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << " connection parameters " << m_sid << "/" << m_user;
77  throw cms::Exception("OMDS not available");
78  }
79 
81  my_locdef.setLocation(m_location);
82 
84  my_rundef.setRunType(m_runtype);
85 
86  RunTag my_runtag;
87  my_runtag.setLocationDef(my_locdef);
88  my_runtag.setRunTypeDef(my_rundef);
89  my_runtag.setGeneralTag(m_gentag);
90 
91  readFromFile("last_tpg_OddweightIdMap_settings.txt");
92 
93  unsigned int min_run;
94 
95  if (m_firstRun < m_i_run_number) {
96  min_run = m_i_run_number + 1;
97  } else {
98  min_run = m_firstRun;
99  }
100 
101  if (min_run < max_since) {
102  min_run = max_since + 1; // we have to add 1 to the last transferred one
103  }
104  edm::LogVerbatim("EcalTPGOddWeightIdMapHandler")
105  << "m_i_run_number" << m_i_run_number << "m_firstRun " << m_firstRun << "max_since " << max_since;
106 
107  unsigned int max_run = m_lastRun;
108  edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << "min_run= " << min_run << "max_run= " << max_run;
109 
110  RunList my_list;
111  my_list = econn->fetchGlobalRunListByLocation(my_runtag, min_run, max_run, my_locdef);
112 
113  std::vector<RunIOV> run_vec = my_list.getRuns();
114  size_t num_runs = run_vec.size();
115  edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << "number of Mon runs is : " << num_runs;
116 
117  unsigned int irun;
118  if (num_runs > 0) {
119  for (size_t kr = 0; kr < run_vec.size(); kr++) {
120  irun = static_cast<unsigned int>(run_vec[kr].getRunNumber());
121 
122  edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << " **************** ";
123  edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << " **************** ";
124  edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << " run= " << irun;
125 
126  // retrieve the data :
127  std::map<EcalLogicID, RunTPGConfigDat> dataset;
128  econn->fetchDataSet(&dataset, &run_vec[kr]);
129 
130  std::string the_config_tag = "";
131  int the_config_version = 0;
132 
133  std::map<EcalLogicID, RunTPGConfigDat>::const_iterator it;
134 
135  int nr = 0;
136  for (it = dataset.begin(); it != dataset.end(); it++) {
137  ++nr;
138  RunTPGConfigDat dat = it->second;
139  the_config_tag = dat.getConfigTag();
140  the_config_version = dat.getVersion();
141  }
142 
143  // it is all the same for all SM... get the last one
144 
145  edm::LogVerbatim("EcalTPGOddWeightIdMapHandler")
146  << " run= " << irun << " tag " << the_config_tag << " version=" << the_config_version;
147 
148  // here we should check if it is the same as previous run.
149 
150  if ((the_config_tag != m_i_tag || the_config_version != m_i_version) && nr > 0) {
151  edm::LogVerbatim("EcalTPGOddWeightIdMapHandler")
152  << "the tag is different from last transferred run ... retrieving last config set from DB";
153 
154  FEConfigMainInfo fe_main_info;
155  fe_main_info.setConfigTag(the_config_tag);
156  fe_main_info.setVersion(the_config_version);
157 
158  try {
159  edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << " before fetch config set";
160  econn->fetchConfigSet(&fe_main_info);
161  edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << " after fetch config set";
162 
163  // now get TPGOddWeightIdMap
164  int weightId = fe_main_info.getWei2Id();
165 
166  if (weightId != m_i_oddweightIdMap) {
167  FEConfigOddWeightInfo fe_odd_weight_info;
168  fe_odd_weight_info.setId(weightId);
169  econn->fetchConfigSet(&fe_odd_weight_info);
170  std::map<EcalLogicID, FEConfigOddWeightGroupDat> dataset_TpgWeight;
171  econn->fetchDataSet(&dataset_TpgWeight, &fe_odd_weight_info);
172  edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << "Got object!";
174  typedef std::map<EcalLogicID, FEConfigOddWeightGroupDat>::const_iterator CIfeweight;
175  EcalLogicID ecid_xt;
177 
178  int igroups = 0;
179  for (CIfeweight p = dataset_TpgWeight.begin(); p != dataset_TpgWeight.end(); p++) {
180  rd_w = p->second;
181  // EB and EE data
183  unsigned int weight0 = static_cast<unsigned int>(rd_w.getWeight4());
184  unsigned int weight1 = static_cast<unsigned int>(rd_w.getWeight3());
185  unsigned int weight2 = static_cast<unsigned int>(rd_w.getWeight2());
186  unsigned int weight3 = static_cast<unsigned int>(rd_w.getWeight1() - 0x80);
187  unsigned int weight4 = static_cast<unsigned int>(rd_w.getWeight0());
188 
189  w.setValues(weight0, weight1, weight2, weight3, weight4);
190  weightMap->setValue(rd_w.getWeightGroupId(), w);
191 
192  ++igroups;
193  }
194 
195  edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << "found " << igroups << "Weight groups";
196 
197  Time_t snc = (Time_t)irun;
198  m_to_transfer.push_back(std::make_pair((EcalTPGOddWeightIdMap*)weightMap, snc));
199 
200  m_i_run_number = irun;
201  m_i_tag = the_config_tag;
202  m_i_version = the_config_version;
203  m_i_oddweightIdMap = weightId;
204 
205  writeFile("last_tpg_OddweightIdMap_settings.txt");
206 
207  } else {
208  m_i_run_number = irun;
209  m_i_tag = the_config_tag;
210  m_i_version = the_config_version;
211 
212  writeFile("last_tpg_OddweightIdMap_settings.txt");
213 
214  edm::LogVerbatim("EcalTPGOddWeightIdMapHandler")
215  << " even if the tag/version is not the same, the weightIdMap id is the same -> no transfer needed ";
216  }
217 
218  } catch (std::exception& e) {
219  throw cms::Exception("FileReadError") << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" << the_config_tag
220  << " version=" << the_config_version << "\n"
221  << e.what();
222  }
223  edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << " **************** ";
224 
225  } else if (nr == 0) {
226  m_i_run_number = irun;
227  edm::LogVerbatim("EcalTPGOddWeightIdMapHandler")
228  << " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed ";
229  edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << " **************** ";
230  } else {
231  m_i_run_number = irun;
232  m_i_tag = the_config_tag;
233  m_i_version = the_config_version;
234  edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << " the tag/version is the same -> no transfer needed ";
235  edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << " **************** ";
236  writeFile("last_tpg_OddweightIdMap_settings.txt");
237  }
238  }
239  }
240 
241  } // usual way
242  edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << "Ecal - > end of getNewObjects -----------";
243 }
245  edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << " reading the input file " << m_file_name;
246  std::ifstream fInput;
247  fInput.open(m_file_name);
248  if (!fInput.is_open()) {
249  throw cms::Exception("FileReadError") << "ERROR : cannot open file " << m_file_name;
250  }
251  unsigned int wloc[5];
254 
255  int igroups = 0;
257  while (!fInput.eof()) {
258  getline(fInput, line);
259  if (!line.empty()) {
260  std::stringstream ss;
261  ss << line;
262  ss >> wloc[0] >> wloc[1] >> wloc[2] >> wloc[3] >> wloc[4];
263  w.setValues(wloc[0], wloc[1], wloc[2], wloc[3], wloc[4]);
264  weightMap->setValue(igroups, w);
265  igroups++;
266  }
267  }
268  edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << "found " << igroups << " Weight groups";
269  try {
270  Time_t snc = (Time_t)m_firstRun;
271  m_to_transfer.push_back(std::make_pair((EcalTPGOddWeightIdMap*)weightMap, snc));
272  } catch (std::exception& e) {
273  throw cms::Exception("FileReadError") << "EcalTPGOddWeightIdMapHandler::readtxtFile error : " << e.what();
274  }
275  edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << " **************** ";
276 }
277 
279  edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << " reading the input file " << m_file_name;
280  std::ifstream fxml;
281  fxml.open(m_file_name);
282  if (!fxml.is_open()) {
283  throw cms::Exception("FileReadError") << "ERROR: Cannot open file : " << m_file_name;
284  }
285  std::string dummyLine, bid;
286  unsigned int wloc[5];
289 
290  int ngroups, igroups;
291  for (int i = 0; i < 5; i++)
292  std::getline(fxml, dummyLine); // skip first lines
293  // get the Weight group number
294  fxml >> bid;
295  std::string stt = bid.substr(7, 1);
296  std::istringstream sc(stt);
297  sc >> ngroups;
298  edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << "found " << ngroups << " Weight groups";
299  for (int i = 0; i < 2; i++)
300  std::getline(fxml, dummyLine); // <item_version>0</item_version>
301  for (int i = 0; i < ngroups; i++) {
302  std::getline(fxml, dummyLine); // <item
303  fxml >> bid; // <first
304  std::size_t found = bid.find("</");
305  stt = bid.substr(7, found - 7);
306  std::istringstream sg1(stt);
307  sg1 >> igroups;
308  if (igroups != i) {
309  throw cms::Exception("MismatchError") << "ERROR in group " << i << ": " << bid << " igroups " << igroups;
310  }
311  for (int i = 0; i < 2; i++)
312  std::getline(fxml, dummyLine); // < second
313  for (int i = 0; i < 5; i++) {
314  fxml >> bid;
315  found = bid.find("</");
316  stt = bid.substr(5, found - 5);
317  std::istringstream w(stt);
318  w >> wloc[i];
319  }
320  w.setValues(wloc[0], wloc[1], wloc[2], wloc[3], wloc[4]);
321  weightMap->setValue(igroups, w);
322  for (int i = 0; i < 3; i++)
323  std::getline(fxml, dummyLine); // </item>
324  }
325  try {
326  Time_t snc = (Time_t)m_firstRun;
327  m_to_transfer.push_back(std::make_pair((EcalTPGOddWeightIdMap*)weightMap, snc));
328 
329  } catch (std::exception& e) {
330  throw cms::Exception("FileReadError") << "EcalTPGOddWeightIdMapHandler::readxmlFile error : " << e.what();
331  }
332  edm::LogVerbatim("EcalTPGOddWeightIdMapHandler") << " **************** ";
333 }
334 
336  //-------------------------------------------------------------
337 
338  m_i_tag = "";
339  m_i_version = 0;
340  m_i_run_number = 0;
341  m_i_oddweightIdMap = 0;
342 
343  FILE* inpFile; // input file
344  inpFile = fopen(inputFile, "r");
345  if (!inpFile) {
346  edm::LogError("EcalTPGOddWeightIdMapHandler") << "*** Can not open file: " << inputFile;
347  return;
348  }
349 
350  char line[256];
351 
352  std::ostringstream str;
353 
354  fgets(line, 255, inpFile);
355  m_i_tag = to_string(line);
356  str << "gen tag " << m_i_tag << std::endl; // should I use this?
357 
358  fgets(line, 255, inpFile);
359  m_i_version = atoi(line);
360  str << "version= " << m_i_version << std::endl;
361 
362  fgets(line, 255, inpFile);
363  m_i_run_number = atoi(line);
364  str << "run_number= " << m_i_run_number << std::endl;
365 
366  fgets(line, 255, inpFile);
367  m_i_oddweightIdMap = atoi(line);
368  str << "weightIdMap_config= " << m_i_oddweightIdMap << std::endl;
369 
370  fclose(inpFile); // close inp. file
371 }
372 
374  //-------------------------------------------------------------
375 
376  std::ofstream myfile;
377  myfile.open(inputFile);
378  myfile << m_i_tag << std::endl;
379  myfile << m_i_version << std::endl;
380  myfile << m_i_run_number << std::endl;
381  myfile << m_i_oddweightIdMap << std::endl;
382 
383  myfile.close();
384 }
size
Write out results.
Log< level::Info, true > LogVerbatim
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
T w() const
EcalTPGOddWeightIdMapHandler(edm::ParameterSet const &)
Definition: RunTag.h:13
int getVersion() const
Log< level::Error, false > LogError
void setVersion(int id)
static std::string to_string(const XMLCh *ch)
void setLocationDef(const LocationDef &locDef)
Definition: RunTag.cc:33
void setValue(const uint32_t &id, const EcalTPGWeights &value)
Log< level::Info, false > LogInfo
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)
int getWei2Id() const