CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC4_patch1/src/DQM/SiStripCommissioningSources/src/CalibrationScanTask.cc

Go to the documentation of this file.
00001 #include "DQM/SiStripCommissioningSources/interface/CalibrationScanTask.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 CalibrationScanTask::CalibrationScanTask( 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, "CalibrationScanTask" ),
00027   runType_(rtype),
00028   calib1_(),calib2_(),
00029   nBins_(65),lastISHA_(1000),lastVFS_(1000),lastCalchan_(1000),
00030   filename_(filename),
00031   run_(run)
00032 {
00033   LogDebug("Commissioning") << "[CalibrationScanTask::CalibrationScanTask] Constructing object...";
00034   // load the pedestals
00035   edm::ESHandle<SiStripPedestals> pedestalsHandle;
00036   setup.get<SiStripPedestalsRcd>().get(pedestalsHandle);
00037   SiStripPedestals::Range detPedRange = pedestalsHandle->getRange(conn.detId());
00038   int start = conn.apvPairNumber()*256;
00039   int stop  = start + 256;
00040   int value = 0;
00041   ped.reserve(256);
00042   LogDebug("Commissioning") << "[CalibrationScanTask::CalibrationScanTask] Loading pedestal for " << conn.detId();
00043   if(conn.detId()==0) return;
00044   for(int strip = start; strip < stop; ++strip) {
00045     value = int(pedestalsHandle->getPed(strip,detPedRange));
00046     if(value>895) value -= 1024;
00047     ped.push_back(value);
00048   }
00049 }
00050 
00051 // -----------------------------------------------------------------------------
00052 //
00053 CalibrationScanTask::~CalibrationScanTask() {
00054   LogDebug("Commissioning") << "[CalibrationScanTask::CalibrationScanTask] Destructing object...";
00055 }
00056 
00057 // -----------------------------------------------------------------------------
00058 //
00059 void CalibrationScanTask::book() {
00060   LogDebug("Commissioning") << "[CalibrationScanTask::book]";
00061 
00062   // construct the histo titles and book two histograms: one per APV
00063   std::string title1 = SiStripHistoTitle( sistrip::EXPERT_HISTO, 
00064                                          runType_, 
00065                                          sistrip::DET_KEY, 
00066                                          connection().detId(),
00067                                          sistrip::APV, 
00068                                          connection().i2cAddr(0) ).title(); 
00069   calib1_.histo( dqm()->book1D( title1, title1, nBins_, 0, 203.125) );
00070   calib1_.isProfile_=false;
00071   calib1_.vNumOfEntries_.resize(nBins_,0);
00072   std::string title2 = SiStripHistoTitle( sistrip::EXPERT_HISTO, 
00073                                          runType_, 
00074                                          sistrip::DET_KEY, 
00075                                          connection().detId(),
00076                                          sistrip::APV, 
00077                                          connection().i2cAddr(1) ).title(); 
00078   calib2_.histo( dqm()->book1D( title2, title2, nBins_, 0, 203.125) );
00079   calib2_.isProfile_=false;
00080   calib2_.vNumOfEntries_.resize(nBins_,0);
00081 
00082   // book the isha, vfs values
00083   std::string pwd = dqm()->pwd();
00084   std::string rootDir = pwd.substr(0,pwd.find(std::string(sistrip::root_) + "/")+(sizeof(sistrip::root_) - 1));
00085   dqm()->setCurrentFolder( rootDir );
00086   std::vector<std::string> existingMEs = dqm()->getMEs();
00087   if(find(existingMEs.begin(),existingMEs.end(),"isha")!=existingMEs.end()) {
00088     ishaElement_ = dqm()->get(dqm()->pwd()+"/isha");
00089   } else {
00090     ishaElement_ = dqm()->bookInt("isha");
00091   }
00092   if(find(existingMEs.begin(),existingMEs.end(),"isha")!=existingMEs.end()) {
00093     vfsElement_ = dqm()->get(dqm()->pwd()+"/vfs");  
00094   } else {
00095     vfsElement_ = dqm()->bookInt("vfs");
00096   }
00097   if(find(existingMEs.begin(),existingMEs.end(),"calchan")!=existingMEs.end()) {
00098     calchanElement_ = dqm()->get(dqm()->pwd()+"/calchan");
00099   } else {
00100     calchanElement_ = dqm()->bookInt("calchan");
00101   }
00102   
00103   LogDebug("Commissioning") << "[CalibrationScanTask::book] done";
00104   
00105 }
00106 
00107 // -----------------------------------------------------------------------------
00108 //
00109 void CalibrationScanTask::fill( const SiStripEventSummary& summary,
00110                             const edm::DetSet<SiStripRawDigi>& digis ) {
00111 //  LogDebug("Commissioning") << "[CalibrationScanTask::fill]: isha/vfs = " << summary.isha() << "/" << summary.vfs();
00112   // Check if ISHA/VFS changed. In that case, save, reset histo, change title, and continue
00113   checkAndSave(summary.isha(),summary.vfs());
00114   // retrieve the delay from the EventSummary
00115   int bin = (100-summary.latency())*8+(7-summary.calSel());
00116   // loop on the strips to fill the histogram
00117   // digis are obtained for an APV pair. 
00118   // strips 0->127  : calib1_
00119   // strips 128->255: calib2_
00120   // then, only some strips are fired at a time.
00121   // We use calChan to know that
00122   int isub,ical = summary.calChan();
00123   isub = ical<4 ? ical+4 : ical-4;
00124   lastCalchan_ = ical;
00125   for (int k=0;k<16;++k)  {
00126     // all strips of the APV are merged in 
00127     updateHistoSet( calib1_,bin,digis.data[ical+k*8].adc()-ped[ical+k*8]-(digis.data[isub+k*8].adc()-ped[isub+k*8]));
00128     updateHistoSet( calib2_,bin,digis.data[128+ical+k*8].adc()-ped[128+ical+k*8]-(digis.data[128+isub+k*8].adc()-ped[128+isub+k*8]));
00129   }
00130   update(); //TODO: temporary: find a better solution later
00131 }
00132 
00133 // -----------------------------------------------------------------------------
00134 //
00135 void CalibrationScanTask::update() {
00136   // LogDebug("Commissioning") << "[CalibrationScanTask::update]"; // huge output
00137   updateHistoSet( calib1_ );
00138   updateHistoSet( calib2_ );
00139 }
00140 
00141 // -----------------------------------------------------------------------------
00142 //
00143 void CalibrationScanTask::checkAndSave(const uint16_t& isha, const uint16_t& vfs ) {
00144   //for the first time
00145   if(lastISHA_==1000 && lastVFS_==1000) {
00146     lastISHA_ = isha;
00147     lastVFS_  = vfs;
00148   }
00149 
00150   // set the calchan value in the correspond string
00151   ishaElement_->Fill(lastISHA_);
00152   vfsElement_->Fill(lastVFS_);
00153   calchanElement_->Fill(lastCalchan_);
00154 
00155   // check if ISHA/VFS has changed
00156   // in that case, save the histograms, reset them, change the title and continue
00157   if(lastISHA_!=isha || lastVFS_!=vfs) {
00158 
00159     // set histograms before saving
00160     update(); //TODO: we must do this for all tasks, otherwise only the first is updated.
00161     
00162     // change the title
00163     std::stringstream complement;
00164     complement << "ISHA" << isha << "_VFS" << vfs;
00165     std::string title1 = SiStripHistoTitle( sistrip::EXPERT_HISTO, 
00166                                             runType_, 
00167                                             sistrip::DET_KEY, 
00168                                             connection().detId(),
00169                                             sistrip::APV, 
00170                                             connection().i2cAddr(0),
00171                                             complement.str() ).title(); 
00172     calib1_.histo()->setTitle(title1);
00173 
00174     std::string title2 = SiStripHistoTitle( sistrip::EXPERT_HISTO, 
00175                                             runType_, 
00176                                             sistrip::DET_KEY, 
00177                                             connection().detId(),
00178                                             sistrip::APV, 
00179                                             connection().i2cAddr(1),
00180                                             complement.str() ).title(); 
00181     calib2_.histo()->setTitle(title2);
00182 
00183     // Strip filename of ".root" extension
00184     std::string name;
00185     if ( filename_.find(".root",0) == std::string::npos ) { name = filename_; }
00186     else { name = filename_.substr( 0, filename_.find(".root",0) ); }
00187   
00188     // Retrieve SCRATCH directory
00189     std::string scratch = "SCRATCH"; //@@ remove trailing slash!!!
00190     std::string dir = "";
00191     if ( getenv(scratch.c_str()) != NULL ) {
00192       dir = getenv(scratch.c_str());
00193     }
00194   
00195     // Save file with appropriate filename 
00196     std::stringstream ss;
00197     if ( !dir.empty() ) { ss << dir << "/"; }
00198     else { ss << "/tmp/"; }
00199     ss << name << "_";
00200     directory(ss,run_); // Add filename with run number, host ip, pid and .root extension
00201     ss << "_ISHA" << lastISHA_ << "_VFS" << lastVFS_; // Add ISHA and VFS values
00202     ss << "_000"; // Add FU instance number (fake)
00203     ss << ".root"; // Append ".root" extension
00204     if ( !filename_.empty() ) {
00205       if(ifstream(ss.str().c_str(),ifstream::in).fail()) { //save only once. Skip if the file already exist
00206         dqm()->save( ss.str() ); 
00207         LogTrace("DQMsource")
00208           << "[SiStripCommissioningSource::" << __func__ << "]"
00209           << " Saved all histograms to file \""
00210           << ss.str() << "\"";
00211       } else {
00212         LogTrace("DQMsource")
00213           << "[SiStripCommissioningSource::" << __func__ << "]"
00214           << " Skipping creation of file \""
00215           << ss.str() << "\" that already exists" ;
00216       }
00217     } else {
00218       edm::LogWarning("DQMsource")
00219         << "[SiStripCommissioningSource::" << __func__ << "]"
00220         << " NULL value for filename! No root file saved!";
00221     }
00222   
00223     // reset
00224     calib1_.vNumOfEntries_.clear();
00225     calib1_.vNumOfEntries_.resize(nBins_,0);
00226     calib2_.vNumOfEntries_.clear();
00227     calib2_.vNumOfEntries_.resize(nBins_,0);
00228   
00229     // set new parameter values
00230     lastISHA_=isha;
00231     lastVFS_=vfs;
00232     // set the calchan value in the correspond string
00233     ishaElement_->Fill(lastISHA_);
00234     vfsElement_->Fill(lastVFS_);
00235     calchanElement_->Fill(lastCalchan_);
00236   }
00237 }
00238 
00239 // -----------------------------------------------------------------------------
00240 // 
00241 void CalibrationScanTask::directory( std::stringstream& dir,
00242                                             uint32_t run_number ) {
00243 
00244   // Get details about host
00245   char hn[256];
00246   gethostname( hn, sizeof(hn) );
00247   struct hostent* he;
00248   he = gethostbyname(hn);
00249 
00250   // Extract host name and ip
00251   std::string host_name;
00252   std::string host_ip;
00253   if ( he ) { 
00254     host_name = std::string(he->h_name);
00255     host_ip = std::string( inet_ntoa( *(struct in_addr*)(he->h_addr) ) );
00256   } else {
00257     host_name = "unknown.cern.ch";
00258     host_ip = "255.255.255.255";
00259   }
00260 
00261   // Reformat IP address
00262   std::string::size_type pos = 0;
00263   std::stringstream ip;
00264   //for ( uint16_t ii = 0; ii < 4; ++ii ) {
00265   while ( pos != std::string::npos ) {
00266     std::string::size_type tmp = host_ip.find(".",pos);
00267     if ( tmp != std::string::npos ) {
00268       ip << std::setw(3)
00269          << std::setfill('0')
00270          << host_ip.substr( pos, tmp-pos ) 
00271          << ".";
00272       pos = tmp+1; // skip the delimiter "."
00273     } else {
00274       ip << std::setw(3)
00275          << std::setfill('0')
00276          << host_ip.substr( pos );
00277       pos = std::string::npos;
00278     }
00279   }
00280   
00281   // Get pid
00282   pid_t pid = getpid();
00283 
00284   // Construct string
00285   dir << std::setw(8) 
00286       << std::setfill('0') 
00287       << run_number
00288       << "_" 
00289       << ip.str()
00290       << "_"
00291       << std::setw(5) 
00292       << std::setfill('0') 
00293       << pid;
00294 
00295 }