CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
EcalTPGBadXTHandler.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","EcalTPGBadXTHandler")) {
27 
28  edm::LogInfo("EcalTPGBadXTHandler") << "EcalTPGBadXT 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("EcalTPGBadXTHandler") << m_sid<<"/"<<m_user<<"/"<<m_location<<"/"<<m_gentag;
40 
41 
42 }
43 
45 {
46 }
47 
48 
50 {
51  edm::LogInfo("EcalTPGBadXTHandler") << "Started GetNewObjects!!!";
52 
53 
54  unsigned int max_since=0;
55  max_since=static_cast<unsigned int>(tagInfo().lastInterval.first);
56  edm::LogInfo("EcalTPGBadXTHandler") << "max_since : " << max_since;
57  edm::LogInfo("EcalTPGBadXTHandler") << "retrieved last payload ";
58 
59  // here we retrieve all the runs after the last from online DB
60  edm::LogInfo("EcalTPGBadXTHandler") << "Retrieving run list from ONLINE DB ... ";
61 
62  edm::LogInfo("EcalTPGBadXTHandler") << "Making connection...";
63  econn = new EcalCondDBInterface( m_sid, m_user, m_pass );
64  edm::LogInfo("EcalTPGBadXTHandler")<< "Done.";
65 
66  if (!econn)
67  {
68  std::cout << " connection parameters " <<m_sid <<"/"<<m_user<<std::endl;
69  // cerr << e.what() << std::endl;
70  throw cms::Exception("OMDS not available");
71  }
72 
73  LocationDef my_locdef;
74  my_locdef.setLocation(m_location);
75 
76  RunTypeDef my_rundef;
77  my_rundef.setRunType(m_runtype);
78 
79  RunTag my_runtag;
80  my_runtag.setLocationDef( my_locdef );
81  my_runtag.setRunTypeDef( my_rundef );
82  my_runtag.setGeneralTag(m_gentag);
83 
84  readFromFile("last_tpg_badXT_settings.txt");
85 
86 
87  unsigned int min_run=m_i_run_number+1;
88 
89  if(m_firstRun<m_i_run_number) {
90  min_run=m_i_run_number+1;
91  } else {
92  min_run=m_firstRun;
93  }
94  if(min_run<max_since) {
95  min_run= max_since+1; // we have to add 1 to the last transferred one
96  }
97 
98  std::cout<<"m_i_run_number"<< m_i_run_number <<"m_firstRun "<<m_firstRun<< "max_since " <<max_since<< std::endl;
99 
100 
101  unsigned int max_run=m_lastRun;
102  edm::LogInfo("EcalTPGBadXTHandler") << "min_run= " << min_run << "max_run= " << max_run;
103 
104  RunList my_list;
105  my_list=econn->fetchGlobalRunListByLocation(my_runtag, min_run, max_run, my_locdef);
106  // my_list=econn->fetchRunListByLocation(my_runtag, min_run, max_run, my_locdef);
107 
108  std::vector<RunIOV> run_vec= my_list.getRuns();
109  size_t num_runs=run_vec.size();
110 
111  std::cout <<"number of runs is : "<< num_runs<< std::endl;
112 
113  std::vector<EcalLogicID> my_EcalLogicId;
114  std::vector<EcalLogicID> my_EcalLogicId_EE;
115 
116  unsigned int irun=0;
117  if(num_runs>0){
118 
119 
120  my_EcalLogicId = econn->getEcalLogicIDSetOrdered( "ECAL_crystal_number_fedccuxt",
121  610, 650,
122  1, 100,
123  0, 100,
124  "EB_crystal_number",123 );
125 
126  my_EcalLogicId_EE = econn->getEcalLogicIDSetOrdered( "ECAL_crystal_number_fedccuxt",
127  600, 700,
128  1, 100,
129  0, 100,
130  "EE_crystal_number",123 );
131 
132 
133 
134  for(size_t kr=0; kr<run_vec.size(); kr++){
135  std::cout << "here we are in run "<<kr<<std::endl;
136  irun=static_cast<unsigned int>(run_vec[kr].getRunNumber());
137 
138  std::cout<<" **************** "<<std::endl;
139  std::cout<<" **************** "<<std::endl;
140  std::cout<<" run= "<<irun<<std::endl;
141 
142  // retrieve the data :
143  std::map<EcalLogicID, RunTPGConfigDat> dataset;
144  econn->fetchDataSet(&dataset, &run_vec[kr]);
145 
146  std::string the_config_tag="";
147  int the_config_version=0;
148 
149  std::map< EcalLogicID, RunTPGConfigDat>::const_iterator it;
150 
151  int nr=0;
152  for( it=dataset.begin(); it!=dataset.end(); it++ )
153  {
154  ++nr;
155  //EcalLogicID ecalid = it->first;
156 
157  RunTPGConfigDat dat = it->second;
158  the_config_tag=dat.getConfigTag();
159  the_config_version=dat.getVersion();
160  }
161 
162  // it is all the same for all SM... get the last one
163 
164 
165  std::cout<<" run= "<<irun<<" tag "<<the_config_tag<<" version="<<the_config_version <<std::endl;
166 
167  // here we should check if it is the same as previous run.
168 
169 
170  if((the_config_tag != m_i_tag || the_config_version != m_i_version ) && nr>0 ) {
171  std::cout<<"the tag is different from last transferred run ... retrieving last config set from DB"<<std::endl;
172 
173  FEConfigMainInfo fe_main_info;
174  fe_main_info.setConfigTag(the_config_tag);
175  fe_main_info.setVersion(the_config_version);
176  std::cout << " version=" <<fe_main_info.getVersion()<< std::endl;
177 
178 
179  try{
180  std::cout << " before fetch config set" << std::endl;
181  econn-> fetchConfigSet(&fe_main_info);
182  std::cout << " after fetch config set" << std::endl;
183 
184 
185  // now get TPGBadXT
186  int badxtId=fe_main_info.getBxtId();
187 
188  if( badxtId != m_i_badXT && badxtId!=0) {
189 
190  FEConfigBadXTInfo fe_badXt_info;
191  fe_badXt_info.setId(badxtId);
192  econn-> fetchConfigSet(&fe_badXt_info);
193  std::vector<FEConfigBadXTDat> dataset_TpgBadXT;
194  econn->fetchConfigDataSet(&dataset_TpgBadXT, &fe_badXt_info);
195 
196  // NB new
197 
198  EcalTPGCrystalStatus* badXt;
199  badXt = produceEcalTrgChannelStatus();
200 
201 
202  typedef std::vector<FEConfigBadXTDat>::const_iterator CIfeped;
203  EcalLogicID ecid_xt;
204  FEConfigBadXTDat rd_badXt;
205  int icells=0;
206 
207  for (CIfeped p = dataset_TpgBadXT.begin(); p != dataset_TpgBadXT.end(); p++) {
208  rd_badXt = *p;
209 
210  int fed_id=rd_badXt.getFedId();
211  //int tcc_id=rd_badXt.getTCCId();
212  int tt_id=rd_badXt.getTTId();
213  int xt_id=rd_badXt.getXTId();
214 
215  // EB data
216  if (fed_id>=610 && fed_id<=645) {
217 
218  // logic id is 1011ssxxxx
219  // get SM id
220  int sm_num=0;
221  if(fed_id<=627 ) sm_num=fed_id-609+18;
222  if(fed_id>627 ) sm_num=fed_id-627;
223 
224  // get crystal id
225  int xt_num=0;
226 
227  for(size_t ixt=0; ixt<my_EcalLogicId.size(); ixt++){
228 
229  if(my_EcalLogicId[ixt].getID1()==fed_id && my_EcalLogicId[ixt].getID2()==tt_id
230  && my_EcalLogicId[ixt].getID3()==xt_id ) {
231  //1011060504
232  int ecid= my_EcalLogicId[ixt].getLogicID();
233  xt_num=(ecid)-(101100+sm_num)*10000;
234 
235  }
236  }
237 
238  std::cout<< " masking crystal "<<sm_num<<"/"<<xt_num<<" from fed/tt/xt"<<
239  fed_id<<"/"<<tt_id<<"/"<<xt_id<< std::endl;
240  EBDetId ebdetid(sm_num,xt_num,EBDetId::SMCRYSTALMODE);
241 
242  badXt->setValue(ebdetid.rawId(),rd_badXt.getStatus());
243  ++icells;
244  } else {
245  // EE data
246 
247  long x=0;
248  long y=0;
249  long z=0;
250 
251  for(size_t ixt=0; ixt<my_EcalLogicId_EE.size(); ixt++){
252 
253  if(my_EcalLogicId_EE[ixt].getID1()==fed_id && my_EcalLogicId_EE[ixt].getID2()==tt_id
254  && my_EcalLogicId_EE[ixt].getID3()==xt_id ) {
255 
256  long ecid=(long) my_EcalLogicId_EE[ixt].getLogicID();
257  // logic_id 201Zxxxyyy Z=0 / 2 -> z= -1 / 1 , x -> 1 100, y -> 1 100
258  y=ecid-( (long)(ecid/1000) ) *1000;
259  x= ( ecid- y) /1000 ;
260  x= x -( (long)(x/1000) ) *1000;
261  z= (ecid-y-x*1000 )/1000000 -2010;
262  if(z==0) z=-1;
263  if(z==2) z= 1;
264  }
265  }
266 
267  EEDetId eedetid(x,y,z);
268  badXt->setValue(eedetid.rawId(),rd_badXt.getStatus());
269  ++icells;
270  }
271  }//end for over data
272 
273  edm::LogInfo("EcalTPGBadXTHandler") << "Finished badXT reading";
274 
275  Time_t snc= (Time_t) irun ;
276  m_to_transfer.push_back(std::make_pair((EcalTPGCrystalStatus*)badXt,snc));
277 
278  m_i_run_number=irun;
279  m_i_tag=the_config_tag;
280  m_i_version=the_config_version;
281  m_i_badXT=badxtId;
282 
283  writeFile("last_tpg_badXT_settings.txt");
284 
285  } else {
286 
287  m_i_run_number=irun;
288  m_i_tag=the_config_tag;
289  m_i_version=the_config_version;
290 
291  writeFile("last_tpg_badXT_settings.txt");
292 
293  std::cout<< " even if the tag/version is not the same, the badXT id is the same -> no transfer needed "<< std::endl;
294 
295  }
296 
297  }
298 
299  catch (std::exception &e) {
300  std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" <<the_config_tag
301  <<" version="<<the_config_version<< std::endl;
302  std::cout << e.what() << std::endl;
303  m_i_run_number=irun;
304 
305  }
306  std::cout<<" **************** "<<std::endl;
307 
308  } else if(nr==0) {
309  m_i_run_number=irun;
310  std::cout<< " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed "<< std::endl;
311  std::cout<<" **************** "<<std::endl;
312  } else {
313  m_i_run_number=irun;
314  m_i_tag=the_config_tag;
315  m_i_version=the_config_version;
316  std::cout<< " the tag/version is the same -> no transfer needed "<< std::endl;
317  std::cout<<" **************** "<<std::endl;
318  writeFile("last_tpg_badXT_settings.txt");
319  }
320 
321 
322  }//end for over kr (nr of runs)
323  }//end if
324 
325  delete econn;
326 
327  edm::LogInfo("EcalTPGBadXTHandler")<< "Ecal - > end of getNewObjects -----------";
328 }
329 
331  //-------------------------------------------------------------
332 
333  m_i_tag="";
334  m_i_version=0;
335  m_i_run_number=0;
336  m_i_badXT=0;
337 
338  FILE *inpFile; // input file
339  inpFile = fopen(inputFile,"r");
340  if(!inpFile) {
341  edm::LogError("EcalTPGBadXTHandler")<<"*** Can not open file: "<<inputFile;
342  }
343 
344  char line[256];
345 
346  std::ostringstream str;
347 
348  fgets(line,255,inpFile);
349  m_i_tag=to_string(line);
350  str << "gen tag " << m_i_tag << std::endl ; // should I use this?
351 
352  fgets(line,255,inpFile);
353  m_i_version=atoi(line);
354  str << "version= " << m_i_version << std::endl ;
355 
356  fgets(line,255,inpFile);
357  m_i_run_number=atoi(line);
358  str << "run_number= " << m_i_run_number << std::endl ;
359 
360  fgets(line,255,inpFile);
361  m_i_badXT=atoi(line);
362  str << "badXT_config= " << m_i_badXT << std::endl ;
363 
364 
365  fclose(inpFile); // close inp. file
366 
367 }
368 
370  //-------------------------------------------------------------
371 
372 
373  std::ofstream myfile;
374  myfile.open (inputFile);
375  myfile << m_i_tag <<std::endl;
376  myfile << m_i_version <<std::endl;
377  myfile << m_i_run_number <<std::endl;
378  myfile << m_i_badXT <<std::endl;
379 
380  myfile.close();
381 
382 }
383 
385 {
386 
388  // barrel
389  for(int ieta=-EBDetId::MAX_IETA; ieta<=EBDetId::MAX_IETA; ++ieta) {
390  if(ieta==0) continue;
391  for(int iphi=EBDetId::MIN_IPHI; iphi<=EBDetId::MAX_IPHI; ++iphi) {
392  if (EBDetId::validDetId(ieta,iphi)) {
393  EBDetId ebid(ieta,iphi);
394  ical->setValue( ebid, 0 );
395  }
396  }
397  }
398  // endcap
399  for(int iX=EEDetId::IX_MIN; iX<=EEDetId::IX_MAX ;++iX) {
400  for(int iY=EEDetId::IY_MIN; iY<=EEDetId::IY_MAX; ++iY) {
401  // make an EEDetId since we need EEDetId::rawId() to be used as the key for the pedestals
402  if (EEDetId::validDetId(iX,iY,1)) {
403  EEDetId eedetidpos(iX,iY,1);
404  ical->setValue( eedetidpos, 0 );
405  }
406  if (EEDetId::validDetId(iX,iY,-1)) {
407  EEDetId eedetidneg(iX,iY,-1);
408  ical->setValue( eedetidneg, 0 );
409  }
410  }
411  }
412  return ical;
413 }
414 
static bool validDetId(int i, int j)
check if a valid index combination
Definition: EBDetId.cc:59
T getParameter(std::string const &) const
std::vector< RunIOV > getRuns()
Definition: RunList.cc:35
static const int MIN_IPHI
Definition: EBDetId.h:121
void writeFile(const char *inputFile)
void setLocationDef(const LocationDef locDef)
Definition: RunTag.cc:53
std::string getConfigTag() const
int getVersion() const
Definition: RunTag.h:13
void setVersion(int id)
static const int IX_MIN
Definition: EEDetId.h:271
static bool validDetId(int crystal_ix, int crystal_iy, int iz)
Definition: EEDetId.cc:562
static const int IY_MIN
Definition: EEDetId.h:275
int getXTId() const
uint32_t rawId() const
get the raw id
Definition: DetId.h:45
void setValue(const uint32_t id, const Item &item)
std::string to_string(const T &t)
Definition: Logger.cc:25
unsigned long long Time_t
Definition: Time.h:16
Definition: DDAxes.h:10
void setRunTypeDef(const RunTypeDef runTypeDef)
Definition: RunTag.cc:70
EcalTPGCrystalStatusMap EcalTPGCrystalStatus
int getStatus() const
EcalTPGCrystalStatus * produceEcalTrgChannelStatus()
static const int IX_MAX
Definition: EEDetId.h:279
EcalTPGBadXTHandler(edm::ParameterSet const &)
int getFedId() const
static const int MAX_IPHI
Definition: EBDetId.h:123
void setRunType(std::string runtype)
Definition: RunTypeDef.cc:33
int getTTId() const
static const int MAX_IETA
Definition: EBDetId.h:122
int getVersion() const
void setConfigTag(std::string x)
Definition: IODConfig.h:31
int getBxtId() const
void setLocation(std::string loc)
Definition: LocationDef.cc:33
tuple cout
Definition: gather_cfg.py:41
static const int IY_MAX
Definition: EEDetId.h:283
void setGeneralTag(std::string tag)
Definition: RunTag.cc:36
static const int SMCRYSTALMODE
Definition: EBDetId.h:146
void readFromFile(const char *inputFile)