Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-09-27 04:04:15

0001 // -*- C++ -*-
0002 //
0003 // Package:    SiPixelOfflineCalibAnalysisBase
0004 // Class:      SiPixelOfflineCalibAnalysisBase
0005 //
0006 /**\class SiPixelOfflineCalibAnalysisBase SiPixelOfflineCalibAnalysisBase.cc CalibTracker/SiPixelTools/src/SiPixelOfflineCalibAnalysisBase.cc
0007 
0008  Description: Base class for Pixel calibrations
0009 
0010  Implementation:
0011      <Notes on implementation>
0012 */
0013 //
0014 // Original Author:  Evan Klose Friis
0015 //    additions by:  Freya Blekman
0016 //         Created:  Tue Nov  6 17:27:19 CET 2007
0017 //
0018 //
0019 
0020 #include "FWCore/Framework/interface/MakerMacros.h"
0021 
0022 #include "CalibTracker/SiPixelTools/interface/SiPixelOfflineCalibAnalysisBase.h"
0023 
0024 #include "Geometry/CommonDetUnit/interface/PixelGeomDetUnit.h"
0025 #include "Geometry/CommonDetUnit/interface/PixelGeomDetType.h"
0026 
0027 #include "CondFormats/SiPixelObjects/interface/SiPixelFrameConverter.h"
0028 #include "CondFormats/SiPixelObjects/interface/ElectronicIndex.h"
0029 #include "CondFormats/SiPixelObjects/interface/DetectorIndex.h"
0030 #include "CondFormats/SiPixelObjects/interface/LocalPixel.h"
0031 #include "TList.h"
0032 
0033 TF1* SiPixelOfflineCalibAnalysisBase::fitFunction_ = nullptr;
0034 std::vector<short> SiPixelOfflineCalibAnalysisBase::vCalValues_(0);
0035 // constructors and destructor
0036 //
0037 SiPixelOfflineCalibAnalysisBase::SiPixelOfflineCalibAnalysisBase(const edm::ParameterSet& iConfig)
0038     : runnumbers_(0), eventCounter_(0) {
0039   siPixelCalibDigiProducer_ = iConfig.getParameter<edm::InputTag>("DetSetVectorSiPixelCalibDigiTag");
0040   createOutputFile_ = iConfig.getUntrackedParameter<bool>("saveFile", false);
0041   outputFileName_ = iConfig.getParameter<std::string>("outputFileName");
0042   daqBE_ = &*edm::Service<DQMStore>();
0043   folderMaker_ = new SiPixelFolderOrganizer();
0044   tPixelCalibDigi = consumes<edm::DetSetVector<SiPixelCalibDigi> >(siPixelCalibDigiProducer_);
0045 
0046   calibTokenBeginRun_ =
0047       esConsumes<SiPixelCalibConfiguration, SiPixelCalibConfigurationRcd, edm::Transition::BeginRun>();
0048   calibToken_ = esConsumes<SiPixelCalibConfiguration, SiPixelCalibConfigurationRcd>();
0049   trackerGeomToken_ = esConsumes<TrackerGeometry, TrackerDigiGeometryRecord>();
0050   cablingMapToken_ = esConsumes<SiPixelFedCablingMap, SiPixelFedCablingMapRcd>();
0051 }
0052 
0053 SiPixelOfflineCalibAnalysisBase::SiPixelOfflineCalibAnalysisBase() {
0054   throw cms::Exception("") << "ERROR: Classes derived from SiPixelOfflineCalibAnalysisBase must call "
0055                               "SiPixelOfflineCalibAnalysisBase::SiPixelOfflineCalibAnalysisBase(const "
0056                               "edm::ParameterSet& iConfig) from their constructor."
0057                            << std::endl;
0058 }
0059 
0060 SiPixelOfflineCalibAnalysisBase::~SiPixelOfflineCalibAnalysisBase() {}
0061 
0062 //
0063 // member functions
0064 //
0065 
0066 // ------------ method called to for each event  ------------
0067 void SiPixelOfflineCalibAnalysisBase::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0068   using namespace edm;
0069 
0070   calib_ = iSetup.getHandle(calibToken_);
0071   geom_ = iSetup.getHandle(trackerGeomToken_);
0072   theCablingMap_ = iSetup.getHandle(cablingMapToken_);
0073   if (eventCounter_ == 0)
0074     this->calibrationSetup(iSetup);
0075   eventCounter_++;
0076 
0077   // check first if you're analyzing the right type of calibration
0078   if (!checkCorrectCalibrationType())
0079     return;
0080 
0081   uint32_t runnumber = iEvent.id().run();
0082   if (runnumbers_.empty())
0083     runnumbers_.push_back(runnumber);
0084   else {
0085     bool foundnumber = false;
0086     for (size_t iter = 0; iter < runnumbers_.size() && !foundnumber; ++iter) {
0087       if (runnumbers_[iter] == runnumber) {
0088         foundnumber = true;
0089         continue;
0090       }
0091     }
0092     if (!foundnumber)
0093       runnumbers_.push_back(runnumber);
0094   }
0095 
0096   Handle<DetSetVector<SiPixelCalibDigi> > thePlaquettes;
0097   iEvent.getByToken(tPixelCalibDigi, thePlaquettes);
0098 
0099   DetSetVector<SiPixelCalibDigi>::const_iterator digiIter;
0100 
0101   //loop over the plaquettes pulsed in this pattern
0102   for (digiIter = thePlaquettes->begin(); digiIter != thePlaquettes->end(); ++digiIter) {
0103     uint32_t detId = digiIter->id;
0104     //check to see if this detID has not been encountered.  If not, run the newDetID (pure virtual) function
0105     if (detIdsEntered_.find(detId) == detIdsEntered_.end()) {
0106       detIdsEntered_.insert(std::make_pair(detId, 0));
0107       detIdNames_.insert(std::make_pair(detId, translateDetIdToString(detId)));
0108       newDetID(detId);
0109     }
0110     DetSet<SiPixelCalibDigi>::const_iterator ipix;
0111     //loop over pixels pulsed in the current plaquette
0112     for (ipix = digiIter->data.begin(); ipix != digiIter->end(); ++ipix) {
0113       //called derived function to fit this curve
0114       this->doFits(detId, ipix);
0115     }
0116   }
0117 }
0118 
0119 void SiPixelOfflineCalibAnalysisBase::beginRun(const edm::Run&, const edm::EventSetup& iSetup) {
0120   //load the calibration information from the database
0121   edm::ESHandle<SiPixelCalibConfiguration> calib = iSetup.getHandle(calibTokenBeginRun_);
0122 
0123   calibrationMode_ = calib->getCalibrationMode();
0124   nTriggers_ = calib->getNTriggers();
0125   vCalValues_ = calib->getVCalValues();
0126   std::cout << "!!!! in beginRun" << std::endl;
0127   edm::LogInfo("SiPixelOfflineCalibAnalysisBase")
0128       << "Calibration file loaded. Mode: " << calibrationMode_ << " nTriggers: " << nTriggers_
0129       << " Vcal steps: " << vCalValues_.size() << std::endl;
0130   //call calibrationSetup virtual function
0131   this->calibrationSetup(iSetup);
0132   theHistogramIdWorker_ = new SiPixelHistogramId(siPixelCalibDigiProducer_.label());
0133 }
0134 
0135 void SiPixelOfflineCalibAnalysisBase::endRun(const edm::Run&, const edm::EventSetup& iSetup) {}
0136 
0137 void SiPixelOfflineCalibAnalysisBase::beginJob() {}
0138 // ------------ method called once each job just before starting event loop  ------------
0139 
0140 // ------------ method called once each job just after ending the event loop  ------------
0141 void SiPixelOfflineCalibAnalysisBase::endJob() {
0142   this->calibrationEnd();
0143   edm::LogInfo("SiPixelOfflineCalibAnalysisBase") << "Running end job... output file name is: " << outputFileName_;
0144   if (!outputFileName_.empty() && createOutputFile_) {
0145     edm::LogInfo("SiPixelOfflineCalibAnalysisBase") << "Writing ROOT file to: " << outputFileName_ << std::endl;
0146     daqBE_->save(outputFileName_);
0147   }
0148 }
0149 
0150 // ------------ helper functions ---------------------------------------------------------
0151 
0152 const std::vector<short>* SiPixelOfflineCalibAnalysisBase::getVcalValues() { return &vCalValues_; }
0153 
0154 std::string SiPixelOfflineCalibAnalysisBase::translateDetIdToString(uint32_t detid) {
0155   std::map<uint32_t, std::string>::iterator detNameIter = detIdNames_.find(detid);
0156   if (detNameIter != detIdNames_.end()) {
0157     return detNameIter->second;
0158   }
0159   std::string output = "DetID translation error";
0160   DetId detId(detid);
0161   uint32_t detSubId = detId.subdetId();
0162   if (detSubId > 2 || detSubId < 1) {
0163     edm::LogError("SiPixelOfflineCalibAnalysisBase")
0164         << "ERROR: Expected a pixel detector ID (1 - barrel, 2 - forward) but got " << detSubId << std::endl;
0165     return output;
0166   }
0167   if (detSubId == 2)  //FPIX
0168   {
0169     PixelEndcapName nameworker(detid);
0170     output = nameworker.name();
0171   } else  //BPIX
0172   {
0173     PixelBarrelName nameworker(detid);
0174     output = nameworker.name();
0175   }
0176   detIdNames_.insert(std::make_pair(detid, output));
0177   return output;
0178 }
0179 
0180 SiPixelOfflineCalibAnalysisBase::MonitorElement* SiPixelOfflineCalibAnalysisBase::bookDQMHistogram1D(
0181     uint32_t detid, std::string name, std::string title, int nchX, double lowX, double highX) {
0182   std::string hid = theHistogramIdWorker_->setHistoId(name, detid);
0183   return daqBE_->book1D(hid, title, nchX, lowX, highX);
0184 }
0185 
0186 SiPixelOfflineCalibAnalysisBase::MonitorElement* SiPixelOfflineCalibAnalysisBase::bookDQMHistogram1D(
0187     uint32_t detid, std::string name, std::string title, int nchX, float* xbinsize) {
0188   std::string hid = theHistogramIdWorker_->setHistoId(name, detid);
0189   return daqBE_->book1D(hid, title, nchX, xbinsize);
0190 }
0191 
0192 SiPixelOfflineCalibAnalysisBase::MonitorElement* SiPixelOfflineCalibAnalysisBase::bookDQMHistogram2D(uint32_t detid,
0193                                                                                                      std::string name,
0194                                                                                                      std::string title,
0195                                                                                                      int nchX,
0196                                                                                                      double lowX,
0197                                                                                                      double highX,
0198                                                                                                      int nchY,
0199                                                                                                      double lowY,
0200                                                                                                      double highY) {
0201   std::string hid = theHistogramIdWorker_->setHistoId(name, detid);
0202   return daqBE_->book2D(hid, title, nchX, lowX, highX, nchY, lowY, highY);
0203 }
0204 
0205 SiPixelOfflineCalibAnalysisBase::MonitorElement* SiPixelOfflineCalibAnalysisBase::bookDQMHistoPlaquetteSummary2D(
0206     uint32_t detid, std::string name, std::string title) {
0207   DetId detId(detid);
0208   const TrackerGeometry& theTracker(*geom_);
0209   const PixelGeomDetUnit* theGeomDet = dynamic_cast<const PixelGeomDetUnit*>(theTracker.idToDet(detId));
0210   int maxcol = theGeomDet->specificTopology().ncolumns();
0211   int maxrow = theGeomDet->specificTopology().nrows();
0212 
0213   std::string hid = theHistogramIdWorker_->setHistoId(name, detid);
0214   return daqBE_->book2D(hid, title, maxcol, 0, maxcol, maxrow, 0, maxrow);
0215 }
0216 
0217 bool SiPixelOfflineCalibAnalysisBase::setDQMDirectory(std::string dirName) {
0218   daqBE_->setCurrentFolder(dirName);
0219   return daqBE_->dirExists(dirName);
0220 }
0221 
0222 bool SiPixelOfflineCalibAnalysisBase::setDQMDirectory(uint32_t detID) {
0223   return folderMaker_->setModuleFolder(detID, 0);
0224 }
0225 
0226 // ------------ virtual functions ------------------------------------------------
0227 bool SiPixelOfflineCalibAnalysisBase::checkCorrectCalibrationType() { return true; }
0228 
0229 bool SiPixelOfflineCalibAnalysisBase::doFits(uint32_t detid, std::vector<SiPixelCalibDigi>::const_iterator ipix) {
0230   short row = ipix->row();
0231   short col = ipix->col();
0232   std::vector<uint8_t> nentries = ipix->getnentries();
0233   std::vector<uint32_t> sum = ipix->getsum();
0234   std::vector<uint32_t> sumquares = ipix->getsumsquares();
0235   //do nothing
0236   //return false;
0237   //
0238   //DEBUG
0239   std::cout << "Row: " << row << "   Col: " << col << std::endl;
0240   for (unsigned int i = 0; i < sum.size(); i++) {
0241     std::cout << sum[i] << "  ";
0242   }
0243   std::cout << std::endl;
0244   return false;
0245 }
0246 
0247 void SiPixelOfflineCalibAnalysisBase::calibrationSetup(const edm::EventSetup&) {
0248   //do nothing
0249 }
0250 
0251 void SiPixelOfflineCalibAnalysisBase::calibrationEnd() {
0252   // do nothing
0253 }
0254 void SiPixelOfflineCalibAnalysisBase::newDetID(uint32_t detid) {
0255   //do nothing
0256   edm::LogInfo("SiPixelOfflineCalibAnalysisBase")
0257       << "SiPixelOfflineCalibAnalysisBase - Found new DetID: " << detid << "  Name: " << detIdNames_[detid];
0258 }
0259 
0260 bool SiPixelOfflineCalibAnalysisBase::checkPixel(uint32_t detid, short row, short col) {
0261   // finds the fed ID:
0262   int thefedid = -1;
0263   for (int fedid = 0; fedid <= 40 && thefedid == -1; ++fedid) {
0264     SiPixelFrameConverter converter(theCablingMap_.product(), fedid);
0265     if (converter.hasDetUnit(detid)) {
0266       thefedid = fedid;
0267     }
0268   }
0269   if (thefedid == -1)
0270     return false;  // fed ID not associated with det ID. No pattern check possible
0271 
0272   SiPixelFrameConverter formatter(theCablingMap_.product(), thefedid);
0273   sipixelobjects::DetectorIndex detector = {detid, row, col};
0274   sipixelobjects::ElectronicIndex cabling;
0275 
0276   formatter.toCabling(cabling, detector);
0277   // cabling should now contain cabling.roc and cabling.dcol  and cabling.pxid
0278 
0279   // however, the coordinates now need to be converted from dcl, pxid to the row,col coordinates used in the calibration info
0280   sipixelobjects::LocalPixel::DcolPxid loc;
0281   loc.dcol = cabling.dcol;
0282   loc.pxid = cabling.pxid;
0283   sipixelobjects::LocalPixel locpixel(loc);
0284   short localrow = locpixel.rocRow();
0285   short localcol = locpixel.rocCol();
0286 
0287   // now get the patterns from the calib object:
0288   std::vector<short> calibcols = calib_->getColumnPattern();
0289   std::vector<short> calibrows = calib_->getRowPattern();
0290   // first check rows:
0291   for (size_t irow = 0; irow < calibrows.size(); ++irow) {
0292     if (calibrows[irow] == localrow) {
0293       // check the columns
0294       for (size_t icol = 0; icol < calibcols.size(); ++icol) {
0295         if (calibcols[icol] == localcol)
0296           return true;
0297       }
0298     }
0299   }
0300 
0301   return false;
0302 }
0303 
0304 // function to add tf1's to ME's:
0305 void SiPixelOfflineCalibAnalysisBase::addTF1ToDQMMonitoringElement(MonitorElement* ele, TF1* func) {
0306   if (func) {
0307     ele->getTH1()->GetListOfFunctions()->Add(func);
0308   }
0309   return;
0310 }