Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:33:17

0001 #include "Validation/RecoTau/plugins/dqmAuxFunctions.h"
0002 
0003 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0004 
0005 #include "DQMServices/Core/interface/DQMStore.h"
0006 
0007 #include <TH1.h>
0008 
0009 #include <iostream>
0010 
0011 std::string replace_string(const std::string& src,
0012                            const std::string& keyword,
0013                            const std::string& parameter,
0014                            unsigned minReplacements,
0015                            unsigned maxReplacements,
0016                            int& errorFlag) {
0017   std::string modSrc = src;
0018   unsigned numReplacements = 0;
0019   while (modSrc.find(keyword) != std::string::npos) {
0020     modSrc.replace(modSrc.find(keyword), keyword.length(), parameter);
0021     ++numReplacements;
0022   }
0023   if ((numReplacements < minReplacements) || (numReplacements > maxReplacements)) {
0024     edm::LogError("replace_string") << " Failed to replace parameter = " << parameter << " in src = " << src << ","
0025                                     << " numReplacements = " << numReplacements << " (min = " << minReplacements
0026                                     << ", max = " << maxReplacements << ") !!";
0027     errorFlag = 1;
0028   }
0029   return modSrc;
0030 }
0031 
0032 //
0033 //-----------------------------------------------------------------------------------------------------------------------
0034 //
0035 
0036 std::string format_vstring(const std::vector<std::string>& vs) {
0037   std::ostringstream os;
0038 
0039   os << "{ ";
0040 
0041   unsigned numEntries = vs.size();
0042   for (unsigned iEntry = 0; iEntry < numEntries; ++iEntry) {
0043     os << vs[iEntry];
0044     if (iEntry < (numEntries - 1))
0045       os << ", ";
0046   }
0047 
0048   os << " }";
0049 
0050   return os.str();
0051 }
0052 
0053 //
0054 //-----------------------------------------------------------------------------------------------------------------------
0055 //
0056 
0057 std::string dqmDirectoryName(const std::string& directory) {
0058   std::string dirName = directory;
0059   //if ( dirName == "" || dirName.find_last_of(dqmSeparator) != (dirName.length() - 1) )  dirName.append(dqmSeparator);
0060   //--- add tailing '/'
0061   if (!dirName.empty() && dirName.find_last_of(dqmSeparator) != (dirName.length() - 1))
0062     dirName.append(dqmSeparator);
0063   return dirName;
0064 }
0065 
0066 std::string dqmSubDirectoryName_merged(const std::string& directory, const std::string& subdirectory) {
0067   std::string subDirName = subdirectory;
0068   //--- remove characters specifying directory part from name of subdirectory
0069   if (subDirName.find(directory) <= 1)
0070     subDirName.replace(subDirName.find(directory), directory.length(), "");
0071   //--- remove tailing '/'s
0072   while (subDirName.find(dqmSeparator) == 0)
0073     subDirName.replace(subDirName.find(dqmSeparator), dqmSeparator.length(), "");
0074   return subDirName;
0075 }
0076 
0077 //
0078 //-----------------------------------------------------------------------------------------------------------------------
0079 //
0080 
0081 void dqmRegisterHistogram(dqm::legacy::DQMStore& dqmStore, TH1* histogram, const std::string& name) {
0082   //std::cout << "<dqmRegisterHistogram>:" << std::endl;
0083   //histogram->SetName(std::string(histogram->GetName()).append("_copied").data());
0084   histogram->SetName(histogram->GetName());
0085   if (TH1F* h = dynamic_cast<TH1F*>(histogram)) {
0086     //std::cout << " --> calling DQMStore::book1D" << std::endl;
0087     dqmStore.book1D(name, h);
0088   } else if (TH1S* h = dynamic_cast<TH1S*>(histogram)) {
0089     //std::cout << " --> calling DQMStore::book1@" << std::endl;
0090     dqmStore.book1S(name, h);
0091   } else if (TH2F* h = dynamic_cast<TH2F*>(histogram)) {
0092     //std::cout << " --> calling DQMStore::book2D" << std::endl;
0093     dqmStore.book2D(name, h);
0094   } else if (TH2S* h = dynamic_cast<TH2S*>(histogram)) {
0095     //std::cout << " --> calling DQMStore::book2S" << std::endl;
0096     dqmStore.book2S(name, h);
0097   } else if (TH3F* h = dynamic_cast<TH3F*>(histogram)) {
0098     //std::cout << " --> calling DQMStore::book3D" << std::endl;
0099     dqmStore.book3D(name, h);
0100   } else if (TProfile* h = dynamic_cast<TProfile*>(histogram)) {
0101     //std::cout << " --> calling DQMStore::bookProfile" << std::endl;
0102     dqmStore.bookProfile(name, h);
0103   } else if (TProfile2D* h = dynamic_cast<TProfile2D*>(histogram)) {
0104     //std::cout << " --> calling DQMStore::bookProfile2D" << std::endl;
0105     dqmStore.bookProfile2D(name, h);
0106   }
0107 }
0108 
0109 void dqmCopyRecursively(dqm::legacy::DQMStore& dqmStore,
0110                         const std::string& inputDirectory,
0111                         const std::string& outputDirectory,
0112                         double scaleFactor,
0113                         int mode,
0114                         bool rmInputDirectory) {
0115   //std::cout << "<copyRecursively>:" << std::endl;
0116   //std::cout << " inputDirectory = " << inputDirectory << std::endl;
0117   //std::cout << " outputDirectory = " << outputDirectory << std::endl;
0118   //std::cout << " rmInputDirectory = " << rmInputDirectory << std::endl;
0119 
0120   //--- copy all monitor elements in current inputDirectory to the outputDirectory
0121   dqmStore.setCurrentFolder(inputDirectory);
0122   std::vector<std::string> meNames = dqmStore.getMEs();
0123   for (std::vector<std::string>::const_iterator meName = meNames.begin(); meName != meNames.end(); ++meName) {
0124     std::string meName_full = dqmDirectoryName(inputDirectory).append(*meName);
0125     //std::cout << " meName_full = " <<  meName_full << std::endl;
0126 
0127     dqmStore.setCurrentFolder(inputDirectory);
0128     dqm::legacy::MonitorElement* meInput = dqmStore.get(meName_full);
0129     //std::cout << " meInput = " << meInput << std::endl;
0130     if (!meInput) {
0131       edm::LogError("copyRecursively") << " Failed to access meName = " << (*meName) << " in DQMStore"
0132                                        << " --> skipping !!";
0133       continue;
0134     }
0135 
0136     TH1* histogram = meInput->getTH1();
0137     //std::cout << " histogram = " << histogram << std::endl;
0138     if (!histogram) {
0139       edm::LogError("copyRecursively") << " Failed to access histogram associated to meName = " << (*meName)
0140                                        << " in DQMStore"
0141                                        << " --> skipping !!";
0142       continue;
0143     }
0144 
0145     std::unique_ptr<TH1> clone(dynamic_cast<TH1*>(histogram->Clone()));
0146     clone->Scale(scaleFactor);
0147 
0148     dqmStore.setCurrentFolder(outputDirectory);
0149     dqm::legacy::MonitorElement* meOutput = dqmStore.get(dqmDirectoryName(outputDirectory).append(*meName));
0150     //std::cout << " meOutput = " << meOutput << std::endl;
0151     //--- check if outputHistogram does already exist
0152     if (meOutput) {
0153       switch (mode) {
0154         case 1:  // print error message
0155           edm::LogError("copyRecursively")
0156               << " meName = " << (*meName) << " already exists in outputDirectory = " << outputDirectory
0157               << " --> skipping !!";
0158           break;
0159         case 2:  // overwrite outputHistogram
0160           dqmRegisterHistogram(dqmStore, clone.release(), *meName);
0161           break;
0162         case 3:  // add histogram to outputHistogram
0163           meOutput->getTH1()->Add(clone.get(), scaleFactor);
0164       }
0165     } else {
0166       dqmRegisterHistogram(dqmStore, clone.release(), *meName);
0167     }
0168   }
0169 
0170   //--- call function recursively for all sub-directories
0171   dqmStore.setCurrentFolder(inputDirectory);
0172   std::vector<std::string> dirNames = dqmStore.getSubdirs();
0173   for (std::vector<std::string>::const_iterator dirName = dirNames.begin(); dirName != dirNames.end(); ++dirName) {
0174     std::string subDirName = dqmSubDirectoryName_merged(inputDirectory, *dirName);
0175     //std::cout << " subDirName = " << subDirName << std::endl;
0176 
0177     std::string inputDirName_full = dqmDirectoryName(inputDirectory).append(subDirName);
0178     //std::cout << " inputDirName_full = " << inputDirName_full << std::endl;
0179 
0180     std::string outputDirName_full = dqmDirectoryName(outputDirectory).append(subDirName);
0181     //std::cout << " outputDirName_full = " << outputDirName_full << std::endl;
0182 
0183     dqmCopyRecursively(dqmStore, inputDirName_full, outputDirName_full, scaleFactor, mode, rmInputDirectory);
0184   }
0185 }
0186 
0187 //
0188 //-----------------------------------------------------------------------------------------------------------------------
0189 //
0190 
0191 void separateHistogramFromDirectoryName(const std::string& histogramAndDirectoryName,
0192                                         std::string& histogramName,
0193                                         std::string& directoryName) {
0194   //std::cout << "<separateHistogramFromDirectoryName>:" << std::endl;
0195 
0196   std::string tempName = histogramAndDirectoryName;
0197 
0198   //--- remove DQM root directory from histogram name
0199   std::string::size_type dqmRootDirectoryPos = tempName.find(dqmRootDirectory);
0200   if (dqmRootDirectoryPos != std::string::npos) {
0201     tempName.replace(dqmRootDirectoryPos, dqmRootDirectory.size(), "");
0202   }
0203 
0204   //std::cout << " tempName = " << tempName << std::endl;
0205 
0206   //--- extract directory from histogram name
0207   std::string::size_type lastPos;
0208   std::string::size_type nextPos = tempName.find(dqmSeparator);
0209   do {
0210     lastPos = nextPos;
0211     nextPos = tempName.find(dqmSeparator, lastPos + 1);
0212   } while (nextPos != std::string::npos);
0213 
0214   histogramName = (lastPos != std::string::npos) ? std::string(tempName, lastPos + 1, tempName.length()) : tempName;
0215   directoryName = (lastPos != std::string::npos) ? std::string(tempName, 0, lastPos) : "";
0216 
0217   //std::cout << " histogramName = " << histogramName << std::endl;
0218   //std::cout << " directoryName = " << directoryName << std::endl;
0219 }