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  if( sm_num==0 && xt_num==0 ) {
241  std::cout<< " ERROR FOR crystal from fed/tt/xt"<<
242  fed_id<<"/"<<tt_id<<"/"<<xt_id<< std::endl;
243  }
244  EBDetId ebdetid(sm_num,xt_num,EBDetId::SMCRYSTALMODE);
245 
246  badXt->setValue(ebdetid.rawId(),rd_badXt.getStatus());
247  ++icells;
248  } else {
249  // EE data
250 
251  long x=0;
252  long y=0;
253  long z=0;
254 
255  for(size_t ixt=0; ixt<my_EcalLogicId_EE.size(); ixt++){
256 
257  if(my_EcalLogicId_EE[ixt].getID1()==fed_id && my_EcalLogicId_EE[ixt].getID2()==tt_id
258  && my_EcalLogicId_EE[ixt].getID3()==xt_id ) {
259 
260  long ecid=(long) my_EcalLogicId_EE[ixt].getLogicID();
261  // logic_id 201Zxxxyyy Z=0 / 2 -> z= -1 / 1 , x -> 1 100, y -> 1 100
262  y=ecid-( (long)(ecid/1000) ) *1000;
263  x= ( ecid- y) /1000 ;
264  x= x -( (long)(x/1000) ) *1000;
265  z= (ecid-y-x*1000 )/1000000 -2010;
266  if(z==0) z=-1;
267  if(z==2) z= 1;
268  }
269  }
270 
271  if( x==0 && y==0 && z==0 ) {
272  std::cout<< " ERROR FOR crystal from fed/tt/xt"<<
273  fed_id<<"/"<<tt_id<<"/"<<xt_id<< std::endl;
274  }
275  EEDetId eedetid(x,y,z);
276  badXt->setValue(eedetid.rawId(),rd_badXt.getStatus());
277  ++icells;
278  }
279  }//end for over data
280 
281  edm::LogInfo("EcalTPGBadXTHandler") << "Finished badXT reading";
282 
283  Time_t snc= (Time_t) irun ;
284  m_to_transfer.push_back(std::make_pair((EcalTPGCrystalStatus*)badXt,snc));
285 
286  m_i_run_number=irun;
287  m_i_tag=the_config_tag;
288  m_i_version=the_config_version;
289  m_i_badXT=badxtId;
290 
291  writeFile("last_tpg_badXT_settings.txt");
292 
293  } else {
294 
295  m_i_run_number=irun;
296  m_i_tag=the_config_tag;
297  m_i_version=the_config_version;
298 
299  writeFile("last_tpg_badXT_settings.txt");
300 
301  std::cout<< " even if the tag/version is not the same, the badXT id is the same -> no transfer needed "<< std::endl;
302 
303  }
304 
305  }
306 
307  catch (std::exception &e) {
308  std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" <<the_config_tag
309  <<" version="<<the_config_version<< std::endl;
310  std::cout << e.what() << std::endl;
311  m_i_run_number=irun;
312 
313  }
314  std::cout<<" **************** "<<std::endl;
315 
316  } else if(nr==0) {
317  m_i_run_number=irun;
318  std::cout<< " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed "<< std::endl;
319  std::cout<<" **************** "<<std::endl;
320  } else {
321  m_i_run_number=irun;
322  m_i_tag=the_config_tag;
323  m_i_version=the_config_version;
324  std::cout<< " the tag/version is the same -> no transfer needed "<< std::endl;
325  std::cout<<" **************** "<<std::endl;
326  writeFile("last_tpg_badXT_settings.txt");
327  }
328 
329 
330  }//end for over kr (nr of runs)
331  }//end if
332 
333  delete econn;
334 
335  edm::LogInfo("EcalTPGBadXTHandler")<< "Ecal - > end of getNewObjects -----------";
336 }
337 
339  //-------------------------------------------------------------
340 
341  m_i_tag="";
342  m_i_version=0;
343  m_i_run_number=0;
344  m_i_badXT=0;
345 
346  FILE *inpFile; // input file
347  inpFile = fopen(inputFile,"r");
348  if(!inpFile) {
349  edm::LogError("EcalTPGBadXTHandler")<<"*** Can not open file: "<<inputFile;
350  }
351 
352  char line[256];
353 
354  std::ostringstream str;
355 
356  fgets(line,255,inpFile);
357  m_i_tag=to_string(line);
358  str << "gen tag " << m_i_tag << std::endl ; // should I use this?
359 
360  fgets(line,255,inpFile);
361  m_i_version=atoi(line);
362  str << "version= " << m_i_version << std::endl ;
363 
364  fgets(line,255,inpFile);
365  m_i_run_number=atoi(line);
366  str << "run_number= " << m_i_run_number << std::endl ;
367 
368  fgets(line,255,inpFile);
369  m_i_badXT=atoi(line);
370  str << "badXT_config= " << m_i_badXT << std::endl ;
371 
372 
373  fclose(inpFile); // close inp. file
374 
375 }
376 
378  //-------------------------------------------------------------
379 
380 
381  std::ofstream myfile;
382  myfile.open (inputFile);
383  myfile << m_i_tag <<std::endl;
384  myfile << m_i_version <<std::endl;
385  myfile << m_i_run_number <<std::endl;
386  myfile << m_i_badXT <<std::endl;
387 
388  myfile.close();
389 
390 }
391 
393 {
394 
396  // barrel
397  for(int ieta=-EBDetId::MAX_IETA; ieta<=EBDetId::MAX_IETA; ++ieta) {
398  if(ieta==0) continue;
399  for(int iphi=EBDetId::MIN_IPHI; iphi<=EBDetId::MAX_IPHI; ++iphi) {
400  if (EBDetId::validDetId(ieta,iphi)) {
401  EBDetId ebid(ieta,iphi);
402  ical->setValue( ebid, 0 );
403  }
404  }
405  }
406  // endcap
407  for(int iX=EEDetId::IX_MIN; iX<=EEDetId::IX_MAX ;++iX) {
408  for(int iY=EEDetId::IY_MIN; iY<=EEDetId::IY_MAX; ++iY) {
409  // make an EEDetId since we need EEDetId::rawId() to be used as the key for the pedestals
410  if (EEDetId::validDetId(iX,iY,1)) {
411  EEDetId eedetidpos(iX,iY,1);
412  ical->setValue( eedetidpos, 0 );
413  }
414  if (EEDetId::validDetId(iX,iY,-1)) {
415  EEDetId eedetidneg(iX,iY,-1);
416  ical->setValue( eedetidneg, 0 );
417  }
418  }
419  }
420  return ical;
421 }
422 
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
double double double z
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:26
unsigned long long Time_t
Definition: Time.h:16
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
tuple dataset
Definition: dataset.py:393
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:121
static const int IY_MAX
Definition: EEDetId.h:283
Definition: DDAxes.h:10
void setGeneralTag(std::string tag)
Definition: RunTag.cc:36
static const int SMCRYSTALMODE
Definition: EBDetId.h:146
void readFromFile(const char *inputFile)