CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
EcalTPGWeightGroupHandler.cc
Go to the documentation of this file.
2 
11 
16 
17 #include<iostream>
18 #include<fstream>
19 
20 
21 #include <time.h>
22 #include <unistd.h>
23 
24 #include <string>
25 #include <cstdio>
26 #include <typeinfo>
27 #include <sstream>
28 
29 
31  : m_name(ps.getUntrackedParameter<std::string>("name","EcalTPGWeightGroupHandler")) {
32 
33  edm::LogInfo("EcalTPGWeightGroupHandler") << "EcalTPGWeightGroup Source handler constructor.";
34  m_firstRun=static_cast<unsigned int>(atoi( ps.getParameter<std::string>("firstRun").c_str()));
35  m_lastRun=static_cast<unsigned int>(atoi( ps.getParameter<std::string>("lastRun").c_str()));
36  m_sid= ps.getParameter<std::string>("OnlineDBSID");
37  m_user= ps.getParameter<std::string>("OnlineDBUser");
38  m_pass= ps.getParameter<std::string>("OnlineDBPassword");
39  m_locationsource= ps.getParameter<std::string>("LocationSource");
40  m_location=ps.getParameter<std::string>("Location");
41  m_gentag=ps.getParameter<std::string>("GenTag");
42  m_runtype=ps.getParameter<std::string>("RunType");
43 
44  edm::LogInfo("EcalTPGWeightGroupHandler") << m_sid<<"/"<<m_user<<"/"<<m_location<<"/"<<m_gentag;
45 }
46 
48 {
49 }
50 
52 {
53 
54  edm::LogInfo("EcalTPGWeightGroupHandler") << "Started GetNewObjects!!!";
55 
56  //check whats already inside of database
57  if (tagInfo().size){
58  //check whats already inside of database
59  std::cout << "got offlineInfo = " << std::endl;
60  std::cout << "tag name = " << tagInfo().name << std::endl;
61  std::cout << "size = " << tagInfo().size << std::endl;
62  } else {
63  std::cout << " First object for this tag " << std::endl;
64  }
65 
66  unsigned int max_since=0;
67  max_since=static_cast<unsigned int>(tagInfo().lastInterval.first);
68  edm::LogInfo("EcalTPGWeightGroupHandler") << "max_since : " << max_since;
69 
70  edm::LogInfo("EcalTPGWeightGroupHandler") << "retrieved last payload ";
71 
72  // here we retrieve all the runs after the last from online DB
73 
74  edm::LogInfo("EcalTPGWeightGroupHandler") << "Retrieving run list from ONLINE DB ... ";
75 
76  edm::LogInfo("EcalTPGWeightGroupHandler") << "Making connection...";
77  econn = new EcalCondDBInterface( m_sid, m_user, m_pass );
78  edm::LogInfo("EcalTPGWeightGroupHandler") << "Done.";
79 
80  if (!econn)
81  {
82  std::cout << " connection parameters " <<m_sid <<"/"<<m_user<<std::endl;
83  // cerr << e.what() << std::endl;
84  throw cms::Exception("OMDS not available");
85  }
86 
87 
88  LocationDef my_locdef;
89  my_locdef.setLocation(m_location);
90 
91  RunTypeDef my_rundef;
92  my_rundef.setRunType(m_runtype);
93 
94  RunTag my_runtag;
95  my_runtag.setLocationDef( my_locdef );
96  my_runtag.setRunTypeDef( my_rundef );
97  my_runtag.setGeneralTag(m_gentag);
98 
99  readFromFile("last_tpg_weightGroup_settings.txt");
100 
101  unsigned int min_run=m_i_run_number+1;
102 
103  if(m_firstRun<m_i_run_number) {
104  min_run=m_i_run_number+1;
105  } else {
106  min_run=m_firstRun;
107  }
108 
109  if(min_run<max_since) {
110  min_run=max_since+1; // we have to add 1 to the last transferred one
111  }
112 
113  std::cout<<"m_i_run_number"<< m_i_run_number <<"m_firstRun "<<m_firstRun<< "max_since " <<max_since<< std::endl;
114 
115  unsigned int max_run=m_lastRun;
116  edm::LogInfo("EcalTPGWeightGroupHandler") << "min_run= " << min_run << " max_run= " << max_run;
117 
118  RunList my_list;
119  my_list=econn->fetchGlobalRunListByLocation(my_runtag, min_run, max_run, my_locdef);
120  // my_list=econn->fetchRunListByLocation(my_runtag,min_run,max_run,my_locdef);
121 
122  std::vector<RunIOV> run_vec= my_list.getRuns();
123  size_t num_runs=run_vec.size();
124 
125  std::cout <<"number of runs is : "<< num_runs<< std::endl;
126 
127  unsigned int irun=0;
128  if(num_runs>0){
129 
130  // going to query the ecal logic id
131  std::vector<EcalLogicID> my_StripEcalLogicId_EE;
132  my_StripEcalLogicId_EE = econn->getEcalLogicIDSetOrdered( "ECAL_readout_strip",
133  1, 2000,
134  1, 70,
135  0,5,
136  "EE_offline_stripid",123 );
137 
138  std::cout <<" GOT the logic ID for the EE trigger strips "<< std::endl;
139 
140  for(size_t kr=0; kr<run_vec.size(); kr++){
141 
142  irun=static_cast<unsigned int>(run_vec[kr].getRunNumber());
143 
144  std::cout<<" **************** "<<std::endl;
145  std::cout<<" **************** "<<std::endl;
146  std::cout<<" run= "<<irun<<std::endl;
147 
148  // retrieve the data :
149  std::map<EcalLogicID, RunTPGConfigDat> dataset;
150  econn->fetchDataSet(&dataset, &run_vec[kr]);
151 
152  std::string the_config_tag="";
153  int the_config_version=0;
154 
155  std::map< EcalLogicID, RunTPGConfigDat>::const_iterator it;
156 
157  int nr=0;
158  for( it=dataset.begin(); it!=dataset.end(); it++ )
159  {
160  ++nr;
161  //EcalLogicID ecalid = it->first;
162 
163  RunTPGConfigDat dat = it->second;
164  the_config_tag=dat.getConfigTag();
165  the_config_version=dat.getVersion();
166  }
167 
168  // it is all the same for all SM... get the last one
169 
170 
171  std::cout<<" run= "<<irun<<" tag "<<the_config_tag<<" version="<<the_config_version <<std::endl;
172 
173  // here we should check if it is the same as previous run.
174 
175 
176  if((the_config_tag != m_i_tag || the_config_version != m_i_version ) && nr>0 ) {
177  std::cout<<"the tag is different from last transferred run ... retrieving last config set from DB"<<std::endl;
178 
179  FEConfigMainInfo fe_main_info;
180  fe_main_info.setConfigTag(the_config_tag);
181  fe_main_info.setVersion(the_config_version);
182 
183  try{
184  std::cout << " before fetch config set" << std::endl;
185  econn-> fetchConfigSet(&fe_main_info);
186  std::cout << " after fetch config set" << std::endl;
187 
188 
189  // now get TPGWeightGroup
190  int wId=fe_main_info.getWeiId();
191 
192  if( wId != m_i_weightGroup ) {
193 
194  FEConfigWeightInfo fe_w_info;
195  fe_w_info.setId(wId);
196  econn-> fetchConfigSet(&fe_w_info);
197  std::map<EcalLogicID, FEConfigWeightDat> dataset_TpgW;
198  econn->fetchDataSet(&dataset_TpgW, &fe_w_info);
199 
200 
201  EcalTPGWeightGroup* weightG = new EcalTPGWeightGroup;
202  typedef std::map<EcalLogicID, FEConfigWeightDat>::const_iterator CIfesli;
203  EcalLogicID ecid_xt;
204  int weightGroup;
205  int icells=0;
206 
207  std::map<std::string,int> map;
208  std::string str;
209 
210  for (CIfesli p = dataset_TpgW.begin(); p != dataset_TpgW.end(); p++) {
211  ecid_xt = p->first;
212  weightGroup = p->second.getWeightGroupId();
213 
214  std::string ecid_name=ecid_xt.getName();
215 
216  // EB data
217  if (ecid_name=="EB_VFE") {
218  int sm=ecid_xt.getID1();
219  int tt=ecid_xt.getID2();
220  int strip=ecid_xt.getID3();
221  int tcc= sm+54;
222  if(sm>18) tcc=sm+18 ;
223 
224  // simple formula to calculate the Srip EB identifier
225 
226  unsigned int stripEBId = 303176+(tt-1)*64+(strip-1)*8+(tcc-37)*8192;
227 
228  weightG->setValue(stripEBId,weightGroup);
229  ++icells;
230  }
231  else if (ecid_name=="ECAL_readout_strip"){
232  // EE data to add
233  int id1=ecid_xt.getID1();
234  int id2=ecid_xt.getID2();
235  int id3=ecid_xt.getID3();
236 
237  bool set_the_strip=false;
238  int stripEEId;
239  for (size_t istrip=0; istrip<my_StripEcalLogicId_EE.size(); istrip++) {
240 
241  if(!set_the_strip){
242 
243  if(my_StripEcalLogicId_EE[istrip].getID1()==id1
244  && my_StripEcalLogicId_EE[istrip].getID2()==id2
245  && my_StripEcalLogicId_EE[istrip].getID3()==id3
246  ){
247  stripEEId =my_StripEcalLogicId_EE[istrip].getLogicID();
248  set_the_strip=true;
249  break;
250  }
251  }
252 
253  }
254 
255  if(set_the_strip){
256 
257  weightG->setValue(stripEEId,weightGroup);
258 
259  } else {
260  std::cout <<" these may be the additional towers TCC/TT "
261  << id1<<"/"<<id2<<std::endl;
262  }
263 
264  ++icells;
265  }
266  }
267 
268  Time_t snc= (Time_t) irun ;
269 
270  m_to_transfer.push_back(std::make_pair((EcalTPGWeightGroup*)weightG,snc));
271 
272  m_i_run_number=irun;
273  m_i_tag=the_config_tag;
274  m_i_version=the_config_version;
275  m_i_weightGroup=wId;
276 
277  writeFile("last_tpg_weightGroup_settings.txt");
278 
279  } else {
280 
281  m_i_run_number=irun;
282  m_i_tag=the_config_tag;
283  m_i_version=the_config_version;
284 
285  writeFile("last_tpg_weightGroup_settings.txt");
286 
287  std::cout<< " even if the tag/version is not the same, the weight group id is the same -> no transfer needed "<< std::endl;
288 
289  }
290 
291  }
292 
293  catch (std::exception &e) {
294  std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" <<the_config_tag
295  <<" version="<<the_config_version<< std::endl;
296  std::cout << e.what() << std::endl;
297  m_i_run_number=irun;
298 
299  }
300  std::cout<<" **************** "<<std::endl;
301 
302  } else if(nr==0) {
303  m_i_run_number=irun;
304  std::cout<< " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed "<< std::endl;
305  std::cout<<" **************** "<<std::endl;
306  } else {
307  m_i_run_number=irun;
308  m_i_tag=the_config_tag;
309  m_i_version=the_config_version;
310  std::cout<< " the tag/version is the same -> no transfer needed "<< std::endl;
311  std::cout<<" **************** "<<std::endl;
312  writeFile("last_tpg_weightGroup_settings.txt");
313  }
314 
315  }
316  }
317 
318  delete econn;
319 
320  edm::LogInfo("EcalTPGWeightGroupHandler") << "Ecal - > end of getNewObjects -----------";
321 
322 }
323 
325  //-------------------------------------------------------------
326 
327  m_i_tag="";
328  m_i_version=0;
329  m_i_run_number=0;
330  m_i_weightGroup=0;
331 
332  FILE *inpFile; // input file
333  inpFile = fopen(inputFile,"r");
334  if(!inpFile) {
335  edm::LogError("EcalTPGWeightGroupHandler")<<"*** Can not open file: "<<inputFile;
336  }
337 
338  char line[256];
339 
340  std::ostringstream str;
341 
342  fgets(line,255,inpFile);
343  m_i_tag=to_string(line);
344  str << "gen tag " << m_i_tag << std::endl ; // should I use this?
345 
346  fgets(line,255,inpFile);
347  m_i_version=atoi(line);
348  str << "version= " << m_i_version << std::endl ;
349 
350  fgets(line,255,inpFile);
351  m_i_run_number=atoi(line);
352  str << "run_number= " << m_i_run_number << std::endl ;
353 
354  fgets(line,255,inpFile);
355  m_i_weightGroup=atoi(line);
356  str << "weightGroup_config= " << m_i_weightGroup << std::endl ;
357 
358 
359  fclose(inpFile); // close inp. file
360 
361 }
362 
364  //-------------------------------------------------------------
365 
366 
367  std::ofstream myfile;
368  myfile.open (inputFile);
369  myfile << m_i_tag <<std::endl;
370  myfile << m_i_version <<std::endl;
371  myfile << m_i_run_number <<std::endl;
372  myfile << m_i_weightGroup <<std::endl;
373 
374  myfile.close();
375 
376 }
T getParameter(std::string const &) const
std::vector< RunIOV > getRuns()
Definition: RunList.cc:35
void setLocationDef(const LocationDef locDef)
Definition: RunTag.cc:53
std::string getConfigTag() const
void strip(std::string &input, const std::string &blanks=" \n\t")
Definition: stringTools.cc:16
int getVersion() const
Definition: RunTag.h:13
void setVersion(int id)
int getID2() const
Definition: EcalLogicID.cc:52
std::string to_string(const T &t)
Definition: Logger.cc:26
std::string getName() const
Definition: EcalLogicID.cc:37
unsigned long long Time_t
Definition: Time.h:16
EcalTPGWeightGroupHandler(edm::ParameterSet const &)
void setRunTypeDef(const RunTypeDef runTypeDef)
Definition: RunTag.cc:70
int getWeiId() const
void setValue(const uint32_t &rawId, const uint32_t &ObjectId)
Definition: EcalTPGGroups.cc:9
int getID1() const
Definition: EcalLogicID.cc:47
void setRunType(std::string runtype)
Definition: RunTypeDef.cc:33
tuple dataset
Definition: dataset.py:400
void setConfigTag(std::string x)
Definition: IODConfig.h:31
void setLocation(std::string loc)
Definition: LocationDef.cc:33
tuple cout
Definition: gather_cfg.py:121
int getID3() const
Definition: EcalLogicID.cc:57
void setGeneralTag(std::string tag)
Definition: RunTag.cc:36
tuple size
Write out results.