CMS 3D CMS Logo

CSCDQM_Collection.cc
Go to the documentation of this file.
1 /* =====================================================================================
2  *
3  * Filename: CSCDQM_Collection.cc
4  *
5  * Description: Histogram booking code
6  *
7  * Version: 1.0
8  * Created: 04/18/2008 03:39:49 PM
9  * Revision: none
10  * Compiler: gcc
11  *
12  * Author: Valdas Rapsevicius (VR), Valdas.Rapsevicius@cern.ch
13  * Company: CERN, CH
14  *
15  * =====================================================================================
16  */
17 
18 #include "CSCDQM_Collection.h"
20 #include <cstdio>
21 #include <string>
22 #include <xercesc/util/XMLString.hpp>
23 #include <xercesc/util/TransService.hpp>
24 
25 namespace cscdqm {
26 
31  Collection::Collection(Configuration* const p_config) { config = p_config; }
32 
38  LOG_INFO << "Reading histograms from " << config->getBOOKING_XML_FILE();
39 
40  if (config->getBOOKING_XML_FILE().empty()) {
41  return;
42  }
43 
44  try {
46  {
48 
49  parser.setValidationScheme(XercesDOMParser::Val_Always);
50  parser.setDoNamespaces(true);
51  parser.setDoSchema(true);
52  parser.setExitOnFirstFatalError(true);
53  parser.setValidationConstraintFatal(true);
55  parser.setErrorHandler(&eh);
56  parser.parse(config->getBOOKING_XML_FILE().c_str());
57 
58  DOMDocument* doc = parser.getDocument();
59  DOMElement* docNode = doc->getDocumentElement();
60  DOMNodeList* itemList = docNode->getChildNodes();
61 
63  for (XMLSize_t i = 0; i < itemList->getLength(); i++) {
64  DOMNode* node = itemList->item(i);
65  if (node->getNodeType() != DOMNode::ELEMENT_NODE) {
66  continue;
67  }
68 
69  std::string nodeName = XMLString::transcode(node->getNodeName());
70 
74  if (nodeName == XML_BOOK_DEFINITION) {
76  getNodeProperties(node, dp);
77 
78  DOMElement* el = dynamic_cast<DOMElement*>(node);
79  std::string id(XMLString::transcode(el->getAttribute(XMLString::transcode(XML_BOOK_DEFINITION_ID))));
80  definitions.insert(make_pair(id, dp));
81 
82  } else
83 
87  if (nodeName == XML_BOOK_HISTOGRAM) {
89 
90  DOMElement* el = dynamic_cast<DOMElement*>(node);
91  if (el->hasAttribute(XMLString::transcode(XML_BOOK_DEFINITION_REF))) {
92  std::string id(XMLString::transcode(el->getAttribute(XMLString::transcode(XML_BOOK_DEFINITION_REF))));
93 
95  for (CoHistoProps::iterator it = d.begin(); it != d.end(); it++) {
96  hp[it->first] = it->second;
97  }
98  }
99 
100  getNodeProperties(node, hp);
101 
104 
105  // Check if this histogram is an ON DEMAND histogram?
108 
109  LOG_DEBUG << "[Collection::load] loading " << prefix << "::" << name
110  << " XML_BOOK_ONDEMAND = " << hp[XML_BOOK_ONDEMAND];
111 
112  CoHistoMap::iterator it = collection.find(prefix);
113  if (it == collection.end()) {
114  CoHisto h;
115  h[name] = hp;
116  collection[prefix] = h;
117  } else {
118  it->second.insert(make_pair(name, hp));
119  }
120  }
121  }
122  }
123 
125 
126  } catch (XMLException& e) {
127  char* message = XMLString::transcode(e.getMessage());
128  throw Exception(message);
129  }
130 
131  for (CoHistoMap::const_iterator i = collection.begin(); i != collection.end(); i++) {
132  LOG_INFO << i->second.size() << " " << i->first << " histograms defined";
133  }
134  }
135 
145  DOMNodeList* props = node->getChildNodes();
146 
147  for (XMLSize_t j = 0; j < props->getLength(); j++) {
148  DOMNode* node = props->item(j);
149  if (node->getNodeType() != DOMNode::ELEMENT_NODE) {
150  continue;
151  }
152  DOMElement* element = dynamic_cast<DOMElement*>(node);
153  std::string name = XMLString::transcode(element->getNodeName());
154 
155  const XMLCh* content = element->getTextContent();
156  XERCES_CPP_NAMESPACE_QUALIFIER TranscodeToStr tc(content, "UTF-8");
157  std::istringstream buffer((const char*)tc.str());
158  std::string value = buffer.str();
159 
160  DOMNamedNodeMap* attributes = node->getAttributes();
161  if (attributes) {
162  for (XMLSize_t i = 0; i < attributes->getLength(); i++) {
163  DOMNode* attribute = attributes->item(i);
164  std::string aname = XMLString::transcode(attribute->getNodeName());
165  std::string avalue = XMLString::transcode(attribute->getNodeValue());
166  p[name + "_" + aname] = avalue;
167  }
168  }
169  p[name] = value;
170  }
171  }
172 
181  CoHistoProps::const_iterator i = h.find(name);
182  if (i == h.end()) {
183  return false;
184  }
185  value = i->second;
186  return true;
187  }
188 
196  const bool Collection::checkHistoValue(const CoHistoProps& h, const std::string& name, int& value) {
197  CoHistoProps::const_iterator i = h.find(name);
198  if (i == h.end()) {
199  return false;
200  }
201  if (EOF == std::sscanf(i->second.c_str(), "%d", &value)) {
202  return false;
203  }
204  return true;
205  }
206 
215  const bool Collection::checkHistoValue(const CoHistoProps& h, const std::string name, double& value) {
216  CoHistoProps::const_iterator i = h.find(name);
217  if (i == h.end()) {
218  return false;
219  }
220  if (EOF == std::sscanf(i->second.c_str(), "%lf", &value)) {
221  return false;
222  }
223  return true;
224  }
225 
235  const std::string& name,
237  const std::string& def_value) {
238  if (!checkHistoValue(h, name, value)) {
239  value = def_value;
240  }
241  return value;
242  }
243 
252  int& Collection::getHistoValue(const CoHistoProps& h, const std::string& name, int& value, const int& def_value) {
253  if (!checkHistoValue(h, name, value)) {
254  value = def_value;
255  }
256  return value;
257  }
258 
267  double& Collection::getHistoValue(const CoHistoProps& h, const std::string name, double& value, const int def_value) {
268  if (!checkHistoValue(h, name, value)) {
269  value = def_value;
270  }
271  return value;
272  }
273 
280  const int Collection::ParseAxisLabels(const std::string& s, std::map<int, std::string>& labels) {
281  std::string tmp = s;
282  std::string::size_type pos = tmp.find('|');
283  char* stopstring = nullptr;
284 
285  while (pos != std::string::npos) {
286  std::string label_pair = tmp.substr(0, pos);
287  tmp.replace(0, pos + 1, "");
288  if (label_pair.find('=') != std::string::npos) {
289  int nbin = strtol(label_pair.substr(0, label_pair.find('=')).c_str(), &stopstring, 10);
290  std::string label = label_pair.substr(label_pair.find('=') + 1, label_pair.length());
291  while (label.find('\'') != std::string::npos) {
292  label.erase(label.find('\''), 1);
293  }
294  labels[nbin] = label;
295  }
296  pos = tmp.find('|');
297  }
298  return labels.size();
299  }
300 
306  CoHistoMap::const_iterator i = collection.find("EMU");
307  if (i != collection.end()) {
308  const CoHisto hs = i->second;
309  for (CoHisto::const_iterator j = hs.begin(); j != hs.end(); j++) {
310  std::string s = "";
312  HistoId hid = 0;
313  if (HistoDef::getHistoIdByName(j->first, hid)) {
314  EMUHistoDef hdef(hid);
315  book(hdef, j->second, config->getFOLDER_EMU());
316  }
317  }
318  }
319  }
320  }
321 
328  CoHistoMap::const_iterator i = collection.find("FED");
329  if (i != collection.end()) {
330  const CoHisto hs = i->second;
331  for (CoHisto::const_iterator j = hs.begin(); j != hs.end(); j++) {
332  std::string s = "";
334  HistoId hid = 0;
335  if (HistoDef::getHistoIdByName(j->first, hid)) {
336  FEDHistoDef hdef(hid, fedId);
337  book(hdef, j->second, config->getFOLDER_FED());
338  }
339  }
340  }
341  }
342  }
343 
349  void Collection::bookDDUHistos(const HwId dduId) const {
350  CoHistoMap::const_iterator i = collection.find("DDU");
351  if (i != collection.end()) {
352  const CoHisto hs = i->second;
353  for (CoHisto::const_iterator j = hs.begin(); j != hs.end(); j++) {
354  std::string s = "";
356  HistoId hid = 0;
357  if (HistoDef::getHistoIdByName(j->first, hid)) {
358  DDUHistoDef hdef(hid, dduId);
359  book(hdef, j->second, config->getFOLDER_DDU());
360  }
361  }
362  }
363  }
364  }
365 
372  void Collection::bookCSCHistos(const HwId crateId, const HwId dmbId) const {
373  CoHistoMap::const_iterator i = collection.find("CSC");
374  if (i != collection.end()) {
375  const CoHisto hs = i->second;
376  for (CoHisto::const_iterator j = hs.begin(); j != hs.end(); j++) {
377  std::string s = "";
378  HistoId hid = 0;
379  if (HistoDef::getHistoIdByName(j->first, hid)) {
381  CSCHistoDef hdef(hid, crateId, dmbId);
382  book(hdef, j->second, config->getFOLDER_CSC());
383  } else {
384  int from = 0, to = 0;
385  if (checkHistoValue(j->second, XML_BOOK_NAME_FROM, from) &&
386  checkHistoValue(j->second, XML_BOOK_NAME_TO, to)) {
387  for (int k = from; k <= to; k++) {
388  CSCHistoDef hdef(hid, crateId, dmbId, k);
389  book(hdef, j->second, config->getFOLDER_CSC());
390  }
391  }
392  }
393  }
394  }
395  }
396  }
397 
406  void Collection::bookCSCHistos(const HistoId hid, const HwId crateId, const HwId dmbId, const HwId addId) const {
407  CoHistoMap::const_iterator i = collection.find("CSC");
408  if (i != collection.end()) {
409  CoHisto::const_iterator j = i->second.find(h::names[hid]);
410  if (j != i->second.end()) {
411  CSCHistoDef hdef(hid, crateId, dmbId, addId);
412  book(hdef, j->second, config->getFOLDER_CSC());
413  }
414  }
415  }
416 
424  void Collection::book(const HistoDef& h, const CoHistoProps& p, const std::string& folder) const {
425  MonitorObject* me = nullptr;
426  std::string name = h.getName(), type, title, s;
427 
429  if (!config->needBookMO(h.getFullPath())) {
430  LOG_INFO << "MOFilter excluded " << name << " from booking";
431  config->fnPutHisto(h, me);
432  return;
433  }
434 
435  int i1, i2, i3;
436  double d1, d2, d3, d4, d5, d6;
437  bool ondemand =
439 
441  throw Exception("Histogram does not have type!");
442  }
444 
445  if (ondemand) {
446  title = h.processTitle(title);
447  }
448 
449  if (type == "h1") {
450  me = config->fnBook(HistoBookRequest(h,
451  H1D,
452  type,
453  folder,
454  title,
455  getHistoValue(p, "XBins", i1, 1),
456  getHistoValue(p, "XMin", d1, 0),
457  getHistoValue(p, "XMax", d2, 1)));
458  } else if (type == "h2") {
459  me = config->fnBook(HistoBookRequest(h,
460  H2D,
461  type,
462  folder,
463  title,
464  getHistoValue(p, "XBins", i1, 1),
465  getHistoValue(p, "XMin", d1, 0),
466  getHistoValue(p, "XMax", d2, 1),
467  getHistoValue(p, "YBins", i2, 1),
468  getHistoValue(p, "YMin", d3, 0),
469  getHistoValue(p, "YMax", d4, 1)));
470  } else if (type == "h3") {
471  me = config->fnBook(HistoBookRequest(h,
472  H3D,
473  type,
474  folder,
475  title,
476  getHistoValue(p, "XBins", i1, 1),
477  getHistoValue(p, "XMin", d1, 0),
478  getHistoValue(p, "XMax", d2, 1),
479  getHistoValue(p, "YBins", i2, 1),
480  getHistoValue(p, "YMin", d3, 0),
481  getHistoValue(p, "YMax", d4, 1),
482  getHistoValue(p, "ZBins", i3, 1),
483  getHistoValue(p, "ZMin", d5, 0),
484  getHistoValue(p, "ZMax", d6, 1)));
485  } else if (type == "hp") {
486  me = config->fnBook(HistoBookRequest(h,
487  PROFILE,
488  type,
489  folder,
490  title,
491  getHistoValue(p, "XBins", i1, 1),
492  getHistoValue(p, "XMin", d1, 0),
493  getHistoValue(p, "XMax", d2, 1)));
494  /*
495  HistoBookRequest(h, PROFILE, type, folder, title,
496  getHistoValue(p, "XBins", i1, 1),
497  getHistoValue(p, "XMin", d1, 0),
498  getHistoValue(p, "XMax", d2, 1),
499  getHistoValue(p, "YBins", i2, 1),
500  getHistoValue(p, "YMin", d3, 0),
501  getHistoValue(p, "YMax", d4, 1)));
502  */
503  } else if (type == "hp2") {
504  me = config->fnBook(HistoBookRequest(h,
505  PROFILE2D,
506  type,
507  folder,
508  title,
509  getHistoValue(p, "XBins", i1, 1),
510  getHistoValue(p, "XMin", d1, 0),
511  getHistoValue(p, "XMax", d2, 1),
512  getHistoValue(p, "YBins", i2, 1),
513  getHistoValue(p, "YMin", d3, 0),
514  getHistoValue(p, "YMax", d4, 1),
515  getHistoValue(p, "ZBins", i3, 1),
516  getHistoValue(p, "ZMin", d5, 0),
517  getHistoValue(p, "ZMax", d6, 1)));
518  } else {
519  throw Exception("Can not book histogram with type: " + type);
520  }
521 
522  if (me != nullptr) {
523  LockType lock(me->mutex);
524  TH1* th = me->getTH1Lock();
525 
526  if (checkHistoValue(p, "XTitle", s)) {
527  if (ondemand) {
528  s = h.processTitle(s);
529  }
530  me->setAxisTitle(s, 1);
531  }
532 
533  if (checkHistoValue(p, "YTitle", s)) {
534  if (ondemand) {
535  s = h.processTitle(s);
536  }
537  me->setAxisTitle(s, 2);
538  }
539 
540  if (checkHistoValue(p, "ZTitle", s)) {
541  if (ondemand) {
542  s = h.processTitle(s);
543  }
544  me->setAxisTitle(s, 3);
545  }
546 
547  if (checkHistoValue(p, "SetOption", s))
548  th->SetOption(s.c_str());
549  if (checkHistoValue(p, "SetStats", i1))
550  th->SetStats(i1);
551  th->SetFillColor(getHistoValue(p, "SetFillColor", i1, DEF_HISTO_COLOR));
552  if (checkHistoValue(p, "SetXLabels", s)) {
553  std::map<int, std::string> labels;
555  th->GetXaxis()->SetNoAlphanumeric(); // For ROOT6 to prevent getting zero means values
556  for (std::map<int, std::string>::iterator l_itr = labels.begin(); l_itr != labels.end(); ++l_itr) {
557  th->GetXaxis()->SetBinLabel(l_itr->first, l_itr->second.c_str());
558  }
559  }
560  if (checkHistoValue(p, "SetYLabels", s)) {
561  std::map<int, std::string> labels;
563  th->GetYaxis()->SetNoAlphanumeric(); // For ROOT6 to prevent getting zero means values
564  for (std::map<int, std::string>::iterator l_itr = labels.begin(); l_itr != labels.end(); ++l_itr) {
565  th->GetYaxis()->SetBinLabel(l_itr->first, l_itr->second.c_str());
566  }
567  }
568  if (checkHistoValue(p, "LabelOption", s)) {
569  std::vector<std::string> v;
570  if (2 == Utility::tokenize(s, v, ",")) {
571  th->LabelsOption(v[0].c_str(), v[1].c_str());
572  }
573  }
574  if (checkHistoValue(p, "SetLabelSize", s)) {
575  std::vector<std::string> v;
576  if (2 == Utility::tokenize(s, v, ",")) {
577  th->SetLabelSize((double)atof(v[0].c_str()), v[1].c_str());
578  }
579  }
580  if (checkHistoValue(p, "SetTitleOffset", s)) {
581  std::vector<std::string> v;
582  if (2 == Utility::tokenize(s, v, ",")) {
583  th->SetTitleOffset((double)atof(v[0].c_str()), v[1].c_str());
584  }
585  }
586  if (checkHistoValue(p, "SetMinimum", d1))
587  th->SetMinimum(d1);
588  if (checkHistoValue(p, "SetMaximum", d1))
589  me->SetMaximum(d1);
590  if (checkHistoValue(p, "SetNdivisionsX", i1)) {
591  th->SetNdivisions(i1, "X");
592  th->GetXaxis()->CenterLabels(true);
593  }
594  if (checkHistoValue(p, "SetNdivisionsY", i1)) {
595  th->SetNdivisions(i1, "Y");
596  th->GetYaxis()->CenterLabels(true);
597  }
598  if (checkHistoValue(p, "SetTickLengthX", d1))
599  th->SetTickLength(d1, "X");
600  if (checkHistoValue(p, "SetTickLengthY", d1))
601  th->SetTickLength(d1, "Y");
602  if (checkHistoValue(p, "SetLabelSizeX", d1))
603  th->SetLabelSize(d1, "X");
604  if (checkHistoValue(p, "SetLabelSizeY", d1))
605  th->SetLabelSize(d1, "Y");
606  if (checkHistoValue(p, "SetLabelSizeZ", d1))
607  th->SetLabelSize(d1, "Z");
608  if (checkHistoValue(p, "SetErrorOption", s))
609  reinterpret_cast<TProfile*>(th)->SetErrorOption(s.c_str());
610 
611  lock.unlock();
612  }
613 
614  LOG_DEBUG << "[Collection::book] booked " << h.getFullPath() << " (" << me << ")";
615 
617  config->fnPutHisto(h, me);
618  }
619 
625  const bool Collection::isOnDemand(const HistoName& name) const {
626  CoHistoMap::const_iterator i = collection.find("CSC");
627  if (i != collection.end()) {
628  CoHisto hs = i->second;
629  CoHisto::const_iterator j = hs.find(name);
630  if (j != hs.end()) {
631  std::string s;
633  }
634  }
635  return false;
636  }
637 
643  std::ostringstream buffer;
644  for (CoHistoMap::const_iterator hdmi = collection.begin(); hdmi != collection.end(); hdmi++) {
645  buffer << hdmi->first << " [" << std::endl;
646  for (CoHisto::const_iterator hdi = hdmi->second.begin(); hdi != hdmi->second.end(); hdi++) {
647  buffer << " " << hdi->first << " [" << std::endl;
648  for (CoHistoProps::const_iterator hi = hdi->second.begin(); hi != hdi->second.end(); hi++) {
649  buffer << " " << hi->first << " = " << hi->second << std::endl;
650  }
651  buffer << " ]" << std::endl;
652  }
653  buffer << " ]" << std::endl;
654  }
655  LOG_INFO << buffer.str();
656  }
657 
658 } // namespace cscdqm
static const bool getHistoIdByName(const std::string &p_name, HistoId &p_id)
Get Histogram ID by name.
static const char XML_BOOK_DEFINITION_ID[]
static const char XML_BOOK_ONDEMAND_FALSE[]
unsigned int HwId
static const char XML_BOOK_HISTO_TYPE[]
static const char XML_BOOK_NAME_FROM[]
static const HistoName names[]
#define LOG_INFO
Definition: CSCDQM_Logger.h:42
Monitoring Object interface used to cover Root object and provide common interface to EventProcessor ...
void xercesTerminate()
Definition: Xerces.cc:23
Abstract Base Histogram Definition.
static const char XML_BOOK_NAME_TO[]
static const char XML_BOOK_ONDEMAND[]
const bool isOnDemand(const HistoName &name) const
Check if the histogram is on demand (by histogram name)
static const char XML_BOOK_DEFINITION_REF[]
static const char XML_BOOK_HISTOGRAM[]
xercesc::DOMDocument DOMDocument
void bookEMUHistos() const
Book EMU histograms.
Definition: config.py:1
static bool regexMatch(const std::string &expression, const std::string &message)
Match RegExp expression string against string message and return result.
#define XERCES_CPP_NAMESPACE_QUALIFIER
Definition: LHERunInfo.h:16
void printCollection() const
Print collection of available histograms and their parameters.
void xercesInitialize()
Definition: Xerces.cc:18
CSC Level Histogram Type.
Collection(Configuration *const p_config)
Constructor.
uint16_t size_type
char const * label
CSCDQM Framework Global Configuration.
unsigned int HistoId
static const char XML_BOOK_HISTO_PREFIX[]
Definition: EPCuts.h:4
static void getNodeProperties(DOMNode *&node, CoHistoProps &hp)
Extract and write single histogram properties from XML node to map.
xercesc::DOMNode DOMNode
static std::string & getHistoValue(const CoHistoProps &h, const std::string &name, std::string &value, const std::string &def_value="")
Find string histogram value in map.
void book(const HistoDef &h, const CoHistoProps &p, const std::string &folder) const
Book histogram.
xercesc::DOMElement DOMElement
EMU Level Histogram Definition.
void load()
Load XML file and fill definition map(s)
static const bool checkHistoValue(const CoHistoProps &h, const std::string &name, std::string &value)
Find string histogram value in map.
Definition: value.py:1
xercesc::DOMNodeList DOMNodeList
DDU Level Histogram Definition.
d
Definition: ztail.py:151
void bookDDUHistos(const HwId dduId) const
Book DDU histograms.
static const char XML_BOOK_ONDEMAND_TRUE[]
xercesc::XercesDOMParser XercesDOMParser
std::map< std::string, CoHistoProps > CoHisto
static const int DEF_HISTO_COLOR
static const char XML_BOOK_DEFINITION[]
FED Level Histogram Definition.
void bookFEDHistos(const HwId fedId) const
Book FED histograms.
xercesc::DOMNamedNodeMap DOMNamedNodeMap
std::string HistoName
static const TPRegexp REGEXP_ONDEMAND("^.*%d.*$")
Takes care of errors and warnings while parsing XML files file in XML format.
static const char XML_BOOK_HISTO_NAME[]
static const char XML_BOOK_HISTO_TITLE[]
void bookCSCHistos(const HwId crateId, const HwId dmbId) const
Book Chamber Histograms.
static const int ParseAxisLabels(const std::string &s, std::map< int, std::string > &labels)
Parse Axis label string and return values in vector.
static constexpr float d1
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
tmp
align.sh
Definition: createJobs.py:716
std::map< std::string, std::string > CoHistoProps
static int tokenize(const std::string &str, std::vector< std::string > &tokens, const std::string &delimiters=" ")
Break string into tokens.
xercesc::XMLException XMLException