Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-05-29 04:20:19

0001 // system include files
0002 #include <iostream>
0003 #include <fstream>
0004 #include <vector>
0005 
0006 // user include files
0007 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0008 #include "FWCore/ServiceRegistry/interface/Service.h"
0009 #include "FWCore/ParameterSet/interface/FileInPath.h"
0010 #include "CondFormats/DataRecord/interface/DeDxCalibrationRcd.h"
0011 #include "CondFormats/PhysicsToolsObjects/interface/DeDxCalibration.h"
0012 #include "CondCore/DBOutputService/interface/PoolDBOutputService.h"
0013 
0014 class DeDxCalibrationDbCreator : public edm::one::EDAnalyzer<edm::one::SharedResources> {
0015 public:
0016   typedef std::pair<uint32_t, unsigned char> ChipId;
0017   enum AllDetector { PXB = 0, PXF = 1, TIB = 2, TID = 3, TOB = 4, TECThin = 5, TECThick = 6, nDets };
0018 
0019   explicit DeDxCalibrationDbCreator(const edm::ParameterSet&);
0020   ~DeDxCalibrationDbCreator() override{};
0021 
0022 private:
0023   void beginJob() override;
0024   void analyze(const edm::Event&, const edm::EventSetup&) override{};
0025 
0026   void readStripProps(std::vector<double>&, std::vector<double>&, std::vector<double>&);
0027   void readGainCorrection(std::map<ChipId, float>&);
0028 
0029   const std::string propFile_, gainFile_;
0030 };
0031 
0032 DeDxCalibrationDbCreator::DeDxCalibrationDbCreator(const edm::ParameterSet& iConfig)
0033     : propFile_(iConfig.getParameter<std::string>("propFile")),
0034       gainFile_(iConfig.getParameter<std::string>("gainFile")) {}
0035 
0036 void DeDxCalibrationDbCreator::beginJob() {
0037   std::map<ChipId, float> gain;
0038   std::vector<double> thr, alpha, sigma;
0039   readStripProps(thr, alpha, sigma);
0040   readGainCorrection(gain);
0041   DeDxCalibration TD(thr, alpha, sigma, gain);
0042 
0043   edm::Service<cond::service::PoolDBOutputService> pool;
0044   if (pool.isAvailable())
0045     pool->writeOneIOV(TD, pool->beginOfTime(), "DeDxCalibrationRcd");
0046 }
0047 
0048 /*****************************************************************************/
0049 void DeDxCalibrationDbCreator::readStripProps(std::vector<double>& thr,
0050                                               std::vector<double>& alpha,
0051                                               std::vector<double>& sigma) {
0052   std::cout << " reading strip properties from " << propFile_;
0053   std::ifstream file(edm::FileInPath(propFile_).fullPath());
0054 
0055   int det;
0056   for (det = PXB; det <= PXF; det++) {
0057     thr.emplace_back(0.);
0058     alpha.emplace_back(0.);
0059     sigma.emplace_back(0.);
0060   }
0061 
0062   while (!file.eof()) {
0063     std::string detName;
0064     float f;
0065 
0066     file >> detName;
0067     file >> f;
0068     thr.emplace_back(f);
0069     file >> f;
0070     alpha.emplace_back(f);
0071     file >> f;
0072     sigma.emplace_back(f);
0073 
0074     det++;
0075   }
0076 
0077   file.close();
0078   std::cout << " [done]" << std::endl;
0079 }
0080 
0081 /*****************************************************************************/
0082 void DeDxCalibrationDbCreator::readGainCorrection(std::map<ChipId, float>& gain) {
0083   std::cout << " reading gain from " << gainFile_;
0084   std::ifstream fileGain(edm::FileInPath(gainFile_).fullPath());
0085 
0086   int i = 0;
0087   while (!fileGain.eof()) {
0088     uint32_t det;
0089     int chip;
0090 
0091     int d;
0092     float g, f;
0093     std::string s;
0094 
0095     fileGain >> std::hex >> det;
0096     fileGain >> std::dec >> chip;
0097 
0098     ChipId detId(det, (unsigned char)chip);
0099 
0100     fileGain >> std::dec >> d;
0101     fileGain >> g;
0102     fileGain >> f;
0103     fileGain >> s;
0104 
0105     if (!fileGain.eof()) {
0106       if (g > 0.5 && g < 2.0)
0107         gain[detId] = g;
0108       else
0109         gain[detId] = -1.;
0110     }
0111 
0112     if (i++ % 5000 == 0)
0113       std::cout << ".";
0114   }
0115 
0116   fileGain.close();
0117   std::cout << " [done]" << std::endl;
0118 }
0119 
0120 //define this as a plug-in
0121 #include "FWCore/Framework/interface/MakerMacros.h"
0122 DEFINE_FWK_MODULE(DeDxCalibrationDbCreator);