CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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  int icells = 0;
195 
196  std::map<std::string, int> map;
198 
199  for (CIfesli p = dataset_TpgW.begin(); p != dataset_TpgW.end(); p++) {
200  ecid_xt = p->first;
201  weightGroup = p->second.getWeightGroupId();
202 
203  std::string ecid_name = ecid_xt.getName();
204 
205  // EB data
206  if (ecid_name == "EB_VFE") {
207  int sm = ecid_xt.getID1();
208  int tt = ecid_xt.getID2();
209  int strip = ecid_xt.getID3();
210  int tcc = sm + 54;
211  if (sm > 18)
212  tcc = sm + 18;
213 
214  // simple formula to calculate the Srip EB identifier
215 
216  unsigned int stripEBId = 303176 + (tt - 1) * 64 + (strip - 1) * 8 + (tcc - 37) * 8192;
217 
218  weightG->setValue(stripEBId, weightGroup);
219  ++icells;
220  } else if (ecid_name == "ECAL_readout_strip") {
221  // EE data to add
222  int id1 = ecid_xt.getID1();
223  int id2 = ecid_xt.getID2();
224  int id3 = ecid_xt.getID3();
225 
226  bool set_the_strip = false;
227  int stripEEId;
228  for (size_t istrip = 0; istrip < my_StripEcalLogicId_EE.size(); istrip++) {
229  if (!set_the_strip) {
230  if (my_StripEcalLogicId_EE[istrip].getID1() == id1 &&
231  my_StripEcalLogicId_EE[istrip].getID2() == id2 &&
232  my_StripEcalLogicId_EE[istrip].getID3() == id3) {
233  stripEEId = my_StripEcalLogicId_EE[istrip].getLogicID();
234  set_the_strip = true;
235  break;
236  }
237  }
238  }
239 
240  if (set_the_strip) {
241  weightG->setValue(stripEEId, weightGroup);
242 
243  } else {
244  edm::LogInfo(" these may be the additional towers TCC/TT ") << id1 << "/" << id2;
245  }
246 
247  ++icells;
248  }
249  }
250 
251  Time_t snc = (Time_t)irun;
252 
253  m_to_transfer.push_back(std::make_pair((EcalTPGWeightGroup*)weightG, snc));
254 
255  m_i_run_number = irun;
256  m_i_tag = the_config_tag;
257  m_i_version = the_config_version;
258  m_i_weightGroup = wId;
259 
260  writeFile("last_tpg_weightGroup_settings.txt");
261 
262  } else {
263  m_i_run_number = irun;
264  m_i_tag = the_config_tag;
265  m_i_version = the_config_version;
266 
267  writeFile("last_tpg_weightGroup_settings.txt");
268 
269  edm::LogInfo(
270  " even if the tag/version is not the same, the weight group id is the same -> no transfer needed ");
271  }
272 
273  }
274 
275  catch (std::exception& e) {
276  edm::LogInfo("ERROR: THIS CONFIG DOES NOT EXIST: tag=")
277  << the_config_tag << " version=" << the_config_version;
278  edm::LogInfo("Exception") << e.what();
279  m_i_run_number = irun;
280  }
281  edm::LogInfo(" **************** ");
282 
283  } else if (nr == 0) {
284  m_i_run_number = irun;
285  edm::LogInfo(" no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed ");
286  edm::LogInfo(" **************** ");
287  } else {
288  m_i_run_number = irun;
289  m_i_tag = the_config_tag;
290  m_i_version = the_config_version;
291  edm::LogInfo(" the tag/version is the same -> no transfer needed ");
292  edm::LogInfo(" **************** ");
293  writeFile("last_tpg_weightGroup_settings.txt");
294  }
295  }
296  }
297 
298  delete econn;
299  } // usual way
300  edm::LogInfo("EcalTPGWeightGroupHandler") << "Ecal - > end of getNewObjects -----------";
301 }
302 
304  edm::LogInfo(" reading the input file ") << m_file_name;
305  std::ifstream fInput;
306  fInput.open(m_file_name);
307  if (!fInput.is_open()) {
308  edm::LogInfo("ERROR : cannot open file ") << m_file_name;
309  exit(1);
310  }
311  int weightGroup, stripEBId, stripEEId;
312  EcalTPGWeightGroup* weightG = new EcalTPGWeightGroup;
313  for (int strip = 0; strip < kEBStrips; strip++) {
314  fInput >> stripEBId >> weightGroup;
315  weightG->setValue(stripEBId, weightGroup);
316  }
317  for (int strip = 0; strip < kEEStrips; strip++) {
318  fInput >> stripEEId >> weightGroup;
319  weightG->setValue(stripEEId, weightGroup);
320  }
321  try {
322  Time_t snc = (Time_t)m_firstRun;
323  m_to_transfer.push_back(std::make_pair((EcalTPGWeightGroup*)weightG, snc));
324  } catch (std::exception& e) {
325  edm::LogInfo("EcalTPGWeightGroupHandler::readtxtFile error : ") << e.what();
326  }
327  edm::LogInfo(" **************** ");
328 }
329 
331  edm::LogInfo(" reading the input file ") << m_file_name;
332  std::ifstream fxml;
333  fxml.open(m_file_name);
334  if (!fxml.is_open()) {
335  edm::LogInfo("ERROR : cannot open file ") << m_file_name;
336  exit(1);
337  }
338  std::string dummyLine, bid;
339  int weightGroup, stripEBId, stripEEId;
340  EcalTPGWeightGroup* weightG = new EcalTPGWeightGroup;
341  for (int i = 0; i < 6; i++)
342  std::getline(fxml, dummyLine); // skip first lines
343  fxml >> bid;
344  std::size_t found = bid.find("</");
345  std::string stt = bid.substr(7, found - 7);
346  for (int i = 0; i < 2; i++)
347  std::getline(fxml, dummyLine); // <item_version>0</item_version>
348  for (int strip = 0; strip < kEBStrips; strip++) {
349  std::getline(fxml, dummyLine); // <item
350  fxml >> bid; // <first
351  found = bid.find("</");
352  stt = bid.substr(7, found - 7);
353  std::istringstream sg1(stt);
354  sg1 >> stripEBId;
355  std::getline(fxml, dummyLine);
356  fxml >> bid; // <second
357  found = bid.find("</");
358  stt = bid.substr(8, found - 8);
359  std::istringstream sg2(stt);
360  sg2 >> weightGroup;
361  weightG->setValue(stripEBId, weightGroup);
362  for (int i = 0; i < 2; i++)
363  std::getline(fxml, dummyLine); // </item>
364  }
365  for (int strip = 0; strip < kEEStrips; strip++) {
366  std::getline(fxml, dummyLine); // <item
367  fxml >> bid; // <first
368  found = bid.find("</");
369  stt = bid.substr(7, found - 7);
370  std::istringstream sg1(stt);
371  sg1 >> stripEEId;
372  std::getline(fxml, dummyLine);
373  fxml >> bid; // <second
374  found = bid.find("</");
375  stt = bid.substr(8, found - 8);
376  std::istringstream sg2(stt);
377  sg2 >> weightGroup;
378  weightG->setValue(stripEEId, weightGroup);
379  for (int i = 0; i < 2; i++)
380  std::getline(fxml, dummyLine); // </item>
381  }
382  try {
383  Time_t snc = (Time_t)m_firstRun;
384  m_to_transfer.push_back(std::make_pair((EcalTPGWeightGroup*)weightG, snc));
385  } catch (std::exception& e) {
386  edm::LogInfo("EcalTPGWeightGroupHandler::readtxtFile error : ") << e.what();
387  }
388  edm::LogInfo(" **************** ");
389 }
390 
392  //-------------------------------------------------------------
393 
394  m_i_tag = "";
395  m_i_version = 0;
396  m_i_run_number = 0;
397  m_i_weightGroup = 0;
398 
399  FILE* inpFile; // input file
400  inpFile = fopen(inputFile, "r");
401  if (!inpFile) {
402  edm::LogError("EcalTPGWeightGroupHandler") << "*** Can not open file: " << inputFile;
403  return;
404  }
405 
406  char line[256];
407 
408  std::ostringstream str;
409 
410  fgets(line, 255, inpFile);
411  m_i_tag = to_string(line);
412  str << "gen tag " << m_i_tag << std::endl; // should I use this?
413 
414  fgets(line, 255, inpFile);
415  m_i_version = atoi(line);
416  str << "version= " << m_i_version << std::endl;
417 
418  fgets(line, 255, inpFile);
419  m_i_run_number = atoi(line);
420  str << "run_number= " << m_i_run_number << std::endl;
421 
422  fgets(line, 255, inpFile);
423  m_i_weightGroup = atoi(line);
424  str << "weightGroup_config= " << m_i_weightGroup << std::endl;
425 
426  fclose(inpFile); // close inp. file
427 }
428 
430  //-------------------------------------------------------------
431 
432  std::ofstream myfile;
433  myfile.open(inputFile);
434  myfile << m_i_tag << std::endl;
435  myfile << m_i_version << std::endl;
436  myfile << m_i_run_number << std::endl;
437  myfile << m_i_weightGroup << std::endl;
438 
439  myfile.close();
440 }
void setRunTypeDef(const RunTypeDef &runTypeDef)
Definition: RunTag.cc:42
std::vector< RunIOV > getRuns()
Definition: RunList.cc:25
const Int_t kEBStrips
std::string getConfigTag() const
int getVersion() const
Definition: RunTag.h:13
Log< level::Error, false > LogError
void setVersion(int id)
void readFromFile(CaloCluster &c, FILE *file)
int getID2() const
Definition: EcalLogicID.cc:32
std::string getName() const
Definition: EcalLogicID.cc:26
EcalTPGWeightGroupHandler(edm::ParameterSet const &)
int getWeiId() const
void setValue(const uint32_t &rawId, const uint32_t &ObjectId)
Definition: EcalTPGGroups.cc:7
int getID1() const
Definition: EcalLogicID.cc:30
const Int_t kEEStrips
void setLocationDef(const LocationDef &locDef)
Definition: RunTag.cc:33
Log< level::Info, false > LogInfo
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
void setConfigTag(std::string x)
Definition: IODConfig.h:29
void setLocation(std::string loc)
Definition: LocationDef.cc:20
int getID3() const
Definition: EcalLogicID.cc:34
void setGeneralTag(std::string tag)
Definition: RunTag.cc:24
#define str(s)
tuple size
Write out results.