CMS 3D CMS Logo

EcalTPGWeightGroupHandler.cc
Go to the documentation of this file.
2 
11 
16 
17 #include<iostream>
18 #include<fstream>
19 
20 
21 #include <ctime>
22 #include <unistd.h>
23 
24 #include <string>
25 #include <cstdio>
26 #include <typeinfo>
27 #include <sstream>
28 
29 const Int_t kEBStrips = 12240, kEEStrips = 2936;
30 
32  : m_name(ps.getUntrackedParameter<std::string>("name","EcalTPGWeightGroupHandler")) {
33 
34  edm::LogInfo("EcalTPGWeightGroupHandler") << "EcalTPGWeightGroup Source handler constructor.";
35  m_firstRun=static_cast<unsigned int>(atoi( ps.getParameter<std::string>("firstRun").c_str()));
36  m_lastRun=static_cast<unsigned int>(atoi( ps.getParameter<std::string>("lastRun").c_str()));
37  m_sid= ps.getParameter<std::string>("OnlineDBSID");
38  m_user= ps.getParameter<std::string>("OnlineDBUser");
39  m_pass= ps.getParameter<std::string>("OnlineDBPassword");
40  m_locationsource= ps.getParameter<std::string>("LocationSource");
41  m_location=ps.getParameter<std::string>("Location");
42  m_gentag=ps.getParameter<std::string>("GenTag");
43  m_runtype=ps.getParameter<std::string>("RunType");
44  m_file_type = ps.getParameter<std::string>("fileType"); // xml/txt
45  m_file_name = ps.getParameter<std::string>("fileName");
46 
47  edm::LogInfo("EcalTPGWeightGroupHandler") << m_sid<<"/"<<m_user<<"/"<<m_location<<"/"<<m_gentag;
48 }
49 
51 }
52 
54  if(m_file_type == "txt") {
55  readtxtFile();
56  }
57  else if(m_file_type == "xml") {
58  readxmlFile();
59  }
60  else {
61  edm::LogInfo("EcalTPGWeightGroupHandler") << "Started GetNewObjects!!!";
62 
63  //check whats already inside of database
64  if (tagInfo().size){
65  //check whats already inside of database
66  edm::LogInfo(" got offlineInfo = ");
67  edm::LogInfo(" tag name = ") << tagInfo().name;
68  edm::LogInfo(" size = ") << tagInfo().size;
69  } else {
70  edm::LogInfo(" First object for this tag ");
71  }
72 
73  unsigned int max_since=0;
74  max_since=static_cast<unsigned int>(tagInfo().lastInterval.first);
75  edm::LogInfo("EcalTPGWeightGroupHandler") << "max_since : " << max_since;
76 
77  edm::LogInfo("EcalTPGWeightGroupHandler") << "retrieved last payload ";
78 
79  // here we retrieve all the runs after the last from online DB
80 
81  edm::LogInfo("EcalTPGWeightGroupHandler") << "Retrieving run list from ONLINE DB ... ";
82 
83  edm::LogInfo("EcalTPGWeightGroupHandler") << "Making connection...";
85  edm::LogInfo("EcalTPGWeightGroupHandler") << "Done.";
86 
87  if (!econn)
88  {
89  edm::LogInfo(" connection parameters ") <<m_sid <<"/"<<m_user;
90  // cerr << e.what();
91  throw cms::Exception("OMDS not available");
92  }
93 
94 
95  LocationDef my_locdef;
96  my_locdef.setLocation(m_location);
97 
98  RunTypeDef my_rundef;
99  my_rundef.setRunType(m_runtype);
100 
101  RunTag my_runtag;
102  my_runtag.setLocationDef( my_locdef );
103  my_runtag.setRunTypeDef( my_rundef );
104  my_runtag.setGeneralTag(m_gentag);
105 
106  readFromFile("last_tpg_weightGroup_settings.txt");
107 
108  unsigned int min_run=m_i_run_number+1;
109 
111  min_run=m_i_run_number+1;
112  } else {
113  min_run=m_firstRun;
114  }
115 
116  if(min_run<max_since) {
117  min_run=max_since+1; // we have to add 1 to the last transferred one
118  }
119 
120  edm::LogInfo("m_i_run_number")<< m_i_run_number <<"m_firstRun "<<m_firstRun<< "max_since " <<max_since;
121 
122  unsigned int max_run=m_lastRun;
123  edm::LogInfo("EcalTPGWeightGroupHandler") << "min_run= " << min_run << " max_run= " << max_run;
124 
125  RunList my_list;
126  my_list=econn->fetchGlobalRunListByLocation(my_runtag, min_run, max_run, my_locdef);
127  // my_list=econn->fetchRunListByLocation(my_runtag,min_run,max_run,my_locdef);
128 
129  std::vector<RunIOV> run_vec= my_list.getRuns();
130  size_t num_runs=run_vec.size();
131 
132  edm::LogInfo("number of runs is : ")<< num_runs;
133 
134  unsigned int irun=0;
135  if(num_runs>0){
136 
137  // going to query the ecal logic id
138  std::vector<EcalLogicID> my_StripEcalLogicId_EE;
139  my_StripEcalLogicId_EE = econn->getEcalLogicIDSetOrdered( "ECAL_readout_strip",
140  1, 2000,
141  1, 70,
142  0,5,
143  "EE_offline_stripid",123 );
144 
145  edm::LogInfo(" GOT the logic ID for the EE trigger strips ");
146 
147  for(size_t kr=0; kr<run_vec.size(); kr++){
148 
149  irun=static_cast<unsigned int>(run_vec[kr].getRunNumber());
150 
151  edm::LogInfo(" **************** ");
152  edm::LogInfo(" **************** ");
153  edm::LogInfo(" run= ")<<irun;
154 
155  // retrieve the data :
156  std::map<EcalLogicID, RunTPGConfigDat> dataset;
157  econn->fetchDataSet(&dataset, &run_vec[kr]);
158 
159  std::string the_config_tag="";
160  int the_config_version=0;
161 
162  std::map< EcalLogicID, RunTPGConfigDat>::const_iterator it;
163 
164  int nr=0;
165  for( it=dataset.begin(); it!=dataset.end(); it++ )
166  {
167  ++nr;
168  //EcalLogicID ecalid = it->first;
169 
170  RunTPGConfigDat dat = it->second;
171  the_config_tag=dat.getConfigTag();
172  the_config_version=dat.getVersion();
173  }
174 
175  // it is all the same for all SM... get the last one
176 
177 
178  edm::LogInfo(" run= ")<<irun<<" tag "<<the_config_tag<<" version="<<the_config_version;
179 
180  // here we should check if it is the same as previous run.
181 
182 
183  if((the_config_tag != m_i_tag || the_config_version != m_i_version ) && nr>0 ) {
184  edm::LogInfo("the tag is different from last transferred run ... retrieving last config set from DB");
185 
186  FEConfigMainInfo fe_main_info;
187  fe_main_info.setConfigTag(the_config_tag);
188  fe_main_info.setVersion(the_config_version);
189 
190  try{
191  edm::LogInfo(" before fetch config set");
192  econn-> fetchConfigSet(&fe_main_info);
193  edm::LogInfo(" after fetch config set");
194 
195 
196  // now get TPGWeightGroup
197  int wId=fe_main_info.getWeiId();
198 
199  if( wId != m_i_weightGroup ) {
200 
201  FEConfigWeightInfo fe_w_info;
202  fe_w_info.setId(wId);
203  econn-> fetchConfigSet(&fe_w_info);
204  std::map<EcalLogicID, FEConfigWeightDat> dataset_TpgW;
205  econn->fetchDataSet(&dataset_TpgW, &fe_w_info);
206 
207 
208  EcalTPGWeightGroup* weightG = new EcalTPGWeightGroup;
209  typedef std::map<EcalLogicID, FEConfigWeightDat>::const_iterator CIfesli;
210  EcalLogicID ecid_xt;
211  int weightGroup;
212  int icells=0;
213 
214  std::map<std::string,int> map;
216 
217  for (CIfesli p = dataset_TpgW.begin(); p != dataset_TpgW.end(); p++) {
218  ecid_xt = p->first;
219  weightGroup = p->second.getWeightGroupId();
220 
221  std::string ecid_name=ecid_xt.getName();
222 
223  // EB data
224  if (ecid_name=="EB_VFE") {
225  int sm=ecid_xt.getID1();
226  int tt=ecid_xt.getID2();
227  int strip=ecid_xt.getID3();
228  int tcc= sm+54;
229  if(sm>18) tcc=sm+18 ;
230 
231  // simple formula to calculate the Srip EB identifier
232 
233  unsigned int stripEBId = 303176+(tt-1)*64+(strip-1)*8+(tcc-37)*8192;
234 
235  weightG->setValue(stripEBId,weightGroup);
236  ++icells;
237  }
238  else if (ecid_name=="ECAL_readout_strip"){
239  // EE data to add
240  int id1=ecid_xt.getID1();
241  int id2=ecid_xt.getID2();
242  int id3=ecid_xt.getID3();
243 
244  bool set_the_strip=false;
245  int stripEEId;
246  for (size_t istrip=0; istrip<my_StripEcalLogicId_EE.size(); istrip++) {
247 
248  if(!set_the_strip){
249 
250  if(my_StripEcalLogicId_EE[istrip].getID1()==id1
251  && my_StripEcalLogicId_EE[istrip].getID2()==id2
252  && my_StripEcalLogicId_EE[istrip].getID3()==id3
253  ){
254  stripEEId =my_StripEcalLogicId_EE[istrip].getLogicID();
255  set_the_strip=true;
256  break;
257  }
258  }
259 
260  }
261 
262  if(set_the_strip){
263 
264  weightG->setValue(stripEEId,weightGroup);
265 
266  } else {
267  edm::LogInfo(" these may be the additional towers TCC/TT ")
268  << id1<<"/"<<id2;
269  }
270 
271  ++icells;
272  }
273  }
274 
275  Time_t snc= (Time_t) irun ;
276 
277  m_to_transfer.push_back(std::make_pair((EcalTPGWeightGroup*)weightG,snc));
278 
279  m_i_run_number=irun;
280  m_i_tag=the_config_tag;
281  m_i_version=the_config_version;
282  m_i_weightGroup=wId;
283 
284  writeFile("last_tpg_weightGroup_settings.txt");
285 
286  } else {
287 
288  m_i_run_number=irun;
289  m_i_tag=the_config_tag;
290  m_i_version=the_config_version;
291 
292  writeFile("last_tpg_weightGroup_settings.txt");
293 
294  edm::LogInfo(" even if the tag/version is not the same, the weight group id is the same -> no transfer needed ");
295 
296  }
297 
298  }
299 
300  catch (std::exception &e) {
301  edm::LogInfo("ERROR: THIS CONFIG DOES NOT EXIST: tag=") <<the_config_tag
302  <<" version="<<the_config_version;
303  edm::LogInfo("Exception")<< e.what();
304  m_i_run_number=irun;
305 
306  }
307  edm::LogInfo(" **************** ");
308 
309  } else if(nr==0) {
310  m_i_run_number=irun;
311  edm::LogInfo(" no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed ");
312  edm::LogInfo(" **************** ");
313  } else {
314  m_i_run_number=irun;
315  m_i_tag=the_config_tag;
316  m_i_version=the_config_version;
317  edm::LogInfo(" the tag/version is the same -> no transfer needed ");
318  edm::LogInfo(" **************** ");
319  writeFile("last_tpg_weightGroup_settings.txt");
320  }
321 
322  }
323  }
324 
325  delete econn;
326  } // usual way
327  edm::LogInfo("EcalTPGWeightGroupHandler") << "Ecal - > end of getNewObjects -----------";
328 }
329 
331  edm::LogInfo(" reading the input file ") << m_file_name;
332  std::ifstream fInput;
333  fInput.open(m_file_name);
334  if(!fInput.is_open()) {
335  edm::LogInfo("ERROR : cannot open file ") << m_file_name;
336  exit (1);
337  }
338  int weightGroup, stripEBId, stripEEId;
339  EcalTPGWeightGroup* weightG = new EcalTPGWeightGroup;
340  for (int strip = 0; strip < kEBStrips; strip++) {
341  fInput >> stripEBId >> weightGroup;
342  weightG->setValue(stripEBId,weightGroup);
343  }
344  for (int strip = 0; strip < kEEStrips; strip++) {
345  fInput >> stripEEId >> weightGroup;
346  weightG->setValue(stripEEId,weightGroup);
347  }
348  try {
349  Time_t snc = (Time_t) m_firstRun;
350  m_to_transfer.push_back(std::make_pair((EcalTPGWeightGroup*)weightG, snc));
351  } catch (std::exception &e) {
352  edm::LogInfo("EcalTPGWeightGroupHandler::readtxtFile error : ") << e.what();
353  }
354  edm::LogInfo(" **************** ");
355 }
356 
358  edm::LogInfo(" reading the input file ") << m_file_name;
359  std::ifstream fxml;
360  fxml.open(m_file_name);
361  if(!fxml.is_open()) {
362  edm::LogInfo("ERROR : cannot open file ") << m_file_name;
363  exit (1);
364  }
365  std::string dummyLine, bid;
366  int weightGroup, stripEBId, stripEEId;
367  EcalTPGWeightGroup* weightG = new EcalTPGWeightGroup;
368  for(int i = 0; i < 6; i++) std::getline(fxml, dummyLine); // skip first lines
369  fxml >> bid;
370  std::size_t found = bid.find("</");
371  std::string stt = bid.substr(7, found - 7);
372  for(int i = 0; i < 2; i++) std::getline(fxml, dummyLine); // <item_version>0</item_version>
373  for (int strip = 0; strip < kEBStrips; strip++) {
374  std::getline(fxml, dummyLine); // <item
375  fxml >> bid; // <first
376  found = bid.find("</");
377  stt = bid.substr(7, found - 7);
378  std::istringstream sg1(stt);
379  sg1 >> stripEBId;
380  std::getline(fxml, dummyLine);
381  fxml >> bid; // <second
382  found = bid.find("</");
383  stt = bid.substr(8, found - 8);
384  std::istringstream sg2(stt);
385  sg2 >> weightGroup;
386  weightG->setValue(stripEBId,weightGroup);
387  for(int i = 0; i < 2; i++) std::getline(fxml, dummyLine); // </item>
388  }
389  for (int strip = 0; strip < kEEStrips; strip++) {
390  std::getline(fxml, dummyLine); // <item
391  fxml >> bid; // <first
392  found = bid.find("</");
393  stt = bid.substr(7, found - 7);
394  std::istringstream sg1(stt);
395  sg1 >> stripEEId;
396  std::getline(fxml, dummyLine);
397  fxml >> bid; // <second
398  found = bid.find("</");
399  stt = bid.substr(8, found - 8);
400  std::istringstream sg2(stt);
401  sg2 >> weightGroup;
402  weightG->setValue(stripEEId,weightGroup);
403  for(int i = 0; i < 2; i++) std::getline(fxml, dummyLine); // </item>
404  }
405  try {
406  Time_t snc = (Time_t) m_firstRun;
407  m_to_transfer.push_back(std::make_pair((EcalTPGWeightGroup*)weightG, snc));
408  } catch (std::exception &e) {
409  edm::LogInfo("EcalTPGWeightGroupHandler::readtxtFile error : ") << e.what();
410  }
411  edm::LogInfo(" **************** ");
412 }
413 
415  //-------------------------------------------------------------
416 
417  m_i_tag="";
418  m_i_version=0;
419  m_i_run_number=0;
420  m_i_weightGroup=0;
421 
422  FILE *inpFile; // input file
423  inpFile = fopen(inputFile,"r");
424  if(!inpFile) {
425  edm::LogError("EcalTPGWeightGroupHandler")<<"*** Can not open file: "<<inputFile;
426  return;
427  }
428 
429  char line[256];
430 
431  std::ostringstream str;
432 
433  fgets(line,255,inpFile);
434  m_i_tag=to_string(line);
435  str << "gen tag " << m_i_tag << std::endl ; // should I use this?
436 
437  fgets(line,255,inpFile);
438  m_i_version=atoi(line);
439  str << "version= " << m_i_version << std::endl ;
440 
441  fgets(line,255,inpFile);
442  m_i_run_number=atoi(line);
443  str << "run_number= " << m_i_run_number << std::endl ;
444 
445  fgets(line,255,inpFile);
446  m_i_weightGroup=atoi(line);
447  str << "weightGroup_config= " << m_i_weightGroup << std::endl ;
448 
449 
450  fclose(inpFile); // close inp. file
451 
452 }
453 
455  //-------------------------------------------------------------
456 
457 
458  std::ofstream myfile;
459  myfile.open (inputFile);
460  myfile << m_i_tag <<std::endl;
461  myfile << m_i_version <<std::endl;
462  myfile << m_i_run_number <<std::endl;
463  myfile << m_i_weightGroup <<std::endl;
464 
465  myfile.close();
466 
467 }
size
Write out results.
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
const Int_t kEBStrips
int getVersion() const
Definition: RunTag.h:13
size_t size
Definition: Types.h:78
void setVersion(int id)
void fetchDataSet(std::map< EcalLogicID, DATT > *fillMap, IOVT *iov) noexcept(false)
std::string name
Definition: Types.h:74
int getID2() const
Definition: EcalLogicID.cc:51
const Int_t kEEStrips
cond::ValidityInterval lastInterval
Definition: Types.h:76
std::string getName() const
Definition: EcalLogicID.cc:36
EcalTPGWeightGroupHandler(edm::ParameterSet const &)
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)
int getWeiId() const
void setValue(const uint32_t &rawId, const uint32_t &ObjectId)
Definition: EcalTPGGroups.cc:9
int getID1() const
Definition: EcalLogicID.cc:46
void setLocationDef(const LocationDef &locDef)
Definition: RunTag.cc:53
void setRunType(std::string runtype)
Definition: RunTypeDef.cc:33
void setConfigTag(std::string x)
Definition: IODConfig.h:31
void setLocation(std::string loc)
Definition: LocationDef.cc:33
int getID3() const
Definition: EcalLogicID.cc:56
void setGeneralTag(std::string tag)
Definition: RunTag.cc:36
#define str(s)