Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-11-17 01:40:48

0001 // -*- C++ -*-
0002 //
0003 // Package:    SiPixelCondObjBuilder
0004 // Class:      SiPixelCondObjBuilder
0005 //
0006 /**\class SiPixelCondObjBuilder SiPixelCondObjBuilder.h SiPixel/test/SiPixelCondObjBuilder.h
0007 
0008  Description: Test analyzer for writing pixel calibration in the DB
0009 
0010  Implementation:
0011      <Notes on implementation>
0012 */
0013 //
0014 // Original Author:  Vincenzo CHIOCHIA
0015 //         Created:  Tue Oct 17 17:40:56 CEST 2006
0016 // $Id: SiPixelCondObjBuilder.h,v 1.9 2009/05/28 22:12:54 dlange Exp $
0017 //
0018 //
0019 
0020 // system includes
0021 #include <memory>
0022 #include <iostream>
0023 #include <string>
0024 
0025 // user includes
0026 #include "CalibTracker/SiPixelESProducers/interface/SiPixelGainCalibrationService.h"
0027 #include "CondCore/DBOutputService/interface/PoolDBOutputService.h"
0028 #include "CondFormats/SiPixelObjects/interface/PixelIndices.h"
0029 #include "FWCore/Framework/interface/Event.h"
0030 #include "FWCore/Framework/interface/EventSetup.h"
0031 #include "FWCore/Framework/interface/Frameworkfwd.h"
0032 #include "FWCore/Framework/interface/MakerMacros.h"
0033 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0034 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0035 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0036 #include "FWCore/ServiceRegistry/interface/Service.h"
0037 #include "Geometry/CommonDetUnit/interface/PixelGeomDetUnit.h"
0038 #include "Geometry/CommonTopologies/interface/PixelTopology.h"
0039 #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
0040 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
0041 
0042 #include "CLHEP/Random/RandGauss.h"
0043 
0044 namespace cms {
0045   class SiPixelCondObjBuilder : public edm::one::EDAnalyzer<> {
0046   public:
0047     explicit SiPixelCondObjBuilder(const edm::ParameterSet& iConfig);
0048 
0049     ~SiPixelCondObjBuilder() override = default;
0050     void beginJob() override;
0051     void analyze(const edm::Event&, const edm::EventSetup&) override;
0052     bool loadFromFile();
0053 
0054   private:
0055     const bool appendMode_;
0056     const edm::ESGetToken<TrackerGeometry, TrackerDigiGeometryRecord> pddToken_;
0057     std::unique_ptr<SiPixelGainCalibration> SiPixelGainCalibration_;
0058     SiPixelGainCalibrationService SiPixelGainCalibrationService_;
0059     const std::string recordName_;
0060 
0061     const double meanPed_;
0062     const double rmsPed_;
0063     const double meanGain_;
0064     const double rmsGain_;
0065     const double secondRocRowGainOffset_;
0066     const double secondRocRowPedOffset_;
0067     const int numberOfModules_;
0068     const bool fromFile_;
0069     const std::string fileName_;
0070 
0071     // Internal class
0072     class CalParameters {
0073     public:
0074       float p0;
0075       float p1;
0076     };
0077     // Map for storing calibration constants
0078     std::map<int, CalParameters, std::less<int> > calmap_;
0079     PixelIndices* pIndexConverter_;  // Pointer to the index converter
0080   };
0081 }  // namespace cms
0082 
0083 namespace cms {
0084   SiPixelCondObjBuilder::SiPixelCondObjBuilder(const edm::ParameterSet& iConfig)
0085       : appendMode_(iConfig.getUntrackedParameter<bool>("appendMode", true)),
0086         pddToken_(esConsumes()),
0087         SiPixelGainCalibration_(nullptr),
0088         SiPixelGainCalibrationService_(iConfig, consumesCollector()),
0089         recordName_(iConfig.getParameter<std::string>("record")),
0090         meanPed_(iConfig.getParameter<double>("meanPed")),
0091         rmsPed_(iConfig.getParameter<double>("rmsPed")),
0092         meanGain_(iConfig.getParameter<double>("meanGain")),
0093         rmsGain_(iConfig.getParameter<double>("rmsGain")),
0094         secondRocRowGainOffset_(iConfig.getParameter<double>("secondRocRowGainOffset")),
0095         secondRocRowPedOffset_(iConfig.getParameter<double>("secondRocRowPedOffset")),
0096         numberOfModules_(iConfig.getParameter<int>("numberOfModules")),
0097         fromFile_(iConfig.getParameter<bool>("fromFile")),
0098         fileName_(iConfig.getParameter<std::string>("fileName")) {
0099     ::putenv((char*)"CORAL_AUTH_USER=me");
0100     ::putenv((char*)"CORAL_AUTH_PASSWORD=test");
0101   }
0102 
0103   void SiPixelCondObjBuilder::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0104     using namespace edm;
0105     unsigned int run = iEvent.id().run();
0106     int nmodules = 0;
0107     uint32_t nchannels = 0;
0108     //    int mycol = 415;
0109     //    int myrow = 159;
0110 
0111     edm::LogInfo("SiPixelCondObjBuilder")
0112         << "... creating dummy SiPixelGainCalibration Data for Run " << run << "\n " << std::endl;
0113     //
0114     // Instantiate Gain calibration offset and define pedestal/gain range
0115     //
0116     float mingain = 0;
0117     float maxgain = 10;
0118     float minped = 0;
0119     float maxped = 255;
0120     SiPixelGainCalibration_ = std::make_unique<SiPixelGainCalibration>(minped, maxped, mingain, maxgain);
0121 
0122     const TrackerGeometry* pDD = &iSetup.getData(pddToken_);
0123     edm::LogInfo("SiPixelCondObjBuilder") << " There are " << pDD->dets().size() << " detectors" << std::endl;
0124 
0125     for (TrackerGeometry::DetContainer::const_iterator it = pDD->dets().begin(); it != pDD->dets().end(); it++) {
0126       if (dynamic_cast<PixelGeomDetUnit const*>((*it)) != nullptr) {
0127         uint32_t detid = ((*it)->geographicalId()).rawId();
0128 
0129         // Stop if module limit reached
0130         nmodules++;
0131         if (nmodules > numberOfModules_)
0132           break;
0133 
0134         const PixelGeomDetUnit* pixDet = dynamic_cast<const PixelGeomDetUnit*>((*it));
0135         const PixelTopology& topol = pixDet->specificTopology();
0136         // Get the module sizes.
0137         int nrows = topol.nrows();     // rows in x
0138         int ncols = topol.ncolumns();  // cols in y
0139         //edm::LogPrint("SiPixelCondObjBuilder") << " ---> PIXEL DETID " << detid << " Cols " << ncols << " Rows " << nrows << std::endl;
0140 
0141         PixelIndices pIndexConverter(ncols, nrows);
0142 
0143         std::vector<char> theSiPixelGainCalibration;
0144 
0145         // Loop over columns and rows of this DetID
0146         for (int i = 0; i < ncols; i++) {
0147           for (int j = 0; j < nrows; j++) {
0148             nchannels++;
0149 
0150             float ped = 0.0, gain = 0.0;
0151 
0152             if (fromFile_) {
0153               // Use calibration from a file
0154               int chipIndex = 0, colROC = 0, rowROC = 0;
0155 
0156               pIndexConverter.transformToROC(i, j, chipIndex, colROC, rowROC);
0157               int chanROC = PixelIndices::pixelToChannelROC(rowROC, colROC);  // use ROC coordinates
0158               //         float pp0=0, pp1=0;
0159               std::map<int, CalParameters, std::less<int> >::const_iterator it = calmap_.find(chanROC);
0160               CalParameters theCalParameters = (*it).second;
0161               ped = theCalParameters.p0;
0162               gain = theCalParameters.p1;
0163 
0164             } else {
0165               if (rmsPed_ > 0) {
0166                 ped = CLHEP::RandGauss::shoot(meanPed_, rmsPed_);
0167                 while (minped > ped || maxped < ped)
0168                   ped = CLHEP::RandGauss::shoot(meanPed_, rmsPed_);
0169 
0170               } else
0171                 ped = meanPed_;
0172               if (rmsGain_ > 0) {
0173                 gain = CLHEP::RandGauss::shoot(meanGain_, rmsGain_);
0174                 while (mingain > gain || maxgain < gain)
0175                   gain = CLHEP::RandGauss::shoot(meanGain_, rmsGain_);
0176               } else
0177                 gain = meanGain_;
0178             }
0179 
0180             //if in the second row of rocs (i.e. a 2xN plaquette) add an offset (if desired) for testing
0181             if (j >= 80) {
0182               ped += secondRocRowPedOffset_;
0183               gain += secondRocRowGainOffset_;
0184 
0185               if (gain > maxgain)
0186                 gain = maxgain;
0187               else if (gain < mingain)
0188                 gain = mingain;
0189 
0190               if (ped > maxped)
0191                 ped = maxped;
0192               else if (ped < minped)
0193                 ped = minped;
0194             }
0195 
0196             //     if(i==mycol && j==myrow) {
0197             //       edm::LogPrint("SiPixelCondObjBuilder") << "       Col "<<i<<" Row "<<j<<" Ped "<<ped<<" Gain "<<gain<<std::endl;
0198             //     }
0199 
0200             //     gain =  2.8;
0201             //     ped  = 28.2;
0202 
0203             // Insert data in the container
0204             SiPixelGainCalibration_->setData(ped, gain, theSiPixelGainCalibration);
0205           }
0206         }
0207 
0208         SiPixelGainCalibration::Range range(theSiPixelGainCalibration.begin(), theSiPixelGainCalibration.end());
0209         if (!SiPixelGainCalibration_->put(detid, range, ncols))
0210           edm::LogError("SiPixelCondObjBuilder")
0211               << "[SiPixelCondObjBuilder::analyze] detid already exists" << std::endl;
0212       }
0213     }
0214     edm::LogPrint("SiPixelCondObjBuilder") << " ---> PIXEL Modules  " << nmodules << std::endl;
0215     edm::LogPrint("SiPixelCondObjBuilder") << " ---> PIXEL Channels " << nchannels << std::endl;
0216 
0217     //   // Try to read object
0218     //    int mynmodules =0;
0219     //    for(TrackerGeometry::DetContainer::const_iterator it = pDD->dets().begin(); it != pDD->dets().end(); it++){
0220     //      if( dynamic_cast<PixelGeomDetUnit const*>((*it))!=0){
0221     //        uint32_t mydetid=((*it)->geographicalId()).rawId();
0222     //        mynmodules++;
0223     //        if( mynmodules > numberOfModules_) break;
0224     //        SiPixelGainCalibration::Range myrange = SiPixelGainCalibration_->getRange(mydetid);
0225     //        float mypedestal = SiPixelGainCalibration_->getPed (mycol,myrow,myrange,416);
0226     //        float mygain     = SiPixelGainCalibration_->getGain(mycol,myrow,myrange,416);
0227     //        //edm::LogPrint("SiPixelCondObjBuilder")<<" PEDESTAL "<< mypedestal<<" GAIN "<<mygain<<std::endl;
0228     //      }
0229     //    }
0230     // Write into DB
0231     edm::LogInfo(" --- writeing to DB!");
0232     edm::Service<cond::service::PoolDBOutputService> mydbservice;
0233     if (!mydbservice.isAvailable()) {
0234       edm::LogPrint("SiPixelCondObjBuilder") << "Didn't get DB" << std::endl;
0235       edm::LogError("db service unavailable");
0236       return;
0237     } else {
0238       edm::LogInfo("DB service OK");
0239     }
0240 
0241     try {
0242       //     size_t callbackToken = mydbservice->callbackToken("SiPixelGainCalibration");
0243       //     edm::LogInfo("SiPixelCondObjBuilder")<<"CallbackToken SiPixelGainCalibration "
0244       //         <<callbackToken<<std::endl;
0245       //       unsigned long long tillTime;
0246       //     if ( appendMode_)
0247       //     tillTime = mydbservice->currentTime();
0248       //       else
0249       //     tillTime = mydbservice->endOfTime();
0250       //     edm::LogInfo("SiPixelCondObjBuilder")<<"[SiPixelCondObjBuilder::analyze] tillTime = "
0251       //         <<tillTime<<std::endl;
0252       //     mydbservice->newValidityForNewPayload<SiPixelGainCalibration>(
0253       //           SiPixelGainCalibration_, tillTime , callbackToken);
0254 
0255       if (mydbservice->isNewTagRequest(recordName_)) {
0256         mydbservice->createOneIOV<SiPixelGainCalibration>(
0257             *SiPixelGainCalibration_, mydbservice->beginOfTime(), recordName_);
0258       } else {
0259         mydbservice->appendOneIOV<SiPixelGainCalibration>(
0260             *SiPixelGainCalibration_, mydbservice->currentTime(), recordName_);
0261       }
0262       edm::LogInfo(" --- all OK");
0263     } catch (const cond::Exception& er) {
0264       edm::LogPrint("SiPixelCondObjBuilder") << "Database exception!   " << er.what() << std::endl;
0265       edm::LogError("SiPixelCondObjBuilder") << er.what() << std::endl;
0266     } catch (const std::exception& er) {
0267       edm::LogPrint("SiPixelCondObjBuilder") << "Standard exception!   " << er.what() << std::endl;
0268       edm::LogError("SiPixelCondObjBuilder") << "caught std::exception " << er.what() << std::endl;
0269     } catch (...) {
0270       edm::LogError("SiPixelCondObjBuilder") << "Funny error" << std::endl;
0271     }
0272   }
0273 
0274   // ------------ method called once each job just before starting event loop  ------------
0275   void SiPixelCondObjBuilder::beginJob() {
0276     if (fromFile_) {
0277       if (loadFromFile()) {
0278         edm::LogInfo("SiPixelCondObjBuilder") << " Calibration loaded: Map size " << calmap_.size() << " max "
0279                                               << calmap_.max_size() << " " << calmap_.empty() << std::endl;
0280       }
0281     }
0282   }
0283 
0284   bool SiPixelCondObjBuilder::loadFromFile() {
0285     float par0, par1;  //,par2,par3;
0286     int colid, rowid;  //rocid
0287     std::string name;
0288 
0289     std::ifstream in_file;                          // data file pointer
0290     in_file.open(fileName_.c_str(), std::ios::in);  // in C++
0291     if (in_file.bad()) {
0292       edm::LogError("SiPixelCondObjBuilder") << "Input file not found" << std::endl;
0293     }
0294     if (in_file.eof() != 0) {
0295       edm::LogError("SiPixelCondObjBuilder") << in_file.eof() << " " << in_file.gcount() << " " << in_file.fail() << " "
0296                                              << in_file.good() << " end of file " << std::endl;
0297       return false;
0298     }
0299     //Load file header
0300     char line[500];
0301     for (int i = 0; i < 3; i++) {
0302       in_file.getline(line, 500, '\n');
0303       edm::LogInfo("SiPixelCondObjBuilder") << line << std::endl;
0304     }
0305     //Loading calibration constants from file, loop on pixels
0306     for (int i = 0; i < (52 * 80); i++) {
0307       in_file >> par0 >> par1 >> name >> colid >> rowid;
0308 
0309       edm::LogPrint("SiPixelCondObjBuilder")
0310           << " Col " << colid << " Row " << rowid << " P0 " << par0 << " P1 " << par1 << std::endl;
0311 
0312       CalParameters onePix;
0313       onePix.p0 = par0;
0314       onePix.p1 = par1;
0315 
0316       // Convert ROC pixel index to channel
0317       int chan = PixelIndices::pixelToChannelROC(rowid, colid);
0318       calmap_.insert(std::pair<int, CalParameters>(chan, onePix));
0319     }
0320 
0321     bool flag;
0322     if (calmap_.size() == 4160) {
0323       flag = true;
0324     } else {
0325       flag = false;
0326     }
0327     return flag;
0328   }
0329 
0330 }  // namespace cms
0331 
0332 using namespace cms;
0333 DEFINE_FWK_MODULE(SiPixelCondObjBuilder);