CMS 3D CMS Logo

/data/git/CMSSW_5_3_11_patch5/src/DQM/SiStripCommissioningSources/src/CalibrationTask.cc

Go to the documentation of this file.
00001 #include "DQM/SiStripCommissioningSources/interface/CalibrationTask.h"
00002 #include "DataFormats/SiStripCommon/interface/SiStripHistoTitle.h"
00003 #include "DQMServices/Core/interface/DQMStore.h"
00004 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00005 #include "FWCore/Framework/interface/EventSetup.h"
00006 #include "FWCore/Framework/interface/ESHandle.h"
00007 #include <CondFormats/DataRecord/interface/SiStripPedestalsRcd.h>
00008 #include <CondFormats/SiStripObjects/interface/SiStripPedestals.h>
00009 #include <DQMServices/Core/interface/MonitorElement.h>
00010 
00011 #include <arpa/inet.h>
00012 #include <sys/unistd.h>
00013 #include <sys/socket.h>
00014 #include <netdb.h>
00015 #include <stdio.h>
00016 #include <fstream>
00017 
00018 // -----------------------------------------------------------------------------
00019 //
00020 CalibrationTask::CalibrationTask( DQMStore* dqm,
00021                               const FedChannelConnection& conn,
00022                               const sistrip::RunType& rtype,
00023                               const char* filename,
00024                               uint32_t run,
00025                               const edm::EventSetup& setup) :
00026   CommissioningTask( dqm, conn, "CalibrationTask" ),
00027   runType_(rtype),
00028   nBins_(65),lastCalChan_(8),
00029   filename_(filename),
00030   run_(run)
00031 {
00032   LogDebug("Commissioning") << "[CalibrationTask::CalibrationTask] Constructing object...";
00033   // load the pedestals
00034   edm::ESHandle<SiStripPedestals> pedestalsHandle;
00035   setup.get<SiStripPedestalsRcd>().get(pedestalsHandle);
00036   SiStripPedestals::Range detPedRange = pedestalsHandle->getRange(conn.detId());
00037   int start = conn.apvPairNumber()*256; 
00038   int stop  = start + 256;
00039   int value = 0;
00040   ped.reserve(256);
00041   for(int strip = start; strip < stop; ++strip) {
00042     value = int(pedestalsHandle->getPed(strip,detPedRange));
00043     if(value>895) value -= 1024;
00044     ped.push_back(value);
00045   }
00046 }
00047 
00048 // -----------------------------------------------------------------------------
00049 //
00050 CalibrationTask::~CalibrationTask() {
00051   LogDebug("Commissioning") << "[CalibrationTask::CalibrationTask] Destructing object...";
00052 }
00053 
00054 // -----------------------------------------------------------------------------
00055 //
00056 void CalibrationTask::book() {
00057   LogDebug("Commissioning") << "[CalibrationTask::book]";
00058 
00059   // book 32 histograms, one for each strip in a calibration group on 2 APVs
00060   calib_.resize(32);
00061   for(int apv=0;apv<2;++apv) {
00062     for(int i=0;i<16;++i) {
00063       std::string title = SiStripHistoTitle( sistrip::EXPERT_HISTO, 
00064                                              runType_, 
00065                                              sistrip::DET_KEY, 
00066                                              connection().detId(),
00067                                              sistrip::APV, 
00068                                              connection().i2cAddr(apv) ).title(); 
00069       std::stringstream complement;
00070       complement << "_" << i ;
00071       title += complement.str();                        
00072       calib_[apv*16+i].histo( dqm()->book1D( title, title, nBins_, 0, 203.125) );
00073       calib_[apv*16+i].isProfile_=false;
00074       calib_[apv*16+i].vNumOfEntries_.resize(nBins_,0);
00075     }
00076   }
00077 
00078   // book the calchan values
00079   std::string pwd = dqm()->pwd();
00080   std::string rootDir = pwd.substr(0,pwd.find(std::string(sistrip::root_) + "/")+(sizeof(sistrip::root_) - 1));
00081   dqm()->setCurrentFolder( rootDir );
00082   std::vector<std::string> existingMEs = dqm()->getMEs();
00083   if(find(existingMEs.begin(),existingMEs.end(),"calchan")!=existingMEs.end()) {
00084     calchanElement_ = dqm()->get(dqm()->pwd()+"/calchan");
00085   } else {
00086     calchanElement_ = dqm()->bookInt("calchan");
00087   }
00088   dqm()->setCurrentFolder(pwd);
00089   
00090   LogDebug("Commissioning") << "[CalibrationTask::book] done";
00091   
00092 }
00093 
00094 // -----------------------------------------------------------------------------
00095 //
00096 void CalibrationTask::fill( const SiStripEventSummary& summary,
00097                             const edm::DetSet<SiStripRawDigi>& digis ) {
00098   LogDebug("Commissioning") << "[CalibrationTask::fill]";
00099   // Check if CalChan changed. In that case, save, reset histo, change title, and continue
00100   int isub,ical = summary.calChan();
00101   isub = ical<4 ? ical+4 : ical-4;
00102   checkAndSave(ical);
00103   // retrieve the delay from the EventSummary
00104   int bin = (100-summary.latency())*8+(7-summary.calSel());
00105   // Fill the histograms.
00106   // data-ped -(data-ped)_isub
00107   // the second term corresponds to the common mode substraction, looking at a strip far away.
00108   for (int apv=0; apv<2; ++apv) {
00109     for (int k=0;k<16;++k)  {
00110       updateHistoSet( calib_[apv*16+k],bin,digis.data[apv*128+ical+k*8].adc()-ped[apv*128+ical+k*8]-(digis.data[apv*128+isub+k*8].adc()-ped[apv*128+isub+k*8]));
00111     }
00112   }
00113   update(); //TODO: temporary: find a better solution later
00114 }
00115 
00116 // -----------------------------------------------------------------------------
00117 //
00118 void CalibrationTask::update() {
00119   // LogDebug("Commissioning") << "[CalibrationTask::update]"; // huge output
00120   for(std::vector<HistoSet>::iterator it=calib_.begin();it<calib_.end();++it) {
00121     updateHistoSet( *it );
00122   }
00123 }
00124 
00125 // -----------------------------------------------------------------------------
00126 //
00127 void CalibrationTask::checkAndSave(const uint16_t& calchan) {
00128   //for the first time
00129   if(lastCalChan_==8) {
00130     // set new parameter value
00131     lastCalChan_ = calchan;
00132     calchanElement_->Fill(lastCalChan_);
00133     return;
00134   }
00135 
00136   //check if CDRV register (ical) has changed
00137   // in that case, save the histograms, reset them, change the title and continue
00138   if(calchan!=lastCalChan_) {
00139     
00140     // set histograms before saving
00141     update(); //TODO: we must do this for all tasks, otherwise only the first is updated.
00142 
00143     // change the title
00144     for(int apv=0;apv<2;++apv) {
00145       for(int i=0;i<16;++i) {
00146         std::stringstream complement;
00147         complement << "STRIP_" << (apv*128+lastCalChan_+i*8) ;
00148         std::string title = SiStripHistoTitle( sistrip::EXPERT_HISTO, 
00149                                                runType_, 
00150                                                sistrip::DET_KEY, 
00151                                                connection().detId(),
00152                                                sistrip::APV, 
00153                                                connection().i2cAddr(apv),
00154                                                complement.str() ).title(); 
00155         calib_[apv*16+i].histo()->setTitle(title);
00156         calib_[apv*16+i].histo()->setAxisTitle(title);
00157       }
00158     }
00159 
00160     // Strip filename of ".root" extension
00161     std::string name;
00162     if ( filename_.find(".root",0) == std::string::npos ) { name = filename_; }
00163     else { name = filename_.substr( 0, filename_.find(".root",0) ); }
00164   
00165     // Retrieve SCRATCH directory
00166     std::string scratch = "SCRATCH"; //@@ remove trailing slash!!!
00167     std::string dir = "";
00168     if ( getenv(scratch.c_str()) != NULL ) {
00169       dir = getenv(scratch.c_str());
00170     }
00171   
00172     // Save file with appropriate filename 
00173     std::stringstream ss;
00174     if ( !dir.empty() ) { ss << dir << "/"; }
00175     else { ss << "/tmp/"; }
00176     ss << name << "_";
00177     directory(ss,run_); // Add filename with run number, host ip, pid and .root extension
00178     ss << "_CALCHAN" << lastCalChan_; // Add CalChan value
00179     ss << "_000"; // Add FU instance number (fake)
00180     ss << ".root"; // Append ".root" extension
00181     if ( !filename_.empty() ) {
00182       if(ifstream(ss.str().c_str(),ifstream::in).fail()) { //save only once. Skip if the file already exist
00183         dqm()->save( ss.str() ); 
00184         LogTrace("DQMsource")
00185           << "[SiStripCommissioningSource::" << __func__ << "]"
00186           << " Saved all histograms to file \""
00187           << ss.str() << "\"";
00188       } else {
00189         LogTrace("DQMsource")
00190           << "[SiStripCommissioningSource::" << __func__ << "]"
00191           << " Skipping creation of file \""
00192           << ss.str() << "\" that already exists" ;
00193       }
00194     } else {
00195       edm::LogWarning("DQMsource")
00196         << "[SiStripCommissioningSource::" << __func__ << "]"
00197         << " NULL value for filename! No root file saved!";
00198     }
00199   
00200     // reset
00201     for(std::vector<HistoSet>::iterator it=calib_.begin();it<calib_.end();++it) {
00202       it->vNumOfEntries_.clear();
00203       it->vNumOfEntries_.resize(nBins_,0);
00204     }
00205     // set new parameter values
00206     lastCalChan_ = calchan;
00207     calchanElement_->Fill(lastCalChan_);
00208   }
00209 }
00210 
00211 // -----------------------------------------------------------------------------
00212 // 
00213 void CalibrationTask::directory( std::stringstream& dir,
00214                                             uint32_t run_number ) {
00215 
00216   // Get details about host
00217   char hn[256];
00218   gethostname( hn, sizeof(hn) );
00219   struct hostent* he;
00220   he = gethostbyname(hn);
00221 
00222   // Extract host name and ip
00223   std::string host_name;
00224   std::string host_ip;
00225   if ( he ) { 
00226     host_name = std::string(he->h_name);
00227     host_ip = std::string( inet_ntoa( *(struct in_addr*)(he->h_addr) ) );
00228   } else {
00229     host_name = "unknown.cern.ch";
00230     host_ip = "255.255.255.255";
00231   }
00232 
00233   // Reformat IP address
00234   std::string::size_type pos = 0;
00235   std::stringstream ip;
00236   //for ( uint16_t ii = 0; ii < 4; ++ii ) {
00237   while ( pos != std::string::npos ) {
00238     std::string::size_type tmp = host_ip.find(".",pos);
00239     if ( tmp != std::string::npos ) {
00240       ip << std::setw(3)
00241          << std::setfill('0')
00242          << host_ip.substr( pos, tmp-pos ) 
00243          << ".";
00244       pos = tmp+1; // skip the delimiter "."
00245     } else {
00246       ip << std::setw(3)
00247          << std::setfill('0')
00248          << host_ip.substr( pos );
00249       pos = std::string::npos;
00250     }
00251   }
00252   
00253   // Get pid
00254   pid_t pid = getpid();
00255 
00256   // Construct string
00257   dir << std::setw(8) 
00258       << std::setfill('0') 
00259       << run_number
00260       << "_" 
00261       << ip.str()
00262       << "_"
00263       << std::setw(5) 
00264       << std::setfill('0') 
00265       << pid;
00266 }