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 
32  config = p_config;
33  }
34 
35 
41  LOG_INFO << "Reading histograms from " << config->getBOOKING_XML_FILE();
42 
43  if (config->getBOOKING_XML_FILE().empty()) {
44  return;
45  }
46 
47  try {
48 
50  {
52 
53  parser.setValidationScheme(XercesDOMParser::Val_Always);
54  parser.setDoNamespaces(true);
55  parser.setDoSchema(true);
56  parser.setExitOnFirstFatalError(true);
57  parser.setValidationConstraintFatal(true);
59  parser.setErrorHandler(&eh);
60  parser.parse(config->getBOOKING_XML_FILE().c_str());
61 
62  DOMDocument *doc = parser.getDocument();
63  DOMElement *docNode = doc->getDocumentElement();
64  DOMNodeList *itemList = docNode->getChildNodes();
65 
67  for (XMLSize_t i = 0; i < itemList->getLength(); i++) {
68 
69  DOMNode* node = itemList->item(i);
70  if (node->getNodeType() != DOMNode::ELEMENT_NODE) { continue; }
71 
72  std::string nodeName = XMLString::transcode(node->getNodeName());
73 
77  if (nodeName == XML_BOOK_DEFINITION) {
78 
80  getNodeProperties(node, dp);
81 
82  DOMElement* el = dynamic_cast<DOMElement*>(node);
83  std::string id(XMLString::transcode(el->getAttribute(XMLString::transcode(XML_BOOK_DEFINITION_ID))));
84  definitions.insert(make_pair(id, dp));
85 
86  } else
87 
91  if (nodeName == XML_BOOK_HISTOGRAM) {
92 
94 
95  DOMElement* el = dynamic_cast<DOMElement*>(node);
96  if (el->hasAttribute(XMLString::transcode(XML_BOOK_DEFINITION_REF))) {
97  std::string id(XMLString::transcode(el->getAttribute(XMLString::transcode(XML_BOOK_DEFINITION_REF))));
98 
99  CoHistoProps d = definitions[id];
100  for (CoHistoProps::iterator it = d.begin(); it != d.end(); it++) {
101  hp[it->first] = it->second;
102  }
103  }
104 
105  getNodeProperties(node, hp);
106 
109 
110  // Check if this histogram is an ON DEMAND histogram?
112 
113  LOG_DEBUG << "[Collection::load] loading " << prefix << "::" << name << " XML_BOOK_ONDEMAND = " << hp[XML_BOOK_ONDEMAND];
114 
115  CoHistoMap::iterator it = collection.find(prefix);
116  if (it == collection.end()) {
117  CoHisto h;
118  h[name] = hp;
119  collection[prefix] = h;
120  } else {
121  it->second.insert(make_pair(name, hp));
122  }
123  }
124  }
125  }
126 
128 
129  } catch (XMLException& e) {
130  char* message = XMLString::transcode(e.getMessage());
131  throw Exception(message);
132  }
133 
134  for (CoHistoMap::const_iterator i = collection.begin(); i != collection.end(); i++) {
135  LOG_INFO << i->second.size() << " " << i->first << " histograms defined";
136  }
137  }
138 
148  DOMNodeList *props = node->getChildNodes();
149 
150  for(XMLSize_t j = 0; j < props->getLength(); j++) {
151  DOMNode* node = props->item(j);
152  if (node->getNodeType() != DOMNode::ELEMENT_NODE) { continue; }
153  DOMElement* element = dynamic_cast<DOMElement*>(node);
154  std::string name = XMLString::transcode(element->getNodeName());
155 
156  const XMLCh *content = element->getTextContent();
157  XERCES_CPP_NAMESPACE_QUALIFIER TranscodeToStr tc(content, "UTF-8");
158  std::istringstream buffer((const char*)tc.str());
160  buffer >> value;
161 
162  DOMNamedNodeMap* attributes = node->getAttributes();
163  if (attributes) {
164  for (XMLSize_t i = 0; i < attributes->getLength(); i++) {
165  DOMNode* attribute = attributes->item(i);
166  std::string aname = XMLString::transcode(attribute->getNodeName());
167  std::string avalue = XMLString::transcode(attribute->getNodeValue());
168  p[name + "_" + aname] = avalue;
169  }
170  }
171  p[name] = value;
172  }
173  }
174 
183  CoHistoProps::const_iterator i = h.find(name);
184  if(i == h.end()) {
185  return false;
186  }
187  value = i->second;
188  return true;
189  }
190 
198  const bool Collection::checkHistoValue(const CoHistoProps& h, const std::string& name, int& value) {
199  CoHistoProps::const_iterator i = h.find(name);
200  if(i == h.end()) {
201  return false;
202  }
203  if(EOF == std::sscanf(i->second.c_str(), "%d", &value)) {
204  return false;
205  }
206  return true;
207  }
208 
217  const bool Collection::checkHistoValue(const CoHistoProps& h, const std::string name, double& value) {
218  CoHistoProps::const_iterator i = h.find(name);
219  if(i == h.end()) {
220  return false;
221  }
222  if(EOF == std::sscanf(i->second.c_str(), "%lf", &value)) {
223  return false;
224  }
225  return true;
226  }
227 
237  if (!checkHistoValue(h, name, value)) {
238  value = def_value;
239  }
240  return value;
241  }
242 
251  int& Collection::getHistoValue(const CoHistoProps& h, const std::string& name, int& value, const int& def_value) {
252  if (!checkHistoValue(h, name, value)) {
253  value = def_value;
254  }
255  return value;
256  }
257 
266  double& Collection::getHistoValue(const CoHistoProps& h, const std::string name, double& value, const int def_value) {
267  if (!checkHistoValue(h, name, value)) {
268  value = def_value;
269  }
270  return value;
271  }
272 
279  const int Collection::ParseAxisLabels(const std::string& s, std::map<int, std::string>& labels) {
280  std::string tmp = s;
281  std::string::size_type pos = tmp.find("|");
282  char* stopstring = NULL;
283 
284  while (pos != std::string::npos) {
285  std::string label_pair = tmp.substr(0, pos);
286  tmp.replace(0, pos + 1, "");
287  if (label_pair.find("=") != std::string::npos) {
288  int nbin = strtol(label_pair.substr(0, label_pair.find("=")).c_str(), &stopstring, 10);
289  std::string label = label_pair.substr(label_pair.find("=") + 1, label_pair.length());
290  while (label.find("\'") != std::string::npos) {
291  label.erase(label.find("\'"), 1);
292  }
293  labels[nbin] = label;
294  }
295  pos = tmp.find("|");
296  }
297  return labels.size();
298  }
299 
305  CoHistoMap::const_iterator i = collection.find("EMU");
306  if (i != collection.end()) {
307  const CoHisto hs = i->second;
308  for (CoHisto::const_iterator j = hs.begin(); j != hs.end(); j++) {
309  std::string s = "";
311  HistoId hid = 0;
312  if (HistoDef::getHistoIdByName(j->first, hid)) {
313  EMUHistoDef hdef(hid);
314  book(hdef, j->second, config->getFOLDER_EMU());
315  }
316  }
317  }
318  }
319  }
320 
327  CoHistoMap::const_iterator i = collection.find("FED");
328  if (i != collection.end()) {
329  const CoHisto hs = i->second;
330  for (CoHisto::const_iterator j = hs.begin(); j != hs.end(); j++) {
331  std::string s = "";
333  HistoId hid = 0;
334  if (HistoDef::getHistoIdByName(j->first, hid)) {
335  FEDHistoDef hdef(hid, fedId);
336  book(hdef, j->second, config->getFOLDER_FED());
337  }
338  }
339  }
340  }
341  }
342 
348  void Collection::bookDDUHistos(const HwId dduId) const {
349  CoHistoMap::const_iterator i = collection.find("DDU");
350  if (i != collection.end()) {
351  const CoHisto hs = i->second;
352  for (CoHisto::const_iterator j = hs.begin(); j != hs.end(); j++) {
353  std::string s = "";
355  HistoId hid = 0;
356  if (HistoDef::getHistoIdByName(j->first, hid)) {
357  DDUHistoDef hdef(hid, dduId);
358  book(hdef, j->second, config->getFOLDER_DDU());
359  }
360  }
361  }
362  }
363  }
364 
371  void Collection::bookCSCHistos(const HwId crateId, const HwId dmbId) const {
372  CoHistoMap::const_iterator i = collection.find("CSC");
373  if (i != collection.end()) {
374  const CoHisto hs = i->second;
375  for (CoHisto::const_iterator j = hs.begin(); j != hs.end(); j++) {
376  std::string s = "";
377  HistoId hid = 0;
378  if (HistoDef::getHistoIdByName(j->first, hid)) {
380  CSCHistoDef hdef(hid, crateId, dmbId);
381  book(hdef, j->second, config->getFOLDER_CSC());
382  } else {
383  int from = 0, to = 0;
384  if (checkHistoValue(j->second, XML_BOOK_NAME_FROM, from) && checkHistoValue(j->second, XML_BOOK_NAME_TO, to)) {
385  for (int k = from; k <= to; k++) {
386  CSCHistoDef hdef(hid, crateId, dmbId, k);
387  book(hdef, j->second, config->getFOLDER_CSC());
388  }
389  }
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 
426  MonitorObject* me = NULL;
427  std::string name = h.getName(), type, title, s;
428 
430  if (!config->needBookMO(h.getFullPath())) {
431  LOG_INFO << "MOFilter excluded " << name << " from booking";
432  config->fnPutHisto(h, me);
433  return;
434  }
435 
436  int i1, i2, i3;
437  double d1, d2, d3, d4, d5, d6;
438  bool ondemand = (getHistoValue(p, XML_BOOK_ONDEMAND, s, XML_BOOK_ONDEMAND_FALSE) == XML_BOOK_ONDEMAND_TRUE ? true : false);
439 
440  if (!checkHistoValue(p, XML_BOOK_HISTO_TYPE, type)) { throw Exception("Histogram does not have type!"); }
442 
443  if (ondemand) {
444  title = h.processTitle(title);
445  }
446 
447  if (type == "h1") {
448  me = config->fnBook(
449  HistoBookRequest(h, H1D, type, folder, title,
450  getHistoValue(p, "XBins", i1, 1),
451  getHistoValue(p, "XMin", d1, 0),
452  getHistoValue(p, "XMax", d2, 1)));
453  } else
454  if(type == "h2") {
455  me = config->fnBook(
456  HistoBookRequest(h, H2D, type, folder, title,
457  getHistoValue(p, "XBins", i1, 1),
458  getHistoValue(p, "XMin", d1, 0),
459  getHistoValue(p, "XMax", d2, 1),
460  getHistoValue(p, "YBins", i2, 1),
461  getHistoValue(p, "YMin", d3, 0),
462  getHistoValue(p, "YMax", d4, 1)));
463  } else
464  if(type == "h3") {
465  me = config->fnBook(
466  HistoBookRequest(h, H3D, type, folder, title,
467  getHistoValue(p, "XBins", i1, 1),
468  getHistoValue(p, "XMin", d1, 0),
469  getHistoValue(p, "XMax", d2, 1),
470  getHistoValue(p, "YBins", i2, 1),
471  getHistoValue(p, "YMin", d3, 0),
472  getHistoValue(p, "YMax", d4, 1),
473  getHistoValue(p, "ZBins", i3, 1),
474  getHistoValue(p, "ZMin", d5, 0),
475  getHistoValue(p, "ZMax", d6, 1)));
476  } else
477  if(type == "hp") {
478  me = config->fnBook(
479  HistoBookRequest(h, PROFILE, type, folder, title,
480  getHistoValue(p, "XBins", i1, 1),
481  getHistoValue(p, "XMin", d1, 0),
482  getHistoValue(p, "XMax", d2, 1)));
483  /*
484  HistoBookRequest(h, PROFILE, type, folder, title,
485  getHistoValue(p, "XBins", i1, 1),
486  getHistoValue(p, "XMin", d1, 0),
487  getHistoValue(p, "XMax", d2, 1),
488  getHistoValue(p, "YBins", i2, 1),
489  getHistoValue(p, "YMin", d3, 0),
490  getHistoValue(p, "YMax", d4, 1)));
491  */
492  } else
493  if(type == "hp2") {
494  me = config->fnBook(
495  HistoBookRequest(h, PROFILE2D, 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  getHistoValue(p, "ZBins", i3, 1),
503  getHistoValue(p, "ZMin", d5, 0),
504  getHistoValue(p, "ZMax", d6, 1)));
505  } else {
506  throw Exception("Can not book histogram with type: " + type);
507  }
508 
509  if(me != NULL) {
510 
511  LockType lock(me->mutex);
512  TH1 *th = me->getTH1Lock();
513 
514  if(checkHistoValue(p, "XTitle", s)) {
515  if (ondemand) {
516  s = h.processTitle(s);
517  }
518  me->setAxisTitle(s, 1);
519  }
520 
521  if(checkHistoValue(p, "YTitle", s)) {
522  if (ondemand) {
523  s = h.processTitle(s);
524  }
525  me->setAxisTitle(s, 2);
526  }
527 
528  if(checkHistoValue(p, "ZTitle", s)) {
529  if (ondemand) {
530  s = h.processTitle(s);
531  }
532  me->setAxisTitle(s, 3);
533  }
534 
535  if(checkHistoValue(p, "SetOption", s)) th->SetOption(s.c_str());
536  if(checkHistoValue(p, "SetStats", i1)) th->SetStats(i1);
537  th->SetFillColor(getHistoValue(p, "SetFillColor", i1, DEF_HISTO_COLOR));
538  if(checkHistoValue(p, "SetXLabels", s)) {
539  std::map<int, std::string> labels;
540  ParseAxisLabels(s, labels);
541  th->GetXaxis()->SetNoAlphanumeric(); // For ROOT6 to prevent getting zero means values
542  for (std::map<int, std::string>::iterator l_itr = labels.begin(); l_itr != labels.end(); ++l_itr) {
543  th->GetXaxis()->SetBinLabel(l_itr->first, l_itr->second.c_str());
544  }
545  }
546  if(checkHistoValue(p, "SetYLabels", s)) {
547  std::map<int, std::string> labels;
548  ParseAxisLabels(s, labels);
549  th->GetYaxis()->SetNoAlphanumeric(); // For ROOT6 to prevent getting zero means values
550  for (std::map<int, std::string>::iterator l_itr = labels.begin(); l_itr != labels.end(); ++l_itr) {
551  th->GetYaxis()->SetBinLabel(l_itr->first, l_itr->second.c_str());
552  }
553  }
554  if(checkHistoValue(p, "LabelOption", s)) {
555  std::vector<std::string> v;
556  if(2 == Utility::tokenize(s, v, ",")) {
557  th->LabelsOption(v[0].c_str(), v[1].c_str());
558  }
559  }
560  if(checkHistoValue(p, "SetLabelSize", s)) {
561  std::vector<std::string> v;
562  if(2 == Utility::tokenize(s, v, ",")) {
563  th->SetLabelSize((double) atof(v[0].c_str()), v[1].c_str());
564  }
565  }
566  if(checkHistoValue(p, "SetTitleOffset", s)) {
567  std::vector<std::string> v;
568  if(2 == Utility::tokenize(s, v, ",")) {
569  th->SetTitleOffset((double) atof(v[0].c_str()), v[1].c_str());
570  }
571  }
572  if(checkHistoValue(p, "SetMinimum", d1)) th->SetMinimum(d1);
573  if(checkHistoValue(p, "SetMaximum", d1)) me->SetMaximum(d1);
574  if(checkHistoValue(p, "SetNdivisionsX", i1)) {
575  th->SetNdivisions(i1, "X");
576  th->GetXaxis()->CenterLabels(true);
577  }
578  if(checkHistoValue(p, "SetNdivisionsY", i1)) {
579  th->SetNdivisions(i1, "Y");
580  th->GetYaxis()->CenterLabels(true);
581  }
582  if(checkHistoValue(p, "SetTickLengthX", d1)) th->SetTickLength(d1, "X");
583  if(checkHistoValue(p, "SetTickLengthY", d1)) th->SetTickLength(d1, "Y");
584  if(checkHistoValue(p, "SetLabelSizeX", d1)) th->SetLabelSize(d1, "X");
585  if(checkHistoValue(p, "SetLabelSizeY", d1)) th->SetLabelSize(d1, "Y");
586  if(checkHistoValue(p, "SetLabelSizeZ", d1)) th->SetLabelSize(d1, "Z");
587  if(checkHistoValue(p, "SetErrorOption", s)) reinterpret_cast<TProfile*>(th)->SetErrorOption(s.c_str());
588 
589  lock.unlock();
590 
591  }
592 
593  LOG_DEBUG << "[Collection::book] booked " << h.getFullPath() << " (" << me << ")";
594 
596  config->fnPutHisto(h, me);
597 
598  }
599 
605  const bool Collection::isOnDemand(const HistoName& name) const {
606  CoHistoMap::const_iterator i = collection.find("CSC");
607  if (i != collection.end()) {
608  CoHisto hs = i->second;
609  CoHisto::const_iterator j = hs.find(name);
610  if (j != hs.end()) {
611  std::string s;
613  }
614  }
615  return false;
616  }
617 
623 
624  std::ostringstream buffer;
625  for(CoHistoMap::const_iterator hdmi = collection.begin(); hdmi != collection.end(); hdmi++) {
626  buffer << hdmi->first << " [" << std::endl;
627  for(CoHisto::const_iterator hdi = hdmi->second.begin(); hdi != hdmi->second.end(); hdi++) {
628  buffer << " " << hdi->first << " [" << std::endl;
629  for(CoHistoProps::const_iterator hi = hdi->second.begin(); hi != hdi->second.end(); hi++) {
630  buffer << " " << hi->first << " = " << hi->second << std::endl;
631  }
632  buffer << " ]" << std::endl;
633  }
634  buffer << " ]" << std::endl;
635  }
636  LOG_INFO << buffer.str();
637  }
638 
639 }
type
Definition: HCALResponse.h:21
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[]
FWCore Framework interface EventSetupRecordImplementation h
Helper function to determine trigger accepts.
virtual const std::string processTitle(const std::string &p_title) const
Process Title by Adding appropriate ID.
#define LOG_INFO
Definition: CSCDQM_Logger.h:43
void bookEMUHistos() const
Book EMU histograms.
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[]
static const char XML_BOOK_DEFINITION_REF[]
static const char XML_BOOK_HISTOGRAM[]
xercesc::DOMDocument DOMDocument
#define NULL
Definition: scimark2.h:8
const bool isOnDemand(const HistoName &name) const
Check if the histogram is on demand (by histogram name)
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 xercesInitialize()
Definition: Xerces.cc:18
CSC Level Histogram Type.
Collection(Configuration *const p_config)
Constructor.
uint16_t size_type
CSCDQM Framework Global Configuration.
unsigned int HistoId
static const char XML_BOOK_HISTO_PREFIX[]
virtual TH1 * getTH1Lock(void)=0
susybsm::HSCParticleRefProd hp
Definition: classes.h:27
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.
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.
static const char XML_BOOK_ONDEMAND_TRUE[]
int k[5][pyjets_maxn]
xercesc::XercesDOMParser XercesDOMParser
void bookFEDHistos(const HwId fedId) const
Book FED histograms.
std::map< std::string, CoHistoProps > CoHisto
auto dp
Definition: deltaR.h:22
static const int DEF_HISTO_COLOR
static const char XML_BOOK_DEFINITION[]
void bookDDUHistos(const HwId dduId) const
Book DDU histograms.
void book(const HistoDef &h, const CoHistoProps &p, const std::string &folder) const
Book histogram.
FED Level Histogram Definition.
xercesc::DOMNamedNodeMap DOMNamedNodeMap
std::string HistoName
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
const std::string getFullPath() const
Get full path of the histogram. It is being constructed by appending path and histogam name...
virtual const std::string getName() const
Get processed histogram name. It can include additional parameter in formated name. This Name is being constructed from raw name and additional parameter.
void bookCSCHistos(const HwId crateId, const HwId dmbId) const
Book Chamber Histograms.
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[]
void printCollection() const
Print collection of available histograms and their parameters.
static const char XML_BOOK_HISTO_TITLE[]
virtual void SetMaximum(const double d)=0
static const int ParseAxisLabels(const std::string &s, std::map< int, std::string > &labels)
Parse Axis label string and return values in vector.
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.
virtual void setAxisTitle(const std::string title, const int axisN)=0
xercesc::XMLException XMLException