CMS 3D CMS Logo

EcalTPGBadStripHandler.cc
Go to the documentation of this file.
2 
10 
12 
13 #include<iostream>
14 #include<fstream>
15 
16 
17 #include <ctime>
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...";
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 
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  return;
308  }
309 
310  char line[256];
311 
312  std::ostringstream str;
313 
314  fgets(line,255,inpFile);
315  m_i_tag=to_string(line);
316  str << "gen tag " << m_i_tag << std::endl ; // should I use this?
317 
318  fgets(line,255,inpFile);
319  m_i_version=atoi(line);
320  str << "version= " << m_i_version << std::endl ;
321 
322  fgets(line,255,inpFile);
323  m_i_run_number=atoi(line);
324  str << "run_number= " << m_i_run_number << std::endl ;
325 
326  fgets(line,255,inpFile);
327  m_i_badStrip=atoi(line);
328  str << "badTT_config= " << m_i_badStrip << std::endl ;
329 
330 
331  fclose(inpFile); // close inp. file
332 
333 }
334 
335 
337  //-------------------------------------------------------------
338 
339 
340  std::ofstream myfile;
341  myfile.open (inputFile);
342  myfile << m_i_tag <<std::endl;
343  myfile << m_i_version <<std::endl;
344  myfile << m_i_run_number <<std::endl;
345  myfile << m_i_badStrip <<std::endl;
346 
347  myfile.close();
348 
349 }
350 
T getParameter(std::string const &) const
void setRunTypeDef(const RunTypeDef &runTypeDef)
Definition: RunTag.cc:70
std::vector< RunIOV > getRuns()
Definition: RunList.cc:35
RunList fetchGlobalRunListByLocation(const RunTag &tag, int min_run, int max_run, const LocationDef &locDef) noexcept(false)
std::string getConfigTag() const
void writeFile(const char *inputFile)
int getVersion() const
Definition: RunTag.h:13
void setVersion(int id)
void fetchDataSet(std::map< EcalLogicID, DATT > *fillMap, IOVT *iov) noexcept(false)
EcalTPGBadStripHandler(edm::ParameterSet const &)
cond::ValidityInterval lastInterval
Definition: Types.h:76
std::vector< EcalLogicID > getEcalLogicIDSetOrdered(std::string name, int fromId1, int toId1, int fromId2=EcalLogicID::NULLID, int toId2=EcalLogicID::NULLID, int fromId3=EcalLogicID::NULLID, int toId3=EcalLogicID::NULLID, std::string mapsTo="", int orderedBy=EcalLogicID::NULLID) noexcept(false)
std::string to_string(char value[])
void setLocationDef(const LocationDef &locDef)
Definition: RunTag.cc:53
void readFromFile(const char *inputFile)
void setRunType(std::string runtype)
Definition: RunTypeDef.cc:33
void setConfigTag(std::string x)
Definition: IODConfig.h:31
int getBstId() const
void setLocation(std::string loc)
Definition: LocationDef.cc:33
void fetchConfigDataSet(std::vector< DATT > *fillMap, ICONF *iconf) noexcept(false)
void setGeneralTag(std::string tag)
Definition: RunTag.cc:36
#define str(s)
void setValue(const uint32_t &id, const uint16_t &val)