Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:59:40

0001 #include "CalibTracker/SiStripChannelGain/plugins/SiStripGainFromAsciiFile.h"
0002 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0003 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0004 
0005 #include "CalibTracker/SiStripCommon/interface/SiStripDetInfoFileReader.h"
0006 
0007 #include "DataFormats/DetId/interface/DetId.h"
0008 
0009 #include <iostream>
0010 #include <fstream>
0011 #include <sstream>
0012 
0013 SiStripGainFromAsciiFile::SiStripGainFromAsciiFile(const edm::ParameterSet& iConfig)
0014     : ConditionDBWriter<SiStripApvGain>(iConfig) {
0015   Asciifilename_ = iConfig.getParameter<std::string>("InputFileName");
0016   referenceValue_ = iConfig.getParameter<double>("referenceValue");
0017   fp_ = iConfig.getUntrackedParameter<edm::FileInPath>("file", edm::FileInPath(SiStripDetInfoFileReader::kDefaultFile));
0018 }
0019 
0020 SiStripGainFromAsciiFile::~SiStripGainFromAsciiFile() {
0021   edm::LogInfo("SiStripGainFromAsciiFile::~SiStripGainFromAsciiFile");
0022 }
0023 
0024 std::unique_ptr<SiStripApvGain> SiStripGainFromAsciiFile::getNewObject() {
0025   auto obj = std::make_unique<SiStripApvGain>();
0026 
0027   std::stringstream ss;
0028   ss.str("");
0029   ss << "[SiStripGainFromAsciiFile::getNewObject]\n Reading Ascii File\n";
0030   FILE* infile = fopen(Asciifilename_.c_str(), "r");
0031   char line[4096];
0032   if (infile) {
0033     while (fgets(line, 4096, infile) != nullptr) {
0034       uint32_t detid;
0035       ModuleGain MG;
0036       MG.apv[0] = 0.0;
0037       MG.apv[1] = 0.0;
0038       MG.apv[2] = 0.0;
0039       MG.apv[3] = 0.0;
0040       MG.apv[4] = 0.0;
0041       MG.apv[5] = 0.0;
0042       char* saveptr;
0043       char* pch = strtok_r(line, " ", &saveptr);
0044       int Arg = 0;
0045       while (pch != nullptr) {
0046         if (Arg == 0) {
0047           sscanf(pch, "%d", &detid);
0048         } else if (Arg <= 6) {
0049           sscanf(pch, "%f", &(MG.apv[Arg - 1]));
0050         } else {
0051           //nothing to do here
0052         }
0053         pch = strtok_r(nullptr, " ", &saveptr);
0054         Arg++;
0055       }
0056       ss << detid << " " << MG.apv[0] << " " << MG.apv[1] << " " << MG.apv[2] << " " << MG.apv[3] << " " << MG.apv[4]
0057          << " " << MG.apv[5] << std::endl;
0058       GainsMap.insert(std::pair<unsigned int, ModuleGain>(detid, MG));
0059     }
0060     fclose(infile);
0061     edm::LogInfo("SiStripGainFromAsciiFile") << ss.str();
0062   } else {
0063     edm::LogError("SiStripGainFromAsciiFile")
0064         << " [SiStripGainFromAsciiFile::getNewObject] Error opening file " << Asciifilename_ << std::endl;
0065     assert(0);
0066   }
0067 
0068   const auto detInfo = SiStripDetInfoFileReader::read(fp_.fullPath());
0069 
0070   ss.str("");
0071   ss << "[SiStripGainFromAsciiFile::getNewObject]\n Filling SiStripApvGain object";
0072   short nApvPair;
0073   for (const auto it : detInfo.getAllDetIds()) {
0074     ModuleGain MG;
0075     if (DetId(it).det() != DetId::Tracker)
0076       continue;
0077 
0078     nApvPair = detInfo.getNumberOfApvsAndStripLength(it).first / 2;
0079 
0080     ss << "Looking at detid " << it << " nApvPairs  " << nApvPair << std::endl;
0081     auto iter = GainsMap.find(it);
0082     if (iter != GainsMap.end()) {
0083       MG = iter->second;
0084       ss << " " << MG.apv[0] << " " << MG.apv[1] << " " << MG.apv[2] << " " << MG.apv[3] << " " << MG.apv[4] << " "
0085          << MG.apv[5] << std::endl;
0086     } else {
0087       ss << "Hard reset for detid " << it << std::endl;
0088       MG.hard_reset(referenceValue_);
0089     }
0090 
0091     std::vector<float> DetGainsVector;
0092 
0093     if (nApvPair == 2) {
0094       DetGainsVector.push_back(MG.apv[0] / referenceValue_);
0095       DetGainsVector.push_back(MG.apv[1] / referenceValue_);
0096       DetGainsVector.push_back(MG.apv[2] / referenceValue_);
0097       DetGainsVector.push_back(MG.apv[3] / referenceValue_);
0098     } else if (nApvPair == 3) {
0099       DetGainsVector.push_back(MG.apv[0] / referenceValue_);
0100       DetGainsVector.push_back(MG.apv[1] / referenceValue_);
0101       DetGainsVector.push_back(MG.apv[2] / referenceValue_);
0102       DetGainsVector.push_back(MG.apv[3] / referenceValue_);
0103       DetGainsVector.push_back(MG.apv[4] / referenceValue_);
0104       DetGainsVector.push_back(MG.apv[5] / referenceValue_);
0105     } else {
0106       edm::LogError("SiStripGainFromAsciiFile") << " SiStripGainFromAsciiFile::getNewObject] ERROR for detid " << it
0107                                                 << " not expected number of APV pairs " << nApvPair << std::endl;
0108     }
0109 
0110     SiStripApvGain::Range range(DetGainsVector.begin(), DetGainsVector.end());
0111     if (!obj->put(it, range)) {
0112       edm::LogError("SiStripGainFromAsciiFile")
0113           << " [SiStripGainFromAsciiFile::getNewObject] detid already exists" << std::endl;
0114       ss << " [SiStripGainFromAsciiFile::getNewObject] detid already exists" << std::endl;
0115     }
0116   }
0117   edm::LogInfo("SiStripGainFromAsciiFile") << ss.str();
0118 
0119   return obj;
0120 }