CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
EcalTPGBadStripHandler.cc
Go to the documentation of this file.
2 
10 
12 
13 #include<iostream>
14 #include<fstream>
15 
16 
17 #include <time.h>
18 #include <unistd.h>
19 
20 #include <string>
21 #include <cstdio>
22 #include <typeinfo>
23 #include <sstream>
24 
26  : m_name(ps.getUntrackedParameter<std::string>("name","EcalTPGBadStripHandler")) {
27 
28  edm::LogInfo("EcalTPGBadStripHandler") << "EcalTPGStripStatus Source handler constructor.";
29  m_firstRun=static_cast<unsigned int>(atoi( ps.getParameter<std::string>("firstRun").c_str()));
30  m_lastRun=static_cast<unsigned int>(atoi( ps.getParameter<std::string>("lastRun").c_str()));
31  m_sid= ps.getParameter<std::string>("OnlineDBSID");
32  m_user= ps.getParameter<std::string>("OnlineDBUser");
33  m_pass= ps.getParameter<std::string>("OnlineDBPassword");
34  m_locationsource= ps.getParameter<std::string>("LocationSource");
35  m_location=ps.getParameter<std::string>("Location");
36  m_gentag=ps.getParameter<std::string>("GenTag");
37  m_runtype=ps.getParameter<std::string>("RunType");
38 
39  edm::LogInfo("EcalTPGBadStripHandler") << m_sid<<"/"<<m_user<<"/"<<m_location<<"/"<<m_gentag;
40 
41 }
42 
44 {
45 }
46 
47 
49 {
50  edm::LogInfo("EcalTPGBadStripHandler") << "Started GetNewObjects!!!";
51 
52  unsigned int max_since=0;
53  max_since=static_cast<unsigned int>(tagInfo().lastInterval.first);
54  edm::LogInfo("EcalTPGBadStripHandler") << "max_since : " << max_since;
55  edm::LogInfo("EcalTPGBadStripHandler") << "retrieved last payload ";
56 
57  // here we retrieve all the runs after the last from online DB
58  edm::LogInfo("EcalTPGBadStripHandler") << "Retrieving run list from ONLINE DB ... ";
59 
60  edm::LogInfo("EcalTPGBadStripHandler") << "Making connection...";
61  econn = new EcalCondDBInterface( m_sid, m_user, m_pass );
62  edm::LogInfo("EcalTPGBadStripHandler") << "Done.";
63 
64  if (!econn)
65  {
66  std::cout << " connection parameters " <<m_sid <<"/"<<m_user<<std::endl;
67  throw cms::Exception("OMDS not available");
68  }
69 
70  LocationDef my_locdef;
71  my_locdef.setLocation(m_location);
72 
73  RunTypeDef my_rundef;
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_badStrip_settings.txt");
82 
83 
84  unsigned int min_run=m_i_run_number+1;
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<< std::endl;
96 
97  unsigned int max_run=m_lastRun;
98  edm::LogInfo("EcalTPGBadStripHandler") << "min_run= " << min_run << "max_run= " << max_run;
99 
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 
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  std::string str="";
111 
112  unsigned int irun=0;
113 
114  if(num_runs>0){
115 
116  // going to query the ecal logic id
117  std::vector<EcalLogicID> my_StripEcalLogicId_EE;
118  my_StripEcalLogicId_EE = econn->getEcalLogicIDSetOrdered("EE_trigger_strip",
119  1, 1000, //"TCC"
120  1, 100, //tower
121  0, 5, //strip
122  "EE_offline_stripid",123 );
123 
124  std::cout <<" GOT the logic ID for the EE trigger strips "<< std::endl;
125 
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 
150  RunTPGConfigDat dat = it->second;
151  the_config_tag=dat.getConfigTag();
152  the_config_version=dat.getVersion();
153  }
154 
155  // it is all the same for all SM... get the last one
156 
157  std::cout<<" run= "<<irun<<" tag "<<the_config_tag<<" version="<<the_config_version <<std::endl;
158 
159  // here we should check if it is the same as previous run.
160 
161  if((the_config_tag != m_i_tag || the_config_version != m_i_version ) && nr>0 ) {
162  std::cout<<" run= "<<irun<<" tag "<<the_config_tag<<" version="<<the_config_version <<std::endl;
163  std::cout<<"the tag is different from last transferred run ... retrieving last config set from DB"<<std::endl;
164 
165  FEConfigMainInfo fe_main_info;
166  fe_main_info.setConfigTag(the_config_tag);
167  fe_main_info.setVersion(the_config_version);
168 
169  try{
170 
171  econn-> fetchConfigSet(&fe_main_info);
172 
173  // now get TPGStripStatus
174  int badstripId=fe_main_info.getBstId();
175 
176  if( badstripId != m_i_badStrip ) {
177 
178  FEConfigBadStripInfo fe_badStrip_info;
179  fe_badStrip_info.setId(badstripId);
180 
181  econn-> fetchConfigSet(&fe_badStrip_info);
182 
183  std::vector< FEConfigBadStripDat > dataset_TpgBadStrip;
184 
185  econn->fetchConfigDataSet(&dataset_TpgBadStrip, &fe_badStrip_info);
186 
187  EcalTPGStripStatus* stripStatus = new EcalTPGStripStatus;
188  typedef std::vector<FEConfigBadStripDat>::const_iterator CIfeped;
189 
190  FEConfigBadStripDat rd_badStrip;
191  //unsigned int rd_stripStatus;
192 
193 
194  // put at 1 the strip that are bad
195  int icells=0;
196  int icellsFound=0;
197  for (CIfeped p = dataset_TpgBadStrip.begin(); p != dataset_TpgBadStrip.end(); p++) {
198 
199  rd_badStrip = *p;
200 
201  //int fed_num=rd_badStrip.getFedId();
202  int tcc_num=rd_badStrip.getTCCId();
203  int tt_num=rd_badStrip.getTTId();
204  int strip_num=rd_badStrip.getStripId();
205 
206  //std::cout << fed_num << " " << tcc_num << " " << tt_num << " " << strip_num << std::endl;
207 
208  // EE data
209  int stripid;
210 
211  bool set_the_strip=false;
212  for (size_t istrip=0; istrip<my_StripEcalLogicId_EE.size(); istrip++) {
213 
214  if(!set_the_strip){
215 
216  if(my_StripEcalLogicId_EE[istrip].getID1()==tcc_num
217  && my_StripEcalLogicId_EE[istrip].getID2()==tt_num
218  && my_StripEcalLogicId_EE[istrip].getID3()==strip_num
219  ){
220  stripid =my_StripEcalLogicId_EE[istrip].getLogicID();
221 
222  set_the_strip=true;
223  ++icellsFound;
224  break;
225  }
226 
227  }
228  }
229 
230 
231  if(set_the_strip){
232  stripStatus->setValue(stripid, (unsigned int)rd_badStrip.getStatus());
233  }
234 
235  ++icells;
236  }
237 
238  // std::cout << " icells = " << icells << std::endl;
239 
240  edm::LogInfo("EcalTPGBadStripHandler") << "Finished badStrip reading.";
241 
242  Time_t snc= (Time_t) irun ;
243 
244  m_to_transfer.push_back(std::make_pair((EcalTPGStripStatus*)stripStatus,snc));
245 
246  m_i_run_number=irun;
247  m_i_tag=the_config_tag;
248  m_i_version=the_config_version;
249  m_i_badStrip=badstripId;
250 
251  writeFile("last_tpg_badStrip_settings.txt");
252 
253  } else {
254 
255  m_i_run_number=irun;
256  m_i_tag=the_config_tag;
257  m_i_version=the_config_version;
258 
259  writeFile("last_tpg_badStrip_settings.txt");
260 
261  // std::cout<< " even if the tag/version is not the same, the badStrip id is the same -> no transfer needed "<< std::endl;
262 
263  }
264 
265  }
266 
267  catch (std::exception &e) {
268  std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" <<the_config_tag
269  <<" version="<<the_config_version<< std::endl;
270  std::cout << e.what() << std::endl;
271  m_i_run_number=irun;
272 
273  }
274 
275 
276  } else if(nr==0) {
277  m_i_run_number=irun;
278  // std::cout<< " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed "<< std::endl;
279  } else {
280  m_i_run_number=irun;
281  m_i_tag=the_config_tag;
282  m_i_version=the_config_version;
283 
284  writeFile("last_tpg_badStrip_settings.txt");
285  }
286  }
287  }
288 
289  delete econn;
290 
291  edm::LogInfo("EcalTPGBadStripHandler") << "Ecal - > end of getNewObjects -----------";
292 }
293 
294 
296  //-------------------------------------------------------------
297 
298  m_i_tag="";
299  m_i_version=0;
300  m_i_run_number=0;
301  m_i_badStrip=0;
302 
303  FILE *inpFile; // input file
304  inpFile = fopen(inputFile,"r");
305  if(!inpFile) {
306  edm::LogError("EcalTPGBadStripHandler")<<"*** Can not open file: "<<inputFile;
307  }
308 
309  char line[256];
310 
311  std::ostringstream str;
312 
313  fgets(line,255,inpFile);
314  m_i_tag=to_string(line);
315  str << "gen tag " << m_i_tag << std::endl ; // should I use this?
316 
317  fgets(line,255,inpFile);
318  m_i_version=atoi(line);
319  str << "version= " << m_i_version << std::endl ;
320 
321  fgets(line,255,inpFile);
322  m_i_run_number=atoi(line);
323  str << "run_number= " << m_i_run_number << std::endl ;
324 
325  fgets(line,255,inpFile);
326  m_i_badStrip=atoi(line);
327  str << "badTT_config= " << m_i_badStrip << std::endl ;
328 
329 
330  fclose(inpFile); // close inp. file
331 
332 }
333 
334 
336  //-------------------------------------------------------------
337 
338 
339  std::ofstream myfile;
340  myfile.open (inputFile);
341  myfile << m_i_tag <<std::endl;
342  myfile << m_i_version <<std::endl;
343  myfile << m_i_run_number <<std::endl;
344  myfile << m_i_badStrip <<std::endl;
345 
346  myfile.close();
347 
348 }
349 
T getParameter(std::string const &) const
std::vector< RunIOV > getRuns()
Definition: RunList.cc:35
void setLocationDef(const LocationDef locDef)
Definition: RunTag.cc:53
std::string getConfigTag() const
void writeFile(const char *inputFile)
int getVersion() const
Definition: RunTag.h:13
void setVersion(int id)
EcalTPGBadStripHandler(edm::ParameterSet const &)
std::string to_string(const T &t)
Definition: Logger.cc:26
unsigned long long Time_t
Definition: Time.h:16
void setRunTypeDef(const RunTypeDef runTypeDef)
Definition: RunTag.cc:70
void readFromFile(const char *inputFile)
void setRunType(std::string runtype)
Definition: RunTypeDef.cc:33
tuple dataset
Definition: dataset.py:393
void setConfigTag(std::string x)
Definition: IODConfig.h:31
int getBstId() const
void setLocation(std::string loc)
Definition: LocationDef.cc:33
tuple cout
Definition: gather_cfg.py:121
void setGeneralTag(std::string tag)
Definition: RunTag.cc:36
void setValue(const uint32_t &id, const uint16_t &val)