CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch12/src/PhysicsTools/UtilAlgos/interface/VariableHelper.h

Go to the documentation of this file.
00001 #ifndef ConfigurableAnalysis_VariableHelper_H
00002 #define ConfigurableAnalysis_VariableHelper_H
00003 
00004 #include "FWCore/ServiceRegistry/interface/ActivityRegistry.h"
00005 #include "PhysicsTools/UtilAlgos/interface/CachingVariable.h"
00006 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00007 #include "DataFormats/Provenance/interface/ModuleDescription.h"
00008 
00009 class VariableHelper {
00010  public:
00011   VariableHelper(const edm::ParameterSet & iConfig);
00012   ~VariableHelper() {
00013     for (iterator it = variables_.begin() ; it!=variables_.end() ;++it){
00014       delete it->second;
00015     }
00016   }
00017   typedef std::map<std::string,const CachingVariable*>::const_iterator iterator;
00018 
00019   const CachingVariable* variable(std::string name)const ;
00020 
00021   iterator begin() { return variables_.begin();}
00022   iterator end() { return variables_.end();}
00023 
00024   void setHolder(std::string hn);
00025   void print() const;
00026   std::string printValues(const edm::Event & event) const;
00027  private:
00028   std::map<std::string,const CachingVariable*> variables_;
00029 };
00030 
00031 
00032 
00033 
00034 class VariableHelperService {
00035  private:
00036   VariableHelper * SetVariableHelperUniqueInstance_;
00037   std::map<std::string, VariableHelper* > multipleInstance_;
00038 
00039   bool printValuesForEachEvent_;
00040   std::string printValuesForEachEventCategory_;
00041  public:
00042   VariableHelperService(const edm::ParameterSet & iConfig,edm::ActivityRegistry & r ){
00043     r.watchPreModule(this, &VariableHelperService::preModule );
00044     r.watchPostProcessEvent(this, &VariableHelperService::postProcess );
00045     printValuesForEachEvent_ = iConfig.exists("printValuesForEachEventCategory");
00046     if (printValuesForEachEvent_) 
00047       printValuesForEachEventCategory_ = iConfig.getParameter<std::string>("printValuesForEachEventCategory");
00048   }
00049   ~VariableHelperService(){
00050     for (std::map<std::string, VariableHelper* > :: iterator it=multipleInstance_.begin(); it!=multipleInstance_.end(); ++it){
00051       delete it->second;
00052     }
00053   }
00054 
00055   VariableHelper & init(std::string user, const edm::ParameterSet & iConfig){
00056     if (multipleInstance_.find(user)!=multipleInstance_.end()){
00057       std::cerr<<user<<" VariableHelper user already defined."<<std::endl;
00058       throw;}
00059     else SetVariableHelperUniqueInstance_ = new VariableHelper(iConfig);
00060     multipleInstance_[user] = SetVariableHelperUniqueInstance_;
00061     SetVariableHelperUniqueInstance_->setHolder(user);
00062 
00063     SetVariableHelperUniqueInstance_->print();
00064     return (*SetVariableHelperUniqueInstance_);
00065   }
00066   
00067   VariableHelper & get(){
00068     if (!SetVariableHelperUniqueInstance_)
00069       {
00070         std::cerr<<" none of VariableHelperUniqueInstance_ or SetVariableHelperUniqueInstance_ is valid."<<std::endl;
00071         throw;
00072       }
00073     else return (*SetVariableHelperUniqueInstance_);
00074   }
00075 
00076   void preModule(const edm::ModuleDescription& desc){
00077     //does a set with the module name, except that it does not throw on non-configured modules
00078     std::map<std::string, VariableHelper* >::iterator f=multipleInstance_.find(desc.moduleLabel());
00079     if (f != multipleInstance_.end())  SetVariableHelperUniqueInstance_ = (f->second);
00080     else { 
00081       //do not say anything but set it to zero to get a safe crash in get() if ever called
00082       SetVariableHelperUniqueInstance_ =0;}
00083   }
00084 
00085   void postProcess(const edm::Event & event, const edm::EventSetup & setup){
00086     if (!printValuesForEachEvent_) return;
00087     std::map<std::string, VariableHelper* >::iterator f= multipleInstance_.begin();
00088     for (; f!=multipleInstance_.end();++f){
00089       //      std::cout<<" category is: "<<printValuesForEachEventCategory_+"|"+f->first<<std::endl;
00090       //      std::cout<<f->first<<"\n"        <<f->second->printValues(event);
00091 
00092       edm::LogInfo(printValuesForEachEventCategory_+"|"+f->first)<<f->first<<"\n"
00093                                                                  <<f->second->printValues(event);
00094     }
00095   }
00096 
00097   VariableHelper & set(std::string user){
00098     std::map<std::string, VariableHelper* >::iterator f=multipleInstance_.find(user);
00099     if (f == multipleInstance_.end()){
00100       std::cerr<<user<<" VariableHelper user not defined."<<std::endl;
00101       throw;
00102     }
00103     else{
00104       SetVariableHelperUniqueInstance_ = (f->second);
00105       return (*SetVariableHelperUniqueInstance_);
00106     }
00107   }
00108 };
00109 
00110 #endif