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){
343  else
345  }
346  else{
348  histo->save( outputFileName_, runNumber_ );
349  }
350  }
351  else {
353  << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
354  << " Client file not saved!";
355  }
356  }
357 
358  // Virtual method to trigger the database upload
360 
361  // Print analyses
362  if ( histos_ ) {
364  histos_->printSummary();
365  }
366 
367  // Remove all ME/CME objects
368  if ( histos_ ) { histos_->remove(); }
369 
371  << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
372  << " Finished analyzing root file(s)...";
373 
374 }
375 
376 // -----------------------------------------------------------------------------
377 //
379  const edm::EventSetup& setup ) {
380  if ( !(event.id().event()%10) ) {
382  << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
383  << " Empty event loop! User can kill job...";
384  }
385 }
386 
387 // -----------------------------------------------------------------------------
388 //
390 
391 // -----------------------------------------------------------------------------
392 //
394 
395  // Check pointer
396  if ( histos_ ) {
398  << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
399  << " CommissioningHistogram object already exists!"
400  << " Aborting...";
401  return;
402  }
403 
404  // Check pointer to BEI
405  if ( !bei_ ) {
407  << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
408  << " NULL pointer to DQMStore!";
409  return;
410  }
411 
412  // Create "commissioning histograms" object
414  else if ( runType_ == sistrip::FED_CABLING ) { histos_ = new FedCablingHistograms( pset, bei_ ); }
415  else if ( runType_ == sistrip::APV_TIMING ) { histos_ = new ApvTimingHistograms( pset, bei_ ); }
416  else if ( runType_ == sistrip::OPTO_SCAN ) { histos_ = new OptoScanHistograms( pset, bei_ ); }
417  else if ( runType_ == sistrip::VPSP_SCAN ) { histos_ = new VpspScanHistograms( pset, bei_ ); }
418  else if ( runType_ == sistrip::PEDESTALS ) { histos_ = new PedestalsHistograms( pset, bei_ ); }
419  else if ( runType_ == sistrip::PEDS_FULL_NOISE ) { histos_ = new PedsFullNoiseHistograms( pset, bei_ ); }
420  else if ( runType_ == sistrip::PEDS_ONLY ) { histos_ = new PedsOnlyHistograms( pset, bei_ ); }
421  else if ( runType_ == sistrip::NOISE ) { histos_ = new NoiseHistograms( pset, bei_ ); }
422  else if ( runType_ == sistrip::APV_LATENCY ||
424  else if ( runType_ == sistrip::CALIBRATION ||
428  else if ( runType_ == sistrip::DAQ_SCOPE_MODE) { histos_ = new DaqScopeModeHistograms( pset, bei_);}
429  else if ( runType_ == sistrip::UNDEFINED_RUN_TYPE ) {
430  histos_ = nullptr;
432  << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
433  << " Undefined run type!";
434  return;
435  } else if ( runType_ == sistrip::UNKNOWN_RUN_TYPE ) {
436  histos_ = nullptr;
438  << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
439  << " Unknown run type!";
440  return;
441  }
442  histos_->configure(pset,setup);
443 }
444 
445 // -----------------------------------------------------------------------------
446 //
448  const std::string path,
449  const std::string partitionName,
450  uint32_t run_number,
451  bool collate_histos ) {
452 
453  std::string runStr;
454  std::stringstream ss;
455  ss << std::setfill('0') << std::setw(8) << run_number;
456  runStr = ss.str();
457 
458  std::string nameStr = "";
459  if ( !collate_histos ) { nameStr = "SiStripCommissioningClient_"; }
460  else { nameStr = "SiStripCommissioningSource_"; }
461 
462  LogTrace("TEST") << " runStr " << runStr;
463 
464  // Open directory
465  DIR* dp;
466  struct dirent* dirp;
467  if ( (dp = opendir(path.c_str())) == nullptr ) {
469  << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
470  << " Error locating directory \"" << path
471  << "\". No such directory!";
472  return;
473  }
474 
475  // Find compatible files
476  while ( (dirp = readdir(dp)) != nullptr ) {
477  std::string fileName(dirp->d_name);
478  bool goodName = ( fileName.find(nameStr) != std::string::npos );
479  bool goodRun = ( fileName.find(runStr) != std::string::npos );
480  bool rootFile = ( fileName.find(".root") != std::string::npos );
481  bool goodPartition = true;
482  if(not partitionName.empty()){
483  goodPartition = ( fileName.find(partitionName) != std::string::npos );
484  }
485 
486  //bool rootFile = ( fileName.rfind(".root",5) == fileName.size()-5 );
487  if ( goodName && goodRun && rootFile && goodPartition ){
489  entry += "/";
490  entry += fileName;
491  files.push_back(entry);
492  }
493  }
494  closedir(dp);
495 
496  // Some debug
497  if ( !collate_histos && files.size() > 1 ) {
498  std::stringstream ss;
499  ss << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
500  << " Found more than one client file!";
501  std::vector<std::string>::const_iterator ifile = files.begin();
502  std::vector<std::string>::const_iterator jfile = files.end();
503  for ( ; ifile != jfile; ++ifile ) { ss << std::endl << *ifile; }
504  edm::LogError(mlDqmClient_) << ss.str();
505  } else if ( files.empty() ) {
507  << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
508  << " No input files found!" ;
509  }
510 
511 }
512 
void save(std::string &filename, uint32_t run_number=0, std::string partitionName="")
static const char runNumber_[]
T getParameter(std::string const &) const
EventNumber_t event() const
Definition: EventID.h:41
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:282
bool existsAs(std::string const &parameterName, bool trackiness=true) const
checks if a parameter exists as a given type
Definition: ParameterSet.h:161
static const char dqmClientFileName_[]
#define nullptr
void parseXML(const std::string &xml_file)
def setup(process, global_tag, zero_tesla=False)
Definition: GeneralSetup.py:2
static const char mlDqmClient_[]
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:59
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:1165
Definition: event.py:1
Definition: Run.h:45
void analyze(const edm::Event &, const edm::EventSetup &) override
virtual void histoAnalysis(bool debug)