CMS 3D CMS Logo

EcalTPGLinConstHandler.cc
Go to the documentation of this file.
2 
9 
10 #include<iostream>
11 
12 #include<fstream>
13 
14 
15 #include <ctime>
16 #include <unistd.h>
17 
18 #include <string>
19 #include <cstdio>
20 #include <typeinfo>
21 #include <sstream>
22 
23 
25  : m_name(ps.getUntrackedParameter<std::string>("name","EcalTPGLinConstHandler")) {
26 
27  edm::LogInfo("EcalTPGLinConstHandler") << "EcalTPGLinConst Source handler constructor";
28  m_firstRun=static_cast<unsigned int>(atoi( ps.getParameter<std::string>("firstRun").c_str()));
29  m_lastRun=static_cast<unsigned int>(atoi( ps.getParameter<std::string>("lastRun").c_str()));
30  m_sid= ps.getParameter<std::string>("OnlineDBSID");
31  m_user= ps.getParameter<std::string>("OnlineDBUser");
32  m_pass= ps.getParameter<std::string>("OnlineDBPassword");
33  m_locationsource= ps.getParameter<std::string>("LocationSource");
34  m_location=ps.getParameter<std::string>("Location");
35  m_gentag=ps.getParameter<std::string>("GenTag");
36  m_runtype=ps.getParameter<std::string>("RunType");
37 
38  edm::LogInfo("EcalTPGLinConstHandler") << m_sid <<"/"<<m_user<<"/"<<m_location<<"/"<<m_gentag;
39 }
40 
42 {
43 }
44 
45 
47 {
48 
49  edm::LogInfo("EcalTPGLinConstHandler") << "Started getNewObjects";
50 
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("EcalTPGLinConstHandler") << "max_since = " << max_since;
63  edm::LogInfo("EcalTPGLinConstHandler")<< "Retrieved last payload ";
64 
65  // here we retrieve all the runs after the last from online DB
66  edm::LogInfo("EcalTPGLinConstHandler")<< "Retrieving run list from ONLINE DB ... " << std::endl;
67 
68  edm::LogInfo("EcalTPGLinConstHandler") << "Making connection..." << std::flush;
70  edm::LogInfo("EcalTPGLinConstHandler") << "Done." << std::endl;
71 
72  if (!econn)
73  {
74  std::cout << " connection parameters " <<m_sid <<"/"<<m_user<<std::endl;
75  // cerr << e.what() << std::endl;
76  throw cms::Exception("OMDS not available");
77  }
78 
79 
80  LocationDef my_locdef;
81  my_locdef.setLocation(m_location);
82 
83  RunTypeDef my_rundef;
84  my_rundef.setRunType(m_runtype);
85 
86  RunTag my_runtag;
87  my_runtag.setLocationDef( my_locdef );
88  my_runtag.setRunTypeDef( my_rundef );
89  my_runtag.setGeneralTag(m_gentag);
90 
91  readFromFile("last_tpg_lin_settings.txt");
92 
93 
94  unsigned int min_run=m_i_run_number+1;
95 
97  min_run=m_i_run_number+1; // we have to add 1 to the last transferred one
98  } else {
99  min_run=m_firstRun;
100  }
101 
102  if(min_run<max_since) {
103  min_run= max_since+1; // we have to add 1 to the last transferred one
104  }
105 
106  std::cout<<"m_i_run_number"<< m_i_run_number <<"m_firstRun "<<m_firstRun<< "max_since " <<max_since<< std::endl;
107 
108  unsigned int max_run=m_lastRun;
109  edm::LogInfo("EcalTPGLinConstHandler") << "min_run= " << min_run << "max_run = " << max_run;
110 
111  RunList my_list;
112  my_list=econn->fetchGlobalRunListByLocation(my_runtag, min_run, max_run, my_locdef);
113  // my_list=econn->fetchRunListByLocation(my_runtag, min_run, max_run, my_locdef);
114 
115  std::vector<RunIOV> run_vec= my_list.getRuns();
116  size_t num_runs=run_vec.size();
117 
118  std::cout <<"number of runs is : "<< num_runs<< std::endl;
119 
120  unsigned int irun=0;
121  if(num_runs>0){
122 
123  for(size_t kr=0; kr<run_vec.size(); kr++){
124 
125  irun=static_cast<unsigned int>(run_vec[kr].getRunNumber());
126  std::cout<<" **************** "<<std::endl;
127  std::cout<<" **************** "<<std::endl;
128  std::cout<<" run= "<<irun<<std::endl;
129 
130  // retrieve the data :
131  std::map<EcalLogicID, RunTPGConfigDat> dataset;
132  econn->fetchDataSet(&dataset, &run_vec[kr]);
133 
134  std::string the_config_tag="";
135  int the_config_version=0;
136 
137  std::map< EcalLogicID, RunTPGConfigDat>::const_iterator it;
138 
139  int nr=0;
140  for( it=dataset.begin(); it!=dataset.end(); it++ )
141  {
142  ++nr;
143  //EcalLogicID ecalid = it->first;
144 
145  RunTPGConfigDat dat = it->second;
146  the_config_tag=dat.getConfigTag();
147  the_config_version=dat.getVersion();
148  }
149 
150 
151  // it is all the same for all SM... get the last one
152 
153 
154  std::cout<<" run= "<<irun<<" tag "<<the_config_tag<<" version="<<the_config_version <<std::endl;
155 
156  // here we should check if it is the same as previous run.
157 
158 
159  if((the_config_tag != m_i_tag || the_config_version != m_i_version ) && nr>0 ) {
160  std::cout<<"the tag is different from last transferred run ... retrieving last config set from DB"<<std::endl;
161 
162  FEConfigMainInfo fe_main_info;
163  fe_main_info.setConfigTag(the_config_tag);
164  fe_main_info.setVersion(the_config_version);
165 
166  try{
167  std::cout << " before fetch config set" << std::endl;
168  econn-> fetchConfigSet(&fe_main_info);
169  std::cout << " after fetch config set" << std::endl;
170 
171 
172  // now get TPGLinConst
173  int linId=fe_main_info.getLinId();
174 
175  if( linId != m_i_lin ) {
176 
177  FEConfigLinInfo fe_lin_info;
178  fe_lin_info.setId(linId);
179  econn-> fetchConfigSet(&fe_lin_info);
180  std::map<EcalLogicID, FEConfigLinDat> dataset_TpgLin;
181  econn->fetchDataSet(&dataset_TpgLin, &fe_lin_info);
182 
184  typedef std::map<EcalLogicID, FEConfigLinDat>::const_iterator CIfelin;
185  EcalLogicID ecid_xt;
186  FEConfigLinDat rd_lin;
187  int icells=0;
188 
189  for (CIfelin p = dataset_TpgLin.begin(); p != dataset_TpgLin.end(); p++) {
190  ecid_xt = p->first;
191  rd_lin = p->second;
192  std::string ecid_name=ecid_xt.getName();
193 
194  //EB data
195  if (ecid_name=="EB_crystal_number") {
196  int sm_num=ecid_xt.getID1();
197  int xt_num=ecid_xt.getID2();
198  EBDetId ebdetid(sm_num,xt_num,EBDetId::SMCRYSTALMODE);
199 
201  item.mult_x1 =rd_lin.getMultX1() ;
202  item.mult_x6 =rd_lin.getMultX6();
203  item.mult_x12 =rd_lin.getMultX12();
204  item.shift_x1 =rd_lin.getShift1() ;
205  item.shift_x6 =rd_lin.getShift6();
206  item.shift_x12 =rd_lin.getShift12();
207 
208  linC->insert(std::make_pair(ebdetid.rawId(),item));
209  ++icells;
210  }
211  else {
212  //EE data
213  int z=ecid_xt.getID1();
214  int x=ecid_xt.getID2();
215  int y=ecid_xt.getID3();
216  EEDetId eedetid(x,y,z,EEDetId::XYMODE);
217 
219 
220  item.mult_x1 =rd_lin.getMultX1() ;
221  item.mult_x6 =rd_lin.getMultX6();
222  item.mult_x12 =rd_lin.getMultX12();
223  item.shift_x1 =rd_lin.getShift1() ;
224  item.shift_x6 =rd_lin.getShift6();
225  item.shift_x12 =rd_lin.getShift12();
226 
227  linC->insert(std::make_pair(eedetid.rawId(),item));
228  ++icells;
229  }
230  }
231 
232  Time_t snc= (Time_t) irun ;
233  m_to_transfer.push_back(std::make_pair((EcalTPGLinearizationConst*)linC,snc));
234 
235  m_i_run_number=irun;
236  m_i_tag=the_config_tag;
237  m_i_version=the_config_version;
238  m_i_lin=linId;
239 
240  writeFile("last_tpg_lin_settings.txt");
241 
242  } else {
243 
244  m_i_run_number=irun;
245  m_i_tag=the_config_tag;
246  m_i_version=the_config_version;
247 
248  writeFile("last_tpg_lin_settings.txt");
249 
250  std::cout<< " even if the tag/version is not the same, the linearization constants id is the same -> no transfer needed "<< std::endl;
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_lin_settings.txt");
273  }
274 
275  }
276  }
277 
278  delete econn;
279 
280  edm::LogInfo("EcalTPGLinConstHandler") << "Ecal - > end of getNewObjects -----------";
281 }
282 
284  //-------------------------------------------------------------
285 
286  m_i_tag="";
287  m_i_version=0;
288  m_i_run_number=0;
289  m_i_lin=0;
290 
291  FILE *inpFile; // input file
292  inpFile = fopen(inputFile,"r");
293  if(!inpFile) {
294  edm::LogError("EcalTPGLinConstHandler")<<"*** Can not open file: "<<inputFile;
295  return;
296  }
297 
298  char line[256];
299 
300  std::ostringstream str;
301 
302  fgets(line,255,inpFile);
303  m_i_tag=to_string(line);
304  str << "gen tag " << m_i_tag << std::endl ; // should I use this?
305 
306  fgets(line,255,inpFile);
307  m_i_version=atoi(line);
308  str << "version= " << m_i_version << std::endl ;
309 
310  fgets(line,255,inpFile);
311  m_i_run_number=atoi(line);
312  str << "run_number= " << m_i_run_number << std::endl ;
313 
314  fgets(line,255,inpFile);
315  m_i_lin=atoi(line);
316  str << "lin_config= " << m_i_lin << std::endl ;
317 
318 
319  fclose(inpFile); // close inp. file
320 
321 }
322 
324  //-------------------------------------------------------------
325 
326 
327  std::ofstream myfile;
328  myfile.open (inputFile);
329  myfile << m_i_tag <<std::endl;
330  myfile << m_i_version <<std::endl;
331  myfile << m_i_run_number <<std::endl;
332  myfile << m_i_lin <<std::endl;
333 
334  myfile.close();
335 
336 }
size
Write out results.
T getParameter(std::string const &) const
void setRunTypeDef(const RunTypeDef &runTypeDef)
Definition: RunTag.cc:70
std::vector< RunIOV > getRuns()
Definition: RunList.cc:35
RunList fetchGlobalRunListByLocation(const RunTag &tag, int min_run, int max_run, const LocationDef &locDef) noexcept(false)
std::string getConfigTag() const
int getVersion() const
Definition: RunTag.h:13
std::string to_string(char value[])
static const int XYMODE
Definition: EEDetId.h:339
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:47
size_t size
Definition: Types.h:77
int getMultX12() const
void setVersion(int id)
void fetchDataSet(std::map< EcalLogicID, DATT > *fillMap, IOVT *iov) noexcept(false)
std::string name
Definition: Types.h:73
void setId(int id)
int getID2() const
Definition: EcalLogicID.cc:51
cond::ValidityInterval lastInterval
Definition: Types.h:75
int getMultX1() const
std::string getName() const
Definition: EcalLogicID.cc:36
int getShift6() const
int getLinId() const
int getID1() const
Definition: EcalLogicID.cc:46
int getShift12() const
int getMultX6() const
void setLocationDef(const LocationDef &locDef)
Definition: RunTag.cc:53
void insert(std::pair< uint32_t, Item > const &a)
void readFromFile(const char *inputFile)
EcalTPGLinConstHandler(edm::ParameterSet const &)
void setRunType(std::string runtype)
Definition: RunTypeDef.cc:33
void setConfigTag(std::string x)
Definition: IODConfig.h:31
void writeFile(const char *inputFile)
void setLocation(std::string loc)
Definition: LocationDef.cc:33
int getID3() const
Definition: EcalLogicID.cc:56
void setGeneralTag(std::string tag)
Definition: RunTag.cc:36
static const int SMCRYSTALMODE
Definition: EBDetId.h:167
#define str(s)
int getShift1() const
EcalTPGLinearizationConstMap EcalTPGLinearizationConst