File indexing completed on 2024-04-06 12:02:52
0001 #include "CondTools/Ecal/plugins/StoreEcalCondition.h"
0002
0003 #include "FWCore/ServiceRegistry/interface/Service.h"
0004 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0005 #include "FWCore/Framework/interface/Event.h"
0006 #include "CondCore/CondDB/interface/Time.h"
0007 #include "CondCore/DBOutputService/interface/PoolDBOutputService.h"
0008 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0009
0010 #include <fstream>
0011 #include <string>
0012 #include <cstring>
0013 #include <ctime>
0014 #include <unistd.h>
0015
0016 using std::string;
0017
0018 StoreEcalCondition::StoreEcalCondition(const edm::ParameterSet& iConfig) {
0019 prog_name_ = "StoreEcalCondition";
0020
0021 logfile_ = iConfig.getParameter<std::string>("logfile");
0022 sm_slot_ = iConfig.getUntrackedParameter<unsigned int>("smSlot", 1);
0023
0024 typedef std::vector<edm::ParameterSet> Parameters;
0025 Parameters toPut = iConfig.getParameter<Parameters>("toPut");
0026 for (Parameters::iterator itToPut = toPut.begin(); itToPut != toPut.end(); ++itToPut) {
0027 inpFileName_.push_back(itToPut->getUntrackedParameter<std::string>("inputFile"));
0028 inpFileNameEE_.push_back(itToPut->getUntrackedParameter<std::string>("inputFileEE"));
0029 objectName_.push_back(itToPut->getUntrackedParameter<std::string>("conditionType"));
0030 since_.push_back(itToPut->getUntrackedParameter<unsigned int>("since"));
0031 }
0032
0033 sm_constr_ = -1;
0034 }
0035
0036 void StoreEcalCondition::endJob() {
0037 edm::Service<cond::service::PoolDBOutputService> mydbservice;
0038 if (!mydbservice.isAvailable()) {
0039 edm::LogError("StoreEcalCondition") << "PoolDBOutputService is unavailable";
0040 return;
0041 }
0042
0043 bool toAppend = false;
0044
0045 std::string message = "finished OK\n";
0046 size_t messageSize = message.size() + 1;
0047 char* messChar = new char[messageSize];
0048 strncpy(messChar, message.c_str(), messageSize);
0049
0050 for (unsigned int i = 0; i < objectName_.size(); i++) {
0051 cond::Time_t newTime;
0052
0053 if (mydbservice->isNewTagRequest(objectName_[i] + std::string("Rcd"))) {
0054
0055
0056
0057 newTime = mydbservice->beginOfTime();
0058 } else {
0059
0060
0061
0062 toAppend = true;
0063 newTime = (cond::Time_t)since_[i];
0064 }
0065 edm::LogInfo("StoreEcalCondition") << "Reading " << objectName_[i] << " from file and writing to DB with newTime "
0066 << newTime;
0067
0068 if (objectName_[i] == "EcalWeightXtalGroups") {
0069 const auto mycali = readEcalWeightXtalGroupsFromFile(inpFileName_[i].c_str());
0070 if (!toAppend) {
0071 mydbservice->createOneIOV<EcalWeightXtalGroups>(*mycali, newTime, "EcalWeightXtalGroupsRcd");
0072 } else {
0073 mydbservice->appendOneIOV<EcalWeightXtalGroups>(*mycali, newTime, "EcalWeightXtalGroupsRcd");
0074 }
0075 } else if (objectName_[i] == "EcalTBWeights") {
0076 const auto mycali = readEcalTBWeightsFromFile(inpFileName_[i].c_str());
0077 if (!toAppend) {
0078 mydbservice->createOneIOV<EcalTBWeights>(*mycali, newTime, "EcalTBWeightsRcd");
0079 } else {
0080 mydbservice->appendOneIOV<EcalTBWeights>(*mycali, newTime, "EcalTBWeightsRcd");
0081 }
0082 } else if (objectName_[i] == "EcalADCToGeVConstant") {
0083 const auto mycali = readEcalADCToGeVConstantFromFile(inpFileName_[i].c_str());
0084 if (!toAppend) {
0085 mydbservice->createOneIOV<EcalADCToGeVConstant>(*mycali, newTime, "EcalADCToGeVConstantRcd");
0086 } else {
0087 mydbservice->appendOneIOV<EcalADCToGeVConstant>(*mycali, newTime, "EcalADCToGeVConstantRcd");
0088 }
0089 } else if (objectName_[i] == "EcalIntercalibConstants") {
0090 const auto mycali = readEcalIntercalibConstantsFromFile(inpFileName_[i].c_str(), inpFileNameEE_[i].c_str());
0091 if (!toAppend) {
0092 mydbservice->createOneIOV<EcalIntercalibConstants>(*mycali, newTime, "EcalIntercalibConstantsRcd");
0093 } else {
0094 mydbservice->appendOneIOV<EcalIntercalibConstants>(*mycali, newTime, "EcalIntercalibConstantsRcd");
0095 }
0096 } else if (objectName_[i] == "EcalPFRecHitThresholds") {
0097 const auto mycali = readEcalPFRecHitThresholdsFromFile(inpFileName_[i].c_str(), inpFileNameEE_[i].c_str());
0098 if (!toAppend) {
0099 mydbservice->createOneIOV<EcalPFRecHitThresholds>(*mycali, newTime, "EcalPFRecHitThresholdsRcd");
0100 } else {
0101 mydbservice->appendOneIOV<EcalPFRecHitThresholds>(*mycali, newTime, "EcalPFRecHitThresholdsRcd");
0102 }
0103 } else if (objectName_[i] == "EcalIntercalibConstantsMC") {
0104 const auto mycali = readEcalIntercalibConstantsMCFromFile(inpFileName_[i].c_str(), inpFileNameEE_[i].c_str());
0105 if (!toAppend) {
0106 mydbservice->createOneIOV<EcalIntercalibConstantsMC>(*mycali, newTime, "EcalIntercalibConstantsMCRcd");
0107 } else {
0108 mydbservice->appendOneIOV<EcalIntercalibConstantsMC>(*mycali, newTime, "EcalIntercalibConstantsMCRcd");
0109 }
0110 } else if (objectName_[i] == "EcalGainRatios") {
0111 const auto mycali = readEcalGainRatiosFromFile(inpFileName_[i].c_str());
0112 if (!toAppend) {
0113 mydbservice->createOneIOV<EcalGainRatios>(*mycali, newTime, "EcalGainRatiosRcd");
0114 } else {
0115 mydbservice->appendOneIOV<EcalGainRatios>(*mycali, newTime, "EcalGainRatiosRcd");
0116 }
0117 } else if (objectName_[i] == "EcalChannelStatus") {
0118 const auto mycali = readEcalChannelStatusFromFile(inpFileName_[i].c_str());
0119 if (!toAppend) {
0120 mydbservice->createOneIOV<EcalChannelStatus>(*mycali, newTime, "EcalChannelStatusRcd");
0121 } else {
0122 mydbservice->appendOneIOV<EcalChannelStatus>(*mycali, newTime, "EcalChannelStatusRcd");
0123 }
0124 } else {
0125 edm::LogError("StoreEcalCondition") << "Object " << objectName_[i] << " is not supported by this program.";
0126 }
0127
0128
0129
0130 writeToLogFileResults(messChar);
0131
0132 edm::LogInfo("StoreEcalCondition") << "Finished endJob";
0133 }
0134
0135 delete[] messChar;
0136 }
0137
0138 StoreEcalCondition::~StoreEcalCondition() {}
0139
0140
0141 void StoreEcalCondition::analyze(const edm::Event& evt, const edm::EventSetup& evtSetup) {
0142
0143 }
0144
0145
0146 void StoreEcalCondition::writeToLogFile(string a, string b, unsigned long long since) {
0147
0148
0149 FILE* outFile;
0150 outFile = fopen(logfile_.c_str(), "a");
0151 if (!outFile) {
0152 edm::LogError("StoreEcalCondition") << "*** Can not open file: " << logfile_;
0153 return;
0154 }
0155 char header[256];
0156 fillHeader(header);
0157 char appendMode[10];
0158 if (since != 0)
0159 sprintf(appendMode, "append");
0160 else
0161 sprintf(appendMode, "create");
0162
0163 fprintf(outFile,
0164 "%s %s condition from file %s written into DB for SM %d (mapped to SM %d) in %s mode (since run %u)\n",
0165 header,
0166 a.c_str(),
0167 b.c_str(),
0168 sm_constr_,
0169 sm_slot_,
0170 appendMode,
0171 (unsigned int)since);
0172
0173 fclose(outFile);
0174 }
0175
0176 void StoreEcalCondition::writeToLogFileResults(char* arg) {
0177
0178
0179 FILE* outFile;
0180 outFile = fopen(logfile_.c_str(), "a");
0181 if (!outFile) {
0182 edm::LogError("StoreEcalCondition") << "*** Can not open file: " << logfile_;
0183 return;
0184 }
0185 char header[256];
0186 fillHeader(header);
0187 fprintf(outFile, "%s %s\n", header, arg);
0188 fclose(outFile);
0189 }
0190
0191
0192 void StoreEcalCondition::fillHeader(char* header)
0193
0194 {
0195 time_t rawtime;
0196 struct tm* timeinfo;
0197 time(&rawtime);
0198 timeinfo = localtime(&rawtime);
0199 char user[50];
0200 sprintf(user, "%s", getlogin());
0201 sprintf(header, "%s %s:", asctime(timeinfo), user);
0202 }
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212
0213
0214
0215
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236 std::shared_ptr<EcalWeightXtalGroups> StoreEcalCondition::readEcalWeightXtalGroupsFromFile(const char* inputFile) {
0237
0238
0239
0240
0241 auto xtalGroups = std::make_shared<EcalWeightXtalGroups>();
0242 std::ifstream groupid_in(inputFile);
0243
0244 if (!groupid_in.is_open()) {
0245 edm::LogError("StoreEcalCondition") << "*** Can not open file: " << inputFile;
0246 return nullptr;
0247 }
0248
0249 int smnumber = -99999;
0250
0251 std::ostringstream str;
0252 groupid_in >> smnumber;
0253 if (smnumber == -99999) {
0254 edm::LogError("StoreEcalCondition") << "ERROR: SM number not found in file";
0255 return nullptr;
0256 }
0257 str << "sm= " << smnumber << "\n";
0258 sm_constr_ = smnumber;
0259
0260 char temp[256];
0261
0262 for (int i = 0; i <= 5; i++) {
0263 groupid_in.getline(temp, 255);
0264 str << temp << "\n";
0265 }
0266
0267
0268 groupid_in.getline(temp, 255);
0269 str << temp << "\n";
0270
0271 edm::LogInfo("StoreEcalCondition") << "GROUPID file " << str.str();
0272
0273 int xtals = 0;
0274 int xtal, ietaf, iphif, groupID;
0275 while (groupid_in.good()) {
0276 groupid_in >> xtal >> ietaf >> iphif >> groupID;
0277 if (groupid_in.eof()) {
0278 break;
0279 }
0280
0281 LogDebug("StoreEcalCondition") << "XTAL=" << xtal << " ETA=" << ietaf << " PHI=" << iphif << " GROUP=" << groupID;
0282
0283
0284 EBDetId ebid(sm_slot_, xtal, EBDetId::SMCRYSTALMODE);
0285
0286 xtalGroups->setValue(ebid.rawId(), EcalXtalGroupId(groupID));
0287 xtals++;
0288 }
0289
0290 if (xtals != 1700) {
0291 edm::LogError("StoreEcalCondition") << "ERROR: GROUPID file did not contain data for 1700 crystals";
0292 return nullptr;
0293 }
0294
0295 edm::LogInfo("StoreEcalCondition") << "Groups for " << xtals << " xtals written into DB";
0296 sm_constr_ = smnumber;
0297
0298 return xtalGroups;
0299 }
0300
0301
0302 std::shared_ptr<EcalTBWeights> StoreEcalCondition::readEcalTBWeightsFromFile(const char* inputFile) {
0303
0304
0305
0306
0307 auto tbwgt = std::make_shared<EcalTBWeights>();
0308
0309 std::ifstream WeightsFileTB(inputFile);
0310 if (!WeightsFileTB.is_open()) {
0311 edm::LogError("StoreEcalCondition") << "*** Can not open file: " << inputFile;
0312 return nullptr;
0313 }
0314
0315 int smnumber = -99999;
0316
0317 std::ostringstream str;
0318 WeightsFileTB >> smnumber;
0319 if (smnumber == -99999)
0320 return nullptr;
0321
0322 str << "sm= " << smnumber << "\n";
0323
0324 char temp[256];
0325
0326 for (int i = 0; i <= 5; i++) {
0327 WeightsFileTB.getline(temp, 255);
0328 str << temp << "\n";
0329 }
0330
0331 edm::LogInfo("StoreEcalCondition") << "Weights file " << str.str();
0332
0333 int ngroups = 0;
0334 while (WeightsFileTB.good()) {
0335 int igroup_ID = -99999;
0336 int nSamples = -99999;
0337 int nTdcBins = -99999;
0338
0339 WeightsFileTB >> igroup_ID >> nSamples >> nTdcBins;
0340 if (igroup_ID == -99999 || nSamples == -99999 || nTdcBins == -99999)
0341 break;
0342
0343 std::ostringstream str;
0344 str << "Igroup=" << igroup_ID << " Nsamples=" << nSamples << " NTdcBins=" << nTdcBins << "\n";
0345
0346 for (int iTdcBin = 0; iTdcBin < nTdcBins; iTdcBin++) {
0347 EcalWeightSet wgt;
0348 EcalWeightSet::EcalWeightMatrix& wgt1 = wgt.getWeightsBeforeGainSwitch();
0349 EcalWeightSet::EcalWeightMatrix& wgt2 = wgt.getWeightsAfterGainSwitch();
0350 EcalWeightSet::EcalChi2WeightMatrix& chisq1 = wgt.getChi2WeightsBeforeGainSwitch();
0351 EcalWeightSet::EcalChi2WeightMatrix& chisq2 = wgt.getChi2WeightsAfterGainSwitch();
0352
0353
0354
0355
0356
0357
0358 for (int j = 0; j < nSamples; ++j) {
0359 double ww = 0.0;
0360 WeightsFileTB >> ww;
0361 wgt1(0, j) = ww;
0362 str << ww << " ";
0363 }
0364 str << "\n";
0365
0366
0367 for (int j = 0; j < nSamples; ++j) {
0368 double ww = 0.0;
0369 WeightsFileTB >> ww;
0370 wgt1(1, j) = ww;
0371 str << ww << " ";
0372 }
0373 str << "\n";
0374
0375
0376 for (int j = 0; j < nSamples; ++j) {
0377 double ww = 0.0;
0378 WeightsFileTB >> ww;
0379 wgt1(2, j) = ww;
0380 str << ww << " ";
0381 }
0382 str << "\n";
0383
0384 for (int j = 0; j < nSamples; ++j) {
0385
0386
0387 for (int k = 0; k < nSamples; ++k) {
0388 double ww = 0.0;
0389 WeightsFileTB >> ww;
0390 chisq1(j, k) = ww;
0391 str << ww << " ";
0392 }
0393 str << "\n";
0394 }
0395
0396
0397 for (int j = 0; j < nSamples; ++j) {
0398 double ww = 0.0;
0399 WeightsFileTB >> ww;
0400 wgt2(0, j) = ww;
0401 str << ww << " ";
0402 }
0403 str << "\n";
0404
0405
0406 for (int j = 0; j < nSamples; ++j) {
0407 double ww = 0.0;
0408 WeightsFileTB >> ww;
0409 wgt2(1, j) = ww;
0410 str << ww << " ";
0411 }
0412 str << "\n";
0413
0414
0415 for (int j = 0; j < nSamples; ++j) {
0416 double ww = 0.0;
0417 WeightsFileTB >> ww;
0418 wgt2(2, j) = ww;
0419 str << ww << " ";
0420 }
0421 str << "\n";
0422
0423 for (int j = 0; j < nSamples; ++j) {
0424
0425
0426 for (int k = 0; k < nSamples; ++k) {
0427 double ww = 0.0;
0428 WeightsFileTB >> ww;
0429 chisq2(j, k) = ww;
0430 str << ww << " ";
0431 }
0432 str << "\n";
0433 }
0434
0435 LogDebug("StoreEcalCondition") << str.str();
0436
0437
0438 tbwgt->setValue(std::make_pair(igroup_ID, iTdcBin + 1), wgt);
0439 }
0440 ngroups++;
0441 }
0442
0443 sm_constr_ = smnumber;
0444
0445 edm::LogInfo("StoreEcalCondition") << "Weights for " << ngroups << " groups written into DB";
0446 return tbwgt;
0447 }
0448
0449
0450 std::shared_ptr<EcalADCToGeVConstant> StoreEcalCondition::readEcalADCToGeVConstantFromFile(const char* inputFile) {
0451
0452
0453 FILE* inpFile;
0454 inpFile = fopen(inputFile, "r");
0455 if (!inpFile) {
0456 edm::LogError("StoreEcalCondition") << "*** Can not open file: " << inputFile;
0457 return nullptr;
0458 }
0459
0460 char line[256];
0461
0462 std::ostringstream str;
0463
0464 fgets(line, 255, inpFile);
0465 int sm_number = atoi(line);
0466 str << "sm= " << sm_number << "\n";
0467
0468 fgets(line, 255, inpFile);
0469
0470
0471 fgets(line, 255, inpFile);
0472 string gen_tag = to_string(line);
0473 str << "gen tag " << gen_tag << "\n";
0474
0475 fgets(line, 255, inpFile);
0476 string cali_method = to_string(line);
0477 str << "cali method " << cali_method << "\n";
0478
0479 fgets(line, 255, inpFile);
0480 string cali_version = to_string(line);
0481 str << "cali version " << cali_version << "\n";
0482
0483 fgets(line, 255, inpFile);
0484 string cali_type = to_string(line);
0485 str << "cali type " << cali_type << "\n";
0486
0487 edm::LogInfo("StoreEcalCondition") << "ADCToGeV file " << str.str();
0488
0489 fgets(line, 255, inpFile);
0490 float adc_to_gev = 0;
0491 sscanf(line, "%f", &adc_to_gev);
0492 LogDebug("StoreEcalCondition") << " calib=" << adc_to_gev;
0493 fgets(line, 255, inpFile);
0494 float adc_to_gev_ee = 0;
0495 sscanf(line, "%f", &adc_to_gev_ee);
0496 LogDebug("StoreEcalCondition") << " calib=" << adc_to_gev_ee;
0497
0498 fclose(inpFile);
0499
0500 sm_constr_ = sm_number;
0501
0502
0503 auto agc = std::make_shared<EcalADCToGeVConstant>(adc_to_gev, adc_to_gev_ee);
0504 edm::LogInfo("StoreEcalCondition") << "ADCtoGeV scale written into the DB";
0505 return agc;
0506 }
0507
0508
0509 std::shared_ptr<EcalPFRecHitThresholds> StoreEcalCondition::readEcalPFRecHitThresholdsFromFile(
0510 const char* inputFile, const char* inputFileEE) {
0511
0512
0513 auto ical = std::make_shared<EcalPFRecHitThresholds>();
0514
0515 FILE* inpFile;
0516 inpFile = fopen(inputFile, "r");
0517 if (!inpFile) {
0518 edm::LogError("StoreEcalCondition") << "*** Can not open file: " << inputFile;
0519 return nullptr;
0520 }
0521
0522 char line[256];
0523
0524 int ieta = 0;
0525 int iphi = 0;
0526 int ix = 0;
0527 int iy = 0;
0528 int iz = 0;
0529
0530 float thresh = 0;
0531
0532 int ii = 0;
0533 while (fgets(line, 255, inpFile)) {
0534 sscanf(line, "%d %d %f ", &ieta, &iphi, &thresh);
0535 if (ii == 0)
0536 edm::LogVerbatim("StoreEcalCondition") << "crystal " << ieta << "/" << iphi << " Thresh= " << thresh << "\n";
0537
0538 if (EBDetId::validDetId(ieta, iphi)) {
0539 EBDetId ebid(ieta, iphi);
0540 ical->setValue(ebid.rawId(), thresh);
0541 ii++;
0542 }
0543 }
0544
0545
0546 fclose(inpFile);
0547
0548 edm::LogInfo("StoreEcalCondition") << "Read PF RecHits for " << ii << " xtals ";
0549
0550 FILE* inpFileEE;
0551 inpFileEE = fopen(inputFileEE, "r");
0552 if (!inpFileEE) {
0553 edm::LogError("StoreEcalCondition") << "*** Can not open file: " << inputFileEE;
0554 return nullptr;
0555 }
0556 ii = 0;
0557 while (fgets(line, 255, inpFileEE)) {
0558 sscanf(line, "%d %d %d %f ", &ix, &iy, &iz, &thresh);
0559 if (ii == 0)
0560 edm::LogVerbatim("StoreEcalCondition")
0561 << "crystal " << ix << "/" << iy << "/" << iz << " Thresh= " << thresh << "\n";
0562 if (EEDetId::validDetId(ix, iy, iz)) {
0563 EEDetId eeid(ix, iy, iz);
0564 ical->setValue(eeid.rawId(), thresh);
0565 ii++;
0566 }
0567 }
0568
0569
0570 fclose(inpFileEE);
0571
0572 edm::LogInfo("StoreEcalCondition") << "loop on EE channels done - number of crystals =" << ii;
0573
0574 return ical;
0575 }
0576
0577
0578 std::shared_ptr<EcalIntercalibConstants> StoreEcalCondition::readEcalIntercalibConstantsFromFile(
0579 const char* inputFile, const char* inputFileEE) {
0580
0581
0582 auto ical = std::make_shared<EcalIntercalibConstants>();
0583
0584 FILE* inpFile;
0585 inpFile = fopen(inputFile, "r");
0586 if (!inpFile) {
0587 edm::LogError("StoreEcalCondition") << "*** Can not open file: " << inputFile;
0588 return nullptr;
0589 }
0590
0591 char line[256];
0592
0593 std::ostringstream str;
0594
0595 fgets(line, 255, inpFile);
0596 string sm_or_all = to_string(line);
0597 int sm_number = 0;
0598 int nchan = 1700;
0599 sm_number = atoi(line);
0600 str << "sm= " << sm_number << "\n";
0601 if (sm_number != -1) {
0602 nchan = 1700;
0603 } else {
0604 nchan = 61200;
0605 }
0606
0607 fgets(line, 255, inpFile);
0608
0609
0610 fgets(line, 255, inpFile);
0611 string gen_tag = to_string(line);
0612 str << "gen tag " << gen_tag << "\n";
0613
0614 fgets(line, 255, inpFile);
0615 string cali_method = to_string(line);
0616 str << "cali method " << cali_method << "\n";
0617
0618 fgets(line, 255, inpFile);
0619 string cali_version = to_string(line);
0620 str << "cali version " << cali_version << "\n";
0621
0622 fgets(line, 255, inpFile);
0623 string cali_type = to_string(line);
0624 str << "cali type " << cali_type << "\n";
0625
0626 edm::LogInfo("StoreEcalCondition") << "Intercalibration file " << str.str();
0627
0628 int sm_num[61200] = {0};
0629 int cry_num[61200] = {0};
0630 float calib[61200] = {0};
0631 float calib_rms[61200] = {0};
0632 int calib_nevents[61200] = {0};
0633 int calib_status[61200] = {0};
0634
0635 int ii = 0;
0636 if (sm_number != -1) {
0637 while (fgets(line, 255, inpFile)) {
0638 sscanf(line, "%d %f %f %d %d", &cry_num[ii], &calib[ii], &calib_rms[ii], &calib_nevents[ii], &calib_status[ii]);
0639
0640
0641
0642 sm_num[ii] = sm_number;
0643 ii++;
0644 }
0645 } else {
0646
0647 edm::LogInfo("StoreEcalCondition") << "mode ALL BARREL";
0648 while (fgets(line, 255, inpFile)) {
0649 sscanf(line, "%d %d %f %f %d", &sm_num[ii], &cry_num[ii], &calib[ii], &calib_rms[ii], &calib_status[ii]);
0650 if (ii == 0)
0651 edm::LogVerbatim("StoreEcalCondition")
0652 << "crystal " << cry_num[ii] << " of sm " << sm_num[ii] << " cali= " << calib[ii] << "\n";
0653 ii++;
0654 }
0655 }
0656
0657
0658 fclose(inpFile);
0659
0660 edm::LogInfo("StoreEcalCondition") << "Read intercalibrations for " << ii << " xtals ";
0661
0662 if (ii != nchan)
0663 edm::LogWarning("StoreEcalCondition") << "Some crystals missing. Missing channels will be set to 0";
0664
0665
0666
0667 sm_constr_ = sm_number;
0668
0669
0670 for (int i = 0; i < nchan; i++) {
0671
0672
0673
0674 int slot_num = convertFromConstructionSMToSlot(sm_num[i], -1);
0675 EBDetId ebid(slot_num, cry_num[i], EBDetId::SMCRYSTALMODE);
0676
0677 ical->setValue(ebid.rawId(), calib[i]);
0678
0679 if (i == 0)
0680 edm::LogVerbatim("StoreEcalCondition") << "crystal " << cry_num[i] << " of sm " << sm_num[i] << " in slot "
0681 << slot_num << " calib= " << calib[i] << "\n";
0682
0683 }
0684
0685 edm::LogInfo("StoreEcalCondition") << "loop on channels done";
0686
0687 FILE* inpFileEE;
0688 inpFileEE = fopen(inputFileEE, "r");
0689 if (!inpFileEE) {
0690 edm::LogError("StoreEcalCondition") << "*** Can not open file: " << inputFile;
0691
0692
0693
0694 for (int iX = EEDetId::IX_MIN; iX <= EEDetId::IX_MAX; ++iX) {
0695 for (int iY = EEDetId::IY_MIN; iY <= EEDetId::IY_MAX; ++iY) {
0696
0697 if (EEDetId::validDetId(iX, iY, 1)) {
0698 EEDetId eedetidpos(iX, iY, 1);
0699 ical->setValue(eedetidpos, 1.0);
0700 }
0701 if (EEDetId::validDetId(iX, iY, -1)) {
0702 EEDetId eedetidneg(iX, iY, -1);
0703 ical->setValue(eedetidneg, 1.0);
0704 }
0705 }
0706 }
0707
0708 } else {
0709 edm::LogInfo("StoreEcalCondition") << "... now reading EE file ...";
0710
0711 int ii = 0;
0712 while (fgets(line, 255, inpFileEE)) {
0713 int iz, ix, iy;
0714 float calibee;
0715 sscanf(line, "%d %d %d %f", &iz, &ix, &iy, &calibee);
0716 if (ii <= 0)
0717 edm::LogVerbatim("StoreEcalCondition")
0718 << "crystal " << iz << "/" << ix << "/" << iy << " cali=" << calibee << "\n";
0719
0720 if (EEDetId::validDetId(ix, iy, iz)) {
0721 EEDetId eedetid(ix, iy, iz);
0722 ical->setValue(eedetid, calibee);
0723 }
0724
0725 ii++;
0726 }
0727
0728 fclose(inpFileEE);
0729 }
0730
0731 edm::LogInfo("StoreEcalCondition") << "loop on EE channels done";
0732
0733 return ical;
0734 }
0735
0736
0737 std::shared_ptr<EcalIntercalibConstantsMC> StoreEcalCondition::readEcalIntercalibConstantsMCFromFile(
0738 const char* inputFile, const char* inputFileEE) {
0739
0740
0741 auto ical = std::make_shared<EcalIntercalibConstantsMC>();
0742
0743 FILE* inpFile;
0744 inpFile = fopen(inputFile, "r");
0745 if (!inpFile) {
0746 edm::LogError("StoreEcalCondition") << "*** Can not open file: " << inputFile;
0747 return nullptr;
0748 }
0749
0750 char line[256];
0751
0752 std::ostringstream str;
0753
0754 fgets(line, 255, inpFile);
0755 string sm_or_all = to_string(line);
0756 int sm_number = 0;
0757 int nchan = 1700;
0758 sm_number = atoi(line);
0759 str << "sm= " << sm_number << "\n";
0760 if (sm_number != -1) {
0761 nchan = 1700;
0762 } else {
0763 nchan = 61200;
0764 }
0765
0766 fgets(line, 255, inpFile);
0767
0768
0769 fgets(line, 255, inpFile);
0770 string gen_tag = to_string(line);
0771 str << "gen tag " << gen_tag << "\n";
0772
0773 fgets(line, 255, inpFile);
0774 string cali_method = to_string(line);
0775 str << "cali method " << cali_method << "\n";
0776
0777 fgets(line, 255, inpFile);
0778 string cali_version = to_string(line);
0779 str << "cali version " << cali_version << "\n";
0780
0781 fgets(line, 255, inpFile);
0782 string cali_type = to_string(line);
0783 str << "cali type " << cali_type << "\n";
0784
0785 edm::LogInfo("StoreEcalCondition") << "Intercalibration file " << str.str();
0786
0787 int sm_num[61200] = {0};
0788 int cry_num[61200] = {0};
0789 float calib[61200] = {0};
0790 float calib_rms[61200] = {0};
0791 int calib_nevents[61200] = {0};
0792 int calib_status[61200] = {0};
0793
0794 int ii = 0;
0795 if (sm_number != -1) {
0796 while (fgets(line, 255, inpFile)) {
0797 sscanf(line, "%d %f %f %d %d", &cry_num[ii], &calib[ii], &calib_rms[ii], &calib_nevents[ii], &calib_status[ii]);
0798
0799
0800
0801 sm_num[ii] = sm_number;
0802 ii++;
0803 }
0804 } else {
0805
0806 edm::LogInfo("StoreEcalCondition") << "mode ALL BARREL";
0807 while (fgets(line, 255, inpFile)) {
0808 sscanf(line, "%d %d %f %f %d", &sm_num[ii], &cry_num[ii], &calib[ii], &calib_rms[ii], &calib_status[ii]);
0809 if (ii == 0)
0810 edm::LogVerbatim("StoreEcalCondition")
0811 << "crystal " << cry_num[ii] << " of sm " << sm_num[ii] << " cali= " << calib[ii] << "\n";
0812 ii++;
0813 }
0814 }
0815
0816
0817 fclose(inpFile);
0818
0819 edm::LogInfo("StoreEcalCondition") << "Read intercalibrations for " << ii << " xtals ";
0820
0821 if (ii != nchan)
0822 edm::LogWarning("StoreEcalCondition") << "Some crystals missing. Missing channels will be set to 0";
0823
0824
0825
0826 sm_constr_ = sm_number;
0827
0828
0829 for (int i = 0; i < nchan; i++) {
0830
0831
0832
0833 int slot_num = convertFromConstructionSMToSlot(sm_num[i], -1);
0834 EBDetId ebid(slot_num, cry_num[i], EBDetId::SMCRYSTALMODE);
0835
0836 ical->setValue(ebid.rawId(), calib[i]);
0837
0838 if (i == 0)
0839 edm::LogVerbatim("StoreEcalCondition") << "crystal " << cry_num[i] << " of sm " << sm_num[i] << " in slot "
0840 << slot_num << " calib= " << calib[i] << "\n";
0841
0842 }
0843
0844 edm::LogInfo("StoreEcalCondition") << "loop on channels done";
0845
0846 FILE* inpFileEE;
0847 inpFileEE = fopen(inputFileEE, "r");
0848 if (!inpFileEE) {
0849 edm::LogError("StoreEcalCondition") << "*** Can not open file: " << inputFile;
0850
0851
0852
0853 for (int iX = EEDetId::IX_MIN; iX <= EEDetId::IX_MAX; ++iX) {
0854 for (int iY = EEDetId::IY_MIN; iY <= EEDetId::IY_MAX; ++iY) {
0855
0856 if (EEDetId::validDetId(iX, iY, 1)) {
0857 EEDetId eedetidpos(iX, iY, 1);
0858 ical->setValue(eedetidpos, 1.0);
0859 }
0860 if (EEDetId::validDetId(iX, iY, -1)) {
0861 EEDetId eedetidneg(iX, iY, -1);
0862 ical->setValue(eedetidneg, 1.0);
0863 }
0864 }
0865 }
0866
0867 } else {
0868 edm::LogInfo("StoreEcalCondition") << "... now reading EE file ...";
0869
0870 int ii = 0;
0871 while (fgets(line, 255, inpFileEE)) {
0872 int iz, ix, iy;
0873 float calibee;
0874 sscanf(line, "%d %d %d %f", &iz, &ix, &iy, &calibee);
0875 if (ii <= 0)
0876 edm::LogVerbatim("StoreEcalCondition")
0877 << "crystal " << iz << "/" << ix << "/" << iy << " cali=" << calibee << "\n";
0878
0879 if (EEDetId::validDetId(ix, iy, iz)) {
0880 EEDetId eedetid(ix, iy, iz);
0881 ical->setValue(eedetid, calibee);
0882 }
0883
0884 ii++;
0885 }
0886
0887 fclose(inpFileEE);
0888 }
0889
0890 edm::LogInfo("StoreEcalCondition") << "loop on EE channels done";
0891
0892 return ical;
0893 }
0894
0895
0896 int StoreEcalCondition::convertFromConstructionSMToSlot(int sm_constr, int sm_slot) {
0897
0898
0899
0900
0901 int slot_to_constr[37] = {-1, 12, 17, 10, 1, 8, 4, 27, 20, 23, 25, 6, 34, 35, 15, 18, 30, 21, 9,
0902 24, 22, 13, 31, 26, 16, 2, 11, 5, 0, 29, 28, 14, 33, 32, 3, 7, 19};
0903 int constr_to_slot[36] = {28, 4, 25, 34, 6, 27, 11, 35, 5, 18, 3, 26, 1, 21, 31, 14, 24, 2,
0904 15, 36, 8, 17, 20, 9, 19, 10, 23, 7, 30, 29, 16, 22, 33, 32, 12, 13};
0905
0906 int result = 0;
0907 if (sm_constr != -1) {
0908 result = constr_to_slot[sm_constr];
0909 } else if (sm_slot != -1) {
0910 result = slot_to_constr[sm_slot];
0911 }
0912 return result;
0913 }
0914
0915
0916 std::shared_ptr<EcalGainRatios> StoreEcalCondition::readEcalGainRatiosFromFile(const char* inputFile) {
0917
0918
0919
0920 auto gratio = std::make_shared<EcalGainRatios>();
0921
0922 FILE* inpFile;
0923 inpFile = fopen(inputFile, "r");
0924 if (!inpFile) {
0925 edm::LogError("StoreEcalCondition") << "*** Can not open file: " << inputFile;
0926 return nullptr;
0927 }
0928
0929 char line[256];
0930 std::ostringstream str;
0931
0932 fgets(line, 255, inpFile);
0933 string sm_or_all = to_string(line);
0934 int sm_number = 0;
0935 sm_number = atoi(line);
0936 str << "sm= " << sm_number << "\n";
0937
0938 fgets(line, 255, inpFile);
0939
0940
0941 fgets(line, 255, inpFile);
0942 string gen_tag = to_string(line);
0943 str << "gen tag " << gen_tag << "\n";
0944
0945 fgets(line, 255, inpFile);
0946 string cali_method = to_string(line);
0947 str << "cali method " << cali_method << "\n";
0948
0949 fgets(line, 255, inpFile);
0950 string cali_version = to_string(line);
0951 str << "cali version " << cali_version << "\n";
0952
0953 fgets(line, 255, inpFile);
0954 string cali_type = to_string(line);
0955
0956 str << "cali type " << cali_type << "\n";
0957
0958 edm::LogInfo("StoreEcalCondition") << "GainRatio file " << str.str();
0959
0960 int cry_num[61200] = {0};
0961 float g1_g12[61200] = {0};
0962 float g6_g12[61200] = {0};
0963 int calib_status[61200] = {0};
0964 int dummy1 = 0;
0965 int dummy2 = 0;
0966 int hash1 = 0;
0967
0968 int ii = 0;
0969
0970 if (sm_number != -1) {
0971 while (fgets(line, 255, inpFile)) {
0972 sscanf(line, "%d %d %d %f %f %d", &dummy1, &dummy2, &cry_num[ii], &g1_g12[ii], &g6_g12[ii], &calib_status[ii]);
0973 ii++;
0974 }
0975
0976 fclose(inpFile);
0977
0978 edm::LogInfo("StoreEcalCondition") << "Read gainRatios for " << ii << " xtals ";
0979 if (ii != 1700)
0980 edm::LogWarning("StoreEcalCondition") << " Missing crystals:: missing channels will be set to 0";
0981
0982
0983 sm_constr_ = sm_number;
0984
0985 for (int i = 0; i < 1700; i++) {
0986
0987
0988 EBDetId ebid(sm_slot_, cry_num[i], EBDetId::SMCRYSTALMODE);
0989 EcalMGPAGainRatio gr;
0990 gr.setGain12Over6(g6_g12[i]);
0991 gr.setGain6Over1(g1_g12[i] / g6_g12[i]);
0992 gratio->setValue(ebid.rawId(), gr);
0993 }
0994
0995 } else {
0996
0997 edm::LogInfo("StoreEcalCondition") << "mode ALL BARREL";
0998 while (fgets(line, 255, inpFile)) {
0999 int eta = 0;
1000 int phi = 0;
1001 sscanf(line, "%d %d %d %f %f", &hash1, &eta, &phi, &g1_g12[ii], &g6_g12[ii]);
1002 if (ii < 20)
1003 edm::LogVerbatim("StoreEcalCondition")
1004 << "crystal eta/phi=" << eta << "/" << phi << " g1_12/g6_12= " << g1_g12[ii] << "/" << g6_g12[ii] << "\n";
1005
1006 if (g1_g12[ii] < 9 || g1_g12[ii] > 15)
1007 g1_g12[ii] = 12.0;
1008 if (g6_g12[ii] < 1 || g6_g12[ii] > 3)
1009 g6_g12[ii] = 2.0;
1010
1011 if (eta < -85 || eta > 85 || eta == 0)
1012 edm::LogVerbatim("StoreEcalCondition") << "error!!!\n";
1013 if (phi < 1 || phi > 360)
1014 edm::LogVerbatim("StoreEcalCondition") << "error!!!\n";
1015
1016 EBDetId ebid(eta, phi, EBDetId::ETAPHIMODE);
1017 EcalMGPAGainRatio gr;
1018 gr.setGain12Over6(g6_g12[ii]);
1019 gr.setGain6Over1(g1_g12[ii] / g6_g12[ii]);
1020 gratio->setValue(ebid.rawId(), gr);
1021
1022 ii++;
1023 }
1024
1025 fclose(inpFile);
1026 if (ii != 61200)
1027 edm::LogWarning("StoreEcalCondition") << " Missing crystals !!!!!!!";
1028
1029 edm::LogInfo("StoreEcalCondition") << "number of crystals read:" << ii;
1030 }
1031
1032 for (int iX = EEDetId::IX_MIN; iX <= EEDetId::IX_MAX; ++iX) {
1033 for (int iY = EEDetId::IY_MIN; iY <= EEDetId::IY_MAX; ++iY) {
1034
1035 EcalMGPAGainRatio gr;
1036 gr.setGain12Over6(2.);
1037 gr.setGain6Over1(6.);
1038
1039 if (EEDetId::validDetId(iX, iY, 1)) {
1040 EEDetId eedetidpos(iX, iY, 1);
1041 gratio->setValue(eedetidpos.rawId(), gr);
1042 }
1043 if (EEDetId::validDetId(iX, iY, -1)) {
1044 EEDetId eedetidneg(iX, iY, -1);
1045 gratio->setValue(eedetidneg.rawId(), gr);
1046 }
1047 }
1048 }
1049
1050 edm::LogInfo("StoreEcalCondition") << " gratio pointer=" << gratio;
1051
1052 edm::LogInfo("StoreEcalCondition") << "now leaving";
1053
1054 return gratio;
1055 }
1056
1057 std::shared_ptr<EcalChannelStatus> StoreEcalCondition::readEcalChannelStatusFromFile(const char* inputFile) {
1058 auto status = std::make_shared<EcalChannelStatus>();
1059
1060 for (int ieta = -EBDetId::MAX_IETA; ieta <= EBDetId::MAX_IETA; ++ieta) {
1061 if (ieta == 0)
1062 continue;
1063 for (int iphi = EBDetId::MIN_IPHI; iphi <= EBDetId::MAX_IPHI; ++iphi) {
1064 if (EBDetId::validDetId(ieta, iphi)) {
1065 EBDetId ebid(ieta, iphi);
1066 status->setValue(ebid, 0);
1067 }
1068 }
1069 }
1070
1071 for (int iX = EEDetId::IX_MIN; iX <= EEDetId::IX_MAX; ++iX) {
1072 for (int iY = EEDetId::IY_MIN; iY <= EEDetId::IY_MAX; ++iY) {
1073
1074 if (EEDetId::validDetId(iX, iY, 1)) {
1075 EEDetId eedetidpos(iX, iY, 1);
1076 status->setValue(eedetidpos, 0);
1077 }
1078 if (EEDetId::validDetId(iX, iY, -1)) {
1079 EEDetId eedetidneg(iX, iY, -1);
1080 status->setValue(eedetidneg, 0);
1081 }
1082 }
1083 }
1084
1085 edm::LogInfo("StoreEcalCondition") << "Reading channel status from file " << inputFile;
1086 FILE* ifile = fopen(inputFile, "r");
1087 if (!ifile)
1088 throw cms::Exception("Cannot open ECAL channel status file");
1089
1090 char line[256];
1091
1092 fgets(line, 255, ifile);
1093 std::string gen_tag = line;
1094 edm::LogVerbatim("StoreEcalCondition") << "Gen tag " << gen_tag << "\n";
1095
1096 fgets(line, 255, ifile);
1097 std::string comment = line;
1098 edm::LogVerbatim("StoreEcalCondition") << "Gen comment " << comment << "\n";
1099
1100 int iovRunStart(0);
1101 fgets(line, 255, ifile);
1102 sscanf(line, "%d", &iovRunStart);
1103 edm::LogVerbatim("StoreEcalCondition") << "IOV START " << iovRunStart << "\n";
1104
1105
1106 int iovRunEnd(0);
1107 fgets(line, 255, ifile);
1108 sscanf(line, "%d", &iovRunEnd);
1109 edm::LogVerbatim("StoreEcalCondition") << "IOV END " << iovRunEnd << "\n";
1110
1111
1112 while (fgets(line, 255, ifile)) {
1113 std::string EBorEE;
1114 int hashedIndex(0);
1115 int chStatus(0);
1116 std::stringstream aStrStream;
1117 aStrStream << line;
1118 aStrStream >> EBorEE >> hashedIndex >> chStatus;
1119 edm::LogVerbatim("StoreEcalCondition")
1120 << EBorEE << " hashedIndex " << hashedIndex << " status " << chStatus << "\n";
1121
1122 if (EBorEE == "EB") {
1123 EBDetId aEBDetId = EBDetId::unhashIndex(hashedIndex);
1124 status->setValue(aEBDetId, chStatus);
1125 } else if (EBorEE == "EE") {
1126
1127 EEDetId aEEDetId = EEDetId::unhashIndex(hashedIndex);
1128 status->setValue(aEEDetId, chStatus);
1129 } else if (EBorEE == "EBTT") {
1130 int ism = hashedIndex;
1131 int itt = chStatus;
1132
1133 int ixtt = (itt - 1) % 4;
1134 int iytt = (itt - 1) / 4;
1135 int ixmin = ixtt * 5;
1136 int iymin = iytt * 5;
1137 int ixmax = (ixtt + 1) * 5 - 1;
1138 int iymax = (iytt + 1) * 5 - 1;
1139 for (int ieta = iymin; ieta <= iymax; ieta++) {
1140 for (int iphi = ixmin; iphi <= ixmax; iphi++) {
1141 int ixt = ieta * 20 + iphi + 1;
1142 edm::LogVerbatim("StoreEcalCondition") << "killing crystal " << ism << "/" << ixt << "\n";
1143 EBDetId ebid(ism, ixt, EBDetId::SMCRYSTALMODE);
1144 status->setValue(ebid, 1);
1145 }
1146 }
1147 }
1148 }
1149
1150 fclose(ifile);
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176 return status;
1177 }