Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-11-13 03:30:49

0001 #include <cstdio>
0002 #include <fstream>
0003 #include <iostream>
0004 #include <memory>
0005 
0006 #include "CondFormats/DataRecord/interface/SiPixelTemplateDBObjectRcd.h"
0007 #include "CondFormats/SiPixelObjects/interface/SiPixelTemplateDBObject.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 SiPixelTemplateDBObjectUploader : public edm::one::EDAnalyzer<> {
0035 public:
0036   explicit SiPixelTemplateDBObjectUploader(const edm::ParameterSet&);
0037   ~SiPixelTemplateDBObjectUploader() 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 SiPixelTemplateDBObjectUploader::SiPixelTemplateDBObjectUploader(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 SiPixelTemplateDBObjectUploader::~SiPixelTemplateDBObjectUploader() = default;
0074 
0075 void SiPixelTemplateDBObjectUploader::beginJob() {}
0076 
0077 void SiPixelTemplateDBObjectUploader::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0078   //--- Make the POOL-ORA object to store the database object
0079   SiPixelTemplateDBObject obj;
0080 
0081   // Set the number of templates to be passed to the dbobject
0082   obj.setNumOfTempl(theTemplateCalibrations.size());
0083 
0084   // Set the version of the template dbobject - this is an external parameter
0085   obj.setVersion(theVersion);
0086 
0087   // Open the template file(s)
0088   for (int m = 0; m < obj.numOfTempl(); ++m) {
0089     edm::FileInPath file(theTemplateCalibrations[m].c_str());
0090     std::ifstream in_file(file.fullPath().c_str(), std::ios::in);
0091     if (in_file.is_open()) {
0092       edm::LogInfo("SiPixelTemplateDBObjectUploader") << "Opened Template File: " << file.fullPath().c_str();
0093 
0094       // Local variables
0095       char title_char[80], c;
0096       SiPixelTemplateDBObject::char2float temp;
0097       float tempstore;
0098       int iter, j, k;
0099 
0100       // Templates contain a header char - we must be clever about storing this
0101       for (iter = 0; (c = in_file.get()) != '\n'; ++iter) {
0102         if (iter < 79) {
0103           title_char[iter] = c;
0104         }
0105       }
0106       if (iter > 78) {
0107         iter = 78;
0108       }
0109       title_char[iter + 1] = '\n';
0110       for (j = 0; j < 80; j += 4) {
0111         temp.c[0] = title_char[j];
0112         temp.c[1] = title_char[j + 1];
0113         temp.c[2] = title_char[j + 2];
0114         temp.c[3] = title_char[j + 3];
0115         obj.push_back(temp.f);
0116         obj.setMaxIndex(obj.maxIndex() + 1);
0117       }
0118 
0119       // Check if the magnetic field is the same as in the header of the input files
0120       for (k = 0; k < 80; k++) {
0121         if ((title_char[k] == '@') && (title_char[k - 1] == 'T')) {
0122           double localMagField = (((int)title_char[k - 4]) - 48) * 10 + ((int)title_char[k - 2]) - 48;
0123           if (theMagField != localMagField) {
0124             edm::LogPrint("SiPixelTemplateDBObjectUploader")
0125                 << "\n -------- WARNING -------- \n Magnetic field in the cfg is " << theMagField << "T while it is "
0126                 << title_char[k - 4] << title_char[k - 2] << title_char[k - 1]
0127                 << " in the header \n ------------------------- \n " << std::endl;
0128           }
0129         }
0130       }
0131 
0132       // Fill the dbobject
0133       in_file >> tempstore;
0134       while (!in_file.eof()) {
0135         obj.setMaxIndex(obj.maxIndex() + 1);
0136         obj.push_back(tempstore);
0137         in_file >> tempstore;
0138       }
0139 
0140       in_file.close();
0141     } else {
0142       // If file didn't open, report this
0143       edm::LogError("SiPixelTemplateDBObjectUploader") << "Error opening File: " << file.fullPath().c_str();
0144     }
0145   }
0146 
0147   //get TrackerGeometry from the event setup
0148   const edm::ESHandle<TrackerGeometry> pDD = iSetup.getHandle(trackerGeometryToken_);
0149   const TrackerGeometry* tGeo = &iSetup.getData(trackerGeometryToken_);
0150 
0151   // Use the TrackerTopology class for layer/disk etc. number
0152   const TrackerTopology* tTopo = &iSetup.getData(trackerTopologyToken_);
0153 
0154   // Check if we are using Phase-1 or Phase-2 geometry
0155   int phase = 0;
0156   if (pDD->isThere(GeomDetEnumerators::P1PXB) && pDD->isThere(GeomDetEnumerators::P1PXEC) == true) {
0157     phase = 1;
0158   } else if (pDD->isThere(GeomDetEnumerators::P2PXB) && pDD->isThere(GeomDetEnumerators::P2PXEC) == true) {
0159     phase = 2;
0160   }
0161   edm::LogPrint("SiPixelTemplateDBObjectUploader") << "Phase-" << phase << " geometry is used \n" << std::endl;
0162 
0163   //Loop over the detector elements and put template IDs in place
0164   for (const auto& it : pDD->detUnits()) {
0165     if (it != nullptr) {
0166       // Here is the actual looping step over all DetIds:
0167       DetId detid = it->geographicalId();
0168       unsigned int layer = 0, ladder = 0, disk = 0, side = 0, blade = 0, panel = 0, module = 0;
0169       short thisID = 10000;
0170       unsigned int iter;
0171 
0172       // Now we sort them into the Barrel and Endcap:
0173       //Barrel Pixels first
0174       if ((phase == 1 && detid.subdetId() == static_cast<int>(PixelSubdetector::PixelBarrel)) ||
0175           (phase == 2 && tGeo->geomDetSubDetector(detid.subdetId()) == GeomDetEnumerators::P2PXB)) {
0176         //Get the layer, ladder, and module corresponding to this detID
0177         layer = tTopo->pxbLayer(detid.rawId());
0178         ladder = tTopo->pxbLadder(detid.rawId());
0179         module = tTopo->pxbModule(detid.rawId());
0180 
0181         if (useVectorIndices) {
0182           --layer;
0183           --ladder;
0184           --module;
0185         }
0186 
0187         //Assign template IDs
0188         //Loop over all the barrel locations
0189         for (iter = 0; iter < theBarrelLocations.size(); ++iter) {
0190           //get the string of this barrel location
0191           std::string loc_string = theBarrelLocations[iter];
0192           //find where the delimiters are
0193           unsigned int first_delim_pos = loc_string.find('_');
0194           unsigned int second_delim_pos = loc_string.find('_', first_delim_pos + 1);
0195           //get the layer, ladder, and module as unsigned ints
0196           unsigned int checklayer = (unsigned int)stoi(loc_string.substr(0, first_delim_pos));
0197           unsigned int checkladder =
0198               (unsigned int)stoi(loc_string.substr(first_delim_pos + 1, second_delim_pos - first_delim_pos - 1));
0199           unsigned int checkmodule = (unsigned int)stoi(loc_string.substr(second_delim_pos + 1, 5));
0200           //check them against the desired layer, ladder, and module
0201           if (ladder == checkladder && layer == checklayer && module == checkmodule)
0202             //if they match, set the template ID
0203             thisID = (short)theBarrelTemplateIds[iter];
0204         }
0205 
0206         if (thisID == 10000 || (!obj.putTemplateID(detid.rawId(), thisID)))
0207           edm::LogPrint("SiPixelTemplateDBObjectUploader")
0208               << " Could not fill barrel layer " << layer << ", module " << module << "\n";
0209         edm::LogPrint("SiPixelTemplateDBObjectUploader")
0210             << "This is a barrel element with: layer " << layer << ", ladder " << ladder << " and module " << module;
0211       }
0212       //Now endcaps
0213       else if ((phase == 1 && detid.subdetId() == static_cast<int>(PixelSubdetector::PixelEndcap)) ||
0214                (phase == 2 && tGeo->geomDetSubDetector(detid.subdetId()) == GeomDetEnumerators::P2PXEC)) {
0215         //Get the DetId's disk, blade, side, panel, and module
0216         disk = tTopo->pxfDisk(detid.rawId());    //1,2,3
0217         blade = tTopo->pxfBlade(detid.rawId());  //1-56 (Ring 1 is 1-22, Ring 2 is 23-56)
0218         side = tTopo->pxfSide(detid.rawId());    //side=1 for -z, 2 for +z
0219         panel = tTopo->pxfPanel(detid.rawId());  //panel=1,2
0220 
0221         if (useVectorIndices) {
0222           --disk;
0223           --blade;
0224           --side;
0225           --panel;
0226         }
0227 
0228         //Assign IDs
0229         //Loop over all the endcap locations
0230         for (iter = 0; iter < theEndcapLocations.size(); ++iter) {
0231           //get the string of this barrel location
0232           std::string loc_string = theEndcapLocations[iter];
0233           //find where the delimiters are
0234           unsigned int first_delim_pos = loc_string.find('_');
0235           unsigned int second_delim_pos = loc_string.find('_', first_delim_pos + 1);
0236           unsigned int third_delim_pos = loc_string.find('_', second_delim_pos + 1);
0237           //get the disk, blade, side, panel, and module as unsigned ints
0238           unsigned int checkdisk = (unsigned int)stoi(loc_string.substr(0, first_delim_pos));
0239           unsigned int checkblade =
0240               (unsigned int)stoi(loc_string.substr(first_delim_pos + 1, second_delim_pos - first_delim_pos - 1));
0241           unsigned int checkside =
0242               (unsigned int)stoi(loc_string.substr(second_delim_pos + 1, third_delim_pos - second_delim_pos - 1));
0243           unsigned int checkpanel = (unsigned int)stoi(loc_string.substr(third_delim_pos + 1, 5));
0244           //check them against the desired disk, blade, side, panel, and module
0245           if (disk == checkdisk && blade == checkblade && side == checkside && panel == checkpanel)
0246             //if they match, set the template ID
0247             thisID = (short)theEndcapTemplateIds[iter];
0248         }
0249 
0250         if (thisID == 10000 || (!obj.putTemplateID(detid.rawId(), thisID)))
0251           edm::LogPrint("SiPixelTemplateDBObjectUploader")
0252               << " Could not fill endcap det unit" << side << ", disk " << disk << ", blade " << blade << ", and panel "
0253               << panel << ".\n";
0254         edm::LogPrint("SiPixelTemplateDBObjectUploader") << "This is an endcap element with: side " << side << ", disk "
0255                                                          << disk << ", blade " << blade << ", and panel " << panel;
0256       } else {
0257         continue;
0258       }
0259 
0260       //Print out the assignment of this detID
0261       short mapnum;
0262       mapnum = obj.getTemplateID(detid.rawId());
0263       edm::LogPrint("SiPixelTemplateDBObjectUploader")
0264           << "The DetID: " << detid.rawId() << " is mapped to the template: " << mapnum << "\n";
0265     }
0266   }
0267 
0268   //--- Create a new IOV
0269   edm::Service<cond::service::PoolDBOutputService> poolDbService;
0270   if (!poolDbService.isAvailable())  // Die if not available
0271     throw cms::Exception("NotAvailable") << "PoolDBOutputService not available";
0272   if (poolDbService->isNewTagRequest("SiPixelTemplateDBObjectRcd"))
0273     poolDbService->writeOneIOV(obj, poolDbService->beginOfTime(), "SiPixelTemplateDBObjectRcd");
0274   else
0275     poolDbService->writeOneIOV(obj, poolDbService->currentTime(), "SiPixelTemplateDBObjectRcd");
0276 }
0277 
0278 void SiPixelTemplateDBObjectUploader::endJob() {}
0279 
0280 DEFINE_FWK_MODULE(SiPixelTemplateDBObjectUploader);