CMS 3D CMS Logo

EcalTPGOddWeightGroupHandler.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", "EcalTPGOddWeightGroupHandler")) {
32  edm::LogInfo("EcalTPGOddWeightGroupHandler") << "EcalTPGOddWeightGroup 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("EcalTPGOddWeightGroupHandler") << 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("EcalTPGOddWeightGroupHandler") << "Started GetNewObjects!!!";
57 
58  //check whats already inside of database
59  if (tagInfo().size) {
60  //check whats already inside of database
61  std::cout << "got offlineInfo = " << std::endl;
62  std::cout << "tag name = " << tagInfo().name << std::endl;
63  std::cout << "size = " << tagInfo().size << std::endl;
64  } else {
65  std::cout << " First object for this tag " << std::endl;
66  }
67 
68  unsigned int max_since = 0;
69  max_since = static_cast<unsigned int>(tagInfo().lastInterval.since);
70  edm::LogInfo("EcalTPGOddWeightGroupHandler") << "max_since : " << max_since;
71 
72  edm::LogInfo("EcalTPGOddWeightGroupHandler") << "retrieved last payload ";
73 
74  // here we retrieve all the runs after the last from online DB
75 
76  edm::LogInfo("EcalTPGOddWeightGroupHandler") << "Retrieving run list from ONLINE DB ... ";
77 
78  edm::LogInfo("EcalTPGOddWeightGroupHandler") << "Making connection...";
79  econn = new EcalCondDBInterface(m_sid, m_user, m_pass);
80  edm::LogInfo("EcalTPGOddWeightGroupHandler") << "Done.";
81 
82  if (!econn) {
83  std::cout << " connection parameters " << m_sid << "/" << m_user << std::endl;
84  // cerr << e.what() << std::endl;
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_OddweightGroup_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  std::cout << "m_i_run_number" << m_i_run_number << "m_firstRun " << m_firstRun << "max_since " << max_since
114  << std::endl;
115 
116  unsigned int max_run = m_lastRun;
117  edm::LogInfo("EcalTPGOddWeightGroupHandler") << "min_run= " << min_run << " max_run= " << max_run;
118 
119  RunList my_list;
120  my_list = econn->fetchGlobalRunListByLocation(my_runtag, min_run, max_run, my_locdef);
121  // my_list=econn->fetchRunListByLocation(my_runtag,min_run,max_run,my_locdef);
122 
123  std::vector<RunIOV> run_vec = my_list.getRuns();
124  size_t num_runs = run_vec.size();
125 
126  std::cout << "number of runs is : " << num_runs << std::endl;
127 
128  unsigned int irun = 0;
129  if (num_runs > 0) {
130  // going to query the ecal logic id
131  std::vector<EcalLogicID> my_StripEcalLogicId_EE;
132  my_StripEcalLogicId_EE =
133  econn->getEcalLogicIDSetOrdered("ECAL_readout_strip", 1, 2000, 1, 70, 0, 5, "EE_offline_stripid", 123);
134 
135  std::cout << " GOT the logic ID for the EE trigger strips " << std::endl;
136 
137  for (size_t kr = 0; kr < run_vec.size(); kr++) {
138  irun = static_cast<unsigned int>(run_vec[kr].getRunNumber());
139 
140  std::cout << " **************** " << std::endl;
141  std::cout << " **************** " << std::endl;
142  std::cout << " run= " << irun << std::endl;
143 
144  // retrieve the data :
145  std::map<EcalLogicID, RunTPGConfigDat> dataset;
146  econn->fetchDataSet(&dataset, &run_vec[kr]);
147 
148  std::string the_config_tag = "";
149  int the_config_version = 0;
150 
151  std::map<EcalLogicID, RunTPGConfigDat>::const_iterator it;
152 
153  int nr = 0;
154  for (it = dataset.begin(); it != dataset.end(); it++) {
155  ++nr;
156  //EcalLogicID ecalid = it->first;
157 
158  RunTPGConfigDat dat = it->second;
159  the_config_tag = dat.getConfigTag();
160  the_config_version = dat.getVersion();
161  }
162 
163  // it is all the same for all SM... get the last one
164 
165  std::cout << " run= " << irun << " tag " << the_config_tag << " version=" << the_config_version << std::endl;
166 
167  // here we should check if it is the same as previous run.
168 
169  if ((the_config_tag != m_i_tag || the_config_version != m_i_version) && nr > 0) {
170  std::cout << "the tag is different from last transferred run ... retrieving last config set from DB"
171  << std::endl;
172 
173  FEConfigMainInfo fe_main_info;
174  fe_main_info.setConfigTag(the_config_tag);
175  fe_main_info.setVersion(the_config_version);
176 
177  try {
178  std::cout << " before fetch config set" << std::endl;
179  econn->fetchConfigSet(&fe_main_info);
180  std::cout << " after fetch config set" << std::endl;
181 
182  // now get TPGOddWeightGroup
183  int wId = fe_main_info.getWei2Id();
184 
185  if (wId != m_i_oddweightGroup) {
186  FEConfigOddWeightInfo fe_oddw_info;
187  fe_oddw_info.setId(wId);
188  econn->fetchConfigSet(&fe_oddw_info);
189  std::map<EcalLogicID, FEConfigOddWeightDat> dataset_TpgW;
190  econn->fetchDataSet(&dataset_TpgW, &fe_oddw_info);
191 
193  typedef std::map<EcalLogicID, FEConfigOddWeightDat>::const_iterator CIfesli;
194  EcalLogicID ecid_xt;
195  int weightGroup;
196 
197  std::map<std::string, int> map;
199 
200  for (CIfesli p = dataset_TpgW.begin(); p != dataset_TpgW.end(); p++) {
201  ecid_xt = p->first;
202  weightGroup = p->second.getWeightGroupId();
203 
204  std::string ecid_name = ecid_xt.getName();
205 
206  // EB data
207  if (ecid_name == "EB_VFE") {
208  int sm = ecid_xt.getID1();
209  int tt = ecid_xt.getID2();
210  int strip = ecid_xt.getID3();
211  int tcc = sm + 54;
212  if (sm > 18)
213  tcc = sm + 18;
214 
215  // simple formula to calculate the Srip EB identifier
216 
217  unsigned int stripEBId = 303176 + (tt - 1) * 64 + (strip - 1) * 8 + (tcc - 37) * 8192;
218 
219  weightG->setValue(stripEBId, weightGroup);
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  std::cout << " these may be the additional towers TCC/TT " << id1 << "/" << id2 << std::endl;
245  }
246  }
247  }
248 
249  Time_t snc = (Time_t)irun;
250 
251  m_to_transfer.push_back(std::make_pair((EcalTPGOddWeightGroup*)weightG, snc));
252 
253  m_i_run_number = irun;
254  m_i_tag = the_config_tag;
255  m_i_version = the_config_version;
256  m_i_oddweightGroup = wId;
257 
258  writeFile("last_tpg_OddweightGroup_settings.txt");
259 
260  } else {
261  m_i_run_number = irun;
262  m_i_tag = the_config_tag;
263  m_i_version = the_config_version;
264 
265  writeFile("last_tpg_OddweightGroup_settings.txt");
266 
267  std::cout
268  << " even if the tag/version is not the same, the weight group id is the same -> no transfer needed "
269  << std::endl;
270  }
271 
272  }
273 
274  catch (std::exception& e) {
275  std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" << the_config_tag
276  << " version=" << the_config_version << std::endl;
277  std::cout << e.what() << std::endl;
278  m_i_run_number = irun;
279  }
280  std::cout << " **************** " << std::endl;
281 
282  } else if (nr == 0) {
283  m_i_run_number = irun;
284  std::cout << " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed " << std::endl;
285  std::cout << " **************** " << std::endl;
286  } else {
287  m_i_run_number = irun;
288  m_i_tag = the_config_tag;
289  m_i_version = the_config_version;
290  std::cout << " the tag/version is the same -> no transfer needed " << std::endl;
291  std::cout << " **************** " << std::endl;
292  writeFile("last_tpg_OddweightGroup_settings.txt");
293  }
294  }
295  }
296 
297  delete econn;
298  } // usual way
299  edm::LogInfo("EcalTPGOddWeightGroupHandler") << "Ecal - > end of getNewObjects -----------";
300 }
301 
303  std::cout << " reading the input file " << m_file_name << std::endl;
304  std::ifstream fInput;
305  fInput.open(m_file_name);
306  if (!fInput.is_open()) {
307  std::cout << "ERROR : cannot open file " << m_file_name << std::endl;
308  exit(1);
309  }
310  int weightGroup, stripEBId, stripEEId;
312  for (int strip = 0; strip < kEBStrips; strip++) {
313  fInput >> stripEBId >> weightGroup;
314  weightG->setValue(stripEBId, weightGroup);
315  }
316  for (int strip = 0; strip < kEEStrips; strip++) {
317  fInput >> stripEEId >> weightGroup;
318  weightG->setValue(stripEEId, weightGroup);
319  }
320  try {
321  Time_t snc = (Time_t)m_firstRun;
322  m_to_transfer.push_back(std::make_pair((EcalTPGOddWeightGroup*)weightG, snc));
323  } catch (std::exception& e) {
324  std::cout << "EcalTPGOddWeightGroupHandler::readtxtFile error : " << e.what() << std::endl;
325  }
326  std::cout << " **************** " << std::endl;
327 }
328 
330  std::cout << " reading the input file " << m_file_name << std::endl;
331  std::ifstream fxml;
332  fxml.open(m_file_name);
333  if (!fxml.is_open()) {
334  std::cout << "ERROR : cannot open file " << m_file_name << std::endl;
335  exit(1);
336  }
337  std::string dummyLine, bid;
338  int weightGroup, stripEBId, stripEEId;
340  for (int i = 0; i < 6; i++)
341  std::getline(fxml, dummyLine); // skip first lines
342  fxml >> bid;
343  // std::cout << bid << std::endl;
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((EcalTPGOddWeightGroup*)weightG, snc));
385  } catch (std::exception& e) {
386  std::cout << "EcalTPGOddWeightGroupHandler::readtxtFile error : " << e.what() << std::endl;
387  }
388  std::cout << " **************** " << std::endl;
389 }
390 
392  //-------------------------------------------------------------
393 
394  m_i_tag = "";
395  m_i_version = 0;
396  m_i_run_number = 0;
397  m_i_oddweightGroup = 0;
398 
399  FILE* inpFile; // input file
400  inpFile = fopen(inputFile, "r");
401  if (!inpFile) {
402  edm::LogError("EcalTPGOddWeightGroupHandler") << "*** 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_oddweightGroup = atoi(line);
424  str << "weightGroup_config= " << m_i_oddweightGroup << 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_oddweightGroup << std::endl;
438 
439  myfile.close();
440 }
size
Write out results.
void setRunTypeDef(const RunTypeDef &runTypeDef)
Definition: RunTag.cc:42
std::vector< RunIOV > getRuns()
Definition: RunList.cc:25
const Int_t kEBStrips
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
int getID1() const
Definition: EcalLogicID.cc:30
std::string getName() const
Definition: EcalLogicID.cc:26
Definition: RunTag.h:13
int getID2() const
Definition: EcalLogicID.cc:32
int getVersion() const
Log< level::Error, false > LogError
void setVersion(int id)
static std::string to_string(const XMLCh *ch)
Definition: TTTypes.h:54
void setValue(const uint32_t &rawId, const uint32_t &ObjectId)
Definition: EcalTPGGroups.cc:7
const Int_t kEEStrips
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)
int getWei2Id() const
def exit(msg="")
EcalTPGOddWeightGroupHandler(edm::ParameterSet const &)