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