CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
EcalTPGPedestalsHandler.cc
Go to the documentation of this file.
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", "EcalTPGPedestalsHandler")) {
25  edm::LogInfo("EcalTPGPedestalsHandler") << "EcalTPGPedestals Source handler constructor";
26  m_firstRun = static_cast<unsigned int>(atoi(ps.getParameter<std::string>("firstRun").c_str()));
27  m_lastRun = static_cast<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("EcalTPGPedestalsHandler") << m_sid << "/" << m_user << "/" << m_location << "/" << m_gentag;
37 }
38 
40 
42  edm::LogInfo("EcalTPGPedestalsHandler") << "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("EcalTPGPedestalsHandler") << "max_since = " << max_since;
57  edm::LogInfo("EcalTPGPedestalsHandler") << "Retrieved last payload ";
58 
59  // here we retrieve all the runs after the last from online DB
60  edm::LogInfo("EcalTPGPedestalsHandler") << "Retrieving run list from ONLINE DB ... " << std::endl;
61 
62  edm::LogInfo("EcalTPGPedestalsHandler") << "Making connection..." << std::flush;
63  econn = new EcalCondDBInterface(m_sid, m_user, m_pass);
64  edm::LogInfo("EcalTPGPedestalsHandler") << "Done." << std::endl;
65 
66  if (!econn) {
67  std::cout << " Connection parameters " << m_sid << "/" << m_user << std::endl;
68  throw cms::Exception("OMDS not available");
69  }
70 
72  my_locdef.setLocation(m_location);
73 
75  my_rundef.setRunType(m_runtype);
76 
77  RunTag my_runtag;
78  my_runtag.setLocationDef(my_locdef);
79  my_runtag.setRunTypeDef(my_rundef);
80  my_runtag.setGeneralTag(m_gentag);
81 
82  readFromFile("last_tpg_ped_settings.txt");
83 
84  unsigned int min_run;
85 
86  if (m_firstRun < m_i_run_number) {
87  min_run = m_i_run_number + 1;
88  } else {
89  min_run = m_firstRun;
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("EcalTPGPedestalsHandler") << "min_run= " << min_run << " max_run = " << max_run;
100  RunList my_list;
101  my_list = econn->fetchGlobalRunListByLocation(my_runtag, min_run, max_run, my_locdef);
102  // my_list=econn->fetchRunListByLocation(my_runtag, min_run, max_run, my_locdef);
103 
104  std::vector<RunIOV> run_vec = my_list.getRuns();
105  size_t num_runs = run_vec.size();
106 
107  std::cout << "number of runs is : " << num_runs << std::endl;
108 
109  unsigned int irun = 0;
110 
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 
115  std::cout << " **************** " << std::endl;
116  std::cout << " **************** " << std::endl;
117  std::cout << " run= " << irun << std::endl;
118 
119  // retrieve the data :
120  std::map<EcalLogicID, RunTPGConfigDat> dataset;
121  econn->fetchDataSet(&dataset, &run_vec[kr]);
122 
123  std::string the_config_tag = "";
124  int the_config_version = 0;
125 
126  std::map<EcalLogicID, RunTPGConfigDat>::const_iterator it;
127 
128  int nr = 0;
129  for (it = dataset.begin(); it != dataset.end(); it++) {
130  ++nr;
131  //EcalLogicID ecalid = it->first;
132 
133  RunTPGConfigDat dat = it->second;
134  the_config_tag = dat.getConfigTag();
135  the_config_version = dat.getVersion();
136  }
137 
138  // it is all the same for all SM... get the last one
139 
140  std::cout << " run= " << irun << " tag " << the_config_tag << " version=" << the_config_version << std::endl;
141 
142  // here we should check if it is the same as previous run.
143 
144  if ((the_config_tag != m_i_tag || the_config_version != m_i_version) && nr > 0) {
145  std::cout << "the tag is different from last transferred run ... retrieving last config set from DB"
146  << std::endl;
147 
148  FEConfigMainInfo fe_main_info;
149  fe_main_info.setConfigTag(the_config_tag);
150  fe_main_info.setVersion(the_config_version);
151 
152  try {
153  std::cout << " before fetch config set" << std::endl;
154  econn->fetchConfigSet(&fe_main_info);
155  std::cout << " after fetch config set" << std::endl;
156 
157  // now get TPGPedestals
158  int pedId = fe_main_info.getPedId();
159 
160  if (pedId != m_i_ped) {
161  FEConfigPedInfo fe_ped_info;
162  fe_ped_info.setId(pedId);
163  econn->fetchConfigSet(&fe_ped_info);
164  std::map<EcalLogicID, FEConfigPedDat> dataset_TpgPed;
165  econn->fetchDataSet(&dataset_TpgPed, &fe_ped_info);
166 
167  // NB new
169  typedef std::map<EcalLogicID, FEConfigPedDat>::const_iterator CIfeped;
170  EcalLogicID ecid_xt;
171  FEConfigPedDat rd_ped;
172  int icells = 0;
173  for (CIfeped p = dataset_TpgPed.begin(); p != dataset_TpgPed.end(); p++) {
174  ecid_xt = p->first;
175  rd_ped = p->second;
176 
177  std::string ecid_name = ecid_xt.getName();
178 
179  // EB data
180  if (ecid_name == "EB_crystal_number") {
181  if (icells < 10)
182  std::cout << " copy the EB data "
183  << " icells = " << icells << std::endl;
184  int sm_num = ecid_xt.getID1();
185  int xt_num = ecid_xt.getID2();
186 
187  EBDetId ebdetid(sm_num, xt_num, EBDetId::SMCRYSTALMODE);
189  item.mean_x1 = (unsigned int)rd_ped.getPedMeanG1();
190  item.mean_x6 = (unsigned int)rd_ped.getPedMeanG6();
191  item.mean_x12 = (unsigned int)rd_ped.getPedMeanG12();
192 
193  peds->insert(std::make_pair(ebdetid.rawId(), item));
194  ++icells;
195  } else if (ecid_name == "EE_crystal_number") {
196  // EE data
197  int z = ecid_xt.getID1();
198  int x = ecid_xt.getID2();
199  int y = ecid_xt.getID3();
200  EEDetId eedetid(x, y, z, EEDetId::XYMODE);
202  item.mean_x1 = (unsigned int)rd_ped.getPedMeanG1();
203  item.mean_x6 = (unsigned int)rd_ped.getPedMeanG6();
204  item.mean_x12 = (unsigned int)rd_ped.getPedMeanG12();
205 
206  peds->insert(std::make_pair(eedetid.rawId(), item));
207  ++icells;
208  }
209  }
210 
211  Time_t snc = (Time_t)irun;
212  m_to_transfer.push_back(std::make_pair((EcalTPGPedestals*)peds, 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_ped = pedId;
218 
219  writeFile("last_tpg_ped_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_ped_settings.txt");
227 
228  std::cout << " even if the tag/version is not the same, the pedestals id is the same -> no transfer needed "
229  << std::endl;
230  }
231 
232  }
233 
234  catch (std::exception& e) {
235  std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" << the_config_tag << " version=" << the_config_version
236  << std::endl;
237  std::cout << e.what() << std::endl;
238  m_i_run_number = irun;
239  }
240  std::cout << " **************** " << std::endl;
241 
242  } else if (nr == 0) {
243  m_i_run_number = irun;
244  std::cout << " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed " << std::endl;
245  std::cout << " **************** " << std::endl;
246  } else {
247  m_i_run_number = irun;
248  m_i_tag = the_config_tag;
249  m_i_version = the_config_version;
250  std::cout << " the tag/version is the same -> no transfer needed " << std::endl;
251  std::cout << " **************** " << std::endl;
252  writeFile("last_tpg_ped_settings.txt");
253  }
254  }
255  }
256 
257  delete econn;
258 
259  edm::LogInfo("EcalTPGPedestalsHandler") << "Ecal - > end of getNewObjects -----------";
260 }
261 
263  //-------------------------------------------------------------
264 
265  m_i_tag = "";
266  m_i_version = 0;
267  m_i_run_number = 0;
268  m_i_ped = 0;
269 
270  FILE* inpFile; // input file
271  inpFile = fopen(inputFile, "r");
272  if (!inpFile) {
273  edm::LogError("EcalTPGPedestalsHandler") << "*** Can not open file: " << inputFile;
274  return;
275  }
276 
277  char line[256];
278 
279  std::ostringstream str;
280 
281  fgets(line, 255, inpFile);
282  m_i_tag = to_string(line);
283  str << "gen tag " << m_i_tag << std::endl; // should I use this?
284 
285  fgets(line, 255, inpFile);
286  m_i_version = atoi(line);
287  str << "version= " << m_i_version << std::endl;
288 
289  fgets(line, 255, inpFile);
290  m_i_run_number = atoi(line);
291  str << "run_number= " << m_i_run_number << std::endl;
292 
293  fgets(line, 255, inpFile);
294  m_i_ped = atoi(line);
295  str << "ped_config= " << m_i_ped << std::endl;
296 
297  fclose(inpFile); // close inp. file
298 }
299 
301  //-------------------------------------------------------------
302 
303  std::ofstream myfile;
304  myfile.open(inputFile);
305  myfile << m_i_tag << std::endl;
306  myfile << m_i_version << std::endl;
307  myfile << m_i_run_number << std::endl;
308  myfile << m_i_ped << std::endl;
309 
310  myfile.close();
311 }
void setRunTypeDef(const RunTypeDef &runTypeDef)
Definition: RunTag.cc:42
std::vector< RunIOV > getRuns()
Definition: RunList.cc:25
std::string getConfigTag() const
float getPedMeanG1() const
int getVersion() const
Definition: RunTag.h:13
static const int XYMODE
Definition: EEDetId.h:335
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:57
std::string to_string(const V &value)
Definition: OMSAccess.h:71
Log< level::Error, false > LogError
void setVersion(int id)
void readFromFile(CaloCluster &c, FILE *file)
int getID2() const
Definition: EcalLogicID.cc:32
void writeFile(const char *inputFile)
std::string getName() const
Definition: EcalLogicID.cc:26
int getID1() const
Definition: EcalLogicID.cc:30
float getPedMeanG12() 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 setRunType(std::string runtype)
Definition: RunTypeDef.cc:21
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
static std::vector< std::string > checklist dat
void readFromFile(const char *inputFile)
float getPedMeanG6() const
int getPedId() const
cond::Time_t Time_t
Definition: Time.h:18
void setConfigTag(std::string x)
Definition: IODConfig.h:29
EcalTPGPedestalsHandler(edm::ParameterSet const &)
void setId(int id)
void setLocation(std::string loc)
Definition: LocationDef.cc:20
tuple cout
Definition: gather_cfg.py:144
int getID3() const
Definition: EcalLogicID.cc:34
void setGeneralTag(std::string tag)
Definition: RunTag.cc:24
static const int SMCRYSTALMODE
Definition: EBDetId.h:159
#define str(s)
EcalTPGPedestalsMap EcalTPGPedestals
tuple size
Write out results.