CMS 3D CMS Logo

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