CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_2_9_HLT1_bphpatch4/src/Validation/RecoTau/plugins/dqmAuxFunctions.cc

Go to the documentation of this file.
00001 #include "Validation/RecoTau/plugins/dqmAuxFunctions.h"
00002 
00003 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00004 
00005 #include "DQMServices/Core/interface/MonitorElement.h"
00006 
00007 #include <TH1.h>
00008 
00009 #include <iostream>
00010 
00011 std::string replace_string(const std::string& src, const std::string& keyword, const std::string& parameter, 
00012                            unsigned minReplacements, unsigned maxReplacements, int& errorFlag)
00013 {
00014   std::string modSrc = src;
00015   unsigned numReplacements = 0;
00016   while ( modSrc.find(keyword) != std::string::npos ) {
00017     modSrc.replace(modSrc.find(keyword), keyword.length(), parameter);
00018     ++numReplacements;
00019   }
00020   if ( (numReplacements < minReplacements) ||
00021        (numReplacements > maxReplacements) ) {
00022     edm::LogError ("replace_string") << " Failed to replace parameter = " << parameter << " in src = " << src << ","
00023                                      << " numReplacements = " << numReplacements 
00024                                      << " (min = " << minReplacements << ", max = " << maxReplacements << ") !!";
00025     errorFlag = 1;
00026   }
00027   return modSrc;
00028 }
00029 
00030 //
00031 //-----------------------------------------------------------------------------------------------------------------------
00032 //
00033 
00034 std::string format_vstring(const std::vector<std::string>& vs)
00035 {
00036   std::ostringstream os;
00037   
00038   os << "{ ";
00039 
00040   unsigned numEntries = vs.size();
00041   for ( unsigned iEntry = 0; iEntry < numEntries; ++iEntry ) {
00042     os << vs[iEntry];
00043     if ( iEntry < (numEntries - 1) ) os << ", ";
00044   }
00045 
00046   os << " }";
00047   
00048   return os.str();
00049 }
00050 
00051 //
00052 //-----------------------------------------------------------------------------------------------------------------------
00053 //
00054 
00055 std::string dqmDirectoryName(const std::string& directory)
00056 {
00057   std::string dirName = directory;
00058   //if ( dirName == "" || dirName.find_last_of(dqmSeparator) != (dirName.length() - 1) )  dirName.append(dqmSeparator);
00059 //--- add tailing '/'
00060   if ( dirName != "" && dirName.find_last_of(dqmSeparator) != (dirName.length() - 1) )  dirName.append(dqmSeparator);
00061   return dirName;
00062 }
00063 
00064 std::string dqmSubDirectoryName_merged(const std::string& directory, const std::string& subdirectory)
00065 { 
00066   std::string subDirName = subdirectory;
00067 //--- remove characters specifying directory part from name of subdirectory
00068   if ( subDirName.find(directory) <= 1 ) subDirName.replace(subDirName.find(directory), directory.length(), "");
00069 //--- remove tailing '/'s
00070   while ( subDirName.find(dqmSeparator) == 0 ) subDirName.replace(subDirName.find(dqmSeparator), dqmSeparator.length(), "");
00071   return subDirName;
00072 }
00073 
00074 //
00075 //-----------------------------------------------------------------------------------------------------------------------
00076 //
00077 
00078 void dqmRegisterHistogram(DQMStore& dqmStore, TH1* histogram, const std::string& name)
00079 {
00080   //std::cout << "<dqmRegisterHistogram>:" << std::endl;
00081   //histogram->SetName(std::string(histogram->GetName()).append("_copied").data());
00082   histogram->SetName(histogram->GetName());  
00083   if ( TH1F* h = dynamic_cast<TH1F*>(histogram) ) {
00084     //std::cout << " --> calling DQMStore::book1D" << std::endl;
00085     dqmStore.book1D(name, h);
00086   } else if ( TH1S* h = dynamic_cast<TH1S*>(histogram) ) {
00087     //std::cout << " --> calling DQMStore::book1@" << std::endl;
00088     dqmStore.book1S(name, h);
00089   } else if ( TH2F* h = dynamic_cast<TH2F*>(histogram) ) {
00090     //std::cout << " --> calling DQMStore::book2D" << std::endl;
00091     dqmStore.book2D(name, h);
00092   } else if ( TH2S* h = dynamic_cast<TH2S*>(histogram) ) {
00093     //std::cout << " --> calling DQMStore::book2S" << std::endl;
00094     dqmStore.book2S(name, h);
00095   } else if ( TH3F* h = dynamic_cast<TH3F*>(histogram) ) {
00096     //std::cout << " --> calling DQMStore::book3D" << std::endl;
00097     dqmStore.book3D(name, h);
00098   } else if ( TProfile* h = dynamic_cast<TProfile*>(histogram) ) {
00099     //std::cout << " --> calling DQMStore::bookProfile" << std::endl;
00100     dqmStore.bookProfile(name, h);
00101   } else if ( TProfile2D* h = dynamic_cast<TProfile2D*>(histogram) ) {
00102     //std::cout << " --> calling DQMStore::bookProfile2D" << std::endl;
00103     dqmStore.bookProfile2D(name, h);
00104   }
00105 }
00106 
00107 void dqmCopyRecursively(DQMStore& dqmStore, const std::string& inputDirectory, const std::string& outputDirectory, 
00108                         double scaleFactor, int mode, bool rmInputDirectory)
00109 {
00110   //std::cout << "<copyRecursively>:" << std::endl;
00111   //std::cout << " inputDirectory = " << inputDirectory << std::endl;
00112   //std::cout << " outputDirectory = " << outputDirectory << std::endl;
00113   //std::cout << " rmInputDirectory = " << rmInputDirectory << std::endl;
00114 
00115   bool meInput_copied = false;
00116 
00117 //--- copy all monitor elements in current inputDirectory to the outputDirectory
00118   dqmStore.setCurrentFolder(inputDirectory);
00119   std::vector<std::string> meNames = dqmStore.getMEs();
00120   for ( std::vector<std::string>::const_iterator meName = meNames.begin();
00121         meName != meNames.end(); ++meName ) {
00122     std::string meName_full = dqmDirectoryName(inputDirectory).append(*meName);
00123     //std::cout << " meName_full = " <<  meName_full << std::endl;
00124 
00125     dqmStore.setCurrentFolder(inputDirectory);
00126     MonitorElement* meInput = dqmStore.get(meName_full);
00127     //std::cout << " meInput = " << meInput << std::endl;
00128     if ( !meInput ) {
00129       edm::LogError ("copyRecursively") << " Failed to access meName = " << (*meName) << " in DQMStore" 
00130                                         << " --> skipping !!";
00131       continue;
00132     }
00133 
00134     TH1* histogram = meInput->getTH1();
00135     //std::cout << " histogram = " << histogram << std::endl;
00136     if ( !histogram ) {
00137       edm::LogError ("copyRecursively") << " Failed to access histogram associated to meName = " << (*meName) << " in DQMStore" 
00138                                         << " --> skipping !!";
00139       continue;
00140     }
00141 
00142     std::auto_ptr<TH1> clone(dynamic_cast<TH1*>(histogram->Clone()));
00143     clone->Scale(scaleFactor);
00144 
00145     dqmStore.setCurrentFolder(outputDirectory);   
00146     MonitorElement* meOutput = dqmStore.get(dqmDirectoryName(outputDirectory).append(*meName));
00147     //std::cout << " meOutput = " << meOutput << std::endl;
00148 //--- check if outputHistogram does already exist
00149     if ( meOutput ) {
00150       switch ( mode ) {
00151       case 1: // print error message 
00152         edm::LogError ("copyRecursively") << " meName = " << (*meName) << " already exists in outputDirectory = " << outputDirectory 
00153                                           << " --> skipping !!";
00154         break;
00155       case 2: // overwrite outputHistogram
00156         dqmRegisterHistogram(dqmStore, clone.release(), *meName);
00157         break;
00158       case 3: // add histogram to outputHistogram
00159         meOutput->getTH1()->Add(clone.get(), scaleFactor);
00160       }
00161     } else {
00162       dqmRegisterHistogram(dqmStore, clone.release(), *meName);
00163     }
00164 
00165     meInput_copied = true;
00166   }
00167 
00168 //--- call function recursively for all sub-directories
00169   dqmStore.setCurrentFolder(inputDirectory);
00170   std::vector<std::string> dirNames = dqmStore.getSubdirs();
00171   for ( std::vector<std::string>::const_iterator dirName = dirNames.begin();
00172         dirName != dirNames.end(); ++dirName ) {
00173     std::string subDirName = dqmSubDirectoryName_merged(inputDirectory, *dirName);
00174     //std::cout << " subDirName = " << subDirName << std::endl;
00175 
00176     std::string inputDirName_full = dqmDirectoryName(inputDirectory).append(subDirName);
00177     //std::cout << " inputDirName_full = " << inputDirName_full << std::endl;
00178 
00179     std::string outputDirName_full = dqmDirectoryName(outputDirectory).append(subDirName);
00180     //std::cout << " outputDirName_full = " << outputDirName_full << std::endl;
00181 
00182     dqmCopyRecursively(dqmStore, inputDirName_full, outputDirName_full, scaleFactor, mode, rmInputDirectory);
00183   }
00184 
00185 //--- delete inputDirectory 
00186 //    (if requested to do so and inputDirectory is **not empty**;
00187 //     otherwise, common parent directories of inputDirectory and outputDirectory might get deleted !!)
00188   if ( rmInputDirectory && meInput_copied ) dqmStore.rmdir(inputDirectory);
00189 }
00190 
00191 //
00192 //-----------------------------------------------------------------------------------------------------------------------
00193 //
00194 
00195 void separateHistogramFromDirectoryName(const std::string& histogramAndDirectoryName, std::string& histogramName, std::string& directoryName)
00196 {
00197   //std::cout << "<separateHistogramFromDirectoryName>:" << std::endl;
00198 
00199   std::string tempName = histogramAndDirectoryName;
00200 
00201 //--- remove DQM root directory from histogram name
00202   std::string::size_type dqmRootDirectoryPos = tempName.find(dqmRootDirectory);
00203   if ( dqmRootDirectoryPos != std::string::npos ) {  
00204     tempName.replace(dqmRootDirectoryPos, dqmRootDirectory.size(), "");  
00205   }  
00206 
00207   //std::cout << " tempName = " << tempName << std::endl;
00208 
00209 //--- extract directory from histogram name
00210   std::string::size_type lastPos;
00211   std::string::size_type nextPos = tempName.find(dqmSeparator);  
00212   do {
00213     lastPos = nextPos;
00214     nextPos = tempName.find(dqmSeparator, lastPos + 1);
00215   } while ( nextPos != std::string::npos );
00216 
00217   histogramName = ( lastPos != std::string::npos ) ? std::string(tempName, lastPos + 1, tempName.length()) : tempName;
00218   directoryName = ( lastPos != std::string::npos ) ? std::string(tempName, 0, lastPos) : "";
00219 
00220   //std::cout << " histogramName = " << histogramName << std::endl;
00221   //std::cout << " directoryName = " << directoryName << std::endl;
00222 }
00223