CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_1/src/DQM/SiStripCommissioningSources/src/SiStripCommissioningSource.cc

Go to the documentation of this file.
00001 #include "DQM/SiStripCommissioningSources/interface/SiStripCommissioningSource.h"
00002 #include "CalibFormats/SiStripObjects/interface/SiStripFecCabling.h"
00003 #include "CondFormats/DataRecord/interface/SiStripFedCablingRcd.h"
00004 #include "CondFormats/SiStripObjects/interface/SiStripFedCabling.h"
00005 #include "DataFormats/Common/interface/Handle.h"
00006 #include "DataFormats/SiStripCommon/interface/SiStripEnumsAndStrings.h"
00007 #include "DataFormats/SiStripCommon/interface/SiStripFecKey.h"
00008 #include "DataFormats/SiStripCommon/interface/SiStripFedKey.h"
00009 #include "DataFormats/SiStripCommon/interface/SiStripEventSummary.h"
00010 #include "DQM/SiStripCommissioningSources/interface/Averages.h"
00011 #include "DQM/SiStripCommissioningSources/interface/FastFedCablingTask.h"
00012 #include "DQM/SiStripCommissioningSources/interface/FedCablingTask.h"
00013 #include "DQM/SiStripCommissioningSources/interface/ApvTimingTask.h"
00014 #include "DQM/SiStripCommissioningSources/interface/FedTimingTask.h"
00015 #include "DQM/SiStripCommissioningSources/interface/OptoScanTask.h"
00016 #include "DQM/SiStripCommissioningSources/interface/VpspScanTask.h"
00017 #include "DQM/SiStripCommissioningSources/interface/PedestalsTask.h"
00018 #include "DQM/SiStripCommissioningSources/interface/PedsOnlyTask.h"
00019 #include "DQM/SiStripCommissioningSources/interface/NoiseTask.h"
00020 #include "DQM/SiStripCommissioningSources/interface/PedsFullNoiseTask.h"
00021 #include "DQM/SiStripCommissioningSources/interface/DaqScopeModeTask.h"
00022 #include "DQM/SiStripCommissioningSources/interface/LatencyTask.h"
00023 #include "DQM/SiStripCommissioningSources/interface/FineDelayTask.h"
00024 #include "DQM/SiStripCommissioningSources/interface/CalibrationTask.h"
00025 #include "DQM/SiStripCommissioningSources/interface/CalibrationScanTask.h"
00026 #include "DQMServices/Core/interface/DQMStore.h"
00027 #include "FWCore/Framework/interface/ESHandle.h"
00028 #include "FWCore/Framework/interface/EventSetup.h"
00029 #include "FWCore/Framework/interface/Event.h"
00030 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00031 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00032 #include "FWCore/ServiceRegistry/interface/Service.h"
00033 #include "FWCore/Utilities/interface/Exception.h"
00034 #include <boost/cstdint.hpp>
00035 #include <memory>
00036 #include <iomanip>
00037 #include <sstream>
00038 #include <time.h>
00039 
00040 #include <sys/types.h>
00041 #include <unistd.h>
00042 #include <iomanip>
00043 
00044 #include <arpa/inet.h>
00045 #include <sys/unistd.h>
00046 #include <sys/socket.h>
00047 #include <netdb.h>
00048 #include <stdio.h>
00049 
00050 
00051 using namespace sistrip;
00052 
00053 // -----------------------------------------------------------------------------
00054 //
00055 SiStripCommissioningSource::SiStripCommissioningSource( const edm::ParameterSet& pset ) :
00056   dqm_(0),
00057   fedCabling_(0),
00058   fecCabling_(0),
00059   inputModuleLabel_( pset.getParameter<std::string>( "InputModuleLabel" ) ),
00060   inputModuleLabelSummary_( pset.getParameter<std::string>( "SummaryInputModuleLabel" ) ),
00061   filename_( pset.getUntrackedParameter<std::string>("RootFileName",sistrip::dqmSourceFileName_) ),
00062   run_(0),
00063   time_(0),
00064   taskConfigurable_( pset.getUntrackedParameter<std::string>("CommissioningTask","UNDEFINED") ),
00065   task_(sistrip::UNDEFINED_RUN_TYPE),
00066   tasks_(),
00067   cablingTasks_(),
00068   tasksExist_(false),
00069   cablingTask_(false),
00070   updateFreq_( pset.getUntrackedParameter<int>("HistoUpdateFreq",1) ),
00071   base_(""),
00072   view_( pset.getUntrackedParameter<std::string>("View", "Default") ),
00073   parameters_(pset)
00074 {
00075   LogTrace(mlDqmSource_)
00076     << "[SiStripCommissioningSource::" << __func__ << "]"
00077     << " Constructing object...";
00078   tasks_.clear();
00079   tasks_.resize( 1024, VecOfTasks(96,static_cast<CommissioningTask*>(0)) ); 
00080 }
00081 
00082 // -----------------------------------------------------------------------------
00083 //
00084 SiStripCommissioningSource::~SiStripCommissioningSource() {
00085   LogTrace(mlDqmSource_)
00086     << "[SiStripCommissioningSource::" << __func__ << "]"
00087     << " Destructing object...";
00088 }
00089 
00090 // -----------------------------------------------------------------------------
00091 //
00092 DQMStore* const SiStripCommissioningSource::dqm( std::string method ) const {
00093   if ( !dqm_ ) { 
00094     std::stringstream ss;
00095     if ( method != "" ) { ss << "[SiStripCommissioningSource::" << method << "]" << std::endl; }
00096     else { ss << "[SiStripCommissioningSource]" << std::endl; }
00097     ss << " NULL pointer to DQMStore";
00098     edm::LogWarning(mlDqmSource_) << ss.str();
00099     return 0;
00100   } else { return dqm_; }
00101 }
00102 
00103 // -----------------------------------------------------------------------------
00104 // Retrieve DQM interface, control cabling and "control view" utility
00105 // class, create histogram directory structure and generate "reverse"
00106 // control cabling.
00107 void SiStripCommissioningSource::beginRun( edm::Run const & run, const edm::EventSetup & setup ) {
00108   LogTrace(mlDqmSource_)
00109     << "[SiStripCommissioningSource::" << __func__ << "]"
00110     << " Configuring..." << std::endl;
00111   
00112   // ---------- DQM back-end interface ----------
00113   
00114   dqm_ = edm::Service<DQMStore>().operator->();
00115   edm::LogInfo(mlDqmSource_)
00116     << "[SiStripCommissioningSource::" << __func__ << "]"
00117     << " DQMStore service: " 
00118     << dqm_;
00119   dqm(__func__);
00120   dqm()->setVerbose(0);
00121   
00122   // ---------- Base directory ----------
00123 
00124   std::stringstream dir(""); 
00125   base_ = dir.str();
00126   
00127   // ---------- FED and FEC cabling ----------
00128   
00129   edm::ESHandle<SiStripFedCabling> fed_cabling;
00130   setup.get<SiStripFedCablingRcd>().get( fed_cabling ); 
00131   fedCabling_ = const_cast<SiStripFedCabling*>( fed_cabling.product() ); 
00132   LogDebug(mlDqmSource_)
00133     << "[SiStripCommissioningSource::" << __func__ << "]"
00134     << "Initialized FED cabling. Number of FEDs is " << fedCabling_->feds().size();
00135   fecCabling_ = new SiStripFecCabling( *fed_cabling );
00136   if ( fecCabling_->crates().empty() ) {
00137     std::stringstream ss;
00138     ss << "[SiStripCommissioningSource::" << __func__ << "]"
00139        << " Empty std::vector returned by FEC cabling object!" 
00140        << " Check if database connection failed...";
00141     edm::LogWarning(mlDqmSource_) << ss.str();
00142   }
00143 
00144   // ---------- Reset ---------- 
00145 
00146   tasksExist_ = false;
00147   task_ = sistrip::UNDEFINED_RUN_TYPE;
00148   cablingTask_ = false;
00149   
00150   remove();
00151 
00152   clearCablingTasks();
00153   clearTasks();
00154   
00155 }
00156 
00157 // -----------------------------------------------------------------------------
00158 //
00159 void SiStripCommissioningSource::endJob() {
00160   LogTrace(mlDqmSource_)
00161     << "[SiStripCommissioningSource::" << __func__ << "]"
00162     << " Halting..." << std::endl;
00163   
00164   // ---------- Update histograms ----------
00165   
00166   // Cabling task
00167   for ( TaskMap::iterator itask = cablingTasks_.begin(); itask != cablingTasks_.end(); itask++ ) { 
00168     if ( itask->second ) { itask->second->updateHistograms(); }
00169   }
00170   
00171   if (task_ == sistrip::APV_LATENCY) {
00172     for (uint16_t partition = 0; partition < 4; ++partition) {
00173       tasks_[0][partition]->updateHistograms();
00174     }
00175   } else if (task_ == sistrip::FINE_DELAY ) {
00176     tasks_[0][0]->updateHistograms();
00177   } else {
00178     // All tasks except cabling 
00179     uint16_t fed_id = 0;
00180     uint16_t fed_ch = 0;
00181     std::vector<uint16_t>::const_iterator ifed = fedCabling_->feds().begin(); 
00182     for ( ; ifed != fedCabling_->feds().end(); ifed++ ) { 
00183       const std::vector<FedChannelConnection>& conns = fedCabling_->connections(*ifed);
00184       std::vector<FedChannelConnection>::const_iterator iconn = conns.begin();
00185       for ( ; iconn != conns.end(); iconn++ ) {
00186         if ( !iconn->isConnected() ) { continue; }
00187         fed_id = iconn->fedId();
00188         fed_ch = iconn->fedCh();
00189         if ( tasks_[fed_id][fed_ch] ) { 
00190           tasks_[fed_id][fed_ch]->updateHistograms();
00191           delete tasks_[fed_id][fed_ch];
00192         }
00193       }
00194     }
00195   }
00196   
00197   // ---------- Save histos to root file ----------
00198 
00199   // Strip filename of ".root" extension
00200   std::string name;
00201   if ( filename_.find(".root",0) == std::string::npos ) { name = filename_; }
00202   else { name = filename_.substr( 0, filename_.find(".root",0) ); }
00203 
00204   // Retrieve SCRATCH directory
00205   std::string scratch = "SCRATCH"; //@@ remove trailing slash!!!
00206   std::string dir = "";
00207   if ( getenv(scratch.c_str()) != NULL ) { 
00208     dir = getenv(scratch.c_str()); 
00209   }
00210 
00211   // Add directory path 
00212   std::stringstream ss; 
00213   if ( !dir.empty() ) { ss << dir << "/"; }
00214   else { ss << "/tmp/"; }
00215 
00216   // Add filename with run number, host ip, pid and .root extension
00217   ss << name << "_";
00218   directory(ss,run_);
00219   ss << ".root";
00220 
00221   // Save file with appropriate filename (if run number is known)
00222   if ( !filename_.empty() ) { 
00223     if ( run_ != 0 ) { dqm()->save( ss.str() ); }
00224     else {
00225       edm::LogWarning(mlDqmSource_)
00226         << "[SiStripCommissioningSource::" << __func__ << "]"
00227         << " NULL value for RunNumber! No root file saved!";
00228     }
00229   } else {
00230     edm::LogWarning(mlDqmSource_)
00231       << "[SiStripCommissioningSource::" << __func__ << "]"
00232       << " NULL value for filename! No root file saved!";
00233   }
00234 
00235   LogTrace(mlDqmSource_)
00236     << "[SiStripCommissioningSource::" << __func__ << "]"
00237     << " Saved all histograms to file \""
00238     << ss.str() << "\"";
00239   
00240   // ---------- Delete histograms ----------
00241   
00242   // Remove all MonitorElements in "SiStrip" dir and below
00243   // remove();
00244   
00245   // Delete histogram objects
00246   // clearCablingTasks();
00247   // clearTasks();
00248   
00249   // ---------- Delete cabling ----------
00250 
00251   if ( fedCabling_ ) { fedCabling_ = 0; }
00252   if ( fecCabling_ ) { delete fecCabling_; fecCabling_ = 0; }
00253   
00254 }
00255 
00256 // ----------------------------------------------------------------------------
00257 //
00258 void SiStripCommissioningSource::analyze( const edm::Event& event, 
00259                                           const edm::EventSetup& setup ) {
00260   // Retrieve commissioning information from "event summary" 
00261   edm::Handle<SiStripEventSummary> summary;
00262   event.getByLabel( inputModuleLabelSummary_, summary );
00263 
00264   // Check if EventSummary has info attached
00265   if ( ( summary->runType() == sistrip::UNDEFINED_RUN_TYPE ||
00266          summary->runType() == sistrip::UNKNOWN_RUN_TYPE ) &&
00267        summary->nullParams() ) {
00268     edm::LogWarning(mlDqmSource_)
00269       << "[SiStripCommissioningSource::" << __func__ << "]"
00270       << " Unknown/undefined RunType and NULL parameter values!"
00271       << " It may be that the 'trigger FED' object was not found!"; 
00272   }
00273 
00274   // Check if need to rebuild FED/FEC cabling objects for connection run
00275   //cablingForConnectionRun( summary->runType() ); //@@ do not use!
00276   
00277   // Extract run number and forward to client
00278   if ( event.id().run() != run_ ) { 
00279     run_ = event.id().run(); 
00280     createRunNumber();
00281   }
00282   
00283   // Coarse event rate counter
00284   if ( !(event.id().event()%updateFreq_) ) {
00285     std::stringstream ss;
00286     ss << "[SiStripCommissioningSource::" << __func__ << "]"
00287        << " The last " << updateFreq_ 
00288        << " events were processed at a rate of ";
00289     if ( time(NULL) == time_ ) { ss << ">" << updateFreq_ << " Hz"; }
00290     else { ss << (updateFreq_/(time(NULL)-time_)) << " Hz"; }
00291     edm::LogVerbatim(mlDqmSource_) << ss.str();
00292     time_ = time(NULL);
00293   }
00294   
00295   // Create commissioning task objects 
00296   if ( !tasksExist_ ) { createTask( summary.product(), setup ); }
00297 
00298   // Retrieve raw digis with mode appropriate to task 
00299   edm::Handle< edm::DetSetVector<SiStripRawDigi> > raw;
00300   if ( task_ == sistrip::DAQ_SCOPE_MODE ) { 
00301     if ( summary->fedReadoutMode() == FED_VIRGIN_RAW ) {
00302       event.getByLabel( inputModuleLabel_, "VirginRaw", raw );
00303     } else if ( summary->fedReadoutMode() == FED_SCOPE_MODE ) {
00304       event.getByLabel( inputModuleLabel_, "ScopeMode", raw );
00305     } else {
00306       std::stringstream ss;
00307       ss << "[SiStripCommissioningSource::" << __func__ << "]"
00308          << " Requested DAQ_SCOPE_MODE but unknown FED"
00309          << " readout mode retrieved from SiStripEventSummary: " 
00310          << SiStripEnumsAndStrings::fedReadoutMode( summary->fedReadoutMode() );
00311       edm::LogWarning(mlDqmSource_) << ss.str();
00312     }
00313   } else if ( task_ == sistrip::FAST_CABLING ||
00314               task_ == sistrip::FED_CABLING ||
00315               task_ == sistrip::APV_TIMING ||
00316               task_ == sistrip::FED_TIMING ||
00317               task_ == sistrip::OPTO_SCAN ) { 
00318     event.getByLabel( inputModuleLabel_, "ScopeMode", raw );
00319   } else if ( task_ == sistrip::VPSP_SCAN ||
00320               task_ == sistrip::CALIBRATION ||
00321               task_ == sistrip::CALIBRATION_DECO ||
00322               task_ == sistrip::CALIBRATION_SCAN ||
00323               task_ == sistrip::CALIBRATION_SCAN_DECO ||
00324               task_ == sistrip::PEDESTALS ||
00325               task_ == sistrip::PEDS_ONLY ||
00326               task_ == sistrip::NOISE ||
00327               task_ == sistrip::PEDS_FULL_NOISE ) {
00328     event.getByLabel( inputModuleLabel_, "VirginRaw", raw );
00329   } else if ( task_ == sistrip::APV_LATENCY ||
00330               task_ == sistrip::FINE_DELAY ) {
00331     event.getByLabel( inputModuleLabel_, "FineDelaySelection", raw );
00332   } else {
00333     std::stringstream ss;
00334     ss << "[SiStripCommissioningSource::" << __func__ << "]"
00335        << " Unknown CommissioningTask: " 
00336        << SiStripEnumsAndStrings::runType( task_ )
00337        << " Unable to establish FED readout mode and retrieve digi container!"
00338        << " Check if SiStripEventSummary object is found/present in Event";
00339     edm::LogWarning(mlDqmSource_) << ss.str();
00340     return;
00341   }
00342 
00343   // Check for NULL pointer to digi container
00344   if ( &(*raw) == 0 ) {
00345     std::stringstream ss;
00346     ss << "[SiStripCommissioningSource::" << __func__ << "]" << std::endl
00347        << " NULL pointer to DetSetVector!" << std::endl
00348        << " Unable to fill histograms!";
00349     edm::LogWarning(mlDqmSource_) << ss.str();
00350     return;
00351   }
00352   
00353   if ( !cablingTask_ ) { fillHistos( summary.product(), *raw );  }
00354   else { fillCablingHistos( summary.product(), *raw ); }
00355   
00356 }
00357 
00358 // ----------------------------------------------------------------------------
00359 //
00360 void SiStripCommissioningSource::fillCablingHistos( const SiStripEventSummary* const summary,
00361                                                     const edm::DetSetVector<SiStripRawDigi>& raw ) {
00362   
00363   // Create FEC key using DCU id and LLD channel from SiStripEventSummary
00364   const SiStripModule& module = fecCabling_->module( summary->dcuId() );
00365   uint16_t lld_channel = ( summary->deviceId() & 0x3 ) + 1;
00366   SiStripFecKey key_object( module.key().fecCrate(),
00367                             module.key().fecSlot(),
00368                             module.key().fecRing(),
00369                             module.key().ccuAddr(),
00370                             module.key().ccuChan(),
00371                             lld_channel );
00372   uint32_t fec_key = key_object.key();
00373   std::stringstream sss;
00374   sss << "[SiStripCommissioningSource::" << __func__ << "]" 
00375       << " Found DcuId 0x"
00376       << std::hex << std::setw(8) << std::setfill('0') << summary->dcuId() << std::dec 
00377       << " with Crate/FEC/Ring/CCU/Module/LLD: "
00378       << module.key().fecCrate() << "/"
00379       << module.key().fecSlot() << "/"
00380       << module.key().fecRing() << "/"
00381       << module.key().ccuAddr() << "/"
00382       << module.key().ccuChan() << "/"
00383       << lld_channel;
00384   edm::LogWarning(mlDqmSource_) << sss.str();
00385   
00386   //LogTrace(mlTest_) << "TEST : " << key_object;
00387   
00388   // Check on whether DCU id is found
00389   if ( key_object.isInvalid( sistrip::CCU_CHAN ) ) {
00390     std::stringstream ss;
00391     ss << "[SiStripCommissioningSource::" << __func__ << "]" 
00392        << " DcuId 0x"
00393        << std::hex << std::setw(8) << std::setfill('0') << summary->dcuId() << std::dec 
00394        << " in 'DAQ register' field not found in cabling map!"
00395        << " (NULL values returned for FEC path)";
00396     edm::LogWarning(mlDqmSource_) << ss.str();
00397     return;
00398   }
00399   
00400   // Iterate through FED ids
00401   std::vector<uint16_t>::const_iterator ifed = fedCabling_->feds().begin(); 
00402   for ( ; ifed != fedCabling_->feds().end(); ifed++ ) {
00403 
00404     // Check if FedId is non-zero
00405     if ( *ifed == sistrip::invalid_ ) { continue; }
00406     
00407     // Container to hold median signal level for FED cabling task
00408     std::map<uint16_t,float> medians; medians.clear(); 
00409     std::map<uint16_t,float> medians1; medians1.clear(); 
00410     
00411     // Iterate through FED channels
00412     for ( uint16_t ichan = 0; ichan < 96; ichan++ ) {
00413       
00414       //       // Retrieve digis for given FED key
00415       //       uint32_t fed_key = SiStripFedKey( *ifed, 
00416       //                                        SiStripFedKey::feUnit(ichan),
00417       //                                        SiStripFedKey::feChan(ichan) ).key();
00418       
00419       // Retrieve digis for given FED key
00420       uint32_t fed_key = ( ( *ifed & sistrip::invalid_ ) << 16 ) | ( ichan & sistrip::invalid_ );
00421       
00422       std::vector< edm::DetSet<SiStripRawDigi> >::const_iterator digis = raw.find( fed_key );
00423       if ( digis != raw.end() ) { 
00424         if ( digis->data.empty() ) { continue; }
00425         
00426         //      if ( digis->data[0].adc() > 500 ) {
00427         //        std::stringstream ss;
00428         //        ss << " HIGH SIGNAL " << digis->data[0].adc() << " FOR"
00429         //           << " FedKey: 0x" << std::hex << std::setw(8) << std::setfill('0') << fed_key << std::dec
00430         //           << " FedId/Ch: " << *ifed << "/" << ichan;
00431         //        LogTrace(mlDqmSource_) << ss.str();
00432         //      }
00433         
00434         Averages ave;
00435         for ( uint16_t idigi = 0; idigi < digis->data.size(); idigi++ ) { 
00436           ave.add( static_cast<uint32_t>(digis->data[idigi].adc()) ); 
00437         }
00438         Averages::Params params;
00439         ave.calc(params);
00440         medians[ichan] = params.median_; // Store median signal level
00441         medians1[ichan] = digis->data[0].adc(); 
00442 
00443         //      if ( !digis->data.empty() ) { medians[ichan] = digis->data[0].adc(); }
00444         //      else { 
00445         //        edm::LogWarning(mlTest_) << "TEST : NO DIGIS!";
00446         //      }
00447         
00448         //              std::stringstream ss;
00449         //              ss << "Channel Averages:" << std::endl
00450         //                 << "  nDigis: " << digis->data.size() << std::endl
00451         //                 << "  num/mean/MEDIAN/rms/max/min: "
00452         //                 << params.num_ << "/"
00453         //                 << params.mean_ << "/"
00454         //                 << params.median_ << "/"
00455         //                 << params.rms_ << "/"
00456         //                 << params.max_ << "/"
00457         //                 << params.min_ << std::endl;
00458         //              LogTrace(mlDqmSource_) << ss.str();
00459 
00460       }
00461       
00462     } // fed channel loop
00463 
00464     // Calculate mean and spread on all (median) signal levels
00465     Averages average;
00466     std::map<uint16_t,float>::const_iterator ii = medians.begin();
00467     for ( ; ii != medians.end(); ii++ ) { average.add( ii->second ); }
00468     Averages::Params tmp;
00469     average.calc(tmp);
00470       
00471     //     std::stringstream ss;
00472     //     ss << "FED Averages:" << std::endl
00473     //        << "  nChans: " << medians.size() << std::endl
00474     //        << "  num/mean/median/rms/max/min: "
00475     //        << tmp.num_ << "/"
00476     //        << tmp.mean_ << "/"
00477     //        << tmp.median_ << "/"
00478     //        << tmp.rms_ << "/"
00479     //        << tmp.max_ << "/"
00480     //        << tmp.min_ << std::endl;
00481     //     LogTrace(mlDqmSource_) << ss.str();
00482       
00483     // Calculate mean and spread on "filtered" data
00484     Averages truncated;
00485     std::map<uint16_t,float>::const_iterator jj = medians.begin();
00486     for ( ; jj != medians.end(); jj++ ) { 
00487       if ( jj->second < tmp.median_+tmp.rms_ ) { 
00488         truncated.add( jj->second ); 
00489       }
00490     }
00491     Averages::Params params;
00492     truncated.calc(params);
00493       
00494     //     std::stringstream ss1;
00495     //     ss1 << "Truncated Averages:" << std::endl
00496     //  << "  nChans: " << medians.size() << std::endl
00497     //  << "  num/mean/median/rms/max/min: "
00498     //  << params.num_ << "/"
00499     //  << params.mean_ << "/"
00500     //  << params.median_ << "/"
00501     //  << params.rms_ << "/"
00502     //  << params.max_ << "/"
00503     //  << params.min_ << std::endl;
00504     //     LogTrace(mlDqmSource_) << ss1.str();
00505 
00506     // Identify channels with signal
00507     std::stringstream ss2;
00508     std::stringstream ss3;
00509     //     ss2 << "Number of possible connections: " << medians.size()
00510     //  << " channel/signal: ";
00511     std::map<uint16_t,float> channels;
00512     std::map<uint16_t,float>::const_iterator ichan = medians.begin();
00513     for ( ; ichan != medians.end(); ichan++ ) { 
00514       //             cout << " mean: " << params.mean_
00515       //           << " rms: " << params.rms_
00516       //           << " thresh: " << params.mean_ + 5.*params.rms_
00517       //           << " value: " << ichan->second
00518       //           << " strip: " << ichan->first << std::endl;
00519       //if ( ichan->second > params.mean_ + 5.*params.rms_ ) { 
00520       if ( ichan->second > 200. ) { 
00521         LogTrace(mlTest_) << "TEST FOUND SIGNAL HIGH: " << *ifed << " " << ichan->first << " " << ichan->second;
00522         channels[ichan->first] = ichan->second;
00523       }
00524       ss2 //<< ichan->first << "/" 
00525         << ichan->second << " ";
00526       ss3 //<< ichan->first << "/" 
00527         << medians1[ichan->first] << " ";
00528     }
00529 
00530     ss2 << std::endl;
00531     ss3 << std::endl;
00532     LogTrace(mlTest_) << "DUMP for FED  " << *ifed << ": " << ss2.str();
00533     LogTrace(mlTest_) << "FIRST ADC VAL " << *ifed << ": " << ss3.str();
00534 
00535     //     LogTrace(mlDqmSource_)
00536     //       << "[FedCablingTask::" << __func__ << "]"
00537     //       << " Found candidate connection between device: 0x"
00538     //       << std::setfill('0') << std::setw(8) << std::hex << summary.deviceId() << std::dec
00539     //       << " with Crate/FEC/Ring/CCU/Module/LLDchannel: " 
00540     //       << connection().fecCrate() << "/"
00541     //       << connection().fecSlot() << "/"
00542     //       << connection().fecRing() << "/"
00543     //       << connection().ccuAddr() << "/"
00544     //       << connection().ccuChan() << "/"
00545     //       << connection().lldChannel()
00546     //       << " and FedId/Ch: "
00547     //       << fed_id << "/" << ichan->first
00548     //       << " with signal " << ichan->second 
00549     //       << " [adc] over background " << "XXX +/- YYY [adc]" 
00550     //       << " (S/N = " << "ZZZ" << ")";
00551 
00552     
00553     // Fill cabling histograms
00554     if ( cablingTasks_.find(fec_key) != cablingTasks_.end() ) {
00555       if ( !channels.empty() ) { 
00556         cablingTasks_[fec_key]->fillHistograms( *summary, *ifed, channels );
00557         SiStripFecKey path( fec_key );
00558         std::stringstream ss;
00559         ss << "[SiStripCommissioningSource::" << __func__ << "]"
00560            << " Filled histogram for '" << cablingTasks_[fec_key]->myName()
00561            << "' object with FecKey: 0x" 
00562            << std::hex << std::setfill('0') << std::setw(8) << fec_key << std::dec
00563            << " and Crate/FEC/ring/CCU/module/LLDchan: " 
00564            << path.fecCrate() << "/"
00565            << path.fecSlot() << "/"
00566            << path.fecRing() << "/"
00567            << path.ccuAddr() << "/"
00568            << path.ccuChan() << "/"
00569            << path.channel();
00570         LogTrace(mlDqmSource_) << ss.str();
00571       }
00572     } else {
00573       SiStripFecKey path( fec_key );
00574       std::stringstream ss;
00575       ss << "[SiStripCommissioningSource::" << __func__ << "]"
00576          << " Unable to find CommissioningTask object with FecKey: 0x" 
00577          << std::hex << std::setfill('0') << std::setw(8) << fec_key << std::dec
00578          << " and Crate/FEC/ring/CCU/module/LLDchan: " 
00579          << path.fecCrate() << "/"
00580          << path.fecSlot() << "/"
00581          << path.fecRing() << "/"
00582          << path.ccuAddr() << "/"
00583          << path.ccuChan() << "/"
00584          << path.channel();
00585       edm::LogWarning(mlDqmSource_) << ss.str();
00586     }
00587   
00588   } // fed id loop
00589   
00590 }
00591 
00592 // ----------------------------------------------------------------------------
00593 //
00594 void SiStripCommissioningSource::fillHistos( const SiStripEventSummary* const summary, 
00595                                              const edm::DetSetVector<SiStripRawDigi>& raw ) {
00596 
00597     // Iterate through FED ids and channels
00598     std::vector<uint16_t>::const_iterator ifed = fedCabling_->feds().begin();
00599     for ( ; ifed != fedCabling_->feds().end(); ifed++ ) {
00600 
00601       // Iterate through connected FED channels
00602       const std::vector<FedChannelConnection>& conns = fedCabling_->connections(*ifed);
00603       std::vector<FedChannelConnection>::const_iterator iconn = conns.begin();
00604       for ( ; iconn != conns.end(); iconn++ ) {
00605 
00606         if ( !(iconn->fedId()) || iconn->fedId() > sistrip::valid_ ) { continue; }
00607      
00608         //       // Create FED key and check if non-zero
00609         //       uint32_t fed_key = SiStripFedKey( iconn->fedId(), 
00610         //                                      SiStripFedKey::feUnit(iconn->fedCh()),
00611         //                                      SiStripFedKey::feChan(iconn->fedCh()) ).key();
00612 
00613         // Create FED key and check if non-zero
00614         // note: the key is not computed using the same formula as in commissioning histograms.
00615         // beware that changes here must match changes in raw2digi and in SiStripFineDelayHit
00616         uint32_t fed_key = ( ( iconn->fedId() & sistrip::invalid_ ) << 16 ) | ( iconn->fedCh() & sistrip::invalid_ );
00617 
00618         // Retrieve digis for given FED key and check if found
00619         std::vector< edm::DetSet<SiStripRawDigi> >::const_iterator digis = raw.find( fed_key ); 
00620 
00621         if ( digis != raw.end() ) {
00622           // tasks involving tracking have partition-level histos, so treat separately
00623           if (task_ == sistrip::APV_LATENCY) {
00624             if ( tasks_[0][iconn->fecCrate()-1] ) {
00625               tasks_[0][iconn->fecCrate()-1]->fillHistograms( *summary, *digis );
00626             } else {
00627               std::stringstream ss;
00628               ss << "[SiStripCommissioningSource::" << __func__ << "]"
00629                  << " Unable to find CommissioningTask for FEC crate " 
00630                  << iconn->fecCrate() << ". Unable to fill histograms!";
00631               edm::LogWarning(mlDqmSource_) << ss.str();
00632             }
00633           } else if (task_ == sistrip::FINE_DELAY) {
00634             if ( tasks_[0][0] ) {
00635               tasks_[0][0]->fillHistograms( *summary, *digis );
00636             } else {
00637               std::stringstream ss;
00638               ss << "[SiStripCommissioningSource::" << __func__ << "]"
00639                  << " Unable to find global CommissioningTask for FineDelay. Unable to fill histograms!";
00640               edm::LogWarning(mlDqmSource_) << ss.str();
00641             }
00642           // now do any other task
00643           } else {
00644             if ( tasks_[iconn->fedId()][iconn->fedCh()] ) {
00645               tasks_[iconn->fedId()][iconn->fedCh()]->fillHistograms( *summary, *digis );
00646             } else {
00647               std::stringstream ss;
00648               ss << "[SiStripCommissioningSource::" << __func__ << "]"
00649                  << " Unable to find CommissioningTask object with FED key " 
00650                  << std::hex << std::setfill('0') << std::setw(8) << fed_key << std::dec
00651                  << " and FED id/ch " 
00652                  << iconn->fedId() << "/"
00653                  << iconn->fedCh()
00654                  << " Unable to fill histograms!";
00655               edm::LogWarning(mlDqmSource_) << ss.str();
00656             }
00657           }
00658         } else {
00659           // issue a warning only for standard runs, as latency and fine delay only deliver 
00660           // pseudo zero-suppressed data
00661           if ( task_ != sistrip::APV_LATENCY &&
00662                task_ != sistrip::FINE_DELAY ) {
00663             std::stringstream ss;
00664             ss << "[SiStripCommissioningSource::" << __func__ << "]"
00665                << " Unable to find any DetSet containing digis for FED key " 
00666                << std::hex << std::setfill('0') << std::setw(8) << fed_key << std::dec
00667                << " and FED id/ch " 
00668                << iconn->fedId() << "/"
00669                << iconn->fedCh();
00670             edm::LogWarning(mlDqmSource_) << ss.str();
00671           }
00672         }
00673       } // fed channel loop
00674     } // fed id loop
00675 
00676 }
00677 
00678 // -----------------------------------------------------------------------------
00679 //
00680 void SiStripCommissioningSource::createRunNumber() {
00681   
00682   // Set commissioning task to default ("undefined") value
00683   if ( !run_ ) {
00684     edm::LogWarning(mlDqmSource_) 
00685       << "[SiStripCommissioningSource::" << __func__ << "]"
00686       << " NULL run number!";
00687     return;
00688   }
00689   
00690   // Create MonitorElement that identifies run number
00691   dqm()->setCurrentFolder( base_ + sistrip::root_ );
00692   std::stringstream run;
00693   run << run_;
00694   dqm()->bookString( std::string(sistrip::runNumber_) + sistrip::sep_ + run.str(), run.str() ); 
00695   
00696 }
00697 
00698 // -----------------------------------------------------------------------------
00699 //
00700 void SiStripCommissioningSource::createTask( const SiStripEventSummary* const summary, const edm::EventSetup& setup ) {
00701   
00702   // Set commissioning task to default ("undefined") value
00703   task_ = sistrip::UNDEFINED_RUN_TYPE;
00704   
00705   // Retrieve commissioning task from EventSummary
00706   if ( summary ) { 
00707     task_ = summary->runType(); 
00708     std::stringstream ss;
00709     ss << "[SiStripCommissioningSource::" << __func__ << "]"
00710        << " Identified CommissioningTask from EventSummary to be \"" 
00711        << SiStripEnumsAndStrings::runType( task_ )
00712        << "\"";
00713     LogTrace(mlDqmSource_) << ss.str();
00714   } else { 
00715     task_ = sistrip::UNKNOWN_RUN_TYPE; 
00716     std::stringstream ss;
00717     ss << "[SiStripCommissioningSource::" << __func__ << "]"
00718        << " NULL pointer to SiStripEventSummary!" 
00719        << " Check SiStripEventSummary is found/present in Event";
00720     edm::LogWarning(mlDqmSource_) << ss.str();
00721   } 
00722   
00723   // Override task with ParameterSet configurable (if defined)
00724   sistrip::RunType configurable = SiStripEnumsAndStrings::runType( taskConfigurable_ );
00725   if ( configurable != sistrip::UNDEFINED_RUN_TYPE &&
00726        configurable != sistrip::UNKNOWN_RUN_TYPE ) { 
00727     std::stringstream ss;
00728     ss << "[SiStripCommissioningSource::" << __func__ << "]"
00729        << " Overriding CommissioningTask from EventSummary (\"" 
00730        << SiStripEnumsAndStrings::runType( task_ )
00731        << "\") with value retrieved from .cfg file (\""
00732        << SiStripEnumsAndStrings::runType( configurable )
00733        << "\")!";
00734     LogTrace(mlDqmSource_) << ss.str();
00735     task_ = configurable; 
00736   }
00737   
00738   // Create ME (std::string) that identifies commissioning task
00739   dqm()->setCurrentFolder( base_ + sistrip::root_ );
00740   std::string task_str = SiStripEnumsAndStrings::runType( task_ );
00741   dqm()->bookString( std::string(sistrip::taskId_) + sistrip::sep_ + task_str, task_str ); 
00742   
00743   // Check commissioning task is known / defined
00744   if ( task_ == sistrip::UNKNOWN_RUN_TYPE ||
00745        task_ == sistrip::UNDEFINED_RUN_TYPE ) {
00746     std::stringstream ss;
00747     ss << "[SiStripCommissioningSource::" << __func__ << "]"
00748        << " Unexpected CommissioningTask found (" 
00749        << static_cast<uint16_t>(task_) << ") \""
00750        << SiStripEnumsAndStrings::runType( task_ ) << "\""
00751        << " Unexpected value found in SiStripEventSummary and/or cfg file"
00752        << " If SiStripEventSummary is not present in Event,"
00753        << " check 'CommissioningTask' configurable in cfg file";
00754     edm::LogWarning(mlDqmSource_) << ss.str();
00755     return; 
00756   } else {
00757     std::stringstream ss;
00758     ss << "[SiStripCommissioningSource::" << __func__ << "]"
00759        << " Identified CommissioningTask to be \"" 
00760        << SiStripEnumsAndStrings::runType( task_ )
00761        << "\"";
00762     LogTrace(mlDqmSource_) << ss.str();
00763   }
00764   
00765   // Check if commissioning task is FED cabling 
00766   if ( task_ == sistrip::FED_CABLING ) { cablingTask_ = true; }
00767   else { cablingTask_ = false; }
00768 
00769   std::stringstream ss;
00770   ss << "[SiStripCommissioningSource::" << __func__ << "]"
00771      << " CommissioningTask: "
00772      << SiStripEnumsAndStrings::runType( summary->runType() );
00773   LogTrace(mlDqmSource_) << ss.str();
00774 
00775   edm::LogVerbatim(mlDqmSource_)
00776     << "[SiStripCommissioningSource::" << __func__ << "]"
00777     << " Creating CommissioningTask objects and booking histograms...";
00778   if ( cablingTask_ ) { createCablingTasks(); }
00779   else { createTasks( task_ , setup ); }
00780   edm::LogVerbatim(mlDqmSource_)
00781     << "[SiStripCommissioningSource::" << __func__ << "]"
00782     << " Finished booking histograms!";
00783   tasksExist_ = true;
00784   
00785 }
00786 
00787 // -----------------------------------------------------------------------------
00788 //
00789 void SiStripCommissioningSource::createCablingTasks() {
00790   
00791   // Iterate through FEC cabling and create commissioning task objects
00792   uint16_t booked = 0;
00793   for ( std::vector<SiStripFecCrate>::const_iterator icrate = fecCabling_->crates().begin(); icrate != fecCabling_->crates().end(); icrate++ ) {
00794     for ( std::vector<SiStripFec>::const_iterator ifec = icrate->fecs().begin(); ifec != icrate->fecs().end(); ifec++ ) {
00795       for ( std::vector<SiStripRing>::const_iterator iring = ifec->rings().begin(); iring != ifec->rings().end(); iring++ ) {
00796         for ( std::vector<SiStripCcu>::const_iterator iccu = iring->ccus().begin(); iccu != iring->ccus().end(); iccu++ ) {
00797           for ( std::vector<SiStripModule>::const_iterator imodule = iccu->modules().begin(); imodule != iccu->modules().end(); imodule++ ) {
00798               
00799             // Build FEC key
00800             SiStripFecKey path( icrate->fecCrate(), 
00801                                 ifec->fecSlot(), 
00802                                 iring->fecRing(), 
00803                                 iccu->ccuAddr(), 
00804                                 imodule->ccuChan() );
00805             
00806             // Check if FEC key is invalid
00807             if ( !path.isValid() ) { continue; }
00808             
00809             // Set working directory prior to booking histograms
00810             std::string dir = base_ + path.path();
00811             dqm()->setCurrentFolder( dir );
00812             
00813             // Iterate through all APV pairs for this module
00814             for ( uint16_t ipair = 0; ipair < imodule->nApvPairs(); ipair++ ) {
00815                 
00816               // Retrieve active APV devices
00817               SiStripModule::PairOfU16 apvs = imodule->activeApvPair( imodule->lldChannel(ipair) );
00818               
00819               // Create connection object to hold all relevant info
00820               FedChannelConnection conn( icrate->fecCrate(), 
00821                                          ifec->fecSlot(), 
00822                                          iring->fecRing(), 
00823                                          iccu->ccuAddr(), 
00824                                          imodule->ccuChan(),
00825                                          apvs.first,
00826                                          apvs.second,
00827                                          imodule->dcuId(),
00828                                          imodule->detId(),
00829                                          imodule->nApvPairs() );
00830               
00831               // Define key encoding control path 
00832               uint32_t key = SiStripFecKey( icrate->fecCrate(), 
00833                                             ifec->fecSlot(), 
00834                                             iring->fecRing(), 
00835                                             iccu->ccuAddr(), 
00836                                             imodule->ccuChan(),
00837                                             imodule->lldChannel(ipair) ).key();
00838                 
00839               // Check key is non zero
00840               if ( !key ) { 
00841                 edm::LogWarning(mlDqmSource_)
00842                   << "[SiStripCommissioningSource::" << __func__ << "]"
00843                   << " Unexpected NULL value for FEC key!";
00844                 continue; 
00845               }
00846                 
00847               // Create cabling task objects if not already existing
00848               if ( cablingTasks_.find( key ) == cablingTasks_.end() ) {
00849                 
00850                 if ( task_ == sistrip::FED_CABLING ) { 
00851                   cablingTasks_[key] = new FedCablingTask( dqm(), conn ); 
00852                 } else if ( task_ == sistrip::UNDEFINED_RUN_TYPE ) { 
00853                   edm::LogWarning(mlDqmSource_)
00854                     << "[SiStripCommissioningSource::" << __func__ << "]"
00855                     << " Undefined CommissioningTask" 
00856                     << " Unable to create FedCablingTask object!";
00857                 } else if ( task_ == sistrip::UNKNOWN_RUN_TYPE ) { 
00858                   edm::LogWarning(mlDqmSource_)
00859                     << "[SiStripCommissioningSource::" << __func__ << "]"
00860                     << " Unknown CommissioningTask" 
00861                     << " Unable to create FedCablingTask object!";
00862                 } else { 
00863                   edm::LogWarning(mlDqmSource_)
00864                     << "[SiStripCommissioningSource::" << __func__ << "]"
00865                     << " Unexpected CommissioningTask: " 
00866                     << SiStripEnumsAndStrings::runType( task_ )
00867                     << " Unable to create FedCablingTask object!";
00868                 }
00869                 
00870                 // Check if key is found and, if so, book histos and set update freq
00871                 if ( cablingTasks_.find( key ) != cablingTasks_.end() ) {
00872                   if ( cablingTasks_[key] ) {
00873                     cablingTasks_[key]->bookHistograms(); 
00874                     cablingTasks_[key]->updateFreq(1); //@@ hardwired to update every event!!! 
00875                     booked++;
00876                     //std::stringstream ss;
00877                     //ss << "[SiStripCommissioningSource::" << __func__ << "]"
00878                     //<< " Booking histograms for '" << cablingTasks_[key]->myName()
00879                     //<< "' object with key 0x" << std::hex << std::setfill('0') << std::setw(8) << key << std::dec
00880                     //<< " in directory \"" << dir << "\"";
00881                     //LogTrace(mlDqmSource_) << ss.str();
00882                   } else {
00883                     std::stringstream ss;
00884                     ss << "[SiStripCommissioningSource::" << __func__ << "]"
00885                        << " NULL pointer to CommissioningTask for key 0x"
00886                        << std::hex << std::setfill('0') << std::setw(8) << key << std::dec
00887                        << " in directory " << dir 
00888                        << " Unable to book histograms!";
00889                     edm::LogWarning(mlDqmSource_) << ss.str();
00890                   }
00891                 } else {
00892                   std::stringstream ss;
00893                   ss << "[SiStripCommissioningSource::" << __func__ << "]"
00894                      << " Unable to find CommissioningTask for key 0x"
00895                      << std::hex << std::setfill('0') << std::setw(8) << key << std::dec
00896                      << " in directory " << dir
00897                      << " Unable to book histograms!";
00898                   edm::LogWarning(mlDqmSource_) << ss.str();
00899                 }
00900               
00901               } else {
00902                 std::stringstream ss;
00903                 ss << "[SiStripCommissioningSource::" << __func__ << "]"
00904                    << " CommissioningTask object already exists for key 0x"
00905                    << std::hex << std::setfill('0') << std::setw(8) << key << std::dec
00906                    << " in directory " << dir 
00907                    << " Unable to create FedCablingTask object!";
00908                 edm::LogWarning(mlDqmSource_) << ss.str();
00909               }
00910               
00911             } // loop through apv pairs
00912           } // loop through modules
00913         } // loop through ccus
00914       } // loop through rings
00915     } // loop through fecs
00916   } // loop through crates
00917 
00918   edm::LogVerbatim(mlDqmSource_)
00919     << "[SiStripCommissioningSource::" << __func__ << "]"
00920     << " Created " << booked 
00921     << " CommissioningTask objects and booked histograms";
00922   
00923 }
00924 
00925 // -----------------------------------------------------------------------------
00926 //
00927 void SiStripCommissioningSource::createTasks( sistrip::RunType run_type, const edm::EventSetup& setup ) {
00928 
00929   uint16_t booked = 0;
00930 
00931   // latency has partition-level histos, so treat separately
00932   if (task_ == sistrip::APV_LATENCY) {
00933 
00934     for (uint16_t partition = 0; partition < 4; ++partition) {
00935       // make a task for every partition; tracker-wide histo is shared
00936       tasks_[0][partition] = new LatencyTask( dqm(), FedChannelConnection(partition+1,sistrip::invalid_,sistrip::invalid_,sistrip::invalid_,sistrip::invalid_) );
00937       tasks_[0][partition]->eventSetup( &setup );
00938       tasks_[0][partition]->bookHistograms();
00939       tasks_[0][partition]->updateFreq( updateFreq_ );
00940       booked++;
00941     }
00942 
00943   // fine-delay has 1 histo for the whole tracker, so treat separately
00944   } else if (task_ == sistrip::FINE_DELAY) {
00945 
00946     tasks_[0][0] = new FineDelayTask( dqm(), FedChannelConnection() );
00947     tasks_[0][0]->eventSetup( &setup );
00948     tasks_[0][0]->bookHistograms();
00949     tasks_[0][0]->updateFreq( updateFreq_ );
00950     booked++;
00951 
00952   } else { // now do any other task
00953 
00954     // Iterate through FED ids and channels 
00955     std::vector<uint16_t>::const_iterator ifed = fedCabling_->feds().begin();
00956     for ( ; ifed != fedCabling_->feds().end(); ifed++ ) {
00957 
00958       // Iterate through connected FED channels
00959       // S.L.: currently copy the vector, because it changes later when
00960       // reading in peds for calibration run -> not understood memory corruption!
00961       std::vector<FedChannelConnection> conns = fedCabling_->connections(*ifed);
00962       std::vector<FedChannelConnection>::const_iterator iconn = conns.begin();
00963       for ( ; iconn != conns.end(); iconn++ ) {
00964 
00965         // Create FEC key
00966         SiStripFecKey fec_key( iconn->fecCrate(), 
00967                                iconn->fecSlot(), 
00968                                iconn->fecRing(), 
00969                                iconn->ccuAddr(), 
00970                                iconn->ccuChan() );
00971         // Create FED key and check if non-zero
00972         SiStripFedKey fed_key( iconn->fedId(), 
00973                                SiStripFedKey::feUnit(iconn->fedCh()),
00974                                SiStripFedKey::feChan(iconn->fedCh()) );
00975         if ( !iconn->isConnected() ) { continue; }
00976 
00977         // define the view in which to work and paths for histograms
00978         //   currently FecView (control view) and FedView (readout view)
00979         //   DetView (detector view) implementation has started
00980         // Set working directory prior to booking histograms 
00981         std::stringstream dir;
00982         dir << base_;
00983         if (view_ == "Default") { // default
00984           if ( run_type == sistrip::FAST_CABLING ) {
00985             dir << fed_key.path(); // cabling in fed view
00986           } else {
00987             dir << fec_key.path(); // all other runs in control view
00988           }
00989         } else if (view_ == "FecView") {
00990           dir << fec_key.path();
00991         } else if (view_ == "FedView") {
00992           dir << fed_key.path();
00993         } else if (view_ == "DetView") {
00994           // currently just by detid from the connection, which is empty...
00995           dir << sistrip::root_ << sistrip::dir_
00996               << sistrip::detectorView_ << sistrip::dir_
00997               << iconn->detId();
00998         } else {
00999           edm::LogWarning(mlDqmSource_)
01000             << "[SiStripCommissioningSource::" << __func__ << "]"
01001             << " Invalid view " << view_ << std::endl
01002             << " Histograms will end up all in the top directory.";
01003         } // end if view_ == ...
01004         dqm()->setCurrentFolder( dir.str() );
01005 
01006         // Create commissioning task objects
01007         if ( !tasks_[iconn->fedId()][iconn->fedCh()] ) { 
01008           if ( task_ == sistrip::FAST_CABLING ) { 
01009             tasks_[iconn->fedId()][iconn->fedCh()] = new FastFedCablingTask( dqm(), *iconn ); 
01010           } else if ( task_ == sistrip::APV_TIMING ) { 
01011             tasks_[iconn->fedId()][iconn->fedCh()] = new ApvTimingTask( dqm(), *iconn ); 
01012           } else if ( task_ == sistrip::FED_TIMING ) { 
01013             tasks_[iconn->fedId()][iconn->fedCh()] = new FedTimingTask( dqm(), *iconn );
01014           } else if ( task_ == sistrip::OPTO_SCAN ) { 
01015             tasks_[iconn->fedId()][iconn->fedCh()] = new OptoScanTask( dqm(), *iconn );
01016           } else if ( task_ == sistrip::VPSP_SCAN ) { 
01017             tasks_[iconn->fedId()][iconn->fedCh()] = new VpspScanTask( dqm(), *iconn );
01018           } else if ( task_ == sistrip::PEDESTALS ) { 
01019             tasks_[iconn->fedId()][iconn->fedCh()] = new PedestalsTask( dqm(), *iconn );
01020           } else if ( task_ == sistrip::PEDS_ONLY ) { 
01021             tasks_[iconn->fedId()][iconn->fedCh()] = new PedsOnlyTask( dqm(), *iconn );
01022           } else if ( task_ == sistrip::NOISE ) { 
01023             tasks_[iconn->fedId()][iconn->fedCh()] = new NoiseTask( dqm(), *iconn );
01024           } else if ( task_ == sistrip::PEDS_FULL_NOISE ) { 
01025             tasks_[iconn->fedId()][iconn->fedCh()] = new PedsFullNoiseTask( dqm(), *iconn, parameters_ );
01026           } else if ( task_ == sistrip::DAQ_SCOPE_MODE ) { 
01027             tasks_[iconn->fedId()][iconn->fedCh()] = new DaqScopeModeTask( dqm(), *iconn );
01028           } else if ( task_ == sistrip::CALIBRATION_SCAN || 
01029                       task_ == sistrip::CALIBRATION_SCAN_DECO ) { 
01030             tasks_[iconn->fedId()][iconn->fedCh()] = new CalibrationScanTask( dqm(), 
01031                                                                               *iconn, 
01032                                                                               task_, 
01033                                                                               filename_.c_str(), 
01034                                                                               run_, 
01035                                                                               setup );
01036           } else if ( task_ == sistrip::CALIBRATION || 
01037                       task_ == sistrip::CALIBRATION_DECO ) { 
01038             tasks_[iconn->fedId()][iconn->fedCh()] = new CalibrationTask( dqm(), 
01039                                                                           *iconn, 
01040                                                                           task_, 
01041                                                                           filename_.c_str(), 
01042                                                                           run_, 
01043                                                                           setup );
01044           } else if ( task_ == sistrip::UNDEFINED_RUN_TYPE ) { 
01045             edm::LogWarning(mlDqmSource_)  
01046               << "[SiStripCommissioningSource::" << __func__ << "]"
01047               << " Undefined CommissioningTask" 
01048               << " Unable to create CommissioningTask object!";
01049           } else { 
01050             edm::LogWarning(mlDqmSource_)
01051               << "[SiStripCommissioningSource::" << __func__ << "]"
01052               << " Unknown CommissioningTask" 
01053               << " Unable to create CommissioningTask object!";
01054           }
01055 
01056           // Check if fed_key is found and, if so, book histos and set update freq
01057           if ( tasks_[iconn->fedId()][iconn->fedCh()] ) {
01058             tasks_[iconn->fedId()][iconn->fedCh()]->eventSetup( &setup );
01059             tasks_[iconn->fedId()][iconn->fedCh()]->bookHistograms(); 
01060             tasks_[iconn->fedId()][iconn->fedCh()]->updateFreq( updateFreq_ ); 
01061             booked++;
01062             //std::stringstream ss;
01063             //ss << "[SiStripCommissioningSource::" << __func__ << "]"
01064             //<< " Booking histograms for '" << tasks_[iconn->fedId()][iconn->fedCh()]->myName()
01065             //<< "' object with key 0x" << std::hex << std::setfill('0') << std::setw(8) << fed_key.key() << std::dec
01066             //<< " in directory " << dir.str();
01067             //LogTrace(mlDqmSource_) << ss.str();
01068           } else {
01069             std::stringstream ss;
01070             ss << "[SiStripCommissioningSource::" << __func__ << "]"
01071                << " NULL pointer to CommissioningTask for key 0x"
01072                << std::hex << std::setfill('0') << std::setw(8) << fed_key.key() << std::dec
01073                << " in directory " << dir.str()
01074                << " Unable to book histograms!";
01075             edm::LogWarning(mlDqmSource_) << ss.str();
01076           }
01077 
01078         } else {
01079           std::stringstream ss;
01080           ss << "[SiStripCommissioningSource::" << __func__ << "]"
01081              << " CommissioningTask object already exists for key 0x"
01082              << std::hex << std::setfill('0') << std::setw(8) << fed_key.key() << std::dec
01083              << " in directory " << dir.str()
01084              << " Unable to create CommissioningTask object!";
01085           edm::LogWarning(mlDqmSource_) << ss.str();
01086         }
01087 
01088       } // loop over fed channels
01089     } // loop over feds
01090   } // end other tasks
01091 
01092   edm::LogVerbatim(mlDqmSource_)
01093     << "[SiStripCommissioningSource::" << __func__ << "]"
01094     << " Created " << booked 
01095     << " CommissioningTask objects and booked histograms";
01096 }
01097 
01098 // ----------------------------------------------------------------------------
01099 //
01100 void SiStripCommissioningSource::clearCablingTasks() {
01101   if ( cablingTasks_.empty() ) { return; }
01102   for ( TaskMap::iterator itask = cablingTasks_.begin(); itask != cablingTasks_.end(); itask++ ) { 
01103     if ( itask->second ) { delete itask->second; }
01104   }
01105   cablingTasks_.clear();
01106 }
01107 
01108 // ----------------------------------------------------------------------------
01109 //
01110 void SiStripCommissioningSource::clearTasks() {
01111   if ( tasks_.empty() ) { return; }
01112   VecOfVecOfTasks::iterator ifed = tasks_.begin();
01113   for ( ; ifed !=  tasks_.end(); ifed++ ) { 
01114     VecOfTasks::iterator ichan = ifed->begin();
01115     for ( ; ichan != ifed->end(); ichan++ ) { 
01116       if ( *ichan ) { delete *ichan; *ichan = 0; }
01117     }
01118     ifed->resize(96,0);
01119   }
01120   tasks_.resize(1024);
01121 }
01122 
01123 // ----------------------------------------------------------------------------
01124 //
01125 void SiStripCommissioningSource::remove() {
01126   dqm()->cd();
01127   dqm()->removeContents(); 
01128   
01129   if( dqm()->dirExists(sistrip::root_) ) {
01130     dqm()->rmdir(sistrip::root_);
01131   }
01132 }
01133 
01134 // -----------------------------------------------------------------------------
01135 // 
01136 void SiStripCommissioningSource::directory( std::stringstream& dir,
01137                                             uint32_t run_number ) {
01138 
01139   // Get details about host
01140   char hn[256];
01141   gethostname( hn, sizeof(hn) );
01142   struct hostent* he;
01143   he = gethostbyname(hn);
01144 
01145   // Extract host name and ip
01146   std::string host_name;
01147   std::string host_ip;
01148   if ( he ) { 
01149     host_name = std::string(he->h_name);
01150     host_ip = std::string( inet_ntoa( *(struct in_addr*)(he->h_addr) ) );
01151   } else {
01152     host_name = "unknown.cern.ch";
01153     host_ip = "255.255.255.255";
01154   }
01155 
01156   // Reformat IP address
01157   std::string::size_type pos = 0;
01158   std::stringstream ip;
01159   //for ( uint16_t ii = 0; ii < 4; ++ii ) {
01160   while ( pos != std::string::npos ) {
01161     std::string::size_type tmp = host_ip.find(".",pos);
01162     if ( tmp != std::string::npos ) {
01163       ip << std::setw(3)
01164          << std::setfill('0')
01165          << host_ip.substr( pos, tmp-pos ) 
01166          << ".";
01167       pos = tmp+1; // skip the delimiter "."
01168     } else {
01169       ip << std::setw(3)
01170          << std::setfill('0')
01171          << host_ip.substr( pos );
01172       pos = std::string::npos;
01173     }
01174   }
01175   
01176   // Get pid
01177   pid_t pid = getpid();
01178 
01179   // Construct string
01180   if ( run_number ) {
01181     dir << std::setw(8) 
01182         << std::setfill('0') 
01183         << run_number
01184         << "_";
01185   }
01186   dir << ip.str()
01187       << "_"
01188       << std::setw(5) 
01189       << std::setfill('0') 
01190       << pid;
01191   
01192 }
01193 
01194 // -----------------------------------------------------------------------------
01195 //
01196 // void SiStripCommissioningSource::cablingForConnectionRun( const sistrip::RunType& type ) {
01197 
01198 //   if ( type == sistrip::FED_CABLING ||
01199 //        type == sistrip::QUITE_FAST_CABLING ||
01200 //        type == sistrip::FAST_CABLING ) {
01201 //     std::stringstream ss;
01202 //     ss << "[SiStripCommissioningSource::" << __func__ << "]"
01203 //        << " Run type is " << SiStripEnumsAndStrings::runType( type ) << "!"
01204 //        << " Checking if cabling should be rebuilt using FED and device descriptions!...";
01205 //     edm::LogVerbatim(mlDqmSource_) << ss.str();
01206 //   } else { return; }
01207 
01208 //   // Build and retrieve SiStripConfigDb object using service
01209 //   SiStripConfigDb* db = edm::Service<SiStripConfigDb>().operator->(); //@@ NOT GUARANTEED TO BE THREAD SAFE! 
01210 //   LogTrace(mlCabling_) 
01211 //     << "[SiStripCommissioningSource::" << __func__ << "]"
01212 //     << " Nota bene: using the SiStripConfigDb API"
01213 //     << " as a \"service\" does not presently guarantee"
01214 //     << " thread-safe behaviour!...";
01215   
01216 //   if ( !db ) { 
01217 //     edm::LogError(mlCabling_) 
01218 //       << "[SiStripCommissioningSource::" << __func__ << "]"
01219 //       << " NULL pointer to SiStripConfigDb returned by service!"
01220 //       << " Cannot check if cabling needs to be rebuilt!";
01221 //     return;
01222 //   }
01223   
01224 //   if ( db->getFedConnections().empty() ) {
01225 //     edm::LogVerbatim(mlCabling_) 
01226 //       << "[SiStripCommissioningSource::" << __func__ << "]"
01227 //       << " Datbase does not contain FED connections!"
01228 //       << " Do not need to rebuild cabling object based on FED and device descriptions!"; 
01229 //    return;
01230 //   }
01231   
01232 //   if ( fecCabling_ ) { delete fecCabling_; fecCabling_ = 0; }
01233 //   if ( fedCabling_ ) { delete fedCabling_; fedCabling_ = 0; }
01234   
01235 //   // Build FEC cabling
01236 //   fecCabling_ = new SiStripFecCabling();
01237 //   SiStripConfigDb::DcuDetIdMap mapping;
01238 //   SiStripFedCablingBuilderFromDb::buildFecCablingFromDevices( db,
01239 //                                                            *fecCabling_,
01240 //                                                            mapping );
01241 //   // Build FED cabling
01242 //   fedCabling_ = new SiStripFedCabling();
01243 //   SiStripFedCablingBuilderFromDb::getFedCabling( *fecCabling_,
01244 //                                               *fedCabling_ );
01245 
01246 //   edm::LogVerbatim(mlCabling_) 
01247 //     << "[SiStripCommissioningSource::" << __func__ << "]"
01248 //     << " Cabling object rebuilt using on FED and device descriptions!";
01249   
01250 // }