CMS 3D CMS Logo

EcalTPGLutGroupHandler.cc
Go to the documentation of this file.
7 
9 
11 
13 
20 
25 
26 #include <iostream>
27 #include <fstream>
28 
29 #include <ctime>
30 #include <unistd.h>
31 
32 #include <string>
33 #include <cstdio>
34 #include <typeinfo>
35 #include <sstream>
36 
38  : m_name(ps.getUntrackedParameter<std::string>("name", "EcalTPGLutGroupHandler")) {
39  edm::LogInfo("EcalTPGLutGroupHandler") << "EcalTPGLutGroup Source handler constructor";
40  m_firstRun = static_cast<unsigned int>(atoi(ps.getParameter<std::string>("firstRun").c_str()));
41  m_lastRun = static_cast<unsigned int>(atoi(ps.getParameter<std::string>("lastRun").c_str()));
42  m_sid = ps.getParameter<std::string>("OnlineDBSID");
43  m_user = ps.getParameter<std::string>("OnlineDBUser");
44  m_pass = ps.getParameter<std::string>("OnlineDBPassword");
45  m_locationsource = ps.getParameter<std::string>("LocationSource");
46  m_location = ps.getParameter<std::string>("Location");
47  m_gentag = ps.getParameter<std::string>("GenTag");
48  m_runtype = ps.getParameter<std::string>("RunType");
49 
50  edm::LogInfo("EcalTPGLutGroupHandler") << m_sid << "/" << m_user << "/" << m_location << "/" << m_gentag;
51 }
52 
54 
56  using namespace edm;
57  using namespace std;
58 
59  edm::LogInfo("EcalTPGLutGroupHandler") << "Started GetNewObjects!!!";
60 
61  /*
62  // geometry
63  ESHandle<CaloGeometry> theGeometry;
64  ESHandle<CaloSubdetectorGeometry> theEndcapGeometry_handle, theBarrelGeometry_handle;
65  evtSetup.get<CaloGeometryRecord>().get( theGeometry );
66  evtSetup.get<EcalEndcapGeometryRecord>().get("EcalEndcap",theEndcapGeometry_handle);
67  evtSetup.get<EcalBarrelGeometryRecord>().get("EcalBarrel",theBarrelGeometry_handle);
68  evtSetup.get<IdealGeometryRecord>().get(eTTmap_);
69  theEndcapGeometry_ = &(*theEndcapGeometry_handle);
70  theBarrelGeometry_ = &(*theBarrelGeometry_handle);
71 
72  // electronics mapping
73  ESHandle< EcalElectronicsMapping > ecalmapping;
74  evtSetup.get< EcalMappingRcd >().get(ecalmapping);
75  theMapping_ = ecalmapping.product();
76 
77  const std::vector<DetId> & eeCells = theEndcapGeometry_->getValidDetIds(DetId::Ecal, EcalEndcap);
78 
79  */
80 
81  //check whats already inside of database
82  if (tagInfo().size) {
83  //check whats already inside of database
84  std::cout << "got offlineInfo = " << std::endl;
85  std::cout << "tag name = " << tagInfo().name << std::endl;
86  std::cout << "size = " << tagInfo().size << std::endl;
87  } else {
88  std::cout << " First object for this tag " << std::endl;
89  }
90 
91  unsigned int max_since = 0;
92  max_since = static_cast<unsigned int>(tagInfo().lastInterval.since);
93  edm::LogInfo("EcalTPGLutGroupHandler") << "max_since : " << max_since;
94  edm::LogInfo("EcalTPGLutGroupHandler") << "retrieved last payload ";
95 
96  // here we retrieve all the runs after the last from online DB
97  edm::LogInfo("EcalTPGLutGroupHandler") << "Retrieving run list from ONLINE DB ... ";
98 
99  edm::LogInfo("EcalTPGLutGroupHandler") << "Making connection...";
100  econn = new EcalCondDBInterface(m_sid, m_user, m_pass);
101  edm::LogInfo("EcalTPGLutGroupHandler") << "Done.";
102 
103  if (!econn) {
104  std::cout << " connection parameters " << m_sid << "/" << m_user << std::endl;
105  // cerr << e.what() << std::endl;
106  throw cms::Exception("OMDS not available");
107  }
108 
110  my_locdef.setLocation(m_location);
111 
113  my_rundef.setRunType(m_runtype);
114 
115  RunTag my_runtag;
116  my_runtag.setLocationDef(my_locdef);
117  my_runtag.setRunTypeDef(my_rundef);
118  my_runtag.setGeneralTag(m_gentag);
119 
120  readFromFile("last_tpg_lutGroup_settings.txt");
121 
122  unsigned int min_run;
123 
124  if (m_firstRun < m_i_run_number) {
125  min_run = m_i_run_number + 1;
126  } else {
127  min_run = m_firstRun;
128  }
129 
130  if (min_run < max_since) {
131  min_run = max_since + 1; // we have to add 1 to the last transferred one
132  }
133 
134  std::cout << "m_i_run_number" << m_i_run_number << "m_firstRun " << m_firstRun << "max_since " << max_since
135  << std::endl;
136 
137  unsigned int max_run = m_lastRun;
138  edm::LogInfo("EcalTPGLutGroupHandler") << "min_run= " << min_run << " max_run= " << max_run;
139 
140  RunList my_list;
141  my_list = econn->fetchGlobalRunListByLocation(my_runtag, min_run, max_run, my_locdef);
142  // my_list=econn->fetchRunListByLocation(my_runtag,min_run,max_run,my_locdef);
143 
144  std::vector<RunIOV> run_vec = my_list.getRuns();
145  size_t num_runs = run_vec.size();
146 
147  std::cout << "number of runs is : " << num_runs << std::endl;
148 
149  std::string str = "";
150 
151  unsigned int irun;
152  if (num_runs > 0) {
153  // going to query the ecal logic id
154  std::vector<EcalLogicID> my_TTEcalLogicId_EE;
155  my_TTEcalLogicId_EE = econn->getEcalLogicIDSetOrdered(
156  "EE_trigger_tower", 1, 200, 1, 70, EcalLogicID::NULLID, EcalLogicID::NULLID, "EE_offline_towerid", 12);
157  std::cout << " GOT the logic ID for the EE trigger towers " << std::endl;
158 
159  for (size_t kr = 0; kr < run_vec.size(); kr++) {
160  irun = static_cast<unsigned int>(run_vec[kr].getRunNumber());
161 
162  std::cout << " **************** " << std::endl;
163  std::cout << " run= " << irun << std::endl;
164 
165  // retrieve the data :
166  std::map<EcalLogicID, RunTPGConfigDat> dataset;
167  econn->fetchDataSet(&dataset, &run_vec[kr]);
168 
169  std::string the_config_tag = "";
170  int the_config_version = 0;
171 
172  std::map<EcalLogicID, RunTPGConfigDat>::const_iterator it;
173 
174  int nr = 0;
175  for (it = dataset.begin(); it != dataset.end(); it++) {
176  ++nr;
177  //EcalLogicID ecalid = it->first;
178 
179  RunTPGConfigDat dat = it->second;
180  the_config_tag = dat.getConfigTag();
181  the_config_version = dat.getVersion();
182  }
183 
184  // it is all the same for all SM... get the last one
185 
186  std::cout << " run= " << irun << " tag " << the_config_tag << " version=" << the_config_version << std::endl;
187 
188  // here we should check if it is the same as previous run.
189 
190  if ((the_config_tag != m_i_tag || the_config_version != m_i_version) && nr > 0) {
191  std::cout << "the tag is different from last transferred run ... retrieving last config set from DB"
192  << std::endl;
193 
194  FEConfigMainInfo fe_main_info;
195  fe_main_info.setConfigTag(the_config_tag);
196  fe_main_info.setVersion(the_config_version);
197 
198  try {
199  std::cout << " before fetch config set" << std::endl;
200  econn->fetchConfigSet(&fe_main_info);
201  std::cout << " after fetch config set" << std::endl;
202 
203  // now get TPGLutGroup
204  int lutId = fe_main_info.getLUTId();
205 
206  if (lutId != m_i_lutGroup) {
207  FEConfigLUTInfo fe_lut_info;
208  fe_lut_info.setId(lutId);
209  econn->fetchConfigSet(&fe_lut_info);
210  std::map<EcalLogicID, FEConfigLUTDat> dataset_TpgLut;
211  econn->fetchDataSet(&dataset_TpgLut, &fe_lut_info);
212 
213  EcalTPGLutGroup *lut = new EcalTPGLutGroup();
214  typedef std::map<EcalLogicID, FEConfigLUTDat>::const_iterator CIfelut;
215  EcalLogicID ecid_xt;
216  FEConfigLUTDat rd_lut;
217  int itowers = 0;
218 
219  for (CIfelut p = dataset_TpgLut.begin(); p != dataset_TpgLut.end(); p++) {
220  ecid_xt = p->first;
221  rd_lut = p->second;
222 
223  std::string ecid_name = ecid_xt.getName();
224 
225  if (ecid_name == "EB_trigger_tower") {
226  // SM number
227  int smid = ecid_xt.getID1();
228  // TT number
229  int towerid = ecid_xt.getID2();
230 
231  int tow_eta = (towerid - 1) / 4;
232  int tow_phi = ((towerid - 1) - tow_eta * 4);
233 
234  int axt = (tow_eta * 5) * 20 + tow_phi * 5 + 1;
235 
236  EBDetId id(smid, axt, EBDetId::SMCRYSTALMODE);
237  const EcalTrigTowerDetId towid = id.tower();
238 
239  /*
240  char ch[10];
241  sprintf(ch,"%d%d", smid, towerid);
242  std::string S="";
243  S.insert(0,ch);
244 
245  unsigned int towerEBId = 0;
246  towerEBId = atoi(S.c_str());
247  */
248 
249  lut->setValue(towid.rawId(), rd_lut.getLUTGroupId());
250  ++itowers;
251  } else if (ecid_name == "EE_trigger_tower") {
252  // EE data
253  // TCC number
254  int tccid = ecid_xt.getID1();
255  // TT number
256  int towerid = ecid_xt.getID2();
257 
258  bool set_the_tower = false;
259  int towid;
260  for (size_t itower = 0; itower < my_TTEcalLogicId_EE.size(); itower++) {
261  if (!set_the_tower) {
262  if (my_TTEcalLogicId_EE[itower].getID1() == tccid &&
263  my_TTEcalLogicId_EE[itower].getID2() == towerid) {
264  towid = my_TTEcalLogicId_EE[itower].getLogicID();
265  set_the_tower = true;
266  break;
267  }
268  }
269  }
270 
271  if (set_the_tower) {
272  lut->setValue(towid, rd_lut.getLUTGroupId());
273  } else {
274  std::cout << " these may be the additional towers TCC/TT " << tccid << "/" << towerid << std::endl;
275  }
276 
277  ++itowers;
278  }
279  }
280 
281  Time_t snc = (Time_t)irun;
282 
283  m_to_transfer.push_back(std::make_pair((EcalTPGLutGroup *)lut, snc));
284 
285  m_i_run_number = irun;
286  m_i_tag = the_config_tag;
287  m_i_version = the_config_version;
288  m_i_lutGroup = lutId;
289 
290  writeFile("last_tpg_lutGroup_settings.txt");
291 
292  } else {
293  m_i_run_number = irun;
294  m_i_tag = the_config_tag;
295  m_i_version = the_config_version;
296 
297  writeFile("last_tpg_lutGroup_settings.txt");
298 
299  std::cout << " even if the tag/version is not the same, the lutGroup id is the same -> no transfer needed "
300  << std::endl;
301  }
302 
303  }
304 
305  catch (std::exception &e) {
306  std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" << the_config_tag << " version=" << the_config_version
307  << std::endl;
308  std::cout << e.what() << std::endl;
309  m_i_run_number = irun;
310  }
311  std::cout << " **************** " << std::endl;
312 
313  } else if (nr == 0) {
314  m_i_run_number = irun;
315  std::cout << " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed " << std::endl;
316  std::cout << " **************** " << std::endl;
317  } else {
318  m_i_run_number = irun;
319  m_i_tag = the_config_tag;
320  m_i_version = the_config_version;
321  std::cout << " the tag/version is the same -> no transfer needed " << std::endl;
322  std::cout << " **************** " << std::endl;
323  writeFile("last_tpg_lutGroup_settings.txt");
324  }
325  }
326  }
327 
328  delete econn;
329 
330  edm::LogInfo("EcalTPGLutGroupHandler") << "Ecal - > end of getNewObjects -----------";
331 }
332 
334  //-------------------------------------------------------------
335 
336  m_i_tag = "";
337  m_i_version = 0;
338  m_i_run_number = 0;
339  m_i_lutGroup = 0;
340 
341  FILE *inpFile; // input file
342  inpFile = fopen(inputFile, "r");
343  if (!inpFile) {
344  edm::LogError("EcalTPGLutGroupHandler") << "*** Can not open file: " << inputFile;
345  return;
346  }
347 
348  char line[256];
349 
350  std::ostringstream str;
351 
352  fgets(line, 255, inpFile);
353  m_i_tag = to_string(line);
354  str << "gen tag " << m_i_tag << std::endl; // should I use this?
355 
356  fgets(line, 255, inpFile);
357  m_i_version = atoi(line);
358  str << "version= " << m_i_version << std::endl;
359 
360  fgets(line, 255, inpFile);
361  m_i_run_number = atoi(line);
362  str << "run_number= " << m_i_run_number << std::endl;
363 
364  fgets(line, 255, inpFile);
365  m_i_lutGroup = atoi(line);
366  str << "lutGroup_config= " << m_i_lutGroup << std::endl;
367 
368  fclose(inpFile); // close inp. file
369 }
370 
372  //-------------------------------------------------------------
373 
374  std::ofstream myfile;
375  myfile.open(inputFile);
376  myfile << m_i_tag << std::endl;
377  myfile << m_i_version << std::endl;
378  myfile << m_i_run_number << std::endl;
379  myfile << m_i_lutGroup << std::endl;
380 
381  myfile.close();
382 }
size
Write out results.
void setRunTypeDef(const RunTypeDef &runTypeDef)
Definition: RunTag.cc:42
std::vector< RunIOV > getRuns()
Definition: RunList.cc:25
int getLUTGroupId() const
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
void readFromFile(const char *inputFile)
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 writeFile(const char *inputFile)
void setValue(const uint32_t &rawId, const uint32_t &ObjectId)
Definition: EcalTPGGroups.cc:7
void setLocationDef(const LocationDef &locDef)
Definition: RunTag.cc:33
Log< level::Info, false > LogInfo
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:57
std::string getConfigTag() const
cond::Time_t Time_t
Definition: Time.h:18
void setConfigTag(std::string x)
Definition: IODConfig.h:29
HLT enums.
static const int NULLID
Definition: EcalLogicID.h:35
int getLUTId() const
void setGeneralTag(std::string tag)
Definition: RunTag.cc:24
static const int SMCRYSTALMODE
Definition: EBDetId.h:159
#define str(s)
EcalTPGLutGroupHandler(edm::ParameterSet const &)
void setId(int id)