HistoHandler.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 #include "Rivet/Rivet.hh"
00003 #include "Rivet/Tools/Logging.hh"
00004 #include "Rivet/HistoHandler.hh"
00005 #include "Rivet/Analysis.hh"
00006 #include <algorithm>
00007 
00008 namespace Rivet {
00009 
00010 
00011   // Initialize instance pointer to null.
00012   HistoHandler* HistoHandler::_instance = 0;
00013 
00014 
00015   HistoHandler* HistoHandler::create() {
00016     if (!_instance) {
00017       _instance = new HistoHandler();
00018       Log::getLog("Rivet.HistoHandler")
00019         << Log::TRACE << "Created new HistoHandler at "
00020         << _instance << endl;
00021     }
00022     return _instance;
00023   }
00024 
00025 
00026   // Get a logger.
00027   Log& HistoHandler::getLog() const {
00028     return Log::getLog("Rivet.HistoHandler");
00029   }
00030 
00031 
00032   void HistoHandler::clear() {
00033     _namedhistos.clear();
00034   }
00035 
00036 
00037   // Delete contained pointers.
00038   HistoHandler::~HistoHandler() {
00039     clear();
00040   }
00041 
00042 
00043   const AnalysisObject* HistoHandler::registerAnalysisObject(const Analysis& parent,
00044                                                              const AnalysisObject& ao,
00045                                                              const string& name) {
00046     getLog() << Log::TRACE << "Trying to register"
00047              << " analysis object " << &ao
00048              << " for parent " << &parent << "(" << parent.name() << ")"
00049              << " with name '" << name << "'" << endl;
00050  
00051     // If this name is already registered for this analysis, throw a complaint
00052     NamedHistosMap::const_iterator nhs = _namedhistos.find(&parent);
00053     if (nhs != _namedhistos.end()) {
00054       NamedHistos::const_iterator nh = nhs->second.find(name);
00055       if (nh != nhs->second.end()) {
00056         stringstream ss;
00057         ss << "Histogram \"" << name
00058            << "\" already exists for parent analysis " << &parent;
00059         throw Error(ss.str());
00060       }
00061     }
00062 
00063     _namedhistos[&parent][name] = &ao;
00064     //return *(_namedhistos[&parent][name]);
00065     return const_cast<AnalysisObject*>(_namedhistos[&parent][name]);
00066   }
00067 
00068 
00069 
00070   AnalysisObject* HistoHandler::_getAnalysisObject(const Analysis& parent,
00071                                                          const string& name) const {
00072     getLog() << Log::TRACE << "Searching for child histo '"
00073              << name << "' of " << &parent << endl;
00074 
00075     NamedHistosMap::const_iterator nhs = _namedhistos.find(&parent);
00076     if (nhs == _namedhistos.end()) {
00077       stringstream ss;
00078       ss << "Couldn't find any histograms for parent analysis " << &parent;
00079       throw Error(ss.str());
00080     }
00081 
00082     NamedHistos::const_iterator nh = nhs->second.find(name);
00083     if (nh == nhs->second.end()) {
00084       stringstream ss;
00085       ss << "Couldn't find histogram \"" << name
00086          << "\" for parent analysis " << &parent;
00087       throw Error(ss.str());
00088     }
00089 
00090     //return *(nh->second);
00091     AnalysisObject* rtn = const_cast<AnalysisObject*>(nh->second);
00092     return rtn;
00093   }
00094 
00095 
00096 }