CMS 3D CMS Logo

SiStripCommissioningOfflineClient.cc
Go to the documentation of this file.
1 
22 #include <iostream>
23 #include <iomanip>
24 #include <fstream>
25 #include <sstream>
26 #include <sys/types.h>
27 #include <dirent.h>
28 #include <cerrno>
29 #include "TProfile.h"
30 #include <cstdint>
31 
32 using namespace sistrip;
33 
34 // -----------------------------------------------------------------------------
35 //
37  : bei_(edm::Service<DQMStore>().operator->()),
38  histos_(nullptr),
39  //inputFiles_( pset.getUntrackedParameter< std::vector<std::string> >( "InputRootFiles", std::vector<std::string>() ) ),
40  outputFileName_(pset.getUntrackedParameter<std::string>("OutputRootFile", "")),
41  collateHistos_(!pset.getUntrackedParameter<bool>("UseClientFile", false)),
42  analyzeHistos_(pset.getUntrackedParameter<bool>("AnalyzeHistos", true)),
43  xmlFile_((pset.getUntrackedParameter<edm::FileInPath>("SummaryXmlFile", edm::FileInPath())).fullPath()),
44  createSummaryPlots_(false),
45  clientHistos_(false),
46  uploadToDb_(false),
47  runType_(sistrip::UNKNOWN_RUN_TYPE),
48  runNumber_(0),
49  partitionName_(pset.existsAs<std::string>("PartitionName") ? pset.getParameter<std::string>("PartitionName")
50  : ""),
51  map_(),
52  plots_(),
53  parameters_(pset) {
54  LogTrace(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
55  << " Constructing object...";
57  pset.getUntrackedParameter<std::string>("FilePath"),
58  pset.existsAs<std::string>("PartitionName") ? pset.getParameter<std::string>("PartitionName") : "",
59  pset.getUntrackedParameter<uint32_t>("RunNumber"),
61 }
62 
63 // -----------------------------------------------------------------------------
64 //
66  LogTrace(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
67  << " Destructing object...";
68 }
69 
70 // -----------------------------------------------------------------------------
71 //
73  LogTrace(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
74  << " Analyzing root file(s)...";
75 
76  // Check for null pointer
77  if (!bei_) {
78  edm::LogError(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
79  << " NULL pointer to DQMStore!"
80  << " Aborting...";
81  return;
82  }
83 
84  // Check if .root file can be opened
85  std::vector<std::string>::const_iterator ifile = inputFiles_.begin();
86  for (; ifile != inputFiles_.end(); ifile++) {
87  std::ifstream root_file;
88  root_file.open(ifile->c_str());
89  if (!root_file.is_open()) {
90  edm::LogError(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
91  << " The input root file \"" << *ifile << "\" could not be opened!"
92  << " Please check the path and filename!";
93  } else {
94  root_file.close();
96  if (found != std::string::npos && clientHistos_) {
97  edm::LogError(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
98  << " The input root files appear to be a mixture"
99  << " of \"Source\" and \"Client\" files!"
100  << " Aborting...";
101  return;
102  }
103  if (found != std::string::npos && inputFiles_.size() != 1) {
104  edm::LogError(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
105  << " There appear to be multiple input \"Client\" root files!"
106  << " Aborting...";
107  return;
108  }
109  if (found != std::string::npos) {
110  clientHistos_ = true;
111  }
112  }
113  }
114  if (clientHistos_ && inputFiles_.size() == 1) {
115  edm::LogVerbatim(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
116  << " Collated histograms found in input root file \"" << inputFiles_[0] << "\"";
117  }
118 
119  // Check if .xml file can be opened
120  if (!xmlFile_.empty()) {
121  std::ifstream xml_file;
122  xml_file.open(xmlFile_.c_str());
123  if (!xml_file.is_open()) {
124  edm::LogError(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
125  << " The SummaryPlot XML file \"" << xmlFile_ << "\" could not be opened!"
126  << " Please check the path and filename!"
127  << " Aborting...";
128  return;
129  } else {
130  createSummaryPlots_ = true;
131  xml_file.close();
132  }
133  }
134 
135  // Open root file(s) and create ME's
136  if (inputFiles_.empty()) {
137  edm::LogError(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
138  << " No input root files specified!";
139  return;
140  }
141 
142  edm::LogVerbatim(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
143  << " Opening root files. This may take some time!...";
144  std::vector<std::string>::const_iterator jfile = inputFiles_.begin();
145  for (; jfile != inputFiles_.end(); jfile++) {
146  LogTrace(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
147  << " Opening root file \"" << *jfile << "\"... (This may take some time.)";
148  if (clientHistos_) {
149  bei_->open(*jfile, false, sistrip::collate_, "");
150  } else {
151  bei_->open(*jfile, false, "SiStrip", sistrip::collate_);
152  }
153  LogTrace(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
154  << " Opened root file \"" << *jfile << "\"!";
155  }
156  edm::LogVerbatim(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
157  << " Opened " << inputFiles_.size() << " root files!";
158 
159  // Retrieve list of histograms
160  auto allmes = bei_->getAllContents("");
161  std::vector<std::string> contents;
162 
163  // If using client file, remove "source" histograms from list
164  if (clientHistos_) {
165  std::set<std::string> temp;
166  for (auto me : allmes) {
167  const auto& name = me->getPathname();
168  if (name.find(sistrip::collate_) != std::string::npos) {
169  temp.insert(name);
170  }
171  }
172  contents.clear();
173  for (const auto& s : temp) {
174  // the old code expects a ":", but does not really need the ME names
175  contents.push_back(s + ":");
176  }
177  }
178 
179  // Some debug
180  LogTrace(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
181  << " Found " << contents.size() << " directories containing MonitorElements";
182 
183  // Some more debug
184  if (false) {
185  std::stringstream ss;
186  ss << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
187  << " Directories found: " << std::endl;
188  std::vector<std::string>::iterator istr = contents.begin();
189  for (; istr != contents.end(); istr++) {
190  ss << " " << *istr << std::endl;
191  }
192  LogTrace(mlDqmClient_) << ss.str();
193  }
194 
195  // Extract run type from contents
197 
198  // Extract run number from contents
200 
201  // Copy custom information to the collated structure
203 
204  // Check runType
206  edm::LogError(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
207  << " Unknown commissioning runType: " << SiStripEnumsAndStrings::runType(runType_)
208  << " and run number is " << runNumber_;
209  return;
210  } else {
211  edm::LogVerbatim(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
212  << " Run type is " << SiStripEnumsAndStrings::runType(runType_)
213  << " and run number is " << runNumber_;
214  }
215 
216  // Open and parse "summary plot" xml file
217  if (createSummaryPlots_) {
218  edm::LogVerbatim(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
219  << " Parsing summary plot XML file...";
220  SummaryPlotXmlParser xml_file;
221  xml_file.parseXML(xmlFile_);
222  plots_ = xml_file.summaryPlots(runType_);
223  edm::LogVerbatim(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
224  << " Parsed summary plot XML file and found " << plots_.size() << " plots defined!";
225  } else {
226  edm::LogWarning(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
227  << " Null string for SummaryPlotXmlFile!"
228  << " No summary plots will be created!";
229  }
230 
231  // Some debug
232  std::stringstream ss;
233  ss << "[SiStripCommissioningOfflineClient::" << __func__ << "]" << std::endl << " Input root files : ";
234  if (inputFiles_.empty()) {
235  ss << "(none)";
236  } else {
237  std::vector<std::string>::const_iterator ifile = inputFiles_.begin();
238  for (; ifile != inputFiles_.end(); ifile++) {
239  if (ifile != inputFiles_.begin()) {
240  ss << std::setw(25) << std::setfill(' ') << ": ";
241  }
242  ss << "\"" << *ifile << "\"" << std::endl;
243  }
244  }
245  ss << " Run type : \"" << SiStripEnumsAndStrings::runType(runType_) << "\"" << std::endl
246  << " Run number : " << runNumber_ << std::endl
247  << " Summary plot XML file : ";
248  if (xmlFile_.empty()) {
249  ss << "(none)";
250  } else {
251  ss << "\"" << xmlFile_ << "\"";
252  }
253  edm::LogVerbatim(mlDqmClient_) << ss.str();
254 
255  // Virtual method that creates CommissioningHistogram object
256  LogTrace(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
257  << " Creating CommissioningHistogram object...";
259  if (histos_) {
260  LogTrace(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
261  << " Created CommissioningHistogram object!";
262  } else {
263  edm::LogError(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
264  << " NULL pointer to CommissioningHistogram object!"
265  << " Aborting...";
266  return;
267  }
268 
269  // Perform collation
270  if (histos_) {
272  }
273 
274  // Perform analysis
275  if (analyzeHistos_) {
276  LogTrace(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
277  << " Analyzing histograms...";
278  if (histos_) {
279  histos_->histoAnalysis(true);
280  }
281  LogTrace(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
282  << " Analyzed histograms!";
283  } else {
284  edm::LogWarning(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
285  << " No histogram analysis performed!";
286  }
287 
288  // Create summary plots
289  if (createSummaryPlots_) {
290  edm::LogVerbatim(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
291  << " Generating summary plots...";
292  std::vector<SummaryPlot>::const_iterator iplot = plots_.begin();
293  for (; iplot != plots_.end(); iplot++) {
294  if (histos_) {
295  histos_->createSummaryHisto(iplot->monitorable(), iplot->presentation(), iplot->level(), iplot->granularity());
296  }
297  }
298  edm::LogVerbatim(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
299  << " Generated summary plots!";
300  } else {
301  edm::LogWarning(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
302  << " No summary plots generated!";
303  }
304 
305  // Save client root file
306  if (histos_) {
307  bool save = parameters_.getUntrackedParameter<bool>("SaveClientFile", true);
308  if (save) {
312  else
314  } else {
317  }
318  } else {
319  edm::LogVerbatim(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
320  << " Client file not saved!";
321  }
322  }
323 
324  // Virtual method to trigger the database upload
326 
327  // Print analyses
328  if (histos_) {
331  }
332 
333  // Remove all ME/CME objects
334  if (histos_) {
335  histos_->remove();
336  }
337 
338  edm::LogVerbatim(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
339  << " Finished analyzing root file(s)...";
340 }
341 
342 // -----------------------------------------------------------------------------
343 //
345  if (!(event.id().event() % 10)) {
346  LogTrace(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
347  << " Empty event loop! User can kill job...";
348  }
349 }
350 
351 // -----------------------------------------------------------------------------
352 //
354 
355 // -----------------------------------------------------------------------------
356 //
358  // Check pointer
359  if (histos_) {
360  edm::LogError(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
361  << " CommissioningHistogram object already exists!"
362  << " Aborting...";
363  return;
364  }
365 
366  // Check pointer to BEI
367  if (!bei_) {
368  edm::LogError(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
369  << " NULL pointer to DQMStore!";
370  return;
371  }
372 
373  // Create "commissioning histograms" object
376  } else if (runType_ == sistrip::FED_CABLING) {
378  } else if (runType_ == sistrip::APV_TIMING) {
380  } else if (runType_ == sistrip::OPTO_SCAN) {
382  } else if (runType_ == sistrip::VPSP_SCAN) {
384  } else if (runType_ == sistrip::PEDESTALS) {
386  } else if (runType_ == sistrip::PEDS_FULL_NOISE) {
388  } else if (runType_ == sistrip::PEDS_ONLY) {
390  } else if (runType_ == sistrip::NOISE) {
397  } else if (runType_ == sistrip::DAQ_SCOPE_MODE) {
399  } else if (runType_ == sistrip::UNDEFINED_RUN_TYPE) {
400  histos_ = nullptr;
401  edm::LogError(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
402  << " Undefined run type!";
403  return;
404  } else if (runType_ == sistrip::UNKNOWN_RUN_TYPE) {
405  histos_ = nullptr;
406  edm::LogError(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
407  << " Unknown run type!";
408  return;
409  }
411 }
412 
413 // -----------------------------------------------------------------------------
414 //
416  const std::string path,
418  uint32_t run_number,
419  bool collate_histos) {
420  std::string runStr;
421  std::stringstream ss;
422  ss << std::setfill('0') << std::setw(8) << run_number;
423  runStr = ss.str();
424 
425  std::string nameStr = "";
426  if (!collate_histos) {
427  nameStr = "SiStripCommissioningClient_";
428  } else {
429  nameStr = "SiStripCommissioningSource_";
430  }
431 
432  LogTrace("TEST") << " runStr " << runStr;
433 
434  // Open directory
435  DIR* dp;
436  struct dirent* dirp;
437  if ((dp = opendir(path.c_str())) == nullptr) {
438  edm::LogError(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
439  << " Error locating directory \"" << path << "\". No such directory!";
440  return;
441  }
442 
443  // Find compatible files
444  while ((dirp = readdir(dp)) != nullptr) {
445  std::string fileName(dirp->d_name);
446  bool goodName = (fileName.find(nameStr) != std::string::npos);
447  bool goodRun = (fileName.find(runStr) != std::string::npos);
448  bool rootFile = (fileName.find(".root") != std::string::npos);
449  bool goodPartition = true;
450  if (not partitionName.empty()) {
451  goodPartition = (fileName.find(partitionName) != std::string::npos);
452  }
453 
454  //bool rootFile = ( fileName.rfind(".root",5) == fileName.size()-5 );
455  if (goodName && goodRun && rootFile && goodPartition) {
457  entry += "/";
458  entry += fileName;
459  files.push_back(entry);
460  }
461  }
462  closedir(dp);
463 
464  // Some debug
465  if (!collate_histos && files.size() > 1) {
466  std::stringstream ss;
467  ss << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
468  << " Found more than one client file!";
469  std::vector<std::string>::const_iterator ifile = files.begin();
470  std::vector<std::string>::const_iterator jfile = files.end();
471  for (; ifile != jfile; ++ifile) {
472  ss << std::endl << *ifile;
473  }
474  edm::LogError(mlDqmClient_) << ss.str();
475  } else if (files.empty()) {
476  edm::LogError(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
477  << " No input files found!";
478  }
479 }
static const char runNumber_[]
Log< level::Info, true > LogVerbatim
void save(std::string &filename, uint32_t run_number=0, std::string partitionName="")
jfile
Definition: looper.py:281
static const char dqmClientFileName_[]
void parseXML(const std::string &xml_file)
static const char mlDqmClient_[]
Log< level::Error, false > LogError
uint16_t size_type
sistrip classes
virtual void configure(const edm::ParameterSet &, const edm::EventSetup &)
#define LogTrace(id)
T getUntrackedParameter(std::string const &, T const &) const
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 std::vector< dqm::harvesting::MonitorElement * > getAllContents(std::string const &path) const
Definition: DQMStore.cc:609
virtual void createHistos(const edm::ParameterSet &, const edm::EventSetup &)
SiStripCommissioningOfflineClient(const edm::ParameterSet &)
static uint32_t runNumber(DQMStore *const, const std::vector< std::string > &)
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 &)
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 > &)
Log< level::Warning, false > LogWarning
static const char collate_[]
void remove(std::string pattern="")
save
Definition: cuy.py:1164
DQM_DEPRECATED bool open(std::string const &filename, bool overwrite=false, std::string const &path="", std::string const &prepend="", OpenRunDirs stripdirs=KeepRunDirs, bool fileMustExist=true)
Definition: DQMStore.cc:790
Definition: event.py:1
Definition: Run.h:45
void analyze(const edm::Event &, const edm::EventSetup &) override
virtual void histoAnalysis(bool debug)