CMS 3D CMS Logo

EcalTPGFineGrainStripEEHandler.cc
Go to the documentation of this file.
2 
4 
6 
12 
15 
16 #include <iostream>
17 #include <fstream>
18 
19 #include <ctime>
20 #include <unistd.h>
21 
22 #include <string>
23 #include <cstdio>
24 #include <typeinfo>
25 #include <sstream>
26 
28  : m_name(ps.getUntrackedParameter<std::string>("name", "EcalTPGFineGrainStripEEHandler")) {
29  edm::LogInfo("EcalTPGFineGrainStripEEHandler") << "EcalTPGFineGrainStripEEHandler Source handler constructor";
30  m_firstRun = static_cast<unsigned int>(atoi(ps.getParameter<std::string>("firstRun").c_str()));
31  m_lastRun = static_cast<unsigned int>(atoi(ps.getParameter<std::string>("lastRun").c_str()));
32  m_sid = ps.getParameter<std::string>("OnlineDBSID");
33  m_user = ps.getParameter<std::string>("OnlineDBUser");
34  m_pass = ps.getParameter<std::string>("OnlineDBPassword");
35  m_locationsource = ps.getParameter<std::string>("LocationSource");
36  m_location = ps.getParameter<std::string>("Location");
37  m_gentag = ps.getParameter<std::string>("GenTag");
38  m_runtype = ps.getParameter<std::string>("RunType");
39 
40  edm::LogInfo("EcalTPGFineGrainStripEEHandler") << m_sid << "/" << m_user << "/" << m_location << "/" << m_gentag;
41 }
42 
44 
46  edm::LogInfo("EcalTPGFineGrainStripEEHandler") << "Started GetNewObjects!!!";
47 
48  //check whats already inside of database
49  if (tagInfo().size) {
50  //check whats already inside of database
51  std::cout << "got offlineInfo = " << std::endl;
52  std::cout << "tag name = " << tagInfo().name << std::endl;
53  std::cout << "size = " << tagInfo().size << std::endl;
54  } else {
55  std::cout << " First object for this tag " << std::endl;
56  }
57 
58  unsigned int max_since = 0;
59  max_since = static_cast<unsigned int>(tagInfo().lastInterval.since);
60  edm::LogInfo("EcalTPGFineGrainStripEEHandler") << "max_since : " << max_since;
61  edm::LogInfo("EcalTPGFineGrainStripEEHandler") << "retrieved last payload ";
62 
63  // here we retrieve all the runs after the last from online DB
64 
65  edm::LogInfo("EcalTPGFineGrainStripEEHandler") << "Retrieving run list from ONLINE DB ... ";
66 
67  edm::LogInfo("EcalTPGFineGrainStripEEHandler") << "Making connection...";
68  econn = new EcalCondDBInterface(m_sid, m_user, m_pass);
69  edm::LogInfo("EcalTPGFineGrainStripEEHandler") << "Done.";
70 
71  if (!econn) {
72  std::cout << " connection parameters " << m_sid << "/" << m_user << std::endl;
73  throw cms::Exception("OMDS not available");
74  }
75 
77  my_locdef.setLocation(m_location);
78 
80  my_rundef.setRunType(m_runtype);
81 
82  RunTag my_runtag;
83  my_runtag.setLocationDef(my_locdef);
84  my_runtag.setRunTypeDef(my_rundef);
85  my_runtag.setGeneralTag(m_gentag);
86 
87  readFromFile("last_tpg_fgrStripEE_settings.txt");
88 
89  unsigned int min_run;
90 
91  if (m_firstRun < m_i_run_number) {
92  min_run = m_i_run_number + 1;
93  } else {
94  min_run = m_firstRun;
95  }
96  if (min_run < max_since) {
97  min_run = max_since + 1; // we have to add 1 to the last transferred one
98  }
99 
100  std::cout << "m_i_run_number" << m_i_run_number << "m_firstRun " << m_firstRun << "max_since " << max_since
101  << std::endl;
102 
103  unsigned int max_run = m_lastRun;
104  edm::LogInfo("EcalTPGFineGrainStripEEHandler") << "min_run= " << min_run << "max_run= " << max_run;
105 
106  RunList my_list;
107  my_list = econn->fetchGlobalRunListByLocation(my_runtag, min_run, max_run, my_locdef);
108  // my_list=econn->fetchRunListByLocation(my_runtag,min_run,max_run, my_locdef);
109 
110  std::vector<RunIOV> run_vec = my_list.getRuns();
111  size_t num_runs = run_vec.size();
112 
113  std::cout << "number of runs is : " << num_runs << std::endl;
114 
115  unsigned int irun = 0;
116  if (num_runs > 0) {
117  // going to query the ecal logic id
118  std::vector<EcalLogicID> my_StripEcalLogicId_EE;
119  my_StripEcalLogicId_EE =
120  econn->getEcalLogicIDSetOrdered("ECAL_readout_strip", 1, 1000, 1, 100, 0, 5, "EE_offline_stripid", 123);
121  std::cout << " GOT the logic ID for the EE trigger strips " << std::endl;
122 
123  for (size_t kr = 0; kr < run_vec.size(); kr++) {
124  irun = static_cast<unsigned int>(run_vec[kr].getRunNumber());
125 
126  std::cout << " **************** " << std::endl;
127  std::cout << " **************** " << std::endl;
128  std::cout << " run= " << irun << std::endl;
129 
130  // retrieve the data :
131  std::map<EcalLogicID, RunTPGConfigDat> dataset;
132  econn->fetchDataSet(&dataset, &run_vec[kr]);
133 
134  std::string the_config_tag = "";
135  int the_config_version = 0;
136 
137  std::map<EcalLogicID, RunTPGConfigDat>::const_iterator it;
138 
139  int nr = 0;
140  for (it = dataset.begin(); it != dataset.end(); it++) {
141  ++nr;
142  //EcalLogicID ecalid = it->first;
143  RunTPGConfigDat dat = it->second;
144  the_config_tag = dat.getConfigTag();
145  the_config_version = dat.getVersion();
146  }
147 
148  // it is all the same for all SM... get the last one
149 
150  std::cout << " run= " << irun << " tag " << the_config_tag << " version=" << the_config_version << std::endl;
151 
152  // here we should check if it is the same as previous run.
153 
154  if ((the_config_tag != m_i_tag || the_config_version != m_i_version) && nr > 0) {
155  std::cout << "the tag is different from last transferred run ... retrieving last config set from DB"
156  << std::endl;
157 
158  FEConfigMainInfo fe_main_info;
159  fe_main_info.setConfigTag(the_config_tag);
160  fe_main_info.setVersion(the_config_version);
161 
162  try {
163  std::cout << " before fetch config set" << std::endl;
164  econn->fetchConfigSet(&fe_main_info);
165  std::cout << " after fetch config set" << std::endl;
166 
167  // now get TPGFineGrainStripEE
168  int fgrId = fe_main_info.getFgrId();
169 
170  if (fgrId != m_i_fgrStripEE) {
171  FEConfigFgrInfo fe_fgr_info;
172  fe_fgr_info.setId(fgrId);
173  econn->fetchConfigSet(&fe_fgr_info);
174  std::map<EcalLogicID, FEConfigFgrEEStripDat> dataset_TpgFineGrainStripEE;
175  econn->fetchDataSet(&dataset_TpgFineGrainStripEE, &fe_fgr_info);
176 
178  typedef std::map<EcalLogicID, FEConfigFgrEEStripDat>::const_iterator CIfefgr;
179  EcalLogicID ecid_xt;
180  FEConfigFgrEEStripDat rd_fgr;
181 
182  int icells = 0;
183 
184  for (CIfefgr p = dataset_TpgFineGrainStripEE.begin(); p != dataset_TpgFineGrainStripEE.end(); p++) {
185  ecid_xt = p->first;
186  rd_fgr = p->second;
187 
188  std::string ecid_name = ecid_xt.getName();
189 
190  // EB data
191  if (ecid_name == "EB_VFE") {
192  int sm = ecid_xt.getID1();
193  int tt = ecid_xt.getID2();
194  int strip = ecid_xt.getID3();
195  int tcc = sm + 54;
196  if (sm > 18)
197  tcc = sm + 18;
198 
199  // simple formula to calculate the Srip EB identifier
200 
201  unsigned int stripEBId = 303176 + (tt - 1) * 64 + (strip - 1) * 8 + (tcc - 37) * 8192;
202 
204  item.threshold = (unsigned int)rd_fgr.getThreshold();
205  item.lut = (unsigned int)rd_fgr.getLUTFgr();
206 
207  fgrStripEE->setValue(stripEBId, item);
208 
209  ++icells;
210  } else if (ecid_name == "ECAL_readout_strip") {
211  // EE data
212  // fed
213  int id1 = ecid_xt.getID1();
214  // ccu
215  int id2 = ecid_xt.getID2();
216  // Strip
217  int id3 = ecid_xt.getID3();
218 
219  bool set_the_strip = false;
220  int stripEEId;
221  for (size_t istrip = 0; istrip < my_StripEcalLogicId_EE.size(); istrip++) {
222  if (!set_the_strip) {
223  if (my_StripEcalLogicId_EE[istrip].getID1() == id1 &&
224  my_StripEcalLogicId_EE[istrip].getID2() == id2 &&
225  my_StripEcalLogicId_EE[istrip].getID3() == id3) {
226  stripEEId = my_StripEcalLogicId_EE[istrip].getLogicID();
227  set_the_strip = true;
228  break;
229  }
230  }
231  }
232 
234  item.threshold = (unsigned int)rd_fgr.getThreshold();
235  item.lut = (unsigned int)rd_fgr.getLUTFgr();
236 
237  if (set_the_strip) {
238  fgrStripEE->setValue(stripEEId, item);
239  } else {
240  std::cout << " these may be the additional towers TCC/TT " << id1 << "/" << id2 << std::endl;
241  }
242 
243  ++icells;
244  }
245  }
246 
247  Time_t snc = (Time_t)irun;
248  m_to_transfer.push_back(std::make_pair((EcalTPGFineGrainStripEE*)fgrStripEE, snc));
249 
250  m_i_run_number = irun;
251  m_i_tag = the_config_tag;
252  m_i_version = the_config_version;
253  m_i_fgrStripEE = fgrId;
254 
255  writeFile("last_tpg_fgrStripEE_settings.txt");
256 
257  } else {
258  m_i_run_number = irun;
259  m_i_tag = the_config_tag;
260  m_i_version = the_config_version;
261 
262  writeFile("last_tpg_fgrStripEE_settings.txt");
263 
264  std::cout << " even if the tag/version is not the same, the fgrStripEEestals id is the same -> no transfer "
265  "needed "
266  << std::endl;
267  }
268 
269  }
270 
271  catch (std::exception& e) {
272  std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" << the_config_tag << " version=" << the_config_version
273  << std::endl;
274  std::cout << e.what() << std::endl;
275  m_i_run_number = irun;
276  }
277  std::cout << " **************** " << std::endl;
278 
279  } else if (nr == 0) {
280  m_i_run_number = irun;
281  std::cout << " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed " << std::endl;
282  std::cout << " **************** " << std::endl;
283  } else {
284  m_i_run_number = irun;
285  m_i_tag = the_config_tag;
286  m_i_version = the_config_version;
287  std::cout << " the tag/version is the same -> no transfer needed " << std::endl;
288  std::cout << " **************** " << std::endl;
289  writeFile("last_tpg_fgrStripEE_settings.txt");
290  }
291  }
292  }
293 
294  delete econn;
295 
296  edm::LogInfo("EcalTPGFineGrainStripEEHandler") << "Ecal - > end of getNewObjects -----------";
297 }
298 
300  //-------------------------------------------------------------
301 
302  m_i_tag = "";
303  m_i_version = 0;
304  m_i_run_number = 0;
305  m_i_fgrStripEE = 0;
306 
307  FILE* inpFile; // input file
308  inpFile = fopen(inputFile, "r");
309  if (!inpFile) {
310  edm::LogError("EcalTPGFineGrainStripEEHandler") << "*** Can not open file: " << inputFile;
311  return;
312  }
313 
314  char line[256];
315 
316  std::ostringstream str;
317 
318  fgets(line, 255, inpFile);
319  m_i_tag = to_string(line);
320  str << "gen tag " << m_i_tag << std::endl; // should I use this?
321 
322  fgets(line, 255, inpFile);
323  m_i_version = atoi(line);
324  str << "version= " << m_i_version << std::endl;
325 
326  fgets(line, 255, inpFile);
327  m_i_run_number = atoi(line);
328  str << "run_number= " << m_i_run_number << std::endl;
329 
330  fgets(line, 255, inpFile);
331  m_i_fgrStripEE = atoi(line);
332  str << "fgrStripEE_config= " << m_i_fgrStripEE << std::endl;
333 
334  fclose(inpFile); // close inp. file
335 }
336 
338  //-------------------------------------------------------------
339 
340  std::ofstream myfile;
341  myfile.open(inputFile);
342  myfile << m_i_tag << std::endl;
343  myfile << m_i_version << std::endl;
344  myfile << m_i_run_number << std::endl;
345  myfile << m_i_fgrStripEE << std::endl;
346 
347  myfile.close();
348 }
size
Write out results.
void setRunTypeDef(const RunTypeDef &runTypeDef)
Definition: RunTag.cc:42
std::vector< RunIOV > getRuns()
Definition: RunList.cc:25
int getFgrId() const
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
unsigned int getLUTFgr() const
unsigned int getThreshold() const
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:77
int getVersion() const
Log< level::Error, false > LogError
void setVersion(int id)
void setValue(const uint32_t &id, const Item &value)
Definition: TTTypes.h:54
void setLocationDef(const LocationDef &locDef)
Definition: RunTag.cc:33
void setId(int id)
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)