Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:24:17

0001 #ifndef ConfigurableAnalysis_VariableHelper_H
0002 #define ConfigurableAnalysis_VariableHelper_H
0003 
0004 #include "FWCore/Framework/interface/ConsumesCollector.h"
0005 #include "FWCore/ServiceRegistry/interface/ActivityRegistry.h"
0006 #include "PhysicsTools/UtilAlgos/interface/CachingVariable.h"
0007 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0008 #include "DataFormats/Provenance/interface/ModuleDescription.h"
0009 #include "FWCore/ServiceRegistry/interface/ModuleCallingContext.h"
0010 
0011 class VariableHelper {
0012 public:
0013   VariableHelper(const edm::ParameterSet& iConfig, edm::ConsumesCollector& iC);
0014   ~VariableHelper() {
0015     for (iterator it = variables_.begin(); it != variables_.end(); ++it) {
0016       delete it->second;
0017     }
0018   }
0019   typedef std::map<std::string, const CachingVariable*>::const_iterator iterator;
0020 
0021   const CachingVariable* variable(std::string name) const;
0022 
0023   iterator begin() { return variables_.begin(); }
0024   iterator end() { return variables_.end(); }
0025 
0026   void setHolder(std::string hn);
0027   void print() const;
0028   std::string printValues(const edm::Event& event) const;
0029 
0030 private:
0031   std::map<std::string, const CachingVariable*> variables_;
0032 };
0033 
0034 class VariableHelperService {
0035 private:
0036   VariableHelper* SetVariableHelperUniqueInstance_;
0037   std::map<std::string, VariableHelper*> multipleInstance_;
0038 
0039   bool printValuesForEachEvent_;
0040   std::string printValuesForEachEventCategory_;
0041 
0042 public:
0043   VariableHelperService(const edm::ParameterSet& iConfig, edm::ActivityRegistry& r)
0044       : SetVariableHelperUniqueInstance_(nullptr) {
0045     //r.watchPreModule(this, &VariableHelperService::preModule );
0046     r.watchPreModuleEvent(this, &VariableHelperService::preModule);
0047     //r.watchPostProcessEvent(this, &VariableHelperService::postProcess );
0048     r.watchPostEvent(this, &VariableHelperService::postProcess);
0049     printValuesForEachEvent_ = iConfig.exists("printValuesForEachEventCategory");
0050     if (printValuesForEachEvent_)
0051       printValuesForEachEventCategory_ = iConfig.getParameter<std::string>("printValuesForEachEventCategory");
0052   }
0053   ~VariableHelperService() {
0054     for (std::map<std::string, VariableHelper*>::iterator it = multipleInstance_.begin(); it != multipleInstance_.end();
0055          ++it) {
0056       delete it->second;
0057     }
0058   }
0059 
0060   VariableHelper& init(std::string user, const edm::ParameterSet& iConfig, edm::ConsumesCollector&& iC) {
0061     if (multipleInstance_.find(user) != multipleInstance_.end()) {
0062       std::cerr << user << " VariableHelper user already defined." << std::endl;
0063       throw;
0064     } else
0065       SetVariableHelperUniqueInstance_ = new VariableHelper(iConfig, iC);
0066     multipleInstance_[user] = SetVariableHelperUniqueInstance_;
0067     SetVariableHelperUniqueInstance_->setHolder(user);
0068 
0069     SetVariableHelperUniqueInstance_->print();
0070     return (*SetVariableHelperUniqueInstance_);
0071   }
0072 
0073   VariableHelper& get() {
0074     if (!SetVariableHelperUniqueInstance_) {
0075       std::cerr << " none of VariableHelperUniqueInstance_ or SetVariableHelperUniqueInstance_ is valid." << std::endl;
0076       throw;
0077     } else
0078       return (*SetVariableHelperUniqueInstance_);
0079   }
0080 
0081   void preModule(edm::StreamContext const&, edm::ModuleCallingContext const& mcc) {
0082     const edm::ModuleDescription& desc = *mcc.moduleDescription();
0083     //does a set with the module name, except that it does not throw on non-configured modules
0084     std::map<std::string, VariableHelper*>::iterator f = multipleInstance_.find(desc.moduleLabel());
0085     if (f != multipleInstance_.end()) {
0086       SetVariableHelperUniqueInstance_ = (f->second);
0087       return;
0088     }
0089     SetVariableHelperUniqueInstance_ = nullptr;
0090   }
0091 
0092   void postProcess(edm::StreamContext const& sc) {
0093     if (!printValuesForEachEvent_)
0094       return;
0095 
0096     /*const edm::Event & event;
0097       std::map<std::string, VariableHelper* >::iterator f= multipleInstance_.begin();
0098       for (; f!=multipleInstance_.end();++f){
0099       //      std::cout<<" category is: "<<printValuesForEachEventCategory_+"|"+f->first<<std::endl;
0100       //      std::cout<<f->first<<"\n"        <<f->second->printValues(event);
0101       
0102       edm::LogInfo(printValuesForEachEventCategory_+"|"+f->first)<<f->first<<"\n"
0103       <<f->second->printValues(event);
0104       }
0105     */
0106   }
0107 
0108   VariableHelper& set(std::string user) {
0109     std::map<std::string, VariableHelper*>::iterator f = multipleInstance_.find(user);
0110     if (f == multipleInstance_.end()) {
0111       std::cerr << user << " VariableHelper user not defined." << std::endl;
0112       throw;
0113     } else {
0114       SetVariableHelperUniqueInstance_ = (f->second);
0115       return (*SetVariableHelperUniqueInstance_);
0116     }
0117   }
0118 };
0119 
0120 #endif