CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/CondTools/HLT/src/AlCaRecoTriggerBitsRcdRead.cc

Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 #include <string>
00019 #include <map>
00020 //#include <vector>
00021 #include <sstream>
00022 #include <fstream>
00023 
00024 // Framework
00025 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00026 #include "FWCore/Framework/interface/EventSetup.h"
00027 #include "FWCore/Framework/interface/ESHandle.h"
00028 #include "FWCore/Framework/interface/ESWatcher.h"
00029 #include "FWCore/Framework/interface/EDAnalyzer.h"
00030 #include "FWCore/Framework/interface/MakerMacros.h"
00031 #include "FWCore/Framework/interface/Run.h"
00032 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00033 #include "FWCore/Utilities/interface/Exception.h"
00034 
00035 // What I want to read:
00036 #include "CondFormats/HLTObjects/interface/AlCaRecoTriggerBits.h"
00037 #include "CondFormats/DataRecord/interface/AlCaRecoTriggerBitsRcd.h"
00038 
00039 
00040 class  AlCaRecoTriggerBitsRcdRead : public edm::EDAnalyzer {
00041 public:
00042   explicit  AlCaRecoTriggerBitsRcdRead(const edm::ParameterSet &cfg);
00043   ~AlCaRecoTriggerBitsRcdRead() {}
00044   
00045   virtual void analyze(const edm::Event &evt, const edm::EventSetup &evtSetup) {}
00046   virtual void beginRun(const edm::Run &run, const edm::EventSetup &evtSetup);
00047   virtual void endJob();
00048 
00049   
00050 private:
00051   // types
00052   enum OutputType {kText, kTwiki, kPython}; //kHtml};
00053 
00054   // methods
00055   OutputType stringToEnum(const std::string &outputType) const;
00056   void printMap(edm::RunNumber_t firstRun, edm::RunNumber_t lastRun,
00057                 const AlCaRecoTriggerBits &triggerMap) const;
00058 
00059   // members
00060   const OutputType outputType_;
00061   edm::ESWatcher<AlCaRecoTriggerBitsRcd> watcher_;
00062   edm::RunNumber_t firstRun_; 
00063   edm::RunNumber_t lastRun_;
00064   AlCaRecoTriggerBits lastTriggerBits_;
00065   std::auto_ptr<std::ofstream> output_;
00066 };
00067 
00071 
00072 AlCaRecoTriggerBitsRcdRead::AlCaRecoTriggerBitsRcdRead(const edm::ParameterSet& cfg)
00073   : outputType_(this->stringToEnum(cfg.getUntrackedParameter<std::string>("outputType"))),
00074     firstRun_(0), lastRun_(0)
00075 {
00076   //   edm::LogInfo("") << "@SUB=AlCaRecoTriggerBitsRcdRead" 
00077   //               << cfg.getParameter<std::string>("@module_label");
00078 
00079   std::string fileName(cfg.getUntrackedParameter<std::string>("rawFileName"));
00080   switch (outputType_) { // now append suffix
00081   case kText:   fileName += ".txt";   break;
00082   case kPython: fileName += ".py";    break;
00083   case kTwiki:  fileName += ".twiki"; break;
00084   }
00085   if (fileName.size()) {
00086     output_.reset(new std::ofstream(fileName.c_str()));
00087     if (!output_->good()) {
00088       edm::LogError("IOproblem") << "Could not open output file " << fileName << ".";
00089       output_.reset();
00090     }
00091   }
00092 
00093 }
00094 
00096 AlCaRecoTriggerBitsRcdRead::OutputType
00097 AlCaRecoTriggerBitsRcdRead::stringToEnum(const std::string &outputTypeStr) const
00098 {
00099   if (outputTypeStr == "text") return kText;
00100   if (outputTypeStr == "twiki") return kTwiki;
00101   if (outputTypeStr == "python") return kPython;
00102   // if (outputTypeStr == "html") return kHtml;
00103 
00104   throw cms::Exception("BadConfig") << "AlCaRecoTriggerBitsRcdRead: "
00105                                     << "outputType '" << outputTypeStr << "' not known,"
00106                                     << " use 'text', 'twiki' or 'python'\n";
00107 
00108   return kTwiki; // never reached, to please compiler
00109 }
00110 
00112 void AlCaRecoTriggerBitsRcdRead::beginRun(const edm::Run& run, const edm::EventSetup& iSetup)
00113 {
00114   if (watcher_.check(iSetup)) { // new IOV for this run
00115     // Print last IOV - if there has already been one:
00116     if (lastRun_ != 0) this->printMap(firstRun_, lastRun_, lastTriggerBits_);
00117   
00118     // Get AlCaRecoTriggerBits from EventSetup:
00119     edm::ESHandle<AlCaRecoTriggerBits> triggerBits;
00120     iSetup.get<AlCaRecoTriggerBitsRcd>().get(triggerBits);
00121     lastTriggerBits_ = *triggerBits; // copy for later use
00122     firstRun_ = run.run();           // keep track where it started
00123   }
00124 
00125   lastRun_ = run.run(); // keep track of last visited run
00126 }
00127 
00129 void AlCaRecoTriggerBitsRcdRead::endJob()
00130 {
00131   // Print for very last IOV, not treated yet in beginRun(..):
00132   this->printMap(firstRun_, lastRun_, lastTriggerBits_);
00133 }
00134 
00136 void AlCaRecoTriggerBitsRcdRead::printMap(edm::RunNumber_t firstRun,
00137                                           edm::RunNumber_t lastRun, 
00138                                           const AlCaRecoTriggerBits &triggerBits) const
00139 {
00140   // Get map of strings to concatenated list of names of HLT paths:
00141   typedef std::map<std::string, std::string> TriggerMap;
00142   const TriggerMap &triggerMap = triggerBits.m_alcarecoToTrig;
00143 
00144   // Collect output for given run numbers via ostringstream.
00145   // Format depends on outputType_ configuration.
00146   std::ostringstream output;
00147   switch (outputType_) {
00148   case kPython:
00149     output << "  triggerLists = cms.VPSet(\n";
00150     // no 'break;'!
00151   case kText:
00152     output << "#\n# AlCaRecoTriggerBits settings for IOV "
00153            << firstRun << "-" << lastRun << ":\n#\n";
00154     break;
00155   case kTwiki:
00156     output << "---+++++ *IOV*: " << firstRun << "-" << lastRun << "\n"
00157            << "| *TriggerBits list key* | *HLT paths* |\n";
00158     break;
00159   }
00160 
00161   //  if (outputType_ == kPython) output << "  triggerLists = cms.VPSet(\n";
00162 
00163   // loop over entries in map
00164   for (TriggerMap::const_iterator i = triggerMap.begin(); i != triggerMap.end(); ++i) {
00165 
00166     if (outputType_ == kPython && i != triggerMap.begin()) output << ",\n";
00167 
00168     switch (outputType_) {
00169     case kPython:
00170       output << "      cms.PSet(listName = cms.string('" << i->first << "'),\n"
00171              << "               hltPaths = cms.vstring(";
00172       break;
00173     case kText:
00174       output << "trigger list key: '" << i->first << "'\npaths:\n";
00175       break;
00176     case kTwiki:
00177       output << "| '" << i->first << "' | ";
00178     }
00179     // We must avoid a map<string,vector<string> > in DB for performance reason,
00180     // so the paths are mapped into one string separated by ';':
00181     const std::vector<std::string> paths = triggerBits.decompose(i->second);
00182     for (unsigned int iPath = 0; iPath < paths.size(); ++iPath) {
00183       if (iPath != 0) {
00184         output << ", "; // next path
00185         switch (outputType_) {
00186         case kPython: // only 2 per line
00187         case kText:   // only 4 per line
00188           if (0 == (iPath % (outputType_ == kPython ? 2 : 4))) {
00189             output << "\n";
00190             if (outputType_ == kPython) output << "                                      ";
00191           }
00192           break;
00193         case kTwiki: // Twiki will handle that
00194           break;
00195         }
00196       }
00197       output << "'" << paths[iPath] << "'";
00198     }
00199     switch (outputType_) {
00200     case kPython:
00201       output << ")\n              )";
00202       break;
00203     case kText:
00204       output << "\n#\n";
00205       break;
00206     case kTwiki:
00207       output << " |\n";
00208     }
00209   }
00210   if (outputType_ == kPython) output << "\n      ) # closing of VPSet triggerLists\n"; 
00211   
00212   // Final output - either message logger or output file:
00213   if (output_.get()) *output_ << output.str();
00214   else edm::LogInfo("") << output.str();
00215 }
00216 
00217 
00218 
00219 //define this as a plug-in
00220 DEFINE_FWK_MODULE(AlCaRecoTriggerBitsRcdRead);