CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_9_patch3/src/DQM/SiStripCommissioningDbClients/src/CalibrationHistosUsingDb.cc

Go to the documentation of this file.
00001 // Last commit: $Id: CalibrationHistosUsingDb.cc,v 1.12 2009/11/10 14:49:02 lowette Exp $
00002 
00003 #include "DQM/SiStripCommissioningDbClients/interface/CalibrationHistosUsingDb.h"
00004 #include "CondFormats/SiStripObjects/interface/CalibrationAnalysis.h"
00005 #include "DataFormats/SiStripCommon/interface/SiStripConstants.h"
00006 #include "DataFormats/SiStripCommon/interface/SiStripFecKey.h"
00007 #include "DQM/SiStripCommon/interface/ExtractTObject.h"
00008 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00009 #include <iostream>
00010 
00011 using namespace sistrip;
00012 
00013 // -----------------------------------------------------------------------------
00015 std::string getBasePath (const std::string &path)
00016 {
00017   return path.substr(0,path.find(std::string(sistrip::root_) + "/")+sizeof(sistrip::root_) );
00018 }
00019 
00020 // -----------------------------------------------------------------------------
00022 CalibrationHistosUsingDb::CalibrationHistosUsingDb( const edm::ParameterSet & pset,
00023                                                     DQMStore* bei,
00024                                                     SiStripConfigDb* const db,
00025                                                     const sistrip::RunType& task ) 
00026   : CommissioningHistograms( pset.getParameter<edm::ParameterSet>("CalibrationParameters"),
00027                              bei,
00028                              task ),
00029     CommissioningHistosUsingDb( db,
00030                                 task ),
00031     CalibrationHistograms( pset.getParameter<edm::ParameterSet>("CalibrationParameters"),
00032                            bei,
00033                            task )
00034 {
00035   LogTrace(mlDqmClient_) 
00036     << "[CalibrationHistosUsingDb::" << __func__ << "]"
00037     << " Constructing object...";
00038   // Load and dump the current ISHA/VFS values. This is used by the standalone analysis script
00039   const SiStripConfigDb::DeviceDescriptionsRange & apvDescriptions = db->getDeviceDescriptions(APV25);
00040   for(SiStripConfigDb::DeviceDescriptionsV::const_iterator apv = apvDescriptions.begin();apv!=apvDescriptions.end();++apv) {
00041     apvDescription* desc = dynamic_cast<apvDescription*>( *apv );
00042     if ( !desc ) { continue; }
00043     // Retrieve device addresses from device description
00044     const SiStripConfigDb::DeviceAddress& addr = db->deviceAddress(*desc);
00045     std::stringstream bin;
00046     bin        << std::setw(1) << std::setfill('0') << addr.fecCrate_;
00047     bin << "." << std::setw(2) << std::setfill('0') << addr.fecSlot_;
00048     bin << "." << std::setw(1) << std::setfill('0') << addr.fecRing_;
00049     bin << "." << std::setw(3) << std::setfill('0') << addr.ccuAddr_;
00050     bin << "." << std::setw(2) << std::setfill('0') << addr.ccuChan_;
00051     bin << "." << desc->getAddress();
00052     LogTrace(mlDqmClient_) << "Present values for ISHA/VFS of APV " 
00053                            << bin.str() << " : " 
00054                            << static_cast<uint16_t>(desc->getIsha()) << " " << static_cast<uint16_t>(desc->getVfs());
00055   }
00056   // Load the histograms with the results
00057   std::string pwd = bei->pwd();
00058   std::string ishaPath = getBasePath(pwd);
00059   ishaPath += "/ControlView/isha";
00060   LogTrace(mlDqmClient_) << "Looking for " << ishaPath;
00061   ishaHistogram_ = ExtractTObject<TH1F>().extract( bei->get(ishaPath) );
00062   std::string vfsPath = getBasePath(pwd);
00063   vfsPath += "/ControlView/vfs";
00064   LogTrace(mlDqmClient_) << "Looking for " << vfsPath;
00065   vfsHistogram_ = ExtractTObject<TH1F>().extract( bei->get(vfsPath) );
00066   
00067 }
00068 
00069 // -----------------------------------------------------------------------------
00071 CalibrationHistosUsingDb::~CalibrationHistosUsingDb() {
00072   LogTrace(mlDqmClient_) 
00073     << "[CalibrationHistosUsingDb::" << __func__ << "]"
00074     << " Destructing object...";
00075 }
00076 
00077 // -----------------------------------------------------------------------------
00079 void CalibrationHistosUsingDb::uploadConfigurations() {
00080   
00081   LogTrace(mlDqmClient_)
00082     << "[CalibrationHistosUsingDb::" << __func__ << "]" << ishaHistogram_ << " " << vfsHistogram_;
00083 
00084   if(!ishaHistogram_ && !vfsHistogram_) return;
00085 
00086   if ( !db() ) {
00087     edm::LogWarning(mlDqmClient_) 
00088       << "[CalibrationHistosUsingDb::" << __func__ << "]"
00089       << " NULL pointer to SiStripConfigDb interface!"
00090       << " Aborting upload...";
00091     return;
00092   }
00093   
00094   // Update all APV device descriptions with new ISHA and VFS settings
00095   SiStripConfigDb::DeviceDescriptionsRange devices = db()->getDeviceDescriptions();
00096   update( devices );
00097   if ( doUploadConf() ) {
00098     edm::LogVerbatim(mlDqmClient_)
00099       << "[CalibrationHistosUsingDb::" << __func__ << "]"
00100       << " Uploading ISHA/VFS settings to DB...";
00101     db()->uploadDeviceDescriptions();
00102     edm::LogVerbatim(mlDqmClient_)
00103       << "[CalibrationHistosUsingDb::" << __func__ << "]"
00104       << " Uploaded ISHA/VFS settings to DB!";
00105   } else {
00106     edm::LogWarning(mlDqmClient_)
00107       << "[CalibrationHistosUsingDb::" << __func__ << "]"
00108       << " TEST only! No ISHA/VFS settings will be uploaded to DB...";
00109   }
00110 
00111   LogTrace(mlDqmClient_)
00112     << "[CalibrationHistosUsingDb::" << __func__ << "]"
00113     << " Upload of ISHA/VFS settings to DB finished!";
00114 
00115 }
00116 
00117 // -----------------------------------------------------------------------------
00119 void CalibrationHistosUsingDb::update( SiStripConfigDb::DeviceDescriptionsRange& devices ) {
00120   
00121   if(!ishaHistogram_ && !vfsHistogram_) return;
00122   
00123   // Iterate through devices and update device descriptions
00124   SiStripConfigDb::DeviceDescriptionsV::const_iterator idevice;
00125   for ( idevice = devices.begin(); idevice != devices.end(); idevice++ ) {
00126 
00127     // Check device type
00128     if ( (*idevice)->getDeviceType() != APV25 ) { continue; }
00129 
00130     // Cast to retrieve appropriate description object
00131     apvDescription* desc = dynamic_cast<apvDescription*>( *idevice );
00132     if ( !desc ) { continue; }
00133 
00134     // Retrieve the device address from device description
00135     const SiStripConfigDb::DeviceAddress& addr = db()->deviceAddress(*desc);
00136 
00137     // Construct the string for that address
00138     std::stringstream bin;
00139     bin        << std::setw(1) << std::setfill('0') << addr.fecCrate_;
00140     bin << "." << std::setw(2) << std::setfill('0') << addr.fecSlot_;
00141     bin << "." << std::setw(1) << std::setfill('0') << addr.fecRing_;
00142     bin << "." << std::setw(3) << std::setfill('0') << addr.ccuAddr_;
00143     bin << "." << std::setw(2) << std::setfill('0') << addr.ccuChan_;
00144     bin << "." << desc->getAddress();
00145 
00146     // Iterate over the histo bins and find the right one
00147     for(int i = 1;i <= ishaHistogram_->GetNbinsX(); ++i) {
00148       std::string label = ishaHistogram_->GetXaxis()->GetBinLabel(i);
00149       if(label == bin.str()) {
00150         desc->setIsha( (int)round(ishaHistogram_->GetBinContent(i)) );
00151         LogDebug(mlDqmClient_) << "Setting ISHA to " << ((int)round(ishaHistogram_->GetBinContent(i))) << " for " << label;
00152       }
00153     }
00154     for(int i = 1;i <= vfsHistogram_->GetNbinsX(); ++i) {
00155       std::string label = vfsHistogram_->GetXaxis()->GetBinLabel(i);
00156       if(label == bin.str()) {
00157         desc->setVfs( (int)round(vfsHistogram_->GetBinContent(i)) );
00158         LogDebug(mlDqmClient_) << "Setting VFS to " << ((int)round(vfsHistogram_->GetBinContent(i))) << " for " << label;
00159       }
00160     }
00161     
00162   }
00163 
00164 }
00165 
00166 // -----------------------------------------------------------------------------
00168 void CalibrationHistosUsingDb::create( SiStripConfigDb::AnalysisDescriptionsV& desc,
00169                                        Analysis analysis) {
00170 
00171   CalibrationAnalysis* anal = dynamic_cast<CalibrationAnalysis*>( analysis->second );
00172   if ( !anal ) { return; }
00173 
00174   SiStripFecKey fec_key( anal->fecKey() );
00175   SiStripFedKey fed_key( anal->fedKey() );
00176 
00177   std::ofstream ofile("calibrationResults.txt",ios_base::app);
00178   for ( uint16_t iapv = 0; iapv < 2; ++iapv ) {
00179 
00180     // Create description
00181     CalibrationAnalysisDescription *tmp;
00182     tmp = new CalibrationAnalysisDescription(anal->amplitudeMean()[iapv],
00183                                              anal->tailMean()[iapv],
00184                                              anal->riseTimeMean()[iapv],
00185                                              anal->timeConstantMean()[iapv],
00186                                              anal->smearingMean()[iapv],
00187                                              anal->chi2Mean()[iapv],
00188                                              anal->deconvMode(),
00189                                              fec_key.fecCrate(),
00190                                              fec_key.fecSlot(),
00191                                              fec_key.fecRing(),
00192                                              fec_key.ccuAddr(),
00193                                              fec_key.ccuChan(),
00194                                              SiStripFecKey::i2cAddr( fec_key.lldChan(), !iapv ),
00195                                              db()->dbParams().partitions().begin()->second.partitionName(),
00196                                              db()->dbParams().partitions().begin()->second.runNumber(),
00197                                              anal->isValid(),
00198                                              "",
00199                                              fed_key.fedId(),
00200                                              fed_key.feUnit(),
00201                                              fed_key.feChan(),
00202                                              fed_key.fedApv(),
00203                                              calchan_,
00204                                              isha_,
00205                                              vfs_ );
00206   
00207     // debug simplified printout in text file
00208     ofile << " " <<  anal->amplitudeMean()[iapv]
00209           << " " <<  anal->tailMean()[iapv]
00210           << " " <<  anal->riseTimeMean()[iapv]
00211           << " " <<  anal->timeConstantMean()[iapv]
00212           << " " <<  anal->smearingMean()[iapv]
00213           << " " <<  anal->chi2Mean()[iapv]
00214           << " " <<  anal->deconvMode()
00215           << " " <<  fec_key.fecCrate()
00216           << " " <<  fec_key.fecSlot()
00217           << " " <<  fec_key.fecRing()
00218           << " " <<  fec_key.ccuAddr()
00219           << " " <<  fec_key.ccuChan()
00220           << " " <<  SiStripFecKey::i2cAddr( fec_key.lldChan(), !iapv )
00221           << " " <<  db()->dbParams().partitions().begin()->second.partitionName()
00222           << " " <<  db()->dbParams().partitions().begin()->second.runNumber()
00223           << " " <<  fed_key.fedId()
00224           << " " <<  fed_key.feUnit()
00225           << " " <<  fed_key.feChan()
00226           << " " <<  fed_key.fedApv()
00227           << " " <<  calchan_
00228           << " " <<  isha_
00229           << " " <<  vfs_ << std::endl;
00230 
00231     // Add comments
00232     typedef std::vector<std::string> Strings;
00233     Strings errors = anal->getErrorCodes();
00234     Strings::const_iterator istr = errors.begin();
00235     Strings::const_iterator jstr = errors.end();
00236     for ( ; istr != jstr; ++istr ) { tmp->addComments( *istr ); }
00237   
00238     // Store description
00239     desc.push_back( tmp );
00240   }
00241   ofile.close();
00242 
00243 }
00244