CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10_patch2/src/Validation/RecoTau/plugins/DQMFileLoader.cc

Go to the documentation of this file.
00001 #include "Validation/RecoTau/plugins/DQMFileLoader.h"
00002 
00003 #include "Validation/RecoTau/plugins/dqmAuxFunctions.h"
00004 
00005 // framework & common header files
00006 #include "FWCore/Framework/interface/Frameworkfwd.h"
00007 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00008 
00009 //DQM services
00010 #include "DQMServices/Core/interface/DQMStore.h"
00011 #include "FWCore/ServiceRegistry/interface/Service.h"
00012 #include "DQMServices/Core/interface/MonitorElement.h"
00013 
00014 #include <TFile.h>
00015 #include <TList.h>
00016 #include <TKey.h>
00017 #include <TH1.h>
00018 
00019 #include <iostream>
00020 
00021 const std::string dqmRootDirectory_inTFile = "DQMData";
00022 
00023 const double defaultScaleFactor = 1.;
00024 
00025 const int verbosity = 0;
00026 
00027 void mapSubDirectoryStructure(TDirectory* directory, std::string directoryName, std::set<std::string>& subDirectories)
00028 {
00029   //std::cout << "<mapSubDirectoryStructure>:" << std::endl;
00030   //std::cout << " directoryName = " << directoryName << std::endl;
00031 
00032   TList* subDirectoryNames = directory->GetListOfKeys();
00033   if ( !subDirectoryNames ) return;
00034 
00035   TIter next(subDirectoryNames);
00036   while ( TKey* key = dynamic_cast<TKey*>(next()) ) {
00037     //std::cout << " key->GetName = " << key->GetName() << std::endl;
00038     TObject* obj = directory->Get(key->GetName());
00039     //std::cout << " obj = " << obj << std::endl;
00040     if ( TDirectory* subDirectory = dynamic_cast<TDirectory*>(obj) ) {
00041       std::string subDirectoryName = dqmDirectoryName(directoryName).append(key->GetName());
00042       //std::cout << " subDirectoryName = " << subDirectoryName << std::endl;
00043     
00044       subDirectories.insert(subDirectoryName);
00045     
00046       mapSubDirectoryStructure(subDirectory, subDirectoryName, subDirectories); 
00047     }
00048   }
00049 } 
00050 
00051 //
00052 //-----------------------------------------------------------------------------------------------------------------------
00053 //
00054 
00055 TauDQMFileLoader::cfgEntryFileSet::cfgEntryFileSet(const std::string& name, const edm::ParameterSet& cfg)
00056 {
00057   //std::cout << "<TauDQMFileLoader::cfgEntryFileSet>" << std::endl;
00058 
00059   name_ = name;
00060 
00061   vstring inputFileList = cfg.getParameter<vstring>("inputFileNames");
00062   for ( vstring::const_iterator inputFile = inputFileList.begin();
00063         inputFile != inputFileList.end(); ++inputFile ) {
00064     if ( inputFile->find(rangeKeyword) != std::string::npos ) {
00065       size_t posRangeStart = inputFile->find(rangeKeyword) + rangeKeyword.length();
00066       size_t posRangeEnd = inputFile->find('#', posRangeStart); 
00067       
00068       size_t posRangeSeparator = inputFile->find('-', posRangeStart);
00069 
00070       if ( (posRangeEnd == std::string::npos) || 
00071            (posRangeSeparator >= posRangeEnd) ) { 
00072         edm::LogError ("TauDQMFileLoader::cfgEntryFileSet") << " Invalid range specification in inputFile = " << (*inputFile) << " !!";
00073         continue;
00074       }
00075 
00076       std::string firstFile = std::string(*inputFile, posRangeStart, posRangeSeparator - posRangeStart);
00077       //std::cout << "firstFile = " << firstFile << std::endl;
00078       std::string lastFile = std::string(*inputFile, posRangeSeparator + 1, posRangeEnd - (posRangeSeparator + 1));
00079       //std::cout << "lastFile = " << lastFile << std::endl;
00080 
00081       if ( firstFile.length() != lastFile.length() ) {
00082         edm::LogError ("TauDQMFileLoader::cfgEntryFileSet") << " Invalid range specification in inputFile = " << (*inputFile) << " !!";
00083         continue;
00084       }
00085 
00086       int numFirstFile = atoi(firstFile.data());
00087       int numLastFile = atoi(lastFile.data());
00088       for ( int iFile = numFirstFile; iFile <= numLastFile; ++iFile ) {
00089         std::ostringstream fileName;
00090         fileName << std::string(*inputFile, 0, inputFile->find(rangeKeyword));
00091         fileName << std::setfill('0') << std::setw(firstFile.length()) << iFile;
00092         fileName << std::string(*inputFile, posRangeEnd + 1);
00093         //std::cout << "iFile = " << iFile << ", fileName = " << fileName.str() << std::endl;
00094         inputFileNames_.push_back(fileName.str());
00095       }
00096     } else {
00097       inputFileNames_.push_back(*inputFile);
00098     }
00099   }
00100 
00101   scaleFactor_ = ( cfg.exists("scaleFactor") ) ? cfg.getParameter<double>("scaleFactor") : defaultScaleFactor;
00102 
00103   //dqmDirectory_store_ = ( cfg.exists("dqmDirectory_store") ) ? cfg.getParameter<std::string>("dqmDirectory_store") : name_;
00104   dqmDirectory_store_ = ( cfg.exists("dqmDirectory_store") ) ? cfg.getParameter<std::string>("dqmDirectory_store") : "";
00105 
00106   if ( verbosity ) print();
00107 }
00108 
00109 void TauDQMFileLoader::cfgEntryFileSet::print() const
00110 {
00111   std::cout << "<cfgEntryFileSet::print>:" << std::endl;
00112   std::cout << " name = " << name_ << std::endl;
00113   std::cout << " inputFileNames = " << format_vstring(inputFileNames_) << std::endl;
00114   std::cout << " scaleFactor = " << scaleFactor_ << std::endl;
00115   std::cout << " dqmDirectory_store = " << dqmDirectory_store_ << std::endl;
00116 }
00117 
00118 //
00119 //-----------------------------------------------------------------------------------------------------------------------
00120 //
00121 
00122 TauDQMFileLoader::TauDQMFileLoader(const edm::ParameterSet& cfg)
00123 {
00124   std::cout << "<TauDQMFileLoader::TauDQMFileLoader>:" << std::endl;
00125 
00126   cfgError_ = 0;
00127 
00128 //--- configure fileSets  
00129   //std::cout << "--> configuring fileSets..." << std::endl;
00130   readCfgParameter<cfgEntryFileSet>(cfg, fileSets_);
00131   
00132 //--- check that dqmDirectory_store configuration parameters are specified for all fileSets,
00133 //    unless there is only one fileSet to be loaded
00134 //    (otherwise histograms of different fileSets get overwritten,
00135 //     once histograms of the next fileSet are loaded)
00136   for ( std::map<std::string, cfgEntryFileSet>::const_iterator fileSet = fileSets_.begin();
00137         fileSet != fileSets_.end(); ++fileSet ) {
00138     if ( fileSet->second.dqmDirectory_store_ == "" && fileSets_.size() > 1 ) {
00139       edm::LogError ("TauDQMFileLoader") << " dqmDirectory_store undefined for fileSet = " << fileSet->second.name_ << " !!";
00140       cfgError_ = 1;
00141       break;
00142     }
00143   }
00144 
00145   std::cout << "done." << std::endl;
00146 }
00147 
00148 TauDQMFileLoader::~TauDQMFileLoader() 
00149 {
00150 // nothing to be done yet...
00151 }
00152 
00153 void TauDQMFileLoader::analyze(const edm::Event&, const edm::EventSetup&)
00154 {
00155 // nothing to be done yet...
00156 }
00157 
00158 void TauDQMFileLoader::endRun(const edm::Run& r, const edm::EventSetup& c)
00159 {
00160   std::cout << "<TauDQMFileLoader::endJob>:" << std::endl;
00161 
00162 //--- check that configuration parameters contain no errors
00163   if ( cfgError_ ) {
00164     edm::LogError ("endJob") << " Error in Configuration ParameterSet" 
00165                              << " --> histograms will NOT be loaded !!";
00166     return;
00167   }
00168 
00169 //--- check that DQMStore service is available
00170   if ( !edm::Service<DQMStore>().isAvailable() ) {
00171     edm::LogError ("endJob") << " Failed to access dqmStore" 
00172                              << " --> histograms will NOT be loaded !!";
00173     return;
00174   }
00175 
00176 //--- stop ROOT from keeping references to all histograms
00177   //TH1::AddDirectory(false);
00178 
00179 //--- check that inputFiles exist;
00180 //    store list of directories existing in inputFile,
00181 //    in order to separate histogram directories existing in the inputFile from directories existing in DQMStore
00182 //    when calling recursive function dqmCopyRecursively
00183   for ( std::map<std::string, cfgEntryFileSet>::const_iterator fileSet = fileSets_.begin();
00184         fileSet != fileSets_.end(); ++fileSet ) {
00185     for ( vstring::const_iterator inputFileName = fileSet->second.inputFileNames_.begin();
00186           inputFileName != fileSet->second.inputFileNames_.end(); ++inputFileName ) {
00187       //std::cout << " checking inputFile = " << (*inputFileName) << std::endl;
00188       TFile inputFile(inputFileName->data());
00189       if ( inputFile.IsZombie() ) {
00190         edm::LogError ("endJob") << " Failed to open inputFile = " << (*inputFileName) 
00191                                  << "--> histograms will NOT be loaded !!";
00192         return;
00193       }
00194  
00195       TObject* obj = inputFile.Get(dqmRootDirectory_inTFile.data());
00196       //std::cout << " obj = " << obj << std::endl;
00197       if ( TDirectory* directory = dynamic_cast<TDirectory*>(obj) ) {
00198         mapSubDirectoryStructure(directory, dqmRootDirectory, subDirectoryMap_[*inputFileName]);
00199       } else {
00200         edm::LogError ("endJob") << " Failed to access " << dqmRootDirectory_inTFile << " in inputFile = " << (*inputFileName) 
00201                                  << "--> histograms will NOT be loaded !!";
00202         return;
00203       }
00204 
00205       inputFile.Close();
00206     }
00207   }
00208   
00209   //for ( std::map<std::string, sstring>::const_iterator inputFile = subDirectoryMap_.begin();
00210   //      inputFile != subDirectoryMap_.end(); ++inputFile ) {
00211   //  std::cout << "inputFile = " << inputFile->first << ":" << std::endl;
00212   //  for ( sstring::const_iterator directory = inputFile->second.begin();
00213   //        directory != inputFile->second.end(); ++directory ) {
00214   //    std::cout << " " << (*directory) << std::endl;
00215   //  }
00216   //}
00217 
00218 //--- load histograms from file
00219   //std::cout << "--> loading histograms from file..." << std::endl;
00220   DQMStore& dqmStore = (*edm::Service<DQMStore>());
00221   for ( std::map<std::string, cfgEntryFileSet>::const_iterator fileSet = fileSets_.begin();
00222         fileSet != fileSets_.end(); ++fileSet ) {
00223     for ( vstring::const_iterator inputFileName = fileSet->second.inputFileNames_.begin();
00224           inputFileName != fileSet->second.inputFileNames_.end(); ++inputFileName ) {
00225       if ( verbosity ) std::cout << " opening inputFile = " << (*inputFileName) << std::endl;
00226       dqmStore.open(*inputFileName, true);
00227 
00228       //if ( verbosity ) dqmStore.showDirStructure();
00229       
00230 //--- if dqmDirectory_store specified in configuration parameters,
00231 //    move histograms from dqmRootDirectory to dqmDirectory_store
00232 //    (if the histograms are not moved, the histograms get overwritten,
00233 //     the next time DQMStore::open is called)
00234       if ( fileSet->second.dqmDirectory_store_ != "" ) {
00235         std::string inputDirectory = dqmRootDirectory;
00236         //std::cout << "inputDirectory = " << inputDirectory << std::endl;
00237         std::string outputDirectory = dqmDirectoryName(std::string(inputDirectory)).append(fileSet->second.dqmDirectory_store_);
00238         //std::cout << "outputDirectory = " << outputDirectory << std::endl;
00239 
00240         dqmStore.setCurrentFolder(inputDirectory);
00241         std::vector<std::string> dirNames = dqmStore.getSubdirs();
00242         for ( std::vector<std::string>::const_iterator dirName = dirNames.begin();
00243               dirName != dirNames.end(); ++dirName ) {
00244           std::string subDirName = dqmSubDirectoryName_merged(inputDirectory, *dirName);
00245           //std::cout << " subDirName = " << subDirName << std::endl;
00246           
00247           const sstring& subDirectories = subDirectoryMap_[*inputFileName];
00248           if ( subDirectories.find(subDirName) != subDirectories.end() ) {
00249             std::string inputDirName_full = dqmDirectoryName(inputDirectory).append(subDirName);
00250             //std::cout << " inputDirName_full = " << inputDirName_full << std::endl;
00251             
00252             std::string outputDirName_full = dqmDirectoryName(outputDirectory).append(subDirName);
00253             //std::cout << " outputDirName_full = " << outputDirName_full << std::endl;
00254 
00255 //--- load histograms contained in inputFile into inputDirectory;
00256 //    when processing first inputFile, check that histograms in outputDirectory do not yet exist;
00257 //    add histograms in inputFile to those in outputDirectory afterwards;
00258 //    clear inputDirectory once finished processing all inputFiles.
00259             int mode = ( inputFileName == fileSet->second.inputFileNames_.begin() ) ? 1 : 3;
00260             dqmCopyRecursively(dqmStore, inputDirName_full, outputDirName_full, fileSet->second.scaleFactor_, mode, true);
00261           }
00262         }
00263       }
00264     }
00265   }
00266 
00267   std::cout << "done." << std::endl; 
00268   if ( verbosity ) dqmStore.showDirStructure();
00269 }
00270 
00271 #include "FWCore/Framework/interface/MakerMacros.h"
00272 
00273 DEFINE_FWK_MODULE(TauDQMFileLoader);