CMS 3D CMS Logo

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