Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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   usesResource("DQMStore");
0122   std::cout << "<TauDQMFileLoader::TauDQMFileLoader>:" << std::endl;
0123 
0124   cfgError_ = 0;
0125 
0126   //--- configure fileSets
0127   //std::cout << "--> configuring fileSets..." << std::endl;
0128   readCfgParameter<cfgEntryFileSet>(cfg, fileSets_);
0129 
0130   //--- check that dqmDirectory_store configuration parameters are specified for all fileSets,
0131   //    unless there is only one fileSet to be loaded
0132   //    (otherwise histograms of different fileSets get overwritten,
0133   //     once histograms of the next fileSet are loaded)
0134   for (std::map<std::string, cfgEntryFileSet>::const_iterator fileSet = fileSets_.begin(); fileSet != fileSets_.end();
0135        ++fileSet) {
0136     if (fileSet->second.dqmDirectory_store_.empty() && fileSets_.size() > 1) {
0137       edm::LogError("TauDQMFileLoader") << " dqmDirectory_store undefined for fileSet = " << fileSet->second.name_
0138                                         << " !!";
0139       cfgError_ = 1;
0140       break;
0141     }
0142   }
0143 
0144   std::cout << "done." << std::endl;
0145 }
0146 
0147 TauDQMFileLoader::~TauDQMFileLoader() {
0148   // nothing to be done yet...
0149 }
0150 
0151 void TauDQMFileLoader::analyze(const edm::Event&, const edm::EventSetup&) {
0152   // nothing to be done yet...
0153 }
0154 
0155 void TauDQMFileLoader::endRun(const edm::Run& r, const edm::EventSetup& c) {
0156   std::cout << "<TauDQMFileLoader::endJob>:" << std::endl;
0157 
0158   //--- check that configuration parameters contain no errors
0159   if (cfgError_) {
0160     edm::LogError("endJob") << " Error in Configuration ParameterSet"
0161                             << " --> histograms will NOT be loaded !!";
0162     return;
0163   }
0164 
0165   //--- check that DQMStore service is available
0166   if (!edm::Service<DQMStore>().isAvailable()) {
0167     edm::LogError("endJob") << " Failed to access dqmStore"
0168                             << " --> histograms will NOT be loaded !!";
0169     return;
0170   }
0171 
0172   //--- stop ROOT from keeping references to all histograms
0173   //TH1::AddDirectory(false);
0174 
0175   //--- check that inputFiles exist;
0176   //    store list of directories existing in inputFile,
0177   //    in order to separate histogram directories existing in the inputFile from directories existing in DQMStore
0178   //    when calling recursive function dqmCopyRecursively
0179   for (std::map<std::string, cfgEntryFileSet>::const_iterator fileSet = fileSets_.begin(); fileSet != fileSets_.end();
0180        ++fileSet) {
0181     for (vstring::const_iterator inputFileName = fileSet->second.inputFileNames_.begin();
0182          inputFileName != fileSet->second.inputFileNames_.end();
0183          ++inputFileName) {
0184       //std::cout << " checking inputFile = " << (*inputFileName) << std::endl;
0185       TFile inputFile(inputFileName->data());
0186       if (inputFile.IsZombie()) {
0187         edm::LogError("endJob") << " Failed to open inputFile = " << (*inputFileName)
0188                                 << "--> histograms will NOT be loaded !!";
0189         return;
0190       }
0191 
0192       TObject* obj = inputFile.Get(dqmRootDirectory_inTFile.data());
0193       //std::cout << " obj = " << obj << std::endl;
0194       if (TDirectory* directory = dynamic_cast<TDirectory*>(obj)) {
0195         mapSubDirectoryStructure(directory, dqmRootDirectory, subDirectoryMap_[*inputFileName]);
0196       } else {
0197         edm::LogError("endJob") << " Failed to access " << dqmRootDirectory_inTFile
0198                                 << " in inputFile = " << (*inputFileName) << "--> histograms will NOT be loaded !!";
0199         return;
0200       }
0201 
0202       inputFile.Close();
0203     }
0204   }
0205 
0206   //for ( std::map<std::string, sstring>::const_iterator inputFile = subDirectoryMap_.begin();
0207   //      inputFile != subDirectoryMap_.end(); ++inputFile ) {
0208   //  std::cout << "inputFile = " << inputFile->first << ":" << std::endl;
0209   //  for ( sstring::const_iterator directory = inputFile->second.begin();
0210   //        directory != inputFile->second.end(); ++directory ) {
0211   //    std::cout << " " << (*directory) << std::endl;
0212   //  }
0213   //}
0214 
0215   //--- load histograms from file
0216   //std::cout << "--> loading histograms from file..." << std::endl;
0217   DQMStore& dqmStore = (*edm::Service<DQMStore>());
0218   for (std::map<std::string, cfgEntryFileSet>::const_iterator fileSet = fileSets_.begin(); fileSet != fileSets_.end();
0219        ++fileSet) {
0220     for (vstring::const_iterator inputFileName = fileSet->second.inputFileNames_.begin();
0221          inputFileName != fileSet->second.inputFileNames_.end();
0222          ++inputFileName) {
0223       if (verbosity)
0224         std::cout << " opening inputFile = " << (*inputFileName) << std::endl;
0225       dqmStore.open(*inputFileName, true);
0226 
0227       //--- if dqmDirectory_store specified in configuration parameters,
0228       //    move histograms from dqmRootDirectory to dqmDirectory_store
0229       //    (if the histograms are not moved, the histograms get overwritten,
0230       //     the next time DQMStore::open is called)
0231       if (!fileSet->second.dqmDirectory_store_.empty()) {
0232         std::string inputDirectory = dqmRootDirectory;
0233         //std::cout << "inputDirectory = " << inputDirectory << std::endl;
0234         std::string outputDirectory =
0235             dqmDirectoryName(std::string(inputDirectory)).append(fileSet->second.dqmDirectory_store_);
0236         //std::cout << "outputDirectory = " << outputDirectory << std::endl;
0237 
0238         dqmStore.setCurrentFolder(inputDirectory);
0239         std::vector<std::string> dirNames = dqmStore.getSubdirs();
0240         for (std::vector<std::string>::const_iterator dirName = dirNames.begin(); dirName != dirNames.end();
0241              ++dirName) {
0242           std::string subDirName = dqmSubDirectoryName_merged(inputDirectory, *dirName);
0243           //std::cout << " subDirName = " << subDirName << std::endl;
0244 
0245           const sstring& subDirectories = subDirectoryMap_[*inputFileName];
0246           if (subDirectories.find(subDirName) != subDirectories.end()) {
0247             std::string inputDirName_full = dqmDirectoryName(inputDirectory).append(subDirName);
0248             //std::cout << " inputDirName_full = " << inputDirName_full << std::endl;
0249 
0250             std::string outputDirName_full = dqmDirectoryName(outputDirectory).append(subDirName);
0251             //std::cout << " outputDirName_full = " << outputDirName_full << std::endl;
0252 
0253             //--- load histograms contained in inputFile into inputDirectory;
0254             //    when processing first inputFile, check that histograms in outputDirectory do not yet exist;
0255             //    add histograms in inputFile to those in outputDirectory afterwards;
0256             //    clear inputDirectory once finished processing all inputFiles.
0257             int mode = (inputFileName == fileSet->second.inputFileNames_.begin()) ? 1 : 3;
0258             dqmCopyRecursively(
0259                 dqmStore, inputDirName_full, outputDirName_full, fileSet->second.scaleFactor_, mode, true);
0260           }
0261         }
0262       }
0263     }
0264   }
0265 
0266   std::cout << "done." << std::endl;
0267 }
0268 
0269 #include "FWCore/Framework/interface/MakerMacros.h"
0270 
0271 DEFINE_FWK_MODULE(TauDQMFileLoader);