CMS 3D CMS Logo

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 
00020 namespace cscdqm {
00021 
00022   Collection::Collection(Configuration* const p_config) {
00023     config = p_config;
00024     load();
00025   }
00026 
00027   
00033   void Collection::load() {
00034 
00035     LOG_INFO << "Reading histograms from " << config->getBOOKING_XML_FILE();
00036 
00037     DOMDocument *doc = 0;
00038 
00039     try {
00040 
00041       XMLPlatformUtils::Initialize();
00042 
00043       boost::shared_ptr<XercesDOMParser> parser(new XercesDOMParser());
00044 
00045       parser->setValidationScheme(XercesDOMParser::Val_Always);
00046       parser->setDoNamespaces(true);
00047       parser->setDoSchema(true);
00048       parser->setExitOnFirstFatalError(true);
00049       parser->setValidationConstraintFatal(true);
00050       XMLFileErrorHandler eh;
00051       parser->setErrorHandler(&eh);
00052 
00053       parser->parse(config->getBOOKING_XML_FILE().c_str());
00054       doc = parser->getDocument();
00055       DOMNode *docNode = (DOMNode*) doc->getDocumentElement();
00056 
00057       DOMNodeList *itemList = docNode->getChildNodes();
00058 
00059       CoHisto definitions;
00060       for(uint32_t i = 0; i < itemList->getLength(); i++) {
00061 
00062         DOMNode* node = itemList->item(i);
00063         if (node->getNodeType() != DOMNode::ELEMENT_NODE) { continue; }
00064 
00065         std::string nodeName = XMLString::transcode(node->getNodeName());
00066 
00070         if (nodeName.compare(XML_BOOK_DEFINITION) == 0) {
00071 
00072           CoHistoProps dp;
00073           getNodeProperties(node, dp);
00074 
00075           DOMElement* el = dynamic_cast<DOMElement*>(node);
00076           std::string id(XMLString::transcode(el->getAttribute(XMLString::transcode(XML_BOOK_DEFINITION_ID))));
00077           definitions.insert(make_pair(id, dp));
00078 
00079         } else
00080 
00084         if (nodeName.compare(XML_BOOK_HISTOGRAM) == 0) {
00085 
00086           CoHistoProps hp;
00087 
00088           DOMElement* el = dynamic_cast<DOMElement*>(node);
00089           if (el->hasAttribute(XMLString::transcode(XML_BOOK_DEFINITION_REF))) {
00090             std::string id(XMLString::transcode(el->getAttribute(XMLString::transcode(XML_BOOK_DEFINITION_REF))));
00091             CoHistoProps d = definitions[id];
00092             for (CoHistoProps::iterator it = d.begin(); it != d.end(); it++) {
00093               hp[it->first] = it->second;
00094             }
00095           }
00096 
00097           getNodeProperties(node, hp);
00098 
00099           std::string name   = hp[XML_BOOK_HISTO_NAME];
00100           std::string prefix = hp[XML_BOOK_HISTO_PREFIX];
00101 
00102           // Check if this histogram is an ON DEMAND histogram?
00103           hp[XML_BOOK_ONDEMAND] = (Utility::regexMatch(REGEXP_ONDEMAND, name) ? XML_BOOK_ONDEMAND_TRUE : XML_BOOK_ONDEMAND_FALSE );
00104 
00105           CoHistoMap::iterator it = collection.find(prefix);
00106           if (it == collection.end()) {
00107             CoHisto h;
00108             h[name] = hp;
00109             collection[prefix] = h; 
00110           } else {
00111             it->second.insert(make_pair(name, hp));
00112           }
00113 
00114         }
00115       }
00116 
00117     } catch (XMLException& e) {
00118       char* message = XMLString::transcode( e.getMessage() );
00119       //if (doc) doc->release();
00120       //XMLPlatformUtils::Terminate();
00121       throw Exception(message);
00122     }
00123 
00124     //if (doc) doc->release();
00125     //XMLPlatformUtils::Terminate();
00126 
00127     for (CoHistoMap::const_iterator i = collection.begin(); i != collection.end(); i++) {
00128       LOG_INFO << i->second.size() << " " << i->first << " histograms defined";
00129     }
00130     
00131   }
00132   
00133   void Collection::getNodeProperties(DOMNode*& node, CoHistoProps& p) {
00134     DOMNodeList *props  = node->getChildNodes();
00135     for(uint32_t j = 0; j < props->getLength(); j++) {
00136       DOMNode* node = props->item(j);
00137       if (node->getNodeType() != DOMNode::ELEMENT_NODE) { continue; }
00138       std::string name  = XMLString::transcode(node->getNodeName());
00139       std::string value = XMLString::transcode(node->getTextContent());
00140       p[name] = value;
00141     }
00142   }
00143 
00151   const bool Collection::checkHistoValue(const CoHistoProps& h, const std::string& name, std::string& value) {
00152     CoHistoProps::const_iterator i = h.find(name);
00153     if(i == h.end()) {
00154       return false;
00155     }
00156     value = i->second;
00157     return true;
00158   }
00159   
00168   const bool Collection::checkHistoValue(const CoHistoProps& h, const std::string& name, int& value) {
00169     CoHistoProps::const_iterator i = h.find(name);
00170     if(i == h.end()) {
00171       return false;
00172     } 
00173     if(EOF == std::sscanf(i->second.c_str(), "%d", &value)) {
00174       return false;
00175     }
00176     return true;
00177   }
00178   
00187   const bool Collection::checkHistoValue(const CoHistoProps& h, const std::string name, double& value) {
00188     CoHistoProps::const_iterator i = h.find(name);
00189     if(i == h.end()) {
00190       return false;
00191     }
00192     if(EOF == std::sscanf(i->second.c_str(), "%lf", &value)) {
00193       return false;
00194     }
00195     return true;
00196   }
00197   
00206   std::string& Collection::getHistoValue(const CoHistoProps& h, const std::string& name, std::string& value, const std::string& def_value) {
00207     if (!checkHistoValue(h, name, value)) {
00208       value = def_value;
00209     }
00210     return value;
00211   }
00212   
00221   int& Collection::getHistoValue(const CoHistoProps& h, const std::string& name, int& value, const int& def_value) {
00222     if (!checkHistoValue(h, name, value)) {
00223       value = def_value;
00224     }
00225     return value;
00226   }
00227   
00236   double& Collection::getHistoValue(const CoHistoProps& h, const std::string name, double& value, const int def_value) {
00237     if (!checkHistoValue(h, name, value)) {
00238       value = def_value;
00239     }
00240     return value;
00241   }
00242   
00249   const int Collection::ParseAxisLabels(const std::string& s, std::map<int, std::string>& labels) {
00250     std::string tmp = s;
00251     std::string::size_type pos = tmp.find("|");
00252     char* stopstring = NULL;
00253   
00254     while (pos != std::string::npos) {
00255       std::string label_pair = tmp.substr(0, pos);
00256       tmp.replace(0, pos + 1, "");
00257       if (label_pair.find("=") != std::string::npos) {
00258         int nbin = strtol(label_pair.substr(0, label_pair.find("=")).c_str(), &stopstring, 10);
00259         std::string label = label_pair.substr(label_pair.find("=") + 1, label_pair.length());
00260         while (label.find("\'") != std::string::npos) {
00261           label.erase(label.find("\'"),1);
00262         }
00263         labels[nbin] = label;
00264       }
00265       pos = tmp.find("|");
00266     }
00267     return labels.size();
00268   }
00269   
00270   void Collection::bookEMUHistos() const {
00271     CoHistoMap::const_iterator i = collection.find("EMU");
00272     if (i != collection.end()) {
00273       const CoHisto hs = i->second;
00274       for (CoHisto::const_iterator j = hs.begin(); j != hs.end(); j++) {
00275         std::string s = "";
00276         if (getHistoValue(j->second, XML_BOOK_ONDEMAND, s, XML_BOOK_ONDEMAND_FALSE) == XML_BOOK_ONDEMAND_FALSE) {
00277           HistoId hid = 0;
00278           if (HistoDef::getHistoIdByName(j->first, hid)) {
00279             book(EMUHistoDef(hid), j->second, config->getFOLDER_EMU());
00280           }
00281         }
00282       }
00283     }
00284   }
00285 
00286   void Collection::bookDDUHistos(const HwId dduId) const {
00287     CoHistoMap::const_iterator i = collection.find("DDU");
00288     if (i != collection.end()) {
00289       const CoHisto hs = i->second;
00290       for (CoHisto::const_iterator j = hs.begin(); j != hs.end(); j++) {
00291         std::string s = "";
00292         if (getHistoValue(j->second, XML_BOOK_ONDEMAND, s, XML_BOOK_ONDEMAND_FALSE) == XML_BOOK_ONDEMAND_FALSE) {
00293           HistoId hid = 0;
00294           if (HistoDef::getHistoIdByName(j->first, hid)) {
00295             book(DDUHistoDef(hid, dduId), j->second, config->getFOLDER_DDU());
00296           }
00297         }
00298       }
00299     }
00300   }
00301 
00302   void Collection::bookCSCHistos(const HwId crateId, const HwId dmbId) const {
00303     CoHistoMap::const_iterator i = collection.find("CSC");
00304     if (i != collection.end()) {
00305       const CoHisto hs = i->second;
00306       for (CoHisto::const_iterator j = hs.begin(); j != hs.end(); j++) {
00307         std::string s = "";
00308         if (getHistoValue(j->second, XML_BOOK_ONDEMAND, s, XML_BOOK_ONDEMAND_FALSE) == XML_BOOK_ONDEMAND_FALSE) {
00309           HistoId hid = 0;
00310           if (HistoDef::getHistoIdByName(j->first, hid)) {
00311             book(CSCHistoDef(hid, crateId, dmbId), j->second, config->getFOLDER_CSC());
00312           }
00313         }
00314       }
00315     }
00316   }
00317 
00318   void Collection::bookCSCHistos(const HistoId hid, const HwId crateId, const HwId dmbId, const HwId addId) const {
00319     CoHistoMap::const_iterator i = collection.find("CSC");
00320     if (i != collection.end()) {
00321       CoHisto::const_iterator j = i->second.find(h::names[hid]);
00322       if (j != i->second.end()) {
00323         book(CSCHistoDef(hid, crateId, dmbId, addId), j->second, config->getFOLDER_CSC());
00324       }
00325     }
00326   }
00327 
00328   void Collection::book(const HistoDef& h, const CoHistoProps& p, const std::string& folder) const {
00329 
00330     MonitorObject* me = NULL;
00331     std::string name = h.getName(), type, title, s;
00332 
00333     if (!config->needBookMO(name)) {
00334       LOG_INFO << "MOFilter excluded " << name << " from bookingfrom booking"; 
00335       config->fnPutHisto(h, me);
00336       return;
00337     }
00338 
00339     int i1, i2, i3;
00340     double d1, d2, d3, d4, d5, d6;
00341     bool ondemand = (getHistoValue(p, XML_BOOK_ONDEMAND, s, XML_BOOK_ONDEMAND_FALSE) == XML_BOOK_ONDEMAND_TRUE ? true : false);
00342       
00343     if (!checkHistoValue(p, XML_BOOK_HISTO_TYPE, type))   { throw Exception("Histogram does not have type!"); }
00344     if (!checkHistoValue(p, XML_BOOK_HISTO_TITLE, title)) { title = name; }
00345 
00346     if (ondemand) {
00347       title = h.processTitle(title);
00348     }
00349 
00350     if (type == "h1") {
00351       me = config->fnBook(
00352         HistoBookRequest(h, H1D, type, folder, title,
00353           getHistoValue(p, "XBins", i1, 1),
00354           getHistoValue(p, "XMin",  d1, 0),
00355           getHistoValue(p, "XMax",  d2, 1)));
00356     } else
00357     if(type == "h2") {
00358       me = config->fnBook(
00359         HistoBookRequest(h, H2D, type, folder, title,
00360           getHistoValue(p, "XBins", i1, 1),
00361           getHistoValue(p, "XMin",  d1, 0),
00362           getHistoValue(p, "XMax",  d2, 1),
00363           getHistoValue(p, "YBins", i2, 1),
00364           getHistoValue(p, "YMin",  d3, 0),
00365           getHistoValue(p, "YMax",  d4, 1)));
00366     } else
00367     if(type == "h3") {
00368       me = config->fnBook(
00369         HistoBookRequest(h, H3D, type, folder, title,
00370           getHistoValue(p, "XBins", i1, 1),
00371           getHistoValue(p, "XMin",  d1, 0),
00372           getHistoValue(p, "XMax",  d2, 1),
00373           getHistoValue(p, "YBins", i2, 1),
00374           getHistoValue(p, "YMin",  d3, 0),
00375           getHistoValue(p, "YMax",  d4, 1),
00376           getHistoValue(p, "ZBins", i3, 1),
00377           getHistoValue(p, "ZMin",  d5, 0),
00378           getHistoValue(p, "ZMax",  d6, 1)));
00379     } else
00380     if(type == "hp") {
00381       me = config->fnBook(
00382         HistoBookRequest(h, PROFILE, type, folder, title,
00383           getHistoValue(p, "XBins", i1, 1),
00384           getHistoValue(p, "XMin",  d1, 0),
00385           getHistoValue(p, "XMax",  d2, 1),
00386           getHistoValue(p, "YBins", i2, 1),
00387           getHistoValue(p, "YMin",  d3, 0),
00388           getHistoValue(p, "YMax",  d4, 1)));
00389     } else
00390     if(type == "hp2") {
00391       me = config->fnBook(
00392         HistoBookRequest(h, PROFILE2D, type, folder, title,
00393           getHistoValue(p, "XBins", i1, 1),
00394           getHistoValue(p, "XMin",  d1, 0),
00395           getHistoValue(p, "XMax",  d2, 1),
00396           getHistoValue(p, "YBins", i2, 1),
00397           getHistoValue(p, "YMin",  d3, 0),
00398           getHistoValue(p, "YMax",  d4, 1),
00399           getHistoValue(p, "ZBins", i3, 1),
00400           getHistoValue(p, "ZMin",  d5, 0),
00401           getHistoValue(p, "ZMax",  d6, 1)));
00402     } else { 
00403       throw Exception("Can not book histogram with type: " + type);
00404     }
00405 
00406     if(me != NULL) {
00407 
00408       TH1 *th = me->getTH1Lock();
00409 
00410       if(checkHistoValue(p, "XTitle", s)) {
00411         if (ondemand) {
00412           s = h.processTitle(s);
00413         }
00414         me->setAxisTitle(s, 1);
00415       }
00416 
00417       if(checkHistoValue(p, "YTitle", s)) {
00418         if (ondemand) {
00419           s = h.processTitle(s);
00420         }
00421         me->setAxisTitle(s, 2);
00422       }
00423 
00424       if(checkHistoValue(p, "ZTitle", s)) {
00425         if (ondemand) {
00426           s = h.processTitle(s);
00427         }
00428           me->setAxisTitle(s, 3);
00429         }
00430 
00431       if(checkHistoValue(p, "SetOption", s)) th->SetOption(s.c_str());
00432       if(checkHistoValue(p, "SetStats", i1)) th->SetStats(i1);
00433       th->SetFillColor(getHistoValue(p, "SetFillColor", i1, DEF_HISTO_COLOR));
00434       if(checkHistoValue(p, "SetXLabels", s)) {
00435         std::map<int, std::string> labels;
00436         ParseAxisLabels(s, labels);
00437         for (std::map<int, std::string>::iterator l_itr = labels.begin(); l_itr != labels.end(); ++l_itr) {
00438           th->GetXaxis()->SetBinLabel(l_itr->first, l_itr->second.c_str());
00439         }
00440       }
00441       if(checkHistoValue(p, "SetYLabels", s)) {
00442         std::map<int, std::string> labels;
00443         ParseAxisLabels(s, labels);
00444         for (std::map<int, std::string>::iterator l_itr = labels.begin(); l_itr != labels.end(); ++l_itr) {
00445           th->GetYaxis()->SetBinLabel(l_itr->first, l_itr->second.c_str());
00446         }
00447       }
00448       if(checkHistoValue(p, "LabelOption", s)) {
00449         std::vector<std::string> v;
00450         if(2 == Utility::tokenize(s, v, ",")) {
00451           th->LabelsOption(v[0].c_str(), v[1].c_str());
00452         }
00453       }
00454       if(checkHistoValue(p, "SetLabelSize", s)) {
00455         std::vector<std::string> v;
00456         if(2 == Utility::tokenize(s, v, ",")) {
00457           th->SetLabelSize((double) atof(v[0].c_str()), v[1].c_str());
00458         }
00459       }
00460       if(checkHistoValue(p, "SetTitleOffset", s)) {
00461         std::vector<std::string> v;
00462         if(2 == Utility::tokenize(s, v, ",")) {
00463           th->SetTitleOffset((double) atof(v[0].c_str()), v[1].c_str());
00464         }
00465       }
00466       if(checkHistoValue(p, "SetMinimum", d1)) th->SetMinimum(d1);
00467       if(checkHistoValue(p, "SetMaximum", d1)) me->SetMaximum(d1);
00468       if(checkHistoValue(p, "SetNdivisionsX", i1)) {
00469         th->SetNdivisions(i1, "X");
00470         th->GetXaxis()->CenterLabels(true);
00471       }
00472       if(checkHistoValue(p, "SetNdivisionsY", i1)) {
00473         th->SetNdivisions(i1, "Y");
00474         th->GetYaxis()->CenterLabels(true);
00475       }
00476       if(checkHistoValue(p, "SetTickLengthX", d1)) th->SetTickLength(d1, "X");
00477       if(checkHistoValue(p, "SetTickLengthY", d1)) th->SetTickLength(d1, "Y");
00478       if(checkHistoValue(p, "SetLabelSizeX", d1)) th->SetLabelSize(d1, "X");
00479       if(checkHistoValue(p, "SetLabelSizeY", d1)) th->SetLabelSize(d1, "Y");
00480       if(checkHistoValue(p, "SetLabelSizeZ", d1)) th->SetLabelSize(d1, "Z");
00481       if(checkHistoValue(p, "SetErrorOption", s)) reinterpret_cast<TProfile*>(th)->SetErrorOption(s.c_str());
00482 
00483       me->unlock();
00484 
00485     }
00486 
00487     config->fnPutHisto(h, me);
00488 
00489   }
00490 
00491   const bool Collection::isOnDemand(const HistoName& name) const {
00492     CoHistoMap::const_iterator i = collection.find("CSC");
00493     if (i != collection.end()) {
00494       CoHisto hs  = i->second;
00495       CoHisto::const_iterator j = hs.find(name);
00496       if (j != hs.end()) { 
00497         std::string s;
00498         return (getHistoValue(j->second, XML_BOOK_ONDEMAND, s, XML_BOOK_ONDEMAND_FALSE) == XML_BOOK_ONDEMAND_TRUE);
00499       }
00500     }
00501     return false;
00502   }
00503 
00509   void Collection::printCollection() const{
00510 
00511     std::ostringstream buffer;
00512     for(CoHistoMap::const_iterator hdmi = collection.begin(); hdmi != collection.end(); hdmi++) {
00513       buffer << hdmi->first << " [" << std::endl;
00514       for(CoHisto::const_iterator hdi = hdmi->second.begin(); hdi != hdmi->second.end(); hdi++) {
00515         buffer << "   " << hdi->first << " [" << std::endl;
00516         for(CoHistoProps::const_iterator hi = hdi->second.begin(); hi != hdi->second.end(); hi++) {
00517           buffer << "     " << hi->first << " = " << hi->second << std::endl;
00518         }
00519         buffer << "   ]" << std::endl;
00520       }
00521       buffer << " ]" << std::endl;
00522     }
00523     LOG_INFO << buffer.str();
00524   }
00525 
00526 }

Generated on Tue Jun 9 17:32:33 2009 for CMSSW by  doxygen 1.5.4