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  unsigned int rd_slid;
210  int icells=0;
211 
212 
213  for (CIfesli p = dataset_TpgSli.begin(); p != dataset_TpgSli.end(); p++) {
214  ecid_xt = p->first;
215  rd_sli = p->second;
216 
217  std::string ecid_name=ecid_xt.getName();
218 
219  // EB data
220  if (ecid_name=="EB_VFE") {
221  int sm=ecid_xt.getID1();
222  int tt=ecid_xt.getID2();
223  int strip=ecid_xt.getID3();
224  int tcc= sm+54;
225  if(sm>18) tcc=sm+18 ;
226  rd_slid = (unsigned int)rd_sli.getSliding();
227 
228  // simple formula to calculate the Srip EB identifier
229 
230  unsigned int stripEBId = 303176+(tt-1)*64+(strip-1)*8+(tcc-37)*8192;
231 
232  sliW->setValue(stripEBId, (unsigned int)rd_sli.getSliding());
233  ++icells;
234 
235  }
236  else if (ecid_name=="ECAL_readout_strip"){
237 
238  // EE data
239  int id1=ecid_xt.getID1(); // dcc
240  int id2=ecid_xt.getID2(); // ccu
241  int id3=ecid_xt.getID3(); // strip
242 
243  rd_slid = (unsigned int)rd_sli.getSliding();
244 
245 
246  bool set_the_strip=false;
247  int stripid;
248  for (size_t istrip=0; istrip<my_StripEcalLogicId_EE.size(); istrip++) {
249 
250  if(!set_the_strip){
251 
252  if(my_StripEcalLogicId_EE[istrip].getID1()==id1
253  && my_StripEcalLogicId_EE[istrip].getID2()==id2
254  && my_StripEcalLogicId_EE[istrip].getID3()==id3
255  ){
256  stripid =my_StripEcalLogicId_EE[istrip].getLogicID();
257  set_the_strip=true;
258  break;
259  }
260  }
261 
262  }
263 
264  if(set_the_strip){
265 
266  sliW->setValue(stripid, (unsigned int)rd_sli.getSliding());
267 
268  } else {
269  std::cout <<" these may be the additional towers TCC/TT "
270  << id1<<"/"<<id2<<std::endl;
271  }
272 
273 
274  ++icells;
275  }
276  }
277 
278 
279  Time_t snc= (Time_t) irun;
280  m_to_transfer.push_back(std::make_pair((EcalTPGSlidingWindow*)sliW,snc));
281 
282  m_i_run_number=irun;
283  m_i_tag=the_config_tag;
284  m_i_version=the_config_version;
285  m_i_sliding=slidingId;
286 
287  writeFile("last_tpg_sliding_settings.txt");
288 
289  } else {
290 
291  m_i_run_number=irun;
292  m_i_tag=the_config_tag;
293  m_i_version=the_config_version;
294 
295  writeFile("last_tpg_sliding_settings.txt");
296 
297  std::cout<< " even if the tag/version is not the same, the sliding windows id is the same -> no transfer needed "<< std::endl;
298 
299  }
300  } catch (std::exception &e) {
301  std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" <<the_config_tag
302  <<" version="<<the_config_version<< std::endl;
303  std::cout << e.what() << std::endl;
304 
305  m_i_run_number=irun;
306 
307  }
308 
309  std::cout<<" **************** "<<std::endl;
310 
311 
312  } else if(nr==0) {
313  m_i_run_number=irun;
314  std::cout<< " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed "<< std::endl;
315  std::cout<<" **************** "<<std::endl;
316  } else {
317  m_i_run_number=irun;
318  m_i_tag=the_config_tag;
319  m_i_version=the_config_version;
320  std::cout<< " the tag/version is the same -> no transfer needed "<< std::endl;
321  std::cout<<" **************** "<<std::endl;
322  writeFile("last_tpg_sliding_settings.txt");
323  }
324 
325  }
326  }
327 
328  delete econn;
329 
330  edm::LogInfo("EcalTPGSlidingWindowHandler") << "Ecal - > end of getNewObjects -----------";
331 
332 }
333 
335  //-------------------------------------------------------------
336 
337  m_i_tag="";
338  m_i_version=0;
339  m_i_run_number=0;
340  m_i_sliding=0;
341 
342  FILE *inpFile; // input file
343  inpFile = fopen(inputFile,"r");
344  if(!inpFile) {
345  edm::LogError("EcalTPGSlidingWindowHandler")<<"*** Can not open file: "<<inputFile;
346  }
347 
348  char line[256];
349 
350  std::ostringstream str;
351 
352  fgets(line,255,inpFile);
353  m_i_tag=to_string(line);
354  str << "gen tag " << m_i_tag << std::endl ; // should I use this?
355 
356  fgets(line,255,inpFile);
357  m_i_version=atoi(line);
358  str << "version= " << m_i_version << std::endl ;
359 
360  fgets(line,255,inpFile);
361  m_i_run_number=atoi(line);
362  str << "run_number= " << m_i_run_number << std::endl ;
363 
364  fgets(line,255,inpFile);
365  m_i_sliding=atoi(line);
366  str << "sliding_config= " << m_i_sliding << std::endl ;
367 
368 
369  fclose(inpFile); // close inp. file
370 
371 }
372 
374  //-------------------------------------------------------------
375 
376 
377  std::ofstream myfile;
378  myfile.open (inputFile);
379  myfile << m_i_tag <<std::endl;
380  myfile << m_i_version <<std::endl;
381  myfile << m_i_run_number <<std::endl;
382  myfile << m_i_sliding <<std::endl;
383 
384  myfile.close();
385 
386 }
387 
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
float getSliding() const
void setVersion(int id)
int getID2() const
Definition: EcalLogicID.cc:52
std::string to_string(const T &t)
Definition: Logger.cc:25
std::string getName() const
Definition: EcalLogicID.cc:37
unsigned long long Time_t
Definition: Time.h:16
void setRunTypeDef(const RunTypeDef runTypeDef)
Definition: RunTag.cc:70
int getID1() const
Definition: EcalLogicID.cc:47
void setValue(const uint32_t &id, const uint32_t &value)
void setRunType(std::string runtype)
Definition: RunTypeDef.cc:33
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:41
int getID3() const
Definition: EcalLogicID.cc:57
void setGeneralTag(std::string tag)
Definition: RunTag.cc:36
tuple size
Write out results.
int getSliId() const