CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_4_5_patch3/src/DQM/CSCMonitorModule/src/CSCDQM_Collection.cc

Go to the documentation of this file.
00001 /*  =====================================================================================
00002  *
00003  *       Filename:  CSCDQM_Collection.cc
00004  *
00005  *    Description:  Histogram booking code
00006  *
00007  *        Version:  1.0
00008  *        Created:  04/18/2008 03:39:49 PM
00009  *       Revision:  none
00010  *       Compiler:  gcc
00011  *
00012  *         Author:  Valdas Rapsevicius (VR), Valdas.Rapsevicius@cern.ch
00013  *        Company:  CERN, CH
00014  *
00015  *  =====================================================================================
00016  */
00017 
00018 #include "DQM/CSCMonitorModule/interface/CSCDQM_Collection.h"
00019 #include <cstdio>
00020 
00021 namespace cscdqm {
00022 
00027   Collection::Collection(Configuration* const p_config) {
00028     config = p_config;
00029   }
00030 
00031   
00036   void Collection::load() {
00037 
00038     LOG_INFO << "Reading histograms from " << config->getBOOKING_XML_FILE();
00039 
00040     if (config->getBOOKING_XML_FILE().empty()) {
00041       return;
00042     }
00043 
00044     try {
00045 
00046       XMLPlatformUtils::Initialize();
00047 
00048       {
00049 
00050         XercesDOMParser parser;
00051 
00052         parser.setValidationScheme(XercesDOMParser::Val_Always);
00053         parser.setDoNamespaces(true);
00054         parser.setDoSchema(true);
00055         parser.setExitOnFirstFatalError(true);
00056         parser.setValidationConstraintFatal(true);
00057         XMLFileErrorHandler eh;
00058         parser.setErrorHandler(&eh);
00059 
00060         parser.parse(config->getBOOKING_XML_FILE().c_str());
00061         DOMDocument *doc = parser.getDocument();
00062         DOMNode *docNode = (DOMNode*) doc->getDocumentElement();
00063   
00064         DOMNodeList *itemList = docNode->getChildNodes();
00065 
00066         CoHisto definitions;
00067         for (uint32_t i = 0; i < itemList->getLength(); i++) {
00068   
00069           DOMNode* node = itemList->item(i);
00070           if (node->getNodeType() != DOMNode::ELEMENT_NODE) { continue; }
00071 
00072           std::string nodeName = XMLString::transcode(node->getNodeName());
00073 
00077           if (nodeName.compare(XML_BOOK_DEFINITION) == 0) {
00078 
00079             CoHistoProps dp;
00080             getNodeProperties(node, dp);
00081 
00082             DOMElement* el = dynamic_cast<DOMElement*>(node);
00083             std::string id(XMLString::transcode(el->getAttribute(XMLString::transcode(XML_BOOK_DEFINITION_ID))));
00084             definitions.insert(make_pair(id, dp));
00085 
00086           } else
00087 
00091           if (nodeName.compare(XML_BOOK_HISTOGRAM) == 0) {
00092   
00093             CoHistoProps hp;
00094 
00095             DOMElement* el = dynamic_cast<DOMElement*>(node);
00096             if (el->hasAttribute(XMLString::transcode(XML_BOOK_DEFINITION_REF))) {
00097               std::string id(XMLString::transcode(el->getAttribute(XMLString::transcode(XML_BOOK_DEFINITION_REF))));
00098               CoHistoProps d = definitions[id];
00099               for (CoHistoProps::iterator it = d.begin(); it != d.end(); it++) {
00100                 hp[it->first] = it->second;
00101               }
00102             }
00103 
00104             getNodeProperties(node, hp);
00105 
00106             std::string name   = hp[XML_BOOK_HISTO_NAME];
00107             std::string prefix = hp[XML_BOOK_HISTO_PREFIX];
00108 
00109             // Check if this histogram is an ON DEMAND histogram?
00110             hp[XML_BOOK_ONDEMAND] = (Utility::regexMatch(REGEXP_ONDEMAND, name) ? XML_BOOK_ONDEMAND_TRUE : XML_BOOK_ONDEMAND_FALSE );
00111 
00112             LOG_DEBUG << "[Collection::load] loading " << prefix << "::" << name << " XML_BOOK_ONDEMAND = " << hp[XML_BOOK_ONDEMAND]; 
00113   
00114             CoHistoMap::iterator it = collection.find(prefix);
00115             if (it == collection.end()) {
00116               CoHisto h;
00117               h[name] = hp;
00118               collection[prefix] = h; 
00119             } else {
00120               it->second.insert(make_pair(name, hp));
00121             }
00122 
00123           }
00124         }
00125 
00126       }
00127 
00128       XMLPlatformUtils::Terminate();
00129 
00130     } catch (XMLException& e) {
00131       char* message = XMLString::transcode(e.getMessage());
00132       throw Exception(message);
00133     }
00134 
00135     for (CoHistoMap::const_iterator i = collection.begin(); i != collection.end(); i++) {
00136       LOG_INFO << i->second.size() << " " << i->first << " histograms defined";
00137     }
00138     
00139   }
00140   
00148   void Collection::getNodeProperties(DOMNode*& node, CoHistoProps& p) {
00149     DOMNodeList *props  = node->getChildNodes();
00150     for(uint32_t j = 0; j < props->getLength(); j++) {
00151       DOMNode* node = props->item(j);
00152       if (node->getNodeType() != DOMNode::ELEMENT_NODE) { continue; }
00153       std::string name  = XMLString::transcode(node->getNodeName());
00154       std::string value = XMLString::transcode(node->getTextContent());
00155       DOMNamedNodeMap* attributes = node->getAttributes();
00156       if (attributes) {
00157         for (uint32_t i = 0; i < attributes->getLength(); i++) {
00158           DOMNode* attribute = attributes->item(i);
00159           std::string aname  = XMLString::transcode(attribute->getNodeName());
00160           std::string avalue = XMLString::transcode(attribute->getNodeValue());
00161           p[name + "_" + aname] = avalue;
00162         }
00163       }
00164       p[name] = value;
00165     }
00166   }
00167 
00175   const bool Collection::checkHistoValue(const CoHistoProps& h, const std::string& name, std::string& value) {
00176     CoHistoProps::const_iterator i = h.find(name);
00177     if(i == h.end()) {
00178       return false;
00179     }
00180     value = i->second;
00181     return true;
00182   }
00183   
00191   const bool Collection::checkHistoValue(const CoHistoProps& h, const std::string& name, int& value) {
00192     CoHistoProps::const_iterator i = h.find(name);
00193     if(i == h.end()) {
00194       return false;
00195     } 
00196     if(EOF == std::sscanf(i->second.c_str(), "%d", &value)) {
00197       return false;
00198     }
00199     return true;
00200   }
00201   
00210   const bool Collection::checkHistoValue(const CoHistoProps& h, const std::string name, double& value) {
00211     CoHistoProps::const_iterator i = h.find(name);
00212     if(i == h.end()) {
00213       return false;
00214     }
00215     if(EOF == std::sscanf(i->second.c_str(), "%lf", &value)) {
00216       return false;
00217     }
00218     return true;
00219   }
00220   
00229   std::string& Collection::getHistoValue(const CoHistoProps& h, const std::string& name, std::string& value, const std::string& def_value) {
00230     if (!checkHistoValue(h, name, value)) {
00231       value = def_value;
00232     }
00233     return value;
00234   }
00235   
00244   int& Collection::getHistoValue(const CoHistoProps& h, const std::string& name, int& value, const int& def_value) {
00245     if (!checkHistoValue(h, name, value)) {
00246       value = def_value;
00247     }
00248     return value;
00249   }
00250   
00259   double& Collection::getHistoValue(const CoHistoProps& h, const std::string name, double& value, const int def_value) {
00260     if (!checkHistoValue(h, name, value)) {
00261       value = def_value;
00262     }
00263     return value;
00264   }
00265   
00272   const int Collection::ParseAxisLabels(const std::string& s, std::map<int, std::string>& labels) {
00273     std::string tmp = s;
00274     std::string::size_type pos = tmp.find("|");
00275     char* stopstring = NULL;
00276   
00277     while (pos != std::string::npos) {
00278       std::string label_pair = tmp.substr(0, pos);
00279       tmp.replace(0, pos + 1, "");
00280       if (label_pair.find("=") != std::string::npos) {
00281         int nbin = strtol(label_pair.substr(0, label_pair.find("=")).c_str(), &stopstring, 10);
00282         std::string label = label_pair.substr(label_pair.find("=") + 1, label_pair.length());
00283         while (label.find("\'") != std::string::npos) {
00284           label.erase(label.find("\'"), 1);
00285         }
00286         labels[nbin] = label;
00287       }
00288       pos = tmp.find("|");
00289     }
00290     return labels.size();
00291   }
00292   
00297   void Collection::bookEMUHistos() const {
00298     CoHistoMap::const_iterator i = collection.find("EMU");
00299     if (i != collection.end()) {
00300       const CoHisto hs = i->second;
00301       for (CoHisto::const_iterator j = hs.begin(); j != hs.end(); j++) {
00302         std::string s = "";
00303         if (getHistoValue(j->second, XML_BOOK_ONDEMAND, s, XML_BOOK_ONDEMAND_FALSE) == XML_BOOK_ONDEMAND_FALSE) {
00304           HistoId hid = 0;
00305           if (HistoDef::getHistoIdByName(j->first, hid)) {
00306             EMUHistoDef hdef(hid);
00307             book(hdef, j->second, config->getFOLDER_EMU());
00308           }
00309         }
00310       }
00311     }
00312   }
00313 
00319   void Collection::bookDDUHistos(const HwId dduId) const {
00320     CoHistoMap::const_iterator i = collection.find("DDU");
00321     if (i != collection.end()) {
00322       const CoHisto hs = i->second;
00323       for (CoHisto::const_iterator j = hs.begin(); j != hs.end(); j++) {
00324         std::string s = "";
00325         if (getHistoValue(j->second, XML_BOOK_ONDEMAND, s, XML_BOOK_ONDEMAND_FALSE) == XML_BOOK_ONDEMAND_FALSE) {
00326           HistoId hid = 0;
00327           if (HistoDef::getHistoIdByName(j->first, hid)) {
00328             DDUHistoDef hdef(hid, dduId);
00329             book(hdef, j->second, config->getFOLDER_DDU());
00330           }
00331         }
00332       }
00333     }
00334   }
00335 
00342   void Collection::bookCSCHistos(const HwId crateId, const HwId dmbId) const {
00343     CoHistoMap::const_iterator i = collection.find("CSC");
00344     if (i != collection.end()) {
00345       const CoHisto hs = i->second;
00346       for (CoHisto::const_iterator j = hs.begin(); j != hs.end(); j++) {
00347         std::string s = "";
00348         HistoId hid = 0;
00349         if (HistoDef::getHistoIdByName(j->first, hid)) {
00350           if (getHistoValue(j->second, XML_BOOK_ONDEMAND, s, XML_BOOK_ONDEMAND_FALSE) == XML_BOOK_ONDEMAND_FALSE) {
00351             CSCHistoDef hdef(hid, crateId, dmbId);
00352             book(hdef, j->second, config->getFOLDER_CSC());
00353           } else {
00354             int from = 0, to = 0;
00355             if (checkHistoValue(j->second, XML_BOOK_NAME_FROM, from) && checkHistoValue(j->second, XML_BOOK_NAME_TO, to)) {
00356               for (int k = from; k <= to; k++) {
00357                 CSCHistoDef hdef(hid, crateId, dmbId, k);
00358                 book(hdef, j->second, config->getFOLDER_CSC());
00359               }
00360             }
00361           }
00362         }
00363       }
00364     }
00365   }
00366 
00375   void Collection::bookCSCHistos(const HistoId hid, const HwId crateId, const HwId dmbId, const HwId addId) const {
00376     CoHistoMap::const_iterator i = collection.find("CSC");
00377     if (i != collection.end()) {
00378       CoHisto::const_iterator j = i->second.find(h::names[hid]);
00379       if (j != i->second.end()) {
00380         CSCHistoDef hdef(hid, crateId, dmbId, addId);
00381         book(hdef, j->second, config->getFOLDER_CSC());
00382       }
00383     }
00384   }
00385 
00393   void Collection::book(const HistoDef& h, const CoHistoProps& p, const std::string& folder) const {
00394 
00395     MonitorObject* me = NULL;
00396     std::string name = h.getName(), type, title, s;
00397 
00399     if (!config->needBookMO(h.getFullPath())) {
00400       LOG_INFO << "MOFilter excluded " << name << " from booking"; 
00401       config->fnPutHisto(h, me);
00402       return;
00403     }
00404 
00405     int i1, i2, i3;
00406     double d1, d2, d3, d4, d5, d6;
00407     bool ondemand = (getHistoValue(p, XML_BOOK_ONDEMAND, s, XML_BOOK_ONDEMAND_FALSE) == XML_BOOK_ONDEMAND_TRUE ? true : false);
00408       
00409     if (!checkHistoValue(p, XML_BOOK_HISTO_TYPE, type))   { throw Exception("Histogram does not have type!"); }
00410     checkHistoValue(p, XML_BOOK_HISTO_TITLE, title);
00411 
00412     if (ondemand) {
00413       title = h.processTitle(title);
00414     }
00415 
00416     if (type == "h1") {
00417       me = config->fnBook(
00418         HistoBookRequest(h, H1D, type, folder, title,
00419           getHistoValue(p, "XBins", i1, 1),
00420           getHistoValue(p, "XMin",  d1, 0),
00421           getHistoValue(p, "XMax",  d2, 1)));
00422     } else
00423     if(type == "h2") {
00424       me = config->fnBook(
00425         HistoBookRequest(h, H2D, type, folder, title,
00426           getHistoValue(p, "XBins", i1, 1),
00427           getHistoValue(p, "XMin",  d1, 0),
00428           getHistoValue(p, "XMax",  d2, 1),
00429           getHistoValue(p, "YBins", i2, 1),
00430           getHistoValue(p, "YMin",  d3, 0),
00431           getHistoValue(p, "YMax",  d4, 1)));
00432     } else
00433     if(type == "h3") {
00434       me = config->fnBook(
00435         HistoBookRequest(h, H3D, type, folder, title,
00436           getHistoValue(p, "XBins", i1, 1),
00437           getHistoValue(p, "XMin",  d1, 0),
00438           getHistoValue(p, "XMax",  d2, 1),
00439           getHistoValue(p, "YBins", i2, 1),
00440           getHistoValue(p, "YMin",  d3, 0),
00441           getHistoValue(p, "YMax",  d4, 1),
00442           getHistoValue(p, "ZBins", i3, 1),
00443           getHistoValue(p, "ZMin",  d5, 0),
00444           getHistoValue(p, "ZMax",  d6, 1)));
00445     } else
00446     if(type == "hp") {
00447       me = config->fnBook(
00448         HistoBookRequest(h, PROFILE, type, folder, title,
00449           getHistoValue(p, "XBins", i1, 1),
00450           getHistoValue(p, "XMin",  d1, 0),
00451           getHistoValue(p, "XMax",  d2, 1),
00452           getHistoValue(p, "YBins", i2, 1),
00453           getHistoValue(p, "YMin",  d3, 0),
00454           getHistoValue(p, "YMax",  d4, 1)));
00455     } else
00456     if(type == "hp2") {
00457       me = config->fnBook(
00458         HistoBookRequest(h, PROFILE2D, type, folder, title,
00459           getHistoValue(p, "XBins", i1, 1),
00460           getHistoValue(p, "XMin",  d1, 0),
00461           getHistoValue(p, "XMax",  d2, 1),
00462           getHistoValue(p, "YBins", i2, 1),
00463           getHistoValue(p, "YMin",  d3, 0),
00464           getHistoValue(p, "YMax",  d4, 1),
00465           getHistoValue(p, "ZBins", i3, 1),
00466           getHistoValue(p, "ZMin",  d5, 0),
00467           getHistoValue(p, "ZMax",  d6, 1)));
00468     } else { 
00469       throw Exception("Can not book histogram with type: " + type);
00470     }
00471 
00472     if(me != NULL) {
00473 
00474       LockType lock(me->mutex);
00475       TH1 *th = me->getTH1Lock();
00476 
00477       if(checkHistoValue(p, "XTitle", s)) {
00478         if (ondemand) {
00479           s = h.processTitle(s);
00480         }
00481         me->setAxisTitle(s, 1);
00482       }
00483 
00484       if(checkHistoValue(p, "YTitle", s)) {
00485         if (ondemand) {
00486           s = h.processTitle(s);
00487         }
00488         me->setAxisTitle(s, 2);
00489       }
00490 
00491       if(checkHistoValue(p, "ZTitle", s)) {
00492         if (ondemand) {
00493           s = h.processTitle(s);
00494         }
00495           me->setAxisTitle(s, 3);
00496         }
00497 
00498       if(checkHistoValue(p, "SetOption", s)) th->SetOption(s.c_str());
00499       if(checkHistoValue(p, "SetStats", i1)) th->SetStats(i1);
00500       th->SetFillColor(getHistoValue(p, "SetFillColor", i1, DEF_HISTO_COLOR));
00501       if(checkHistoValue(p, "SetXLabels", s)) {
00502         std::map<int, std::string> labels;
00503         ParseAxisLabels(s, labels);
00504         for (std::map<int, std::string>::iterator l_itr = labels.begin(); l_itr != labels.end(); ++l_itr) {
00505           th->GetXaxis()->SetBinLabel(l_itr->first, l_itr->second.c_str());
00506         }
00507       }
00508       if(checkHistoValue(p, "SetYLabels", s)) {
00509         std::map<int, std::string> labels;
00510         ParseAxisLabels(s, labels);
00511         for (std::map<int, std::string>::iterator l_itr = labels.begin(); l_itr != labels.end(); ++l_itr) {
00512           th->GetYaxis()->SetBinLabel(l_itr->first, l_itr->second.c_str());
00513         }
00514       }
00515       if(checkHistoValue(p, "LabelOption", s)) {
00516         std::vector<std::string> v;
00517         if(2 == Utility::tokenize(s, v, ",")) {
00518           th->LabelsOption(v[0].c_str(), v[1].c_str());
00519         }
00520       }
00521       if(checkHistoValue(p, "SetLabelSize", s)) {
00522         std::vector<std::string> v;
00523         if(2 == Utility::tokenize(s, v, ",")) {
00524           th->SetLabelSize((double) atof(v[0].c_str()), v[1].c_str());
00525         }
00526       }
00527       if(checkHistoValue(p, "SetTitleOffset", s)) {
00528         std::vector<std::string> v;
00529         if(2 == Utility::tokenize(s, v, ",")) {
00530           th->SetTitleOffset((double) atof(v[0].c_str()), v[1].c_str());
00531         }
00532       }
00533       if(checkHistoValue(p, "SetMinimum", d1)) th->SetMinimum(d1);
00534       if(checkHistoValue(p, "SetMaximum", d1)) me->SetMaximum(d1);
00535       if(checkHistoValue(p, "SetNdivisionsX", i1)) {
00536         th->SetNdivisions(i1, "X");
00537         th->GetXaxis()->CenterLabels(true);
00538       }
00539       if(checkHistoValue(p, "SetNdivisionsY", i1)) {
00540         th->SetNdivisions(i1, "Y");
00541         th->GetYaxis()->CenterLabels(true);
00542       }
00543       if(checkHistoValue(p, "SetTickLengthX", d1)) th->SetTickLength(d1, "X");
00544       if(checkHistoValue(p, "SetTickLengthY", d1)) th->SetTickLength(d1, "Y");
00545       if(checkHistoValue(p, "SetLabelSizeX", d1)) th->SetLabelSize(d1, "X");
00546       if(checkHistoValue(p, "SetLabelSizeY", d1)) th->SetLabelSize(d1, "Y");
00547       if(checkHistoValue(p, "SetLabelSizeZ", d1)) th->SetLabelSize(d1, "Z");
00548       if(checkHistoValue(p, "SetErrorOption", s)) reinterpret_cast<TProfile*>(th)->SetErrorOption(s.c_str());
00549 
00550       lock.unlock();
00551 
00552     }
00553 
00554     LOG_DEBUG << "[Collection::book] booked " << h.getFullPath() << " (" << me << ")"; 
00555 
00557     config->fnPutHisto(h, me);
00558 
00559   }
00560 
00566   const bool Collection::isOnDemand(const HistoName& name) const {
00567     CoHistoMap::const_iterator i = collection.find("CSC");
00568     if (i != collection.end()) {
00569       CoHisto hs  = i->second;
00570       CoHisto::const_iterator j = hs.find(name);
00571       if (j != hs.end()) { 
00572         std::string s;
00573         return (getHistoValue(j->second, XML_BOOK_ONDEMAND, s, XML_BOOK_ONDEMAND_FALSE) == XML_BOOK_ONDEMAND_TRUE);
00574       }
00575     }
00576     return false;
00577   }
00578 
00583   void Collection::printCollection() const{
00584 
00585     std::ostringstream buffer;
00586     for(CoHistoMap::const_iterator hdmi = collection.begin(); hdmi != collection.end(); hdmi++) {
00587       buffer << hdmi->first << " [" << std::endl;
00588       for(CoHisto::const_iterator hdi = hdmi->second.begin(); hdi != hdmi->second.end(); hdi++) {
00589         buffer << "   " << hdi->first << " [" << std::endl;
00590         for(CoHistoProps::const_iterator hi = hdi->second.begin(); hi != hdi->second.end(); hi++) {
00591           buffer << "     " << hi->first << " = " << hi->second << std::endl;
00592         }
00593         buffer << "   ]" << std::endl;
00594       }
00595       buffer << " ]" << std::endl;
00596     }
00597     LOG_INFO << buffer.str();
00598   }
00599 
00600 }