Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:01:05

0001 /** \class GBRForestWriter
0002  *
0003  * Read GBRForest objects from ROOT file input
0004  * and store it in SQL-lite output file
0005  *
0006  * \authors Christian Veelken, LLR
0007  *
0008  */
0009 
0010 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0011 #include "FWCore/Framework/interface/Event.h"
0012 #include "FWCore/Framework/interface/Frameworkfwd.h"
0013 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0014 #include "FWCore/Utilities/interface/Exception.h"
0015 #include "CondCore/DBOutputService/interface/PoolDBOutputService.h"
0016 #include "FWCore/ServiceRegistry/interface/Service.h"
0017 #include "CommonTools/MVAUtils/interface/GBRForestTools.h"
0018 #include "FWCore/Framework/interface/MakerMacros.h"
0019 
0020 #include <TFile.h>
0021 #include <string>
0022 #include <vector>
0023 
0024 class GBRForestWriter : public edm::one::EDAnalyzer<> {
0025 public:
0026   GBRForestWriter(const edm::ParameterSet&);
0027   ~GBRForestWriter() override;
0028 
0029 private:
0030   void analyze(const edm::Event&, const edm::EventSetup&) override;
0031 
0032   std::string moduleLabel_;
0033 
0034   bool hasRun_;
0035 
0036   typedef std::vector<std::string> vstring;
0037 
0038   struct categoryEntryType {
0039     categoryEntryType(const edm::ParameterSet& cfg) {
0040       if (cfg.existsAs<edm::FileInPath>("inputFileName")) {
0041         edm::FileInPath inputFileName_fip = cfg.getParameter<edm::FileInPath>("inputFileName");
0042         inputFileName_ = inputFileName_fip.fullPath();
0043       } else if (cfg.existsAs<std::string>("inputFileName")) {
0044         inputFileName_ = cfg.getParameter<std::string>("inputFileName");
0045       } else
0046         throw cms::Exception("GBRForestWriter") << " Undefined Configuration Parameter 'inputFileName !!\n";
0047       std::string inputFileType_string = cfg.getParameter<std::string>("inputFileType");
0048       if (inputFileType_string == "XML")
0049         inputFileType_ = kXML;
0050       else if (inputFileType_string == "GBRForest")
0051         inputFileType_ = kGBRForest;
0052       else
0053         throw cms::Exception("GBRForestWriter")
0054             << " Invalid Configuration Parameter 'inputFileType' = " << inputFileType_string << " !!\n";
0055       if (inputFileType_ == kXML) {
0056         inputVariables_ = cfg.getParameter<vstring>("inputVariables");
0057         spectatorVariables_ = cfg.getParameter<vstring>("spectatorVariables");
0058         methodName_ = cfg.getParameter<std::string>("methodName");
0059         gbrForestName_ =
0060             (cfg.existsAs<std::string>("gbrForestName") ? cfg.getParameter<std::string>("gbrForestName") : methodName_);
0061       } else {
0062         gbrForestName_ = cfg.getParameter<std::string>("gbrForestName");
0063       }
0064     }
0065     ~categoryEntryType() {}
0066     std::string inputFileName_;
0067     enum { kXML, kGBRForest };
0068     int inputFileType_;
0069     vstring inputVariables_;
0070     vstring spectatorVariables_;
0071     std::string gbrForestName_;
0072     std::string methodName_;
0073   };
0074   struct jobEntryType {
0075     jobEntryType(const edm::ParameterSet& cfg) {
0076       if (cfg.exists("categories")) {
0077         edm::VParameterSet cfgCategories = cfg.getParameter<edm::VParameterSet>("categories");
0078         for (edm::VParameterSet::const_iterator cfgCategory = cfgCategories.begin(); cfgCategory != cfgCategories.end();
0079              ++cfgCategory) {
0080           categoryEntryType* category = new categoryEntryType(*cfgCategory);
0081           categories_.push_back(category);
0082         }
0083       } else {
0084         categoryEntryType* category = new categoryEntryType(cfg);
0085         categories_.push_back(category);
0086       }
0087       std::string outputFileType_string = cfg.getParameter<std::string>("outputFileType");
0088       if (outputFileType_string == "GBRForest")
0089         outputFileType_ = kGBRForest;
0090       else if (outputFileType_string == "SQLLite")
0091         outputFileType_ = kSQLLite;
0092       else
0093         throw cms::Exception("GBRForestWriter")
0094             << " Invalid Configuration Parameter 'outputFileType' = " << outputFileType_string << " !!\n";
0095       if (outputFileType_ == kGBRForest) {
0096         outputFileName_ = cfg.getParameter<std::string>("outputFileName");
0097       }
0098       if (outputFileType_ == kSQLLite) {
0099         outputRecord_ = cfg.getParameter<std::string>("outputRecord");
0100       }
0101     }
0102     ~jobEntryType() {
0103       for (std::vector<categoryEntryType*>::iterator it = categories_.begin(); it != categories_.end(); ++it) {
0104         delete (*it);
0105       }
0106     }
0107     std::vector<categoryEntryType*> categories_;
0108     enum { kGBRForest, kSQLLite };
0109     int outputFileType_;
0110     std::string outputFileName_;
0111     std::string outputRecord_;
0112   };
0113   std::vector<jobEntryType*> jobs_;
0114 };
0115 
0116 GBRForestWriter::GBRForestWriter(const edm::ParameterSet& cfg)
0117     : moduleLabel_(cfg.getParameter<std::string>("@module_label")) {
0118   edm::VParameterSet cfgJobs = cfg.getParameter<edm::VParameterSet>("jobs");
0119   for (edm::VParameterSet::const_iterator cfgJob = cfgJobs.begin(); cfgJob != cfgJobs.end(); ++cfgJob) {
0120     jobEntryType* job = new jobEntryType(*cfgJob);
0121     jobs_.push_back(job);
0122   }
0123 }
0124 
0125 GBRForestWriter::~GBRForestWriter() {
0126   for (std::vector<jobEntryType*>::iterator it = jobs_.begin(); it != jobs_.end(); ++it) {
0127     delete (*it);
0128   }
0129 }
0130 
0131 void GBRForestWriter::analyze(const edm::Event&, const edm::EventSetup&) {
0132   for (std::vector<jobEntryType*>::iterator job = jobs_.begin(); job != jobs_.end(); ++job) {
0133     std::map<std::string, const GBRForest*> gbrForests;  // key = name
0134     for (std::vector<categoryEntryType*>::iterator category = (*job)->categories_.begin();
0135          category != (*job)->categories_.end();
0136          ++category) {
0137       const GBRForest* gbrForest = nullptr;
0138       if ((*category)->inputFileType_ == categoryEntryType::kXML) {
0139         gbrForest = createGBRForest((*category)->inputFileName_).release();
0140       } else if ((*category)->inputFileType_ == categoryEntryType::kGBRForest) {
0141         TFile* inputFile = new TFile((*category)->inputFileName_.data());
0142         // gbrForest = dynamic_cast<GBRForest*>(inputFile->Get((*category)->gbrForestName_.data())); // CV:
0143         // dynamic_cast<GBRForest*> fails for some reason ?!
0144         gbrForest = (GBRForest*)inputFile->Get((*category)->gbrForestName_.data());
0145         delete inputFile;
0146       }
0147       if (!gbrForest)
0148         throw cms::Exception("GBRForestWriter") << " Failed to load GBRForest = " << (*category)->gbrForestName_.data()
0149                                                 << " from file = " << (*category)->inputFileName_ << " !!\n";
0150       gbrForests[(*category)->gbrForestName_] = gbrForest;
0151     }
0152     if ((*job)->outputFileType_ == jobEntryType::kGBRForest) {
0153       TFile* outputFile = new TFile((*job)->outputFileName_.data(), "RECREATE");
0154 
0155       for (std::map<std::string, const GBRForest*>::iterator gbrForest = gbrForests.begin();
0156            gbrForest != gbrForests.end();
0157            ++gbrForest) {
0158         outputFile->WriteObject(gbrForest->second, gbrForest->first.data());
0159       }
0160       delete outputFile;
0161     } else if ((*job)->outputFileType_ == jobEntryType::kSQLLite) {
0162       edm::Service<cond::service::PoolDBOutputService> dbService;
0163       if (!dbService.isAvailable())
0164         throw cms::Exception("GBRForestWriter") << " Failed to access PoolDBOutputService !!\n";
0165 
0166       for (std::map<std::string, const GBRForest*>::iterator gbrForest = gbrForests.begin();
0167            gbrForest != gbrForests.end();
0168            ++gbrForest) {
0169         std::string outputRecord = (*job)->outputRecord_;
0170         if (gbrForests.size() > 1)
0171           outputRecord.append("_").append(gbrForest->first);
0172         dbService->writeOneIOV(*gbrForest->second, dbService->beginOfTime(), outputRecord);
0173       }
0174     }
0175 
0176     // gbrforest deletion
0177     for (std::map<std::string, const GBRForest*>::iterator gbrForest = gbrForests.begin();
0178          gbrForest != gbrForests.end();
0179          ++gbrForest) {
0180       delete gbrForest->second;
0181     }
0182   }
0183 }
0184 
0185 DEFINE_FWK_MODULE(GBRForestWriter);