CMS 3D CMS Logo

dqmAuxFunctions.cc
Go to the documentation of this file.
2 
4 
6 
7 #include <TH1.h>
8 
9 #include <iostream>
10 
12  const std::string& keyword,
13  const std::string& parameter,
14  unsigned minReplacements,
15  unsigned maxReplacements,
16  int& errorFlag) {
17  std::string modSrc = src;
18  unsigned numReplacements = 0;
19  while (modSrc.find(keyword) != std::string::npos) {
20  modSrc.replace(modSrc.find(keyword), keyword.length(), parameter);
21  ++numReplacements;
22  }
23  if ((numReplacements < minReplacements) || (numReplacements > maxReplacements)) {
24  edm::LogError("replace_string") << " Failed to replace parameter = " << parameter << " in src = " << src << ","
25  << " numReplacements = " << numReplacements << " (min = " << minReplacements
26  << ", max = " << maxReplacements << ") !!";
27  errorFlag = 1;
28  }
29  return modSrc;
30 }
31 
32 //
33 //-----------------------------------------------------------------------------------------------------------------------
34 //
35 
36 std::string format_vstring(const std::vector<std::string>& vs) {
37  std::ostringstream os;
38 
39  os << "{ ";
40 
41  unsigned numEntries = vs.size();
42  for (unsigned iEntry = 0; iEntry < numEntries; ++iEntry) {
43  os << vs[iEntry];
44  if (iEntry < (numEntries - 1))
45  os << ", ";
46  }
47 
48  os << " }";
49 
50  return os.str();
51 }
52 
53 //
54 //-----------------------------------------------------------------------------------------------------------------------
55 //
56 
59  //if ( dirName == "" || dirName.find_last_of(dqmSeparator) != (dirName.length() - 1) ) dirName.append(dqmSeparator);
60  //--- add tailing '/'
61  if (!dirName.empty() && dirName.find_last_of(dqmSeparator) != (dirName.length() - 1))
62  dirName.append(dqmSeparator);
63  return dirName;
64 }
65 
67  std::string subDirName = subdirectory;
68  //--- remove characters specifying directory part from name of subdirectory
69  if (subDirName.find(directory) <= 1)
70  subDirName.replace(subDirName.find(directory), directory.length(), "");
71  //--- remove tailing '/'s
72  while (subDirName.find(dqmSeparator) == 0)
73  subDirName.replace(subDirName.find(dqmSeparator), dqmSeparator.length(), "");
74  return subDirName;
75 }
76 
77 //
78 //-----------------------------------------------------------------------------------------------------------------------
79 //
80 
82  //std::cout << "<dqmRegisterHistogram>:" << std::endl;
83  //histogram->SetName(std::string(histogram->GetName()).append("_copied").data());
84  histogram->SetName(histogram->GetName());
85  if (TH1F* h = dynamic_cast<TH1F*>(histogram)) {
86  //std::cout << " --> calling DQMStore::book1D" << std::endl;
87  dqmStore.book1D(name, h);
88  } else if (TH1S* h = dynamic_cast<TH1S*>(histogram)) {
89  //std::cout << " --> calling DQMStore::book1@" << std::endl;
90  dqmStore.book1S(name, h);
91  } else if (TH2F* h = dynamic_cast<TH2F*>(histogram)) {
92  //std::cout << " --> calling DQMStore::book2D" << std::endl;
93  dqmStore.book2D(name, h);
94  } else if (TH2S* h = dynamic_cast<TH2S*>(histogram)) {
95  //std::cout << " --> calling DQMStore::book2S" << std::endl;
96  dqmStore.book2S(name, h);
97  } else if (TH3F* h = dynamic_cast<TH3F*>(histogram)) {
98  //std::cout << " --> calling DQMStore::book3D" << std::endl;
99  dqmStore.book3D(name, h);
100  } else if (TProfile* h = dynamic_cast<TProfile*>(histogram)) {
101  //std::cout << " --> calling DQMStore::bookProfile" << std::endl;
102  dqmStore.bookProfile(name, h);
103  } else if (TProfile2D* h = dynamic_cast<TProfile2D*>(histogram)) {
104  //std::cout << " --> calling DQMStore::bookProfile2D" << std::endl;
105  dqmStore.bookProfile2D(name, h);
106  }
107 }
108 
110  const std::string& inputDirectory,
111  const std::string& outputDirectory,
112  double scaleFactor,
113  int mode,
114  bool rmInputDirectory) {
115  //std::cout << "<copyRecursively>:" << std::endl;
116  //std::cout << " inputDirectory = " << inputDirectory << std::endl;
117  //std::cout << " outputDirectory = " << outputDirectory << std::endl;
118  //std::cout << " rmInputDirectory = " << rmInputDirectory << std::endl;
119 
120  //--- copy all monitor elements in current inputDirectory to the outputDirectory
121  dqmStore.setCurrentFolder(inputDirectory);
122  std::vector<std::string> meNames = dqmStore.getMEs();
123  for (std::vector<std::string>::const_iterator meName = meNames.begin(); meName != meNames.end(); ++meName) {
124  std::string meName_full = dqmDirectoryName(inputDirectory).append(*meName);
125  //std::cout << " meName_full = " << meName_full << std::endl;
126 
127  dqmStore.setCurrentFolder(inputDirectory);
128  dqm::legacy::MonitorElement* meInput = dqmStore.get(meName_full);
129  //std::cout << " meInput = " << meInput << std::endl;
130  if (!meInput) {
131  edm::LogError("copyRecursively") << " Failed to access meName = " << (*meName) << " in DQMStore"
132  << " --> skipping !!";
133  continue;
134  }
135 
136  TH1* histogram = meInput->getTH1();
137  //std::cout << " histogram = " << histogram << std::endl;
138  if (!histogram) {
139  edm::LogError("copyRecursively") << " Failed to access histogram associated to meName = " << (*meName)
140  << " in DQMStore"
141  << " --> skipping !!";
142  continue;
143  }
144 
145  std::unique_ptr<TH1> clone(dynamic_cast<TH1*>(histogram->Clone()));
146  clone->Scale(scaleFactor);
147 
148  dqmStore.setCurrentFolder(outputDirectory);
149  dqm::legacy::MonitorElement* meOutput = dqmStore.get(dqmDirectoryName(outputDirectory).append(*meName));
150  //std::cout << " meOutput = " << meOutput << std::endl;
151  //--- check if outputHistogram does already exist
152  if (meOutput) {
153  switch (mode) {
154  case 1: // print error message
155  edm::LogError("copyRecursively")
156  << " meName = " << (*meName) << " already exists in outputDirectory = " << outputDirectory
157  << " --> skipping !!";
158  break;
159  case 2: // overwrite outputHistogram
160  dqmRegisterHistogram(dqmStore, clone.release(), *meName);
161  break;
162  case 3: // add histogram to outputHistogram
163  meOutput->getTH1()->Add(clone.get(), scaleFactor);
164  }
165  } else {
166  dqmRegisterHistogram(dqmStore, clone.release(), *meName);
167  }
168  }
169 
170  //--- call function recursively for all sub-directories
171  dqmStore.setCurrentFolder(inputDirectory);
172  std::vector<std::string> dirNames = dqmStore.getSubdirs();
173  for (std::vector<std::string>::const_iterator dirName = dirNames.begin(); dirName != dirNames.end(); ++dirName) {
174  std::string subDirName = dqmSubDirectoryName_merged(inputDirectory, *dirName);
175  //std::cout << " subDirName = " << subDirName << std::endl;
176 
177  std::string inputDirName_full = dqmDirectoryName(inputDirectory).append(subDirName);
178  //std::cout << " inputDirName_full = " << inputDirName_full << std::endl;
179 
180  std::string outputDirName_full = dqmDirectoryName(outputDirectory).append(subDirName);
181  //std::cout << " outputDirName_full = " << outputDirName_full << std::endl;
182 
183  dqmCopyRecursively(dqmStore, inputDirName_full, outputDirName_full, scaleFactor, mode, rmInputDirectory);
184  }
185 }
186 
187 //
188 //-----------------------------------------------------------------------------------------------------------------------
189 //
190 
191 void separateHistogramFromDirectoryName(const std::string& histogramAndDirectoryName,
193  std::string& directoryName) {
194  //std::cout << "<separateHistogramFromDirectoryName>:" << std::endl;
195 
196  std::string tempName = histogramAndDirectoryName;
197 
198  //--- remove DQM root directory from histogram name
199  std::string::size_type dqmRootDirectoryPos = tempName.find(dqmRootDirectory);
200  if (dqmRootDirectoryPos != std::string::npos) {
201  tempName.replace(dqmRootDirectoryPos, dqmRootDirectory.size(), "");
202  }
203 
204  //std::cout << " tempName = " << tempName << std::endl;
205 
206  //--- extract directory from histogram name
207  std::string::size_type lastPos;
208  std::string::size_type nextPos = tempName.find(dqmSeparator);
209  do {
210  lastPos = nextPos;
211  nextPos = tempName.find(dqmSeparator, lastPos + 1);
212  } while (nextPos != std::string::npos);
213 
214  histogramName = (lastPos != std::string::npos) ? std::string(tempName, lastPos + 1, tempName.length()) : tempName;
215  directoryName = (lastPos != std::string::npos) ? std::string(tempName, 0, lastPos) : "";
216 
217  //std::cout << " histogramName = " << histogramName << std::endl;
218  //std::cout << " directoryName = " << directoryName << std::endl;
219 }
dqmRegisterHistogram
void dqmRegisterHistogram(dqm::legacy::DQMStore &dqmStore, TH1 *histogram, const std::string &name)
Definition: dqmAuxFunctions.cc:81
MessageLogger.h
pfMETsysShiftCorrections_cfi.parameter
parameter
Definition: pfMETsysShiftCorrections_cfi.py:118
dqmAuxFunctions.h
ALCARECOPromptCalibProdSiPixelAli0T_cff.mode
mode
Definition: ALCARECOPromptCalibProdSiPixelAli0T_cff.py:96
replace_string
std::string replace_string(const std::string &src, const std::string &keyword, const std::string &parameter, unsigned minReplacements, unsigned maxReplacements, int &errorFlag)
Definition: dqmAuxFunctions.cc:11
DQMStore.h
ECALResponse_cfi.histogramName
histogramName
Definition: ECALResponse_cfi.py:6
dqm::legacy::MonitorElement
Definition: MonitorElement.h:461
trigger::size_type
uint16_t size_type
Definition: TriggerTypeDefs.h:18
parameter
Definition: vlib.h:168
dqm::legacy::DQMStore
Definition: DQMStore.h:727
clone
TEveGeoShape * clone(const TEveElement *element, TEveElement *parent)
Definition: eve_macros.cc:135
dqmSubDirectoryName_merged
std::string dqmSubDirectoryName_merged(const std::string &directory, const std::string &subdirectory)
Definition: dqmAuxFunctions.cc:66
dqmRootDirectory
const std::string dqmRootDirectory
Definition: dqmAuxFunctions.h:38
h
dqmDirectoryName
std::string dqmDirectoryName(const std::string &directory)
Definition: dqmAuxFunctions.cc:57
dqm::legacy::MonitorElement::getTH1
virtual TH1 * getTH1() const
Definition: MonitorElement.h:474
L1TBPTX_cfi.dqmStore
dqmStore
Definition: L1TBPTX_cfi.py:6
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
edm::numEntries
Long64_t numEntries(TFile *hdl, std::string const &trname)
Definition: CollUtil.cc:50
edm::LogError
Definition: MessageLogger.h:183
TrackRefitter_38T_cff.src
src
Definition: TrackRefitter_38T_cff.py:24
dqmCopyRecursively
void dqmCopyRecursively(dqm::legacy::DQMStore &dqmStore, const std::string &inputDirectory, const std::string &outputDirectory, double scaleFactor, int mode, bool rmInputDirectory)
Definition: dqmAuxFunctions.cc:109
personalPlayback.subdirectory
subdirectory
Definition: personalPlayback.py:461
mps_setup.append
append
Definition: mps_setup.py:85
separateHistogramFromDirectoryName
void separateHistogramFromDirectoryName(const std::string &histogramAndDirectoryName, std::string &histogramName, std::string &directoryName)
Definition: dqmAuxFunctions.cc:191
format_vstring
std::string format_vstring(const std::vector< std::string > &vs)
Definition: dqmAuxFunctions.cc:36
createBeamHaloJobs.directory
string directory
Definition: createBeamHaloJobs.py:211
multiplicitycorr_cfi.scaleFactor
scaleFactor
Definition: multiplicitycorr_cfi.py:7
TrackerOfflineValidation_Dqm_cff.dirName
dirName
Definition: TrackerOfflineValidation_Dqm_cff.py:55
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
dqmSeparator
const std::string dqmSeparator
Definition: EwkTauDQM.cc:8