CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
EcalTPGWeightIdMapHandler.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", "EcalTPGWeightIdMapHandler")) {
24  edm::LogInfo("EcalTPGWeightIdMapHandler") << "EcalTPGWeightIdMap 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("EcalTPGWeightIdMapHandler") << 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::LogInfo("EcalTPGWeightIdMapHandler") << "Started GetNewObjects!!!";
49 
50  //check whats already inside of database
51  if (tagInfo().size) {
52  //check whats already inside of database
53  edm::LogInfo("got offlineInfo = ");
54  edm::LogInfo("tag name = ") << tagInfo().name;
55  edm::LogInfo("size = ") << tagInfo().size;
56  } else {
57  edm::LogInfo(" First object for this tag ");
58  }
59 
60  unsigned int max_since = 0;
61  max_since = static_cast<unsigned int>(tagInfo().lastInterval.since);
62  edm::LogInfo("EcalTPGWeightIdMapHandler") << "max_since : " << max_since;
63  Ref weightIdMap_db = lastPayload();
64 
65  edm::LogInfo("EcalTPGWeightIdMapHandler") << "retrieved last payload ";
66 
67  // here we retrieve all the runs after the last from online DB
68  edm::LogInfo("EcalTPGWeightIdMapHandler") << "Retrieving run list from ONLINE DB ... ";
69 
70  edm::LogInfo("EcalTPGWeightIdMapHandler") << "Making connection...";
71  econn = new EcalCondDBInterface(m_sid, m_user, m_pass);
72  edm::LogInfo("EcalTPGWeightIdMapHandler") << "Done.";
73 
74  if (!econn) {
75  edm::LogInfo(" connection parameters ") << m_sid << "/" << m_user;
76  // cerr << e.what() << std::endl;
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_weightIdMap_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 
105  edm::LogInfo("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::LogInfo("EcalTPGWeightIdMapHandler") << "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  // my_list=econn->fetchRunListByLocation(my_runtag,min_run,max_run,my_locdef);
113 
114  std::vector<RunIOV> run_vec = my_list.getRuns();
115  size_t num_runs = run_vec.size();
116  edm::LogInfo("EcalTPGWeightIdMapHandler") << "number of Mon runs is : " << num_runs;
117 
118  unsigned int irun;
119  if (num_runs > 0) {
120  for (size_t kr = 0; kr < run_vec.size(); kr++) {
121  irun = static_cast<unsigned int>(run_vec[kr].getRunNumber());
122 
123  edm::LogInfo(" run= ") << irun;
124 
125  // retrieve the data :
126  std::map<EcalLogicID, RunTPGConfigDat> dataset;
127  econn->fetchDataSet(&dataset, &run_vec[kr]);
128 
129  std::string the_config_tag = "";
130  int the_config_version = 0;
131 
132  std::map<EcalLogicID, RunTPGConfigDat>::const_iterator it;
133 
134  int nr = 0;
135  for (it = dataset.begin(); it != dataset.end(); it++) {
136  ++nr;
137  //EcalLogicID ecalid = it->first;
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::LogInfo(" run= ") << irun << " tag " << the_config_tag << " version=" << the_config_version;
146 
147  // here we should check if it is the same as previous run.
148 
149  if ((the_config_tag != m_i_tag || the_config_version != m_i_version) && nr > 0) {
150  edm::LogInfo("the tag is different from last transferred run ... retrieving last config set from DB");
151 
152  FEConfigMainInfo fe_main_info;
153  fe_main_info.setConfigTag(the_config_tag);
154  fe_main_info.setVersion(the_config_version);
155 
156  try {
157  econn->fetchConfigSet(&fe_main_info);
158 
159  // now get TPGWeightIdMap
160  int weightId = fe_main_info.getWeiId();
161 
162  if (weightId != m_i_weightIdMap) {
163  FEConfigWeightInfo fe_weight_info;
164  fe_weight_info.setId(weightId);
165  econn->fetchConfigSet(&fe_weight_info);
166  std::map<EcalLogicID, FEConfigWeightGroupDat> dataset_TpgWeight;
167  econn->fetchDataSet(&dataset_TpgWeight, &fe_weight_info);
168  edm::LogInfo("EcalTPGWeightIdMapHandler") << "Got object!";
169  EcalTPGWeightIdMap* weightMap = new EcalTPGWeightIdMap;
170  typedef std::map<EcalLogicID, FEConfigWeightGroupDat>::const_iterator CIfeweight;
171  EcalLogicID ecid_xt;
173 
174  int igroups = 0;
175  for (CIfeweight p = dataset_TpgWeight.begin(); p != dataset_TpgWeight.end(); p++) {
176  rd_w = p->second;
177  // EB and EE data
179  unsigned int weight0 = static_cast<unsigned int>(rd_w.getWeight4());
180  unsigned int weight1 = static_cast<unsigned int>(rd_w.getWeight3());
181  unsigned int weight2 = static_cast<unsigned int>(rd_w.getWeight2());
182  unsigned int weight3 = static_cast<unsigned int>(rd_w.getWeight1() - 0x80);
183  unsigned int weight4 = static_cast<unsigned int>(rd_w.getWeight0());
184 
185  w.setValues(weight0, weight1, weight2, weight3, weight4);
186  weightMap->setValue(rd_w.getWeightGroupId(), w);
187 
188  ++igroups;
189  }
190 
191  edm::LogInfo("EcalTPGWeightIdMapHandler") << "found " << igroups << "Weight groups";
192 
193  Time_t snc = (Time_t)irun;
194  m_to_transfer.push_back(std::make_pair((EcalTPGWeightIdMap*)weightMap, snc));
195 
196  m_i_run_number = irun;
197  m_i_tag = the_config_tag;
198  m_i_version = the_config_version;
199  m_i_weightIdMap = weightId;
200 
201  writeFile("last_tpg_weightIdMap_settings.txt");
202 
203  } else {
204  m_i_run_number = irun;
205  m_i_tag = the_config_tag;
206  m_i_version = the_config_version;
207 
208  writeFile("last_tpg_weightIdMap_settings.txt");
209 
210  edm::LogInfo(
211  " even if the tag/version is not the same, the weightIdMap id is the same -> no transfer needed ");
212  }
213 
214  } catch (std::exception& e) {
215  edm::LogInfo("ERROR: THIS CONFIG DOES NOT EXIST: tag=")
216  << the_config_tag << " version=" << the_config_version;
217  edm::LogInfo("error") << e.what();
218  m_i_run_number = irun;
219  }
220 
221  } else if (nr == 0) {
222  m_i_run_number = irun;
223  edm::LogInfo(" no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor, no transfer needed ");
224  } else {
225  m_i_run_number = irun;
226  m_i_tag = the_config_tag;
227  m_i_version = the_config_version;
228  edm::LogInfo(" the tag/version is the same, no transfer needed ");
229  writeFile("last_tpg_weightIdMap_settings.txt");
230  }
231  }
232  }
233 
234  delete econn;
235  } // usual way
236  edm::LogInfo("EcalTPGWeightIdMapHandler") << "Ecal - > end of getNewObjects -----------";
237 }
239  edm::LogInfo(" reading the input file ") << m_file_name;
240  std::ifstream fInput;
241  fInput.open(m_file_name);
242  if (!fInput.is_open()) {
243  edm::LogInfo("ERROR : cannot open file ") << m_file_name;
244  exit(1);
245  }
246  unsigned int wloc[5];
248  EcalTPGWeightIdMap* weightMap = new EcalTPGWeightIdMap;
249  int igroups = 0;
251  while (!fInput.eof()) {
252  getline(fInput, line);
253  if (!line.empty()) {
254  std::stringstream ss;
255  ss << line;
256  ss >> wloc[0] >> wloc[1] >> wloc[2] >> wloc[3] >> wloc[4];
257  // edm::LogInfo(" Weights: ") << wloc[0] << " " << wloc[1] << " " << wloc[2] << " " << wloc[3] << " " << wloc[4];
258  w.setValues(wloc[0], wloc[1], wloc[2], wloc[3], wloc[4]);
259  weightMap->setValue(igroups, w);
260  igroups++;
261  }
262  }
263  edm::LogInfo("EcalTPGWeightIdMapHandler") << "found " << igroups << " Weight groups";
264  try {
265  Time_t snc = (Time_t)m_firstRun;
266  m_to_transfer.push_back(std::make_pair((EcalTPGWeightIdMap*)weightMap, snc));
267  } catch (std::exception& e) {
268  edm::LogInfo("EcalTPGWeightIdMapHandler::readtxtFile error : ") << e.what();
269  }
270 }
271 
273  edm::LogInfo(" reading the input file ") << m_file_name;
274  std::ifstream fxml;
275  fxml.open(m_file_name);
276  if (!fxml.is_open()) {
277  edm::LogInfo("ERROR : cannot open file ") << m_file_name;
278  exit(1);
279  }
280  std::string dummyLine, bid;
281  unsigned int wloc[5];
283  EcalTPGWeightIdMap* weightMap = new EcalTPGWeightIdMap;
284  int ngroups, igroups = 0;
285  edm::LogInfo("EcalTPGWeightIdMapHandler") << "found " << igroups << "Weight groups";
286  for (int i = 0; i < 5; i++)
287  std::getline(fxml, dummyLine); // skip first lines
288  // get the Weight group number
289  fxml >> bid;
290  std::string stt = bid.substr(7, 1);
291  std::istringstream sc(stt);
292  sc >> ngroups;
293  edm::LogInfo("EcalTPGWeightIdMapHandler") << "found " << ngroups << " Weight groups";
294  for (int i = 0; i < 2; i++)
295  std::getline(fxml, dummyLine); // <item_version>0</item_version>
296  for (int i = 0; i < ngroups; i++) {
297  std::getline(fxml, dummyLine); // <item
298  // edm::LogInfo(" group ") << i << " first line " << dummyLine;
299  fxml >> bid; // <first
300  std::size_t found = bid.find("</");
301  stt = bid.substr(7, found - 7);
302  std::istringstream sg1(stt);
303  sg1 >> igroups;
304  if (igroups != i) {
305  edm::LogInfo(" group ") << i << ": " << bid << " igroups " << igroups;
306  exit(-1);
307  }
308  for (int i = 0; i < 2; i++)
309  std::getline(fxml, dummyLine); // < second
310  for (int i = 0; i < 5; i++) {
311  fxml >> bid;
312  found = bid.find("</");
313  stt = bid.substr(5, found - 5);
314  std::istringstream w(stt);
315  w >> wloc[i];
316  }
317  w.setValues(wloc[0], wloc[1], wloc[2], wloc[3], wloc[4]);
318  weightMap->setValue(igroups, w);
319  for (int i = 0; i < 3; i++)
320  std::getline(fxml, dummyLine); // </item>
321  // edm::LogInfo(" group ") << i << " last line " << dummyLine;
322  }
323  try {
324  Time_t snc = (Time_t)m_firstRun;
325  m_to_transfer.push_back(std::make_pair((EcalTPGWeightIdMap*)weightMap, snc));
326  } catch (std::exception& e) {
327  edm::LogInfo("EcalTPGWeightIdMapHandler::readxmlFile error : ") << e.what();
328  }
329 }
330 
332  //-------------------------------------------------------------
333 
334  m_i_tag = "";
335  m_i_version = 0;
336  m_i_run_number = 0;
337  m_i_weightIdMap = 0;
338 
339  FILE* inpFile; // input file
340  inpFile = fopen(inputFile, "r");
341  if (!inpFile) {
342  edm::LogError("EcalTPGWeightIdMapHandler") << "*** Can not open file: " << inputFile;
343  return;
344  }
345 
346  char line[256];
347 
348  std::ostringstream str;
349 
350  fgets(line, 255, inpFile);
351  m_i_tag = to_string(line);
352  str << "gen tag " << m_i_tag << std::endl; // should I use this?
353 
354  fgets(line, 255, inpFile);
355  m_i_version = atoi(line);
356  str << "version= " << m_i_version << std::endl;
357 
358  fgets(line, 255, inpFile);
359  m_i_run_number = atoi(line);
360  str << "run_number= " << m_i_run_number << std::endl;
361 
362  fgets(line, 255, inpFile);
363  m_i_weightIdMap = atoi(line);
364  str << "weightIdMap_config= " << m_i_weightIdMap << std::endl;
365 
366  fclose(inpFile); // close inp. file
367 }
368 
370  //-------------------------------------------------------------
371 
372  std::ofstream myfile;
373  myfile.open(inputFile);
374  myfile << m_i_tag << std::endl;
375  myfile << m_i_version << std::endl;
376  myfile << m_i_run_number << std::endl;
377  myfile << m_i_weightIdMap << std::endl;
378 
379  myfile.close();
380 }
void setRunTypeDef(const RunTypeDef &runTypeDef)
Definition: RunTag.cc:42
std::vector< RunIOV > getRuns()
Definition: RunList.cc:25
std::string getConfigTag() const
int getVersion() const
Definition: RunTag.h:13
std::string to_string(const V &value)
Definition: OMSAccess.h:71
Log< level::Error, false > LogError
void setVersion(int id)
void readFromFile(CaloCluster &c, FILE *file)
int getWeiId() const
void setLocationDef(const LocationDef &locDef)
Definition: RunTag.cc:33
Log< level::Info, false > LogInfo
void setValue(const uint32_t &id, const EcalTPGWeights &value)
EcalTPGWeightIdMapHandler(edm::ParameterSet const &)
void setRunType(std::string runtype)
Definition: RunTypeDef.cc:21
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
static std::vector< std::string > checklist dat
cond::Time_t Time_t
Definition: Time.h:18
std::unique_ptr< EcalTPGWeightIdMap > Ref
void setConfigTag(std::string x)
Definition: IODConfig.h:29
void setLocation(std::string loc)
Definition: LocationDef.cc:20
T w() const
void setGeneralTag(std::string tag)
Definition: RunTag.cc:24
void setValues(const uint32_t &w0, const uint32_t &w1, const uint32_t &w2, const uint32_t &w3, const uint32_t &w4)
#define str(s)
tuple size
Write out results.