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
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
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
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
00112
00113 checkAndSave(summary.isha(),summary.vfs());
00114
00115 int bin = (100-summary.latency())*8+(7-summary.calSel());
00116
00117
00118
00119
00120
00121
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
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();
00131 }
00132
00133
00134
00135 void CalibrationScanTask::update() {
00136
00137 updateHistoSet( calib1_ );
00138 updateHistoSet( calib2_ );
00139 }
00140
00141
00142
00143 void CalibrationScanTask::checkAndSave(const uint16_t& isha, const uint16_t& vfs ) {
00144
00145 if(lastISHA_==1000 && lastVFS_==1000) {
00146 lastISHA_ = isha;
00147 lastVFS_ = vfs;
00148 }
00149
00150
00151 ishaElement_->Fill(lastISHA_);
00152 vfsElement_->Fill(lastVFS_);
00153 calchanElement_->Fill(lastCalchan_);
00154
00155
00156
00157 if(lastISHA_!=isha || lastVFS_!=vfs) {
00158
00159
00160 update();
00161
00162
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
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
00189 std::string scratch = "SCRATCH";
00190 std::string dir = "";
00191 if ( getenv(scratch.c_str()) != NULL ) {
00192 dir = getenv(scratch.c_str());
00193 }
00194
00195
00196 std::stringstream ss;
00197 if ( !dir.empty() ) { ss << dir << "/"; }
00198 else { ss << "/tmp/"; }
00199 ss << name << "_";
00200 directory(ss,run_);
00201 ss << "_ISHA" << lastISHA_ << "_VFS" << lastVFS_;
00202 ss << "_000";
00203 ss << ".root";
00204 if ( !filename_.empty() ) {
00205 if(ifstream(ss.str().c_str(),ifstream::in).fail()) {
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
00224 calib1_.vNumOfEntries_.clear();
00225 calib1_.vNumOfEntries_.resize(nBins_,0);
00226 calib2_.vNumOfEntries_.clear();
00227 calib2_.vNumOfEntries_.resize(nBins_,0);
00228
00229
00230 lastISHA_=isha;
00231 lastVFS_=vfs;
00232
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
00245 char hn[256];
00246 gethostname( hn, sizeof(hn) );
00247 struct hostent* he;
00248 he = gethostbyname(hn);
00249
00250
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
00262 std::string::size_type pos = 0;
00263 std::stringstream ip;
00264
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;
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
00282 pid_t pid = getpid();
00283
00284
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 }