CMS 3D CMS Logo

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