CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_1/src/DQM/SiStripCommissioningClients/src/SiStripCommissioningOfflineClient.cc

Go to the documentation of this file.
00001 // Last commit: $Id: SiStripCommissioningOfflineClient.cc,v 1.44 2010/01/04 16:47:06 lowette Exp $
00002 
00003 #include "DQM/SiStripCommissioningClients/interface/SiStripCommissioningOfflineClient.h"
00004 #include "DataFormats/SiStripCommon/interface/SiStripEnumsAndStrings.h"
00005 #include "DataFormats/SiStripCommon/interface/SiStripHistoTitle.h"
00006 #include "DataFormats/SiStripCommon/interface/SiStripFecKey.h"
00007 #include "DQM/SiStripCommissioningClients/interface/FastFedCablingHistograms.h"
00008 #include "DQM/SiStripCommissioningClients/interface/FedCablingHistograms.h"
00009 #include "DQM/SiStripCommissioningClients/interface/ApvTimingHistograms.h"
00010 #include "DQM/SiStripCommissioningClients/interface/OptoScanHistograms.h"
00011 #include "DQM/SiStripCommissioningClients/interface/VpspScanHistograms.h"
00012 #include "DQM/SiStripCommissioningClients/interface/PedestalsHistograms.h"
00013 #include "DQM/SiStripCommissioningClients/interface/PedsOnlyHistograms.h"
00014 #include "DQM/SiStripCommissioningClients/interface/NoiseHistograms.h"
00015 #include "DQM/SiStripCommissioningClients/interface/SamplingHistograms.h"
00016 #include "DQM/SiStripCommissioningClients/interface/CalibrationHistograms.h"
00017 #include "FWCore/ServiceRegistry/interface/Service.h"
00018 #include "DQMServices/Core/interface/DQMStore.h"
00019 #include "DQMServices/Core/interface/MonitorElement.h"
00020 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00021 #include "FWCore/ParameterSet/interface/FileInPath.h"
00022 #include <boost/cstdint.hpp>
00023 #include <iostream>
00024 #include <iomanip>
00025 #include <fstream>
00026 #include <sstream>
00027 #include <sys/types.h>
00028 #include <dirent.h>
00029 #include <errno.h>
00030 #include "TProfile.h"
00031 
00032 using namespace sistrip;
00033 
00034 // -----------------------------------------------------------------------------
00035 // 
00036 SiStripCommissioningOfflineClient::SiStripCommissioningOfflineClient( const edm::ParameterSet& pset ) 
00037   : bei_( edm::Service<DQMStore>().operator->() ),
00038     histos_(0),
00039     //inputFiles_( pset.getUntrackedParameter< std::vector<std::string> >( "InputRootFiles", std::vector<std::string>() ) ),
00040     outputFileName_( pset.getUntrackedParameter<std::string>( "OutputRootFile", "" ) ),
00041     collateHistos_( !pset.getUntrackedParameter<bool>( "UseClientFile", false ) ),
00042     analyzeHistos_( pset.getUntrackedParameter<bool>( "AnalyzeHistos", true ) ),
00043     xmlFile_( (pset.getUntrackedParameter<edm::FileInPath>( "SummaryXmlFile", edm::FileInPath() )).fullPath() ),
00044     createSummaryPlots_( false ),
00045     clientHistos_( false ), 
00046     uploadToDb_( false ), 
00047     runType_(sistrip::UNKNOWN_RUN_TYPE),
00048     runNumber_(0),
00049     map_(),
00050     plots_(),
00051     parameters_(pset)
00052 {
00053   LogTrace(mlDqmClient_)
00054     << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00055     << " Constructing object...";
00056   setInputFiles( inputFiles_,
00057                  pset.getUntrackedParameter<std::string>( "FilePath" ),
00058                  pset.getUntrackedParameter<uint32_t>("RunNumber"), 
00059                  collateHistos_ );
00060 }
00061 
00062 // -----------------------------------------------------------------------------
00063 // 
00064 SiStripCommissioningOfflineClient::~SiStripCommissioningOfflineClient() {
00065   LogTrace(mlDqmClient_)
00066     << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00067     << " Destructing object...";
00068 }
00069 
00070 // -----------------------------------------------------------------------------
00071 // 
00072 void SiStripCommissioningOfflineClient::beginRun( const edm::Run& run, const edm::EventSetup& setup ) {
00073   LogTrace(mlDqmClient_) 
00074     << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00075     << " Analyzing root file(s)...";
00076 
00077   // Check for null pointer
00078   if ( !bei_ ) {
00079     edm::LogError(mlDqmClient_)
00080       << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00081       << " NULL pointer to DQMStore!"
00082       << " Aborting...";
00083     return;
00084   }
00085   bei_->setVerbose(0);
00086 
00087   // Check if .root file can be opened
00088   std::vector<std::string>::const_iterator ifile = inputFiles_.begin();
00089   for ( ; ifile != inputFiles_.end(); ifile++ ) {
00090     ifstream root_file;
00091     root_file.open( ifile->c_str() );
00092     if( !root_file.is_open() ) {
00093       edm::LogError(mlDqmClient_)
00094         << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00095         << " The input root file \"" << *ifile
00096         << "\" could not be opened!"
00097         << " Please check the path and filename!";
00098     } else { 
00099       root_file.close(); 
00100       std::string::size_type found = ifile->find(sistrip::dqmClientFileName_);
00101       if ( found != std::string::npos && clientHistos_ ) {
00102         edm::LogError(mlDqmClient_)
00103           << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00104           << " The input root files appear to be a mixture"
00105           << " of \"Source\" and \"Client\" files!"
00106           << " Aborting...";
00107         return;
00108       }
00109       if ( found != std::string::npos && inputFiles_.size() != 1 ) {
00110         edm::LogError(mlDqmClient_)
00111           << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00112           << " There appear to be multiple input \"Client\" root files!"
00113           << " Aborting...";
00114         return;
00115       }
00116       if ( found != std::string::npos ) { clientHistos_ = true; }
00117     }
00118   }
00119   if ( clientHistos_ && inputFiles_.size() == 1 ) {
00120     edm::LogVerbatim(mlDqmClient_)
00121       << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00122       << " Collated histograms found in input root file \""
00123       << inputFiles_[0] << "\"";
00124   }
00125   
00126   // Check if .xml file can be opened
00127   if ( !xmlFile_.empty() ) {
00128     ifstream xml_file;
00129     xml_file.open( xmlFile_.c_str() );
00130     if( !xml_file.is_open() ) {
00131       edm::LogError(mlDqmClient_)
00132         << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00133         << " The SummaryPlot XML file \"" << xmlFile_
00134         << "\" could not be opened!"
00135         << " Please check the path and filename!"
00136         << " Aborting...";
00137       return;
00138     } else { 
00139       createSummaryPlots_ = true;
00140       xml_file.close(); 
00141     }
00142   }
00143 
00144   // Open root file(s) and create ME's
00145   if ( inputFiles_.empty() ) {
00146     edm::LogError(mlDqmClient_)
00147       << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00148       << " No input root files specified!";
00149     return;
00150   }
00151 
00152   edm::LogVerbatim(mlDqmClient_)
00153     << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00154     << " Opening root files. This may take some time!...";
00155   std::vector<std::string>::const_iterator jfile = inputFiles_.begin();
00156   for ( ; jfile != inputFiles_.end(); jfile++ ) {
00157     LogTrace(mlDqmClient_)
00158       << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00159       << " Opening root file \"" << *jfile
00160       << "\"... (This may take some time.)";
00161     if ( clientHistos_ ) {
00162       bei_->open( *jfile, false, sistrip::collate_, "" );
00163     } else { 
00164       bei_->open( *jfile, false, "SiStrip", sistrip::collate_ );
00165     }
00166     LogTrace(mlDqmClient_)
00167       << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00168       << " Opened root file \"" << *jfile << "\"!";
00169   }
00170   edm::LogVerbatim(mlDqmClient_)
00171     << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00172     << " Opened " << inputFiles_.size() << " root files!";
00173   
00174   // Retrieve list of histograms
00175   std::vector<std::string> contents;
00176   bei_->getContents( contents ); 
00177   
00178   // If using client file, remove "source" histograms from list
00179   if ( clientHistos_ ) {
00180     std::vector<std::string> temp;
00181     std::vector<std::string>::iterator istr = contents.begin();
00182     for ( ; istr != contents.end(); istr++ ) {
00183       if ( istr->find(sistrip::collate_) != std::string::npos ) { 
00184         temp.push_back( *istr );
00185       }
00186     }
00187     contents.clear();
00188     contents = temp;
00189   }
00190   
00191   // Some debug
00192   LogTrace(mlDqmClient_)
00193     << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00194     << " Found " << contents.size() 
00195     << " directories containing MonitorElements";
00196   
00197   // Some more debug
00198   if (0) {
00199     std::stringstream ss;
00200     ss << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00201        << " Directories found: " << std::endl;
00202     std::vector<std::string>::iterator istr = contents.begin();
00203     for ( ; istr != contents.end(); istr++ ) { ss << " " << *istr << std::endl; }
00204     LogTrace(mlDqmClient_) << ss.str();
00205   }
00206   
00207   // Extract run type from contents
00208   runType_ = CommissioningHistograms::runType( bei_, contents ); 
00209   
00210   // Extract run number from contents
00211   runNumber_ = CommissioningHistograms::runNumber( bei_, contents ); 
00212 
00213   // Copy custom information to the collated structure
00214   CommissioningHistograms::copyCustomInformation( bei_, contents );
00215 
00216   // Check runType
00217   if ( runType_ == sistrip::UNKNOWN_RUN_TYPE ) { 
00218     edm::LogError(mlDqmClient_)
00219       << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00220       << " Unknown commissioning runType: " 
00221       << SiStripEnumsAndStrings::runType( runType_ )
00222       << " and run number is " << runNumber_;
00223     return;
00224   } else {
00225     edm::LogVerbatim(mlDqmClient_)
00226       << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00227       << " Run type is " 
00228       << SiStripEnumsAndStrings::runType( runType_ )
00229       << " and run number is " << runNumber_;
00230   }
00231   
00232   // Open and parse "summary plot" xml file
00233   if ( createSummaryPlots_ ) {
00234     edm::LogVerbatim(mlDqmClient_)
00235       << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00236       << " Parsing summary plot XML file...";
00237     SummaryPlotXmlParser xml_file;
00238     xml_file.parseXML(xmlFile_);
00239     plots_ = xml_file.summaryPlots(runType_);
00240     edm::LogVerbatim(mlDqmClient_)
00241       << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00242       << " Parsed summary plot XML file and found " 
00243       << plots_.size() << " plots defined!";
00244   } else {
00245     edm::LogWarning(mlDqmClient_)
00246       << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00247       << " Null string for SummaryPlotXmlFile!"
00248       << " No summary plots will be created!";
00249   }
00250   
00251   // Some debug
00252   std::stringstream ss;
00253   ss << "[SiStripCommissioningOfflineClient::" << __func__ << "]" << std::endl
00254      << " Input root files      : ";
00255   if ( inputFiles_.empty() ) { ss << "(none)"; }
00256   else {
00257     std::vector<std::string>::const_iterator ifile = inputFiles_.begin();
00258     for ( ; ifile != inputFiles_.end(); ifile++ ) {
00259       if ( ifile != inputFiles_.begin() ) { 
00260         ss << std::setw(25) << std::setfill(' ') << ": ";
00261       }
00262       ss << "\"" << *ifile << "\"" << std::endl; 
00263     }
00264   }
00265   ss << " Run type              : \"" 
00266      << SiStripEnumsAndStrings::runType( runType_ ) << "\"" << std::endl
00267      << " Run number            :  " << runNumber_ << std::endl
00268      << " Summary plot XML file : ";
00269   if ( xmlFile_.empty() ) { ss << "(none)"; }
00270   else { ss << "\"" << xmlFile_ << "\""; }
00271   edm::LogVerbatim(mlDqmClient_) << ss.str();
00272 
00273   // Virtual method that creates CommissioningHistogram object
00274   LogTrace(mlDqmClient_)
00275     << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00276     << " Creating CommissioningHistogram object...";
00277   createHistos(parameters_, setup); 
00278   if ( histos_ ) {
00279     LogTrace(mlDqmClient_)
00280       << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00281       << " Created CommissioningHistogram object!";
00282   } else {
00283     edm::LogError(mlDqmClient_)
00284       << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00285       << " NULL pointer to CommissioningHistogram object!"
00286       << " Aborting...";
00287     return;
00288   }
00289   
00290   // Perform collation
00291   if ( histos_ ) { 
00292     histos_->extractHistograms( contents ); 
00293   }
00294   
00295   // Perform analysis
00296   if ( analyzeHistos_ ) { 
00297     LogTrace(mlDqmClient_)
00298       << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00299       << " Analyzing histograms...";
00300     if ( histos_ ) { histos_->histoAnalysis( true ); }
00301     LogTrace(mlDqmClient_)
00302       << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00303       << " Analyzed histograms!";
00304   } else {
00305     edm::LogWarning(mlDqmClient_)
00306       << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00307       << " No histogram analysis performed!";
00308   }
00309   
00310   // Create summary plots
00311   if ( createSummaryPlots_ ) { 
00312     edm::LogVerbatim(mlDqmClient_)
00313       << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00314       << " Generating summary plots...";
00315     std::vector<SummaryPlot>::const_iterator iplot =  plots_.begin();
00316     for ( ; iplot != plots_.end(); iplot++ ) {
00317       if ( histos_ ) { 
00318         histos_->createSummaryHisto( iplot->monitorable(),
00319                                      iplot->presentation(),
00320                                      iplot->level(),
00321                                      iplot->granularity() );
00322       }
00323     }
00324     edm::LogVerbatim(mlDqmClient_)
00325       << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00326       << " Generated summary plots!";
00327   } else {
00328     edm::LogWarning(mlDqmClient_)
00329       << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00330       << " No summary plots generated!";
00331   }
00332   
00333   // Save client root file
00334   if ( histos_ ) {
00335     bool save = parameters_.getUntrackedParameter<bool>( "SaveClientFile", true );
00336     if ( save ) { histos_->save( outputFileName_, runNumber_ ); }
00337     else {
00338       edm::LogVerbatim(mlDqmClient_)
00339         << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00340         << " Client file not saved!";
00341     }
00342   }
00343   
00344   // Virtual method to trigger the database upload
00345   uploadToConfigDb();
00346   
00347   // Print analyses
00348   if ( histos_ ) { 
00349     histos_->printAnalyses(); 
00350     histos_->printSummary(); 
00351   }
00352   
00353   // Remove all ME/CME objects
00354   if ( histos_ ) { histos_->remove(); } 
00355   
00356   edm::LogVerbatim(mlDqmClient_)
00357     << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00358     << " Finished analyzing root file(s)...";
00359   
00360 }
00361   
00362 // -----------------------------------------------------------------------------
00363 // 
00364 void SiStripCommissioningOfflineClient::analyze( const edm::Event& event, 
00365                                                  const edm::EventSetup& setup ) {
00366   if ( !(event.id().event()%10) ) {
00367     LogTrace(mlDqmClient_) 
00368       << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00369       << " Empty event loop! User can kill job...";
00370   }
00371 }
00372 
00373 // -----------------------------------------------------------------------------
00374 // 
00375 void SiStripCommissioningOfflineClient::endJob() {}
00376 
00377 // -----------------------------------------------------------------------------
00378 // 
00379 void SiStripCommissioningOfflineClient::createHistos( const edm::ParameterSet& pset, const edm::EventSetup& setup ) {
00380   
00381   // Check pointer
00382   if ( histos_ ) {
00383     edm::LogError(mlDqmClient_)
00384       << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00385       << " CommissioningHistogram object already exists!"
00386       << " Aborting...";
00387     return;
00388   } 
00389   
00390   // Check pointer to BEI
00391   if ( !bei_ ) {
00392     edm::LogError(mlDqmClient_)
00393       << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00394       << " NULL pointer to DQMStore!";
00395     return;
00396   }
00397 
00398   // Create "commissioning histograms" object 
00399   if      ( runType_ == sistrip::FAST_CABLING )         { histos_ = new FastFedCablingHistograms( pset, bei_ ); }
00400   else if ( runType_ == sistrip::FED_CABLING )          { histos_ = new FedCablingHistograms( pset, bei_ ); }
00401   else if ( runType_ == sistrip::APV_TIMING )           { histos_ = new ApvTimingHistograms( pset, bei_ ); }
00402   else if ( runType_ == sistrip::OPTO_SCAN )            { histos_ = new OptoScanHistograms( pset, bei_ ); }
00403   else if ( runType_ == sistrip::VPSP_SCAN )            { histos_ = new VpspScanHistograms( pset, bei_ ); }
00404   else if ( runType_ == sistrip::PEDESTALS )            { histos_ = new PedestalsHistograms( pset, bei_ ); }
00405   else if ( runType_ == sistrip::PEDS_ONLY )            { histos_ = new PedsOnlyHistograms( pset, bei_ ); }
00406   else if ( runType_ == sistrip::NOISE )                { histos_ = new NoiseHistograms( pset, bei_ ); }
00407   else if ( runType_ == sistrip::APV_LATENCY      ||
00408             runType_ == sistrip::FINE_DELAY           ) { histos_ = new SamplingHistograms( pset, bei_,runType_ ); }
00409   else if ( runType_ == sistrip::CALIBRATION      ||
00410             runType_ == sistrip::CALIBRATION_DECO ||
00411             runType_ == sistrip::CALIBRATION_SCAN ||
00412             runType_ == sistrip::CALIBRATION_SCAN_DECO) { histos_ = new CalibrationHistograms( pset, bei_,runType_ ); }
00413   else if ( runType_ == sistrip::UNDEFINED_RUN_TYPE   ) { 
00414     histos_ = 0; 
00415     edm::LogError(mlDqmClient_)
00416       << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00417       << " Undefined run type!";
00418     return;
00419   } else if ( runType_ == sistrip::UNKNOWN_RUN_TYPE )   { 
00420     histos_ = 0;
00421     edm::LogError(mlDqmClient_)
00422       << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00423       << " Unknown run type!";
00424     return;
00425   }
00426   histos_->configure(pset,setup);
00427 }
00428 
00429 // -----------------------------------------------------------------------------
00430 // 
00431 void SiStripCommissioningOfflineClient::setInputFiles( std::vector<std::string>& files,
00432                                                        const std::string path,
00433                                                        uint32_t run_number, 
00434                                                        bool collate_histos ) {
00435 
00436   std::string runStr;
00437   std::stringstream ss;
00438   ss << std::setfill('0') << std::setw(8) << run_number;
00439   runStr = ss.str();
00440   
00441   std::string nameStr = "";
00442   if ( !collate_histos ) { nameStr = "SiStripCommissioningClient_"; }
00443   else { nameStr = "SiStripCommissioningSource_"; }
00444 
00445   LogTrace("TEST") << " runStr " << runStr;
00446 
00447   // Open directory
00448   DIR* dp;
00449   struct dirent* dirp;
00450   if ( (dp = opendir(path.c_str())) == NULL ) {
00451     edm::LogError(mlDqmClient_) 
00452       << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00453       << " Error locating directory \"" << path
00454       << "\". No such directory!";
00455     return;
00456   }
00457 
00458   // Find compatible files
00459   while ( (dirp = readdir(dp)) != NULL ) {
00460     std::string fileName(dirp->d_name);
00461     bool goodName = ( fileName.find(nameStr) != std::string::npos );
00462     bool goodRun  = ( fileName.find(runStr) != std::string::npos );
00463     bool rootFile = ( fileName.find(".root") != std::string::npos );
00464     //bool rootFile = ( fileName.rfind(".root",5) == fileName.size()-5 );
00465     if ( goodName && goodRun && rootFile ) {
00466       std::string entry = path;
00467       entry += "/";
00468       entry += fileName;
00469       files.push_back(entry);
00470     }
00471   }
00472   closedir(dp);
00473 
00474   // Some debug  
00475   if ( !collate_histos && files.size() > 1 ) {
00476     std::stringstream ss;
00477     ss << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00478        << " Found more than one client file!";
00479     std::vector<std::string>::const_iterator ifile = files.begin();
00480     std::vector<std::string>::const_iterator jfile = files.end();
00481     for ( ; ifile != jfile; ++ifile ) { ss << std::endl << *ifile; }
00482     edm::LogError(mlDqmClient_) << ss.str();
00483   } else if ( files.empty() ) {
00484     edm::LogError(mlDqmClient_)
00485       << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00486       << " No input files found!" ;
00487   }
00488 
00489 }
00490