CMS 3D CMS Logo

EcalTPGPedestalsHandler.cc
Go to the documentation of this file.
9 
11 
12 #include<iostream>
13 #include<fstream>
14 
15 
16 #include <ctime>
17 #include <unistd.h>
18 
19 #include <string>
20 #include <cstdio>
21 #include <typeinfo>
22 #include <sstream>
23 
24 
25 
27  : m_name(ps.getUntrackedParameter<std::string>("name","EcalTPGPedestalsHandler")) {
28 
29  edm::LogInfo("EcalTPGPedestalsHandler") << "EcalTPGPedestals Source handler constructor";
30  m_firstRun=static_cast<unsigned int>(atoi( ps.getParameter<std::string>("firstRun").c_str()));
31  m_lastRun=static_cast<unsigned int>(atoi( ps.getParameter<std::string>("lastRun").c_str()));
32  m_sid= ps.getParameter<std::string>("OnlineDBSID");
33  m_user= ps.getParameter<std::string>("OnlineDBUser");
34  m_pass= ps.getParameter<std::string>("OnlineDBPassword");
35  m_locationsource= ps.getParameter<std::string>("LocationSource");
36  m_location=ps.getParameter<std::string>("Location");
37  m_gentag=ps.getParameter<std::string>("GenTag");
38  m_runtype=ps.getParameter<std::string>("RunType");
39 
40  edm::LogInfo("EcalTPGPedestalsHandler")<< m_sid<<"/"<<m_user<<"/"<<m_location<<"/"<<m_gentag;
41 
42 }
43 
45 {
46 }
47 
48 
50 {
51 
52  edm::LogInfo("EcalTPGPedestalsHandler") << "Started getNewObjects";
53 
54  //check whats already inside of database
55  if (tagInfo().size){
56  //check whats already inside of database
57  std::cout << "got offlineInfo = " << std::endl;
58  std::cout << "tag name = " << tagInfo().name << std::endl;
59  std::cout << "size = " << tagInfo().size << std::endl;
60  } else {
61  std::cout << " First object for this tag " << std::endl;
62  }
63 
64  unsigned int max_since =0;
65  max_since=static_cast<unsigned int>(tagInfo().lastInterval.first);
66  edm::LogInfo("EcalTPGPedestalsHandler") << "max_since = " << max_since;
67  edm::LogInfo("EcalTPGPedestalsHandler")<< "Retrieved last payload ";
68 
69  // here we retrieve all the runs after the last from online DB
70  edm::LogInfo("EcalTPGPedestalsHandler")<< "Retrieving run list from ONLINE DB ... " << std::endl;
71 
72  edm::LogInfo("EcalTPGPedestalsHandler") << "Making connection..." << std::flush;
74  edm::LogInfo("EcalTPGPedestalsHandler") << "Done." << std::endl;
75 
76  if (!econn)
77  {
78  std::cout << " Connection parameters " <<m_sid <<"/"<<m_user<<std::endl;
79  throw cms::Exception("OMDS not available");
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 
94  readFromFile("last_tpg_ped_settings.txt");
95 
96 
97  unsigned int min_run=m_i_run_number+1;
98 
100  min_run=m_i_run_number+1;
101  } else {
102  min_run=m_firstRun;
103  }
104  if(min_run<max_since) {
105  min_run= max_since+1; // we have to add 1 to the last transferred one
106  }
107 
108  std::cout<<"m_i_run_number"<< m_i_run_number <<"m_firstRun "<<m_firstRun<< "max_since " <<max_since<< std::endl;
109 
110  unsigned int max_run=m_lastRun;
111  edm::LogInfo("EcalTPGPedestalsHandler") <<"min_run= " << min_run << " max_run = " << max_run;
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 
116  std::vector<RunIOV> run_vec= my_list.getRuns();
117  size_t num_runs=run_vec.size();
118 
119  std::cout <<"number of runs is : "<< num_runs<< std::endl;
120 
121  unsigned int irun=0;
122 
123  if(num_runs>0){
124 
125  for(size_t kr=0; kr<run_vec.size(); kr++){
126  irun=static_cast<unsigned int>(run_vec[kr].getRunNumber());
127 
128  std::cout<<" **************** "<<std::endl;
129  std::cout<<" **************** "<<std::endl;
130  std::cout<<" run= "<<irun<<std::endl;
131 
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 
148  RunTPGConfigDat dat = it->second;
149  the_config_tag=dat.getConfigTag();
150  the_config_version=dat.getVersion();
151  }
152 
153  // it is all the same for all SM... get the last one
154 
155 
156  std::cout<<" run= "<<irun<<" tag "<<the_config_tag<<" version="<<the_config_version <<std::endl;
157 
158  // here we should check if it is the same as previous run.
159 
160 
161  if((the_config_tag != m_i_tag || the_config_version != m_i_version ) && nr>0 ) {
162  std::cout<<"the tag is different from last transferred run ... retrieving last config set from DB"<<std::endl;
163 
164  FEConfigMainInfo fe_main_info;
165  fe_main_info.setConfigTag(the_config_tag);
166  fe_main_info.setVersion(the_config_version);
167 
168  try{
169  std::cout << " before fetch config set" << std::endl;
170  econn-> fetchConfigSet(&fe_main_info);
171  std::cout << " after fetch config set" << std::endl;
172 
173  // now get TPGPedestals
174  int pedId=fe_main_info.getPedId();
175 
176  if( pedId != m_i_ped ) {
177 
178  FEConfigPedInfo fe_ped_info;
179  fe_ped_info.setId(pedId);
180  econn-> fetchConfigSet(&fe_ped_info);
181  std::map<EcalLogicID, FEConfigPedDat> dataset_TpgPed;
182  econn->fetchDataSet(&dataset_TpgPed, &fe_ped_info);
183 
184  // NB new
186  typedef std::map<EcalLogicID, FEConfigPedDat>::const_iterator CIfeped;
187  EcalLogicID ecid_xt;
188  FEConfigPedDat rd_ped;
189  int icells=0;
190  for (CIfeped p = dataset_TpgPed.begin(); p != dataset_TpgPed.end(); p++)
191  {
192  ecid_xt = p->first;
193  rd_ped = p->second;
194 
195  std::string ecid_name=ecid_xt.getName();
196 
197  // EB data
198  if (ecid_name=="EB_crystal_number") {
199  if(icells<10) std::cout << " copy the EB data " << " icells = " << icells << std::endl;
200  int sm_num=ecid_xt.getID1();
201  int xt_num=ecid_xt.getID2();
202 
203  EBDetId ebdetid(sm_num,xt_num,EBDetId::SMCRYSTALMODE);
205  item.mean_x1 =(unsigned int)rd_ped.getPedMeanG1() ;
206  item.mean_x6 =(unsigned int)rd_ped.getPedMeanG6();
207  item.mean_x12 =(unsigned int)rd_ped.getPedMeanG12();
208 
209  peds->insert(std::make_pair(ebdetid.rawId(),item));
210  ++icells;
211  }else if (ecid_name=="EE_crystal_number"){
212 
213  // EE data
214  int z=ecid_xt.getID1();
215  int x=ecid_xt.getID2();
216  int y=ecid_xt.getID3();
217  EEDetId eedetid(x,y,z,EEDetId::XYMODE);
219  item.mean_x1 =(unsigned int)rd_ped.getPedMeanG1();
220  item.mean_x6 =(unsigned int)rd_ped.getPedMeanG6();
221  item.mean_x12 =(unsigned int)rd_ped.getPedMeanG12();
222 
223  peds->insert(std::make_pair(eedetid.rawId(),item));
224  ++icells;
225  }
226  }
227 
228 
229  Time_t snc= (Time_t) irun;
230  m_to_transfer.push_back(std::make_pair((EcalTPGPedestals*)peds,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_ped=pedId;
236 
237  writeFile("last_tpg_ped_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_ped_settings.txt");
246 
247  std::cout<< " even if the tag/version is not the same, the pedestals 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_ped_settings.txt");
273  }
274 
275  }
276  }
277 
278 
279  delete econn;
280 
281  edm::LogInfo("EcalTPGPedestalsHandler") << "Ecal - > end of getNewObjects -----------";
282 
283 }
284 
285 
286 
288  //-------------------------------------------------------------
289 
290  m_i_tag="";
291  m_i_version=0;
292  m_i_run_number=0;
293  m_i_ped=0;
294 
295  FILE *inpFile; // input file
296  inpFile = fopen(inputFile,"r");
297  if(!inpFile) {
298  edm::LogError("EcalTPGPedestalsHandler")<<"*** Can not open file: "<<inputFile;
299  return;
300  }
301 
302  char line[256];
303 
304  std::ostringstream str;
305 
306  fgets(line,255,inpFile);
307  m_i_tag=to_string(line);
308  str << "gen tag " << m_i_tag << std::endl ; // should I use this?
309 
310  fgets(line,255,inpFile);
311  m_i_version=atoi(line);
312  str << "version= " << m_i_version << std::endl ;
313 
314  fgets(line,255,inpFile);
315  m_i_run_number=atoi(line);
316  str << "run_number= " << m_i_run_number << std::endl ;
317 
318  fgets(line,255,inpFile);
319  m_i_ped=atoi(line);
320  str << "ped_config= " << m_i_ped << std::endl ;
321 
322 
323  fclose(inpFile); // close inp. file
324 
325 }
326 
328  //-------------------------------------------------------------
329 
330 
331  std::ofstream myfile;
332  myfile.open (inputFile);
333  myfile << m_i_tag <<std::endl;
334  myfile << m_i_version <<std::endl;
335  myfile << m_i_run_number <<std::endl;
336  myfile << m_i_ped <<std::endl;
337 
338  myfile.close();
339 
340 }
341 
342 
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
float getPedMeanG1() const
int getVersion() const
Definition: RunTag.h:13
static const int XYMODE
Definition: EEDetId.h:335
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:50
size_t size
Definition: Types.h:78
void setVersion(int id)
void fetchDataSet(std::map< EcalLogicID, DATT > *fillMap, IOVT *iov) noexcept(false)
std::string name
Definition: Types.h:74
int getID2() const
Definition: EcalLogicID.cc:51
void writeFile(const char *inputFile)
cond::ValidityInterval lastInterval
Definition: Types.h:76
std::string getName() const
Definition: EcalLogicID.cc:36
int getID1() const
Definition: EcalLogicID.cc:46
float getPedMeanG12() const
void setLocationDef(const LocationDef &locDef)
Definition: RunTag.cc:53
void insert(std::pair< uint32_t, Item > const &a)
std::string to_string(char value[])
void setRunType(std::string runtype)
Definition: RunTypeDef.cc:33
void readFromFile(const char *inputFile)
float getPedMeanG6() const
int getPedId() const
void setConfigTag(std::string x)
Definition: IODConfig.h:31
EcalTPGPedestalsHandler(edm::ParameterSet const &)
void setId(int id)
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:159
#define str(s)
EcalTPGPedestalsMap EcalTPGPedestals