CMS 3D CMS Logo

EcalTPGLinConstHandler.cc
Go to the documentation of this file.
2 
9 
10 #include <iostream>
11 
12 #include <fstream>
13 
14 #include <ctime>
15 #include <unistd.h>
16 
17 #include <string>
18 #include <cstdio>
19 #include <typeinfo>
20 #include <sstream>
21 
23  : m_name(ps.getUntrackedParameter<std::string>("name", "EcalTPGLinConstHandler")) {
24  edm::LogInfo("EcalTPGLinConstHandler") << "EcalTPGLinConst Source handler constructor";
25  m_firstRun = static_cast<unsigned int>(atoi(ps.getParameter<std::string>("firstRun").c_str()));
26  m_lastRun = static_cast<unsigned int>(atoi(ps.getParameter<std::string>("lastRun").c_str()));
27  m_sid = ps.getParameter<std::string>("OnlineDBSID");
28  m_user = ps.getParameter<std::string>("OnlineDBUser");
29  m_pass = ps.getParameter<std::string>("OnlineDBPassword");
30  m_locationsource = ps.getParameter<std::string>("LocationSource");
31  m_location = ps.getParameter<std::string>("Location");
32  m_gentag = ps.getParameter<std::string>("GenTag");
33  m_runtype = ps.getParameter<std::string>("RunType");
34 
35  edm::LogInfo("EcalTPGLinConstHandler") << m_sid << "/" << m_user << "/" << m_location << "/" << m_gentag;
36 }
37 
39 
41  edm::LogInfo("EcalTPGLinConstHandler") << "Started getNewObjects";
42 
43  if (tagInfo().size) {
44  //check whats already inside of database
45  std::cout << "got offlineInfo = " << std::endl;
46  std::cout << "tag name = " << tagInfo().name << std::endl;
47  std::cout << "size = " << tagInfo().size << std::endl;
48  } else {
49  std::cout << " First object for this tag " << std::endl;
50  }
51 
52  unsigned int max_since = 0;
53  max_since = static_cast<unsigned int>(tagInfo().lastInterval.since);
54  edm::LogInfo("EcalTPGLinConstHandler") << "max_since = " << max_since;
55  edm::LogInfo("EcalTPGLinConstHandler") << "Retrieved last payload ";
56 
57  // here we retrieve all the runs after the last from online DB
58  edm::LogInfo("EcalTPGLinConstHandler") << "Retrieving run list from ONLINE DB ... " << std::endl;
59 
60  edm::LogInfo("EcalTPGLinConstHandler") << "Making connection..." << std::flush;
61  econn = new EcalCondDBInterface(m_sid, m_user, m_pass);
62  edm::LogInfo("EcalTPGLinConstHandler") << "Done." << std::endl;
63 
64  if (!econn) {
65  std::cout << " connection parameters " << m_sid << "/" << m_user << std::endl;
66  // cerr << e.what() << std::endl;
67  throw cms::Exception("OMDS not available");
68  }
69 
71  my_locdef.setLocation(m_location);
72 
74  my_rundef.setRunType(m_runtype);
75 
76  RunTag my_runtag;
77  my_runtag.setLocationDef(my_locdef);
78  my_runtag.setRunTypeDef(my_rundef);
79  my_runtag.setGeneralTag(m_gentag);
80 
81  readFromFile("last_tpg_lin_settings.txt");
82 
83  unsigned int min_run;
84 
85  if (m_firstRun < m_i_run_number) {
86  min_run = m_i_run_number + 1; // we have to add 1 to the last transferred one
87  } else {
88  min_run = m_firstRun;
89  }
90 
91  if (min_run < max_since) {
92  min_run = max_since + 1; // we have to add 1 to the last transferred one
93  }
94 
95  std::cout << "m_i_run_number" << m_i_run_number << "m_firstRun " << m_firstRun << "max_since " << max_since
96  << std::endl;
97 
98  unsigned int max_run = m_lastRun;
99  edm::LogInfo("EcalTPGLinConstHandler") << "min_run= " << min_run << "max_run = " << max_run;
100 
101  RunList my_list;
102  my_list = econn->fetchGlobalRunListByLocation(my_runtag, min_run, max_run, my_locdef);
103  // my_list=econn->fetchRunListByLocation(my_runtag, min_run, max_run, my_locdef);
104 
105  std::vector<RunIOV> run_vec = my_list.getRuns();
106  size_t num_runs = run_vec.size();
107 
108  std::cout << "number of runs is : " << num_runs << std::endl;
109 
110  unsigned int irun = 0;
111  if (num_runs > 0) {
112  for (size_t kr = 0; kr < run_vec.size(); kr++) {
113  irun = static_cast<unsigned int>(run_vec[kr].getRunNumber());
114  std::cout << " **************** " << std::endl;
115  std::cout << " **************** " << std::endl;
116  std::cout << " run= " << irun << std::endl;
117 
118  // retrieve the data :
119  std::map<EcalLogicID, RunTPGConfigDat> dataset;
120  econn->fetchDataSet(&dataset, &run_vec[kr]);
121 
122  std::string the_config_tag = "";
123  int the_config_version = 0;
124 
125  std::map<EcalLogicID, RunTPGConfigDat>::const_iterator it;
126 
127  int nr = 0;
128  for (it = dataset.begin(); it != dataset.end(); it++) {
129  ++nr;
130  //EcalLogicID ecalid = it->first;
131 
132  RunTPGConfigDat dat = it->second;
133  the_config_tag = dat.getConfigTag();
134  the_config_version = dat.getVersion();
135  }
136 
137  // it is all the same for all SM... get the last one
138 
139  std::cout << " run= " << irun << " tag " << the_config_tag << " version=" << the_config_version << std::endl;
140 
141  // here we should check if it is the same as previous run.
142 
143  if ((the_config_tag != m_i_tag || the_config_version != m_i_version) && nr > 0) {
144  std::cout << "the tag is different from last transferred run ... retrieving last config set from DB"
145  << std::endl;
146 
147  FEConfigMainInfo fe_main_info;
148  fe_main_info.setConfigTag(the_config_tag);
149  fe_main_info.setVersion(the_config_version);
150 
151  try {
152  std::cout << " before fetch config set" << std::endl;
153  econn->fetchConfigSet(&fe_main_info);
154  std::cout << " after fetch config set" << std::endl;
155 
156  // now get TPGLinConst
157  int linId = fe_main_info.getLinId();
158 
159  if (linId != m_i_lin) {
160  FEConfigLinInfo fe_lin_info;
161  fe_lin_info.setId(linId);
162  econn->fetchConfigSet(&fe_lin_info);
163  std::map<EcalLogicID, FEConfigLinDat> dataset_TpgLin;
164  econn->fetchDataSet(&dataset_TpgLin, &fe_lin_info);
165 
167  typedef std::map<EcalLogicID, FEConfigLinDat>::const_iterator CIfelin;
168  EcalLogicID ecid_xt;
169  FEConfigLinDat rd_lin;
170 
171  for (CIfelin p = dataset_TpgLin.begin(); p != dataset_TpgLin.end(); p++) {
172  ecid_xt = p->first;
173  rd_lin = p->second;
174  std::string ecid_name = ecid_xt.getName();
175 
176  //EB data
177  if (ecid_name == "EB_crystal_number") {
178  int sm_num = ecid_xt.getID1();
179  int xt_num = ecid_xt.getID2();
180  EBDetId ebdetid(sm_num, xt_num, EBDetId::SMCRYSTALMODE);
181 
183  item.mult_x1 = rd_lin.getMultX1();
184  item.mult_x6 = rd_lin.getMultX6();
185  item.mult_x12 = rd_lin.getMultX12();
186  item.shift_x1 = rd_lin.getShift1();
187  item.shift_x6 = rd_lin.getShift6();
188  item.shift_x12 = rd_lin.getShift12();
189 
190  linC->insert(std::make_pair(ebdetid.rawId(), item));
191  } else {
192  //EE data
193  int z = ecid_xt.getID1();
194  int x = ecid_xt.getID2();
195  int y = ecid_xt.getID3();
196  EEDetId eedetid(x, y, z, EEDetId::XYMODE);
197 
199 
200  item.mult_x1 = rd_lin.getMultX1();
201  item.mult_x6 = rd_lin.getMultX6();
202  item.mult_x12 = rd_lin.getMultX12();
203  item.shift_x1 = rd_lin.getShift1();
204  item.shift_x6 = rd_lin.getShift6();
205  item.shift_x12 = rd_lin.getShift12();
206 
207  linC->insert(std::make_pair(eedetid.rawId(), item));
208  }
209  }
210 
211  Time_t snc = (Time_t)irun;
212  m_to_transfer.push_back(std::make_pair((EcalTPGLinearizationConst *)linC, snc));
213 
214  m_i_run_number = irun;
215  m_i_tag = the_config_tag;
216  m_i_version = the_config_version;
217  m_i_lin = linId;
218 
219  writeFile("last_tpg_lin_settings.txt");
220 
221  } else {
222  m_i_run_number = irun;
223  m_i_tag = the_config_tag;
224  m_i_version = the_config_version;
225 
226  writeFile("last_tpg_lin_settings.txt");
227 
228  std::cout << " even if the tag/version is not the same, the linearization constants id is the same -> no "
229  "transfer needed "
230  << std::endl;
231  }
232  } catch (std::exception &e) {
233  std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" << the_config_tag << " version=" << the_config_version
234  << std::endl;
235  std::cout << e.what() << std::endl;
236  m_i_run_number = irun;
237  }
238  std::cout << " **************** " << std::endl;
239 
240  } else if (nr == 0) {
241  m_i_run_number = irun;
242  std::cout << " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed " << std::endl;
243  std::cout << " **************** " << std::endl;
244  } else {
245  m_i_run_number = irun;
246  m_i_tag = the_config_tag;
247  m_i_version = the_config_version;
248  std::cout << " the tag/version is the same -> no transfer needed " << std::endl;
249  std::cout << " **************** " << std::endl;
250  writeFile("last_tpg_lin_settings.txt");
251  }
252  }
253  }
254 
255  delete econn;
256 
257  edm::LogInfo("EcalTPGLinConstHandler") << "Ecal - > end of getNewObjects -----------";
258 }
259 
261  //-------------------------------------------------------------
262 
263  m_i_tag = "";
264  m_i_version = 0;
265  m_i_run_number = 0;
266  m_i_lin = 0;
267 
268  FILE *inpFile; // input file
269  inpFile = fopen(inputFile, "r");
270  if (!inpFile) {
271  edm::LogError("EcalTPGLinConstHandler") << "*** Can not open file: " << inputFile;
272  return;
273  }
274 
275  char line[256];
276 
277  std::ostringstream str;
278 
279  fgets(line, 255, inpFile);
280  m_i_tag = to_string(line);
281  str << "gen tag " << m_i_tag << std::endl; // should I use this?
282 
283  fgets(line, 255, inpFile);
284  m_i_version = atoi(line);
285  str << "version= " << m_i_version << std::endl;
286 
287  fgets(line, 255, inpFile);
288  m_i_run_number = atoi(line);
289  str << "run_number= " << m_i_run_number << std::endl;
290 
291  fgets(line, 255, inpFile);
292  m_i_lin = atoi(line);
293  str << "lin_config= " << m_i_lin << std::endl;
294 
295  fclose(inpFile); // close inp. file
296 }
297 
299  //-------------------------------------------------------------
300 
301  std::ofstream myfile;
302  myfile.open(inputFile);
303  myfile << m_i_tag << std::endl;
304  myfile << m_i_version << std::endl;
305  myfile << m_i_run_number << std::endl;
306  myfile << m_i_lin << std::endl;
307 
308  myfile.close();
309 }
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:303
int getMultX1() 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
int getShift12() const
static const int XYMODE
Definition: EEDetId.h:335
int getVersion() const
Log< level::Error, false > LogError
void setVersion(int id)
static std::string to_string(const XMLCh *ch)
void setId(int id)
int getLinId() const
int getMultX12() const
void setLocationDef(const LocationDef &locDef)
Definition: RunTag.cc:33
void insert(std::pair< uint32_t, Item > const &a)
Log< level::Info, false > LogInfo
void readFromFile(const char *inputFile)
int getShift1() const
EcalTPGLinConstHandler(edm::ParameterSet const &)
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
int getID3() const
Definition: EcalLogicID.cc:34
void setConfigTag(std::string x)
Definition: IODConfig.h:29
void writeFile(const char *inputFile)
int getShift6() const
void setGeneralTag(std::string tag)
Definition: RunTag.cc:24
static const int SMCRYSTALMODE
Definition: EBDetId.h:159
#define str(s)
int getMultX6() const
EcalTPGLinearizationConstMap EcalTPGLinearizationConst