CMS 3D CMS Logo

EcalTPGLutIdMapHandler.cc
Go to the documentation of this file.
2 
9 
11 
12 #include <iostream>
13 #include <fstream>
14 
15 #include <ctime>
16 #include <unistd.h>
17 
18 #include <string>
19 #include <cstdio>
20 #include <typeinfo>
21 #include <sstream>
22 
24  : m_name(ps.getUntrackedParameter<std::string>("name", "EcalTPGLutIdMapHandler")) {
25  edm::LogInfo("EcalTPGLutIdMapHandler") << "EcalTPGLutIdMap Source handler constructor";
26  m_firstRun = (unsigned int)atoi(ps.getParameter<std::string>("firstRun").c_str());
27  m_lastRun = (unsigned int)atoi(ps.getParameter<std::string>("lastRun").c_str());
28  m_sid = ps.getParameter<std::string>("OnlineDBSID");
29  m_user = ps.getParameter<std::string>("OnlineDBUser");
30  m_pass = ps.getParameter<std::string>("OnlineDBPassword");
31  m_locationsource = ps.getParameter<std::string>("LocationSource");
32  m_location = ps.getParameter<std::string>("Location");
33  m_gentag = ps.getParameter<std::string>("GenTag");
34  m_runtype = ps.getParameter<std::string>("RunType");
35 
36  edm::LogInfo("EcalTPGLutIdMapHandler") << m_sid << "/" << m_user << "/" << m_location << "/" << m_gentag;
37 }
38 
40 
42  edm::LogInfo("EcalTPGLutIdMapHandler") << "Started GetNewObjects!!!";
43 
44  //check whats already inside of database
45  if (tagInfo().size) {
46  //check whats already inside of database
47  std::cout << "got offlineInfo = " << std::endl;
48  std::cout << "tag name = " << tagInfo().name << std::endl;
49  std::cout << "size = " << tagInfo().size << std::endl;
50  } else {
51  std::cout << " First object for this tag " << std::endl;
52  }
53 
54  unsigned int max_since = 0;
55  max_since = static_cast<unsigned int>(tagInfo().lastInterval.since);
56  edm::LogInfo("EcalTPGLutIdMapHandler") << "max_since : " << max_since;
57  edm::LogInfo("EcalTPGLutIdMapHandler") << "retrieved last payload ";
58 
59  // here we retrieve all the runs after the last from online DB
60  edm::LogInfo("EcalTPGLutIdMapHandler") << "Retrieving run list from ONLINE DB ... ";
61 
62  edm::LogInfo("EcalTPGLutIdMapHandler") << "Making connection...";
63  econn = new EcalCondDBInterface(m_sid, m_user, m_pass);
64  edm::LogInfo("EcalTPGLutIdMapHandler") << "Done.";
65 
66  if (!econn) {
67  std::cout << " connection parameters " << m_sid << "/" << m_user << std::endl;
68  // cerr << e.what() << std::endl;
69  throw cms::Exception("OMDS not available");
70  }
71 
73  my_locdef.setLocation(m_location);
74 
76  my_rundef.setRunType(m_runtype);
77 
78  RunTag my_runtag;
79  my_runtag.setLocationDef(my_locdef);
80  my_runtag.setRunTypeDef(my_rundef);
81  my_runtag.setGeneralTag(m_gentag);
82 
83  readFromFile("last_tpg_lutIdMap_settings.txt");
84 
85  unsigned int min_run;
86 
87  if (m_firstRun < m_i_run_number) {
88  min_run = m_i_run_number + 1;
89  } else {
90  min_run = m_firstRun;
91  }
92 
93  if (min_run < max_since) {
94  min_run = max_since + 1; // we have to add 1 to the last transferred one
95  }
96 
97  std::cout << "m_i_run_number" << m_i_run_number << "m_firstRun " << m_firstRun << "max_since " << max_since
98  << std::endl;
99 
100  unsigned int max_run = m_lastRun;
101  edm::LogInfo("EcalTPGLutIdMapHandler") << "min_run= " << min_run << "max_run= " << max_run;
102 
103  RunList my_list;
104  my_list = econn->fetchGlobalRunListByLocation(my_runtag, min_run, max_run, my_locdef);
105  // my_list=econn->fetchRunListByLocation(my_runtag,min_run,max_run,my_locdef);
106 
107  std::vector<RunIOV> run_vec = my_list.getRuns();
108  size_t num_runs = run_vec.size();
109 
110  std::cout << "number of runs is : " << num_runs << std::endl;
111 
112  unsigned int irun;
113  if (num_runs > 0) {
114  for (size_t kr = 0; kr < run_vec.size(); kr++) {
115  irun = static_cast<unsigned int>(run_vec[kr].getRunNumber());
116 
117  std::cout << " **************** " << std::endl;
118  std::cout << " **************** " << std::endl;
119  std::cout << " run= " << irun << std::endl;
120 
121  // retrieve the data :
122  std::map<EcalLogicID, RunTPGConfigDat> dataset;
123  econn->fetchDataSet(&dataset, &run_vec[kr]);
124 
125  std::string the_config_tag = "";
126  int the_config_version = 0;
127 
128  std::map<EcalLogicID, RunTPGConfigDat>::const_iterator it;
129 
130  int nr = 0;
131  for (it = dataset.begin(); it != dataset.end(); it++) {
132  ++nr;
133  EcalLogicID ecalid = it->first;
134  RunTPGConfigDat dat = it->second;
135  the_config_tag = dat.getConfigTag();
136  the_config_version = dat.getVersion();
137  }
138 
139  // it is all the same for all SM... get the last one
140 
141  std::cout << " run= " << irun << " tag " << the_config_tag << " version=" << the_config_version << std::endl;
142 
143  // here we should check if it is the same as previous run.
144 
145  if ((the_config_tag != m_i_tag || the_config_version != m_i_version) && nr > 0) {
146  std::cout << "the tag is different from last transferred run ... retrieving last config set from DB"
147  << std::endl;
148 
149  FEConfigMainInfo fe_main_info;
150  fe_main_info.setConfigTag(the_config_tag);
151  fe_main_info.setVersion(the_config_version);
152 
153  try {
154  std::cout << " before fetch config set" << std::endl;
155  econn->fetchConfigSet(&fe_main_info);
156  std::cout << " after fetch config set" << std::endl;
157 
158  // now get TPGLutIdMap
159  int lutId = fe_main_info.getLUTId();
160 
161  if (lutId != m_i_lutIdMap) {
162  FEConfigLUTInfo fe_lut_info;
163  fe_lut_info.setId(lutId);
164  econn->fetchConfigSet(&fe_lut_info);
165  std::map<EcalLogicID, FEConfigLUTGroupDat> dataset_TpgLut;
166 
167  econn->fetchDataSet(&dataset_TpgLut, &fe_lut_info);
168  edm::LogInfo("EcalTPGLutIdMapHandler") << "Got object!";
169 
170  EcalTPGLutIdMap* lutMap = new EcalTPGLutIdMap;
171 
172  typedef std::map<EcalLogicID, FEConfigLUTGroupDat>::const_iterator CIfelut;
173  EcalLogicID ecid_xt;
174  FEConfigLUTGroupDat rd_lut;
175 
176  for (CIfelut p = dataset_TpgLut.begin(); p != dataset_TpgLut.end(); p++) {
177  ecid_xt = p->first;
178  rd_lut = p->second;
179 
180  //std::string ecid_name=ecid_xt.getName();
181 
182  unsigned int lutArray[1024];
183  for (int ilut = 0; ilut < 1024; ++ilut) {
184  lutArray[ilut] = rd_lut.getLUTValue(ilut);
185  std::cout << ilut << " " << rd_lut.getLUTValue(ilut) << std::endl;
186  }
187 
188  EcalTPGLut mylut;
189  mylut.setLut(lutArray);
190  lutMap->setValue(rd_lut.getLUTGroupId(), mylut);
191  }
192 
193  Time_t snc = (Time_t)irun;
194  m_to_transfer.push_back(std::make_pair((EcalTPGLutIdMap*)lutMap, 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_lutIdMap = lutId;
200 
201  writeFile("last_tpg_lutIdMap_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_lutIdMap_settings.txt");
209 
210  std::cout << " even if the tag/version is not the same, the lutIdMap id is the same -> no transfer needed "
211  << std::endl;
212  }
213 
214  }
215 
216  catch (std::exception& e) {
217  std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" << the_config_tag << " version=" << the_config_version
218  << std::endl;
219  std::cout << e.what() << std::endl;
220  m_i_run_number = irun;
221  }
222  std::cout << " **************** " << std::endl;
223 
224  } else if (nr == 0) {
225  m_i_run_number = irun;
226  std::cout << " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed " << std::endl;
227  std::cout << " **************** " << std::endl;
228  } else {
229  m_i_run_number = irun;
230  m_i_tag = the_config_tag;
231  m_i_version = the_config_version;
232  std::cout << " the tag/version is the same -> no transfer needed " << std::endl;
233  std::cout << " **************** " << std::endl;
234  writeFile("last_tpg_lutIdMap_settings.txt");
235  }
236  }
237  }
238 
239  delete econn;
240 
241  edm::LogInfo("EcalTPGLutIdMapHandler") << "Ecal - > end of getNewObjects -----------";
242 }
243 
245  //-------------------------------------------------------------
246 
247  m_i_tag = "";
248  m_i_version = 0;
249  m_i_run_number = 0;
250  m_i_lutIdMap = 0;
251 
252  FILE* inpFile; // input file
253  inpFile = fopen(inputFile, "r");
254  if (!inpFile) {
255  edm::LogError("EcalTPGLutIdMapHandler") << "*** Can not open file: " << inputFile;
256  return;
257  }
258 
259  char line[256];
260 
261  std::ostringstream str;
262 
263  fgets(line, 255, inpFile);
264  m_i_tag = to_string(line);
265  str << "gen tag " << m_i_tag << std::endl; // should I use this?
266 
267  fgets(line, 255, inpFile);
268  m_i_version = atoi(line);
269  str << "version= " << m_i_version << std::endl;
270 
271  fgets(line, 255, inpFile);
272  m_i_run_number = atoi(line);
273  str << "run_number= " << m_i_run_number << std::endl;
274 
275  fgets(line, 255, inpFile);
276  m_i_lutIdMap = atoi(line);
277  str << "lutIdMap_config= " << m_i_lutIdMap << std::endl;
278 
279  fclose(inpFile); // close inp. file
280 }
281 
283  //-------------------------------------------------------------
284 
285  std::ofstream myfile;
286  myfile.open(inputFile);
287  myfile << m_i_tag << std::endl;
288  myfile << m_i_version << std::endl;
289  myfile << m_i_run_number << std::endl;
290  myfile << m_i_lutIdMap << std::endl;
291 
292  myfile.close();
293 }
size
Write out results.
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:307
int getLUTValue(int i) const
void writeFile(const char *inputFile)
Definition: RunTag.h:13
EcalTPGLutIdMapHandler(edm::ParameterSet const &)
int getVersion() const
Log< level::Error, false > LogError
void setVersion(int id)
void setLut(const unsigned int *lut)
Definition: EcalTPGLut.cc:15
static std::string to_string(const XMLCh *ch)
void setValue(const uint32_t &id, const EcalTPGLut &value)
void setLocationDef(const LocationDef &locDef)
Definition: RunTag.cc:33
Log< level::Info, false > LogInfo
void readFromFile(const char *inputFile)
std::string getConfigTag() const
cond::Time_t Time_t
Definition: Time.h:18
void setConfigTag(std::string x)
Definition: IODConfig.h:29
int getLUTId() const
void setGeneralTag(std::string tag)
Definition: RunTag.cc:24
#define str(s)
void setId(int id)