CMS 3D CMS Logo

SiStripCommissioningOfflineClient.cc

Go to the documentation of this file.
00001 // Last commit: $Id: SiStripCommissioningOfflineClient.cc,v 1.40 2008/07/09 15:59:51 bainbrid 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/SiStripCommissioningClient.h"
00008 #include "DQM/SiStripCommissioningClients/interface/FastFedCablingHistograms.h"
00009 #include "DQM/SiStripCommissioningClients/interface/FedCablingHistograms.h"
00010 #include "DQM/SiStripCommissioningClients/interface/ApvTimingHistograms.h"
00011 #include "DQM/SiStripCommissioningClients/interface/OptoScanHistograms.h"
00012 #include "DQM/SiStripCommissioningClients/interface/VpspScanHistograms.h"
00013 #include "DQM/SiStripCommissioningClients/interface/PedestalsHistograms.h"
00014 #include "DQM/SiStripCommissioningClients/interface/PedsOnlyHistograms.h"
00015 #include "DQM/SiStripCommissioningClients/interface/NoiseHistograms.h"
00016 #include "DQM/SiStripCommissioningClients/interface/SamplingHistograms.h"
00017 #include "DQM/SiStripCommissioningClients/interface/CalibrationHistograms.h"
00018 #include "DQMServices/Core/interface/DQMStore.h"
00019 #include "DQMServices/Core/interface/DQMOldReceiver.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 #include "DQMServices/Core/interface/MonitorElement.h"
00032 
00033 using namespace sistrip;
00034 
00035 // -----------------------------------------------------------------------------
00036 // 
00037 SiStripCommissioningOfflineClient::SiStripCommissioningOfflineClient( const edm::ParameterSet& pset ) 
00038   : mui_( new DQMOldReceiver() ),
00039     histos_(0),
00040     //inputFiles_( pset.getUntrackedParameter< std::vector<std::string> >( "InputRootFiles", std::vector<std::string>() ) ),
00041     outputFileName_( pset.getUntrackedParameter<std::string>( "OutputRootFile", "" ) ),
00042     collateHistos_( !pset.getUntrackedParameter<bool>( "UseClientFile", false ) ),
00043     analyzeHistos_( pset.getUntrackedParameter<bool>( "AnalyzeHistos", true ) ),
00044     xmlFile_( (pset.getUntrackedParameter<edm::FileInPath>( "SummaryXmlFile", edm::FileInPath() )).fullPath() ),
00045     createSummaryPlots_( false ),
00046     clientHistos_( false ), 
00047     uploadToDb_( false ), 
00048     runType_(sistrip::UNKNOWN_RUN_TYPE),
00049     runNumber_(0),
00050     map_(),
00051     plots_(),
00052     parameters_(pset)
00053 {
00054   LogTrace(mlDqmClient_)
00055     << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00056     << " Constructing object...";
00057   setInputFiles( inputFiles_,
00058                  pset.getUntrackedParameter<std::string>( "FilePath" ),
00059                  pset.getUntrackedParameter<uint32_t>("RunNumber"), 
00060                  collateHistos_ );
00061 }
00062 
00063 // -----------------------------------------------------------------------------
00064 // 
00065 SiStripCommissioningOfflineClient::~SiStripCommissioningOfflineClient() {
00066   LogTrace(mlDqmClient_)
00067     << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00068     << " Destructing object...";
00069 }
00070 
00071 // -----------------------------------------------------------------------------
00072 // 
00073 void SiStripCommissioningOfflineClient::beginJob( const edm::EventSetup& setup ) {
00074   LogTrace(mlDqmClient_) 
00075     << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00076     << " Analyzing root file(s)...";
00077 
00078   // Check for null pointer
00079   if ( !mui_ ) {
00080     edm::LogError(mlDqmClient_)
00081       << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00082       << " NULL pointer to DQMOldReceiver!"
00083       << " Aborting...";
00084     return;
00085   }
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   // Retrieve BEI and check for null pointer 
00145   DQMStore* bei = mui_->getBEInterface();
00146   if ( !bei ) {
00147     edm::LogError(mlDqmClient_)
00148       << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00149       << " NULL pointer to DQMStore!"
00150       << " Aborting...";
00151     return;
00152   }
00153   bei->setVerbose(0);
00154   
00155   // Open root file(s) and create ME's
00156   if ( inputFiles_.empty() ) {
00157     edm::LogError(mlDqmClient_)
00158       << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00159       << " No input root files specified!";
00160     return;
00161   }
00162 
00163   edm::LogVerbatim(mlDqmClient_)
00164     << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00165     << " Opening root files. This may take some time!...";
00166   std::vector<std::string>::const_iterator jfile = inputFiles_.begin();
00167   for ( ; jfile != inputFiles_.end(); jfile++ ) {
00168     LogTrace(mlDqmClient_)
00169       << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00170       << " Opening root file \"" << *jfile
00171       << "\"... (This may take some time.)";
00172 #ifdef USING_NEW_COLLATE_METHODS
00173     if ( clientHistos_ ) {
00174       bei->open( *jfile, false, sistrip::collate_, "" );
00175     } else { 
00176       bei->open( *jfile, false, "SiStrip", sistrip::collate_ );
00177     }
00178 #else
00179     bei->open( *jfile );
00180 #endif
00181     LogTrace(mlDqmClient_)
00182       << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00183       << " Opened root file \"" << *jfile << "\"!";
00184   }
00185   edm::LogVerbatim(mlDqmClient_)
00186     << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00187     << " Opened " << inputFiles_.size() << " root files!";
00188   
00189   // Retrieve list of histograms
00190   std::vector<std::string> contents;
00191   bei->getContents( contents ); 
00192   
00193   // If using client file, remove "source" histograms from list
00194   if ( clientHistos_ ) {
00195     std::vector<std::string> temp;
00196     std::vector<std::string>::iterator istr = contents.begin();
00197     for ( ; istr != contents.end(); istr++ ) {
00198       if ( istr->find(sistrip::collate_) != std::string::npos ) { 
00199         temp.push_back( *istr );
00200       }
00201     }
00202     contents.clear();
00203     contents = temp;
00204   }
00205   
00206   // Some debug
00207   LogTrace(mlDqmClient_)
00208     << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00209     << " Found " << contents.size() 
00210     << " directories containing MonitorElements";
00211   
00212   // Some more debug
00213   if (0) {
00214     std::stringstream ss;
00215     ss << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00216        << " Directories found: " << std::endl;
00217     std::vector<std::string>::iterator istr = contents.begin();
00218     for ( ; istr != contents.end(); istr++ ) { ss << " " << *istr << std::endl; }
00219     LogTrace(mlDqmClient_) << ss.str();
00220   }
00221   
00222   // Extract run type from contents
00223   runType_ = CommissioningHistograms::runType( bei, contents ); 
00224   
00225   // Extract run number from contents
00226   runNumber_ = CommissioningHistograms::runNumber( bei, contents ); 
00227 
00228   // Copy custom information to the collated structure
00229   CommissioningHistograms::copyCustomInformation( bei, contents );
00230 
00231   // Check runType
00232   if ( runType_ == sistrip::UNKNOWN_RUN_TYPE ) { 
00233     edm::LogError(mlDqmClient_)
00234       << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00235       << " Unknown commissioning runType: " 
00236       << SiStripEnumsAndStrings::runType( runType_ )
00237       << " and run number is " << runNumber_;
00238     return;
00239   } else {
00240     edm::LogVerbatim(mlDqmClient_)
00241       << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00242       << " Run type is " 
00243       << SiStripEnumsAndStrings::runType( runType_ )
00244       << " and run number is " << runNumber_;
00245   }
00246   
00247   // Open and parse "summary plot" xml file
00248   if ( createSummaryPlots_ ) {
00249     edm::LogVerbatim(mlDqmClient_)
00250       << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00251       << " Parsing summary plot XML file...";
00252     SummaryPlotXmlParser xml_file;
00253     xml_file.parseXML(xmlFile_);
00254     plots_ = xml_file.summaryPlots(runType_);
00255     edm::LogVerbatim(mlDqmClient_)
00256       << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00257       << " Parsed summary plot XML file and found " 
00258       << plots_.size() << " plots defined!";
00259   } else {
00260     edm::LogWarning(mlDqmClient_)
00261       << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00262       << " Null string for SummaryPlotXmlFile!"
00263       << " No summary plots will be created!";
00264   }
00265   
00266   // Some debug
00267   std::stringstream ss;
00268   ss << "[SiStripCommissioningOfflineClient::" << __func__ << "]" << std::endl
00269      << " Input root files      : ";
00270   if ( inputFiles_.empty() ) { ss << "(none)"; }
00271   else {
00272     std::vector<std::string>::const_iterator ifile = inputFiles_.begin();
00273     for ( ; ifile != inputFiles_.end(); ifile++ ) {
00274       if ( ifile != inputFiles_.begin() ) { 
00275         ss << std::setw(25) << std::setfill(' ') << ": ";
00276       }
00277       ss << "\"" << *ifile << "\"" << std::endl; 
00278     }
00279   }
00280   ss << " Run type              : \"" 
00281      << SiStripEnumsAndStrings::runType( runType_ ) << "\"" << std::endl
00282      << " Run number            :  " << runNumber_ << std::endl
00283      << " Summary plot XML file : ";
00284   if ( xmlFile_.empty() ) { ss << "(none)"; }
00285   else { ss << "\"" << xmlFile_ << "\""; }
00286   edm::LogVerbatim(mlDqmClient_) << ss.str();
00287 
00288   // Virtual method that creates CommissioningHistogram object
00289   LogTrace(mlDqmClient_)
00290     << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00291     << " Creating CommissioningHistogram object...";
00292   createHistos(parameters_, setup); 
00293   if ( histos_ ) {
00294     LogTrace(mlDqmClient_)
00295       << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00296       << " Created CommissioningHistogram object!";
00297   } else {
00298     edm::LogError(mlDqmClient_)
00299       << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00300       << " NULL pointer to CommissioningHistogram object!"
00301       << " Aborting...";
00302     return;
00303   }
00304   
00305   // Perform collation
00306   if ( histos_ ) { 
00307 #ifdef USING_NEW_COLLATE_METHODS
00308     histos_->extractHistograms( contents ); 
00309 #else
00310     if ( !clientHistos_ ) { histos_->createCollations( contents ); }
00311     else { histos_->extractHistograms( contents ); }
00312 #endif
00313   }
00314 
00315   // Trigger update methods
00316 #ifndef USING_NEW_COLLATE_METHODS
00317   edm::LogVerbatim(mlDqmClient_)
00318     << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00319     << " Triggering update of histograms..."
00320     << " (This may take some time!)";
00321   if ( mui_ ) { mui_->doSummary(); }
00322   edm::LogVerbatim(mlDqmClient_)
00323     << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00324     << " Triggered update of histograms!";
00325 #endif
00326   
00327   // Perform analysis
00328   if ( analyzeHistos_ ) { 
00329     LogTrace(mlDqmClient_)
00330       << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00331       << " Analyzing histograms...";
00332     if ( histos_ ) { histos_->histoAnalysis( true ); }
00333     LogTrace(mlDqmClient_)
00334       << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00335       << " Analyzed histograms!";
00336   } else {
00337     edm::LogWarning(mlDqmClient_)
00338       << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00339       << " No histogram analysis performed!";
00340   }
00341   
00342   // Create summary plots
00343   if ( createSummaryPlots_ ) { 
00344     edm::LogVerbatim(mlDqmClient_)
00345       << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00346       << " Generating summary plots...";
00347     std::vector<SummaryPlot>::const_iterator iplot =  plots_.begin();
00348     for ( ; iplot != plots_.end(); iplot++ ) {
00349       if ( histos_ ) { 
00350         histos_->createSummaryHisto( iplot->monitorable(),
00351                                      iplot->presentation(),
00352                                      iplot->level(),
00353                                      iplot->granularity() );
00354       }
00355     }
00356     edm::LogVerbatim(mlDqmClient_)
00357       << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00358       << " Generated summary plots!";
00359   } else {
00360     edm::LogWarning(mlDqmClient_)
00361       << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00362       << " No summary plots generated!";
00363   }
00364   
00365   // Save client root file
00366   if ( histos_ ) {
00367     bool save = parameters_.getUntrackedParameter<bool>( "SaveClientFile", true );
00368     if ( save ) { histos_->save( outputFileName_, runNumber_ ); }
00369     else {
00370       edm::LogVerbatim(mlDqmClient_)
00371         << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00372         << " Client file not saved!";
00373     }
00374   }
00375   
00376   // Virtual method to trigger the database upload
00377   uploadToConfigDb();
00378   
00379   // Print analyses
00380   if ( histos_ ) { 
00381     histos_->printAnalyses(); 
00382     histos_->printSummary(); 
00383   }
00384   
00385   // Remove all ME/CME objects
00386   if ( histos_ ) { histos_->remove(); } 
00387   
00388   edm::LogVerbatim(mlDqmClient_)
00389     << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00390     << " Finished analyzing root file(s)...";
00391   
00392 }
00393   
00394 // -----------------------------------------------------------------------------
00395 // 
00396 void SiStripCommissioningOfflineClient::analyze( const edm::Event& event, 
00397                                                  const edm::EventSetup& setup ) {
00398   if ( !(event.id().event()%10) ) {
00399     LogTrace(mlDqmClient_) 
00400       << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00401       << " Empty event loop! User can kill job...";
00402   }
00403 }
00404 
00405 // -----------------------------------------------------------------------------
00406 // 
00407 void SiStripCommissioningOfflineClient::endJob() {}
00408 
00409 // -----------------------------------------------------------------------------
00410 // 
00411 void SiStripCommissioningOfflineClient::createHistos( const edm::ParameterSet& pset, const edm::EventSetup& setup) {
00412   
00413   // Check pointer
00414   if ( histos_ ) {
00415     edm::LogError(mlDqmClient_)
00416       << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00417       << " CommissioningHistogram object already exists!"
00418       << " Aborting...";
00419     return;
00420   } 
00421   
00422   // Check pointer to MUI
00423   if ( !mui_ ) {
00424     edm::LogError(mlDqmClient_)
00425       << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00426       << " NULL pointer to DQMOldReceiver!";
00427     return;
00428   }
00429 
00430   // Create "commissioning histograms" object 
00431   if      ( runType_ == sistrip::FAST_CABLING )         { histos_ = new FastFedCablingHistograms( mui_ ); }
00432   else if ( runType_ == sistrip::FED_CABLING )          { histos_ = new FedCablingHistograms(  mui_ ); }
00433   else if ( runType_ == sistrip::APV_TIMING )           { histos_ = new ApvTimingHistograms(   mui_ ); }
00434   else if ( runType_ == sistrip::OPTO_SCAN )            { histos_ = new OptoScanHistograms(    mui_ ); }
00435   else if ( runType_ == sistrip::VPSP_SCAN )            { histos_ = new VpspScanHistograms(    mui_ ); }
00436   else if ( runType_ == sistrip::PEDESTALS )            { histos_ = new PedestalsHistograms(   mui_ ); }
00437   else if ( runType_ == sistrip::PEDS_ONLY )            { histos_ = new PedsOnlyHistograms(   mui_ ); }
00438   else if ( runType_ == sistrip::NOISE )                { histos_ = new NoiseHistograms(   mui_ ); }
00439   else if ( runType_ == sistrip::APV_LATENCY      ||
00440             runType_ == sistrip::FINE_DELAY           ) { histos_ = new SamplingHistograms(    mui_,runType_ ); }
00441   else if ( runType_ == sistrip::CALIBRATION      ||
00442             runType_ == sistrip::CALIBRATION_DECO ||
00443             runType_ == sistrip::CALIBRATION_SCAN ||
00444             runType_ == sistrip::CALIBRATION_SCAN_DECO) { histos_ = new CalibrationHistograms( mui_,runType_ ); }
00445   else if ( runType_ == sistrip::UNDEFINED_RUN_TYPE   ) { 
00446     histos_ = 0; 
00447     edm::LogError(mlDqmClient_)
00448       << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00449       << " Undefined run type!";
00450     return;
00451   } else if ( runType_ == sistrip::UNKNOWN_RUN_TYPE )   { 
00452     histos_ = 0;
00453     edm::LogError(mlDqmClient_)
00454       << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00455       << " Unknown run type!";
00456     return;
00457   }
00458   histos_->configure(pset,setup);
00459 }
00460 
00461 // -----------------------------------------------------------------------------
00462 // 
00463 void SiStripCommissioningOfflineClient::setInputFiles( std::vector<std::string>& files,
00464                                                        const std::string path,
00465                                                        uint32_t run_number, 
00466                                                        bool collate_histos ) {
00467 
00468   std::string runStr;
00469   std::stringstream ss;
00470   ss << std::setfill('0') << std::setw(8) << run_number;
00471   runStr = ss.str();
00472   
00473   std::string nameStr = "";
00474   if ( !collate_histos ) { nameStr = "SiStripCommissioningClient_"; }
00475   else { nameStr = "SiStripCommissioningSource_"; }
00476 
00477   LogTrace("TEST") << " runStr " << runStr;
00478 
00479   // Open directory
00480   DIR* dp;
00481   struct dirent* dirp;
00482   if ( (dp = opendir(path.c_str())) == NULL ) {
00483     edm::LogError(mlDqmClient_) 
00484       << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00485       << " Error locating directory \"" << path
00486       << "\". No such directory!";
00487     return;
00488   }
00489 
00490   // Find compatible files
00491   while ( (dirp = readdir(dp)) != NULL ) {
00492     std::string fileName(dirp->d_name);
00493     bool goodName = ( fileName.find(nameStr) != std::string::npos );
00494     bool goodRun  = ( fileName.find(runStr) != std::string::npos );
00495     bool rootFile = ( fileName.find(".root") != std::string::npos );
00496     //bool rootFile = ( fileName.rfind(".root",5) == fileName.size()-5 );
00497     if ( goodName && goodRun && rootFile ) {
00498       std::string entry = path;
00499       entry += "/";
00500       entry += fileName;
00501       files.push_back(entry);
00502     }
00503   }
00504   closedir(dp);
00505 
00506   // Some debug  
00507   if ( !collate_histos && files.size() > 1 ) {
00508     std::stringstream ss;
00509     ss << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00510        << " Found more than one client file!";
00511     std::vector<std::string>::const_iterator ifile = files.begin();
00512     std::vector<std::string>::const_iterator jfile = files.end();
00513     for ( ; ifile != jfile; ++ifile ) { ss << std::endl << *ifile; }
00514     edm::LogError(mlDqmClient_) << ss.str();
00515   } else if ( files.empty() ) {
00516     edm::LogError(mlDqmClient_)
00517       << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
00518       << " No input files found!" ;
00519   }
00520 
00521 }
00522 

Generated on Tue Jun 9 17:33:28 2009 for CMSSW by  doxygen 1.5.4