CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
EcalTPGLutGroupHandler.cc
Go to the documentation of this file.
7 
9 
10 
12 
14 
15 
16 
23 
28 
29 
30 
31 #include<iostream>
32 #include<fstream>
33 
34 #include <time.h>
35 #include <unistd.h>
36 
37 #include <string>
38 #include <cstdio>
39 #include <typeinfo>
40 #include <sstream>
41 
43  : m_name(ps.getUntrackedParameter<std::string>("name","EcalTPGLutGroupHandler")) {
44 
45  edm::LogInfo("EcalTPGLutGroupHandler") << "EcalTPGLutGroup Source handler constructor";
46  m_firstRun=static_cast<unsigned int>(atoi( ps.getParameter<std::string>("firstRun").c_str()));
47  m_lastRun=static_cast<unsigned int>(atoi( ps.getParameter<std::string>("lastRun").c_str()));
48  m_sid= ps.getParameter<std::string>("OnlineDBSID");
49  m_user= ps.getParameter<std::string>("OnlineDBUser");
50  m_pass= ps.getParameter<std::string>("OnlineDBPassword");
51  m_locationsource= ps.getParameter<std::string>("LocationSource");
52  m_location=ps.getParameter<std::string>("Location");
53  m_gentag=ps.getParameter<std::string>("GenTag");
54  m_runtype=ps.getParameter<std::string>("RunType");
55 
56  edm::LogInfo("EcalTPGLutGroupHandler") << m_sid<<"/"<<m_user<<"/"<<m_location<<"/"<<m_gentag;
57 }
58 
60 {
61 }
62 
63 
65 {
66 
67 
68  using namespace edm;
69  using namespace std;
70 
71  edm::LogInfo("EcalTPGLutGroupHandler") << "Started GetNewObjects!!!";
72 
73 
74  /*
75  // geometry
76  ESHandle<CaloGeometry> theGeometry;
77  ESHandle<CaloSubdetectorGeometry> theEndcapGeometry_handle, theBarrelGeometry_handle;
78  evtSetup.get<CaloGeometryRecord>().get( theGeometry );
79  evtSetup.get<EcalEndcapGeometryRecord>().get("EcalEndcap",theEndcapGeometry_handle);
80  evtSetup.get<EcalBarrelGeometryRecord>().get("EcalBarrel",theBarrelGeometry_handle);
81  evtSetup.get<IdealGeometryRecord>().get(eTTmap_);
82  theEndcapGeometry_ = &(*theEndcapGeometry_handle);
83  theBarrelGeometry_ = &(*theBarrelGeometry_handle);
84 
85  // electronics mapping
86  ESHandle< EcalElectronicsMapping > ecalmapping;
87  evtSetup.get< EcalMappingRcd >().get(ecalmapping);
88  theMapping_ = ecalmapping.product();
89 
90  const std::vector<DetId> & eeCells = theEndcapGeometry_->getValidDetIds(DetId::Ecal, EcalEndcap);
91 
92  */
93 
94 
95 
96  //check whats already inside of database
97  if (tagInfo().size){
98  //check whats already inside of database
99  std::cout << "got offlineInfo = " << std::endl;
100  std::cout << "tag name = " << tagInfo().name << std::endl;
101  std::cout << "size = " << tagInfo().size << std::endl;
102  } else {
103  std::cout << " First object for this tag " << std::endl;
104  }
105 
106  unsigned int max_since=0;
107  max_since=static_cast<unsigned int>(tagInfo().lastInterval.first);
108  edm::LogInfo("EcalTPGLutGroupHandler") << "max_since : " << max_since;
109  edm::LogInfo("EcalTPGLutGroupHandler") << "retrieved last payload ";
110 
111  // here we retrieve all the runs after the last from online DB
112  edm::LogInfo("EcalTPGLutGroupHandler") << "Retrieving run list from ONLINE DB ... ";
113 
114  edm::LogInfo("EcalTPGLutGroupHandler") << "Making connection...";
115  econn = new EcalCondDBInterface( m_sid, m_user, m_pass );
116  edm::LogInfo("EcalTPGLutGroupHandler") << "Done.";
117 
118  if (!econn)
119  {
120  std::cout << " connection parameters " <<m_sid <<"/"<<m_user<<std::endl;
121  // cerr << e.what() << std::endl;
122  throw cms::Exception("OMDS not available");
123  }
124 
125 
126  LocationDef my_locdef;
127  my_locdef.setLocation(m_location);
128 
129  RunTypeDef my_rundef;
130  my_rundef.setRunType(m_runtype);
131 
132  RunTag my_runtag;
133  my_runtag.setLocationDef( my_locdef );
134  my_runtag.setRunTypeDef( my_rundef );
135  my_runtag.setGeneralTag(m_gentag);
136 
137 
138  readFromFile("last_tpg_lutGroup_settings.txt");
139 
140 
141  unsigned int min_run=m_i_run_number+1;
142 
143  if(m_firstRun<m_i_run_number) {
144  min_run=m_i_run_number+1;
145  } else {
146  min_run=m_firstRun;
147  }
148 
149 
150  if(min_run<max_since) {
151  min_run= max_since+1; // we have to add 1 to the last transferred one
152  }
153 
154  std::cout<<"m_i_run_number"<< m_i_run_number <<"m_firstRun "<<m_firstRun<< "max_since " <<max_since<< std::endl;
155 
156  unsigned int max_run=m_lastRun;
157  edm::LogInfo("EcalTPGLutGroupHandler") << "min_run= " << min_run << " max_run= " << max_run;
158 
159  RunList my_list;
160  my_list=econn->fetchGlobalRunListByLocation(my_runtag, min_run, max_run, my_locdef);
161  // my_list=econn->fetchRunListByLocation(my_runtag,min_run,max_run,my_locdef);
162 
163  std::vector<RunIOV> run_vec= my_list.getRuns();
164  size_t num_runs=run_vec.size();
165 
166  std::cout <<"number of runs is : "<< num_runs<< std::endl;
167 
168  std::string str="";
169 
170  unsigned int irun;
171  if(num_runs>0){
172 
173 
174  // going to query the ecal logic id
175  std::vector<EcalLogicID> my_TTEcalLogicId_EE;
176  my_TTEcalLogicId_EE = econn->getEcalLogicIDSetOrdered( "EE_trigger_tower",
177  1, 200,
178  1, 70,
180  "EE_offline_towerid",12 );
181  std::cout <<" GOT the logic ID for the EE trigger towers "<< std::endl;
182 
183  for(size_t kr=0; kr<run_vec.size(); kr++){
184 
185  irun=static_cast<unsigned int>(run_vec[kr].getRunNumber());
186 
187  std::cout<<" **************** "<<std::endl;
188  std::cout<<" run= "<<irun<<std::endl;
189 
190  // retrieve the data :
191  std::map<EcalLogicID, RunTPGConfigDat> dataset;
192  econn->fetchDataSet(&dataset, &run_vec[kr]);
193 
194  std::string the_config_tag="";
195  int the_config_version=0;
196 
197  std::map< EcalLogicID, RunTPGConfigDat>::const_iterator it;
198 
199  int nr=0;
200  for( it=dataset.begin(); it!=dataset.end(); it++ )
201  {
202  ++nr;
203  //EcalLogicID ecalid = it->first;
204 
205  RunTPGConfigDat dat = it->second;
206  the_config_tag=dat.getConfigTag();
207  the_config_version=dat.getVersion();
208  }
209 
210 
211  // it is all the same for all SM... get the last one
212 
213 
214  std::cout<<" run= "<<irun<<" tag "<<the_config_tag<<" version="<<the_config_version <<std::endl;
215 
216  // here we should check if it is the same as previous run.
217 
218 
219  if((the_config_tag != m_i_tag || the_config_version != m_i_version ) && nr>0 ) {
220  std::cout<<"the tag is different from last transferred run ... retrieving last config set from DB"<<std::endl;
221 
222  FEConfigMainInfo fe_main_info;
223  fe_main_info.setConfigTag(the_config_tag);
224  fe_main_info.setVersion(the_config_version);
225 
226  try{
227  std::cout << " before fetch config set" << std::endl;
228  econn-> fetchConfigSet(&fe_main_info);
229  std::cout << " after fetch config set" << std::endl;
230 
231 
232  // now get TPGLutGroup
233  int lutId=fe_main_info.getLUTId();
234 
235  if( lutId != m_i_lutGroup ) {
236 
237  FEConfigLUTInfo fe_lut_info;
238  fe_lut_info.setId(lutId);
239  econn-> fetchConfigSet(&fe_lut_info);
240  std::map<EcalLogicID, FEConfigLUTDat> dataset_TpgLut;
241  econn->fetchDataSet(&dataset_TpgLut, &fe_lut_info);
242 
244  typedef std::map<EcalLogicID, FEConfigLUTDat>::const_iterator CIfelut;
245  EcalLogicID ecid_xt;
246  FEConfigLUTDat rd_lut;
247  int itowers=0;
248 
249  for (CIfelut p = dataset_TpgLut.begin(); p != dataset_TpgLut.end(); p++)
250  {
251  ecid_xt = p->first;
252  rd_lut = p->second;
253 
254  std::string ecid_name=ecid_xt.getName();
255 
256  if(ecid_name=="EB_trigger_tower") {
257  // SM number
258  int smid=ecid_xt.getID1();
259  // TT number
260  int towerid=ecid_xt.getID2();
261 
262 
263  int tow_eta=(towerid-1)/4;
264  int tow_phi=((towerid-1)-tow_eta*4);
265 
266  int axt=(tow_eta*5)*20 + tow_phi*5 +1 ;
267 
268  EBDetId id(smid, axt, EBDetId::SMCRYSTALMODE ) ;
269  const EcalTrigTowerDetId towid= id.tower();
270 
271  /*
272  char ch[10];
273  sprintf(ch,"%d%d", smid, towerid);
274  std::string S="";
275  S.insert(0,ch);
276 
277  unsigned int towerEBId = 0;
278  towerEBId = atoi(S.c_str());
279  */
280 
281 
282  lut->setValue(towid.rawId(), rd_lut.getLUTGroupId());
283  ++itowers;
284  }
285  else if (ecid_name=="EE_trigger_tower") {
286  // EE data
287  // TCC number
288  int tccid=ecid_xt.getID1();
289  // TT number
290  int towerid=ecid_xt.getID2();
291 
292  bool set_the_tower=false;
293  int towid;
294  for (size_t itower=0; itower<my_TTEcalLogicId_EE.size(); itower++) {
295 
296  if(!set_the_tower){
297 
298  if(my_TTEcalLogicId_EE[itower].getID1()==tccid && my_TTEcalLogicId_EE[itower].getID2()==towerid){
299  towid =my_TTEcalLogicId_EE[itower].getLogicID();
300  set_the_tower=true;
301  break;
302  }
303  }
304 
305  }
306 
307  if(set_the_tower){
308  lut->setValue(towid, rd_lut.getLUTGroupId());
309  } else {
310  std::cout <<" these may be the additional towers TCC/TT "
311  << tccid<<"/"<<towerid<<std::endl;
312  }
313 
314  ++itowers;
315  }
316  }
317 
318 
319  Time_t snc= (Time_t) irun;
320 
321  m_to_transfer.push_back(std::make_pair((EcalTPGLutGroup*)lut,snc));
322 
323  m_i_run_number=irun;
324  m_i_tag=the_config_tag;
325  m_i_version=the_config_version;
326  m_i_lutGroup=lutId;
327 
328  writeFile("last_tpg_lutGroup_settings.txt");
329 
330  } else {
331 
332  m_i_run_number=irun;
333  m_i_tag=the_config_tag;
334  m_i_version=the_config_version;
335 
336  writeFile("last_tpg_lutGroup_settings.txt");
337 
338  std::cout<< " even if the tag/version is not the same, the lutGroup id is the same -> no transfer needed "<< std::endl;
339 
340  }
341 
342  }
343 
344  catch (std::exception &e) {
345  std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" <<the_config_tag
346  <<" version="<<the_config_version<< std::endl;
347  std::cout << e.what() << std::endl;
348  m_i_run_number=irun;
349 
350  }
351  std::cout<<" **************** "<<std::endl;
352 
353  } else if(nr==0) {
354  m_i_run_number=irun;
355  std::cout<< " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed "<< std::endl;
356  std::cout<<" **************** "<<std::endl;
357  } else {
358  m_i_run_number=irun;
359  m_i_tag=the_config_tag;
360  m_i_version=the_config_version;
361  std::cout<< " the tag/version is the same -> no transfer needed "<< std::endl;
362  std::cout<<" **************** "<<std::endl;
363  writeFile("last_tpg_lutGroup_settings.txt");
364  }
365  }
366  }
367 
368  delete econn;
369 
370  edm::LogInfo("EcalTPGLutGroupHandler") << "Ecal - > end of getNewObjects -----------";
371 }
372 
374  //-------------------------------------------------------------
375 
376  m_i_tag="";
377  m_i_version=0;
378  m_i_run_number=0;
379  m_i_lutGroup=0;
380 
381  FILE *inpFile; // input file
382  inpFile = fopen(inputFile,"r");
383  if(!inpFile) {
384  edm::LogError("EcalTPGLutGroupHandler")<<"*** Can not open file: "<<inputFile;
385  return;
386  }
387 
388  char line[256];
389 
390  std::ostringstream str;
391 
392  fgets(line,255,inpFile);
393  m_i_tag=to_string(line);
394  str << "gen tag " << m_i_tag << std::endl ; // should I use this?
395 
396  fgets(line,255,inpFile);
397  m_i_version=atoi(line);
398  str << "version= " << m_i_version << std::endl ;
399 
400  fgets(line,255,inpFile);
401  m_i_run_number=atoi(line);
402  str << "run_number= " << m_i_run_number << std::endl ;
403 
404  fgets(line,255,inpFile);
405  m_i_lutGroup=atoi(line);
406  str << "lutGroup_config= " << m_i_lutGroup << std::endl ;
407 
408 
409  fclose(inpFile); // close inp. file
410 
411 }
412 
414  //-------------------------------------------------------------
415 
416 
417  std::ofstream myfile;
418  myfile.open (inputFile);
419  myfile << m_i_tag <<std::endl;
420  myfile << m_i_version <<std::endl;
421  myfile << m_i_run_number <<std::endl;
422  myfile << m_i_lutGroup <<std::endl;
423 
424  myfile.close();
425 
426 }
T getParameter(std::string const &) const
std::vector< RunIOV > getRuns()
Definition: RunList.cc:35
void readFromFile(const char *inputFile)
std::string getConfigTag() const
int getVersion() const
Definition: RunTag.h:13
void setVersion(int id)
void writeFile(const char *inputFile)
int getID2() const
Definition: EcalLogicID.cc:51
uint32_t rawId() const
get the raw id
Definition: DetId.h:43
std::string getName() const
Definition: EcalLogicID.cc:36
void setValue(const uint32_t &rawId, const uint32_t &ObjectId)
Definition: EcalTPGGroups.cc:9
int getID1() const
Definition: EcalLogicID.cc:46
tuple lut
Definition: lumiPlot.py:244
void setLocationDef(const LocationDef &locDef)
Definition: RunTag.cc:53
void setRunType(std::string runtype)
Definition: RunTypeDef.cc:33
tuple dataset
Definition: dataset.py:859
cond::Time_t Time_t
Definition: Time.h:17
void setConfigTag(std::string x)
Definition: IODConfig.h:31
static const int NULLID
Definition: EcalLogicID.h:42
int getLUTGroupId() const
void setLocation(std::string loc)
Definition: LocationDef.cc:33
tuple cout
Definition: gather_cfg.py:145
int getLUTId() const
static const int SMCRYSTALMODE
Definition: EBDetId.h:167
tuple size
Write out results.
EcalTPGLutGroupHandler(edm::ParameterSet const &)
void setId(int id)