CMS 3D CMS Logo

StoreESCondition.cc
Go to the documentation of this file.
2 
8 
9 #include <fstream>
10 #include <string>
11 #include <cstring>
12 #include <ctime>
13 #include <unistd.h>
14 
15 using std::string;
16 
18  prog_name_ = "StoreESCondition";
19 
20  logfile_ = iConfig.getParameter<std::string>("logfile");
21 
22  esgain_ = iConfig.getParameter<unsigned int>("gain");
23 
24  typedef std::vector<edm::ParameterSet> Parameters;
25  Parameters toPut = iConfig.getParameter<Parameters>("toPut");
26  for (Parameters::iterator itToPut = toPut.begin(); itToPut != toPut.end(); ++itToPut) {
27  inpFileName_.push_back(itToPut->getUntrackedParameter<std::string>("inputFile"));
28  objectName_.push_back(itToPut->getUntrackedParameter<std::string>("conditionType"));
29  since_.push_back(itToPut->getUntrackedParameter<unsigned int>("since"));
30  }
31 }
32 
35  if (!mydbservice.isAvailable()) {
36  edm::LogError("StoreESCondition") << "PoolDBOutputService is unavailable"
37  << "\n";
38  return;
39  }
40 
41  bool toAppend = false;
42  // copy a string to the char *
43  std::string message = "finished OK\n";
44  size_t messageSize = message.size() + 1;
45  char* messChar = new char[messageSize];
46  strncpy(messChar, message.c_str(), messageSize);
47 
48  for (unsigned int i = 0; i < objectName_.size(); i++) {
49  cond::Time_t newTime;
50 
51  if (mydbservice->isNewTagRequest(objectName_[i] + std::string("Rcd"))) {
52  // This is the first object for this tag.
53  // Append mode should be off.
54  // newTime is the end of this new objects IOV.
55  newTime = mydbservice->beginOfTime();
56  } else {
57  // There should already be an object in the DB for this tag.
58  // Append IOV mode should be on.
59  // newTime is the beginning of this new objects IOV.
60  toAppend = true;
61  newTime = (cond::Time_t)since_[i];
62  }
63  edm::LogInfo("StoreESCondition") << "Reading " << objectName_[i] << " from file and writing to DB with newTime "
64  << newTime << "\n";
65  if (objectName_[i] == "ESChannelStatus") {
66  edm::LogInfo("StoreESCondition") << " ESChannelStatus file " << inpFileName_[i] << "\n";
67  const auto mycali = readESChannelStatusFromFile(inpFileName_[i].c_str());
68  edm::LogInfo("StoreESCondition") << " ESChannelStatus file read "
69  << "\n";
70  if (!toAppend) {
71  edm::LogInfo("StoreESCondition") << " before create "
72  << "\n";
73  mydbservice->createOneIOV<ESChannelStatus>(*mycali, newTime, "ESChannelStatusRcd");
74  edm::LogInfo("StoreESCondition") << " after create "
75  << "\n";
76  } else {
77  edm::LogInfo("StoreESCondition") << " before append "
78  << "\n";
79  mydbservice->appendOneIOV<ESChannelStatus>(*mycali, newTime, "ESChannelStatusRcd");
80  edm::LogInfo("StoreESCondition") << " after append "
81  << "\n";
82  }
83  } else if (objectName_[i] == "ESIntercalibConstants") {
84  const auto myintercalib = readESIntercalibConstantsFromFile(inpFileName_[i].c_str());
85  if (!toAppend) {
86  mydbservice->createOneIOV<ESIntercalibConstants>(*myintercalib, newTime, "ESIntercalibConstantsRcd");
87  } else {
88  mydbservice->appendOneIOV<ESIntercalibConstants>(*myintercalib, newTime, "ESIntercalibConstantsRcd");
89  }
90  } else if (objectName_[i] == "ESTimeSampleWeights") {
91  const auto myintercalib = readESTimeSampleWeightsFromFile(inpFileName_[i].c_str());
92  if (!toAppend) {
93  mydbservice->createOneIOV<ESTimeSampleWeights>(*myintercalib, newTime, "ESTimeSampleWeightsRcd");
94  } else {
95  mydbservice->appendOneIOV<ESTimeSampleWeights>(*myintercalib, newTime, "ESTimeSampleWeightsRcd");
96  }
97  } else if (objectName_[i] == "ESGain") {
98  const auto myintercalib = readESGainFromFile(inpFileName_[i].c_str());
99  if (!toAppend) {
100  mydbservice->createOneIOV<ESGain>(*myintercalib, newTime, "ESGainRcd");
101  } else {
102  mydbservice->appendOneIOV<ESGain>(*myintercalib, newTime, "ESGainRcd");
103  }
104  } else if (objectName_[i] == "ESMissingEnergyCalibration") {
105  const auto myintercalib = readESMissingEnergyFromFile(inpFileName_[i].c_str());
106  if (!toAppend) {
107  mydbservice->createOneIOV<ESMissingEnergyCalibration>(*myintercalib, newTime, "ESMissingEnergyCalibrationRcd");
108  } else {
109  mydbservice->appendOneIOV<ESMissingEnergyCalibration>(*myintercalib, newTime, "ESMissingEnergyCalibrationRcd");
110  }
111  } else if (objectName_[i] == "ESRecHitRatioCuts") {
112  const auto myintercalib = readESRecHitRatioCutsFromFile(inpFileName_[i].c_str());
113  if (!toAppend) {
114  mydbservice->createOneIOV<ESRecHitRatioCuts>(*myintercalib, newTime, "ESRecHitRatioCutsRcd");
115  } else {
116  mydbservice->appendOneIOV<ESRecHitRatioCuts>(*myintercalib, newTime, "ESRecHitRatioCutsRcd");
117  }
118  } else if (objectName_[i] == "ESThresholds") {
119  const auto myintercalib = readESThresholdsFromFile(inpFileName_[i].c_str());
120  if (!toAppend) {
121  mydbservice->createOneIOV<ESThresholds>(*myintercalib, newTime, "ESThresholdsRcd");
122  } else {
123  mydbservice->appendOneIOV<ESThresholds>(*myintercalib, newTime, "ESThresholdsRcd");
124  }
125  } else if (objectName_[i] == "ESPedestals") {
126  const auto myintercalib = readESPedestalsFromFile(inpFileName_[i].c_str());
127  if (!toAppend) {
128  mydbservice->createOneIOV<ESPedestals>(*myintercalib, newTime, "ESPedestalsRcd");
129  } else {
130  mydbservice->appendOneIOV<ESPedestals>(*myintercalib, newTime, "ESPedestalsRcd");
131  }
132  } else if (objectName_[i] == "ESEEIntercalibConstants") {
133  const auto myintercalib = readESEEIntercalibConstantsFromFile(inpFileName_[i].c_str());
134  if (!toAppend) {
135  mydbservice->createOneIOV<ESEEIntercalibConstants>(*myintercalib, newTime, "ESEEIntercalibConstantsRcd");
136  } else {
137  mydbservice->appendOneIOV<ESEEIntercalibConstants>(*myintercalib, newTime, "ESEEIntercalibConstantsRcd");
138  }
139  } else {
140  edm::LogError("StoreESCondition") << "Object " << objectName_[i] << " is not supported by this program.";
141  }
142  // if more records write here else if ....
143 
144  writeToLogFileResults(messChar);
145 
146  edm::LogInfo("StoreESCondition") << "Finished endJob";
147  }
148 
149  delete[] messChar;
150 }
151 
153 
154 void StoreESCondition::analyze(const edm::Event& evt, const edm::EventSetup& evtSetup) {}
155 
156 void StoreESCondition::writeToLogFile(string a, string b, unsigned long long since) {
157  FILE* outFile; // output log file for appending
158  outFile = fopen(logfile_.c_str(), "a");
159  if (!outFile) {
160  edm::LogError("StoreESCondition") << "*** Can not open file: " << logfile_;
161  return;
162  }
163  char header[256];
165  char appendMode[10];
166  if (since != 0)
167  strcpy(appendMode, "append");
168  else
169  strcpy(appendMode, "create");
170 
171  //fprintf(outFile, "%s %s condition from file %s written into DB for SM %d (mapped to SM %d) in %s mode (since run %u)\n",
172  //header, a.c_str(), b.c_str(), appendMode, (unsigned int)since);
173 
174  fclose(outFile); // close out file
175 }
176 
178  FILE* outFile; // output log file for appending
179  outFile = fopen(logfile_.c_str(), "a");
180  if (!outFile) {
181  edm::LogError("StoreESCondition") << "*** Can not open file: " << logfile_;
182  return;
183  }
184  char header[256];
186  fprintf(outFile, "%s %s\n", header, arg);
187  fclose(outFile); // close out file
188 }
189 
191  time_t rawtime;
192  struct tm* timeinfo;
193  time(&rawtime);
194  timeinfo = localtime(&rawtime);
195  char user[50];
196  strcpy(user, getlogin());
197  strcpy(header, asctime(timeinfo));
198  strcpy(header, user);
199 }
200 
201 std::shared_ptr<ESThresholds> StoreESCondition::readESThresholdsFromFile(const char* inputFile) {
202  std::ifstream ESThresholdsFile(edm::FileInPath(inputFile).fullPath().c_str());
203  float ts2, zs; //2nd time sample, ZS threshold
204  ESThresholdsFile >> ts2;
205  ESThresholdsFile >> zs;
206  auto esThresholds = std::make_shared<ESThresholds>(ts2, zs);
207 
208  return esThresholds;
209 }
210 
211 std::shared_ptr<ESEEIntercalibConstants> StoreESCondition::readESEEIntercalibConstantsFromFile(const char* inputFile) {
212  std::ifstream ESEEIntercalibFile(edm::FileInPath(inputFile).fullPath().c_str());
213  float gammaLow0, alphaLow0, gammaHigh0, alphaHigh0, gammaLow1, alphaLow1, gammaHigh1, alphaHigh1, gammaLow2,
214  alphaLow2, gammaHigh2, alphaHigh2, gammaLow3, alphaLow3, gammaHigh3, alphaHigh3;
215  ESEEIntercalibFile >> gammaLow0;
216  ESEEIntercalibFile >> alphaLow0;
217  ESEEIntercalibFile >> gammaHigh0;
218  ESEEIntercalibFile >> alphaHigh0;
219  ESEEIntercalibFile >> gammaLow1;
220  ESEEIntercalibFile >> alphaLow1;
221  ESEEIntercalibFile >> gammaHigh1;
222  ESEEIntercalibFile >> alphaHigh1;
223  ESEEIntercalibFile >> gammaLow2;
224  ESEEIntercalibFile >> alphaLow2;
225  ESEEIntercalibFile >> gammaHigh2;
226  ESEEIntercalibFile >> alphaHigh2;
227  ESEEIntercalibFile >> gammaLow3;
228  ESEEIntercalibFile >> alphaLow3;
229  ESEEIntercalibFile >> gammaHigh3;
230  ESEEIntercalibFile >> alphaHigh3;
231  auto eseeIntercalibConstants = std::make_shared<ESEEIntercalibConstants>(gammaLow0,
232  alphaLow0,
233  gammaHigh0,
234  alphaHigh0,
235  gammaLow1,
236  alphaLow1,
237  gammaHigh1,
238  alphaHigh1,
239  gammaLow2,
240  alphaLow2,
241  gammaHigh2,
242  alphaHigh2,
243  gammaLow3,
244  alphaLow3,
245  gammaHigh3,
246  alphaHigh3);
247 
248  return eseeIntercalibConstants;
249 }
250 
251 std::shared_ptr<ESMissingEnergyCalibration> StoreESCondition::readESMissingEnergyFromFile(const char* inputFile) {
252  std::ifstream ESMissingEnergyFile(edm::FileInPath(inputFile).fullPath().c_str());
253  float ConstAEta0, ConstBEta0, ConstAEta1, ConstBEta1, ConstAEta2, ConstBEta2, ConstAEta3, ConstBEta3;
254  ESMissingEnergyFile >> ConstAEta0;
255  ESMissingEnergyFile >> ConstBEta0;
256  ESMissingEnergyFile >> ConstAEta1;
257  ESMissingEnergyFile >> ConstBEta1;
258  ESMissingEnergyFile >> ConstAEta2;
259  ESMissingEnergyFile >> ConstBEta2;
260  ESMissingEnergyFile >> ConstAEta3;
261  ESMissingEnergyFile >> ConstBEta3;
262  auto esMissingEnergy = std::make_shared<ESMissingEnergyCalibration>(
263  ConstAEta0, ConstBEta0, ConstAEta1, ConstBEta1, ConstAEta2, ConstBEta2, ConstAEta3, ConstBEta3);
264 
265  return esMissingEnergy;
266 }
267 
268 std::shared_ptr<ESPedestals> StoreESCondition::readESPedestalsFromFile(const char* inputFile) {
269  auto esPedestals = std::make_shared<ESPedestals>();
270 
271  // int ped[2][2][40][40][32];
272  // for (int i=0; i<2; ++i)
273  // for (int j=0; j<2; ++j)
274  // for (int k=0; k<40; ++k)
275  // for (int m=0; m<40; ++m)
276  // for (int n=0; n<32; ++n)
277  // ped[i][j][k][m][n] = 0;
278 
280 
281  int iz, ip, ix, iy, is, ped_, zside;
282  std::ifstream pedestalFile(edm::FileInPath(inputFile).fullPath().c_str());
283 
284  for (int i = 0; i < 137216; ++i) {
285  pedestalFile >> iz >> ip >> ix >> iy >> is >> ped_;
286 
287  zside = (iz == -1) ? 1 : 0;
288  ped[zside][ip - 1][ix - 1][iy - 1][is - 1] = ped_;
289  }
290 
291  for (int iz = -1; iz <= 1; ++iz) {
292  if (iz == 0)
293  continue;
294  zside = (iz == -1) ? 1 : 0;
295 
296  for (int iplane = ESDetId::PLANE_MIN; iplane <= ESDetId::PLANE_MAX; ++iplane)
297  for (int ix = ESDetId::IX_MIN; ix <= ESDetId::IX_MAX; ++ix)
298  for (int iy = ESDetId::IY_MIN; iy <= ESDetId::IY_MAX; ++iy)
299  for (int istrip = ESDetId::ISTRIP_MIN; istrip <= ESDetId::ISTRIP_MAX; ++istrip) {
300  ESPedestals::Item ESitem;
301  ESitem.mean = ped[zside][iplane - 1][ix - 1][iy - 1][istrip - 1];
302  ESitem.rms = 3; // LG : 3, HG : 6
303 
304  if (ESDetId::validDetId(istrip, ix, iy, iplane, iz)) {
305  ESDetId esId(istrip, ix, iy, iplane, iz);
306  esPedestals->insert(std::make_pair(esId.rawId(), ESitem));
307  }
308  }
309  }
310 
311  return esPedestals;
312 }
313 
314 std::shared_ptr<ESRecHitRatioCuts> StoreESCondition::readESRecHitRatioCutsFromFile(const char* inputFile) {
315  std::ifstream ESRecHitRatioCutsFile(edm::FileInPath(inputFile).fullPath().c_str());
316 
317  float r12Low, r23Low, r12High, r23High;
318  ESRecHitRatioCutsFile >> r12Low;
319  ESRecHitRatioCutsFile >> r23Low;
320  ESRecHitRatioCutsFile >> r12High;
321  ESRecHitRatioCutsFile >> r23High;
322  auto esRecHitRatioCuts = std::make_shared<ESRecHitRatioCuts>(r12Low, r23Low, r12High, r23High);
323 
324  return esRecHitRatioCuts;
325 }
326 
327 std::shared_ptr<ESGain> StoreESCondition::readESGainFromFile(const char* inputFile) {
328  std::ifstream amplFile(edm::FileInPath(inputFile).fullPath().c_str());
329 
330  int gain;
331  amplFile >> gain;
332  edm::LogInfo("StoreESCondition") << "gain : " << gain << "\n";
333 
334  auto esGain = std::make_shared<ESGain>(gain); // 1: LG, 2: HG
335  return esGain;
336 }
337 
338 std::shared_ptr<ESTimeSampleWeights> StoreESCondition::readESTimeSampleWeightsFromFile(const char* inputFile) {
339  std::ifstream amplFile(edm::FileInPath(inputFile).fullPath().c_str());
340 
341  float w[3];
342  for (int k = 0; k < 3; ++k) {
343  float ww;
344  amplFile >> ww;
345  w[k] = ww;
346  edm::LogInfo("StoreESCondition") << "weight : " << k << " " << w[k] << "\n";
347  }
348 
349  auto esWeights = std::make_shared<ESTimeSampleWeights>(w[0], w[1], w[2]);
350  return esWeights;
351 }
352 
353 std::shared_ptr<ESIntercalibConstants> StoreESCondition::readESIntercalibConstantsFromFile(const char* inputFile) {
354  auto ical = std::make_shared<ESIntercalibConstants>();
355 
356  std::ifstream mipFile(edm::FileInPath(inputFile).fullPath().c_str());
357 
358  for (int i = 0; i < 137216; ++i) {
359  int iz, ip, ix, iy, is;
360  double mip;
361  mipFile >> iz >> ip >> ix >> iy >> is >> mip;
362  //if (mip <20 || mip> 70) cout<<iz<<" "<<ip<<" "<<ix<<" "<<iy<<" "<<is<<" "<<mip<<endl; // HG
363  // LG : HG MIP/6/1.14
364  //mip = mip/6/1.14;
365  // LG : HG MIP/6
366  if (esgain_ == 1)
367  mip = mip / 6.; // LG
368  if (mip < 20 || mip > 70)
369  edm::LogInfo("StoreESCondition") << iz << " " << ip << " " << ix << " " << iy << " " << is << " " << mip
370  << "\n"; // LG
371 
372  if (ESDetId::validDetId(is, ix, iy, ip, iz)) {
373  ESDetId esId(is, ix, iy, ip, iz);
374  ical->setValue(esId.rawId(), mip);
375  }
376  }
377 
378  return ical;
379 }
380 
381 std::shared_ptr<ESChannelStatus> StoreESCondition::readESChannelStatusFromFile(const char* inputFile) {
382  int z[1000], p[1000], x[1000], y[1000], nsensors;
383  std::ifstream statusFile(edm::FileInPath(inputFile).fullPath().c_str());
384  statusFile >> nsensors;
385  edm::LogInfo("StoreESCondition") << " nsensors " << nsensors << "\n";
386  if (nsensors >= 1000) {
387  edm::LogInfo("StoreESCondition") << " *** value too high, modify the method!***"
388  << "\n";
389  exit(-1);
390  }
391  for (int i = 0; i < nsensors; ++i) {
392  statusFile >> z[i] >> p[i] >> x[i] >> y[i];
393  }
394  auto ecalStatus = std::make_shared<ESChannelStatus>();
395  int Nbstatus = 0, Nbstrip = 0;
396  for (int istrip = ESDetId::ISTRIP_MIN; istrip <= ESDetId::ISTRIP_MAX; istrip++) {
397  for (int ix = ESDetId::IX_MIN; ix <= ESDetId::IX_MAX; ix++) {
398  for (int iy = ESDetId::IY_MIN; iy <= ESDetId::IY_MAX; iy++) {
399  for (int iplane = 1; iplane <= 2; iplane++) {
400  for (int izeta = -1; izeta <= 1; izeta = izeta + 2) {
401  // if (izeta==0) continue;
402  // try {
403 
404  //ESDetId Plane iplane Zside izeta
405  // if(!ESDetId::validDetId(istrip,ix,iy,iplane,izeta)) cout << " Unvalid DetId" << endl;
406  // else {
407  if (ESDetId::validDetId(istrip, ix, iy, iplane, izeta)) {
408  ESDetId anESId(istrip, ix, iy, iplane, izeta);
409  int status = 0;
410  // std::ifstream statusFile(edm::FileInPath(inputFile).fullPath().c_str());
411  Nbstrip++;
412  for (int i = 0; i < nsensors; ++i) {
413  if (izeta == z[i] && iplane == p[i] && ix == x[i] && iy == y[i])
414  status = 1;
415  }
416  if (status == 1) {
417  Nbstatus++;
418  if (istrip == 1)
419  edm::LogInfo("StoreESCondition") << " Bad channel ix " << ix << " iy " << iy << " iplane " << iplane
420  << " iz " << izeta << "\n"; // print only once
421  }
422  ecalStatus->setValue(anESId, status);
423  // statusFile.close();
424  } // valid DetId
425  // catch ( cms::Exception &e ) { }
426  } // loop over z
427  } // loop over plane
428  } // loop over y
429  } // loop over x
430  } // loop over strips
431  edm::LogInfo("StoreESCondition") << " Nb of strips " << Nbstrip << " Number of bad channels " << Nbstatus << "\n";
432  statusFile.close();
433 
434  // overwrite the statuses which are in the file
435  return ecalStatus;
436 }
std::shared_ptr< ESTimeSampleWeights > readESTimeSampleWeightsFromFile(const char *)
std::shared_ptr< ESGain > readESGainFromFile(const char *)
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
std::shared_ptr< ESThresholds > readESThresholdsFromFile(const char *)
T w() const
Definition: ESGain.h:7
static const int IY_MAX
Definition: ESDetId.h:67
std::vector< unsigned long long > since_
int zside(DetId const &)
static const int ISTRIP_MAX
Definition: ESDetId.h:69
Log< level::Error, false > LogError
A arg
Definition: Factorize.h:31
std::shared_ptr< ESMissingEnergyCalibration > readESMissingEnergyFromFile(const char *)
void createOneIOV(const T &payload, cond::Time_t firstSinceTime, const std::string &recordName)
static const int PLANE_MIN
Definition: ESDetId.h:70
std::string logfile_
void appendOneIOV(const T &payload, cond::Time_t sinceTime, const std::string &recordName)
std::vector< std::string > inpFileName_
unsigned long long Time_t
Definition: Time.h:14
static const int PLANE_MAX
Definition: ESDetId.h:71
bool isNewTagRequest(const std::string &recordName)
std::shared_ptr< ESRecHitRatioCuts > readESRecHitRatioCutsFromFile(const char *)
std::shared_ptr< ESPedestals > readESPedestalsFromFile(const char *)
std::shared_ptr< ESIntercalibConstants > readESIntercalibConstantsFromFile(const char *)
float mean
Definition: ESPedestals.h:16
void writeToLogFileResults(char *)
void writeToLogFile(std::string, std::string, unsigned long long)
std::shared_ptr< ESEEIntercalibConstants > readESEEIntercalibConstantsFromFile(const char *)
Log< level::Info, false > LogInfo
std::vector< std::string > objectName_
static const int IY_MIN
Definition: ESDetId.h:65
static const int IZ_NUM
Definition: ESDetId.h:72
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:57
~StoreESCondition() override
static bool validDetId(int istrip, int ixs, int iys, int iplane, int iz)
check if a valid index combination
Definition: ESDetId.cc:15
double b
Definition: hdecay.h:118
unsigned int esgain_
std::vector< AlignmentParameters * > Parameters
Definition: Utilities.h:32
static const int IX_MAX
Definition: ESDetId.h:66
float rms
Definition: ESPedestals.h:17
double a
Definition: hdecay.h:119
std::shared_ptr< ESChannelStatus > readESChannelStatusFromFile(const char *)
void fillHeader(char *)
bool isAvailable() const
Definition: Service.h:40
std::string prog_name_
void analyze(const edm::Event &evt, const edm::EventSetup &evtSetup) override
StoreESCondition(const edm::ParameterSet &iConfig)
void endJob() override
static const int ISTRIP_MIN
Definition: ESDetId.h:68
def exit(msg="")
static const int IX_MIN
Definition: ESDetId.h:64