Back to home page

Project CMSSW displayed by LXR

 
 

    


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   //--- Make the POOL-ORA object to store the database object
0079   SiPixel2DTemplateDBObject obj;
0080 
0081   // Local variables
0082   int m;
0083 
0084   // Set the number of templates to be passed to the dbobject
0085   obj.setNumOfTempl(theTemplateCalibrations.size());
0086   // Set the version of the template dbobject - this is an external parameter
0087   obj.setVersion(theVersion);
0088 
0089   // Open the template file(s)
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       // Local variables
0097       char title_char[80], c;
0098       SiPixel2DTemplateDBObject::char2float temp;
0099       float tempstore;
0100       int iter, j;
0101 
0102       // Templates contain a header char - we must be clever about storing this
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       // Fill the dbobject
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       // If file didn't open, report this
0132       edm::LogError("SiPixel2DTemplateDBObjectUploader") << "Error opening File " << (file.fullPath()).c_str();
0133     }
0134   }
0135 
0136   //get TrackerGeometry from the event setup
0137   const edm::ESHandle<TrackerGeometry> pDD = iSetup.getHandle(trackerGeometryToken_);
0138   const TrackerGeometry* tGeo = &iSetup.getData(trackerGeometryToken_);
0139 
0140   // Use the TrackerTopology class for layer/disk etc. number
0141   const TrackerTopology* tTopo = &iSetup.getData(trackerTopologyToken_);
0142 
0143   // Check if we are using Phase-1 or Phase-2 geometry
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   //Loop over the detector elements and put template IDs in place
0153   for (const auto& it : pDD->detUnits()) {
0154     if (it != nullptr) {
0155       // Here is the actual looping step over all DetIds:
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       // Now we sort them into the Barrel and Endcap:
0163       //Barrel Pixels first
0164       if ((phase == 1 && detid.subdetId() == static_cast<int>(PixelSubdetector::PixelBarrel)) ||
0165           (phase == 2 && tGeo->geomDetSubDetector(detid.subdetId()) == GeomDetEnumerators::P2PXB)) {
0166         //Get the layer, ladder, and module corresponding to this detID
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         //Assign template IDs
0178         //Loop over all the barrel locations
0179         for (iter = 0; iter < theBarrelLocations.size(); ++iter) {
0180           //get the string of this barrel location
0181           std::string loc_string = theBarrelLocations[iter];
0182           //find where the delimiters are
0183           unsigned int first_delim_pos = loc_string.find('_');
0184           unsigned int second_delim_pos = loc_string.find('_', first_delim_pos + 1);
0185           //get the layer, ladder, and module as unsigned ints
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           //check them against the desired layer, ladder, and module
0191           if (ladder == checkladder && layer == checklayer && module == checkmodule)
0192             //if they match, set the template ID
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       //Now endcaps
0203       else if ((phase == 1 && detid.subdetId() == static_cast<int>(PixelSubdetector::PixelEndcap)) ||
0204                (phase == 2 && tGeo->geomDetSubDetector(detid.subdetId()) == GeomDetEnumerators::P2PXEC)) {
0205         //Get the DetId's disk, blade, side, panel, and module
0206         disk = tTopo->pxfDisk(detid.rawId());    //1,2,3
0207         blade = tTopo->pxfBlade(detid.rawId());  //1-56 (Ring 1 is 1-22, Ring 2 is 23-56)
0208         side = tTopo->pxfSide(detid.rawId());    //side=1 for -z, 2 for +z
0209         panel = tTopo->pxfPanel(detid.rawId());  //panel=1,2
0210 
0211         if (useVectorIndices) {
0212           --disk;
0213           --blade;
0214           --side;
0215           --panel;
0216         }
0217 
0218         //Assign IDs
0219 
0220         //Loop over all the endcap locations
0221         for (iter = 0; iter < theEndcapLocations.size(); ++iter) {
0222           //get the string of this barrel location
0223           std::string loc_string = theEndcapLocations[iter];
0224           //find where the delimiters are
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           //get the disk, blade, side, panel, and module as unsigned ints
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           //check them against the desired disk, blade, side, panel, and module
0236           if (disk == checkdisk && blade == checkblade && side == checkside && panel == checkpanel)
0237             //if they match, set the template ID
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       //Print out the assignment of this detID
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   //--- Create a new IOV
0261   edm::Service<cond::service::PoolDBOutputService> poolDbService;
0262   if (!poolDbService.isAvailable())  // Die if not available
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);