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
0006 #include "FWCore/Framework/interface/Frameworkfwd.h"
0007 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0008
0009
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
0028
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
0037 TObject* obj = directory->Get(key->GetName());
0038
0039 if (TDirectory* subDirectory = dynamic_cast<TDirectory*>(obj)) {
0040 std::string subDirectoryName = dqmDirectoryName(directoryName).append(key->GetName());
0041
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
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
0075 std::string lastFile = std::string(*inputFile, posRangeSeparator + 1, posRangeEnd - (posRangeSeparator + 1));
0076
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
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
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
0127
0128 readCfgParameter<cfgEntryFileSet>(cfg, fileSets_);
0129
0130
0131
0132
0133
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
0149 }
0150
0151 void TauDQMFileLoader::analyze(const edm::Event&, const edm::EventSetup&) {
0152
0153 }
0154
0155 void TauDQMFileLoader::endRun(const edm::Run& r, const edm::EventSetup& c) {
0156 std::cout << "<TauDQMFileLoader::endJob>:" << std::endl;
0157
0158
0159 if (cfgError_) {
0160 edm::LogError("endJob") << " Error in Configuration ParameterSet"
0161 << " --> histograms will NOT be loaded !!";
0162 return;
0163 }
0164
0165
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
0173
0174
0175
0176
0177
0178
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
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
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
0207
0208
0209
0210
0211
0212
0213
0214
0215
0216
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
0228
0229
0230
0231 if (!fileSet->second.dqmDirectory_store_.empty()) {
0232 std::string inputDirectory = dqmRootDirectory;
0233
0234 std::string outputDirectory =
0235 dqmDirectoryName(std::string(inputDirectory)).append(fileSet->second.dqmDirectory_store_);
0236
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
0244
0245 const sstring& subDirectories = subDirectoryMap_[*inputFileName];
0246 if (subDirectories.find(subDirName) != subDirectories.end()) {
0247 std::string inputDirName_full = dqmDirectoryName(inputDirectory).append(subDirName);
0248
0249
0250 std::string outputDirName_full = dqmDirectoryName(outputDirectory).append(subDirName);
0251
0252
0253
0254
0255
0256
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);