CMS 3D CMS Logo

SiStripCommissioningOfflineClient.cc
Go to the documentation of this file.
1 
23 #include <boost/cstdint.hpp>
24 #include <iostream>
25 #include <iomanip>
26 #include <fstream>
27 #include <sstream>
28 #include <sys/types.h>
29 #include <dirent.h>
30 #include <cerrno>
31 #include "TProfile.h"
32 
33 using namespace sistrip;
34 
35 // -----------------------------------------------------------------------------
36 //
38  : bei_( edm::Service<DQMStore>().operator->() ),
39  histos_(nullptr),
40  //inputFiles_( pset.getUntrackedParameter< std::vector<std::string> >( "InputRootFiles", std::vector<std::string>() ) ),
41  outputFileName_( pset.getUntrackedParameter<std::string>( "OutputRootFile", "" ) ),
42  collateHistos_( !pset.getUntrackedParameter<bool>( "UseClientFile", false ) ),
43  analyzeHistos_( pset.getUntrackedParameter<bool>( "AnalyzeHistos", true ) ),
44  xmlFile_( (pset.getUntrackedParameter<edm::FileInPath>( "SummaryXmlFile", edm::FileInPath() )).fullPath() ),
45  createSummaryPlots_( false ),
46  clientHistos_( false ),
47  uploadToDb_( false ),
48  runType_(sistrip::UNKNOWN_RUN_TYPE),
49  runNumber_(0),
50  partitionName_(pset.existsAs<std::string>("PartitionName") ? pset.getParameter<std::string>("PartitionName") : ""),
51  map_(),
52  plots_(),
53  parameters_(pset)
54 {
56  << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
57  << " Constructing object...";
59  pset.getUntrackedParameter<std::string>( "FilePath" ),
60  pset.existsAs<std::string>("PartitionName") ? pset.getParameter<std::string>("PartitionName") : "",
61  pset.getUntrackedParameter<uint32_t>("RunNumber"),
63 }
64 
65 // -----------------------------------------------------------------------------
66 //
69  << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
70  << " Destructing object...";
71 }
72 
73 // -----------------------------------------------------------------------------
74 //
77  << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
78  << " Analyzing root file(s)...";
79 
80  // Check for null pointer
81  if ( !bei_ ) {
83  << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
84  << " NULL pointer to DQMStore!"
85  << " Aborting...";
86  return;
87  }
88  bei_->setVerbose(0);
89 
90  // Check if .root file can be opened
91  std::vector<std::string>::const_iterator ifile = inputFiles_.begin();
92  for ( ; ifile != inputFiles_.end(); ifile++ ) {
93  std::ifstream root_file;
94  root_file.open( ifile->c_str() );
95  if( !root_file.is_open() ) {
97  << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
98  << " The input root file \"" << *ifile
99  << "\" could not be opened!"
100  << " Please check the path and filename!";
101  } else {
102  root_file.close();
104  if ( found != std::string::npos && clientHistos_ ) {
106  << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
107  << " The input root files appear to be a mixture"
108  << " of \"Source\" and \"Client\" files!"
109  << " Aborting...";
110  return;
111  }
112  if ( found != std::string::npos && inputFiles_.size() != 1 ) {
114  << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
115  << " There appear to be multiple input \"Client\" root files!"
116  << " Aborting...";
117  return;
118  }
119  if ( found != std::string::npos ) { clientHistos_ = true; }
120  }
121  }
122  if ( clientHistos_ && inputFiles_.size() == 1 ) {
124  << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
125  << " Collated histograms found in input root file \""
126  << inputFiles_[0] << "\"";
127  }
128 
129  // Check if .xml file can be opened
130  if ( !xmlFile_.empty() ) {
131  std::ifstream xml_file;
132  xml_file.open( xmlFile_.c_str() );
133  if( !xml_file.is_open() ) {
135  << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
136  << " The SummaryPlot XML file \"" << xmlFile_
137  << "\" could not be opened!"
138  << " Please check the path and filename!"
139  << " Aborting...";
140  return;
141  } else {
142  createSummaryPlots_ = true;
143  xml_file.close();
144  }
145  }
146 
147  // Open root file(s) and create ME's
148  if ( inputFiles_.empty() ) {
150  << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
151  << " No input root files specified!";
152  return;
153  }
154 
156  << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
157  << " Opening root files. This may take some time!...";
158  std::vector<std::string>::const_iterator jfile = inputFiles_.begin();
159  for ( ; jfile != inputFiles_.end(); jfile++ ) {
161  << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
162  << " Opening root file \"" << *jfile
163  << "\"... (This may take some time.)";
164  if ( clientHistos_ ) {
165  bei_->open( *jfile, false, sistrip::collate_, "" );
166  } else {
167  bei_->open( *jfile, false, "SiStrip", sistrip::collate_ );
168  }
170  << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
171  << " Opened root file \"" << *jfile << "\"!";
172  }
174  << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
175  << " Opened " << inputFiles_.size() << " root files!";
176 
177  // Retrieve list of histograms
178  std::vector<std::string> contents;
179  bei_->getContents( contents );
180 
181  // If using client file, remove "source" histograms from list
182  if ( clientHistos_ ) {
183  std::vector<std::string> temp;
184  std::vector<std::string>::iterator istr = contents.begin();
185  for ( ; istr != contents.end(); istr++ ) {
186  if ( istr->find(sistrip::collate_) != std::string::npos ) {
187  temp.push_back( *istr );
188  }
189  }
190  contents.clear();
191  contents = temp;
192  }
193 
194  // Some debug
196  << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
197  << " Found " << contents.size()
198  << " directories containing MonitorElements";
199 
200  // Some more debug
201  if (false) {
202  std::stringstream ss;
203  ss << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
204  << " Directories found: " << std::endl;
205  std::vector<std::string>::iterator istr = contents.begin();
206  for ( ; istr != contents.end(); istr++ ) { ss << " " << *istr << std::endl; }
207  LogTrace(mlDqmClient_) << ss.str();
208  }
209 
210  // Extract run type from contents
212 
213  // Extract run number from contents
215 
216  // Copy custom information to the collated structure
218 
219  // Check runType
222  << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
223  << " Unknown commissioning runType: "
225  << " and run number is " << runNumber_;
226  return;
227  } else {
229  << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
230  << " Run type is "
232  << " and run number is " << runNumber_;
233  }
234 
235  // Open and parse "summary plot" xml file
236  if ( createSummaryPlots_ ) {
238  << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
239  << " Parsing summary plot XML file...";
240  SummaryPlotXmlParser xml_file;
241  xml_file.parseXML(xmlFile_);
242  plots_ = xml_file.summaryPlots(runType_);
244  << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
245  << " Parsed summary plot XML file and found "
246  << plots_.size() << " plots defined!";
247  } else {
249  << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
250  << " Null string for SummaryPlotXmlFile!"
251  << " No summary plots will be created!";
252  }
253 
254  // Some debug
255  std::stringstream ss;
256  ss << "[SiStripCommissioningOfflineClient::" << __func__ << "]" << std::endl
257  << " Input root files : ";
258  if ( inputFiles_.empty() ) { ss << "(none)"; }
259  else {
260  std::vector<std::string>::const_iterator ifile = inputFiles_.begin();
261  for ( ; ifile != inputFiles_.end(); ifile++ ) {
262  if ( ifile != inputFiles_.begin() ) {
263  ss << std::setw(25) << std::setfill(' ') << ": ";
264  }
265  ss << "\"" << *ifile << "\"" << std::endl;
266  }
267  }
268  ss << " Run type : \""
269  << SiStripEnumsAndStrings::runType( runType_ ) << "\"" << std::endl
270  << " Run number : " << runNumber_ << std::endl
271  << " Summary plot XML file : ";
272  if ( xmlFile_.empty() ) { ss << "(none)"; }
273  else { ss << "\"" << xmlFile_ << "\""; }
274  edm::LogVerbatim(mlDqmClient_) << ss.str();
275 
276  // Virtual method that creates CommissioningHistogram object
278  << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
279  << " Creating CommissioningHistogram object...";
280  createHistos(parameters_, setup);
281  if ( histos_ ) {
283  << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
284  << " Created CommissioningHistogram object!";
285  } else {
287  << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
288  << " NULL pointer to CommissioningHistogram object!"
289  << " Aborting...";
290  return;
291  }
292 
293  // Perform collation
294  if ( histos_ ) {
295  histos_->extractHistograms( contents );
296  }
297 
298  // Perform analysis
299  if ( analyzeHistos_ ) {
301  << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
302  << " Analyzing histograms...";
303  if ( histos_ ) { histos_->histoAnalysis( true ); }
305  << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
306  << " Analyzed histograms!";
307  } else {
309  << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
310  << " No histogram analysis performed!";
311  }
312 
313  // Create summary plots
314  if ( createSummaryPlots_ ) {
316  << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
317  << " Generating summary plots...";
318  std::vector<SummaryPlot>::const_iterator iplot = plots_.begin();
319  for ( ; iplot != plots_.end(); iplot++ ) {
320  if ( histos_ ) {
321  histos_->createSummaryHisto( iplot->monitorable(),
322  iplot->presentation(),
323  iplot->level(),
324  iplot->granularity() );
325  }
326  }
328  << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
329  << " Generated summary plots!";
330  } else {
332  << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
333  << " No summary plots generated!";
334  }
335 
336  // Save client root file
337  if ( histos_ ) {
338  bool save = parameters_.getUntrackedParameter<bool>( "SaveClientFile", true );
339  if ( save ) {
342  else
344  }
345  else {
347  << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
348  << " Client file not saved!";
349  }
350  }
351 
352  // Virtual method to trigger the database upload
354 
355  // Print analyses
356  if ( histos_ ) {
358  histos_->printSummary();
359  }
360 
361  // Remove all ME/CME objects
362  if ( histos_ ) { histos_->remove(); }
363 
365  << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
366  << " Finished analyzing root file(s)...";
367 
368 }
369 
370 // -----------------------------------------------------------------------------
371 //
373  const edm::EventSetup& setup ) {
374  if ( !(event.id().event()%10) ) {
376  << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
377  << " Empty event loop! User can kill job...";
378  }
379 }
380 
381 // -----------------------------------------------------------------------------
382 //
384 
385 // -----------------------------------------------------------------------------
386 //
388 
389  // Check pointer
390  if ( histos_ ) {
392  << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
393  << " CommissioningHistogram object already exists!"
394  << " Aborting...";
395  return;
396  }
397 
398  // Check pointer to BEI
399  if ( !bei_ ) {
401  << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
402  << " NULL pointer to DQMStore!";
403  return;
404  }
405 
406  // Create "commissioning histograms" object
408  else if ( runType_ == sistrip::FED_CABLING ) { histos_ = new FedCablingHistograms( pset, bei_ ); }
409  else if ( runType_ == sistrip::APV_TIMING ) { histos_ = new ApvTimingHistograms( pset, bei_ ); }
410  else if ( runType_ == sistrip::OPTO_SCAN ) { histos_ = new OptoScanHistograms( pset, bei_ ); }
411  else if ( runType_ == sistrip::VPSP_SCAN ) { histos_ = new VpspScanHistograms( pset, bei_ ); }
412  else if ( runType_ == sistrip::PEDESTALS ) { histos_ = new PedestalsHistograms( pset, bei_ ); }
413  else if ( runType_ == sistrip::PEDS_FULL_NOISE ) { histos_ = new PedsFullNoiseHistograms( pset, bei_ ); }
414  else if ( runType_ == sistrip::PEDS_ONLY ) { histos_ = new PedsOnlyHistograms( pset, bei_ ); }
415  else if ( runType_ == sistrip::NOISE ) { histos_ = new NoiseHistograms( pset, bei_ ); }
416  else if ( runType_ == sistrip::APV_LATENCY ||
418  else if ( runType_ == sistrip::CALIBRATION ||
422  else if ( runType_ == sistrip::DAQ_SCOPE_MODE) { histos_ = new DaqScopeModeHistograms( pset, bei_);}
423  else if ( runType_ == sistrip::UNDEFINED_RUN_TYPE ) {
424  histos_ = nullptr;
426  << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
427  << " Undefined run type!";
428  return;
429  } else if ( runType_ == sistrip::UNKNOWN_RUN_TYPE ) {
430  histos_ = nullptr;
432  << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
433  << " Unknown run type!";
434  return;
435  }
436  histos_->configure(pset,setup);
437 }
438 
439 // -----------------------------------------------------------------------------
440 //
442  const std::string path,
443  const std::string partitionName,
444  uint32_t run_number,
445  bool collate_histos ) {
446 
447  std::string runStr;
448  std::stringstream ss;
449  ss << std::setfill('0') << std::setw(8) << run_number;
450  runStr = ss.str();
451 
452  std::string nameStr = "";
453  if ( !collate_histos ) { nameStr = "SiStripCommissioningClient_"; }
454  else { nameStr = "SiStripCommissioningSource_"; }
455 
456  LogTrace("TEST") << " runStr " << runStr;
457 
458  // Open directory
459  DIR* dp;
460  struct dirent* dirp;
461  if ( (dp = opendir(path.c_str())) == nullptr ) {
463  << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
464  << " Error locating directory \"" << path
465  << "\". No such directory!";
466  return;
467  }
468 
469  // Find compatible files
470  while ( (dirp = readdir(dp)) != nullptr ) {
471  std::string fileName(dirp->d_name);
472  bool goodName = ( fileName.find(nameStr) != std::string::npos );
473  bool goodRun = ( fileName.find(runStr) != std::string::npos );
474  bool rootFile = ( fileName.find(".root") != std::string::npos );
475  bool goodPartition = true;
476  if(not partitionName.empty()){
477  goodPartition = ( fileName.find(partitionName) != std::string::npos );
478  }
479 
480  //bool rootFile = ( fileName.rfind(".root",5) == fileName.size()-5 );
481  if ( goodName && goodRun && rootFile && goodPartition ){
483  entry += "/";
484  entry += fileName;
485  files.push_back(entry);
486  }
487  }
488  closedir(dp);
489 
490  // Some debug
491  if ( !collate_histos && files.size() > 1 ) {
492  std::stringstream ss;
493  ss << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
494  << " Found more than one client file!";
495  std::vector<std::string>::const_iterator ifile = files.begin();
496  std::vector<std::string>::const_iterator jfile = files.end();
497  for ( ; ifile != jfile; ++ifile ) { ss << std::endl << *ifile; }
498  edm::LogError(mlDqmClient_) << ss.str();
499  } else if ( files.empty() ) {
501  << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
502  << " No input files found!" ;
503  }
504 
505 }
506 
static const char runNumber_[]
T getParameter(std::string const &) const
EventNumber_t event() const
Definition: EventID.h:41
Map map_
T getUntrackedParameter(std::string const &, T const &) const
void save(std::string &filename, uint32_t run_number=0, std::string partitionName="")
jfile
Definition: looper.py:279
bool existsAs(std::string const &parameterName, bool trackiness=true) const
checks if a parameter exists as a given type
Definition: ParameterSet.h:186
static const char dqmClientFileName_[]
void parseXML(const std::string &xml_file)
def setup(process, global_tag, zero_tesla=False)
Definition: GeneralSetup.py:1
static const char mlDqmClient_[]
#define nullptr
uint16_t size_type
sistrip classes
virtual void configure(const edm::ParameterSet &, const edm::EventSetup &)
static std::string runType(const sistrip::RunType &)
Parses the "summary plot" xml configuration file.
void extractHistograms(const std::vector< std::string > &)
static sistrip::RunType runType(DQMStore *const, const std::vector< std::string > &)
virtual void createHistos(const edm::ParameterSet &, const edm::EventSetup &)
SiStripCommissioningOfflineClient(const edm::ParameterSet &)
static uint32_t runNumber(DQMStore *const, const std::vector< std::string > &)
#define LogTrace(id)
virtual void setInputFiles(std::vector< std::string > &, const std::string, const std::string, uint32_t, bool)
void beginRun(const edm::Run &, const edm::EventSetup &) override
std::vector< SummaryPlot > summaryPlots(const sistrip::RunType &)
edm::EventID id() const
Definition: EventBase.h:60
HLT enums.
virtual void createSummaryHisto(const sistrip::Monitorable &, const sistrip::Presentation &, const std::string &top_level_dir, const sistrip::Granularity &)
static void copyCustomInformation(DQMStore *const, const std::vector< std::string > &)
static const char collate_[]
void remove(std::string pattern="")
save
Definition: cuy.py:1164
Definition: event.py:1
Definition: Run.h:44
void analyze(const edm::Event &, const edm::EventSetup &) override
virtual void histoAnalysis(bool debug)