CMS 3D CMS Logo

EcalTPGBadTTHandler.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","EcalTPGBadTTHandler")) {
27 
28  edm::LogInfo("EcalTPGBadTTHandler") << "EcalTPGTowerStatus 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("EcalTPGBadTTHandler") << m_sid<<"/"<<m_user<<"/"<<m_location<<"/"<<m_gentag;
40 
41 }
42 
44 {
45 }
46 
47 
49 {
50  edm::LogInfo("EcalTPGBadTTHandler") << "Started GetNewObjects!!!";
51 
52  unsigned int max_since=0;
53  max_since=static_cast<unsigned int>(tagInfo().lastInterval.first);
54  edm::LogInfo("EcalTPGBadTTHandler") << "max_since : " << max_since;
55  edm::LogInfo("EcalTPGBadTTHandler") << "retrieved last payload ";
56 
57  // here we retrieve all the runs after the last from online DB
58  edm::LogInfo("EcalTPGBadTTHandler") << "Retrieving run list from ONLINE DB ... ";
59 
60  edm::LogInfo("EcalTPGBadTTHandler") << "Making connection...";
62  edm::LogInfo("EcalTPGBadTTHandler") << "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_badTT_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("EcalTPGBadTTHandler") << "min_run= " << min_run << "max_run= " << max_run;
99 
100  RunList my_list;
101  // my_list=econn->fetchRunListByLocation(my_runtag, min_run, max_run, my_locdef);
102  my_list=econn->fetchGlobalRunListByLocation(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  std::string str="";
110 
111  unsigned int irun=0;
112  if(num_runs>0){
113 
114 
115  // going to query the ecal logic id
116  std::vector<EcalLogicID> my_TTEcalLogicId_EE;
117  my_TTEcalLogicId_EE = econn->getEcalLogicIDSetOrdered( "EE_trigger_tower",
118  1, 200,
119  1, 70,
121  "EE_offline_towerid",12 );
122  std::cout <<" GOT the logic ID for the EE trigger towers "<< std::endl;
123 
124 
125 
126  for(size_t kr=0; kr<run_vec.size(); kr++){
127 
128  irun=static_cast<unsigned int>(run_vec[kr].getRunNumber());
129 
130  // retrieve the data :
131  std::map<EcalLogicID, RunTPGConfigDat> dataset;
132  econn->fetchDataSet(&dataset, &run_vec[kr]);
133 
134  std::string the_config_tag="";
135  int the_config_version=0;
136 
137  std::map< EcalLogicID, RunTPGConfigDat>::const_iterator it;
138 
139  int nr=0;
140  for( it=dataset.begin(); it!=dataset.end(); it++ )
141  {
142  ++nr;
143  //EcalLogicID ecalid = it->first;
144 
145  RunTPGConfigDat dat = it->second;
146  the_config_tag=dat.getConfigTag();
147  the_config_version=dat.getVersion();
148  }
149 
150  // it is all the same for all SM... get the last one
151 
152 
153  // here we should check if it is the same as previous run.
154 
155 
156  if((the_config_tag != m_i_tag || the_config_version != m_i_version ) && nr>0 ) {
157  std::cout<<" run= "<<irun<<" tag "<<the_config_tag<<" version="<<the_config_version <<std::endl;
158  std::cout<<"the tag is different from last transferred run ... retrieving last config set from DB"<<std::endl;
159 
160  FEConfigMainInfo fe_main_info;
161  fe_main_info.setConfigTag(the_config_tag);
162  fe_main_info.setVersion(the_config_version);
163 
164  try{
165 
166  econn-> fetchConfigSet(&fe_main_info);
167 
168  // now get TPGTowerStatus
169  int badttId=fe_main_info.getBttId();
170 
171  if( badttId != m_i_badTT ) {
172 
173  FEConfigBadTTInfo fe_badTT_info;
174  fe_badTT_info.setId(badttId);
175 
176  econn-> fetchConfigSet(&fe_badTT_info);
177 
178  std::vector< FEConfigBadTTDat > dataset_TpgBadTT;
179 
180  econn->fetchConfigDataSet(&dataset_TpgBadTT, &fe_badTT_info);
181 
182  EcalTPGTowerStatus* towerStatus = new EcalTPGTowerStatus;
183  typedef std::vector<FEConfigBadTTDat>::const_iterator CIfeped;
184  EcalLogicID ecid_xt;
185  FEConfigBadTTDat rd_badTT;
186 
187  // reset the map
188  // EB
189  for(int ism=1; ism<=36; ism++) {
190  for(int ito=1; ito<=68; ito++) {
191  int tow_eta=(ito-1)/4;
192  int tow_phi=((ito-1)-tow_eta*4);
193  int axt=(tow_eta*5)*20 + tow_phi*5 +1 ;
195  const EcalTrigTowerDetId towid= id.tower();
196  int tower_status=0;
197  towerStatus->setValue(towid.rawId(),tower_status);
198  }
199  }
200  //EE
201  for (size_t itower=0; itower<my_TTEcalLogicId_EE.size(); itower++) {
202  int towid =my_TTEcalLogicId_EE[itower].getLogicID();
203  int tower_status=0;
204  towerStatus->setValue(towid,tower_status);
205  }
206 
207  // now put at 1 those that are bad
208  int icells=0;
209  for (CIfeped p = dataset_TpgBadTT.begin(); p != dataset_TpgBadTT.end(); p++) {
210  rd_badTT = *p;
211 
212  int tcc_num=rd_badTT.getTCCId();
213  int tt_num=rd_badTT.getTTId();
214 
215  std::cout<< " tcc/tt"<< tcc_num<<"/"<<tt_num<< std::endl;
216 
217  if(tcc_num>36 && tcc_num<=72) {
218  // SM number
219  int smid=tcc_num-54;
220  if(tcc_num<55) smid=tcc_num-18;
221  // TT number
222  int towerid=tt_num;
223 
224  int tow_eta=(towerid-1)/4;
225  int tow_phi=((towerid-1)-tow_eta*4);
226  int axt=(tow_eta*5)*20 + tow_phi*5 +1 ;
227 
228  EBDetId id(smid, axt, EBDetId::SMCRYSTALMODE ) ;
229  const EcalTrigTowerDetId towid= id.tower();
230  towerStatus->setValue(towid.rawId(),rd_badTT.getStatus());
231 
232  ++icells;
233  } else {
234  // EE data
235 
236  // TCC number
237  int tccid=tcc_num;
238  // TT number
239  int towerid=tt_num;
240 
241  bool set_the_tower=false;
242  int towid;
243  for (size_t itower=0; itower<my_TTEcalLogicId_EE.size(); itower++) {
244 
245  if(!set_the_tower){
246 
247  if(my_TTEcalLogicId_EE[itower].getID1()==tccid && my_TTEcalLogicId_EE[itower].getID2()==towerid){
248  towid =my_TTEcalLogicId_EE[itower].getLogicID();
249  set_the_tower=true;
250  break;
251  }
252  }
253 
254  }
255 
256  if(set_the_tower){
257 
258  towerStatus->setValue(towid,rd_badTT.getStatus());
259 
260 
261  } else {
262  std::cout <<" these may be the additional towers TCC/TT "
263  << tccid<<"/"<<towerid<<std::endl;
264  }
265 
266  ++icells;
267 
268  }
269  }
270 
271 
272 
273 
274 
275 
276  edm::LogInfo("EcalTPGBadTTHandler") << "Finished badTT reading.";
277 
278  Time_t snc= (Time_t) irun ;
279 
280  m_to_transfer.push_back(std::make_pair((EcalTPGTowerStatus*)towerStatus,snc));
281 
282  m_i_run_number=irun;
283  m_i_tag=the_config_tag;
284  m_i_version=the_config_version;
285  m_i_badTT=badttId;
286 
287  writeFile("last_tpg_badTT_settings.txt");
288 
289  } else {
290 
291  m_i_run_number=irun;
292  m_i_tag=the_config_tag;
293  m_i_version=the_config_version;
294 
295  writeFile("last_tpg_badTT_settings.txt");
296 
297  // std::cout<< " even if the tag/version is not the same, the badTT id is the same -> no transfer needed "<< std::endl;
298 
299  }
300 
301  }
302 
303  catch (std::exception &e) {
304  std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" <<the_config_tag
305  <<" version="<<the_config_version<< std::endl;
306  std::cout << e.what() << std::endl;
307  m_i_run_number=irun;
308 
309  }
310 
311 
312  } else if(nr==0) {
313  m_i_run_number=irun;
314  // std::cout<< " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed "<< std::endl;
315  } else {
316  m_i_run_number=irun;
317  m_i_tag=the_config_tag;
318  m_i_version=the_config_version;
319 
320  writeFile("last_tpg_badTT_settings.txt");
321  }
322  }
323  }
324 
325  delete econn;
326 
327  edm::LogInfo("EcalTPGBadTTHandler") << "Ecal - > end of getNewObjects -----------";
328 }
329 
330 
332  //-------------------------------------------------------------
333 
334  m_i_tag="";
335  m_i_version=0;
336  m_i_run_number=0;
337  m_i_badTT=0;
338 
339  FILE *inpFile; // input file
340  inpFile = fopen(inputFile,"r");
341  if(!inpFile) {
342  edm::LogError("EcalTPGBadTTHandler")<<"*** Can not open file: "<<inputFile;
343  return;
344  }
345 
346  char line[256];
347 
348  std::ostringstream str;
349 
350  fgets(line,255,inpFile);
351  m_i_tag=to_string(line);
352  str << "gen tag " << m_i_tag << std::endl ; // should I use this?
353 
354  fgets(line,255,inpFile);
355  m_i_version=atoi(line);
356  str << "version= " << m_i_version << std::endl ;
357 
358  fgets(line,255,inpFile);
359  m_i_run_number=atoi(line);
360  str << "run_number= " << m_i_run_number << std::endl ;
361 
362  fgets(line,255,inpFile);
363  m_i_badTT=atoi(line);
364  str << "badTT_config= " << m_i_badTT << std::endl ;
365 
366 
367  fclose(inpFile); // close inp. file
368 
369 }
370 
372  //-------------------------------------------------------------
373 
374 
375  std::ofstream myfile;
376  myfile.open (inputFile);
377  myfile << m_i_tag <<std::endl;
378  myfile << m_i_version <<std::endl;
379  myfile << m_i_run_number <<std::endl;
380  myfile << m_i_badTT <<std::endl;
381 
382  myfile.close();
383 
384 }
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
int getVersion() const
Definition: RunTag.h:13
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:50
void setVersion(int id)
void fetchDataSet(std::map< EcalLogicID, DATT > *fillMap, IOVT *iov) noexcept(false)
cond::ValidityInterval lastInterval
Definition: Types.h:76
std::string to_string(char value[])
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)
void writeFile(const char *inputFile)
int getTTId() const
void setLocationDef(const LocationDef &locDef)
Definition: RunTag.cc:53
EcalCondDBInterface * econn
int getBttId() const
void setRunType(std::string runtype)
Definition: RunTypeDef.cc:33
void readFromFile(const char *inputFile)
void setConfigTag(std::string x)
Definition: IODConfig.h:31
static const int NULLID
Definition: EcalLogicID.h:42
int getStatus() const
void setLocation(std::string loc)
Definition: LocationDef.cc:33
void setValue(const uint32_t &id, const uint16_t &val)
int getTCCId() const
void fetchConfigDataSet(std::vector< DATT > *fillMap, ICONF *iconf) noexcept(false)
void setGeneralTag(std::string tag)
Definition: RunTag.cc:36
static const int SMCRYSTALMODE
Definition: EBDetId.h:159
#define str(s)
int ism(int ieta, int iphi)
Definition: EcalPyUtils.cc:56
EcalTPGBadTTHandler(edm::ParameterSet const &)
std::string id() const override