CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/CalibTracker/SiPixelTools/src/SiPixelOfflineCalibAnalysisBase.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:    SiPixelOfflineCalibAnalysisBase
00004 // Class:      SiPixelOfflineCalibAnalysisBase
00005 // 
00013 //
00014 // Original Author:  Evan Klose Friis
00015 //    additions by:  Freya Blekman
00016 //         Created:  Tue Nov  6 17:27:19 CET 2007
00017 // $Id: SiPixelOfflineCalibAnalysisBase.cc,v 1.19 2011/06/29 10:29:36 eulisse Exp $
00018 //
00019 //
00020 
00021 #include "CalibTracker/SiPixelTools/interface/SiPixelOfflineCalibAnalysisBase.h"
00022 
00023 #include "Geometry/TrackerGeometryBuilder/interface/PixelGeomDetUnit.h"
00024 #include "Geometry/TrackerGeometryBuilder/interface/PixelGeomDetType.h"
00025 #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h" 
00026 
00027 #include "CondFormats/SiPixelObjects/interface/SiPixelFrameConverter.h"
00028 #include "CondFormats/SiPixelObjects/interface/ElectronicIndex.h"
00029 #include "CondFormats/SiPixelObjects/interface/DetectorIndex.h"
00030 #include "CondFormats/SiPixelObjects/interface/LocalPixel.h"
00031 #include "TList.h"
00032 
00033 TF1* SiPixelOfflineCalibAnalysisBase::fitFunction_ = NULL;
00034 std::vector<short>  SiPixelOfflineCalibAnalysisBase::vCalValues_(0);
00035 // constructors and destructor
00036 //
00037 SiPixelOfflineCalibAnalysisBase::SiPixelOfflineCalibAnalysisBase(const edm::ParameterSet& iConfig):runnumbers_(0),eventCounter_(0)
00038 {
00039    siPixelCalibDigiProducer_ = iConfig.getParameter<edm::InputTag>("DetSetVectorSiPixelCalibDigiTag");
00040    createOutputFile_ = iConfig.getUntrackedParameter<bool>("saveFile",false);
00041    outputFileName_ = iConfig.getParameter<std::string>("outputFileName");
00042    daqBE_ = &*edm::Service<DQMStore>();
00043    folderMaker_ = new SiPixelFolderOrganizer();
00044    
00045 }
00046 
00047 SiPixelOfflineCalibAnalysisBase::SiPixelOfflineCalibAnalysisBase()
00048 {
00049    throw cms::Exception("") << "ERROR: Classes derived from SiPixelOfflineCalibAnalysisBase must call SiPixelOfflineCalibAnalysisBase::SiPixelOfflineCalibAnalysisBase(const edm::ParameterSet& iConfig) from their constructor." << std::endl;
00050 }
00051 
00052 SiPixelOfflineCalibAnalysisBase::~SiPixelOfflineCalibAnalysisBase()
00053 {
00054 }
00055 
00056 
00057 //
00058 // member functions
00059 //
00060 
00061 // ------------ method called to for each event  ------------
00062 void
00063 SiPixelOfflineCalibAnalysisBase::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup)
00064 {
00065    using namespace edm;
00066 
00067    iSetup.get<TrackerDigiGeometryRecord>().get( geom_ );
00068    iSetup.get<SiPixelFedCablingMapRcd>().get(theCablingMap_);
00069    iSetup.get<SiPixelCalibConfigurationRcd>().get(calib_);
00070    if(eventCounter_==0)
00071      this->calibrationSetup(iSetup);
00072    eventCounter_++;
00073      
00074    // check first if you're analyzing the right type of calibration
00075    if(!checkCorrectCalibrationType())
00076      return;
00077    
00078    uint32_t runnumber=iEvent.id().run();
00079    if(runnumbers_.size()==0)
00080      runnumbers_.push_back(runnumber);
00081    else{
00082      bool foundnumber=false;
00083      for(size_t iter=0; iter<runnumbers_.size() && !foundnumber; ++ iter){
00084        if(runnumbers_[iter]==runnumber){
00085          foundnumber=true;
00086          continue;
00087        }
00088      }
00089      if(!foundnumber)
00090        runnumbers_.push_back(runnumber);
00091    }
00092     
00093    Handle<DetSetVector<SiPixelCalibDigi> > thePlaquettes;
00094    iEvent.getByLabel(siPixelCalibDigiProducer_, thePlaquettes);
00095 
00096    DetSetVector<SiPixelCalibDigi>::const_iterator digiIter;
00097 
00098    //loop over the plaquettes pulsed in this pattern 
00099    for(digiIter=thePlaquettes->begin(); digiIter!=thePlaquettes->end(); ++digiIter) 
00100    {
00101       uint32_t detId = digiIter->id;
00102       //check to see if this detID has not been encountered.  If not, run the newDetID (pure virtual) function
00103       if (detIdsEntered_.find(detId) == detIdsEntered_.end()) 
00104       {
00105          detIdsEntered_.insert(std::make_pair(detId, 0));
00106          detIdNames_.insert(std::make_pair(detId, translateDetIdToString(detId)));
00107          newDetID(detId);
00108       }
00109       DetSet<SiPixelCalibDigi>::const_iterator ipix;
00110       //loop over pixels pulsed in the current plaquette
00111       for(ipix=digiIter->data.begin(); ipix!=digiIter->end(); ++ipix)
00112       {
00113          //called derived function to fit this curve
00114         this->doFits(detId, ipix);
00115       }
00116    }
00117    
00118 }
00119 
00120 void SiPixelOfflineCalibAnalysisBase::beginRun(const edm::Run &, const edm::EventSetup &iSetup)
00121 {
00122    //load the calibration information from the database
00123    iSetup.get<SiPixelCalibConfigurationRcd>().get(calib_);
00124    iSetup.get<TrackerDigiGeometryRecord>().get( geom_ );
00125    iSetup.get<SiPixelFedCablingMapRcd>().get(theCablingMap_);
00126 
00127    calibrationMode_     = calib_->getCalibrationMode();
00128    nTriggers_           = calib_->getNTriggers();
00129    vCalValues_          = calib_->getVCalValues();
00130    std::cout << "!!!! in beginRun" << std::endl;
00131    edm::LogInfo("SiPixelOfflineCalibAnalysisBase") << "Calibration file loaded. Mode: " << calibrationMode_ << " nTriggers: " << nTriggers_ << " Vcal steps: " << vCalValues_.size() << std::endl;
00132    //call calibrationSetup virtual function
00133    this->calibrationSetup(iSetup);
00134    theHistogramIdWorker_ = new SiPixelHistogramId(siPixelCalibDigiProducer_.label());
00135 }
00136 void 
00137 SiPixelOfflineCalibAnalysisBase::beginJob()
00138 {
00139 
00140 }
00141 // ------------ method called once each job just before starting event loop  ------------
00142 
00143 // ------------ method called once each job just after ending the event loop  ------------
00144 void 
00145 SiPixelOfflineCalibAnalysisBase::endJob() {
00146   this->calibrationEnd();
00147    edm::LogInfo("SiPixelOfflineCalibAnalysisBase") << "Running end job... output file name is: " << outputFileName_;
00148    if (!outputFileName_.empty() && createOutputFile_) 
00149    {
00150       edm::LogInfo("SiPixelOfflineCalibAnalysisBase") << "Writing ROOT file to: " << outputFileName_ << std::endl;
00151       daqBE_->save(outputFileName_);
00152    }
00153 }
00154 
00155 // ------------ helper functions ---------------------------------------------------------
00156 
00157 const std::vector<short>* 
00158 SiPixelOfflineCalibAnalysisBase::getVcalValues()
00159 {
00160    return &vCalValues_;
00161 }
00162 
00163 std::string
00164 SiPixelOfflineCalibAnalysisBase::translateDetIdToString(uint32_t detid)
00165 {
00166    std::map<uint32_t, std::string>::iterator detNameIter = detIdNames_.find(detid);
00167    if (detNameIter != detIdNames_.end()) {
00168       return detNameIter->second;
00169    }
00170    std::string output = "DetID translation error";
00171    DetId detId(detid);
00172    uint32_t detSubId = detId.subdetId();
00173    if (detSubId > 2 || detSubId < 1)
00174    {
00175       edm::LogError("SiPixelOfflineCalibAnalysisBase") << "ERROR: Expected a pixel detector ID (1 - barrel, 2 - forward) but got " << detSubId << std::endl;
00176       return output;
00177    }
00178    if (detSubId == 2)   //FPIX
00179    {
00180       PixelEndcapName nameworker(detid);
00181       output = nameworker.name();
00182    } else               //BPIX
00183    {
00184       PixelBarrelName nameworker(detid);
00185       output = nameworker.name();
00186    }
00187    detIdNames_.insert(std::make_pair(detid, output));
00188    return output;
00189 }
00190 
00191 MonitorElement* SiPixelOfflineCalibAnalysisBase::bookDQMHistogram1D(uint32_t detid, std::string name, std::string title, int nchX, double lowX, double highX)  
00192 {
00193   std::string hid = theHistogramIdWorker_->setHistoId(name,detid);
00194   return daqBE_->book1D(hid, title, nchX, lowX, highX);
00195 }
00196 
00197 MonitorElement* SiPixelOfflineCalibAnalysisBase::bookDQMHistogram1D(uint32_t detid, std::string name, std::string title, int nchX, float *xbinsize)
00198 {
00199   std::string hid = theHistogramIdWorker_->setHistoId(name,detid);
00200   return daqBE_->book1D(hid, title, nchX, xbinsize);
00201 }
00202 
00203 MonitorElement* SiPixelOfflineCalibAnalysisBase::bookDQMHistogram2D(uint32_t detid, std::string name, std::string title, int nchX, double lowX, double highX, int nchY, double lowY, double highY)
00204 {
00205   std::string hid = theHistogramIdWorker_->setHistoId(name,detid);
00206   return daqBE_->book2D(hid, title, nchX, lowX, highX, nchY, lowY, highY);
00207 }
00208 
00209 MonitorElement* SiPixelOfflineCalibAnalysisBase::bookDQMHistoPlaquetteSummary2D(uint32_t detid, std::string name,std::string title){
00210   DetId detId(detid);
00211   const TrackerGeometry &theTracker(*geom_);
00212   const PixelGeomDetUnit *theGeomDet = dynamic_cast<const PixelGeomDetUnit*> ( theTracker.idToDet(detId) ); 
00213   int maxcol = theGeomDet->specificTopology().ncolumns();
00214   int maxrow = theGeomDet->specificTopology().nrows();
00215   
00216 
00217   std::string hid = theHistogramIdWorker_->setHistoId(name,detid);
00218   return daqBE_->book2D(hid,title,maxcol,0,maxcol,maxrow,0,maxrow);
00219 }
00220 
00221 bool
00222 SiPixelOfflineCalibAnalysisBase::setDQMDirectory(std::string dirName)
00223 {
00224    daqBE_->setCurrentFolder(dirName);
00225    return daqBE_->dirExists(dirName);
00226 }
00227 
00228 bool
00229 SiPixelOfflineCalibAnalysisBase::setDQMDirectory(uint32_t detID)
00230 {
00231   return folderMaker_->setModuleFolder(detID,0);
00232 }
00233 
00234 
00235 // ------------ virtual functions ------------------------------------------------
00236 bool 
00237 SiPixelOfflineCalibAnalysisBase::checkCorrectCalibrationType()
00238 {
00239   return true;
00240 }
00241 
00242 bool
00243 SiPixelOfflineCalibAnalysisBase::doFits(uint32_t detid, std::vector<SiPixelCalibDigi>::const_iterator ipix)
00244 {
00245   short row=ipix->row();
00246   short col=ipix->col();
00247   std::vector<uint8_t> nentries = ipix->getnentries();
00248   std::vector<uint32_t> sum = ipix->getsum();
00249   std::vector<uint32_t> sumquares = ipix->getsumsquares();
00250    //do nothing
00251    //return false;
00252    //
00253    //DEBUG
00254    std::cout << "Row: " << row << "   Col: " << col << std::endl;
00255    for (unsigned int i = 0; i < sum.size(); i++)
00256    {
00257       std::cout << sum[i] << "  ";
00258    }
00259    std::cout << std::endl;
00260    return false;
00261 
00262 }
00263 
00264 void 
00265 SiPixelOfflineCalibAnalysisBase::calibrationSetup(const edm::EventSetup&)
00266 {
00267    //do nothing
00268 }
00269 
00270 void SiPixelOfflineCalibAnalysisBase::calibrationEnd()
00271 {
00272   // do nothing
00273 }
00274 void 
00275 SiPixelOfflineCalibAnalysisBase::newDetID(uint32_t detid)
00276 {
00277    //do nothing
00278    edm::LogInfo("SiPixelOfflineCalibAnalysisBase") << "SiPixelOfflineCalibAnalysisBase - Found new DetID: " << detid << "  Name: " << detIdNames_[detid];
00279 }
00280 
00281 bool
00282 SiPixelOfflineCalibAnalysisBase::checkPixel(uint32_t detid,short row, short col)
00283 {
00284   // finds the fed ID:
00285   int thefedid = -1;
00286   for(int fedid=0; fedid<=40 && thefedid==-1; ++fedid)
00287     {
00288       SiPixelFrameConverter converter(theCablingMap_.product(),fedid);
00289       if(converter.hasDetUnit(detid))
00290         {
00291           thefedid=fedid;
00292         }
00293     }
00294   if(thefedid==-1)
00295     return false; // fed ID not associated with det ID. No pattern check possible
00296   
00297   SiPixelFrameConverter formatter(theCablingMap_.product(),thefedid);
00298   sipixelobjects::DetectorIndex detector ={detid, row, col};
00299   sipixelobjects::ElectronicIndex cabling;
00300   
00301   formatter.toCabling(cabling,detector);
00302   // cabling should now contain cabling.roc and cabling.dcol  and cabling.pxid
00303 
00304   // however, the coordinates now need to be converted from dcl, pxid to the row,col coordinates used in the calibration info
00305   sipixelobjects::LocalPixel::DcolPxid loc;
00306   loc.dcol = cabling.dcol;
00307   loc.pxid = cabling.pxid;
00308   sipixelobjects::LocalPixel locpixel(loc);
00309   short localrow = locpixel.rocRow();
00310   short localcol = locpixel.rocCol();
00311 
00312   // now get the patterns from the calib object:
00313   std::vector<short> calibcols = calib_->getColumnPattern();
00314   std::vector<short> calibrows = calib_->getRowPattern();
00315   // first check rows:
00316   for(size_t irow=0; irow<calibrows.size(); ++irow)
00317     {
00318       if(calibrows[irow]==localrow)
00319         {
00320           // check the columns
00321           for(size_t icol=0; icol<calibcols.size(); ++icol)
00322             {
00323               if(calibcols[icol]==localcol)
00324                 return true;
00325             }
00326         }
00327     }
00328 
00329   return false;
00330 }
00331 
00332 // function to add tf1's to ME's:
00333 void SiPixelOfflineCalibAnalysisBase::addTF1ToDQMMonitoringElement(MonitorElement *ele, TF1 *func){
00334   
00335   if(func){
00336     ele->getTH1()->GetListOfFunctions()->Add(func);
00337   }
00338   return;
00339 }
00340 //define this as a plug-in
00341 DEFINE_FWK_MODULE(SiPixelOfflineCalibAnalysisBase);