Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:33:58

0001 #include "Validation/RecoTau/plugins/DQMFileLoader.h"
0002 
0003 #include "Validation/RecoTau/plugins/dqmAuxFunctions.h"
0004 
0005 // framework & common header files
0006 #include "FWCore/Framework/interface/Frameworkfwd.h"
0007 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0008 
0009 //DQM services
0010 #include "DQMServices/Core/interface/DQMStore.h"
0011 #include "FWCore/ServiceRegistry/interface/Service.h"
0012 
0013 #include <TFile.h>
0014 #include <TList.h>
0015 #include <TKey.h>
0016 #include <TH1.h>
0017 
0018 #include <iostream>
0019 
0020 const std::string dqmRootDirectory_inTFile = "DQMData";
0021 
0022 const double defaultScaleFactor = 1.;
0023 
0024 const int verbosity = 0;
0025 
0026 void mapSubDirectoryStructure(TDirectory* directory, std::string directoryName, std::set<std::string>& subDirectories) {
0027   //std::cout << "<mapSubDirectoryStructure>:" << std::endl;
0028   //std::cout << " directoryName = " << directoryName << std::endl;
0029 
0030   TList* subDirectoryNames = directory->GetListOfKeys();
0031   if (!subDirectoryNames)
0032     return;
0033 
0034   TIter next(subDirectoryNames);
0035   while (TKey* key = dynamic_cast<TKey*>(next())) {
0036     //std::cout << " key->GetName = " << key->GetName() << std::endl;
0037     TObject* obj = directory->Get(key->GetName());
0038     //std::cout << " obj = " << obj << std::endl;
0039     if (TDirectory* subDirectory = dynamic_cast<TDirectory*>(obj)) {
0040       std::string subDirectoryName = dqmDirectoryName(directoryName).append(key->GetName());
0041       //std::cout << " subDirectoryName = " << subDirectoryName << std::endl;
0042 
0043       subDirectories.insert(subDirectoryName);
0044 
0045       mapSubDirectoryStructure(subDirectory, subDirectoryName, subDirectories);
0046     }
0047   }
0048 }
0049 
0050 //
0051 //-----------------------------------------------------------------------------------------------------------------------
0052 //
0053 
0054 TauDQMFileLoader::cfgEntryFileSet::cfgEntryFileSet(const std::string& name, const edm::ParameterSet& cfg) {
0055   //std::cout << "<TauDQMFileLoader::cfgEntryFileSet>" << std::endl;
0056 
0057   name_ = name;
0058 
0059   vstring inputFileList = cfg.getParameter<vstring>("inputFileNames");
0060   for (vstring::const_iterator inputFile = inputFileList.begin(); inputFile != inputFileList.end(); ++inputFile) {
0061     if (inputFile->find(rangeKeyword) != std::string::npos) {
0062       size_t posRangeStart = inputFile->find(rangeKeyword) + rangeKeyword.length();
0063       size_t posRangeEnd = inputFile->find('#', posRangeStart);
0064 
0065       size_t posRangeSeparator = inputFile->find('-', posRangeStart);
0066 
0067       if ((posRangeEnd == std::string::npos) || (posRangeSeparator >= posRangeEnd)) {
0068         edm::LogError("TauDQMFileLoader::cfgEntryFileSet")
0069             << " Invalid range specification in inputFile = " << (*inputFile) << " !!";
0070         continue;
0071       }
0072 
0073       std::string firstFile = std::string(*inputFile, posRangeStart, posRangeSeparator - posRangeStart);
0074       //std::cout << "firstFile = " << firstFile << std::endl;
0075       std::string lastFile = std::string(*inputFile, posRangeSeparator + 1, posRangeEnd - (posRangeSeparator + 1));
0076       //std::cout << "lastFile = " << lastFile << std::endl;
0077 
0078       if (firstFile.length() != lastFile.length()) {
0079         edm::LogError("TauDQMFileLoader::cfgEntryFileSet")
0080             << " Invalid range specification in inputFile = " << (*inputFile) << " !!";
0081         continue;
0082       }
0083 
0084       int numFirstFile = atoi(firstFile.data());
0085       int numLastFile = atoi(lastFile.data());
0086       for (int iFile = numFirstFile; iFile <= numLastFile; ++iFile) {
0087         std::ostringstream fileName;
0088         fileName << std::string(*inputFile, 0, inputFile->find(rangeKeyword));
0089         fileName << std::setfill('0') << std::setw(firstFile.length()) << iFile;
0090         fileName << std::string(*inputFile, posRangeEnd + 1);
0091         //std::cout << "iFile = " << iFile << ", fileName = " << fileName.str() << std::endl;
0092         inputFileNames_.push_back(fileName.str());
0093       }
0094     } else {
0095       inputFileNames_.push_back(*inputFile);
0096     }
0097   }
0098 
0099   scaleFactor_ = (cfg.exists("scaleFactor")) ? cfg.getParameter<double>("scaleFactor") : defaultScaleFactor;
0100 
0101   //dqmDirectory_store_ = ( cfg.exists("dqmDirectory_store") ) ? cfg.getParameter<std::string>("dqmDirectory_store") : name_;
0102   dqmDirectory_store_ = (cfg.exists("dqmDirectory_store")) ? cfg.getParameter<std::string>("dqmDirectory_store") : "";
0103 
0104   if (verbosity)
0105     print();
0106 }
0107 
0108 void TauDQMFileLoader::cfgEntryFileSet::print() const {
0109   std::cout << "<cfgEntryFileSet::print>:" << std::endl;
0110   std::cout << " name = " << name_ << std::endl;
0111   std::cout << " inputFileNames = " << format_vstring(inputFileNames_) << std::endl;
0112   std::cout << " scaleFactor = " << scaleFactor_ << std::endl;
0113   std::cout << " dqmDirectory_store = " << dqmDirectory_store_ << std::endl;
0114 }
0115 
0116 //
0117 //-----------------------------------------------------------------------------------------------------------------------
0118 //
0119 
0120 TauDQMFileLoader::TauDQMFileLoader(const edm::ParameterSet& cfg) {
0121   std::cout << "<TauDQMFileLoader::TauDQMFileLoader>:" << std::endl;
0122 
0123   cfgError_ = 0;
0124 
0125   //--- configure fileSets
0126   //std::cout << "--> configuring fileSets..." << std::endl;
0127   readCfgParameter<cfgEntryFileSet>(cfg, fileSets_);
0128 
0129   //--- check that dqmDirectory_store configuration parameters are specified for all fileSets,
0130   //    unless there is only one fileSet to be loaded
0131   //    (otherwise histograms of different fileSets get overwritten,
0132   //     once histograms of the next fileSet are loaded)
0133   for (std::map<std::string, cfgEntryFileSet>::const_iterator fileSet = fileSets_.begin(); fileSet != fileSets_.end();
0134        ++fileSet) {
0135     if (fileSet->second.dqmDirectory_store_.empty() && fileSets_.size() > 1) {
0136       edm::LogError("TauDQMFileLoader") << " dqmDirectory_store undefined for fileSet = " << fileSet->second.name_
0137                                         << " !!";
0138       cfgError_ = 1;
0139       break;
0140     }
0141   }
0142 
0143   std::cout << "done." << std::endl;
0144 }
0145 
0146 TauDQMFileLoader::~TauDQMFileLoader() {
0147   // nothing to be done yet...
0148 }
0149 
0150 void TauDQMFileLoader::analyze(const edm::Event&, const edm::EventSetup&) {
0151   // nothing to be done yet...
0152 }
0153 
0154 void TauDQMFileLoader::endRun(const edm::Run& r, const edm::EventSetup& c) {
0155   std::cout << "<TauDQMFileLoader::endJob>:" << std::endl;
0156 
0157   //--- check that configuration parameters contain no errors
0158   if (cfgError_) {
0159     edm::LogError("endJob") << " Error in Configuration ParameterSet"
0160                             << " --> histograms will NOT be loaded !!";
0161     return;
0162   }
0163 
0164   //--- check that DQMStore service is available
0165   if (!edm::Service<DQMStore>().isAvailable()) {
0166     edm::LogError("endJob") << " Failed to access dqmStore"
0167                             << " --> histograms will NOT be loaded !!";
0168     return;
0169   }
0170 
0171   //--- stop ROOT from keeping references to all histograms
0172   //TH1::AddDirectory(false);
0173 
0174   //--- check that inputFiles exist;
0175   //    store list of directories existing in inputFile,
0176   //    in order to separate histogram directories existing in the inputFile from directories existing in DQMStore
0177   //    when calling recursive function dqmCopyRecursively
0178   for (std::map<std::string, cfgEntryFileSet>::const_iterator fileSet = fileSets_.begin(); fileSet != fileSets_.end();
0179        ++fileSet) {
0180     for (vstring::const_iterator inputFileName = fileSet->second.inputFileNames_.begin();
0181          inputFileName != fileSet->second.inputFileNames_.end();
0182          ++inputFileName) {
0183       //std::cout << " checking inputFile = " << (*inputFileName) << std::endl;
0184       TFile inputFile(inputFileName->data());
0185       if (inputFile.IsZombie()) {
0186         edm::LogError("endJob") << " Failed to open inputFile = " << (*inputFileName)
0187                                 << "--> histograms will NOT be loaded !!";
0188         return;
0189       }
0190 
0191       TObject* obj = inputFile.Get(dqmRootDirectory_inTFile.data());
0192       //std::cout << " obj = " << obj << std::endl;
0193       if (TDirectory* directory = dynamic_cast<TDirectory*>(obj)) {
0194         mapSubDirectoryStructure(directory, dqmRootDirectory, subDirectoryMap_[*inputFileName]);
0195       } else {
0196         edm::LogError("endJob") << " Failed to access " << dqmRootDirectory_inTFile
0197                                 << " in inputFile = " << (*inputFileName) << "--> histograms will NOT be loaded !!";
0198         return;
0199       }
0200 
0201       inputFile.Close();
0202     }
0203   }
0204 
0205   //for ( std::map<std::string, sstring>::const_iterator inputFile = subDirectoryMap_.begin();
0206   //      inputFile != subDirectoryMap_.end(); ++inputFile ) {
0207   //  std::cout << "inputFile = " << inputFile->first << ":" << std::endl;
0208   //  for ( sstring::const_iterator directory = inputFile->second.begin();
0209   //        directory != inputFile->second.end(); ++directory ) {
0210   //    std::cout << " " << (*directory) << std::endl;
0211   //  }
0212   //}
0213 
0214   //--- load histograms from file
0215   //std::cout << "--> loading histograms from file..." << std::endl;
0216   DQMStore& dqmStore = (*edm::Service<DQMStore>());
0217   for (std::map<std::string, cfgEntryFileSet>::const_iterator fileSet = fileSets_.begin(); fileSet != fileSets_.end();
0218        ++fileSet) {
0219     for (vstring::const_iterator inputFileName = fileSet->second.inputFileNames_.begin();
0220          inputFileName != fileSet->second.inputFileNames_.end();
0221          ++inputFileName) {
0222       if (verbosity)
0223         std::cout << " opening inputFile = " << (*inputFileName) << std::endl;
0224       dqmStore.open(*inputFileName, true);
0225 
0226       //--- if dqmDirectory_store specified in configuration parameters,
0227       //    move histograms from dqmRootDirectory to dqmDirectory_store
0228       //    (if the histograms are not moved, the histograms get overwritten,
0229       //     the next time DQMStore::open is called)
0230       if (!fileSet->second.dqmDirectory_store_.empty()) {
0231         std::string inputDirectory = dqmRootDirectory;
0232         //std::cout << "inputDirectory = " << inputDirectory << std::endl;
0233         std::string outputDirectory =
0234             dqmDirectoryName(std::string(inputDirectory)).append(fileSet->second.dqmDirectory_store_);
0235         //std::cout << "outputDirectory = " << outputDirectory << std::endl;
0236 
0237         dqmStore.setCurrentFolder(inputDirectory);
0238         std::vector<std::string> dirNames = dqmStore.getSubdirs();
0239         for (std::vector<std::string>::const_iterator dirName = dirNames.begin(); dirName != dirNames.end();
0240              ++dirName) {
0241           std::string subDirName = dqmSubDirectoryName_merged(inputDirectory, *dirName);
0242           //std::cout << " subDirName = " << subDirName << std::endl;
0243 
0244           const sstring& subDirectories = subDirectoryMap_[*inputFileName];
0245           if (subDirectories.find(subDirName) != subDirectories.end()) {
0246             std::string inputDirName_full = dqmDirectoryName(inputDirectory).append(subDirName);
0247             //std::cout << " inputDirName_full = " << inputDirName_full << std::endl;
0248 
0249             std::string outputDirName_full = dqmDirectoryName(outputDirectory).append(subDirName);
0250             //std::cout << " outputDirName_full = " << outputDirName_full << std::endl;
0251 
0252             //--- load histograms contained in inputFile into inputDirectory;
0253             //    when processing first inputFile, check that histograms in outputDirectory do not yet exist;
0254             //    add histograms in inputFile to those in outputDirectory afterwards;
0255             //    clear inputDirectory once finished processing all inputFiles.
0256             int mode = (inputFileName == fileSet->second.inputFileNames_.begin()) ? 1 : 3;
0257             dqmCopyRecursively(
0258                 dqmStore, inputDirName_full, outputDirName_full, fileSet->second.scaleFactor_, mode, true);
0259           }
0260         }
0261       }
0262     }
0263   }
0264 
0265   std::cout << "done." << std::endl;
0266 }
0267 
0268 #include "FWCore/Framework/interface/MakerMacros.h"
0269 
0270 DEFINE_FWK_MODULE(TauDQMFileLoader);