CMS 3D CMS Logo

DQMFileLoader.cc
Go to the documentation of this file.
2 
4 
5 // framework & common header files
8 
9 //DQM services
12 
13 #include <TFile.h>
14 #include <TList.h>
15 #include <TKey.h>
16 #include <TH1.h>
17 
18 #include <iostream>
19 
21 
22 const double defaultScaleFactor = 1.;
23 
24 const int verbosity = 0;
25 
26 void mapSubDirectoryStructure(TDirectory* directory, std::string directoryName, std::set<std::string>& subDirectories) {
27  //std::cout << "<mapSubDirectoryStructure>:" << std::endl;
28  //std::cout << " directoryName = " << directoryName << std::endl;
29 
30  TList* subDirectoryNames = directory->GetListOfKeys();
31  if (!subDirectoryNames)
32  return;
33 
34  TIter next(subDirectoryNames);
35  while (TKey* key = dynamic_cast<TKey*>(next())) {
36  //std::cout << " key->GetName = " << key->GetName() << std::endl;
37  TObject* obj = directory->Get(key->GetName());
38  //std::cout << " obj = " << obj << std::endl;
39  if (TDirectory* subDirectory = dynamic_cast<TDirectory*>(obj)) {
40  std::string subDirectoryName = dqmDirectoryName(directoryName).append(key->GetName());
41  //std::cout << " subDirectoryName = " << subDirectoryName << std::endl;
42 
43  subDirectories.insert(subDirectoryName);
44 
45  mapSubDirectoryStructure(subDirectory, subDirectoryName, subDirectories);
46  }
47  }
48 }
49 
50 //
51 //-----------------------------------------------------------------------------------------------------------------------
52 //
53 
55  //std::cout << "<TauDQMFileLoader::cfgEntryFileSet>" << std::endl;
56 
57  name_ = name;
58 
59  vstring inputFileList = cfg.getParameter<vstring>("inputFileNames");
60  for (vstring::const_iterator inputFile = inputFileList.begin(); inputFile != inputFileList.end(); ++inputFile) {
61  if (inputFile->find(rangeKeyword) != std::string::npos) {
62  size_t posRangeStart = inputFile->find(rangeKeyword) + rangeKeyword.length();
63  size_t posRangeEnd = inputFile->find('#', posRangeStart);
64 
65  size_t posRangeSeparator = inputFile->find('-', posRangeStart);
66 
67  if ((posRangeEnd == std::string::npos) || (posRangeSeparator >= posRangeEnd)) {
68  edm::LogError("TauDQMFileLoader::cfgEntryFileSet")
69  << " Invalid range specification in inputFile = " << (*inputFile) << " !!";
70  continue;
71  }
72 
73  std::string firstFile = std::string(*inputFile, posRangeStart, posRangeSeparator - posRangeStart);
74  //std::cout << "firstFile = " << firstFile << std::endl;
75  std::string lastFile = std::string(*inputFile, posRangeSeparator + 1, posRangeEnd - (posRangeSeparator + 1));
76  //std::cout << "lastFile = " << lastFile << std::endl;
77 
78  if (firstFile.length() != lastFile.length()) {
79  edm::LogError("TauDQMFileLoader::cfgEntryFileSet")
80  << " Invalid range specification in inputFile = " << (*inputFile) << " !!";
81  continue;
82  }
83 
84  int numFirstFile = atoi(firstFile.data());
85  int numLastFile = atoi(lastFile.data());
86  for (int iFile = numFirstFile; iFile <= numLastFile; ++iFile) {
87  std::ostringstream fileName;
89  fileName << std::setfill('0') << std::setw(firstFile.length()) << iFile;
90  fileName << std::string(*inputFile, posRangeEnd + 1);
91  //std::cout << "iFile = " << iFile << ", fileName = " << fileName.str() << std::endl;
92  inputFileNames_.push_back(fileName.str());
93  }
94  } else {
95  inputFileNames_.push_back(*inputFile);
96  }
97  }
98 
99  scaleFactor_ = (cfg.exists("scaleFactor")) ? cfg.getParameter<double>("scaleFactor") : defaultScaleFactor;
100 
101  //dqmDirectory_store_ = ( cfg.exists("dqmDirectory_store") ) ? cfg.getParameter<std::string>("dqmDirectory_store") : name_;
102  dqmDirectory_store_ = (cfg.exists("dqmDirectory_store")) ? cfg.getParameter<std::string>("dqmDirectory_store") : "";
103 
104  if (verbosity)
105  print();
106 }
107 
109  std::cout << "<cfgEntryFileSet::print>:" << std::endl;
110  std::cout << " name = " << name_ << std::endl;
111  std::cout << " inputFileNames = " << format_vstring(inputFileNames_) << std::endl;
112  std::cout << " scaleFactor = " << scaleFactor_ << std::endl;
113  std::cout << " dqmDirectory_store = " << dqmDirectory_store_ << std::endl;
114 }
115 
116 //
117 //-----------------------------------------------------------------------------------------------------------------------
118 //
119 
121  usesResource("DQMStore");
122  std::cout << "<TauDQMFileLoader::TauDQMFileLoader>:" << std::endl;
123 
124  cfgError_ = 0;
125 
126  //--- configure fileSets
127  //std::cout << "--> configuring fileSets..." << std::endl;
128  readCfgParameter<cfgEntryFileSet>(cfg, fileSets_);
129 
130  //--- check that dqmDirectory_store configuration parameters are specified for all fileSets,
131  // unless there is only one fileSet to be loaded
132  // (otherwise histograms of different fileSets get overwritten,
133  // once histograms of the next fileSet are loaded)
134  for (std::map<std::string, cfgEntryFileSet>::const_iterator fileSet = fileSets_.begin(); fileSet != fileSets_.end();
135  ++fileSet) {
136  if (fileSet->second.dqmDirectory_store_.empty() && fileSets_.size() > 1) {
137  edm::LogError("TauDQMFileLoader") << " dqmDirectory_store undefined for fileSet = " << fileSet->second.name_
138  << " !!";
139  cfgError_ = 1;
140  break;
141  }
142  }
143 
144  std::cout << "done." << std::endl;
145 }
146 
148  // nothing to be done yet...
149 }
150 
152  // nothing to be done yet...
153 }
154 
156  std::cout << "<TauDQMFileLoader::endJob>:" << std::endl;
157 
158  //--- check that configuration parameters contain no errors
159  if (cfgError_) {
160  edm::LogError("endJob") << " Error in Configuration ParameterSet"
161  << " --> histograms will NOT be loaded !!";
162  return;
163  }
164 
165  //--- check that DQMStore service is available
166  if (!edm::Service<DQMStore>().isAvailable()) {
167  edm::LogError("endJob") << " Failed to access dqmStore"
168  << " --> histograms will NOT be loaded !!";
169  return;
170  }
171 
172  //--- stop ROOT from keeping references to all histograms
173  //TH1::AddDirectory(false);
174 
175  //--- check that inputFiles exist;
176  // store list of directories existing in inputFile,
177  // in order to separate histogram directories existing in the inputFile from directories existing in DQMStore
178  // when calling recursive function dqmCopyRecursively
179  for (std::map<std::string, cfgEntryFileSet>::const_iterator fileSet = fileSets_.begin(); fileSet != fileSets_.end();
180  ++fileSet) {
181  for (vstring::const_iterator inputFileName = fileSet->second.inputFileNames_.begin();
182  inputFileName != fileSet->second.inputFileNames_.end();
183  ++inputFileName) {
184  //std::cout << " checking inputFile = " << (*inputFileName) << std::endl;
185  TFile inputFile(inputFileName->data());
186  if (inputFile.IsZombie()) {
187  edm::LogError("endJob") << " Failed to open inputFile = " << (*inputFileName)
188  << "--> histograms will NOT be loaded !!";
189  return;
190  }
191 
192  TObject* obj = inputFile.Get(dqmRootDirectory_inTFile.data());
193  //std::cout << " obj = " << obj << std::endl;
194  if (TDirectory* directory = dynamic_cast<TDirectory*>(obj)) {
196  } else {
197  edm::LogError("endJob") << " Failed to access " << dqmRootDirectory_inTFile
198  << " in inputFile = " << (*inputFileName) << "--> histograms will NOT be loaded !!";
199  return;
200  }
201 
202  inputFile.Close();
203  }
204  }
205 
206  //for ( std::map<std::string, sstring>::const_iterator inputFile = subDirectoryMap_.begin();
207  // inputFile != subDirectoryMap_.end(); ++inputFile ) {
208  // std::cout << "inputFile = " << inputFile->first << ":" << std::endl;
209  // for ( sstring::const_iterator directory = inputFile->second.begin();
210  // directory != inputFile->second.end(); ++directory ) {
211  // std::cout << " " << (*directory) << std::endl;
212  // }
213  //}
214 
215  //--- load histograms from file
216  //std::cout << "--> loading histograms from file..." << std::endl;
218  for (std::map<std::string, cfgEntryFileSet>::const_iterator fileSet = fileSets_.begin(); fileSet != fileSets_.end();
219  ++fileSet) {
220  for (vstring::const_iterator inputFileName = fileSet->second.inputFileNames_.begin();
221  inputFileName != fileSet->second.inputFileNames_.end();
222  ++inputFileName) {
223  if (verbosity)
224  std::cout << " opening inputFile = " << (*inputFileName) << std::endl;
225  dqmStore.open(*inputFileName, true);
226 
227  //--- if dqmDirectory_store specified in configuration parameters,
228  // move histograms from dqmRootDirectory to dqmDirectory_store
229  // (if the histograms are not moved, the histograms get overwritten,
230  // the next time DQMStore::open is called)
231  if (!fileSet->second.dqmDirectory_store_.empty()) {
232  std::string inputDirectory = dqmRootDirectory;
233  //std::cout << "inputDirectory = " << inputDirectory << std::endl;
234  std::string outputDirectory =
235  dqmDirectoryName(std::string(inputDirectory)).append(fileSet->second.dqmDirectory_store_);
236  //std::cout << "outputDirectory = " << outputDirectory << std::endl;
237 
238  dqmStore.setCurrentFolder(inputDirectory);
239  std::vector<std::string> dirNames = dqmStore.getSubdirs();
240  for (std::vector<std::string>::const_iterator dirName = dirNames.begin(); dirName != dirNames.end();
241  ++dirName) {
242  std::string subDirName = dqmSubDirectoryName_merged(inputDirectory, *dirName);
243  //std::cout << " subDirName = " << subDirName << std::endl;
244 
245  const sstring& subDirectories = subDirectoryMap_[*inputFileName];
246  if (subDirectories.find(subDirName) != subDirectories.end()) {
247  std::string inputDirName_full = dqmDirectoryName(inputDirectory).append(subDirName);
248  //std::cout << " inputDirName_full = " << inputDirName_full << std::endl;
249 
250  std::string outputDirName_full = dqmDirectoryName(outputDirectory).append(subDirName);
251  //std::cout << " outputDirName_full = " << outputDirName_full << std::endl;
252 
253  //--- load histograms contained in inputFile into inputDirectory;
254  // when processing first inputFile, check that histograms in outputDirectory do not yet exist;
255  // add histograms in inputFile to those in outputDirectory afterwards;
256  // clear inputDirectory once finished processing all inputFiles.
257  int mode = (inputFileName == fileSet->second.inputFileNames_.begin()) ? 1 : 3;
259  dqmStore, inputDirName_full, outputDirName_full, fileSet->second.scaleFactor_, mode, true);
260  }
261  }
262  }
263  }
264  }
265 
266  std::cout << "done." << std::endl;
267 }
268 
270 
const double defaultScaleFactor
std::set< std::string > sstring
Definition: DQMFileLoader.h:29
Log< level::Error, false > LogError
std::string dqmDirectoryName(const std::string &dqmRootDirectory, const std::string &dqmSubDirectory)
Definition: EwkTauDQM.cc:10
const std::string rangeKeyword
const std::string dqmRootDirectory_inTFile
std::string dqmSubDirectoryName_merged(const std::string &directory, const std::string &subdirectory)
std::map< std::string, sstring > subDirectoryMap_
Definition: DQMFileLoader.h:49
void endRun(const edm::Run &r, const edm::EventSetup &c) override
std::vector< std::string > vstring
Definition: DQMFileLoader.h:28
void dqmCopyRecursively(dqm::legacy::DQMStore &dqmStore, const std::string &inputDirectory, const std::string &outputDirectory, double scaleFactor, int mode, bool rmInputDirectory)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
std::string format_vstring(const std::vector< std::string > &vs)
const int verbosity
cfgEntryFileSet(const std::string &, const edm::ParameterSet &)
std::map< std::string, cfgEntryFileSet > fileSets_
Definition: DQMFileLoader.h:48
~TauDQMFileLoader() override
TauDQMFileLoader(const edm::ParameterSet &)
const std::string dqmRootDirectory
void mapSubDirectoryStructure(TDirectory *directory, std::string directoryName, std::set< std::string > &subDirectories)
Definition: Run.h:45
void analyze(const edm::Event &, const edm::EventSetup &) override