CMS 3D CMS Logo

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