File indexing completed on 2024-04-06 12:03:24
0001 #include <cstdio>
0002 #include <fstream>
0003 #include <iostream>
0004 #include <memory>
0005
0006 #include "CondFormats/DataRecord/interface/SiPixelGenErrorDBObjectRcd.h"
0007 #include "CondFormats/SiPixelObjects/interface/SiPixelGenErrorDBObject.h"
0008
0009 #include "CondCore/DBOutputService/interface/PoolDBOutputService.h"
0010 #include "FWCore/ServiceRegistry/interface/Service.h"
0011
0012 #include "DataFormats/SiPixelDetId/interface/PixelSubdetector.h"
0013 #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
0014 #include "Geometry/CommonTopologies/interface/PixelGeomDetUnit.h"
0015 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
0016
0017 #include "DataFormats/DetId/interface/DetId.h"
0018 #include "DataFormats/TrackerCommon/interface/TrackerTopology.h"
0019 #include "DataFormats/TrackerCommon/interface/PixelBarrelName.h"
0020 #include "DataFormats/TrackerCommon/interface/PixelEndcapName.h"
0021
0022 #include "FWCore/Framework/interface/Frameworkfwd.h"
0023 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0024
0025 #include "FWCore/Framework/interface/Event.h"
0026 #include "FWCore/Framework/interface/MakerMacros.h"
0027 #include "FWCore/Framework/interface/ESHandle.h"
0028
0029 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0030 #include "FWCore/Framework/interface/ConsumesCollector.h"
0031 #include "FWCore/Utilities/interface/ESGetToken.h"
0032
0033 class SiPixelGenErrorDBObjectUploader : public edm::one::EDAnalyzer<> {
0034 public:
0035 explicit SiPixelGenErrorDBObjectUploader(const edm::ParameterSet&);
0036 ~SiPixelGenErrorDBObjectUploader() override;
0037
0038 typedef std::vector<std::string> vstring;
0039
0040 private:
0041 void beginJob() override;
0042 void analyze(const edm::Event&, const edm::EventSetup&) override;
0043 void endJob() override;
0044
0045 vstring theGenErrorCalibrations;
0046 std::string theGenErrorBaseString;
0047 float theVersion;
0048 float theMagField;
0049 std::vector<uint32_t> theDetIds;
0050 vstring theBarrelLocations;
0051 vstring theEndcapLocations;
0052 std::vector<uint32_t> theBarrelGenErrIds;
0053 std::vector<uint32_t> theEndcapGenErrIds;
0054 bool useVectorIndices;
0055 edm::ESGetToken<TrackerGeometry, TrackerDigiGeometryRecord> trackerGeometryToken_;
0056 edm::ESGetToken<TrackerTopology, TrackerTopologyRcd> trackerTopologyToken_;
0057 };
0058
0059 SiPixelGenErrorDBObjectUploader::SiPixelGenErrorDBObjectUploader(const edm::ParameterSet& iConfig)
0060 : theGenErrorCalibrations(iConfig.getParameter<vstring>("siPixelGenErrorCalibrations")),
0061 theGenErrorBaseString(iConfig.getParameter<std::string>("theGenErrorBaseString")),
0062 theVersion(iConfig.getParameter<double>("Version")),
0063 theMagField(iConfig.getParameter<double>("MagField")),
0064 theBarrelLocations(iConfig.getParameter<std::vector<std::string> >("barrelLocations")),
0065 theEndcapLocations(iConfig.getParameter<std::vector<std::string> >("endcapLocations")),
0066 theBarrelGenErrIds(iConfig.getParameter<std::vector<uint32_t> >("barrelGenErrIds")),
0067 theEndcapGenErrIds(iConfig.getParameter<std::vector<uint32_t> >("endcapGenErrIds")),
0068 useVectorIndices(iConfig.getUntrackedParameter<bool>("useVectorIndices", false)),
0069 trackerGeometryToken_(esConsumes()),
0070 trackerTopologyToken_(esConsumes()) {}
0071
0072 SiPixelGenErrorDBObjectUploader::~SiPixelGenErrorDBObjectUploader() = default;
0073
0074 void SiPixelGenErrorDBObjectUploader::beginJob() {}
0075
0076 void SiPixelGenErrorDBObjectUploader::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0077
0078 SiPixelGenErrorDBObject obj;
0079
0080
0081 int m;
0082
0083
0084 obj.setNumOfTempl(theGenErrorCalibrations.size());
0085
0086
0087 obj.setVersion(theVersion);
0088
0089
0090 for (m = 0; m < obj.numOfTempl(); ++m) {
0091 edm::FileInPath file(theGenErrorCalibrations[m].c_str());
0092 std::ifstream in_file(file.fullPath().c_str(), std::ios::in);
0093 if (in_file.is_open()) {
0094 edm::LogInfo("GenError Info") << "Opened GenError File: " << file.fullPath().c_str();
0095
0096
0097 char title_char[80], c;
0098 SiPixelGenErrorDBObject::char2float temp;
0099 float tempstore;
0100 int iter, j, k;
0101
0102
0103 for (iter = 0; (c = in_file.get()) != '\n'; ++iter) {
0104 if (iter < 79) {
0105 title_char[iter] = c;
0106 }
0107 }
0108 if (iter > 78) {
0109 iter = 78;
0110 }
0111 title_char[iter + 1] = '\n';
0112
0113 for (j = 0; j < 80; j += 4) {
0114 temp.c[0] = title_char[j];
0115 temp.c[1] = title_char[j + 1];
0116 temp.c[2] = title_char[j + 2];
0117 temp.c[3] = title_char[j + 3];
0118 obj.push_back(temp.f);
0119 obj.setMaxIndex(obj.maxIndex() + 1);
0120 }
0121
0122
0123 for (k = 0; k < 80; k++) {
0124 if ((title_char[k] == '@') && (title_char[k - 1] == 'T')) {
0125 double localMagField = (((int)title_char[k - 4]) - 48) * 10 + ((int)title_char[k - 2]) - 48;
0126 if (theMagField != localMagField) {
0127 edm::LogPrint("SiPixelGenErrorDBObjectUploader")
0128 << "\n -------- WARNING -------- \n Magnetic field in the cfg is " << theMagField << "T while it is "
0129 << title_char[k - 4] << title_char[k - 2] << title_char[k - 1]
0130 << " in the header \n ------------------------- \n " << std::endl;
0131 }
0132 }
0133 }
0134
0135
0136 in_file >> tempstore;
0137 while (!in_file.eof()) {
0138 obj.setMaxIndex(obj.maxIndex() + 1);
0139 obj.push_back(tempstore);
0140 in_file >> tempstore;
0141 }
0142
0143 in_file.close();
0144 } else {
0145
0146 edm::LogError("SiPixelGenErrorDBObjectUploader") << "Error opening File " << file.fullPath().c_str();
0147 }
0148 }
0149
0150
0151 const edm::ESHandle<TrackerGeometry> pDD = iSetup.getHandle(trackerGeometryToken_);
0152 const TrackerGeometry* tGeo = &iSetup.getData(trackerGeometryToken_);
0153
0154
0155 const TrackerTopology* tTopo = &iSetup.getData(trackerTopologyToken_);
0156
0157
0158 int phase = 0;
0159 if (pDD->isThere(GeomDetEnumerators::P1PXB) && pDD->isThere(GeomDetEnumerators::P1PXEC) == true) {
0160 phase = 1;
0161 } else if (pDD->isThere(GeomDetEnumerators::P2PXB) && pDD->isThere(GeomDetEnumerators::P2PXEC) == true) {
0162 phase = 2;
0163 }
0164 edm::LogPrint("SiPixelGenErrorDBObjectUploader") << "Phase-" << phase << " geometry is used" << std::endl;
0165
0166
0167 for (const auto& it : pDD->detUnits()) {
0168 if (it != nullptr) {
0169
0170 DetId detid = it->geographicalId();
0171 unsigned int layer = 0, ladder = 0, disk = 0, side = 0, blade = 0, panel = 0, module = 0;
0172
0173
0174 short thisID = 10000;
0175 unsigned int iter;
0176
0177
0178
0179 if ((phase == 1 && detid.subdetId() == static_cast<int>(PixelSubdetector::PixelBarrel)) ||
0180 (phase == 2 && tGeo->geomDetSubDetector(detid.subdetId()) == GeomDetEnumerators::P2PXB)) {
0181
0182 layer = tTopo->pxbLayer(detid.rawId());
0183 ladder = tTopo->pxbLadder(detid.rawId());
0184 module = tTopo->pxbModule(detid.rawId());
0185
0186 if (useVectorIndices) {
0187 --layer;
0188 --ladder;
0189 --module;
0190 }
0191
0192
0193
0194 for (iter = 0; iter < theBarrelLocations.size(); ++iter) {
0195
0196 std::string loc_string = theBarrelLocations[iter];
0197
0198 unsigned int first_delim_pos = loc_string.find('_');
0199 unsigned int second_delim_pos = loc_string.find('_', first_delim_pos + 1);
0200
0201 unsigned int checklayer = (unsigned int)stoi(loc_string.substr(0, first_delim_pos));
0202 unsigned int checkladder =
0203 (unsigned int)stoi(loc_string.substr(first_delim_pos + 1, second_delim_pos - first_delim_pos - 1));
0204 unsigned int checkmodule = (unsigned int)stoi(loc_string.substr(second_delim_pos + 1, 5));
0205
0206 if (ladder == checkladder && layer == checklayer && module == checkmodule)
0207
0208 thisID = (short)theBarrelGenErrIds[iter];
0209 }
0210
0211 if (thisID == 10000 || (!obj.putGenErrorID(detid.rawId(), thisID)))
0212 edm::LogPrint("SiPixelGenErrorDBObjectUploader")
0213 << " Could not fill barrel layer " << layer << ", module " << module << "\n";
0214 edm::LogPrint("SiPixelGenErrorDBObjectUploader")
0215 << "This is a barrel element with: layer " << layer << ", ladder " << ladder << " and module " << module;
0216 }
0217
0218 else if ((phase == 1 && detid.subdetId() == static_cast<int>(PixelSubdetector::PixelEndcap)) ||
0219 (phase == 2 && tGeo->geomDetSubDetector(detid.subdetId()) == GeomDetEnumerators::P2PXEC)) {
0220
0221 disk = tTopo->pxfDisk(detid.rawId());
0222 blade = tTopo->pxfBlade(detid.rawId());
0223 side = tTopo->pxfSide(detid.rawId());
0224 panel = tTopo->pxfPanel(detid.rawId());
0225
0226 if (useVectorIndices) {
0227 --disk;
0228 --blade;
0229 --side;
0230 --panel;
0231 }
0232
0233
0234
0235 for (iter = 0; iter < theEndcapLocations.size(); ++iter) {
0236
0237 std::string loc_string = theEndcapLocations[iter];
0238
0239 unsigned int first_delim_pos = loc_string.find('_');
0240 unsigned int second_delim_pos = loc_string.find('_', first_delim_pos + 1);
0241 unsigned int third_delim_pos = loc_string.find('_', second_delim_pos + 1);
0242
0243 unsigned int checkdisk = (unsigned int)stoi(loc_string.substr(0, first_delim_pos));
0244 unsigned int checkblade =
0245 (unsigned int)stoi(loc_string.substr(first_delim_pos + 1, second_delim_pos - first_delim_pos - 1));
0246 unsigned int checkside =
0247 (unsigned int)stoi(loc_string.substr(second_delim_pos + 1, third_delim_pos - second_delim_pos - 1));
0248 unsigned int checkpanel = (unsigned int)stoi(loc_string.substr(third_delim_pos + 1, 5));
0249
0250 if (disk == checkdisk && blade == checkblade && side == checkside && panel == checkpanel)
0251
0252 thisID = (short)theEndcapGenErrIds[iter];
0253 }
0254
0255 if (thisID == 10000 || (!obj.putGenErrorID(detid.rawId(), thisID)))
0256 edm::LogPrint("SiPixelGenErrorDBObjectUploader")
0257 << " Could not fill endcap det unit" << side << ", disk " << disk << ", blade " << blade << ", panel "
0258 << panel << ".\n";
0259 edm::LogPrint("SiPixelGenErrorDBObjectUploader") << "This is an endcap element with: side " << side << ", disk "
0260 << disk << ", blade " << blade << ", panel " << panel;
0261 } else {
0262 continue;
0263 }
0264
0265
0266 short mapnum;
0267 mapnum = obj.getGenErrorID(detid.rawId());
0268 edm::LogPrint("SiPixelGenErrorDBObjectUploader")
0269 << "The DetID: " << detid.rawId() << " is mapped to the template: " << mapnum << "\n";
0270 }
0271 }
0272
0273
0274 edm::Service<cond::service::PoolDBOutputService> poolDbService;
0275 if (!poolDbService.isAvailable())
0276 throw cms::Exception("NotAvailable") << "PoolDBOutputService not available";
0277 if (poolDbService->isNewTagRequest("SiPixelGenErrorDBObjectRcd"))
0278 poolDbService->writeOneIOV(obj, poolDbService->beginOfTime(), "SiPixelGenErrorDBObjectRcd");
0279 else
0280 poolDbService->writeOneIOV(obj, poolDbService->currentTime(), "SiPixelGenErrorDBObjectRcd");
0281 }
0282
0283 void SiPixelGenErrorDBObjectUploader::endJob() {}
0284
0285 DEFINE_FWK_MODULE(SiPixelGenErrorDBObjectUploader);