File indexing completed on 2024-04-06 12:03:22
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021 #include <memory>
0022 #include <iostream>
0023 #include <string>
0024
0025
0026 #include "CalibTracker/SiPixelESProducers/interface/SiPixelGainCalibrationService.h"
0027 #include "CondCore/DBOutputService/interface/PoolDBOutputService.h"
0028 #include "CondFormats/SiPixelObjects/interface/PixelIndices.h"
0029 #include "FWCore/Framework/interface/Event.h"
0030 #include "FWCore/Framework/interface/EventSetup.h"
0031 #include "FWCore/Framework/interface/Frameworkfwd.h"
0032 #include "FWCore/Framework/interface/MakerMacros.h"
0033 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0034 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0035 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0036 #include "FWCore/ServiceRegistry/interface/Service.h"
0037 #include "Geometry/CommonDetUnit/interface/PixelGeomDetUnit.h"
0038 #include "Geometry/CommonTopologies/interface/PixelTopology.h"
0039 #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
0040 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
0041
0042 #include "CLHEP/Random/RandGauss.h"
0043
0044 namespace cms {
0045 class SiPixelCondObjBuilder : public edm::one::EDAnalyzer<> {
0046 public:
0047 explicit SiPixelCondObjBuilder(const edm::ParameterSet& iConfig);
0048
0049 ~SiPixelCondObjBuilder() override = default;
0050 void beginJob() override;
0051 void analyze(const edm::Event&, const edm::EventSetup&) override;
0052 bool loadFromFile();
0053
0054 private:
0055 const bool appendMode_;
0056 const edm::ESGetToken<TrackerGeometry, TrackerDigiGeometryRecord> pddToken_;
0057 std::unique_ptr<SiPixelGainCalibration> SiPixelGainCalibration_;
0058 SiPixelGainCalibrationService SiPixelGainCalibrationService_;
0059 const std::string recordName_;
0060
0061 const double meanPed_;
0062 const double rmsPed_;
0063 const double meanGain_;
0064 const double rmsGain_;
0065 const double secondRocRowGainOffset_;
0066 const double secondRocRowPedOffset_;
0067 const int numberOfModules_;
0068 const bool fromFile_;
0069 const std::string fileName_;
0070
0071
0072 class CalParameters {
0073 public:
0074 float p0;
0075 float p1;
0076 };
0077
0078 std::map<int, CalParameters, std::less<int> > calmap_;
0079 PixelIndices* pIndexConverter_;
0080 };
0081 }
0082
0083 namespace cms {
0084 SiPixelCondObjBuilder::SiPixelCondObjBuilder(const edm::ParameterSet& iConfig)
0085 : appendMode_(iConfig.getUntrackedParameter<bool>("appendMode", true)),
0086 pddToken_(esConsumes()),
0087 SiPixelGainCalibration_(nullptr),
0088 SiPixelGainCalibrationService_(iConfig, consumesCollector()),
0089 recordName_(iConfig.getParameter<std::string>("record")),
0090 meanPed_(iConfig.getParameter<double>("meanPed")),
0091 rmsPed_(iConfig.getParameter<double>("rmsPed")),
0092 meanGain_(iConfig.getParameter<double>("meanGain")),
0093 rmsGain_(iConfig.getParameter<double>("rmsGain")),
0094 secondRocRowGainOffset_(iConfig.getParameter<double>("secondRocRowGainOffset")),
0095 secondRocRowPedOffset_(iConfig.getParameter<double>("secondRocRowPedOffset")),
0096 numberOfModules_(iConfig.getParameter<int>("numberOfModules")),
0097 fromFile_(iConfig.getParameter<bool>("fromFile")),
0098 fileName_(iConfig.getParameter<std::string>("fileName")) {
0099 ::putenv((char*)"CORAL_AUTH_USER=me");
0100 ::putenv((char*)"CORAL_AUTH_PASSWORD=test");
0101 }
0102
0103 void SiPixelCondObjBuilder::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0104 using namespace edm;
0105 unsigned int run = iEvent.id().run();
0106 int nmodules = 0;
0107 uint32_t nchannels = 0;
0108
0109
0110
0111 edm::LogInfo("SiPixelCondObjBuilder")
0112 << "... creating dummy SiPixelGainCalibration Data for Run " << run << "\n " << std::endl;
0113
0114
0115
0116 float mingain = 0;
0117 float maxgain = 10;
0118 float minped = 0;
0119 float maxped = 255;
0120 SiPixelGainCalibration_ = std::make_unique<SiPixelGainCalibration>(minped, maxped, mingain, maxgain);
0121
0122 const TrackerGeometry* pDD = &iSetup.getData(pddToken_);
0123 edm::LogInfo("SiPixelCondObjBuilder") << " There are " << pDD->dets().size() << " detectors" << std::endl;
0124
0125 for (TrackerGeometry::DetContainer::const_iterator it = pDD->dets().begin(); it != pDD->dets().end(); it++) {
0126 if (dynamic_cast<PixelGeomDetUnit const*>((*it)) != nullptr) {
0127 uint32_t detid = ((*it)->geographicalId()).rawId();
0128
0129
0130 nmodules++;
0131 if (nmodules > numberOfModules_)
0132 break;
0133
0134 const PixelGeomDetUnit* pixDet = dynamic_cast<const PixelGeomDetUnit*>((*it));
0135 const PixelTopology& topol = pixDet->specificTopology();
0136
0137 int nrows = topol.nrows();
0138 int ncols = topol.ncolumns();
0139
0140
0141 PixelIndices pIndexConverter(ncols, nrows);
0142
0143 std::vector<char> theSiPixelGainCalibration;
0144
0145
0146 for (int i = 0; i < ncols; i++) {
0147 for (int j = 0; j < nrows; j++) {
0148 nchannels++;
0149
0150 float ped = 0.0, gain = 0.0;
0151
0152 if (fromFile_) {
0153
0154 int chipIndex = 0, colROC = 0, rowROC = 0;
0155
0156 pIndexConverter.transformToROC(i, j, chipIndex, colROC, rowROC);
0157 int chanROC = PixelIndices::pixelToChannelROC(rowROC, colROC);
0158
0159 std::map<int, CalParameters, std::less<int> >::const_iterator it = calmap_.find(chanROC);
0160 CalParameters theCalParameters = (*it).second;
0161 ped = theCalParameters.p0;
0162 gain = theCalParameters.p1;
0163
0164 } else {
0165 if (rmsPed_ > 0) {
0166 ped = CLHEP::RandGauss::shoot(meanPed_, rmsPed_);
0167 while (minped > ped || maxped < ped)
0168 ped = CLHEP::RandGauss::shoot(meanPed_, rmsPed_);
0169
0170 } else
0171 ped = meanPed_;
0172 if (rmsGain_ > 0) {
0173 gain = CLHEP::RandGauss::shoot(meanGain_, rmsGain_);
0174 while (mingain > gain || maxgain < gain)
0175 gain = CLHEP::RandGauss::shoot(meanGain_, rmsGain_);
0176 } else
0177 gain = meanGain_;
0178 }
0179
0180
0181 if (j >= 80) {
0182 ped += secondRocRowPedOffset_;
0183 gain += secondRocRowGainOffset_;
0184
0185 if (gain > maxgain)
0186 gain = maxgain;
0187 else if (gain < mingain)
0188 gain = mingain;
0189
0190 if (ped > maxped)
0191 ped = maxped;
0192 else if (ped < minped)
0193 ped = minped;
0194 }
0195
0196
0197
0198
0199
0200
0201
0202
0203
0204 SiPixelGainCalibration_->setData(ped, gain, theSiPixelGainCalibration);
0205 }
0206 }
0207
0208 SiPixelGainCalibration::Range range(theSiPixelGainCalibration.begin(), theSiPixelGainCalibration.end());
0209 if (!SiPixelGainCalibration_->put(detid, range, ncols))
0210 edm::LogError("SiPixelCondObjBuilder")
0211 << "[SiPixelCondObjBuilder::analyze] detid already exists" << std::endl;
0212 }
0213 }
0214 edm::LogPrint("SiPixelCondObjBuilder") << " ---> PIXEL Modules " << nmodules << std::endl;
0215 edm::LogPrint("SiPixelCondObjBuilder") << " ---> PIXEL Channels " << nchannels << std::endl;
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231 edm::LogInfo(" --- writeing to DB!");
0232 edm::Service<cond::service::PoolDBOutputService> mydbservice;
0233 if (!mydbservice.isAvailable()) {
0234 edm::LogPrint("SiPixelCondObjBuilder") << "Didn't get DB" << std::endl;
0235 edm::LogError("db service unavailable");
0236 return;
0237 } else {
0238 edm::LogInfo("DB service OK");
0239 }
0240
0241 try {
0242
0243
0244
0245
0246
0247
0248
0249
0250
0251
0252
0253
0254
0255 if (mydbservice->isNewTagRequest(recordName_)) {
0256 mydbservice->createOneIOV<SiPixelGainCalibration>(
0257 *SiPixelGainCalibration_, mydbservice->beginOfTime(), recordName_);
0258 } else {
0259 mydbservice->appendOneIOV<SiPixelGainCalibration>(
0260 *SiPixelGainCalibration_, mydbservice->currentTime(), recordName_);
0261 }
0262 edm::LogInfo(" --- all OK");
0263 } catch (const cond::Exception& er) {
0264 edm::LogPrint("SiPixelCondObjBuilder") << "Database exception! " << er.what() << std::endl;
0265 edm::LogError("SiPixelCondObjBuilder") << er.what() << std::endl;
0266 } catch (const std::exception& er) {
0267 edm::LogPrint("SiPixelCondObjBuilder") << "Standard exception! " << er.what() << std::endl;
0268 edm::LogError("SiPixelCondObjBuilder") << "caught std::exception " << er.what() << std::endl;
0269 } catch (...) {
0270 edm::LogError("SiPixelCondObjBuilder") << "Funny error" << std::endl;
0271 }
0272 }
0273
0274
0275 void SiPixelCondObjBuilder::beginJob() {
0276 if (fromFile_) {
0277 if (loadFromFile()) {
0278 edm::LogInfo("SiPixelCondObjBuilder") << " Calibration loaded: Map size " << calmap_.size() << " max "
0279 << calmap_.max_size() << " " << calmap_.empty() << std::endl;
0280 }
0281 }
0282 }
0283
0284 bool SiPixelCondObjBuilder::loadFromFile() {
0285 float par0, par1;
0286 int colid, rowid;
0287 std::string name;
0288
0289 std::ifstream in_file;
0290 in_file.open(fileName_.c_str(), std::ios::in);
0291 if (in_file.bad()) {
0292 edm::LogError("SiPixelCondObjBuilder") << "Input file not found" << std::endl;
0293 }
0294 if (in_file.eof() != 0) {
0295 edm::LogError("SiPixelCondObjBuilder") << in_file.eof() << " " << in_file.gcount() << " " << in_file.fail() << " "
0296 << in_file.good() << " end of file " << std::endl;
0297 return false;
0298 }
0299
0300 char line[500];
0301 for (int i = 0; i < 3; i++) {
0302 in_file.getline(line, 500, '\n');
0303 edm::LogInfo("SiPixelCondObjBuilder") << line << std::endl;
0304 }
0305
0306 for (int i = 0; i < (52 * 80); i++) {
0307 in_file >> par0 >> par1 >> name >> colid >> rowid;
0308
0309 edm::LogPrint("SiPixelCondObjBuilder")
0310 << " Col " << colid << " Row " << rowid << " P0 " << par0 << " P1 " << par1 << std::endl;
0311
0312 CalParameters onePix;
0313 onePix.p0 = par0;
0314 onePix.p1 = par1;
0315
0316
0317 int chan = PixelIndices::pixelToChannelROC(rowid, colid);
0318 calmap_.insert(std::pair<int, CalParameters>(chan, onePix));
0319 }
0320
0321 bool flag;
0322 if (calmap_.size() == 4160) {
0323 flag = true;
0324 } else {
0325 flag = false;
0326 }
0327 return flag;
0328 }
0329
0330 }
0331
0332 using namespace cms;
0333 DEFINE_FWK_MODULE(SiPixelCondObjBuilder);