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