CMS 3D CMS Logo

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