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