Back to home page

Project CMSSW displayed by LXR

 
 

    


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   //--- Make the POOL-ORA object to store the database object
0078   SiPixelGenErrorDBObject obj;
0079 
0080   // Local variables
0081   int m;
0082 
0083   // Set the number of GenErrors to be passed to the dbobject
0084   obj.setNumOfTempl(theGenErrorCalibrations.size());
0085 
0086   // Set the version of the GenError dbobject - this is an external parameter
0087   obj.setVersion(theVersion);
0088 
0089   // Open the GenError file(s)
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       // Local variables
0097       char title_char[80], c;
0098       SiPixelGenErrorDBObject::char2float temp;
0099       float tempstore;
0100       int iter, j, k;
0101 
0102       // GenErrors 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 
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       // Check if the magnetic field is the same as in the header of the input files
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       // Fill the dbobject
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       // If file didn't open, report this
0146       edm::LogError("SiPixelGenErrorDBObjectUploader") << "Error opening File " << file.fullPath().c_str();
0147     }
0148   }
0149 
0150   //get TrackerGeometry from the event setup
0151   const edm::ESHandle<TrackerGeometry> pDD = iSetup.getHandle(trackerGeometryToken_);
0152   const TrackerGeometry* tGeo = &iSetup.getData(trackerGeometryToken_);
0153 
0154   // Use the TrackerTopology class for layer/disk etc. number
0155   const TrackerTopology* tTopo = &iSetup.getData(trackerTopologyToken_);
0156 
0157   // Check if we are using Phase-1 or Phase-2 geometry
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   //Loop over the detector elements and put the GenError IDs in place
0167   for (const auto& it : pDD->detUnits()) {
0168     if (it != nullptr) {
0169       // Here is the actual looping step over all DetIds:
0170       DetId detid = it->geographicalId();
0171       unsigned int layer = 0, ladder = 0, disk = 0, side = 0, blade = 0, panel = 0, module = 0;
0172       // Some extra variables that can be used for Phase 1 - comment in if needed
0173       // unsigned int shl=0, sec=0, half=0, flipped=0, ring=0;
0174       short thisID = 10000;
0175       unsigned int iter;
0176 
0177       // Now we sort them into the Barrel and Endcap:
0178       //Barrel Pixels first
0179       if ((phase == 1 && detid.subdetId() == static_cast<int>(PixelSubdetector::PixelBarrel)) ||
0180           (phase == 2 && tGeo->geomDetSubDetector(detid.subdetId()) == GeomDetEnumerators::P2PXB)) {
0181         //Get the layer, ladder, and module corresponding to this DetID
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         //Assign template IDs
0193         //Loop over all the barrel locations
0194         for (iter = 0; iter < theBarrelLocations.size(); ++iter) {
0195           //get the string of this barrel location
0196           std::string loc_string = theBarrelLocations[iter];
0197           //find where the delimiters are
0198           unsigned int first_delim_pos = loc_string.find('_');
0199           unsigned int second_delim_pos = loc_string.find('_', first_delim_pos + 1);
0200           //get the layer, ladder, and module as unsigned ints
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           //check them against the desired layer, ladder, and module
0206           if (ladder == checkladder && layer == checklayer && module == checkmodule)
0207             //if they match, set the template ID
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       //Now endcaps
0218       else if ((phase == 1 && detid.subdetId() == static_cast<int>(PixelSubdetector::PixelEndcap)) ||
0219                (phase == 2 && tGeo->geomDetSubDetector(detid.subdetId()) == GeomDetEnumerators::P2PXEC)) {
0220         //Get the DetID's disk, blade, side, panel, and module
0221         disk = tTopo->pxfDisk(detid.rawId());    //1,2,3
0222         blade = tTopo->pxfBlade(detid.rawId());  //1-56 (Ring 1 is 1-22, Ring 2 is 23-56)
0223         side = tTopo->pxfSide(detid.rawId());    //side=1 for -z, 2 for +z
0224         panel = tTopo->pxfPanel(detid.rawId());  //panel=1,2
0225 
0226         if (useVectorIndices) {
0227           --disk;
0228           --blade;
0229           --side;
0230           --panel;
0231         }
0232 
0233         //Assign IDs
0234         //Loop over all the endcap locations
0235         for (iter = 0; iter < theEndcapLocations.size(); ++iter) {
0236           //get the string of this barrel location
0237           std::string loc_string = theEndcapLocations[iter];
0238           //find where the delimiters are
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           //get the disk, blade, side, panel, and module as unsigned ints
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           //check them against the desired disk, blade, side, panel, and module
0250           if (disk == checkdisk && blade == checkblade && side == checkside && panel == checkpanel)
0251             //if they match, set the template ID
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       //Print out the assignment of this DetID
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   //--- Create a new IOV
0274   edm::Service<cond::service::PoolDBOutputService> poolDbService;
0275   if (!poolDbService.isAvailable())  // Die if not available
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);