CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
EcalTPGFineGrainEBGroupHandler.cc
Go to the documentation of this file.
10 
11 #include<iostream>
12 #include<fstream>
13 
14 
15 #include <time.h>
16 #include <unistd.h>
17 
18 #include <string>
19 #include <cstdio>
20 #include <typeinfo>
21 #include <sstream>
22 
24  : m_name(ps.getUntrackedParameter<std::string>("name","EcalTPGFineGrainEBGroupHandler")) {
25 
26  edm::LogInfo("EcalTPGFineGrainEBGroupHandler") << "EcalTPGFineGrainEBGroup Source handler constructor.";
27  m_firstRun=static_cast<unsigned int>(atoi( ps.getParameter<std::string>("firstRun").c_str()));
28  m_lastRun=static_cast<unsigned int>(atoi( ps.getParameter<std::string>("lastRun").c_str()));
29  m_sid= ps.getParameter<std::string>("OnlineDBSID");
30  m_user= ps.getParameter<std::string>("OnlineDBUser");
31  m_pass= ps.getParameter<std::string>("OnlineDBPassword");
32  m_locationsource= ps.getParameter<std::string>("LocationSource");
33  m_location=ps.getParameter<std::string>("Location");
34  m_gentag=ps.getParameter<std::string>("GenTag");
35  m_runtype=ps.getParameter<std::string>("RunType");
36 
37  edm::LogInfo("EcalTPGFineGrainEBGroupHandler")<< m_sid<<"/"<<m_user<<"/"<<m_location<<"/"<<m_gentag;
38 
39 }
40 
42 {
43 }
44 
46 {
47 
48  edm::LogInfo("EcalTPGFineGrainEBGroupHandler") << "Started GetNewObjects!!!";
49 
50  //check whats already inside of database
51  if (tagInfo().size){
52  //check whats already inside of database
53  std::cout << "got offlineInfo = " << std::endl;
54  std::cout << "tag name = " << tagInfo().name << std::endl;
55  std::cout << "size = " << tagInfo().size << std::endl;
56  } else {
57  std::cout << " First object for this tag " << std::endl;
58  }
59 
60  unsigned int max_since=0;
61  max_since=static_cast<unsigned int>(tagInfo().lastInterval.first);
62  edm::LogInfo("EcalTPGFineGrainEBGroupHandler") << "max_since : " << max_since;
63  Ref fgrGroup_db = lastPayload();
64 
65  edm::LogInfo("EcalTPGFineGrainEBGroupHandler") << "retrieved last payload ";
66 
67  // here we retrieve all the runs after the last from online DB
68  edm::LogInfo("EcalTPGFineGrainEBGroupHandler") << "Retrieving run list from ONLINE DB ... ";
69 
70  edm::LogInfo("EcalTPGFineGrainEBGroupHandler") << "Making connection...";
71  econn = new EcalCondDBInterface( m_sid, m_user, m_pass );
72  edm::LogInfo("EcalTPGFineGrainEBGroupHandler") << "Done.";
73 
74  if (!econn)
75  {
76  std::cout << " connection parameters " <<m_sid <<"/"<<m_user<<std::endl;
77  // cerr << e.what() << std::endl;
78  throw cms::Exception("OMDS not available");
79  }
80 
81 
82  LocationDef my_locdef;
83  my_locdef.setLocation(m_location);
84 
85  RunTypeDef my_rundef;
86  my_rundef.setRunType(m_runtype);
87 
88  RunTag my_runtag;
89  my_runtag.setLocationDef( my_locdef );
90  my_runtag.setRunTypeDef( my_rundef );
91  my_runtag.setGeneralTag(m_gentag);
92 
93  readFromFile("last_tpg_fgrGroup_settings.txt");
94 
95 
96  unsigned int min_run=m_i_run_number+1;
97 
98  if(m_firstRun<m_i_run_number) {
99  min_run=m_i_run_number+1;
100  } else {
101  min_run=m_firstRun;
102  }
103  if(min_run<max_since) {
104  min_run=max_since+1; // we have to add 1 to the last transferred one
105  }
106 
107  std::cout<<"m_i_run_number"<< m_i_run_number <<"m_firstRun "<<m_firstRun<< "max_since " <<max_since<< std::endl;
108 
109  unsigned int max_run=m_lastRun;
110  edm::LogInfo("EcalTPGFineGrainEBGroupHandler") << "min_run= " << min_run << " max_run= " << max_run;
111 
112  RunList my_list;
113  my_list=econn->fetchGlobalRunListByLocation(my_runtag, min_run, max_run, my_locdef);
114  // my_list=econn->fetchRunListByLocation(my_runtag,min_run,max_run,my_locdef);
115  printf ("after fetchRunList\n");fflush(stdout);
116 
117  std::vector<RunIOV> run_vec= my_list.getRuns();
118  size_t num_runs=run_vec.size();
119 
120  std::cout <<"number of runs is : "<< num_runs<< std::endl;
121 
122  unsigned int irun;
123  if(num_runs>0){
124 
125  for(size_t kr=0; kr<run_vec.size(); kr++){
126 
127  irun=static_cast<unsigned int>(run_vec[kr].getRunNumber());
128 
129  std::cout<<" **************** "<<std::endl;
130  std::cout<<" **************** "<<std::endl;
131  std::cout<<" run= "<<irun<<std::endl;
132 
133  // retrieve the data
134  std::map<EcalLogicID, RunTPGConfigDat> dataset;
135  econn->fetchDataSet(&dataset, &run_vec[kr]);
136 
137  std::string the_config_tag="";
138  int the_config_version=0;
139 
140  std::map< EcalLogicID, RunTPGConfigDat>::const_iterator it;
141 
142  int nr=0;
143  for( it=dataset.begin(); it!=dataset.end(); it++ )
144  {
145  ++nr;
146  //EcalLogicID ecalid = it->first;
147  RunTPGConfigDat dat = it->second;
148  the_config_tag=dat.getConfigTag();
149  the_config_version=dat.getVersion();
150  }
151 
152  // it is all the same for all SM... get the last one
153 
154 
155  std::cout<<" run= "<<irun<<" tag "<<the_config_tag<<" version="<<the_config_version <<std::endl;
156 
157  // here we should check if it is the same as previous run.
158 
159 
160  if((the_config_tag != m_i_tag || the_config_version != m_i_version ) && nr>0 ) {
161  std::cout<<"the tag is different from last transferred run ... retrieving last config set from DB"<<std::endl;
162 
163  FEConfigMainInfo fe_main_info;
164  fe_main_info.setConfigTag(the_config_tag);
165  fe_main_info.setVersion(the_config_version);
166 
167  try{
168  std::cout << " before fetch config set" << std::endl;
169  econn-> fetchConfigSet(&fe_main_info);
170  std::cout << " after fetch config set" << std::endl;
171 
172 
173  // now get TPGFineGrainEBGroup
174  int fgrId=fe_main_info.getFgrId();
175 
176  if( fgrId != m_i_fgrGroup ) {
177 
178  FEConfigFgrInfo fe_fgr_info;
179  fe_fgr_info.setId(fgrId);
180  econn-> fetchConfigSet(&fe_fgr_info);
181  std::map<EcalLogicID, FEConfigFgrDat> dataset_TpgFineGrainEB;
182  econn->fetchDataSet(&dataset_TpgFineGrainEB, &fe_fgr_info);
183 
185  typedef std::map<EcalLogicID, FEConfigFgrDat>::const_iterator CIfefgr;
186  EcalLogicID ecid_xt;
187  FEConfigFgrDat rd_fgr;
188  int itowers=0;
189 
190  for (CIfefgr p = dataset_TpgFineGrainEB.begin(); p != dataset_TpgFineGrainEB.end(); p++) {
191  ecid_xt = p->first;
192  rd_fgr = p->second;
193 
194  std::string ecid_name=ecid_xt.getName();
195 
196  if (ecid_name=="EB_trigger_tower") {
197  // SM number
198  int smid=ecid_xt.getID1();
199  // TT number
200  int towerid=ecid_xt.getID2();
201 
202  /*
203  char identTT[10];
204  sprintf(identTT,"%d%d", smid, towerid);
205 
206  std::string S="";
207  S.insert(0,identTT);
208 
209  unsigned int towerEBId = 0;
210  towerEBId = atoi(S.c_str());
211 
212  */
213 
214  int tow_eta=(towerid-1)/4;
215  int tow_phi=((towerid-1)-tow_eta*4);
216 
217  int axt=(tow_eta*5)*20 + tow_phi*5 +1 ;
218 
219  EBDetId id(smid, axt, EBDetId::SMCRYSTALMODE ) ;
220  const EcalTrigTowerDetId towid= id.tower();
221 
222 
223  fgrMap->setValue(towid.rawId() , rd_fgr.getFgrGroupId());
224  ++itowers;
225  }
226  }
227 
228  Time_t snc= (Time_t) irun ;
229 
230  m_to_transfer.push_back(std::make_pair((EcalTPGFineGrainEBGroup *)fgrMap,snc));
231 
232  m_i_run_number=irun;
233  m_i_tag=the_config_tag;
234  m_i_version=the_config_version;
235  m_i_fgrGroup=fgrId;
236 
237  writeFile("last_tpg_fgrGroup_settings.txt");
238 
239  } else {
240 
241  m_i_run_number=irun;
242  m_i_tag=the_config_tag;
243  m_i_version=the_config_version;
244 
245  writeFile("last_tpg_fgrGroup_settings.txt");
246 
247  std::cout<< " even if the tag/version is not the same, the fgrGroup id is the same -> no transfer needed "<< std::endl;
248 
249  }
250 
251  }
252 
253  catch (std::exception &e) {
254  std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" <<the_config_tag
255  <<" version="<<the_config_version<< std::endl;
256  std::cout << e.what() << std::endl;
257  m_i_run_number=irun;
258 
259  }
260  std::cout<<" **************** "<<std::endl;
261 
262  } else if(nr==0) {
263  m_i_run_number=irun;
264  std::cout<< " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed "<< std::endl;
265  std::cout<<" **************** "<<std::endl;
266  } else {
267  m_i_run_number=irun;
268  m_i_tag=the_config_tag;
269  m_i_version=the_config_version;
270  std::cout<< " the tag/version is the same -> no transfer needed "<< std::endl;
271  std::cout<<" **************** "<<std::endl;
272  writeFile("last_tpg_fgrGroup_settings.txt");
273  }
274 
275 
276  }
277  }
278 
279  delete econn;
280  edm::LogInfo("EcalTPGFineGrainEBGroupHandler") << "Ecal - > end of getNewObjects -----------";
281 }
282 
283 
285  //-------------------------------------------------------------
286 
287  m_i_tag="";
288  m_i_version=0;
289  m_i_run_number=0;
290  m_i_fgrGroup=0;
291 
292  FILE *inpFile; // input file
293  inpFile = fopen(inputFile,"r");
294  if(!inpFile) {
295  edm::LogError("EcalTPGFineGrainEBGroupHandler")<<"*** Can not open file: "<<inputFile;
296  return;
297  }
298 
299  char line[256];
300 
301  std::ostringstream str;
302 
303  fgets(line,255,inpFile);
304  m_i_tag=to_string(line);
305  str << "gen tag " << m_i_tag << std::endl ; // should I use this?
306 
307  fgets(line,255,inpFile);
308  m_i_version=atoi(line);
309  str << "version= " << m_i_version << std::endl ;
310 
311  fgets(line,255,inpFile);
312  m_i_run_number=atoi(line);
313  str << "run_number= " << m_i_run_number << std::endl ;
314 
315  fgets(line,255,inpFile);
316  m_i_fgrGroup=atoi(line);
317  str << "fgrGroup_config= " << m_i_fgrGroup << std::endl ;
318 
319 
320  fclose(inpFile); // close inp. file
321 
322 }
323 
325  //-------------------------------------------------------------
326 
327 
328  std::ofstream myfile;
329  myfile.open (inputFile);
330  myfile << m_i_tag <<std::endl;
331  myfile << m_i_version <<std::endl;
332  myfile << m_i_run_number <<std::endl;
333  myfile << m_i_fgrGroup <<std::endl;
334 
335  myfile.close();
336 
337 }
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
void setVersion(int id)
int getID2() const
Definition: EcalLogicID.cc:51
uint32_t rawId() const
get the raw id
Definition: DetId.h:43
std::string to_string(const T &t)
Definition: Logger.cc:26
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
void setLocationDef(const LocationDef &locDef)
Definition: RunTag.cc:53
void setId(int id)
int getFgrId() const
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
void setLocation(std::string loc)
Definition: LocationDef.cc:33
tuple cout
Definition: gather_cfg.py:121
void setGeneralTag(std::string tag)
Definition: RunTag.cc:36
int getFgrGroupId() const
static const int SMCRYSTALMODE
Definition: EBDetId.h:167
tuple size
Write out results.